-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathg.js
1618 lines (1398 loc) · 42.6 KB
/
g.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
/**
* @license
* (c) 2012 Rhys Brett-Bowen, Catch.com
* G-object may be freely distributed under the MIT license.
* For all details and documentation:
* /~https://github.com/rhysbrettbowen/G-closure
*/
goog.provide('$');
goog.provide('$$');
goog.provide('G');
goog.provide('GG');
goog.require('goog.Timer');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.json');
goog.require('goog.style');
/**
* @param {*} input to create the G with.
* @param {string|Element|Node=} opt_mod elemnt to look under.
* @constructor
* @return {G} the G object.
*/
G = function(input, opt_mod) {
if (!goog.isDefAndNotNull(input))
input = [];
// already a G so return
if (input.constructor == G)
return /** @type {G} */(new G.init(input.toArray()));
// if the mod is a string it's a selector
if (goog.isString(opt_mod)) {
opt_mod = GG.elsBySelector(/** @type {string} */(opt_mod))[0];
}
if (input.constructor == document.createDocumentFragment().constructor &&
input.childNodes.length)
input = goog.array.clone(input.childNodes);
// if it's an element then wrap it
if (input.nodeType)
input = [input];
// a string is a selector
else if (goog.isString(input)) {
if (goog.string.trimLeft(input).charAt(0) == '<') {
input = goog.dom.htmlToDocumentFragment(input);
if (input.constructor == document.createDocumentFragment().constructor &&
input.childNodes.length)
input = goog.array.clone(input.childNodes);
else
input = [input];
} else {
input = GG.elsBySelector(input, opt_mod);
}
if (!input) {
input = [];
}
}
// no input is an empty G object
else if (!input) {
input = [];
}
// if it's not an array then make it one
if (typeof input === 'object' && 'setInterval' in input) {
input = [input];
}
else if (!goog.isArrayLike(input)) {
input = [input];
}
// G is actually G.init instantiated with an array
return /** @type {G} */(new G.init(
/** @type {{length: number}} */(input)));
};
// remove this line if you don't want to use $
$ = G;
GG = {};
// remove this line if you don't want to use $$
$$ = GG;
/** @type {number} */
G.prototype.length = 0;
/**
* @constructor
* @extends {G}
* @param {goog.array.ArrayLike} input array to turn in to G.
* @return {G} the new G object.
*/
G.init = function(input) {
// copy all elements in to the object and return itself as G
for (var i = 0; i < input.length; i++)
this[i] = input[i];
this.length = input.length;
return this;
};
GG.fn = G.init.prototype = G.prototype;
/**
* The constructor should be G
*/
G.prototype.constructor = G;
GG.selectorEngine_ = null;
GG.matchesEngine_ = null;
/**
* change the default selector engine.
*
* @param {Function} engine should take two arguments, the string selector and
* optionally an element to look under and return an array of elements
*/
GG.setSelectorEngine = function(engine) {
GG.selectorEngine_ = engine;
};
/**
* change the default matches engine.
*
* @param {Function} engine should take two arguments, the element and the
* selector string and return a boolean for a match.
*/
GG.setMatchesEngine = function(engine) {
GG.matchesEngine_ = engine;
};
/**
* css filters and their corresponding filter functions
*/
GG.cssFilters = {
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'visible': function(el) {return /** @type {boolean} */(G(el).visible());},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'hidden': function(el) {return !/** @type {boolean} */(G(el).visible());},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'selected': function(el) {return el.selected;},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'checked': function(el) {return el.checked;},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'first': function(el) {return el == el.parentNode.firstChild;},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'last': function(el) {return el == el.parentNode.lastChild;},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'even': function(el) {
return (G(el.parentNode).children().index(el) % 2) === 0;
},
/**
* @param {Element|Node} el to check.
* @return {boolean} whether element matches the filter.
*/
'odd': function(el) {
return (G(el.parentNode).children().index(el) % 2) === 1;
}
};
/**
* takes a string like 'tagName[ .className]', '.className' or '#elementId'
* mod is the element to search from
*
* @param {string} input selector string.
* @param {Element|Node=} opt_mod element or node to look under.
* @return {goog.array.ArrayLike} nodelist of found elements.
*/
GG.elsBySelector = function(input, opt_mod) {
if (input.charAt(0) == '-')
input = '.' + input.substring(1);
if(GG.selectorEngine_)
return GG.selectorEngine_(input, opt_mod);
var ret;
opt_mod = opt_mod || document;
// use native querySelectorAll if available
if (opt_mod.querySelectorAll) {
ret = opt_mod.querySelectorAll(input.indexOf(':') >= 0 ?
input.replace(/\:.*/, '') :
input);
// if not then parse string and use closure library functions
} else if (input.charAt(0) == '.') {
ret = (goog.dom.getElementsByClass(input.substring(1)
.replace(/[\:\s].*/, ''),
/** @type {Element} */(opt_mod)) || []);
} else if (input.charAt(0) == '#') {
ret = [goog.dom.getElement(input)];
} else {
ret = goog.dom.getElementsByTagNameAndClass(input.replace(/\s.*/, ''),
input.replace(/.*\./, '').replace(/[\:\s].*/, '') || null,
/** @type {Element} */(opt_mod));
}
// filter the results with css filters
if (input.indexOf(':') >= 0) {
return GG.grep(ret, GG.cssFilters[input.substring(input.indexOf(':') + 1)]);
}
return ret;
};
/**
* takes an element and a function or selector string and returns true if the
* element matches the selector. Tries to use native functions where it can.
* Will only work with no native function if the element has a parent
*
* @param {Element|Node} element to test.
* @param {Function|string=} opt_selector function that takes element and
* returns boolean or a selector string.
* @return {boolean} if the element matches the selector.
*/
GG.matches = function(element, opt_selector) {
if (opt_selector && opt_selector.charAt(0) == '-')
opt_selector = '.' + opt_selector.substring(1);
if (GG.matchesEngine_)
return GG.matchesEngine_(element, opt_selector);
// handle where opt selector is function or not defined
if (!goog.isDef(opt_selector))
return true;
if (goog.isFunction(opt_selector)) {
return opt_selector(element);
}
// use native MatechesSelector where available
var matchesSelector = element['webkitMatchesSelector'] ||
element['mozMatchesSelector'] ||
element['oMatchesSelector'] ||
element['matchesSelector'];
if (matchesSelector) {
return matchesSelector.call(element, opt_selector);
}
// else see if element is in it's parent if calling the selector on it
var parent = element.parentNode;
var els = GG.elsBySelector(/** @type {string} */(opt_selector), parent);
return goog.array.contains(els, element);
};
// Utility functions
/**
* Extends an object with another object.
* This operates 'in-place'; it does not create a new Object.
*
* @param {Object} obj The object to modify.
* @param {...Object} var_args The objects from which values will be copied.
* @return {Object} that was extended.
*/
GG.extend = function(obj, var_args) {
goog.object.extend(obj, var_args);
return obj;
};
/**
* Whether a node contains another node.
* @param {Node} parent The node that should contain the other node.
* @param {Node} descendant The node to test presence of.
* @return {boolean} Whether the parent node contains the descendent node.
*/
GG.contains = goog.dom.contains;
/**
* returns the data set on the element
*
* @param {Element} element to check for data.
* @param {string=} opt_key for the data.
* @param {string=} opt_value the value to set for the key.
* @return {G|string} the element wrapped in a G.
*/
GG.data = function(element, opt_key, opt_value) {
return G(element).data(opt_key, opt_value);
};
/**
* runs a function on each element in the collection
*
* @param {goog.array.ArrayLike|G|string} collection to run on.
* @param {Function} callback function to run.
* @return {G} collection as a G object.
*/
GG.each = function(collection, callback) {
return G(collection).each(callback);
};
/**
* filters an array based on a function
*
* @param {goog.array.ArrayLike} array to filter.
* @param {Function} fn can take 2 arguments. The element and the index.
* @param {boolean=} opt_invert whether to have the opposite returned.
* @return {Array} the filtered array.
*/
GG.grep = function(array, fn, opt_invert) {
var func = fn;
if (opt_invert)
func = function(el, ind) {
return !fn(el, ind);
};
return goog.array.filter(array, func);
};
/**
* Calls the given function once, after the optional pause.
*
* The function is always called asynchronously, even if the delay is 0. This
* is a common trick to schedule a function to run after a batch of browser
* event processing.
*
* @param {Function} listener Function or object that has a handleEvent method.
* @param {number=} opt_delay Milliseconds to wait; default is 0.
* @param {Object=} opt_handler Object in whose scope to call the listener.
* @return {number} A handle to the timer ID.
*/
GG.wait = goog.Timer.callOnce;
/**
* Clears a timeout initiated by callOnce
*
* @param {?number} timerId a timer ID.
*/
GG.clearWait = goog.Timer.clear;
/**
* returns the index of the value in the array
*
* @param {Object} value to find.
* @param {goog.array.ArrayLike} array to look in.
* @param {number=} opt_index to look from.
* @return {number} the index of the value.
*/
GG.inArray = function(value, array, opt_index) {
return goog.array.indexOf(array, value, opt_index);
};
/**
* flattens arrays together - useful to get array from Gs
*/
GG.flatten = function (var_args) {
var result = [];
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (goog.isArrayLike(element)) {
result.push.apply(result, GG.flatten.apply(null, element));
} else {
result.push(element);
}
}
return result;
};
/**
* maps each element in the array or object to the output of a function
*
* @param {Array|Object} array to map.
* @param {Function} callback the function that maps a given element to the
* returned value.
* @return {Array|Object} the mapped array/object.
*/
GG.map = function(array, callback) {
if (goog.isArrayLike(array)) {
return goog.array.map(/** @type {goog.array.ArrayLike} */(array),
callback);
} else {
return goog.object.map(array, callback);
}
};
/**
* works a bit different from jquery. Arrays are given back without square
* brackets and objects are put in with square brackets. It's an unsafe
* recursion so don't use back references - only meant for a data object
*
* myObject = {
* a = {
* one: 1,
* two: 2,
* three: 3
* },
* b: [1,2,3]
* };
*
* G.param(myObject) // b=1&b=2&b=3&a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3
*
* @param {Object} obj to change in to a parameter string.
* @return {string} the query string.
*/
GG.param = function(obj) {
var myObj = goog.object.clone(obj);
var check = function(val, key) {
if (!goog.isArray(val) && goog.isObject(val)) {
goog.object.forEach(val, function(val2, key2) {
myObj[key + '[' + key2 + ']'] = val2;
});
delete myObj[key];
return true;
}
return false;
};
var flag = false;
while (flag) {
flag = goog.object.some(myObj, check);
}
return goog.Uri.QueryData.createFromMap(myObj).toString();
};
/**
* Returns a new array that is the result of joining the arguments. If arrays
* are passed then their items are added, however, if non-arrays are passed they
* will be added to the return array as is.
*
* Note that ArrayLike objects will be added as is, rather than having their
* items added.
*
* goog.array.concat([1, 2], [3, 4]) -> [1, 2, 3, 4]
* goog.array.concat(0, [1, 2]) -> [0, 1, 2]
* goog.array.concat([1, 2], null) -> [1, 2, null]
*
* There is bug in all current versions of IE (6, 7 and 8) where arrays created
* in an iframe become corrupted soon (not immediately) after the iframe is
* destroyed. This is common if loading data via goog.net.IframeIo, for example.
* This corruption only affects the concat method which will start throwing
* Catastrophic Errors (#-2147418113).
*
* See http://endoflow.com/scratch/corrupted-arrays.html for a test case.
*
* Internally goog.array should use this, so that all methods will continue to
* work on these broken array objects.
*
* @param {...*} var_args Items to concatenate. Arrays will have each item
* added, while primitives and objects will be added as is.
* @return {!Array} The new resultant array.
*/
GG.merge = goog.array.concat;
/**
* Parses a JSON string and returns the result. This throws an exception if
* the string is an invalid JSON string.
*
* Note that this is very slow on large strings. If you trust the source of
* the string then you should use unsafeParse instead.
*
* @param {*} s The JSON string to parse.
* @return {Object} The object generated from the JSON string.
*/
GG.parseJSON = goog.json.parse;
/**
* Partially applies this function to a particular 'this object' and zero or
* more arguments. The result is a new function with some arguments of the first
* function pre-filled and the value of |this| 'pre-specified'.<br><br>
*
* Remaining arguments specified at call-time are appended to the pre-
* specified ones.<br><br>
*
* Also see: {@link #partial}.<br><br>
*
* Usage:
* <pre>var barMethBound = bind(myFunction, myObj, 'arg1', 'arg2');
* barMethBound('arg3', 'arg4');</pre>
*
* @param {Function} fn A function to partially apply.
* @param {Object|undefined} selfObj Specifies the object which |this| should
* point to when the function is run.
* @param {...*} var_args Additional arguments that are partially
* applied to the function.
* @return {!Function} A partially-applied form of the function bind() was
* invoked as a method of.
* @suppress {deprecated} See above.
*/
GG.proxy = goog.bind;
/**
* Trims white spaces to the left and right of a string.
* @param {string} str The string to trim.
* @return {string} A trimmed copy of {@code str}.
*/
GG.trim = goog.string.trim;
/**
* Removes all duplicates from an array (retaining only the first
* occurrence of each array element). This function modifies the
* array in place and doesn't change the order of the non-duplicate items.
*
* For objects, duplicates are identified as having the same unique ID as
* defined by {@link goog.getUid}.
*
* Runtime: N,
* Worstcase space: 2N (no dupes)
*
* @param {goog.array.ArrayLike} arr The array from which to remove duplicates.
* @param {Array=} opt_rv An optional array in which to return the results,
* instead of performing the removal inplace. If specified, the original
* array will remain unchanged.
*/
GG.unique = goog.array.removeDuplicates;
/**
* Null function used for default values of callbacks, etc.
* @return {void} Nothing.
*/
GG.noop = goog.nullFunction;
// Array Functions
/**
* @param {Function} fn function to apply.
* @param {Object=} opt_handler to bind 'this' to.
* @return {G} the G object.
*/
G.prototype.each = function(fn, opt_handler) {
goog.array.forEach(/** @type {goog.array.ArrayLike} */(this),
function(el, index) {
goog.bind(fn, opt_handler || el)(el, index);
});
return this;
};
/**
* @param {Function=} opt_fn comparator should take in two elements and
* return -1, 0 or 1.
* @param {Object=} opt_handler to bind 'this' to
* add on sort from array prototype.
* @return {G} the sorted G object.
*/
G.prototype.sort = function(opt_fn, opt_handler) {
goog.array.ARRAY_PROTOTYPE_.sort.call(this,
goog.bind(opt_fn || goog.array.defaultCompare, opt_handler || this));
return this;
};
/**
* @return {G} the sorted G object.
*/
G.prototype.reverse = function() {
goog.array.ARRAY_PROTOTYPE_.reverse.call(this);
return this;
};
/**
* @param {Function|string} fn function return true to keep element.
* @param {Object=} opt_handler to bind 'this' to.
* @param {boolean=} opt_not whther to inverse the results.
* @return {G} the G object.
*/
G.prototype.filter = function(fn, opt_handler, opt_not) {
// handles some css filter strings
if (goog.isString(fn)) {
var select = fn;
var length = this.size();
fn = {
':odd': function(val, ind) {
return ind % 2 === 1;
},
':even': function(val, ind) {
return ind % 2 === 0;
},
':first': function(val, ind) {
return ind === 0;
},
':last': function(val, ind) {
return ind === this.length - 1;
}
}[select];
opt_handler = this;
}
// filters based on handler and whether opt_not is used
return G(goog.array.filter(/** @type {goog.array.ArrayLike} */(this),
function(el, ind) {
return Boolean(opt_not) != Boolean(goog.bind(
/** @type {Function} */(fn), opt_handler || this)(el, ind));
}));
};
/**
* @param {Function|string} fn to - return true to keep element.
* @param {Object=} opt_handler to bind 'this' to.
* @return {G} the G object.
*/
G.prototype.not = function(fn, opt_handler) {
return this.filter(fn, opt_handler, true);
};
/**
* @param {Function} fn function to apply.
* @param {Object=} opt_handler to bind 'this' to.
* @return {G} the G object.
*/
G.prototype.map = function(fn, opt_handler) {
return G(goog.array.map(/** @type {goog.array.ArrayLike} */(this),
fn, opt_handler));
};
/**
* @param {*} obj to test the array for.
* @return {boolean} true if it is in G.
*/
G.prototype.contains = function(obj) {
return goog.array.contains(/** @type {goog.array.ArrayLike} */(this), obj);
};
/**
* @param {number|Function} ind index ro get item from, can be negative to
* look from the back or a function that should take an element and return
* true if it is the correct element to return.
* @param {Object=} opt_handler to bind 'this' to.
* @return {*} the element.
*/
G.prototype.get = function(ind, opt_handler) {
if (goog.isFunction(ind))
return goog.array.find(/** @type {goog.array.ArrayLike} */(this),
ind, opt_handler);
if (ind < 0)
ind = this.length + ind;
return this[ind || 0];
};
/**
* @return {*} the first element.
*/
G.prototype.first = function() {
return this.get(0);
};
/**
* @return {*} the last element.
*/
G.prototype.last = function() {
return this.get(-1);
};
/**
* @return {Array} a plain array.
*/
G.prototype.toArray = function() {
var arr = [];
this.each(function(val) {arr.push(val);});
return arr;
};
/**
* @param {number} index slice the array to one element at index.
* @return {G} the G object.
*/
G.prototype.eq = function(index) {
return G(this.get(index));
};
/**
* @return {number} the length of G.
*/
G.prototype.size = function() {
return this.length;
};
/**
* @param {*} arr element to add to the array.
* @return {G} the G object.
*/
G.prototype.add = function(arr) {
var array = G(arr);
var length = array.length;
var alength = this.length;
for (var i = 0; i < length; i++) {
this[i + alength] = array[i];
}
this.length = length + alength;
return this;
};
/**
* @param {*} arr element to remove from the array.
* @return {G} the G object.
*/
G.prototype.remove = function(arr) {
if (goog.isArrayLike(arr))
return G(arr).each(function(el) {this.remove(el);}, this);
var array = this.toArray();
goog.array.remove(array, arr);
return G(array);
};
// DOM functions
/**
* @param {Object|string} style name of style to get or set.
* @param {string=} opt_val value of css style.
* @return {G|string} computed style if no val given.
*/
G.prototype.css = function(style, opt_val) {
if (goog.isString(style) && !goog.isDef(opt_val)) {
if (goog.userAgent.IE)
return this[0].currentStyle &&
this[0].currentStyle[goog.string.toCamelCase(style)];
return goog.style.getComputedStyle(this[0], style);
}
this.each(function(el) {
goog.style.setStyle(el, style, opt_val);
});
return this;
};
/**
* @param {string=} opt_input number of pixels or css string.
* @return {G|number} number of pixels if no input.
*/
G.prototype.top = function(opt_input) {
if (goog.isDef(opt_input)) {
if (goog.isNumber(opt_input))
opt_input += 'px';
this.each(function(el) {el.style.top = opt_input;});
return this;
}
return goog.style.getBounds(/** @type {Element} */(this.get(0))).top;
};
/**
* the coords of the first element to the page.
*
* @return {goog.math.Rect} the top, left, width and height
*/
G.prototype.position = function() {
return goog.style.getBounds(this[0]);
};
/**
* the offset from the offset parent or a passed in element
*
* @param {G|Element=} opt_input the element to get the offset from.
* @return {{top: number, left: number}} coordinates top & left
*/
G.prototype.offset = function(opt_input) {
var ans;
if (!opt_input)
ans = goog.style.getPosition(this[0]);
else
ans = goog.style.getRelativePosition(this[0], G(opt_input)[0]);
return {
top: ans.y,
left: ans.x
};
};
/**
* @param {number|string=} opt_input number of pixels or css string.
* @return {G|number} number of pixels if no input.
*/
G.prototype.left = function(opt_input) {
if (goog.isDef(opt_input)) {
if (goog.isNumber(opt_input))
opt_input += 'px';
this.each(function(el) {el.style.left = opt_input;});
return this;
}
return goog.style.getBounds(/** @type {Element} */(this.get(0))).left;
};
/**
* @param {number|string=} opt_input number of pixels or css string.
* @return {G|number} width in pixels if no input.
*/
G.prototype.width = function(opt_input) {
if (goog.isDef(opt_input)) {
if (goog.isNumber(opt_input))
opt_input += 'px';
this.each(function(el) {el.style.width = opt_input;});
return this;
}
return goog.style.getBounds(/** @type {Element} */(this.get(0))).width;
};
/**
* @param {Element|Node|string|Function=} opt_selector to test against.
* @return {number} the index in the array.
*/
G.prototype.index = function(opt_selector) {
return goog.array.findIndex(this.toArray(), function(el) {
if (opt_selector.nodeType)
return el == opt_selector;
return GG.matches(el, /** @type {Function|string} */(opt_selector));
});
};
/**
* @param {number|string=} opt_input number of pixels or css string.
* @return {G|number} height in pixels if no input.
*/
G.prototype.height = function(opt_input) {
if (goog.isDef(opt_input)) {
if (goog.isNumber(opt_input))
opt_input += 'px';
this.each(function(el) {
el.style.height = opt_input;
});
return this;
}
return goog.style.getBounds(/** @type {Element} */(this.get(0))).height;
};
/**
* @param {string} selector string to find elements under each element in G.
* @return {G} the G object with new found elements.
*/
G.prototype.find = function(selector) {
var ret = [];
this.each(function(el) {
goog.array.forEach(GG.elsBySelector(selector, el) || [],
function(ele) {
goog.array.insert(ret, ele);
}
);
});
return G(ret);
};
/**
* @param {Function|boolean=} opt_bool to change elements visible status.
* @return {G|boolean} if no boolean then whether the first item is visible.
*/
G.prototype.visible = function(opt_bool) {
if (!goog.isDef(opt_bool))
return goog.style.isElementShown(this[0]);
if(goog.isBoolean(opt_bool)) {
var bool = opt_bool;
opt_bool = function() {return bool;};
}
var show = /** @type {Function} */(opt_bool);
return this.each(function(el) {goog.style.showElement(el, show(el));});
};
/**
* @return {G} the G object.
*/
G.prototype.show = function() {
return /** @type {G} */(this.visible(true));
};
/**
* @return {G} the G object.
*/
G.prototype.hide = function() {
return /** @type {G} */(this.visible(false));
};
/**
* @param {string|Object.<string, string>} key or object of keys and values.
* @param {string=} opt_val string to set value if key is string.
* @return {G|string} the G object or string if no val given.
*/
G.prototype.attr = function(key, opt_val) {
if (goog.isString(key) && !goog.isDef(opt_val)) {
return this[0].getAttribute(key);
}
if (goog.isString(key)) {
var temp = {};
goog.object.set(temp, key, opt_val);
key = temp;
}
if (goog.isObject(key)) {
this.each(function(el) {
goog.dom.setProperties(el, /** @type {Object} */(key));
});
}
return this;
};
/**
* @param {string=} opt_key to associate data with.
* @param {string=} opt_val value of the key.
* @return {G|string} the value if not opt_val given.
*/
G.prototype.data = function(opt_key, opt_val) {
return this.attr('data-' + (opt_key || 'id'), opt_val);
};
/**
* @return {G} the G object with all nodes removed from DOM.
*/
G.prototype.removeNode = function() {
return this.each(function(el) {
goog.dom.removeNode(el);
});
};
/**
* @param {Element|Node|G} node to replace first node with.
* @return {G} the G object with first node replaces.
*/
G.prototype.replace = function(node) {
if(goog.isArrayLike(node))
node = node[0];
goog.dom.replaceNode(node, this[0]);
return G(node);
};
/**
* @param {string=} opt_val the value to set.
* @return {G|string} string that is the value if no val defined otherwise G.
*/
G.prototype.val = function(opt_val) {
if (goog.isDef(opt_val))
return this.each(function(el) {el.value = opt_val;});
return this[0].value;
};
/**
* @return {G} the G object with all children under elements removed.
*/
G.prototype.empty = function() {
return this.each(goog.dom.removeChildren);