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

Feature: add cookie for menu state #5

Open
lenamtl opened this issue Jun 3, 2014 · 16 comments
Open

Feature: add cookie for menu state #5

lenamtl opened this issue Jun 3, 2014 · 16 comments

Comments

@lenamtl
Copy link

lenamtl commented Jun 3, 2014

Are you planning to add a way to remember menu state using cookie
or localstrorage?

@onokumus
Copy link
Owner

onokumus commented Jun 3, 2014

@lenamtl
I'm not planning to this in near future.

@valgen
Copy link

valgen commented Jun 12, 2014

I have added a basic cookie support as I needed it for my open source CMS (http://www.mvcwcms.com).

If you have more than one menu in a single page you need to specify the cookieName attribute as in the following example:

        $('#menu').metisMenu({
            cookieName: "MetisMenuState1"
        });
        $('#menu2').metisMenu({
            toggle: false,
            cookieName: "MetisMenuState2"
        });

This is the modified plugin:

;(function ($, window, document, undefined) {

    var pluginName = "metisMenu",
        defaults = {
            toggle: true,
            cookieName: "MetisMenuState"
        };

    function Plugin(element, options) {
        this.element = element;
        this.settings = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }

    Plugin.prototype = {
        init: function () {

            var $this = $(this.element),
                $toggle = this.settings.toggle,
                $cookieName = this.settings.cookieName;

            //Restores the menu state from cookies
            var documentCookie = document.cookie;
            $this.find('li').has('ul').each(function (i) {
                var pos = documentCookie.indexOf($cookieName + "_" + i + "=");
                if (pos > -1) {
                    documentCookie.substr(pos).split('=')[1].indexOf('false') ? $(this).addClass('active') : $(this).removeClass('active');
                }
            });

            if (this.isIE() <= 9) {
                $this.find('li.active').has('ul').children('ul').collapse('show');
                $this.find('li').not('.active').has('ul').children('ul').collapse('hide');
            } else {
                $this.find('li.active').has('ul').children('ul').addClass('collapse in');
                $this.find('li').not('.active').has('ul').children('ul').addClass('collapse');
            }

            $this.find('li').has('ul').children('a').on('click', function (e) {
                e.preventDefault();

                $(this).parent('li').toggleClass('active').children('ul').collapse('toggle');

                if ($toggle) {
                    $(this).parent('li').siblings().removeClass('active').children('ul.in').collapse('hide');
                }

                //Stores the menu state in cookies
                $this.find('li').has('ul').each(function (i) {
                    document.cookie = $cookieName + "_" + i + "=" + $(this).hasClass('active');
                });
            });
        },

        isIE: function() {//https://gist.github.com/padolsey/527683
            var undef,
                v = 3,
                div = document.createElement('div'),
                all = div.getElementsByTagName('i');

            while (
                div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
                all[0]
            );
            return v > 4 ? v : undef;
        }
    };

    $.fn[ pluginName ] = function (options) {
        return this.each(function () {
            if (!$.data(this, "plugin_" + pluginName)) {
                $.data(this, "plugin_" + pluginName, new Plugin(this, options));
            }
        });
    };

})(jQuery, window, document);

@onokumus onokumus mentioned this issue Jun 24, 2014
@zaki1
Copy link

zaki1 commented Jun 25, 2014

I have two or more menus as below :

And as you suggested I am giving each menu an id and passing a cookie name :

 
        (function ($) {
         $('#artistMenu').metisMenu({
                cookieName: "MetisMenuState1"
            });
            $('#lyricsMenu').metisMenu({
                toggle: false,
                cookieName: "MetisMenuState2"
            });
        })(jQuery);

But when I click on "Add Artist" sub menu the first time it is fine, then open "Manage lyrics" and click on "All lyrics" the view changes but menu would be still "Add Lyrics"...if I click once more it seems to be fine..any solutions?

valgen added a commit to valgen/metisMenu that referenced this issue Jun 28, 2014
Added cookie support as specified here: onokumus#5
@valgen valgen mentioned this issue Jun 28, 2014
@valgen
Copy link

valgen commented Jun 28, 2014

I have fixed the above issue by adding a cookie expiration.

Change has been submitted here: https://github.com/onokumus/metisMenu/pull/10/files

Now you can also specify the cookieExpiration (number of days) that by default is 1.

@mwinandy
Copy link

mwinandy commented Nov 4, 2014

Great feature added ;) thanks

@rygel
Copy link

rygel commented Dec 18, 2014

@valgen thanks for the great feature.
@onokumus please add this to your release.

@FreeAsInBeer
Copy link

@valgen Thanks! Works great.

@lenamtl
Copy link
Author

lenamtl commented Jan 13, 2015

Thanks

@imshenitsky
Copy link

Works great. Why it's not in master?

@mteichtahl
Copy link

I would like to see this in master perhaps 2.0.1 ASAP

@lenamtl
Copy link
Author

lenamtl commented May 5, 2015

+1 Yes please add it to the latest version.

I'm trying to use this code with the actual JS code but the JS code is different, anyone have done the modification yet?

@mteichtahl
Copy link

You will need to patch this manually.

Sent from my iPhone

On 6 May 2015, at 05:04, lenamtl notifications@github.com wrote:

+1 Yes please add it to the latest version.

I'm trying to use this code with the actual JS code but the JS code is different, anyone have done the modification yet?


Reply to this email directly or view it on GitHub.

@Webkungen
Copy link

Any change someone can add the feature to "find" the current selected item by location/url rather than with cookies?

Thanks!

@amastaneh
Copy link

+1

@3xxx
Copy link

3xxx commented May 22, 2016

how to use this plugin?

@esfomeado
Copy link

Any update?

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