-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.fortunewheel.js
423 lines (392 loc) · 11.6 KB
/
jquery.fortunewheel.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
* Wheel of Fortune like input jQuery Plugin
* Author: Yaron Uliel
* Copyright: 2014 Social It Tech Marketing
*/
window.FortuneWheel = {
getConsoleDebugger: function(){
return {
clear: function(){
console.clear();
},
log: function(){
var i = -1, l = arguments.length, args = [], fn = 'console.log(args)';
while(++i<l){
args.push('args['+i+']');
};
fn = new Function('args',fn.replace(/args/,args.join(',')));
fn(arguments);
}
}
},
getDebugger: function(elem){
return {
clear: function(){
if(elem!=""){
$(elem).html("");
}
},
log: function(){
if(elem!=""){
var string = "";
for(var i in arguments){
string+=arguments[i].toString();
if(i<arguments.length-1){
string+=", ";
}
}
$(elem).append($("<div>").text(string));
}
}
};
},
init: function(selector,options){
$(selector).fortuneWheel(options);
}
};
(function($, w, d){
var Browser = {
ua: w.navigator.userAgent.toLowerCase(),
isAndroid: function(){
return Browser.ua.indexOf("android")>-1;
},
isIOS: function(){
var iOS = ( Browser.ua.match(/(ipad|iphone|ipod)/g) ? true : false );
return iOS;
},
isOldAndroid: function(){
return Browser.ua.indexOf("android 2.")>-1 || Browser.ua.indexOf("gecko) version/")>-1;
},
isProblematic: function(){
return Browser.isIOS() || Browser.isOldAndroid();
}
};
var instances = [];
var FortuneWheel = function(elem, options){
this.getElement = function(){
return elem;
}
var defaultOptions = {
placeholder: null,
pattern: [],
hideElem: true,
wordSpace: 15,
spaceClass: "word-space",
showCaretSymbol: true, //works on chrome only
placeholderClass: 'fortune-wheel',
allowNumbers: false,
trimValue: true,
allowBlanks: false,
allowDeleting: true,
restrict: null, // regex for allowed characters
blurOnLastChar: false,
byWords: false,
charSize: 30,
filledClass: 'filled',
eventFull: 'change-full',
eventNotFull: 'change-not-full',
//debug: w.FortuneWheel.getDebugger(""),
full: function(value){}, //callback when full
partial: function(value){} //callback when change partially
};
var instance = this;
elem.data('fortune_wheel', instance);
var placeholder = null,
chars = [],
jQueryChars = [];
this.isFull = function(){
var full = true;
$(chars).each(function(){
var val = $.trim($(this).val());
var length = $(this).attr("maxlength");
if(val.length<length){
full = false;
return false;
}
});
return full;
};
this.getValue = function(){
var text = "";
$(chars).each(function(){
var val = $(this).val();
text+=(val==""?" ":val);
if($(this).hasClass(options.spaceClass)){
text+=" ";
};
});
return options.trimValue?$.trim(text):text;
};
function isRTL(elem){
return $(elem).css("direction").toLowerCase()=="rtl";
}
function initPlaceholder(){
var holder = null;
if(options.placeholder!=null){
holder = $(options.placeholder);
}
if(!holder || holder.length==0){
holder = $("<div />");
$(elem).before(holder);
}
holder.html("").addClass(options.placeholderClass);
return holder;
}
function getByPos(pos){
return $(pos>=0 && pos<chars.length?chars[pos]:null);
}
function fullTrigger(){
$(chars).each(function(){
var val = $.trim($(this).val()).replace(/\s/g,'').length;
var maxlength = parseInt($(this).attr("maxlength"));
$(this).toggleClass(options.filledClass,val>=maxlength);
});
if(instance.isFull()){
elem.trigger(options.eventFull);
if(typeof(options.full)=="function"){
options.full(instance.getValue());
}
} else {
elem.trigger(options.eventNotFull);
if(typeof(options.partial)=="function"){
options.partial(instance.getValue());
}
}
}
function log(){
if(options.debug){
options.debug.log.apply(this, arguments);
}
}
function setValue(value){
var current_value = instance.getValue();
if(current_value==value){
return;
}
var start_val = value.replace(/\s+/g,"");
var total_chars = 0;
$(jQueryChars).each(function(i){
var currentChar = $(this);
var maxlength = parseInt(currentChar.prop("maxlength"));
currentChar.val(start_val.substr(total_chars, maxlength));
total_chars+=maxlength;
});
}
function initEventListeners(){
$(chars).change(function(e){
var new_val = instance.getValue();
if(new_val!=elem.val()){
elem.val(new_val);
}
fullTrigger();
}).keyup(function(e){
fullTrigger();
});
if(!Browser.isOldAndroid()){
$(chars).keydown(function(e){
var rtl = isRTL($(this));
var pos = $(this).data("pos");
var next = getByPos(pos+1);
var prev = getByPos(pos-1);
switch(e.keyCode){
case 46: //delete
if(option.byWords){
break;
}
if(options.allowBlanks){
$(this).val("").trigger("change");
}
return false;
case 8: //backspace
if(Browser.isProblematic()){
return true;
}
var current_val = $(this).val();
if(options.allowDeleting){
$(this).val(current_val.substring(0,current_val.length-1)).trigger("change");
}
if(current_val.length-1<=0){
prev.focus();
}
return false;
case 32: //space
if(options.byWords){
break;
}
if(options.allowBlanks){
$(this).val(" ").trigger("change");
next.focus();
}
return false;
case 37: //left arrow
if(!options.byWords){
(rtl?next:prev).focus();
return false;
}
return true;
case 39: //right arrow
if(!options.byWords){
(rtl?prev:next).focus();
return false;
}
return true;
}
}).on(Browser.isAndroid()?"input":"keypress",function(e){
var pos = $(this).data("pos");
var next = getByPos(pos+1);
var keycode = e.keyCode || e.which || 0;
var key = String.fromCharCode(keycode);
var length = $(this).val().length;
var maxlength = parseInt($(this).attr("maxlength"));
if(!options.allowNumbers && !isNaN(key)){
return false;
}
if(options.restrict && keycode!=0 && !(new RegExp('['+options.restrict+']')).test(key)){
return false;
}
if(key!="" && keycode!=0){
if(options.byWords){
$(this).val(($(this).val()+key).substring(0,maxlength)).trigger("change");
} else {
$(this).val(key).trigger("change");
}
}
length = $(this).val().length;
if(!Browser.isProblematic() && next.length==0 && (options.blurOnLastChar || keycode==0)){
$(this).blur();
} else {
if(Browser.isAndroid()){
$(this).val($(this).val().substring(0,maxlength));
}
if(Browser.isAndroid() && next.val()!=""){
$(this).blur();
} else {
if(length>=maxlength && !Browser.isProblematic()){
next.focus();
}
}
}
if(keycode!=0){
return false;
}
});
}
if(Browser.isAndroid() && !Browser.isOldAndroid()){
chars.focus(function(){
$(this).val("").trigger("change");
})
}
if(!options.allowBlanks){
var focusclass = "focus"+(Math.floor(Math.random()*100000));
$(chars).focus(function(e){
if($(this).val()!="" || $(this).hasClass(focusclass)){
$(this).removeClass(focusclass);
return true;
} else {
$(chars).each(function(){
if($(this).val()==""){
$(this).addClass(focusclass).focus();
return false;
}
});
}
});
}
$(elem).change(function(){
var value = $(this).val();
setValue(value);
})
}
function getPattern(){
var from = [];
if(options.pattern.length>0){
from = options.pattern;
} else {
from = $(elem).data("pattern");
}
if(typeof(from)=="object"){
return from;
} else {
return from.toString().split(",");
}
}
function init(){
var bw = !!options.byWords;
placeholder.html();
var pattern = getPattern();
var currentChar = null;
jQueryChars = [];
for(var i in pattern){
if(isNaN(pattern[i])){
continue;
}
var inputs = bw?1:pattern[i];
for(var j=0; j<inputs; j++){
var cls = bw?"word":"char";
var maxlength = bw?pattern[i]:1;
currentChar = $('<input type="text" data-pos="'+(jQueryChars.length)+'" maxlength="'+maxlength+'" class="'+cls+'" />');
if(!options.showCaretSymbol){
currentChar.attr("readonly","readonly");
}
placeholder.append(currentChar);
currentChar.css({height:options.charSize, width: options.charSize*maxlength, lineHeight: options.charSize+"px"});
jQueryChars.push(currentChar);
}
if(i<pattern.length-1 && currentChar!=null){
currentChar.addClass(options.spaceClass).css((isRTL(placeholder)?"margin-left":"margin-right"),options.wordSpace);
}
}
setValue(elem.val());
chars = $(jQueryChars).map (function () {return this.toArray(); } );
initEventListeners();
if(options.hideElem){
elem.hide();
}
fullTrigger();
}
this.destroy = function(){
$(chars).remove();
elem.show();
}
options = $.extend(defaultOptions, options);
if(Browser.isProblematic()){
options.byWords = true;
}
placeholder = initPlaceholder();
init();
};
FortuneWheel.destroy = function(elem){
var pos = getInstancePerElement(elem);
if(pos>-1){
var instance = instances[pos];
instance.destroy();
instances[pos] = null;
}
};
function getInstancePerElement(elem){
var pos = -1;
if(elem!=null){
for(var i in instances){
if(instances[i]!=null && elem.get(0) == instances[i].getElement().get(0)){
pos = i;
break;
}
}
}
return pos;
}
$.fn.fortuneWheel = function(options){
$(this).filter("input[type=text]").each(function(){
var elem = $(this);
if(options=="destroy"){
FortuneWheel.destroy(elem);
} else {
var pos = getInstancePerElement(elem);
if(pos==-1){
instances.push(new FortuneWheel(elem, options));
}
}
});
return this;
};
})(jQuery, window, document);