root / snf-cyclades-app / synnefo / ui / new_ui / ui / vendor / foundation / foundation.dropdown.js @ faad3c72
History | View | Annotate | Download (3.4 kB)
1 |
/*jslint unparam: true, browser: true, indent: 2 */
|
---|---|
2 |
|
3 |
;(function ($, window, document, undefined) { |
4 |
'use strict';
|
5 |
|
6 |
Foundation.libs.dropdown = { |
7 |
name : 'dropdown', |
8 |
|
9 |
version : '4.0.9', |
10 |
|
11 |
settings : {
|
12 |
activeClass: 'open' |
13 |
}, |
14 |
|
15 |
init : function (scope, method, options) { |
16 |
this.scope = scope || this.scope; |
17 |
Foundation.inherit(this, 'throttle'); |
18 |
|
19 |
if (typeof method === 'object') { |
20 |
$.extend(true, this.settings, method); |
21 |
} |
22 |
|
23 |
if (typeof method != 'string') { |
24 |
|
25 |
if (!this.settings.init) { |
26 |
this.events();
|
27 |
} |
28 |
|
29 |
return this.settings.init; |
30 |
} else {
|
31 |
return this[method].call(this, options); |
32 |
} |
33 |
}, |
34 |
|
35 |
events : function () { |
36 |
var self = this; |
37 |
|
38 |
$(this.scope).on('click.fndtn.dropdown', '[data-dropdown]', function (e) { |
39 |
e.preventDefault(); |
40 |
e.stopPropagation(); |
41 |
self.toggle($(this)); |
42 |
}); |
43 |
|
44 |
$('*, html, body').on('click.fndtn.dropdown', function (e) { |
45 |
if (!$(e.target).data('dropdown')) { |
46 |
$('[data-dropdown-content]') |
47 |
.css('left', '-99999px') |
48 |
.removeClass(self.settings.activeClass); |
49 |
} |
50 |
}); |
51 |
|
52 |
$(window).on('resize.fndtn.dropdown', self.throttle(function () { |
53 |
self.resize.call(self); |
54 |
}, 50)).trigger('resize'); |
55 |
|
56 |
this.settings.init = true; |
57 |
}, |
58 |
|
59 |
toggle : function (target, resize) { |
60 |
var dropdown = $('#' + target.data('dropdown')); |
61 |
|
62 |
$('[data-dropdown-content]').not(dropdown).css('left', '-99999px').removeClass(this.settings.activeClass); |
63 |
|
64 |
if (dropdown.hasClass(this.settings.activeClass)) { |
65 |
dropdown |
66 |
.css('left', '-99999px') |
67 |
.removeClass(this.settings.activeClass);
|
68 |
} else {
|
69 |
this
|
70 |
.css(dropdown |
71 |
.addClass(this.settings.activeClass), target);
|
72 |
} |
73 |
}, |
74 |
|
75 |
resize : function () { |
76 |
var dropdown = $('[data-dropdown-content].open'), |
77 |
target = $("[data-dropdown='" + dropdown.attr('id') + "']"); |
78 |
|
79 |
if (dropdown.length && target.length) {
|
80 |
this.css(dropdown, target);
|
81 |
} |
82 |
}, |
83 |
|
84 |
css : function (dropdown, target) { |
85 |
if (dropdown.parent()[0] === $('body')[0]) { |
86 |
var position = target.offset();
|
87 |
} else {
|
88 |
var position = target.position();
|
89 |
} |
90 |
|
91 |
if (this.small()) { |
92 |
dropdown.css({ |
93 |
position : 'absolute', |
94 |
width: '95%', |
95 |
left: '2.5%', |
96 |
'max-width': 'none', |
97 |
top: position.top + this.outerHeight(target) |
98 |
}); |
99 |
} else {
|
100 |
if ($(window).width() > this.outerWidth(dropdown) + target.offset().left) { |
101 |
var left = position.left;
|
102 |
} else {
|
103 |
if (!dropdown.hasClass('right')) { |
104 |
dropdown.addClass('right');
|
105 |
} |
106 |
var left = position.left - (this.outerWidth(dropdown) - this.outerWidth(target)); |
107 |
} |
108 |
dropdown.attr('style', '').css({ |
109 |
position : 'absolute', |
110 |
top: position.top + this.outerHeight(target), |
111 |
left: left
|
112 |
}); |
113 |
} |
114 |
|
115 |
return dropdown;
|
116 |
}, |
117 |
|
118 |
small : function () { |
119 |
return $(window).width() < 768 || $('html').hasClass('lt-ie9'); |
120 |
}, |
121 |
|
122 |
off: function () { |
123 |
$(this.scope).off('.fndtn.dropdown'); |
124 |
$('html, body').off('.fndtn.dropdown'); |
125 |
$(window).off('.fndtn.dropdown'); |
126 |
$('[data-dropdown-content]').off('.fndtn.dropdown'); |
127 |
this.settings.init = false; |
128 |
} |
129 |
}; |
130 |
}(Foundation.zj, this, this.document)); |