-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjqueryui-init.js
270 lines (227 loc) · 6.87 KB
/
jqueryui-init.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*!
* jQueryUI Init v0.0.4
* https://github.com/sash/jqueryui-init/
*
* Copyright 2010, Alexander Alexiev
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function($){
var rclass = /[\n\t\r]/g,
rspaces = /\s+/,
rreturn = /\r/g,
rspecialurl = /^(?:href|src|style)$/,
rtype = /^(?:button|input)$/i,
rfocusable = /^(?:button|input|object|select|textarea)$/i,
rclickable = /^a(?:rea)?$/i,
rradiocheck = /^(?:radio|checkbox)$/i;
$.extend($.fn, {
'attrs':function(prefix, list){
if (list){
var list = [];
$(this).each(function() {
var attributes = [];
for(var key in this.attributes) {
if(!isNaN(key)) {
if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
attributes.push(this.attributes[key].name);
}
}
}
list.push(attributes);
});
return (list.length > 1 ? list : list[0]);
}
else{
var res={};
var that = $(this)
$.each(that.attrs(prefix, true), function(i, v){
var k = v.substr(prefix.length).split('-')
var t = res
var l = false
for(var i in k){
if (k[i].length>0){
if (l) {if (typeof t[l] == 'undefined')t[l]={}; t = t[l]}
l = k[i]
}
}
var set = parseInt(that.attr(v))+'' == that.attr(v) ? parseInt(that.attr(v)) : ( parseFloat(that.attr(v))+'' == that.attr(v) ? parseFloat(that.attr(v)) : that.attr(v) )
t[l]=set;
})
return res
}
},
addUi: function( value ) {
if ( jQuery.isFunction(value) ) {
return this.each(function(i) {
var self = jQuery(this);
self.addClass( value.call(this, i, self.attr("data-ui")) );
});
}
if ( value && typeof value === "string" ) {
var classNames = (value || "").split( rspaces );
for ( var i = 0, l = this.length; i < l; i++ ) {
var elem = $(this[i]);
if ( !elem.attr('data-ui') ) {
elem.attr('data-ui', value);
} else {
var className = " " + elem.attr('data-ui') + " ",
setClass = elem.attr('data-ui');
for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
setClass += " " + classNames[c];
}
}
elem.attr("data-ui", jQuery.trim( setClass ));
}
//TODO: Automagically initialize the component. Livequery only takes into account appearing and disappaering of the attr, not changes
}
}
return this;
},
removeUi: function( value ) {
if ( jQuery.isFunction(value) ) {
return this.each(function(i) {
var self = jQuery(this);
self.removeClass( value.call(this, i, self.attr("data-ui")) );
});
}
if ( (value && typeof value === "string") || value === undefined ) {
var classNames = (value || "").split( rspaces ), uninit = value;
for ( var i = 0, l = this.length; i < l; i++ ) {
var elem = $(this[i]);
if ( elem.attr('data-ui') ) {
if ( value ) {
var className = (" " + elem.attr('data-ui') + " ").replace(rclass, " ");
for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
className = className.replace(" " + classNames[c] + " ", " ");
}
var set = jQuery.trim( className )
if (set)
elem.attr('data-ui', set);
else elem.removeAttr('data-ui')
} else {
uninit = elem.attr('data-ui');
elem.removeAttr("data-ui");
}
}
}
}
return this;
},
toggleUi: function( value, stateVal ) {
var type = typeof value,
isBool = typeof stateVal === "boolean";
if ( jQuery.isFunction( value ) ) {
return this.each(function(i) {
var self = jQuery(this);
self.toggleUi( value.call(this, i, self.attr("data-ui"), stateVal), stateVal );
});
}
return this.each(function() {
if ( type === "string" ) {
// toggle individual class names
var className,
i = 0,
self = jQuery( this ),
state = stateVal,
classNames = value.split( rspaces );
while ( (className = classNames[ i++ ]) ) {
// check each className given, space seperated list
state = isBool ? state : !self.hasUi( className );
self[ state ? "addUi" : "removeUi" ]( className );
}
} else if ( type === "undefined" || type === "boolean" ) {
// toggle individual class names
var
self = jQuery( this );
if ( this.attr('data-ui') ) {
// store className if set
jQuery._data( this, "__dataUi__", self.attr('data-ui') );
}
// toggle whole className
self.attr('data-ui', self.attr('data-ui') || value === false ? "" : jQuery._data( this, "__dataUi__" ) || "") ;
}
});
},
hasUi: function( selector ) {
var className = " " + selector + " ";
for ( var i = 0, l = this.length; i < l; i++ ) {
if ( (" " + $(this[i]).attr('data-ui') + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
return true;
}
}
return false;
},
'_uiinit':function(element){
// Prevent dual init
if (!$(this).attr('data-ui-'+element) && typeof $.fn[element] == 'function'){
$(this).attr('data-ui-'+element, element)
// Initialize the widget
eval('$(this).'+element+'($(this).attrs("data-ui-'+element+'-"))')
}
return this
},
'_uidestroy':function(element){
// Prevent dual destroy
if ($(this).attr('data-ui-'+element)){
$(this).removeAttr('data-ui-'+element)
// Destroy the widget
eval('$(this).'+element+'("destroy")')
}
return this
},
'uiinit':function(element){
var el = this, inits = []
if (typeof element != 'undefined'){
inits = [element]
}
else {
inits = ($(this).attr('data-ui')||"").split(' ');
}
$.each(inits, function(v, i){
if (i) $(el)._uiinit(i)
})
this.trigger('uiinit');
return this
},
'uidestroy':function(element){
var el = this, inits = []
if (typeof element != 'undefined'){
inits = [element]
}
else {
inits = ($(this).attr('data-ui')||"").split(' ');
}
$.each(inits, function(v, i){
if (i) $(el)._uidestroy(i)
})
this.trigger('uidestroy');
return this
}
})
if (typeof $.fn.livequery == 'function'){
var widget_save = $.widget;
var widget_bridge_save = $.widget.bridge;
$.widget = function( name, base, prototype ){
widget_save(name, base, prototype)
var w = name.split('.')[1]
$('*[data-ui~="'+w+'"]').livequery(function(){
$(this).uiinit(w);
}, function (){
$(this).uidestroy(w);
})
}
$.widget.bridge = widget_bridge_save
$.each('button buttonset accordion datepicker dialog progressbar slider tabs draggable droppable resizable selectable sortable'.split(' '), function (i, w){
// Register the initialization handler
$('*[data-ui~="'+w+'"]').livequery(function(){
$(this).uiinit(w);
}, function (){
$(this).uidestroy(w);
})
})
}
else{
throw 'jqueryui-init: livequery is not included'
}
})(jQuery)