vpn.email
Version:
vpn.email client
148 lines (126 loc) • 4.99 kB
JavaScript
$.widget("metro.dropdown", {
version: "3.0.0",
options: {
effect: window.METRO_SHOW_TYPE,
toggleElement: false,
noClose: false,
onDrop: function(object){},
onUp: function(object){}
},
_create: function(){
var that = this, element = this.element, o = this.options,
menu = this.element,
name = this.name,
parent = this.element.parent();
var toggle;
$.each(element.data(), function(key, value){
if (key in o) {
try {
o[key] = $.parseJSON(value);
} catch (e) {
o[key] = value;
}
}
});
toggle = o.toggleElement ? $(o.toggleElement) : parent.children('.dropdown-toggle').length > 0 ? parent.children('.dropdown-toggle') : parent.children('a:nth-child(1)');
if (METRO_SHOW_TYPE !== undefined) {
this.options.effect = METRO_SHOW_TYPE;
}
toggle.on('click.'+name, function(e){
parent.siblings(parent[0].tagName).removeClass("active-container");
$(".active-container").removeClass("active-container");
if (menu.css('display') === 'block' && !menu.hasClass('keep-open')) {
that._close(menu);
} else {
$('[data-role=dropdown]').each(function(i, el){
if (!menu.parents('[data-role=dropdown]').is(el) && !$(el).hasClass('keep-open') && $(el).css('display') === 'block') {
that._close(el);
}
});
if (menu.hasClass('horizontal')) {
menu.css({
'visibility': 'hidden',
'display': 'block'
});
var item_length = $(menu.children('li')[0]).outerWidth();
//var item_length2 = $(menu.children('li')[0]).width();
menu.css({
'visibility': 'visible',
'display': 'none'
});
var menu_width = menu.children('li').length * item_length + (menu.children('li').length - 1);
menu.css('width', menu_width);
}
that._open(menu);
parent.addClass("active-container");
}
e.preventDefault();
e.stopPropagation();
});
if (o.noClose === true) {
element.on('click', function (e) {
// e.preventDefault();
e.stopPropagation();
});
}
$(menu).find('li.disabled a').on('click', function(e){
e.preventDefault();
});
element.data('dropdown', this);
},
_open: function(el){
var parent = this.element.parent(), o = this.options;
var toggle = o.toggleElement ? $(o.toggleElement) : parent.children('.dropdown-toggle').length > 0 ? parent.children('.dropdown-toggle') : parent.children('a:nth-child(1)');
switch (this.options.effect) {
case 'fade': $(el).fadeIn('fast'); break;
case 'slide': $(el).slideDown('fast'); break;
default: $(el).show();
}
this._trigger("onOpen", null, el);
toggle.addClass('active-toggle');
if (typeof o.onDrop === 'function') {
o.onDrop(el);
} else {
if (typeof window[o.onDrop] === 'function') {
window[o.onDrop](el);
} else {
var result = eval("(function(){"+o.onDrop+"})");
result.call(el);
}
}
},
_close: function(el){
var parent = $(el).parent(), o = this.options;
var toggle = o.toggleElement ? $(o.toggleElement) : parent.children('.dropdown-toggle').length > 0 ? parent.children('.dropdown-toggle') : parent.children('a:nth-child(1)');
switch (this.options.effect) {
case 'fade': $(el).fadeOut('fast'); break;
case 'slide': $(el).slideUp('fast'); break;
default: $(el).hide();
}
this._trigger("onClose", null, el);
toggle.removeClass('active-toggle');
if (typeof o.onUp === 'function') {
o.onUp(el);
} else {
if (typeof window[o.onUp] === 'function') {
window[o.onUp](el);
} else {
var result = eval("(function(){"+o.onUp+"})");
result.call(el);
}
}
},
_destroy: function(){
},
_setOption: function(key, value){
this._super('_setOption', key, value);
}
});
$(document).on('click', function(e){
$('[data-role=dropdown]').each(function(i, el){
if (!$(el).hasClass('keep-open') && $(el).css('display')==='block') {
var that = $(el).data('dropdown');
that._close(el);
}
});
});