-
Notifications
You must be signed in to change notification settings - Fork 0
/
EZgetset.work2.js
937 lines (820 loc) · 34.8 KB
/
EZgetset.work2.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
/*global
EZgetFieldValue,
EZselectScroll,
EZnone,
EZgetEl:true, EZgetValue:true, EZgetValues:true, EZsetValue:true,
EZ, dw:true, DWfile, e:true, g */
var e;
(function jshint_globals_not_used() { //global variables and functions defined but not used
e = [
e, g, dw, DWfile]
});
/*---------------------------------------------------------------------------------------------
EZ.getEl(selectors, doc) -- FULL FUNCTIONALITY --
If multiple elements match the selector or the selector is a collection al elements, only the
1st element is returned. If the selector is an array, all elements matching all non-empty
selectors is returned. To always get a single element for selector arrays, use EZ._().
if no element is found matching any of specified selectors, a generic element is returned not
connected to anything visible. It has all the most common properties and attibutes of any tag.
This avoids js errors when subsequent code assumes an element or field is returned which often
happens when selectors are mis-spelled or elements are deleted w/o deleting all related script.
Only enough selectors are tried until one or moe elements match form fields or elements
UNLESS called from EZ.getAll() in which case all selectors are processed. In either case,
selectors are used until match found for fieldname, id, tagName or className in that order.
ARGUMENTS:
selectors one or more html elements or a comma delimited string of selector(s), or array
of elements and selectors (only one element or selector allow per array item)
doc (optional) root element used to locate elements matching specified selectors
defaults to document if only 2 args and 2nd arg is options
options (optional) determines how many elements are returned and/or an array containing
(last arg) one or more elements is returned as explained below:
false only one element returned
true only 1st element found is returned as single element array
@ all elements matching any of the selectors always returned as
array even if none are found
default =true when selectors arg is array =false otherwise
RETURNS:
either single element or array of elements matching one or more selectors -- if no elements
are found matching any of the supplied selectors, a pseudo element is created and returned.
TODO:
support selector prefixes and only try matching associated type:
"#" element id -- "." className -- "@" fieldName -- "<" tagName with or w/o ">" suffix
when id or field name specified, only select if under doc root
compound seelectors e.g. #someid.li ul
multiple arguments containing more selectors ??
// determine if stopping after 1st match or using all all selectors
var patternfunction = /function\s*(\w*)\s*\((.*?)\)[^{]*{\s*([\s\S]*)}/;
var callerName = EZ.matchPlus(arguments.callee.caller, patternfunction)[1];
var stopAfterFirst = /^(RZ|EZ)_$/.test(callerName) //always stop if caller EZ_()
|| /^(RZ|EZ)\$$/.test(callerName) //never stop if caller EZ$() or ...
|| (!EZ.is(els) && els.length > 1); //...multiple selectors but not els
--------------------------------------------------------------------------------------------------*/
EZ.getEl = EZgetEl = function EZgetEl(selectors, nodes)
{
var settings = EZ.is(nodes, EZ.getEl.getSettings)
? EZ.merge(nodes, {selectors:selectors}) //called with pre-defined settings
: new EZ.getEl.getSettings(this, arguments); //breakpoint for settings not defined
/** PARTIAL LIST OF SETTINGS:
*
* one:T/F only return 1st html element found -- all:T/F return Array of all tags for all selectors
*
* root(s) starting dom nodes -- nodes: additional starting nodes -- doc: legacy single root
*
* selectors: Array of selectors: only first match unless specified as Array (implicit all:true)
*
* notFound: object returned if no tags found: EZnone by default -- null if nodes=null
*/
//--------------------------------------------------------------------
// legacy code -- doc must be document object
// use above new code for correct doc value
//--------------------------------------------------------------------
if (settings.legacy)
{
var doc = settings.doc;
var els = settings.selectors[0];
for (var i=0; i<els.length; i++)
{
var el = els[i] || '';
if (el == null) continue;
if (typeof(el) != 'object' && doc.forms.length && doc.forms[0][el])
el = doc.forms[0][el]; //el is form field
if (typeof(el) != 'object') //if not object, try as id
el = doc.getElementById(el + '');
if (el == null)
{
var tags = document.getElementsByTagName(els[i]+'');
if (tags.length) return tags[0];
}
if (el) return el; //return if element
}
return null; //null if el not found
}
//--------------------------------------------------------------------
// newest code
//--------------------------------------------------------------------
var tags = [];
//----- Nothing selected
while (settings.selectors.length > 0 && settings.roots.length > 0)
{
//----------------------------------------------------------------------------------
//----- FAST SELECT: all selectors are elements
//----------------------------------------------------------------------------------
if (EZ.isEl(settings.selectors)) //nodes exploded && (!settings.nodes.length || EZ.isEl(settings.nodes)))
{ //keep tags in default or specified root(s)
tags = settings.selectors;
if (!EZgetEl_getDescendants(tags, settings.roots))
break;
if (settings.nodes.length)
EZgetEl_getDescendants(tags, settings.nodes);
break;
}
//----------------------------------------------------------------------------------
//----- Full selection algorithm
//----------------------------------------------------------------------------------
[].every.call(settings.selectors, function forEverySpecifiedSelector(sel)
{
if (!EZgetEl_isSelectorType(sel) || sel == null) return true; //skip invalid type
sel = EZ.toArray(sel, false); //TODO ','
[].every.call(settings.selectors, function forEveryCommaDelimitedSelector(sel)
{
if (!EZgetEl_isSelectorType(sel) || sel == null) return true;
return [].every.call(settings.roots, function forEveryRootElement(root)
{
//-----------------------------------------------------------------------------
// recursively call EZ.getEl() for chained selections from:
// optional nodes argument when called from context element...
// ...fully exploded into elements by getSettings()
// -AND/OR- by space delimited selectors ... exploded below
//-----------------------------------------------------------------------------
var chainedSelections = EZ.toArray(sel,' ');
sel = chainedSelections.pop(); //remove single selector or last in chain
[
settings.nodes, //1st apply nodes selector
chainedSelections //selector(s) exploded except last one
].forEach(function forEachChain(nodes)
{
if (nodes.length > 0) debugger;
if (EZ.quit) return false;
//for ALL nodes and chained selections . . .
nodes.forEach(function forEachChainedSelector(node)
{ //get and save ALL matching tags . . .
EZgetEl_addElements(nodes, EZ.getEl(node, settings.baseSetting));
}); //last selector applied to all matches
});
//----- if selector is element, array of elements or html collection
if (EZ.isEl(sel) //...only add elements descendant of roots??
&& EZgetEl_getDescendants(sel,root))
if (EZgetEl_addElements(sel)) return;
//----------------------------------------------------------------------------
// for compound selector (e.g #footer.div.input) keep checking until any part
// of selectors does not match or all parts checked.
//-----------------------------------------------------------------------------
var parts = EZgetEl_getCompoundParts(sel);
if (!parts) parts = [];
if (parts.length > 1) debugger;
if (EZ.quit) return false;
return parts.every(function forEverySelectorPart(sel)
{
if (sel == null) return false; //invalid compound
if (EZ.isArray(sel)) //sel is Array of parts if compound . . .
{
return sel.every(function forEveryMatchingPart(sel)
{
if (!sel) return true; //continue empty or EZnone selector
el = EZ.getEl(el, settings.baseSettings);
if (!el.length) //if not found, break out of loop
return false;
if (EZgetEl_addElements(el)) //also break if one is enough
return false;
});
}
//??? NOTE: below code is only run via recursive if above compound code runs
var e, prefix = '';
if (typeof(sel) == 'string')
{
var groups = sel.matchPlus(/([#.<@])?(.*?)(>|$)/);
prefix = groups[1]; //if selector qualified with: # . < -or- @
sel = groups[2]; //remove qualifier
}
if ('@'.indexOf(prefix) < 1)
{ //try for form field match
e = EZgetEl_getFormField(sel,root)
if (e != null)
{
if (EZgetEl_addElements(e)) //quit if one is enough
return false;
}
}
if ('#'.indexOf(prefix) < 1)
{ //try for id match
e = EZ.getEl.getDocument(root).getElementById(sel);
if (EZ.isEl(e) && EZgetEl_getDescendants(e,root) != null)
{
if (EZgetEl_addElements(e)) //quit if one is enough
return false;
}
}
if ('<'.indexOf(prefix) < 1)
{ //try as tagName
e = root.getElementsByTagName(sel);
if (EZ.isEl(e))
{
if (EZgetEl_addElements(e)) //quit if one is enough
return false;
}
}
if ('.'.indexOf(prefix) < 1)
{ //try as css class
e = root.getElementsByClassName(sel)
if (EZ.isEl(e))
{
if (EZgetEl_addElements(e)) //quit if one is enough
return false;
}
}
return true; //breakpoint here for not found or selecting all
}); //end of compound parts
}); //end of roots
}); //end of comma delimited selector
}); //end of selectors
break;
} //end Fast or Full selection
//----- Set tags to settings.notFound if no tags selected
if (!tags.length)
{
var notFound = '.notFound'.ov(settings) || 'EZnone';
tags = notFound == 'EZnone'
? [EZnone(settings)] //EZnone() pseudo tag is default
: typeof(settings.notFound) == 'object'
? settings.notFound //e.g. [] or {} and null
: [settings.notFound]; //Array if notFound is not object
}
else
tags = tags.removeDups();
EZ.tags = tags;
EZ.el = tags[0];
//================================================================================
if (settings.one && tags != null) //if returning 1st tag...
return EZ.bindElements(tags[0]) //...bind 1st tag if not yet bound
else //if returning Array or html collection...
return EZ.bindElements(tags) //...bind Array/collection and tags
//================================================================================
/**
* Add nodes to tags array, return true to continue selecting elements
*/
function EZgetEl_addElements(nodes)
{
if (nodes == null) return true;
if (settings.asis && !tags.length && EZ.isArrayLike(nodes))
tags = nodes; //save html collection as is if no other tags
else
{
if (!EZ.isArrayLike(tags)) //html collection...
tags = [].slice.call(tags); //...copy to new Array
EZ.toArray(nodes).forEach(function(el)
{
tags.push(el) //add each node to tags
});
}
return !settings.all; //return true to quit selecting more nodes
}
/**
* return selector parts as Array of selectors for compound selectors
* otherwise return selection as single element Array.
*
*/
function EZgetEl_getCompoundParts(sel)
{
if (typeof(sel) != 'string') return [sel]; //return if not string
if (/:\]\[]/.test(sel)) return undefined; //unsupported selector char
if (!/[#.:\]\[\w\d]/.test(sel)) return null; //invalid selector char
var groups = sel.matchPlus(/(\S)(([#.])([\w\d]*))/);
if (groups) return [sel]; //if nothing found, not compound
var parts = [];
if (/^[\w\d]/.test(groups[0])) //1st part is tag
parts.push('<' + groups.shift());
while (groups.length) //process remaining parts
{
var prefix = groups.shift();
if (!groups.length) return null; //invalid if last taken
if (/^[\w\d]/.test(groups[0])) return null; //invalid if not followed by word
parts.push(prefix + groups.shift());
}
return parts;
}
/**
* Get html collection of all form field(s) matching selector -- return any that are decendents
* of element that called EZ.getEl() -or- all elements if EZ.getEl() was not called from element.
*
* return null if none found or none are decendents otherwise return those that are decendents.
*/
function EZgetEl_getFormField(sel,root)
{
var tags = [];
[].every.call(EZ.getEl.getDocument(root).forms, function forEveryForm(form)
{
if (!form[sel]) return true; //keep looking until field(s) found
if (false && !tags.length)
tags = EZ.toArrayLike(form[sel]); //retain html collection
else
{
tags = [].slice.call(tags);
tags = tags.concat(EZ.toArray(form[sel]));
}
//tags.push(form[sel]);
if (EZgetEl_getDescendants(tags,root) == null)
return true; //keep looking if no tags are descendants
});
return (tags.length > 0) ? tags : null;
}
/**
* return tags that are decendents of element that called EZ.getEl()
disabled this test
-OR- all tags returned when
* EZ.getEl() not called from an element i.e. EZ.getEl.settingsroot is document object.
*/
function EZgetEl_getDescendants(tags, root)
{
if (tags == null) return tags; // || root == document //could have diff document
tags = EZ.toArray(tags, false);
for (var idx=tags.length-1; idx>=0; idx--)
{
//if (root.indexOf(tags[0].ownerDocument) != -1) continue;
if (!EZ.isAncestor(tags[idx], root))
Array.prototype.splice.call(tags, idx, 1); //remove tags[idx] is not decendent
}
return (tags.length > 0) ? tags : null;
}
/**
* return true if valid variable typeof and valid element, Array or collection of elements
* TODO: ^([.#a-zA-Z0-9_:[\])])+[,><a-zA-Z0-9_~=\"\":[\] ]*?$
*/
function EZgetEl_isSelectorType(sel)
{
if ((!sel && sel !== '0')
|| 'string number object'.indexOf(typeof(sel)) == -1)
return false; //skip empty or EZnone selector
if (typeof(sel) == 'object' && !EZ.isEl(sel))
return false;
return true;
}
}
//_____________________________________________________________________________________________
EZ.getEl.test = function ()
{
/*global
tstArrayObj, tstFn, tstObj, tstObjArrayLike,
tstObjEmptyArray, t_doc, t_html, t_head, t_title, t_body, t_wrap, t_labels, t_inputs,
t_forms, t_fm, t_none, t_tags, t_array, t_divs, t_radios, t_radio01, t_label_some,
t_mixed, t_idandclass
*/
(function jshint_globals_not_used() { //global variables and functions defined but not used
e = [
tstArrayObj, tstFn, tstObj, tstObjArrayLike,
tstObjEmptyArray, t_doc, t_html, t_head, t_title, t_body, t_wrap, t_labels, t_inputs,
t_forms, t_fm, t_none, t_tags, t_array, t_divs, t_radios, t_radio01, t_label_some,
t_mixed, t_idandclass
]})
var note
EZ.test.run("----- backup copy of saved data -----", {EZ: {note:'not chained selector'}});
if (true) return;
var ex_radios = [].slice.call(t_radios);
EZ.test.run(['EZtest_radio'], {EZ: {ex:ex_radios, note:'selector: radio group name'}});
EZ.test.run(['test_id1'],t_wrap, {EZ: {ex:t_idandclass, note:'id AND class'}});
EZ.test.run('input',t_wrap, {EZ: {ex:t_inputs[0], note:'field'}});
EZ.test.run('EZTEST_TAG' ,t_wrap, {EZ: {ex:t_tags[0], note:'tag' }});
EZ.test.run('EZtest_class0',t_wrap, {EZ: {ex:t_tags[0], note:'class'}});
EZ.test.run('test_id1',t_wrap, {EZ: {ex:t_labels[1], note:'id selector'}});
EZ.test.run('.test_id1',t_wrap, {EZ: {ex:t_inputs[3], note:'class not id' }});
EZ.test.run('@input',t_wrap, {EZ: {ex:t_inputs[0], note:'field'}});
EZ.test.run('<tags',t_wrap, {EZ: {ex:t_tags[0], note:'tag' }});
EZ.test.run('.class1',t_wrap, {EZ: {ex:t_tags[1], note:'class'}});
//EZ.test.run(tags.EZ, '', {EZ: {ex:t_none, note:'' }});
EZ.test.run(null, {EZ: {ex:t_none, note:'' }});
EZ.test.run(0, {EZ: {ex:t_none, note:'' }});
EZ.test.run('0', {EZ: {ex:t_none, note:'' }});
EZ.test.run('tag', {EZ: {ex:t_tags[0], note:'' }});
EZ.test.run(['tag'], {EZ: {ex:t_tags, note:'' }});
EZ.test.run(['tag'],false, {EZ: {ex:t_tags[0], note:'' }});
EZ.test.run('tag', true, {EZ: {ex:t_tags, note:'' }});
EZ.test.run('tag', false, {EZ: {ex:t_tags[0], note:'' }});
EZ.test.run('tag', null, {EZ: {ex:t_tags, note:'' }});
EZ.test.run('notags', {EZ: {ex:t_none, note:'' }});
EZ.test.run('notags', null, {EZ: {ex:null, note:'' }});
EZ.test.run('div', t_tags[1], {EZ: {ex:t_divs[1] }});
EZ.test.run('div', {EZ: {ex:t_divs }});
EZ.test.run('radio','div', {EZ: {ex:t_radios[1] }});
EZ.test.run('radio',t_tags[1], {EZ: {ex:t_radio01, }});
EZ.test.run('radio', {EZ: {ex:t_radio01, ctx:t_tags[1] }});
//not sure how these should work -- convert to chained ??
EZ.test.run('radio',t_divs[1], {EZ: {ex:null, ctx:t_tags[1] }});
EZ.test.run('radio','div', {EZ: {ex:null, ctx:t_tags[1] }});
EZ.test.run('radio','notag', {EZ: {ex:null, ctx:t_tags[1] }});
note = 'not document descendant'
var div = document.createElement('div'); //alot more descendand scenarios
EZ.test.run(EZ.getEl, div, {EZ: {ex:null, note:note}} );
}
/**
* return document object for el
*/
EZ.getEl.getDocument = function EZgetEl_getDocument(el)
{
return EZ.is(el, document.constructor)
? el //el is document object
: el.ownerDocument
|| document; //safety for expected
}
/*---------------------------------------------------------------------------------------------
* 1st arg is always selector(s); if more optional arguments . . .
*
* options last arg if boolean, null or any non-dom html object*
* defaultOptions 2nd arg if more than 2 args and any non-dom html object*
*
* nodes 2nd arg when 2nd arg is NOT options, defaultOptions,
* undefined, null or blank -and- IS dom html element object*
* or selector specified as string or Array otherwise...
* default: document
*
* NOTE: * EZ.isNonElObject() or EZ.isEl() used to determine if dom object or not
-----------------------------------------------------------------------------------*/
EZ.getEl.getSettings = function EZgetEl_getSettings(ctx, args)
{
//----- context root element(s) -- EZget/EZset pass context elements as nodes
this.roots = ctx == window || typeof(ctx) == 'function' ? []
: EZ.toArray(ctx); //breakpoint here for element context
var args = args.length ? [].slice.call(args) : ['']; //get real non-empty Array
//----- get defaultOptions -- its last arg if object containing defaults property
var defaultOptions = '.defaults'.ov(args[args.length-1])
? args.pop().defaults
: '';
//----- get options -- its now last arg if any of following:
var options = args[args.length-1];
options = (options === true) ? {all: true, one:false}
: (options === false) ? {all: false, one:true}
: (options === null) ? {notFound: false}
: EZ.isObject(options) ? options
: ''; //breakpoint here for options NOT found
if (options)
args.pop(); //breakpoint here for options found
//----- get selectors argement -- now next item in args --
this.selectors = args.shift();
var isMultipleSelectors = (EZ.isArray(this.selectors) && !EZ.isEl(this.selectors));
//keep ArrayLike html collection...
this.selectors = EZ.toArray(this.selectors, false); //...so it can returned "as is"
//----- get nodes argument -- now next item in args
this.nodes = EZ.toArray(args.shift()); //clone nodes html collection as...
//...real Array its not returned
//---------------------------------------------------------------------------------
// set overrideable options to specified value or defaults
//---------------------------------------------------------------------------------
var settings = { //value or object returned when no tags found
notFound: 'EZ.legacy.EZgetEl.notFound'.ov('EZnone'),
uniqueEZnone: false, //use unique EZnone when needed
all: isMultipleSelectors, //return all tags found by all selectors
one: !isMultipleSelectors, //return 1st tag found only 1st tag of collection
asis: false, //html colections returned as is
legacy: options ? false : 'EZ.legacy.EZgetEl'.ov(false)
}
this.baseSettings = EZ.merge({mergeOptions:{append:false}}, settings, defaultOptions, options);
EZ.merge(this, this.baseSettings);
// base settings for recursive calls to EZ.getEl()
this.baseSettings = EZ.merge(settings, {legacy:false, notFound:[], all:true, one:false});
//----- get elements for any context roots and/or nodes not specified as elements
EZgetEl_getElements(this.roots, this.baseSettings, document);
EZgetEl_getElements(this.nodes, this.baseSettings, this.roots);
//------ if roots not specified, use for nodes if specified
if (!this.roots.length && this.nodes.length > 0)
{
this.roots = this.nodes;
this.nodes = [];
}
if (!this.roots.length)
this.roots = [document];
if (this.selectors.length > 1
|| (this.nodes && this.nodes.length > 0) //if not moved to roots
|| (this.roots.length > 1 || !EZ.is(this.doc,document.constructor)))
this.legacy = false;
if (this.legacy)
this.doc = this.roots[0];
//_________________________________________________________________________________
/**
* recursive calls to EZ.getEl() to find elements for non-element selectors
* no nodes filter, array of all selections, empty array if no tags found.
* Uses pre-defined settings so EZ.getEl_getSettings() is not called again.
*/
function EZgetEl_getElements(tags, baseSettings, roots)
{
if (!tags) return;
if (!EZ.isArrayLike(tags) && !EZ.isEl(tags)) return;
if (EZ.isArrayLike(tags) && tags.length === 0) return;
var root = roots && roots.length && roots.length > 0 ? roots : document;
var settings = EZ.merge({mergeOptions:{maxdepth:0}}, baseSettings, {roots:root})
for (var idx=tags.length-1; idx>=0; idx--) //for each tag in reverse order
{
var el = tags[idx]; //replace each non-el selector with 0+ html elements
if (!EZ.isEl(el))
{
el = EZ.getEl(el, settings);
var spliceArgs = [].concat([idx,1], [].slice.call(el) );
Array.prototype.splice.apply(tags, spliceArgs);
}
}
}
}
/*---------------------------------------------------------------------------------------------
return element value(s) as array -- useful for multi-select field
blank or no values returned as ??
---------------------------------------------------------------------------------------------*/
EZ.getValues = EZgetValues = function EZgetValues(el, defaultValue)
{
var tags = EZ.toArray(el); //returns empty Array if el null, blank or undefined
var values = [];
[].forEach.call(tags,function(el)
{
var val = EZgetValue(el, defaultValue);
values = values.concat( EZ.toArray(val, '|') )
});
return values;
}
/*---------------------------------------------------------------------------------------------
return element value -- cloned from EASY.js beta:
if unknown el return defaultValue or undefined
if form field, return field value else el.innerHTML if defined otherwise null
TODO: defaultValue may not be complete
consolidate, merge or sync with EZget(), EZval() and EZget_basic()
---------------------------------------------------------------------------------------------*/
function EZgetValue(el, defaultValue)
{ //el arg is single element, html collection or selector(s)
el = EZ.getEl(el,true)[0]; //get 1st element of collection or matching 1st selector
if (!el || el.undefined)
return defaultValue; //TODO: return default or undefined??
var name = el.name || el.id || '';
var value = el.value != EZ.undefined ? el.value
: el.innerHTML != EZ.undefined ? el.innerHTML
: '';
switch (EZ.getTagType(el))
{
case 'textarea':
case 'text':
case 'password':
case 'hidden':
return value;
case 'radio':
{ //get all radio group tags
var tags = document.getElementsByName(el.name || el.id || '');
if (el.name && tags.length > 1) //fall thru to checkbox if single button
{
for (var i=0; i<tags.length; i++)
{
if (!tags[i].checked) continue;
value = EZ.getTagValue(tags[i]);
if (value !== '') //return value if defined...
return value.isTrueFalseValue();
var id = tags[i].id;
if (id && id != name) //...return id if diff from name
return id;
//...return parent label text if any
var label = EZ.getParent(tags[i], 'label'); //see EZ.getAncestor()
if (!EZ.isNone(label))
return EZ.trim(label.innerText).isTrueFalseValue();
return ''; //no value, id or label text found for checked button
}
return ''; //no button checked
}
}
/* jshint ignore:start*/ //FALL-thru
case 'checkbox':
/* jshint ignore:end */
{
value = EZ.getTagValue(el);
if (value !== '')
return el.checked ? value : '';
else
return el.checked;
}
/* jshint ignore:start*/ //FALL-thru
case 'select':
/* jshint ignore:end */
{ //use EZgetFieldValue() if el.selectedIndex >= 0
if (el.selectedIndex == -1) return '';
if (typeof(EZgetFieldValue) == 'function')
return EZgetFieldValue(el);
return el.options[el.selectedIndex].value || el.options[el.selectedIndex].text
}
/* jshint ignore:start*/ //FALL-thru
default:
/* jshint ignore:end */
{ //return innerHTML if defined
if (el.innerHTML != EZ.undefined)
return value;
}
}
return null; //TODO: should null ever be returned??
}
/*---------------------------------------------------------------------------------------------
set element value -- cloned from EASY.js beta:
set and for convenience return value
if form field, set field value otherwise set el.innerHTML
TODO: defaultValue may not be complete
consolidate, merge or sync with EZset(), EZval() and EZset_basic()
select/ text field -- .replace(/(\r\n|\n|\r)/g, EZ.constant.EOL);
var type = /input/i.test(el.tagName || '')
? el.type //use tag.type for input tag
: el.tagName || ''; //otherwise tagName if defined
switch (type.toLowerCase())
---------------------------------------------------------------------------------------------*/
function EZsetValue(el, value, defaultValue)
{
var els = EZ.getEl(el,true); //get all element(s)
if (!els || els.undefined) //if no field or element found -- return blank
return '';
if (!EZ.isArray(el)) //only set 1st if el was not Array
els = [els[0]]
var values = [];
els.forEach(function(el)
{
var tagName = (el.tagName || '').toLowerCase();
var name = el.name || el.id || '';
EZ.wasValue = '';
value = (value != EZ.undefined) ? value //value, defaultValue or blank
: (defaultValue !== undefined) ? defaultValue : '';
var isValueTrueLike = EZisTrueLike(value);
var isValueFalseLike = EZisFalseLike(value);
switch (EZ.getTagType(el))
{
case 'textarea':
case 'text':
case 'password':
case 'hidden':
{
EZ.wasValue = el.value;
return values.push(el.value = value.toString());
}
case 'radio':
{ //get all radio group tags
var tags = document.getElementsByName(el.name || el.id || '');
if (el.name && tags.length > 1) //fall thru to checkbox if not radio group
{ //length=1 is possible forgot when
value = value.toString();
var trueLikeButton = '';
var falseLikeButton = '';
var idButton = '';
var blankButton = '';
var labelEqualsButton = '';
var labelContainsButton = '';
var labelTrueLikeButton = '';
var labelFalseLikeButton = '';
for (var i=0; i<tags.length; i++)
{
var buttonValue = EZ.getTagValue(tags[i]);
if (value !== '' && buttonValue.toLowerCase() === value.toLowerCase())
{
tags[i].checked = true;
return values.push(EZgetValue(name)); //return if matching value found
}
if (value === '')
{
if (blankButton === '')
blankButton == tags[i];
}
if (EZisTrueLike(buttonValue))
{ //button value is trueLike...
if (!isValueTrueLike)
tags[i].checked = false; //...uncheck if value not trueLike
if (!trueLikeButton)
trueLikeButton = tags[i]; //...remember if 1st trueLike button
}
if (EZisFalseLike(buttonValue))
{ //button value is falseLike...
if (!isValueFalseLike)
tags[i].checked = false; //...uncheck if value not falseLike
if (!falseLikeButton)
falseLikeButton = tags[i]; //...remember if 1st falseLike button
}
var id = tags[i].id;
if (value !== '' && id && id !== name && id === value)
idButton = tags[i]; //non-blank value matches id diff from name
while (true) //id has priority over label text so...
{ //...only test label parent (not for="id")
//var label = EZgetParent(tags[i], 'label'); //see EZ.getAncestor()
var label = EZ.getAncestor(tags[i], 'label'); //see EZ.getAncestor()
if (!EZ.is(label)) break;
var labelText = EZ.trim(label.innerText).toLowerCase();
if (labelText === '') break;
if (!labelEqualsButton && labelText === value.toLowerCase())
labelEqualsButton = tags[i];
if (!labelContainsButton && labelText.indexOf(value.toLowerCase()) != -1)
labelContainsButton = tags[i];
if (!labelTrueLikeButton && EZisTrueLike(labelText))
labelTrueLikeButton = tags[i];
if (!labelFalseLikeButton && EZisFalseLike(labelText))
labelFalseLikeButton = tags[i];
break;
}
}
//----- no button matched non-blank value . . .
if (trueLikeButton && isValueTrueLike) //if value trueLike...
trueLikeButton.checked = true; //...check 1st trueLikeButton if found
else if (blankButton && value === '') //if value blank...
falseLikeButton.checked = true; //...check 1st falseLikeButton if found
else if (falseLikeButton && isValueFalseLike) //if value falseLike...
falseLikeButton.checked = true; //...check 1st falseLikeButton if found
else if (idButton) //value matches a button id (not id=name)
idButton.checked = true;
//check value to labelText...
else if (labelEqualsButton)
labelEqualsButton.checked = true; //...value matches full labelText
else if (labelTrueLikeButton && isValueTrueLike)
labelTrueLikeButton.checked = true; //...trueLike value matches trueLike label
else if (labelFalseLikeButton && isValueFalseLike)
labelFalseLikeButton.checked = true; //...falseLike value matches falseLike label
else if (labelContainsButton)
labelContainsButton.checked = true; //...value matches some labelText
return values.push(EZgetValue(name));
}
}
/* jshint ignore:start*/ //FALL-thru
case 'checkbox':
/* jshint ignore:end */
{
value = value.toString();
var elValue = EZ.getTagValue(el);
EZ.wasValue = !el.checked ? ''
: elValue === '' ? true
: elValue;
// ...check if value matches checkbox value
if (elValue.toLowerCase() === value.toLowerCase())
el.checked = true;
// ...check if value trueLike & checkbox trueLike or blank
else if ((elValue === '' || !elValue.isFalseLike()) && value.isTrueLike())
el.checked = true;
// ...otherwise uncheck
else
el.checked = false;
return values.push(EZgetValue(el));
}
/* jshint ignore:start*/ //FALL-thru
case 'select':
/* jshint ignore:end */
{
EZ.wasValue = [];
var textIndex = [];
for (var i=0; i<el.options.length; i++)
{
if (el.options[i].checked)
EZ.wasValue.push(el.options[i].value)
if (el.options[i].value == value)
{
el.options[i].selected = true;
if (el.type == 'select-one') break;
}
else if (el.options[i].text == value)
textIndex.push(i);
}
EZ.wasValue = EZ.wasValue.join('|');
if (i == el.options.length)
textIndex.some(function(i)
{
el.options[i].selected = true;
if (el.type == 'select-one') return values.push(true);
});
if (el.selectedIndex != -1 && window.EZselectScroll)
EZselectScroll(el,i) //scroll into view
return values.push(EZgetValue(el));
}
/* jshint ignore:start*/ //FALL-thru
default:
/* jshint ignore:end */
{
while (tagName && tagName != 'input')
{ //TODO: 1st empty innerHTML
if (el.innerHTML != EZ.undefined)
{ //return innerHTML if defined
EZ.wasValue = el.innerHTML;
return values.push(el.innerHTML = value);
}
}
}
}
});
return (EZ.isArray(el)) ? values : values[0]
if (EZ.quit != EZ.undefined)
debugger;
return '';
}
/*---------------------------------------------------------------------------------------------
chrome value is 'on' if not specified -- verify by checking outerHTML for value
return empty string if not explicitly specified
---------------------------------------------------------------------------------------------*/
EZ.getTagValue = function EZgetTagValue(el)
{
var value = el.value;
if (value == 'on' && !/value="(.*?)"/.test(el.outerHTML) )
value = '';
return value;
}
/*______________________________________________________________________________________________
functions ported from EASY js -- new functions and updates to be ported back to EASY js
______________________________________________________________________________________________*/
/*---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------*/
function EZisTrueLike(value, options)
{
if (EZ.isTrueLike) return EZ.isTrueLike(value, options);
if (value === true || value === 'true'
|| value === 'on' || value === 'yes') return true;
if (typeof(value) == 'object' && !EZ.isNone(value)) return true;
return false;
}
/*---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------*/
function EZisFalseLike(value)
{
if (value === false || value === 'false'
|| value === null || value == EZ.undefined
|| value === 'off' || value === 'no' || value === '') return true;
if (EZ.isNone(value)) return true;
return false;
}
/*---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------*/