Repository navigation
Expand file tree
/
Copy pathjquerymy.js
More file actions
4206 lines (3643 loc) · 116 KB
/
Copy pathjquerymy.js
File metadata and controls
4206 lines (3643 loc) · 116 KB
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
/*
* jQuery.my 1.1.9
* Requires jQuery 1.11.0+, SugarJS 1.3.9-1.4.x
*
* — js2txt removes 'anonymous' name from functions
* — $.my.f.unmask receives 3 args as well as 2
* — modal positioned correctly in scrollable container
* — lists redrawn are rebuild to be bound with objects newly passed
* — ... it means undo works on lists without bugs.
*
* More details at jquerymy.com
*
* (c) @ermouth, thanks @carpogoryanin, @ftescht
* 2015-09-10
*/
;(function ($) {var _version = "jQuery.my 1.1.9";
// Some shortcuts and constants
var lang = "en",
wURL = window.URL || window.webkitURL,
ie8 = !document.addEventListener,
isA = Object.isArray,
isB = Object.isBoolean,
isS = Object.isString,
isO = Object.isObject,
isN = Object.isNumber,
isR = Object.isRegExp,
isF = Object.isFunction,
Keys = Object.keys,
forms = _getref($,"my.f.repo")? $.my.f.repo():{_src:{}, _name:"Default manifest cache"},
restyles = _getref($,"my.f.restyles")? $.my.f.restyles():{},
$E = $.extend, T = $.type, N = null, TMP,
n = function (o) {return o!==null && o!==undefined;},
d8 = "{yyyy}-{MM}-{dd}", h24="{HH}:{mm}",
Ob = "object", Da = "data", Ar = "array",
St = "string", Fu = "function", Ch = "change",
rthis = /^this\./,
isP = function (a) {
// detects promise
return !!(null!=a&&(isO(a)||a.jquery)&&isF(a.then)&&isF(a.fail)&&isF(a.state));
};
//=======================================
function _ERR () { if (window.console) console.error.apply (console, arguments); }
function _CON () { if (window.console) console.log.apply (console, arguments); }
//=======================================
// Manifest repo getter/setter and helpers
var _cache = function _localCache (A1, A2) {
// ( no args ) – returns all forms obj container
// ({manifest}, {container}) – caches form in container, id must be defined, return form or null
// ({manifest}) – caches form in local container, id must be defined
// ("form.Id", "exist") – true/false
// ("form.Id", {container}) – get from container
// ("form.Id") – get from local cache
var ref, obj;
if (isS(A1)) {
ref=A1;
obj = _getref(isO(A2)?A2:forms, ref);
if ("exist"===A2) return isO(obj);
return !obj?null:Object.clone(obj,true);
} else if (isO(A1)){
obj = _putmanifest (A1, A2);
if (!isO(obj)) {
return null;
}
return obj;
} else if (undefined===A1) {
return forms._src;
} else if (null===A1) {
return Object.reject(forms,/^_/);
}else return null;
};
// - - - - - - - - - - - - - - - - - - - -
var _ajax = $.ajax;
// - - - - - - - - - - - - - - - - - - - -
function _getref (obj, ref) {
// Gets branch of obj by string ref like "data.list.items.1"
// or array ref like ["data","list","items","1"]
return (isS(ref) ?ref.split("."):isA(ref)?ref:[""])
.reduce(function (a,b) {
if (null != a) {
if (/=/.test(b)) {
var q = b.split("=",2);
if (isO(a)) {
return Object.values(a).find(function(elt){
if (!isO(elt) && !isA(elt)) return false;
return (elt[q[0]] == q[1]);
});
} else if (isA(a)) {
return a.find(function(elt){
if (!isO(elt) && !isA(elt)) return false;
return (elt[q[0]] == q[1]);
});
} else return undefined;
}
else if (null != a && null != a[b]) {
return a[b];
}
}
return undefined;
}, obj);
}
// - - - - - - - - - - - - - - - - - - - -
function _manifest (manifest, ref) {
// Dereferences pointer to form component,
// manifest is caller manifest obj,
// internal function
var t, ext;
if (isO(ref)) return ref;
else if (isS(ref)) {
//try to find it on manifest
t = _getref(manifest, ref);
//then in local repo as original
if (null==t) t = Object.clone(forms._src[ref],true);
//then in local repo as part of component
if (null==t) {
t = _getref(forms, ref);
if (isO(t) && isO(t._self)) t=Object.clone(t._self,true);
else if (isO(t)) t = Object.clone(t,true);
}
//then in ext repo as part of component
if (null==t && _getref(manifest,"params.cache")) {
ext = _getref(manifest,"params.cache");
if (isF(ext)) t = ext(ref);
else if (isO(ext)) t = _cache(ref, ext);
if (isO(t)){
if (isO(t._self)) t=Object.clone(t._self,true);
Object.merge(t, {params:{cache:ext}}, true);
}
}
if (null!=t && isO(t)) {
ext = ext||_getref(manifest,"params.cache");
if (ext) Object.merge(t, {params:{cache:ext}}, true);
return t;
}
else throw "Component "+ref+" not found.";
} else if (isF(ref)) {
return ref.apply(manifest, Array.prototype.slice.call(arguments, 2));
} else return null;
}
// - - - - - - - - - - - - - - - - - - - -
function _putmanifest (obj0, root0) {
// Mounts obj to root in a branch, defined in
// obj.id property. If id =="x.y.z", root will be
// deep extended with {x:{y:{z:obj}}}.
// obj also is unjsonned and extended with _self ref,
// which point to original version of obj.
//Returns direct link to entire branch obj or string error.
var i, j, keys, file, root=root0||forms, obj=obj0, path, id, prev, res;
if (!(isO(root) && isO(obj) && isO(obj.ui) && isS(obj.id)))
return "Can’t save manifest into cache, invalid arguments.";
if (!root.hasOwnProperty("_src")) root._src={};
id = obj.id;
try {
obj=Object.clone(obj0, true);
}
catch (e) {
return "Can’t save circular-referencing object into cache.";
}
//unwind string defs of functions
try {
if (!obj.params || (obj.params && !obj.params.strict)) _unjson(obj, true);
}
catch (e) {
return "Invalid manifest, parse error.";
}
//blobify files
i = _files2urls (obj);
if (isS(i)) {
_ERR(i);
return i;
}
//mark manifest as unjsonned
Object.merge(obj,{params:{strict:true}}, true);
// save it
root._src[id] = obj;
if (prev = f.mask(root, id)) {
if (prev.params && prev.params.protect) return "Can’t save manifest into cache over protected one.";
else {
// Remove prev version
if (prev._self) delete prev._self;
$.extend(!0, root, f.unmask("",id));
}
}
// Mount new version
$.extend (!0, root, f.unmask(obj, id));
// Re-mount sub-manifests if any
keys = Object
.keys(root._src)
.filter(function(e){
return e.startsWith(id+'.');
});
keys.sort();
keys.forEach(function(id){
var prev;
if (prev=f.mask(root, id)) {
// Remove subform if any
if (prev._self) delete prev._self;
$.extend (!0, root, f.unmask("",id));
}
// Re-mount version from repo
$.extend (!0, root, f.unmask(root._src[id], id));
});
// Mount _self link to uncompiled manifest
res = _getref(root,id);
if (ie8) res["_self"] = root._src[id];
else Object.defineProperty(res, "_self", {
get: function () { return root._src[id]; },
set: function () { throw "Can’t change manifest cache entry directly.";},
enumerable : false,
configurable : true
});
// End manifest
return res;
}
function _files2urls (obj) {
// Unwinds base64 representations of attached binaries
// into Blobs and objectURLs
var i, flist = [], file;
if (isO(obj.files) && Object.size(obj.files)) {
//blobify files
for (i in obj.files) {
file = obj.files[i];
if (isO(file)) {
if (isS(file.data) && !file.url) {
if (wURL) {
try {
f.base642blob(file.data,function(res){
file.blob = res;
file.url = wURL.createObjectURL(file.blob);
},(file.content_type||file.mime));
flist.push(i);
} catch(e) {
return "Invalid base64 data in files/"+i+".";
}
} else {
//ie8-9 fallback
file.url = 'data:'+(file.content_type||file.mime)+';base64,'+file.data;
flist.push(i);
}
}
}
else return "Non-object member "+i+" in files section.";
}
}
return flist;
}
//########################################################
// Storage of rules defined by cascading selectors
// very similar to css. Leafs are processors
// or processing rules for this type of node
var MY = {
//getter and setter functions for different types of nodes
vals : {
".my-form": function ($o, v) {
//object is jQuery.my instance
if ($o && $o.my ) {var d = $o.my(Da); return Object.equal(d,v)?d:$o.my(Da, v, true);}
else return v||N;
},
".hasDatepicker":function ($o,v) {
//object has jQ UI datepicker
if(n(v)) $o.datepicker("setDate", ((v=="")?v:Date.create(v)));
var date = $o.datepicker("getDate");
return (date?date.format(d8):"");
},
".my-tags": function ($o,v) {
//object is jQ tags control
if (n(v)) {
if (isS(v) || isN(v)) $o.tags(Da,[v+""]);
else if (isA(v)) $o.tags(Da,v);
}
return $o.tags(Da);
},
".ui-draggable": function ($o,v) {
//object is jQ UI draggable
if (n(v) && isO(v)) {
var c = {};
if (!isNaN(v.left)) c.left = Number(v.left).ceil(2)+"px";
if (!isNaN(v.top)) c.top = Number(v.top).ceil(2)+"px";
if (c.left || c.top) $o.css(c);
}
var p = $o.position();
return {
left:((v&&!isNaN(v.left))?(v.left*1).ceil(2):p.left.ceil(2)),
top:((v&&!isNaN(v.top))?(v.top*1).ceil(2):p.top.ceil(2))
};
},
".my-form-list": function ($o,list) {
//object is list of forms
var i,old,xold,mod,eq,ctr = 0,
sP = "ui-sortable", sPlc= "."+sP+"-placeholder",
od = $o.data("formlist")||{},
gen = od.generator||{},
itemSel = gen.selector||">.my-form",
tmpl = gen.template||"<div></div>",
tmplIsVar = /\{/.test(tmpl),
hasher = gen.hash|| f.sdbmCode,
ider = gen.id|| f.sdbmCode,
extHasher = gen.ext,
delay = gen.delay||50,
sortable = $o.is("."+sP),
sorting = !!$o.children(sPlc).size(),
result=[], redraw = [],
$n, $drag, now = Date.now();
var $c = sortable?$o.find($o.sortable("option","items")):$o.find(itemSel);
if (n(list) && isA(list)) {
//return list passed if dragging taking place
if (sorting) return list;
// first we must estimate
// if putting new data over old
// changes anything
old= []; xold={};
$c.each(function (i) {
var $x = $(this), xd = $x.data("my"), dd;
if (xd) {
old.push(xd.data);
if (dd = $x.data("formlist")) {
xold[dd.oid] = xd.data;
}
}
});
//fast compare
eq=false;
if (old && old.length===list.length) for (eq=true, i=0;i<old.length;i++) if (old[i]!==list[i]) eq=false;
if (!eq || extHasher){
// more comprehemsive compare, for example
// applying [{a:1},{b:4}] over [{a:1,b:2},{a:3,b:4}]
// must not force any recalc
mod = $.extend(true, [], old, list);
if (!Object.equal(old,mod) || mod.length!=list.length) {
//we have new data, hash it
var hash=[], oid = [], xoid = {}, present={};
redraw = [];
// Build id <-> idx and hashes
for (i=0;i<list.length;i++) {
oid[i]=ider(list[i], i)+"";
xoid[oid[i]]=i;
hash[i] = (ider!==hasher? hasher(list[i], i):oid[i])+"";
}
//clean childs with no match to new data
$c.each(function () {
var $x = $(this),
md = $x.data("formlist")||{},
coid = md.oid;
if (coid && xoid.hasOwnProperty(coid) && list[xoid[coid]] === xold[coid]) present[coid] = $(this);
else {
if ($x.is(".my-form")) $x.my("remove", true);
else $x.remove();
}
});
//iterate list
for (i=0;i<list.length;i++) {
if (present[oid[i]]) {
$n = present[oid[i]].detach().appendTo($o);
result.push($n.my("data"));
// decide if we must redraw
if (hash[i]!==($n.data("formlist")||{}).hash) redraw.push($n);
} else {
$n = $(tmplIsVar?tmpl.assign(list[i]):tmpl).appendTo($o);
$n.data("formlist",{
list:list[i],
index:i,
hash:hash[i],
oid:oid[i]
});
//ToDo – allow it to be async
$n.my(
_manifest (gen.parent, gen.manifest, list[i], i, list, $o),
list[i]
).then(function($n){
$n.on("change.my", _itemChange.debounce(delay/1.3));
}.fill($n));
result.push($n.my("data"));
}
}
// redraw if any
if (redraw.length && extHasher){
for (i=0;i<redraw.length;i++) _redraw(redraw[i]);
redraw = [];
}
old=xold=mod=null;
return result;
}
}
xold=mod=null;
return old;
} else if ($c.size()) {
if ((now-gen.stamp>1.4*delay)
|| now-gen.stamp > 100
|| !gen.stashed
|| (gen.stashed.length !== $c.size()-(sorting?1:0))
) {
if (sorting) $drag = $o.find('>.'+sP+'-helper');
var ri=0;
$c.each(function (idx, elt) {
var $x = $(elt), xd, xf, chash, dirty = false;
if (!sorting || !$x.hasClass(sP+'-helper')) {
if (sorting && $x.hasClass(sP + '-placeholder')) $x = $drag;
xd = $x.data("my");
xf = $x.data("formlist");
if (xd && xf) {
result.push(xd.data);
if (ri != xf.index) {
xf.index = ri;
dirty = !0;
}
if (extHasher) {
chash = hasher(xd.data, ri)+"";
if (chash !== xf.hash) {
xf.hash = chash;
dirty = !0;
}
}
ri += 1;
if (dirty) redraw.push($x);
}
}
});
for (i=0;i<redraw.length;i++) _redraw(redraw[i]);
redraw=[];
gen.stamp = now;
gen.stashed = result;
}
else result = gen.stashed.slice(0);
return result;
}
return list||[];
function _itemChange () {
var $this = $(this), df, i,
dd=$this.data("my");
if (dd) {
df = $this.data("formlist");
i = df.index;
df.hash = hasher(dd.data, i)+"";
if (ider!==hasher) df.oid = ider(dd.data, i)+"";
else df.oid =df.hash;
}
$o.trigger("check.my");
}
function _redraw($form) {
if ($form.data("my").locktill+delay/1.3<now) $form.my("redraw");
}
},
".ui-sortable": function ($o, list) {
//jQ UI sortable
var a = [],
sP = "ui-sortable",
sPlaceholder= ">."+sP+"-placeholder",
$c = $o.find($o.sortable("option","items")),
$m;
if (n(list) && isA(list)) {
var w = {}, z={}, v = list.unique();
//return list passed if some field has focus of dragging taking place
if (
$o.find("input:focus:eq(0),textarea:focus:eq(0)").size() ||
$o.find(sPlaceholder).size()
) return v;
$c.each(function () {w[f.sdbmCode(f.extval($(this)))] = $(this);});
for (var i=v.length-1; i>=0; i--) {
var j = f.sdbmCode(v[i]);
if (w[j]) {
w[j].prependTo($o).show();z[j]=true;
if (a.indexOf(v[i])==-1) a.push(v[i]);
}
}
a=a.reverse();
for (i in w) if (!z[i]) w[i].hide();
} else {
var $p = $o.find(sPlaceholder), $q = $o.eq(0);
if ($p.size()!=0) {
//if placeholder state changed saving new data
if ($q.my()[sP] != $p.position().left+""+$p.position().top) {
$c = $c.filter(":visible:not(:disabled, .ui-state-disabled, .ui-sortable-helper)");
$m = $o.find($o.sortable("option","items")).filter(".ui-sortable-helper");
$c.each(function () {
var $x = $(this);
if ($x.is(".ui-sortable-placeholder")) {a.push(f.extval($m));}
else a.push(f.extval($x));
});
//caching placeholder state and data retrieved
$q.my()[sP] = $p.position().left+""+$p.position().top;
$q.my()[sP+"1"] = a;
} else a = $q.my()[sP+"1"];
if (a==N) $c.each(function () {a.push(f.extval($(this)));});
} else {
$c = $o.find($o.sortable("option","items"))
.filter(":visible:not(:disabled, .ui-state-disabled)");
$c.each(function () {a.push(f.extval($(this)));});
}
}
return a;
},
"input[type=date]":function ($o,v) {
//object is date input
if(n(v)) {
if (v!="") d = Date.create(v).format(d8); else d = "";
if (isS(d) && !/Invalid/.test(d)) $o.val(d);
return d;
}
var d = $o.val();
return (d!=""?Date.create(d).format(d8):"");
},
"input[type=time]":function ($o,v) {
//object is time input
if(n(v)) {
if (v!="") d = Date.create(v).format(h24); else d = "";
if (isS(d) && !/Invalid/.test(d)) $o.val(d);
return d;
}
var d = $o.val();
return (d!=""?Date.create(d).format(h24):"");
},
"input":{
"[type='text'],[type='number'],[type='search'],[type='hidden'],[type='password'],[type='button'],[type='range'],:not([type])":{
//nearly all main input types and button
".ui-slider-input": function ($o,v) {
//input with jQ UI slider() applied
if (n(v)) $o.val(v).slider("refresh");
},
".tagstrip input.value": function ($o,v) {
//input of tagstrip() applied
if (n(v)) $o.val(v).trigger("update");
},
"div.select2-container+input": function ($o, v) {
//select2
if (n(v) && JSON.stringify(v)!== JSON.stringify($o.select2("val")))
$o.select2("val", (isA(v)?v:[v]));
return $o.select2("val");
},
"": function ($o,v) {if(n(v)) $o.val(v+"");}
},
":radio":function ($o,v) {
//radio buttons
var pos = -1;
if (n(v)) {
$o.each(function (ind) {
var val = $(this).val();
if ((v+"")===(val+"")) pos=ind;
});
var jqcheck = $o.eq(0).checkboxradio;
if (jqcheck) $o.each(function (ind){
var $x = $(this);
if (pos!=ind && $x.is(":checked"))
$x.prop("checked",false).checkboxradio("refresh");
});
if (pos>-1) {
var $x = $o.eq(pos);
if (!$x.is(":checked")) {
$x.prop("checked",true);
if (jqcheck) $x.checkboxradio("refresh");
}
} else if (!jqcheck) $o.each(function () { $(this).prop("checked",false); });
}
if (pos==-1) for (var ind=0; ind<$o.size(); ind++) {
if ($o.eq(ind).is(":checked")) pos=ind;
}
return pos!=-1?$o.eq(pos).val():"";
},
":checkbox": function ($o, v0) {
//checkbox
var pos = -1, v = v0, a = [];
if (n(v)) {
if (!isA(v)) v = [v0];
var jqcheck = !!$o.eq(0).checkboxradio;
$o.each(function (ind) {
var $x = $(this), val = $x.val(), on = $x.is(":checked");
if (v.indexOf(val)!=-1) {
a.push(val);
if (!on) $x.prop("checked", true);
} else if (on) $x.prop("checked", false);
if (jqcheck) $x.checkboxradio("refresh");
});
} else {
$o.each(function () {
var $x = $(this);
if ($x.is(":checked")) a.push($x.val());
});
}
return a;
}
},
"select": {
".ui-slider-switch": function ($o,v) {
//on-off in jQ Mobile
if (n(v)) {
$o.val(v+"");
$o.slider("refresh");
}
},
"div.select2-container+select":{
"[multiple]": function ($o, v) {
if (n(v)) $o.select2("val", (isA(v)?v:[v]));
return $o.select2("val");
},
"":function ($o, v) {
if (n(v)) $o.select2("val", v+"");
return $o.select2("val");
}
},
"[multiple]": function ($o,v) {
if (n(v)) {
$o.val(v,[]);
if ($o.selectmenu && ($o.data("uiSelectmenu") || $o.data("selectmenu"))) $o.selectmenu("refresh",true);
//the only way to check if we have jQ UI selectmenu() attached
}
},
"": function ($o,v) {
if (n(v)) {
$o.val(v+"");
if ($o.selectmenu && ($o.data("uiSelectmenu") || $o.data("selectmenu"))) $o.selectmenu("refresh",true);
}
}
},
"textarea": {
".my-cleditor":function ($o,v) {
if(n(v)) $o.val(v).cleditor()[0].updateFrame();
return $o.val();
},
"div.redactor_box textarea,.redactor": function ($o,v) {
var r9 = $o.hasClass('my-redactor-9');
if(n(v)) {
if(r9) $o.redactor('set', v);
else $o.setCode(v, false);
return v;
}
return r9 ? $o.redactor('get') : $o.getCode();
},
".my-codemirror":function ($o,v){
if (n(v)) {
$o[0].nextSibling.CodeMirror.setValue(v);
return v;
}
return $o[0].nextSibling.CodeMirror.getValue();
},
"":function ($o,v) {if(n(v)) $o.val(v+"");}
},
"fieldset,form,section,footer,aside,.my-container": function ($o, v) {
//object is class-manageable container,
//value is an array of css rules
var clist = Array.prototype.slice.call($o[0],0).sort(),
list = v;
if (n(v)) {
if (isS(v)) list = v.split(/[,\s]+/).compact(true);
if (isA(list)) {
list.sort();
if (list.join(" ")!==clist.join(" ")) {
$o.atrr("css", list.join(" "));
clist = list;
}
}
}
return clist;
},
"div,span,a,p,li,td,th,h1,h2,h3,h4,h5,h6":{
".ui-slider":function ($o, v){
if(n(v)) $o.slider("option",$o.slider("option","values")?"values":"value", f.clone(v));
return f.clone($o.slider("option","values")||$o.slider("option","value")||0);
},
".ui-buttonset": function ($o,v) {
//jQ UI buttonset ()
if (!n(v)) {
var jor = $o.find(":radio:checked");
if (jor.size() && jor.button) return jor.val()||jor.button("option", "label") ;
} else if (v!="") {
var jon = N;
$o.find(":radio").each(function () {
jon=( ($(this).val()||$(this).button("option", "label"))==v?$(this):jon );
});
if (jon) {
jon.attr("checked",true);
$o.buttonset("refresh");
return v;
}
}
$o.find(":radio:checked").attr("checked",false);
$o.buttonset("refresh");
return "";
},
".ace_editor":function ($o,v) {
if(n(v)) ace.edit($o[0]).setValue(v);
return ace.edit($o[0]).getValue(v);
},
"": function ($o,v) {
if(n(v)) $o.html(v);
return $o.html();
}
},
"pre,code":function ($o,v) {
if(n(v)) $o.html(v);
return $o.html();
},
"img":function ($o,v) {
if (n(v)) $o.attr("src",v);
return $o.attr("src")||"";
},
"":function ($o,v) {
if (n(v)) $o.html(v);
return $o.html()||$o.text()||String($o.val())||"";
}
},
//messages
//########################################################
msg:{
"":{en:"Invalid input", ru:(TMP="Неверное значение")},
formError:{en:"Form error",ru:"Ошибка формы"},
initFailed:{
en:'<p class="my-error">Form init failed</p>',
ru:'<p class="my-error">Ошибка инициализации формы</p>'
},
badInput:{en:"Invalid input", ru:TMP},
patternMismatch:{en:"Pattern mismatch", ru:"Не соответствует шаблону"},
rangeOverflow:{en:"Over maximum", ru:"Больше максимума"},
rangeUnderflow:{en:"Under minimum", ru:"Меньше минимума"},
stepMismatch:{en:"Step mismatch", ru:"Не кратно шагу"},
tooLong:{en:"Too long", ru:"Слишком длинно"},
typeMismatch:{en:"Invalid type", ru:"Неверный тип"},
valueMissing:{en:"Required", ru:"Обязательное поле"}
},
// Different controls’ events to watch for
// ########################################################
events: {
".hasDatepicker":"change.my check.my",
".my-form,.my-tags":"change.my check.my",
".ui-slider":"slide.my check.my",
"div.redactor_box textarea":"redactor.my check.my",
".my-codemirror":"codemirror.my check.my",
".ace_editor":"ace.my check.my",
".my-form-list":"sortupdate.my check.my",
".ui-sortable":"sortchange.my sortupdate.my check.my",
".ui-draggable":"drag.my dragstop.my check.my",
"a, .pseudolink, input[type=button], button": "click.my",
"img, :radio, :checkbox": "click.my check.my",
"div.select2-container+input,div.select2-container+select":"change.my check.my input.my",
".ui-buttonset,input, select, textarea":
"blur.my change.my check.my"+(navigator.appName.to(5)==="Micro"?" keyup.my":" input.my"),
"":"check.my"
},
// Functions retrieving container for different controls
// ########################################################
containers: {
"*[data-role='fieldcontain'] *":{ //jQuery Mobile
"input,textarea,select,button,:radio": function ($o) {
return $o.parents('[data-role="fieldcontain"]').eq(0);
}
},
".tagstrip *.value": function ($o){ //$.tagstrip()
return $o.parents('.tagstrip').eq(0);
},
"div.redactor_box textarea":function ($o){
return $o.parents('div.redactor_box').eq(0).parent();
},
".my-tags,.hasDatepicker,.ui-widget,input,textarea,select,button" :{
".my-cleditor": function ($o) {
return $o.parents('div.cleditorMain').eq(0).parent();
},
"": function ($o) {
var p = $o[0].parentNode, t = p.nodeName;
if (/^(div|span|a|p|form|fieldset|li|ul|td|th|h\d)$/i.test(t)) return $(p);
else return $o.parents('div,span,a,p,form,fieldset,li,ul,td,th,h1,h2,h3,h4,h5,h6').eq(0);
}
},
"": function ($o) {return $o;}
},
// Disablers and enablers
// ########################################################
offon: { //if x==true disables control else enables
".ace_editor": function (x,$o) {ace.edit($o[0]).setReadOnly(x);},
".ui-selectable": function (x,$o) {f.jquix($o,"selectable",x);},
".ui-slider": function (x,$o) {f.jquix($o,"slider",x);},
".ui-draggable": function (x,$o) {f.jquix($o,"draggable",x);},
".ui-buttonset": function (x,$o) {f.jquix($o,"buttonset",x);},
".hasDatepicker": function (x,$o) {f.jquix($o,"datepicker",x);},
".my-form":function (x,$o){$o.my("disabled", !!x);},
"div.select2-container+input,div.select2-container+select":
function (x,$o) {f.jquix($o,"select2",x);},
".my-cleditor": function (x,$o) { $o.cleditor()[0].disable(!!x);},
"": function (x, $o) {$o.attr("disabled", !!x);}
},
// Destructors
// ########################################################
destroy:{
".hasDatepicker":function ($o){$o.datepicker("destroy");},
".ui-slider":function ($o){$o.slider("destroy");},
".ui-sortable":{
".my-form-list":function ($o){
$o.find(">.my-form").each(function () {
$(this).my("remove");
});
$o.removeClass("my-form-list");
$o.sortable("destroy");
},
"":function ($o){$o.sortable("destroy");}
},
".my-form-list":function ($o){
$o.find(">.my-form").each(function () {
$(this).my("remove");
});
},
".ui-draggable":function ($o){$o.draggable("destroy");},
".my-redactor-8":function ($o){
$o.destroyEditor();
$o.removeClass("my-redactor-8");
},
"div.select2-container+input,div.select2-container+select":
function ($o){$o.select2('destroy');},
".my-form": function ($o) {$o.my("remove");},
"textarea": {
".my-codemirror": function ($o) {
$o[0].nextSibling.CodeMirror.toTextArea();
$o.removeClass("my-codemirror");
}
}
}
};
// Default values for .params section of manifest
// ########################################################
MY.params = {
container:function ($o) { // container getter
return _traverse($o, MY.containers)($o);
},
change:N,
recalcDepth:2, // depth of dependencies resolver tree
delay:0, // default delay of bind invocation
strict:false, // if true form assumed unjsonned
restyle:-1, // delay of <style> repaint on screen resize, -1 for no repaint
locale:(TMP=(navigator.language||navigator.userLanguage||"en").substr(0,2)),
messages:Object.map(MY.msg, function (k,v){return v[TMP]||v.en;}),
errorTip:".my-error-tip", // $ selector of err tip container
errorCss:"my-error", // class applied on container on err
animate:0, // err tips animation duration
effect: function ($e, onoff, duration) { // err tips animation effect
if (onoff) return $e.fadeIn(duration); $e.fadeOut(duration);
},
remember:0, // undo steps to remember
silent:false,
history:{}, // form undo history
historyDelay:100, // delay in ms between calls of history(),
loader: function (manifestId, parentId) { // default loader
var pi = $.Deferred(), m = _cache(manifestId+"");
if (m) pi.resolve(m);
else pi.reject(null);
return pi.promise();
},
ajaxTimeout:10000
};
var f = _getHelpersLib();
//########## SYSTEM FUNCTIONS ##########
//=======================================
// Pub/sub broadcaster
function _broadcast($root, msg) {
var supress = false, fc, next;
if (isO(msg) && msg.channel && msg.message) {
$root.find(".my-listen-"+f.sdbmCode(msg.channel))
.each(function(){
var $c = $(this),
my = $c.data("my"),
fn, v=undefined;
if (my && my.ui.listen && isF(my.ui.listen[msg.channel]) ) {
fn = my.ui.listen[msg.channel];
try{
v = fn.call( my.manifest || my.root.my().manifest, my.data, msg.message, $c);
} catch(e) {
_ERR("Listener failed", e.message, e.stack);
}
if (v!==undefined) {
if (v===null) $c.trigger("check");
else if (v) $c.trigger("recalc");
}
}
});
}
}
//=======================================
// Require
function _require(man, params0){
// Checks and loads required libs,
// returns promise resolved with manifest
// or rejected with err list.
var i, j, k, pi = $.Deferred(),
chunks = [], checks = {}, err=[], r, line,
params = $E(true, {
ajaxTimeout:10000,
loader: MY.params.loader
}, params0||{}),
row, subrow, chunk,
Row = {ref:null, ajax:{type:"GET", async:true, timeout: params.ajaxTimeout}};
if (!isO(man)) pi.reject(["Invalid manifest."]);
else if (!isA(man.require)) pi.resolve(man);
else {
r = man.require;