-
Notifications
You must be signed in to change notification settings - Fork 0
/
uxrocket.clear.js
340 lines (270 loc) · 9.46 KB
/
uxrocket.clear.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* UX Rocket
* jQuery based clear input/textarea action handler
* @author Bilal Cinarli
*/
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object' && typeof require === 'function') {
// Browserify
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
'use strict';
var ux, // local shorthand
i = 1,
rocketName = 'uxrClear',
defaults = {
icon: 'icon-cross',
clearAlso: null,
// callbacks
onReady: false,
onClear: false,
onUpdate: false,
onRemove: false
},
events = {
click: 'click.' + rocketName,
focus: 'focus.' + rocketName,
input: 'input.' + rocketName,
keyup: 'keyup.' + rocketName,
paste: 'paste.' + rocketName
},
ns = {
prefix: 'uxr-',
rocket: 'uxRocket',
data: rocketName,
name: 'clear',
wrap: 'uxr-plugin-wrap',
classes: {
wrap: 'wrap',
ready: 'ready',
icon: 'icon',
visible: 'icon-visible'
}
};
// Constructor Method
var Clear = function(el, options, selector) {
var $el = $(el);
this._instance = i;
this._name = rocketName;
this._defaults = defaults;
this.el = el;
this.$el = $el;
this.selector = selector;
this.options = $.extend(true, {}, defaults, options, $el.data());
i++;
this.init();
};
$.extend(Clear.prototype, {
init: function() {
var uxrocket = this.$el.data(ns.rocket) || {};
// add ready class
this.$el.addClass(utils.getClassname('ready'));
// register plugin data to rocket
uxrocket[ns.data] = {
hasWrapper: true,
wrapper: ns.wrap,
ready: utils.getClassname('ready'),
selector: this.selector,
options: this.options
};
this.$el.data(ns.rocket, uxrocket);
// set plugin layout
this.setLayout();
if (this.$el.val().length > 0) {
this.$el.parent().addClass(utils.getClassname('visible'));
}
utils.callback(this.options.onReady);
this.bindUIActions();
},
handleClasses: function() {
this.classList = this.$el.context.className.replace(utils.getClassname('ready'), '');
if (this.selector.charAt(0) === '.') {
this.classList = this.classList.replace(this.selector.substr(1), '');
}
this.classList += ns.wrap + ' ' + utils.getClassname('wrap') + ' ' + utils.getClassname('wrap') + '-' + this._instance;
this.classList = $.trim(this.classList);
},
removeClasses: function() {
this.$el.removeClass(utils.getClassname('ready'));
this.$el.parent().removeClass(this.classList.replace(ns.wrap, ''));
},
handleWrapper: function() {
this.$el.parent().is('.' + ns.wrap) ?
this.$el.parent().addClass(this.classList) :
this.$el.wrap('<span class="' + this.classList + '"></span>');
},
addIcon: function() {
this.$el.after('<i class="' + utils.getClassname('icon') + '"></i>');
this.$icon = this.$el.next('.' + utils.getClassname('icon'));
},
setLayout: function() {
this.handleClasses();
this.handleWrapper();
this.addIcon();
},
removeLayout: function() {
var _this = this,
uxrocket = _this.$el.data(ns.rocket);
// remove or reformat wrap
if (Object.keys && Object.keys(uxrocket).length === 1) {
_this.$el.unwrap();
}
else {
_this.$el.parent().removeClass(ns.wrap);
}
_this.$el.next('.' + utils.getClassname('icon')).remove();
},
bindUIActions: function() {
var _this = this;
var pasted = false;
_this.$el.on(events.keyup + ' ' + events.focus + ' ' + events.input, function() {
_this.onKeyup();
});
_this.$el.siblings('.' + utils.getClassname('icon')).on(events.click, function(e) {
_this.onClick(e);
});
_this.$el.on(events.paste, function() {
pasted = true;
});
$('body').on('DOMNodeRemoved', function(e) {
if (e.target === _this.el && !pasted) {
_this.cleanUp();
} else {
pasted = false;
}
});
},
unbindUIActions: function() {
this.$el.off('.' + rocketName);
},
onKeyup: function() {
var _this = this;
if (this.$el.val().length > 0) {
_this.$el.parent().addClass(utils.getClassname('visible'));
}
else {
_this.$el.parent().removeClass(utils.getClassname('visible'));
}
},
onClick: function(e) {
e.preventDefault();
this.$el.val('').parent().removeClass(utils.getClassname('visible'));
utils.callback(this.options.onClear);
},
update: function(options) {
return ux.update(this.el, options);
},
destroy: function() {
return ux.destroy(this.el);
},
// cleans wrapper, icons etc when element removed before plugin destroyed
cleanUp: function() {
$('.' + utils.getClassname('wrap') + '-' + this._instance).remove();
}
});
var utils = {
callback: function(fn) {
// if callback string is function call it directly
if (typeof fn === 'function') {
fn.apply(this);
}
// if callback defined via data-attribute, call it via new Function
else {
if (fn !== false) {
var _fn = /([a-zA-Z._$0-9]+)(\(?(.*)?\))?/.exec(fn),
_fn_ns = _fn[1].split('.'),
_args = _fn[3] ? _fn[3] : '',
func = _fn_ns.pop(),
context = _fn_ns[0] ? window[_fn_ns[0]] : window;
for (var i = 1; i < _fn_ns.length; i++) {
context = context[_fn_ns[i]];
}
return context[func](_args);
}
}
},
getStringVariable: function(str) {
var val;
// check if it is chained
if (str.indexOf('.') > -1) {
var chain = str.split('.'),
chainVal = window[chain[0]];
for (var i = 1; i < chain.length; i++) {
chainVal = chainVal[chain[i]];
}
val = chainVal;
}
else {
val = window[str];
}
return val;
},
getClassname: function(which) {
return ns.prefix + ns.name + '-' + ns.classes[which];
}
};
ux = $.fn.clear = $.fn.uxrclear = $.uxrclear = function(options) {
var selector = this.selector;
return this.each(function() {
if ($.data(this, ns.data)) {
return;
}
// Bind the plugin and attach the instance to data
$.data(this, ns.data, new Clear(this, options, selector));
});
};
ux.update = function(el, options) {
var $el, opts;
// all elements will update according to new options
if (typeof options === 'undefined' && typeof el === 'object') {
$el = $('.' + utils.getClassname('ready'));
opts = el;
}
else {
$el = $(el);
opts = options;
}
$el.filter('input').each(function() {
var _this = $(this),
_instance = _this.data(ns.data),
_opts = _instance.options;
// update new options
_instance.options = $.extend(true, {}, _opts, opts);
// use onUpdate callback from original options
utils.callback(_opts.onUpdate);
});
};
ux.destroy = function(el) {
var $el = el !== undefined ? $(el) : $('.' + utils.getClassname('ready'));
$el.filter('input').each(function() {
var _this = $(this),
_instance = _this.data(ns.data),
_uxrocket = _this.data(ns.rocket);
// remove ready class
_instance.removeClasses();
// remove plugin events
_instance.unbindUIActions();
// remove layout
_instance.removeLayout();
// remove plugin data
_this.removeData(ns.data);
// remove uxRocket registry
delete _uxrocket[ns.data];
_this.data(ns.rocket, _uxrocket);
utils.callback(_instance.options.onRemove);
});
};
// version
ux.version = '1.0.0';
// default settings
ux.settings = defaults;
ux.namespace = ns;
}));