Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using localStorage instead of cookie #3

Closed
madrang opened this issue May 15, 2011 · 2 comments
Closed

Using localStorage instead of cookie #3

madrang opened this issue May 15, 2011 · 2 comments

Comments

@madrang
Copy link

madrang commented May 15, 2011

localStorage is made to store Client code information, Cookie is better use to store information that need to be shared with the server. So i think that to store Open/Selected node the default should be localStorage instead.

/*
 * jsTree storage plugin
 * Stores the currently opened/selected nodes in a localStorage and then restores them
 */
(function ($) {
    $.jstree.plugin("localStorage", {
        __init : function () {
            if(typeof localStorage === "undefined") { throw "jsTree localStorage: localStorage is not supported by your browser."; }

            var s = this._get_settings().localStorage;
            var tmp;

            if(!!s.save_loaded) {
                tmp = localStorage.getItem(s.save_loaded);
                if(tmp && tmp.length) { this.data.core.to_load = tmp.split(","); }
            }
            if(!!s.save_opened) {
                tmp = localStorage.getItem(s.save_opened);
                if(tmp && tmp.length) { this.data.core.to_open = tmp.split(","); }
            }
            if(!!s.save_selected) {
                tmp = localStorage.getItem(s.save_selected);
                if(tmp && tmp.length && this.data.ui) { this.data.ui.to_select = tmp.split(","); }
            }
            this.get_container()
                .one( ( this.data.ui ? "reselect" : "reopen" ) + ".jstree", $.proxy(function () {
                    this.get_container()
                        .bind("open_node.jstree close_node.jstree select_node.jstree deselect_node.jstree", $.proxy(function (e) { 
                                if(this._get_settings().localStorage.auto_save) { this.save_node((e.handleObj.namespace + e.handleObj.type).replace("jstree","")); }
                            }, this));
                }, this));
        },
        defaults : {
            save_loaded     : "jstree_load",
            save_opened     : "jstree_open",
            save_selected   : "jstree_select",
            auto_save       : true,
            localStorage_options    : {}
        },
        _fn : {
            save_node : function (c) {
                if(this.data.core.refreshing) { return; }
                var s = this._get_settings().localStorage;
                if(!c) { // if called manually and not by event
                    if(s.save_loaded) {
                        this.save_loaded();
                        localStorage.setItem(s.save_loaded, this.data.core.to_load.join(","));
                    }
                    if(s.save_opened) {
                        this.save_opened();
                        localStorage.setItem(s.save_opened, this.data.core.to_open.join(","));
                    }
                    if(s.save_selected && this.data.ui) {
                        this.save_selected();
                        localStorage.setItem(s.save_selected, this.data.ui.to_select.join(","));
                    }
                    return;
                }
                switch(c) {
                    case "open_node":
                    case "close_node":
                        if(!!s.save_opened) {
                            this.save_opened(); 
                            localStorage.setItem(s.save_opened, this.data.core.to_open.join(","));
                        }
                        if(!!s.save_loaded) {
                            this.save_loaded(); 
                            localStorage.setItem(s.save_loaded, this.data.core.to_load.join(",")); 
                        }
                        break;
                    case "select_node":
                    case "deselect_node":
                        if(!!s.save_selected && this.data.ui) { 
                            this.save_selected(); 
                            localStorage.setItem(s.save_selected, this.data.ui.to_select.join(",")); 
                        }
                        break;
                }
            }
        }
    });
    // include localStorage by default
    $.jstree.defaults.plugins.push("localStorage");
})(jQuery);

edit: tried to fix code formating issue.
edit2: found how to properly use "GitHub Flavored Markdown"

@vakata
Copy link
Owner

vakata commented May 15, 2011

Thank you for the contribution! I will update the 1.0 branch very soon. I was also thinking about localStorage, but at the time of releasing the cookie plugin browser support was minimal, now things are different. Thank you for the great work. I was thinking of maybe using a minimal library that will support all major browser + a cookie fallback maybe (sorry if this is non-sense - I haven't looked at localStorage for some time now. Once again - thanks! I will release the 1.0 state plugin soon.

@vakata vakata closed this as completed May 15, 2011
@vakata
Copy link
Owner

vakata commented May 16, 2011

I added a helper for local storage (used jstorage), which tries to work with localstorage, globalstorage, userdata (for IE), and finally falls back to cookies.
I also just commited a small state plugin that seems to work, not thoroughly tested though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants