history

webix.history

webix.history helper.

Please look into the linked official documentation.

References

helpers
event(), isUndefined().

External references

Official documentation page.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
webix.history = {
    track:function(id, url){
        this._init_state(id, url);

        var view = webix.$$(id);

        var handler = function(){
            if (webix.history._ignored) return;

            if (view.getValue)
                webix.history.push(id, view.getValue());
        };

        if (view.getActiveId)
            view.attachEvent("onViewChange", handler);
        else
            view.attachEvent("onChange", handler);
    },
    _set_state:function(view, state){
        webix.history._ignored = 1;

        view = webix.$$(view);
        if (view.callEvent("onBeforeHistoryNav", [state]))
            if (view.setValue)
                view.setValue(state);

        webix.history._ignored = 0;
    },
    push:function(view, url, value){
        view = webix.$$(view);
        var new_url = "";
        if (url)
            new_url = "#!/"+url;
        if (webix.isUndefined(value)){
            if (view.getValue)
                value = view.getValue();
            else
                value = url;
        }

        window.history.pushState({ webix:true, id:view._settings.id, value:value }, "", new_url);
    },
    _init_state:function(view, url){
        webix.event(window, "popstate", function(ev){
            if (ev.state && ev.state.webix){
                webix.history._set_state(ev.state.id, ev.state.value);
            }
        });

        var state = window.location.hash;
        webix.noanimate = true;
        if (state && state.indexOf("#!/") === 0)
            webix.history._set_state(view, state.replace("#!/",""));
        else if (url){
            webix.history.push(view, url);
            webix.history._set_state(view, url);
        }
        webix.noanimate = false;

        this._init_state = function(){};
    }
};