/
jquery.toggle-event.js
187 lines (167 loc) · 4.45 KB
/
jquery.toggle-event.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* jQuery toggleEvent plugin
*
* Loosely based on https://github.com/fkling/jQuery-Function-Toggle-Plugin
*
* @author David Lundgren
* @copyright 2012 David Lundgren <dlundgren@syberisle.net>
* @license MIT
*/
!function($) {
/**
* The manager for toggleEvent
* @param element
* @param options
*/
var toggleManager = function(element, options) {
this.init(element, options);
};
/**
* triggers the event
* @param event
*/
function apply(event) {
$(this).toggleEvent('trigger', event);
}
/**
* The prototype for the toggle manager
* @type {{constructor: Function, init: Function, enable: Function, disable: Function, trigger: Function, bind: Function, unbind: Function}}
*/
toggleManager.prototype = {
constructor: toggleManager,
/**
* Initializes the toggle manager
* @param element
* @param options
*/
init : function (element, options) {
this.options = options;
this.events = {};
this.eventStatus = {};
this.$element = $(element);
for (var key in this.options.functions) {
this.bind(key, this.options.functions);
}
},
/**
* Enables the given type
* @param type
*/
enable : function(type) {
this.eventStatus[type] = true;
},
/**
* Disables the given type
* @param type
*/
disable : function (type) {
this.eventStatus[type] = false;
},
/**
* Triggers the type based on the event if it is enabled
* @param event
*/
trigger : function (event) {
if (this.eventStatus[event.type]) {
var dataAttr = 'data-toggle-' + event.type + this.events[event.type],
index = parseInt(this.$element.attr(dataAttr)),
count = this.options.functions[event.type].length;
this.options.functions[event.type][index].call(this.$element, event);
this.$element.attr(dataAttr, (index + 1) % count);
}
},
/**
* Binds a groups of function to the given type
*
* @param type
* @param functions
*/
bind : function (type, functions) {
if (this.events[type]) {
throw new Error("Type " + type + " already bound");
}
this.events[type] = (new Date()).getTime();
this.eventStatus[type] = true;
this.$element.on(type, apply);
this.$element.attr('data-toggle-' + type + this.events[type], 0);
},
/**
* Unbinds the given type from the element
*
* @param type
*/
unbind : function (type) {
if (type) {
this.$element.off(type, apply);
}
else {
// unbind everything
for (var key in this.options.functions) {
this.$element.off(key, apply);
}
}
}
};
/**
* jQuery plugin for toggling events
*
* @param option
* @returns {*}
*/
$.fn.toggleEvent = function(option) {
var functions = Array.prototype.slice.call(arguments, 1);
return this.each(function () {
var $this = $(this),
numFunc = functions.length,
data = $this.data('toggle-event'),
options = $.extend({}, $.fn.toggleEvent.defaults, $this.data(), typeof option == 'object' && option);
if (typeof option == 'string' && (option != 'trigger' && option != 'enable' && option != 'disable' && option !== 'unbind')) {
var funcs = {};
funcs[option] = functions;
options['functions'] = $.extend({}, options['functions'], funcs);
}
if (!data) {
$this.data('toggle-event', (data = new toggleManager(this, options)));
}
else if (typeof option == 'string') {
if (option == 'trigger') {
if (numFunc != 1) {
throw new Error("toggleEvent::" + option + " requires one argument");
}
else if (typeof functions[0] != 'object') {
throw new Error("toggleEvent::" + option + " first argument must be the event object");
}
data.trigger(functions[0]);
}
else if (option == 'enable' || option == 'disable' || option == 'unbind') {
if (numFunc != 1) {
throw new Error("toggleEvent::" + option + " requires one argument");
}
else if (typeof functions[0] != 'string') {
throw new Error("toggleEvent::" + option + " argument must be a string");
}
data[option](functions[0]);
}
else if (option == 'bind') {
throw new Error("toggleEvent::bind is not a valid method");
}
else {
// we just bind everything else
data.bind(option, functions);
}
}
})
};
/**
* The defaults for the toggleEvent
* @type {{functions: {}}}
*/
$.fn.toggleEvent.defaults = {
functions : {}
};
/**
* The constructor
* @type {Function}
*/
$.fn.toggleEvent.Constructor = toggleManager;
}(window.jQuery);