forked from vitmalina/w2ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw2ui.js
8422 lines (7954 loc) · 306 KB
/
w2ui.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* w2ui 1.2.1 (c) http://w2ui.com, [email protected] */
var w2ui = w2ui || {};
var w2obj = w2obj || {}; // expose object to be able to overwrite default functions
/************************************************
* Library: Web 2.0 UI for jQuery
* - Following objects are defines
* - w2ui - object that contains all created objects
* - w2utils - basic utilities
* - w2ui.w2evet - generic event object
* - Dependencies: jQuery
*
* == NICE TO HAVE ==
*
************************************************/
var w2utils = (function () {
var obj = {
settings : {
locale : "en-us",
date_format : "mm/dd/yyyy",
date_display : "Mon dd, yyyy",
time_format : "hh:mi pm",
currency : "^[\$\€\£\¥]?[-]?[0-9]*[\.]?[0-9]+$",
float : "^[-]?[0-9]*[\.]?[0-9]+$",
shortmonths : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
fullmonths : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
shortdays : ["M", "T", "W", "T", "F", "S","S"],
fulldays : ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
RESTfull : false,
phrases : {} // empty object for english phrases
},
isInt : isInt,
isFloat : isFloat,
isMoney : isMoney,
isHex : isHex,
isAlphaNumeric : isAlphaNumeric,
isEmail : isEmail,
isDate : isDate,
isTime : isTime,
size : size,
age : age,
formatDate : formatDate,
date : date,
stripTags : stripTags,
encodeTags : encodeTags,
escapeId : escapeId,
base64encode : base64encode,
base64decode : base64decode,
transition : transition,
getSize : getSize,
lang : lang,
locale : locale
}
return obj;
function isInt (val) {
var re = /^[-]?[0-9]+$/;
return re.test(val);
}
function isFloat (val) {
var re = new RegExp(w2utils.settings.float);
return re.test(val);
}
function isMoney (val) {
var re = new RegExp(w2utils.settings.currency);
return re.test(val);
}
function isHex (val) {
var re = /^[a-fA-F0-9]+$/;
return re.test(val);
}
function isAlphaNumeric (val) {
var re = /^[a-zA-Z0-9_-]+$/;
return re.test(val);
}
function isEmail (val) {
var email = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
return email.test(val);
}
function isDate (val, format) {
if (typeof val == 'undefined' || val == null) return false;
if (typeof format == 'undefined' || format == null) format = w2utils.settings.date_format;
// European format dd/mm/yyyy
if (format.toLowerCase() == 'dd/mm/yyyy' || format.toLowerCase() == 'dd-mm-yyyy' || format.toLowerCase() == 'dd.mm.yyyy') {
val = val.replace(/-/g, '/').replace(/\./g, '/');
if (val.split("/").length != 3) return false;
var month = val.split("/")[1];
var day = val.split("/")[0];
var year = val.split("/")[2];
var obj = new Date(year, month-1, day);
if ((obj.getMonth()+1 != month) || (obj.getDate() != day) || (obj.getFullYear() != year)) return false;
return true;
}
// US Format will get converted normally
var dt = new Date(val.replace(/-/g, '/').replace(/\./g, '/'));
if (dt == 'Invalid Date') return false;
// make sure it is in correct format
if (typeof format != 'undefined') {
var dt = this.formatDate(dt, format);
if (dt != val) return false;
}
return true;
}
function isTime (val) {
// Both formats 10:20pm and 22:20
if (String(val) == 'undefined') return false;
var max;
// -- process american foramt
val = val.toUpperCase();
if (val.indexOf('PM') >= 0 || val.indexOf('AM') >= 0) max = 12; else max = 23;
val = $.trim(val.replace('AM', ''));
val = $.trim(val.replace('PM', ''));
// ---
var tmp = val.split(':');
if (tmp.length != 2) { return false; }
if (tmp[0] == '' || parseInt(tmp[0]) < 0 || parseInt(tmp[0]) > max || !this.isInt(tmp[0])) { return false; }
if (tmp[1] == '' || parseInt(tmp[1]) < 0 || parseInt(tmp[1]) > 59 || !this.isInt(tmp[1])) { return false; }
return true;
}
function formatDate (dateStr, format) { // IMPORTANT dateStr HAS TO BE valid JavaScript Date String
var months = w2utils.settings.shortmonths;
var fullMonths = w2utils.settings.fullmonths;
if (typeof format == 'undefined') format = this.settings.date_format;
if (typeof dateStr == 'undefined' || dateStr == '' || dateStr == null) return '';
var dt = new Date(dateStr);
if (w2utils.isInt(dateStr)) dt = new Date(Number(dateStr)); // for unix timestamps
var tmp = String(dateStr).split('-');
if (tmp.length == 3) dt = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
var tmp = String(dateStr).split('/');
if (tmp.length == 3) dt = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
if (dt == 'Invalid Date') return '';
var year = dt.getFullYear();
var month = dt.getMonth();
var date = dt.getDate();
var res = format.toLowerCase()
.replace('yyyy', year)
.replace('mm', month+1)
.replace('dd', date)
.replace('mon', months[month])
.replace('month', fullMonths[month]);
return res;
}
function date (dateStr) {
var months = w2utils.settings.shortmonths;
if (dateStr == '' || typeof dateStr == 'undefined' || dateStr == null) return '';
if (w2utils.isInt(dateStr)) dateStr = Number(dateStr); // for unix timestamps
var d1 = new Date(dateStr);
if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
var tmp = String(dateStr).split('-');
if (tmp.length == 3) d1 = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
var tmp = String(dateStr).split('/');
if (tmp.length == 3) d1 = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
if (d1 == 'Invalid Date') return '';
var d2 = new Date(); // today
var d3 = new Date();
d3.setTime(d3.getTime() - 86400000); // yesterday
var dd1 = months[d1.getMonth()] + ' ' + d1.getDate() + ', ' + d1.getFullYear();
var dd2 = months[d2.getMonth()] + ' ' + d2.getDate() + ', ' + d2.getFullYear();
var dd3 = months[d3.getMonth()] + ' ' + d3.getDate() + ', ' + d3.getFullYear();
var time = (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
var time2= (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ':' + (d1.getSeconds() < 10 ? '0' : '') + d1.getSeconds() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
var dsp = dd1;
if (dd1 == dd2) dsp = time;
if (dd1 == dd3) dsp = w2utils.lang('Yesterday');
return '<span title="'+ dd1 +' ' + time2 +'">'+ dsp +'</span>';
}
function age (timeStr) {
if (timeStr == '' || typeof timeStr == 'undefined' || timeStr == null) return '';
if (w2utils.isInt(timeStr)) timeStr = Number(timeStr); // for unix timestamps
var d1 = new Date(timeStr);
if (w2utils.isInt(timeStr)) d1 = new Date(Number(timeStr)); // for unix timestamps
var tmp = String(timeStr).split('-');
if (tmp.length == 3) d1 = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
var tmp = String(timeStr).split('/');
if (tmp.length == 3) d1 = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
if (d1 == 'Invalid Time') return '';
var d2 = new Date();
var sec = (d2.getTime() - d1.getTime()) / 1000;
var amount = '';
var type = '';
if (sec < 60) {
amount = Math.floor(sec);
type = 'sec';
} else if (sec < 60*60) {
amount = Math.floor(sec/60);
type = 'min';
} else if (sec < 24*60*60) {
amount = Math.floor(sec/60/60);
type = 'hour';
} else if (sec < 30*24*60*60) {
amount = Math.floor(sec/24/60/60);
type = 'day';
} else if (sec < 12*30*24*60*60) {
amount = Math.floor(sec/30/24/60/60);
type = 'month';
} else if (sec >= 12*30*24*60*60) {
amount = Math.floor(sec/12/30/24/60/60);
type = 'year';
}
return amount + ' ' + type + (amount > 1 ? 's' : '');
}
function size (sizeStr) {
if (!w2utils.isFloat(sizeStr) || sizeStr == '') return '';
sizeStr = parseFloat(sizeStr);
var sizes = ['Bt', 'KB', 'MB', 'GB', 'TB'];
var i = parseInt( Math.floor( Math.log(sizeStr) / Math.log(1024) ) );
return (Math.floor(sizeStr / Math.pow(1024, i) * 10) / 10).toFixed(i == 0 ? 0 : 1) + ' ' + sizes[i];
}
function stripTags (html) {
if (html == null) return html;
switch (typeof html) {
case 'number':
break;
case 'string':
html = $.trim(String(html).replace(/(<([^>]+)>)/ig, ""));
break;
case 'object':
for (var a in html) html[a] = this.stripTags(html[a]);
break;
}
return html;
}
function encodeTags (html) {
if (html == null) return html;
switch (typeof html) {
case 'number':
break;
case 'string':
html = String(html).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
break;
case 'object':
for (var a in html) html[a] = this.encodeTags(html[a]);
break;
}
return html;
}
function escapeId (id) {
if (typeof id == 'undefined' || id == '' || id == null) return '';
return String(id).replace(/([;&,\.\+\*\~'`:"\!\^#$%@\[\]\(\)=<>\|\/? {}\\])/g, '\\$1');
}
function base64encode (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
input = utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
function utf8_encode (string) {
var string = String(string).replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
return output;
}
function base64decode (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = utf8_decode(output);
function utf8_decode (utftext) {
var string = "";
var i = 0;
var c = 0, c1 = 0, c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
return output;
}
function transition (div_old, div_new, type, callBack) {
var width = $(div_old).width();
var height = $(div_old).height();
var time = 0.5;
if (!div_old || !div_new) {
console.log('ERROR: Cannot do transition when one of the divs is null');
return;
}
div_old.parentNode.style.cssText += cross('perspective', '700px') +'; overflow: hidden;';
div_old.style.cssText += '; position: absolute; z-index: 1019; '+ cross('backface-visibility', 'hidden');
div_new.style.cssText += '; position: absolute; z-index: 1020; '+ cross('backface-visibility', 'hidden');
switch (type) {
case 'slide-left':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d('+ width + 'px, 0, 0)', 'translate('+ width +'px, 0)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_old.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
}, 1);
break;
case 'slide-right':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0px, 0, 0)', 'translate(0px, 0)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d('+ width +'px, 0, 0)', 'translate('+ width +'px, 0)');
}, 1);
break;
case 'slide-down':
// init divs
div_old.style.cssText += 'overflow: hidden; z-index: 1; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; z-index: 0; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
}, 1);
break;
case 'slide-up':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
}, 1);
break;
case 'flip-left':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('-transform', 'rotateY(0deg)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(-180deg)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(180deg)');
}, 1);
break;
case 'flip-right':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(0deg)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(180deg)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(-180deg)');
}, 1);
break;
case 'flip-down':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(180deg)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(-180deg)');
}, 1);
break;
case 'flip-up':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(-180deg)');
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(180deg)');
}, 1);
break;
case 'pop-in':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') + '; '+ cross('transform', 'scale(.8)') + '; opacity: 0;';
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
div_old.style.cssText += cross('transition', time+'s') +';';
}, 1);
break;
case 'pop-out':
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time+'s') +'; opacity: 1;';
div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1.7)') +'; opacity: 0;';
}, 1);
break;
default:
// init divs
div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
$(div_new).show();
// -- need a timing function because otherwise not working
window.setTimeout(function() {
div_new.style.cssText += cross('transition', time +'s') +'; opacity: 1;';
div_old.style.cssText += cross('transition', time +'s');
}, 1);
break;
}
setTimeout(function () {
if (type == 'slide-down') {
$(div_old).css('z-index', '1019');
$(div_new).css('z-index', '1020');
}
if (div_new) {
$(div_new).css({
'opacity': '1',
'-webkit-transition': '',
'-moz-transition': '',
'-ms-transition': '',
'-o-transition': '',
'-webkit-transform': '',
'-moz-transform': '',
'-ms-transform': '',
'-o-transform': '',
'-webkit-backface-visibility': '',
'-moz-backface-visibility': '',
'-ms-backface-visibility': '',
'-o-backface-visibility': ''
});
}
if (div_old) {
$(div_old).css({
'opacity': '1',
'-webkit-transition': '',
'-moz-transition': '',
'-ms-transition': '',
'-o-transition': '',
'-webkit-transform': '',
'-moz-transform': '',
'-ms-transform': '',
'-o-transform': '',
'-webkit-backface-visibility': '',
'-moz-backface-visibility': '',
'-ms-backface-visibility': '',
'-o-backface-visibility': ''
});
if (div_old.parentNode) $(div_old.parentNode).css({
'-webkit-perspective': '',
'-moz-perspective': '',
'-ms-perspective': '',
'-o-perspective': ''
});
}
if (typeof callBack == 'function') callBack();
}, time * 1000);
function cross(property, value, none_webkit_value) {
if (!$.browser.webkit && typeof none_webkit_value != 'undefined') value = none_webkit_value;
return ';'+ property +': '+ value +'; -webkit-'+ property +': '+ value +'; -moz-'+ property +': '+ value +'; '+
'-ms-'+ property +': '+ value +'; -o-'+ property +': '+ value +';';
}
}
function getSize (el, type) {
var bwidth = {
left: parseInt($(el).css('border-left-width')) || 0,
right: parseInt($(el).css('border-right-width')) || 0,
top: parseInt($(el).css('border-top-width')) || 0,
bottom: parseInt($(el).css('border-bottom-width')) || 0
}
var mwidth = {
left: parseInt($(el).css('margin-left')) || 0,
right: parseInt($(el).css('margin-right')) || 0,
top: parseInt($(el).css('margin-top')) || 0,
bottom: parseInt($(el).css('margin-bottom')) || 0
}
var pwidth = {
left: parseInt($(el).css('padding-left')) || 0,
right: parseInt($(el).css('padding-right')) || 0,
top: parseInt($(el).css('padding-top')) || 0,
bottom: parseInt($(el).css('padding-bottom')) || 0
}
switch (type) {
case 'top': return bwidth.top + mwidth.top + pwidth.top;
case 'bottom': return bwidth.bottom + mwidth.bottom + pwidth.bottom;
case 'left': return bwidth.left + mwidth.left + pwidth.left;
case 'right': return bwidth.right + mwidth.right + pwidth.right;
case 'width': return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right + parseInt($(el).width());
case 'height': return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom + parseInt($(el).height());
case '+width': return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right;
case '+height': return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom;
}
return 0;
}
function lang (phrase) {
var translation = this.settings.phrases[phrase];
if (typeof translation == 'undefined') return phrase; else return translation;
}
function locale (localParam) {
var settings = w2utils.settings;
var param;
if (typeof localParam == 'string') localParam = { lang: localParam };
$.extend( { path : '', lang : 'en-us' }, localParam);
var result = localParam['lang'].toLowerCase().match(/^([a-z]{2})\-([a-z]{2})$/);
if (result !== null && result.length === 3) {
param = result[1] + '-' + result[2].toUpperCase();
if ( param === settings.locale) {
return param;
}
param = localParam.path + "locale/" + param.toLowerCase() + ".json";
$.ajax({
url: param,
type: "GET",
dataType: "json",
cache : false,
async : false,
}).done(function( data) {
$.extend( settings, data);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
return settings.locale;
}
})();
/***********************************************************
* Generic Event Object
* --- This object is reused across all other
* --- widgets in w2ui.
*
*********************************************************/
$.w2event = {
on: function (eventData, handler) {
if (!$.isPlainObject(eventData)) eventData = { type: eventData };
eventData = $.extend({ type: null, execute: 'before', target: null, onComplete: null }, eventData);
if (typeof eventData.type == 'undefined') { console.log('ERROR: You must specify event type when calling .on() method of '+ this.name); return; }
if (typeof handler == 'undefined') { console.log('ERROR: You must specify event handler function when calling .on() method of '+ this.name); return; }
this.handlers.push({ event: eventData, handler: handler });
},
off: function (eventData, handler) {
if (!$.isPlainObject(eventData)) eventData = { type: eventData };
eventData = $.extend({}, { type: null, execute: 'before', target: null, onComleted: null }, eventData);
if (typeof eventData.type == 'undefined') { console.log('ERROR: You must specify event type when calling .off() method of '+ this.name); return; }
if (typeof handler == 'undefined') { handler = null; }
// remove handlers
var newHandlers = [];
for (var h in this.handlers) {
var t = this.handlers[h];
if ( (t.event.type == eventData.type || eventData.type == '*')
&& (t.event.target == eventData.target || eventData.target == null)
&& (t.handler == handler || handler == null)) {
// match
} else {
newHandlers.push(t);
}
}
this.handlers = newHandlers;
},
trigger: function (eventData) {
var eventData = $.extend({ type: null, phase: 'before', target: null, stop: false }, eventData,
{ preventDefault: function () { this.stop = true; } });
if (typeof eventData.target == 'undefined') eventData.target = null;
// process events in REVERSE order
for (var h = this.handlers.length-1; h >= 0; h--) {
var t = this.handlers[h];
if ( (t.event.type == eventData.type || t.event.type == '*')
&& (t.event.target == eventData.target || t.event.target == null)
&& (t.event.execute == eventData.phase || t.event.execute == '*' || t.event.phase == '*') ) {
var ret = t.handler.call(this, eventData.target, eventData);
if ($.isPlainObject(ret)) {
$.extend(eventData, ret);
if (eventData.stop === true) return eventData;
}
}
}
// main object events
if (eventData.phase == 'before'
&& typeof this['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)] == 'function') {
var ret = this['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)].call(this, eventData.target, eventData);
if ($.isPlainObject(ret)) {
$.extend(eventData, ret);
if (eventData.stop === true) return eventData;
}
}
// item object events
if (typeof eventData.object != 'undefined' && eventData.object != null && eventData.phase == 'before'
&& typeof eventData.object['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)] == 'function') {
var ret = eventData.object['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)].call(this, eventData.target, eventData);
if ($.isPlainObject(ret)) {
$.extend(eventData, ret);
if (eventData.stop === true) return eventData;
}
}
// execute onComplete
if (eventData.phase == 'after' && eventData.onComplete != null) eventData.onComplete.call(this, eventData);
return eventData;
}
};
/***********************************************************
* Commonly used plugins
* --- used primarily in grid and form
*
*********************************************************/
(function () {
$.fn.w2render = function (name) {
if ($(this).length > 0) {
if (typeof name == 'string' && w2ui[name]) w2ui[name].render($(this)[0]);
if (typeof name == 'object') name.render($(this)[0]);
}
}
$.fn.w2destroy = function (name) {
if (typeof name == 'undefined' && this.length > 0) name = this.data('w2name');
if (typeof name == 'string' && w2ui[name]) w2ui[name].destroy();
if (typeof name == 'object') name.destroy();
}
$.fn.w2lite = function () {
}
// -- w2tag - appears on the right side from element, there can be multiple on screen at a time
$.fn.w2tag = function (text, options) {
if (!$.isPlainObject(options)) options = {};
if (!$.isPlainObject(options.css)) options.css = {};
if (typeof options['class'] == 'undefined') options['class'] = '';
// remove all tags
if ($(this).length == 0) {
$('.global-tag').each(function (index, elem) {
var opt = $(elem).data('options');
if (typeof opt == 'undefined') opt = {};
$($(elem).data('taged-el')).removeClass( opt['class'] );
clearInterval($(elem).data('timer'));
$(elem).remove();
});
return;
}
return $(this).each(function (index, el) {
// show or hide tag
var tagOrigID = el.id;
var tagID = w2utils.escapeId(el.id);
if (text == '' || text == null || typeof text == 'undefined') {
$('#global-tag-'+tagID).css('opacity', 0);
setTimeout(function () {
// remmove element
clearInterval($('#global-tag-'+tagID).data('timer'));
$('#global-tag-'+tagID).remove();
}, 300);
} else {
// remove elements
clearInterval($('#global-tag-'+tagID).data('timer'));
$('#global-tag-'+tagID).remove();
// insert
$('body').append('<div id="global-tag-'+ tagOrigID +'" class="global-tag" '+
' style="position: absolute; z-index: 1701; opacity: 0; -webkit-transition: opacity .3s; -moz-transition: opacity .3s; -ms-transition: opacity .3s; -o-transition: opacity .3s"></div>');
var timer = setInterval(function () {
// monitor if destroyed
if ($(el).length == 0 || ($(el).offset().left == 0 && $(el).offset().top == 0)) {
clearInterval($('#global-tag-'+tagID).data('timer'));
tmp_hide();
return;
}
// monitor if moved
if ($('#global-tag-'+tagID).data('position') != ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top) {
$('#global-tag-'+tagID).css({
'-webkit-transition': '.2s',
'-moz-transition': '.2s',
'-ms-transition': '.2s',
'-o-transition': '.2s',
left: ($(el).offset().left + el.offsetWidth) + 'px',
top: $(el).offset().top + 'px'
}).data('position', ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top);
}
}, 100);
setTimeout(function () {
if (!$(el).offset()) return;
$('#global-tag-'+tagID).css({
opacity: '1',
left: ($(el).offset().left + el.offsetWidth) + 'px',
top: $(el).offset().top + 'px'
}).html('<div style="margin-top: -2px 0px 0px -2px; white-space: nowrap;"> <div class="bubble-tag">'+ text +'</div> </div>')
.data('text', text)
.data('taged-el', el)
.data('options', options)
.data('position', ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top)
.data('timer', timer);
$(el).off('keypress', tmp_hide).on('keypress', tmp_hide).off('change', tmp_hide).on('change', tmp_hide)
.css(options.css).addClass(options['class']);
if (typeof options.onShow == 'function') options.onShow();
}, 1);
var originalCSS = '';
if ($(el).length > 0) originalCSS = $(el)[0].style.cssText;
// bind event to hide it
function tmp_hide() {
if ($('#global-tag-'+tagID).length <= 0) return;
clearInterval($('#global-tag-'+tagID).data('timer'));
$('#global-tag-'+tagID).remove();
$(el).off('keypress', tmp_hide).removeClass(options['class']);
if ($(el).length > 0) $(el)[0].style.cssText = originalCSS;
if (typeof options.onHide == 'function') options.onHide();
}
}
});
}
// w2overlay - appears under the element, there can be only one at a time
$.fn.w2overlay = function (html, options) {
var isOpened = false;
if (!$.isPlainObject(options)) options = {};
if (!$.isPlainObject(options.css)) options.css = {};
if (this.length == 0 || html == '' || typeof html == 'undefined') {
if (typeof options.onHide == 'function') options.onHide();
$('#w2ui-overlay').remove();
$(document).off('click', hide);
return;
}
// insert (or re-insert) overlay
if ($('#w2ui-overlay').length > 0) { isOpened = true; $(document).off('click', hide); $('#w2ui-overlay').remove(); }
$('body').append('<div id="w2ui-overlay" class="w2ui-reset w2ui-overlay"><div></div></div>');
// init
var obj = this;
var div = $('#w2ui-overlay div');
div.css(options.css).html(html);
if (typeof options['class'] != 'undefined') div.addClass(options['class']);
if (typeof options.top == 'undefined') options.top = 0;
if (typeof options.left == 'undefined') options.left = 0;
if (typeof options.width == 'undefined') options.width = 100;
if (typeof options.height == 'undefined') options.height = 0;
// pickup bg color of first div
var bc = div.css('background-color');
var div = $('#w2ui-overlay');
if (typeof bc != 'undefined' && bc != 'rgba(0, 0, 0, 0)' && bc != 'transparent') div.css('background-color', bc);
div.css({
display : 'none',
left : ($(obj).offset().left + options.left) + 'px',
top : ($(obj).offset().top + w2utils.getSize($(obj), 'height') + 3 + options.top) + 'px',
'min-width' : (options.width ? options.width : 'auto'),
'min-height' : (options.height ? options.height : 'auto')
})
.fadeIn('fast')
.data('position', ($(obj).offset().left) + 'x' + ($(obj).offset().top + obj.offsetHeight))
.on('click', function (event) {
if (event.stopPropagation) event.stopPropagation(); else event.cancelBubble = true;
});
// click anywhere else hides the drop down
var hide = function () {
if (typeof options.onHide == 'function') options.onHide();
$('#w2ui-overlay').remove();
$(document).off('click', hide);
}
// need time to display
setTimeout(function () {
$(document).on('click', hide);
if (typeof options.onShow == 'function') options.onShow();
}, 1);
}
})();
/************************************************************************
* Library: Web 2.0 UI for jQuery (using prototypical inheritance)
* - Following objects defined
* - w2ui.w2grid - grid widget
* - $.w2grid - jQuery wrapper
* - Dependencies: jQuery, w2utils, w2toolbar, w2fields, w2popup
*
* == NICE TO HAVE ==
* - global search apply types and drop downs
* - editable fields (list) - better inline editing
* - move doExpand into the record
* - infinite scroll (buffered scroll)
* - frozen columns
* - column autosize based on largest content
*
************************************************************************/
(function () {
var w2grid = function(options) {
// public properties
this.name = null;
this.box = null; // HTML element that hold this element
this.header = '';
this.url = '';
this.columns = []; // { field, caption, size, attr, render, hidden, gridMinWidth, [editable: {type, inTag, outTag, style, items}] }
this.columnGroups = []; // { span: int, caption: 'string', master: true/false }
this.records = []; // { recid: int(requied), field1: 'value1', ... fieldN: 'valueN', style: 'string', editable: true/false, summary: true/false }
this.searches = []; // { type, caption, field, inTag, outTag, default, items, hidden }
this.searchData = [];
this.sortData = [];
this.postData = {};
this.toolbar = {}; // if not empty object; then it is toolbar object
this.show = {
header : false,
toolbar : false,
footer : false,
columnHeaders : true,
lineNumbers : false,
expandColumn : false,
selectColumn : false,
emptyRecords : true,
toolbarReload : true,
toolbarColumns : true,
toolbarSearch : true,
toolbarAdd : false,
toolbarDelete : false,
toolbarSave : false
},
this.fixedBody = true; // if false; then grid grows with data
this.fixedRecord = true; // if false; then record height grows with data
this.multiSearch = true;
this.multiSelect = true;
this.multiSort = true;
this.keyboard = true; // if user clicks on the list; it will bind all events from the keyboard for that list
this.total = 0; // total number of records
this.page = 0; // current page
this.recordsPerPage = 50;
this.style = '';
this.msgDelete = w2utils.lang('Are you sure you want to delete selected records?');
this.msgNotJSON = w2utils.lang('Returned data is not in valid JSON format.');
this.msgRefresh = w2utils.lang('Refreshing...');
// events
this.onRequest = null; // called on any server event
this.onLoad = null;
this.onAdd = null;
this.onDelete = null;
this.onSave = null;
this.onSelect = null;
this.onUnselect = null;
this.onClick = null;
this.onDblClick = null;
this.onSort = null;
this.onSearch = null;
this.onChange = null; // called when editable record is changed
this.onExpand = null;
this.onError = null;
this.onRender = null;
this.onRefresh = null;
this.onResize = null;
this.onDestroy = null;
// internal
this.last = {
field : 'all',
caption : w2utils.lang('All Fields'),
logic : 'OR',
search : '',
multi : false,
scrollTop : 0,
scrollLeft : 0,
selected : [],
sortData : null,
sortCount : 0,
xhr : null
}
this.isIOS = (navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
navigator.userAgent.toLowerCase().indexOf('ipad') != -1) ? true : false;
$.extend(true, this, options);
};
// ====================================================
// -- Registers as a jQuery plugin