-
Notifications
You must be signed in to change notification settings - Fork 859
/
Copy pathbuttons.js
115 lines (95 loc) · 3.33 KB
/
buttons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*! @license
* Project: Buttons
* Description: A highly customizable CSS button library built with Sass and Compass
* Author: Alex Wolfe and Rob Levin
* License: Apache License v2.0
*/
// the semi-colon before function invocation is a safety net against concatenated
// scripts and/or other plugins which may not be closed properly.
;(function ( $, window, document, undefined ) {
'use strict';
// undefined is used here as the undefined global variable in ECMAScript 3 is
// mutable (ie. it can be changed by someone else). undefined isn't really being
// passed in so we can ensure the value of it is truly undefined. In ES5, undefined
// can no longer be modified.
// window and document are passed through as local variable rather than global
// as this (slightly) quickens the resolution process and can be more efficiently
// minified (especially when both are regularly referenced in your plugin).
// Create the defaults once
var pluginName = "menuButton";
var menuClass = ".button-dropdown";
var defaults = {
propertyName: "value"
};
// The actual plugin constructor
function Plugin( element, options ) {
//SET OPTIONS
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
//REGISTER ELEMENT
this.$element = $(element);
//INITIALIZE
this.init();
}
Plugin.prototype = {
constructor: Plugin,
init: function() {
// WE DON'T STOP PROPGATION SO CLICKS WILL AUTOMATICALLY
// TOGGLE AND REMOVE THE DROPDOWN
this.toggle();
},
toggle: function(el, options) {
if(this.$element.data('dropdown') === 'show') {
this.hideMenu();
}
else {
this.showMenu();
}
},
showMenu: function() {
this.$element.data('dropdown', 'show');
this.$element.find('ul').show();
this.$element.find('.button:first').addClass('is-active');
},
hideMenu: function() {
this.$element.data('dropdown', 'hide');
this.$element.find('ul').hide();
this.$element.find('.button:first').removeClass('is-active');
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
// TOGGLE BUTTON IF IT EXISTS
if ($.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName).toggle();
}
// OTHERWISE CREATE A NEW INSTANCE
else {
$.data(this, "plugin_" + pluginName, new Plugin( this, options ));
}
});
};
//CLOSE OPEN DROPDOWN MENUS IF CLICKED SOMEWHERE ELSE
$(document).on('click', function(e) {
$.each($('[data-buttons=dropdown]'), function(i, value) {
if ($(e.target.offsetParent)[0] != $(this)[0]) {
if ($.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName).hideMenu();
$(this).find('ul').hide();
}
}
});
});
//DELEGATE CLICK EVENT FOR DROPDOWN MENUS
$(document).on('click', '[data-buttons=dropdown]', function(e) {
var $dropdown = $(e.currentTarget);
$dropdown.menuButton();
});
//IGNORE CLICK EVENTS FROM DISPLAY BUTTON IN DROPDOWN
$(document).on('click', '[data-buttons=dropdown] > a', function(e) {
e.preventDefault();
});
})( jQuery, window, document);