/*! =========================================================
* Sliding Menu v0.3.0
* http://github.danielcardoso.net/sliding-menu/
* ==========================================================
* Copyright (c) 2014-2019 DanielCardoso.net.
* Licensed under MIT.
* ======================================================== */
if (typeof jQuery === 'undefined') {
throw new Error('Sliding Menu requires jQuery');
}
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function ($) {
'use strict';
var slidingMenuUsedIds, SlidingMenu;
slidingMenuUsedIds = [];
SlidingMenu = function (element, options) {
this.options = undefined;
this.$el = undefined;
this.currentPanel = undefined;
this.init(element, options);
};
SlidingMenu.NAME = 'Sliding Menu';
SlidingMenu.VERSION = '0.3.0';
SlidingMenu.MAIN_CLASS = 'sliding-menu';
SlidingMenu.PANEL_CLASS = SlidingMenu.MAIN_CLASS + '__panel';
SlidingMenu.ICON_CLASS = SlidingMenu.MAIN_CLASS + '__icon';
SlidingMenu.NAVIGATION_CLASS = SlidingMenu.MAIN_CLASS + '__nav';
SlidingMenu.BACK_CLASS = SlidingMenu.MAIN_CLASS + '__back';
SlidingMenu.SEPARATOR_CLASS = SlidingMenu.MAIN_CLASS + '__separator';
SlidingMenu.SET_ICON_CLASS = 'sm-set-icon';
SlidingMenu.DEFAULTS = {
// Adicional class for menu element
className: '',
// Default slide animation speed
transitionDuration: 250,
// A JSON object to build the menu from. Check our JSON example.
dataJSON: false,
// The link to the selected panel. Set to false to use the root panel
initHref: false,
// Label for the back button. Set to true to use the link's own label
backLabel: 'Back'
};
SlidingMenu.prototype.init = function (element, options) {
this.$el = $(element);
if (this.$el.hasClass(SlidingMenu.MAIN_CLASS)) {
return;
}
this.options = this.getOptions(options);
this.events();
this.process();
};
SlidingMenu.prototype.$ = function (selector) {
return this.$el.find(selector);
};
SlidingMenu.prototype.events = function () {
this.$el.on('click', 'a, .' + SlidingMenu.NAVIGATION_CLASS, $.proxy(this._onClickItem, this));
};
SlidingMenu.prototype._onClickItem = function (event) {
var linker, targetPanel, movePanelTo;
linker = $(event.currentTarget);
if (linker.attr('data-id') !== undefined) {
movePanelTo = linker.hasClass(SlidingMenu.BACK_CLASS);
targetPanel = this.$('.' + SlidingMenu.PANEL_CLASS + '[data-id="' + linker.attr('data-id') + '"]');
if (this.currentPanel.attr('data-id') !== targetPanel.attr('data-id')) {
this.currentPanel.stop(true, true).animate({
left: movePanelTo ? '100%' : '-100%'
}, this.options.transitionDuration);
targetPanel.stop(true, true).css('left', movePanelTo ? '-100%' : '100%').animate({
left: 0
}, this.options.transitionDuration);
this.$el.stop(true, true).animate({
height: targetPanel.height()
}, this.options.transitionDuration);
} else {
targetPanel.css({
'left': 0
});
this.$el.height(targetPanel.height());
}
this.currentPanel = targetPanel;
}
if (!linker.hasClass(SlidingMenu.NAVIGATION_CLASS)) {
this.$('li.active').removeClass('active');
linker.closest('li').addClass('active');
}
};
SlidingMenu.prototype.process = function () {
var data;
if (this.options.dataJSON === true) {
data = this.processJSON(this.options.dataJSON);
} else {
data = this.processHTML();
}
this.setMenuContent(data);
};
SlidingMenu.prototype.processJSON = function (data, parent, backLabel) {
var root, panels;
root = {
id: SlidingMenu.PANEL_CLASS + '-' + this.getNewId(),
root: parent ? false : true,
children: []
};
panels = [];
if (parent) {
root.children.push({
panelId: parent.id,
href: false,
label: this.options.backLabel === true ? backLabel : this.options.backLabel,
_styleClass: SlidingMenu.BACK_CLASS + ' ' + SlidingMenu.NAVIGATION_CLASS
});
}
$(data).each($.proxy(function (index, item) {
var panel;
root.children.push(item);
if (item.children) {
panel = this.processJSON(item.children, root, item.label);
item.panelId = panel[0].id;
item._styleClass = SlidingMenu.NAVIGATION_CLASS;
panels = panels.concat(panel);
// Delete all childrens
delete item.children;
}
}, this));
return [root].concat(panels);
};
SlidingMenu.prototype.processHTML = function (parentElem, parentObj, backLabel) {
var root, panels;
root = {
id: SlidingMenu.PANEL_CLASS + '-' + this.getNewId(),
root: parentElem ? false : true,
children: []
};
panels = [];
if (parentElem !== undefined) {
root.children.push({
panelId: parentObj.id,
href: false,
label: this.options.backLabel === true ? backLabel : this.options.backLabel,
_styleClass: SlidingMenu.BACK_CLASS + ' ' + SlidingMenu.NAVIGATION_CLASS
});
} else {
parentElem = this.$el.children('ul');
}
parentElem.children('li').each($.proxy(function (key, item) {
var itemObj, itemLink, panel, subPanel;
item = $(item);
if (!item.hasClass('separator')) {
itemLink = item.children('a');
itemObj = {
icon: itemLink.find('.' + SlidingMenu.SET_ICON_CLASS).attr('class') || undefined,
href: itemLink.attr('href'),
label: itemLink.html(),
classNames: {
parent: this.trimWhiteSpaces(item.attr('class') || ''),
element: this.trimWhiteSpaces(itemLink.attr('class') || '')
}
};
if (itemObj.icon !== undefined) {
itemObj.icon = (itemObj.icon).replace(SlidingMenu.SET_ICON_CLASS, '');
}
subPanel = item.children('ul');
if (subPanel.length !== 0) {
panel = this.processHTML(subPanel, root, itemObj.label);
itemObj.panelId = panel[0].id;
itemObj._styleClass = SlidingMenu.NAVIGATION_CLASS;
panels = panels.concat(panel);
}
} else {
itemObj = {
separator: true
};
}
root.children.push(itemObj);
}, this));
return [root].concat(panels);
};
SlidingMenu.prototype.setMenuContent = function (json) {
var rootPanel;
this.$el
.empty()
.addClass(SlidingMenu.MAIN_CLASS + ' ' + this.options.className);
$(json).each($.proxy(function (index, item) {
var panel;
panel = $('');
if (item.root) {
rootPanel = '.' + SlidingMenu.PANEL_CLASS + '[data-id="' + item.id + '"]';
}
// panel.attr('id', item.id);
panel.attr('data-id', item.id);
panel.addClass(SlidingMenu.PANEL_CLASS);
$(item.children).each(function (index, item) {
var li, link, icon;
li = $('- ');
if (item.separator !== true) {
link = item.panelId ? $('