@angular-wave/angular.ts 0.0.45 → 0.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ import {
10
10
  isString,
11
11
  isUndefined,
12
12
  lowercase,
13
- nodeName_,
13
+ getNodeName,
14
14
  shallowCopy,
15
15
  } from "../../shared/utils";
16
16
  import { CACHE, EXPANDO } from "../../core/cache/cache";
@@ -261,7 +261,7 @@ JQLite.prototype.injector = function () {
261
261
  */
262
262
  JQLite.prototype.on = function (type, fn) {
263
263
  // Do not add event handlers to non-elements because they will not be cleaned up.
264
- for (let i = 0, ii = this.length; i < ii; i++) {
264
+ for (let i = 0; i < this.length; i++) {
265
265
  const element = this[i];
266
266
  if (!elementAcceptsData(element)) {
267
267
  return;
@@ -311,7 +311,7 @@ JQLite.prototype.on = function (type, fn) {
311
311
  * @returns {JQLite}
312
312
  */
313
313
  JQLite.prototype.off = function (type, fn) {
314
- for (let i = 0, ii = this.length; i < ii; i++) {
314
+ for (let i = 0; i < this.length; i++) {
315
315
  const element = this[i];
316
316
  const expandoStore = getExpando(element);
317
317
  const events = expandoStore && expandoStore.events;
@@ -357,7 +357,7 @@ JQLite.prototype.off = function (type, fn) {
357
357
  * @returns {JQLite}
358
358
  */
359
359
  JQLite.prototype.removeData = function (name) {
360
- for (let i = 0, ii = this.length; i < ii; i++) {
360
+ for (let i = 0; i < this.length; i++) {
361
361
  const element = this[i];
362
362
  removeElementData(element, name);
363
363
  }
@@ -371,7 +371,7 @@ JQLite.prototype.removeData = function (name) {
371
371
  * @returns {JQLite|any}
372
372
  */
373
373
  JQLite.prototype.inheritedData = function (name, value) {
374
- for (let i = 0, ii = this.length; i < ii; i++) {
374
+ for (let i = 0; i < this.length; i++) {
375
375
  const element = this[0];
376
376
  let res = getInheritedData(element, name, value);
377
377
  if (value) {
@@ -398,124 +398,145 @@ JQLite.prototype.html = function (value) {
398
398
  return this;
399
399
  };
400
400
 
401
- /// ///////////////////////////////////////
402
- // Functions iterating getter/setters.
403
- // these functions return self on setter and
404
- // value on get.
405
- /// ///////////////////////////////////////
406
- forEach(
407
- {
408
- data: getOrSetCacheData,
409
- attr(element, name, value) {
410
- let ret;
401
+ /**
402
+ * Get the combined text contents of each element in the JQLite collection
403
+ * or set the text contents of all elements.
404
+ * @param {string} [value]
405
+ * @returns {JQLite|string}
406
+ */
407
+ JQLite.prototype.text = function (value) {
408
+ let res = "";
409
+ for (let i = 0; i < this.length; i++) {
410
+ const element = this[i];
411
+ if (isUndefined(value)) {
412
+ // read
411
413
  const { nodeType } = element;
412
- if (
413
- nodeType === Node.TEXT_NODE ||
414
- nodeType === Node.ATTRIBUTE_NODE ||
415
- nodeType === Node.COMMENT_NODE ||
416
- !element.getAttribute
417
- ) {
418
- return;
419
- }
414
+ res +=
415
+ nodeType === Node.ELEMENT_NODE || nodeType === Node.TEXT_NODE
416
+ ? element.textContent
417
+ : "";
418
+ } else {
419
+ // write
420
+ element.textContent = value;
421
+ }
422
+ }
420
423
 
421
- const lowercasedName = lowercase(name);
422
- const isBooleanAttr = BOOLEAN_ATTR[lowercasedName];
424
+ if (isUndefined(value)) {
425
+ return res;
426
+ } else {
427
+ return this;
428
+ }
429
+ };
423
430
 
424
- if (isDefined(value)) {
425
- // setter
431
+ /**
432
+ * Gets or sets the values of form elements such as input, select and textarea in a JQLite collection.
433
+ * @param {any} value
434
+ * @returns {JQLite|any}
435
+ */
436
+ JQLite.prototype.val = function (value) {
437
+ // We can only get or set a value of
438
+ for (let i = 0; i < this.length; i++) {
439
+ const element = this[i];
440
+ if (isUndefined(value)) {
441
+ // read
442
+ if (element.multiple && getNodeName(element) === "select") {
443
+ const result = [];
444
+ forEach(element.options, (option) => {
445
+ if (option.selected) {
446
+ result.push(option.value || option.text);
447
+ }
448
+ });
449
+ return result;
450
+ }
451
+ return element.value;
452
+ } else {
453
+ // write
454
+ element.value = value;
455
+ return this;
456
+ }
457
+ }
458
+ };
426
459
 
427
- if (value === null || (value === false && isBooleanAttr)) {
428
- element.removeAttribute(name);
429
- } else {
430
- element.setAttribute(name, isBooleanAttr ? lowercasedName : value);
431
- }
432
- } else {
433
- // getter
460
+ /**
461
+ * @param {string|Object} name
462
+ * @param {any} value
463
+ * @returns
464
+ */
465
+ JQLite.prototype.attr = function (name, value) {
466
+ for (let i = 0; i < this.length; i++) {
467
+ const element = this[i];
468
+ let ret;
469
+ const { nodeType } = element;
470
+ if (
471
+ nodeType === Node.TEXT_NODE ||
472
+ nodeType === Node.ATTRIBUTE_NODE ||
473
+ nodeType === Node.COMMENT_NODE ||
474
+ !element.getAttribute
475
+ ) {
476
+ continue;
477
+ }
434
478
 
435
- ret = element.getAttribute(name);
479
+ const lowercasedName = lowercase(name);
480
+ const isBooleanAttr = BOOLEAN_ATTR[lowercasedName];
436
481
 
437
- if (isBooleanAttr && ret !== null) {
438
- ret = lowercasedName;
439
- }
440
- // Normalize non-existing attributes to undefined (as jQuery).
441
- return ret === null ? undefined : ret;
482
+ if (isObject(name)) {
483
+ for (let key in name) {
484
+ element.setAttribute(key, isBooleanAttr ? lowercasedName : name[key]);
442
485
  }
443
- },
444
- text: (function () {
445
- getText.$dv = "";
446
- return getText;
486
+ } else if (isDefined(value)) {
487
+ // setter
447
488
 
448
- function getText(element, value) {
449
- if (isUndefined(value)) {
450
- const { nodeType } = element;
451
- return nodeType === Node.ELEMENT_NODE || nodeType === Node.TEXT_NODE
452
- ? element.textContent
453
- : "";
454
- }
455
- element.textContent = value;
456
- }
457
- })(),
458
- val(element, value) {
459
- if (isUndefined(value)) {
460
- if (element.multiple && nodeName_(element) === "select") {
461
- const result = [];
462
- forEach(element.options, (option) => {
463
- if (option.selected) {
464
- result.push(option.value || option.text);
465
- }
466
- });
467
- return result;
468
- }
469
- return element.value;
489
+ if (value === null || (value === false && isBooleanAttr)) {
490
+ element.removeAttribute(name);
491
+ } else {
492
+ element.setAttribute(name, isBooleanAttr ? lowercasedName : value);
470
493
  }
471
- element.value = value;
472
- },
473
- },
474
- (fn, name) => {
475
- /**
476
- * Properties: writes return selection, reads return first value
477
- */
478
- JQLite.prototype[name] = function (arg1, arg2) {
479
- let i;
480
- let key;
481
- const nodeCount = this.length;
482
-
483
- // JQLiteEmpty takes no arguments but is a setter.
484
- if (isUndefined(fn.length === 2 && fn !== getController ? arg1 : arg2)) {
485
- if (isObject(arg1)) {
486
- // we are a write, but the object properties are the key/values
487
- for (i = 0; i < nodeCount; i++) {
488
- if (fn === getOrSetCacheData) {
489
- fn(this[i], arg1);
490
- } else {
491
- for (key in arg1) {
492
- fn(this[i], key, arg1[key]);
493
- }
494
- }
495
- }
496
- // return self for chaining
497
- return this;
498
- }
499
- // we are a read, so read the first child.
500
- // TODO: do we still need this?
501
- let value = fn.$dv;
502
- // Only if we have $dv do we iterate over all, otherwise it is just the first element.
503
- const jj = isUndefined(value) ? Math.min(nodeCount, 1) : nodeCount;
504
- for (let j = 0; j < jj; j++) {
505
- const nodeValue = fn(this[j], arg1, arg2);
506
- value = value ? value + nodeValue : nodeValue;
507
- }
508
- return value;
494
+ } else {
495
+ // getter
496
+ ret = element.getAttribute(name);
497
+ if (isBooleanAttr && ret !== null) {
498
+ ret = lowercasedName;
509
499
  }
510
- // we are a write, so apply to all children
500
+ // Normalize non-existing attributes to undefined (as jQuery).
501
+ return ret === null ? undefined : ret;
502
+ }
503
+ }
504
+
505
+ if (isDefined(value) || isObject(name)) {
506
+ return this;
507
+ }
508
+ };
509
+
510
+ /**
511
+ * @param {string|any} key - The key (as a string) to get/set or an object for mass-setting.
512
+ * @param {any} [value] - The value to set. If not provided, the function acts as a getter.
513
+ * @returns {JQLite|any} - The retrieved data if acting as a getter. Otherwise, returns undefined.
514
+ */
515
+ JQLite.prototype.data = function (key, value) {
516
+ let i;
517
+ const nodeCount = this.length;
518
+ if (isUndefined(value)) {
519
+ if (isObject(key)) {
520
+ // we are a write, but the object properties are the key/values
511
521
  for (i = 0; i < nodeCount; i++) {
512
- fn(this[i], arg1, arg2);
522
+ getOrSetCacheData(this[i], key);
513
523
  }
514
- // return self for chaining
515
524
  return this;
516
- };
517
- },
518
- );
525
+ }
526
+ // we are a read, so read the first child.
527
+ const jj = isUndefined(value) ? Math.min(nodeCount, 1) : nodeCount;
528
+ for (let j = 0; j < jj; j++) {
529
+ const nodeValue = getOrSetCacheData(this[j], key, value);
530
+ value = value ? value + nodeValue : nodeValue;
531
+ }
532
+ return value;
533
+ }
534
+ // we are a write, so apply to all children
535
+ for (i = 0; i < nodeCount; i++) {
536
+ getOrSetCacheData(this[i], key, value);
537
+ }
538
+ return this;
539
+ };
519
540
 
520
541
  /// ///////////////////////////////////////
521
542
  // Functions iterating traversal.
@@ -955,7 +976,7 @@ function getController(element, name) {
955
976
  /**
956
977
  *
957
978
  * @param {Element} element
958
- * @param {string} name
979
+ * @param {string|string[]} name
959
980
  * @param {any} [value]
960
981
  * @returns
961
982
  */
@@ -1136,13 +1157,13 @@ export function getBooleanAttrName(element, name) {
1136
1157
  const booleanAttr = BOOLEAN_ATTR[name.toLowerCase()];
1137
1158
 
1138
1159
  // booleanAttr is here twice to minimize DOM access
1139
- return booleanAttr && BOOLEAN_ELEMENTS[nodeName_(element)] && booleanAttr;
1160
+ return booleanAttr && BOOLEAN_ELEMENTS[getNodeName(element)] && booleanAttr;
1140
1161
  }
1141
1162
 
1142
1163
  /**
1143
1164
  * Takes an array of elements, calls any `$destroy` event handlers, removes any data in cache, and finally removes any
1144
1165
  * listeners.
1145
- * @param {NodeListOf<Element>} nodes
1166
+ * @param {NodeListOf<Element>|Element[]} nodes
1146
1167
  */
1147
1168
  export function cleanElementData(nodes) {
1148
1169
  for (let i = 0, ii = nodes.length; i < ii; i++) {
@@ -757,7 +757,6 @@ describe("jqLite", () => {
757
757
  expect(selector.attr("prop", "value")).toEqual(selector);
758
758
  expect(JQLite(a).attr("prop")).toEqual("value");
759
759
  expect(JQLite(b).attr("prop")).toEqual("value");
760
-
761
760
  expect(selector.attr({ prop: "new value" })).toEqual(selector);
762
761
  expect(JQLite(a).attr("prop")).toEqual("new value");
763
762
  expect(JQLite(b).attr("prop")).toEqual("new value");
@@ -594,8 +594,19 @@ export function isElement(node) {
594
594
  ); // We have an on and find method part of jQuery API.
595
595
  }
596
596
 
597
- export function nodeName_(element) {
598
- return lowercase(element.nodeName || (element[0] && element[0].nodeName));
597
+ /**
598
+ * Returns a string appropriate for the type of node.
599
+ *
600
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/nodeName)
601
+ *
602
+ * @param {JQLite|Element} element
603
+ * @returns
604
+ */
605
+ export function getNodeName(element) {
606
+ return lowercase(
607
+ /** @type {Element} */ (element).nodeName ||
608
+ (element[0] && element[0].nodeName),
609
+ );
599
610
  }
600
611
 
601
612
  export function includes(array, obj) {
@@ -76,6 +76,31 @@ export class JQLite {
76
76
  * @returns {JQLite|any|undefined}
77
77
  */
78
78
  html(value: string): JQLite | any | undefined;
79
+ /**
80
+ * Get the combined text contents of each element in the JQLite collection
81
+ * or set the text contents of all elements.
82
+ * @param {string} [value]
83
+ * @returns {JQLite|string}
84
+ */
85
+ text(value?: string): JQLite | string;
86
+ /**
87
+ * Gets or sets the values of form elements such as input, select and textarea in a JQLite collection.
88
+ * @param {any} value
89
+ * @returns {JQLite|any}
90
+ */
91
+ val(value: any): JQLite | any;
92
+ /**
93
+ * @param {string|Object} name
94
+ * @param {any} value
95
+ * @returns
96
+ */
97
+ attr(name: string | any, value: any): any;
98
+ /**
99
+ * @param {string|any} key - The key (as a string) to get/set or an object for mass-setting.
100
+ * @param {any} [value] - The value to set. If not provided, the function acts as a getter.
101
+ * @returns {JQLite|any} - The retrieved data if acting as a getter. Otherwise, returns undefined.
102
+ */
103
+ data(key: string | any, value?: any): JQLite | any;
79
104
  toString(): string;
80
105
  eq(index: any): JQLite;
81
106
  length: number;
@@ -147,7 +172,7 @@ export function getBooleanAttrName(element: any, name: any): any;
147
172
  /**
148
173
  * Takes an array of elements, calls any `$destroy` event handlers, removes any data in cache, and finally removes any
149
174
  * listeners.
150
- * @param {NodeListOf<Element>} nodes
175
+ * @param {NodeListOf<Element>|Element[]} nodes
151
176
  */
152
- export function cleanElementData(nodes: NodeListOf<Element>): void;
177
+ export function cleanElementData(nodes: NodeListOf<Element> | Element[]): void;
153
178
  export const BOOLEAN_ATTR: {};
@@ -344,7 +344,15 @@ export function hasCustomToString(obj: any): boolean;
344
344
  * @returns {boolean} True if `value` is a DOM element (or wrapped jQuery element).
345
345
  */
346
346
  export function isElement(node: any): boolean;
347
- export function nodeName_(element: any): string;
347
+ /**
348
+ * Returns a string appropriate for the type of node.
349
+ *
350
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Node/nodeName)
351
+ *
352
+ * @param {JQLite|Element} element
353
+ * @returns
354
+ */
355
+ export function getNodeName(element: JQLite | Element): string;
348
356
  export function includes(array: any, obj: any): boolean;
349
357
  /**
350
358
  * Removes the first occurrence of a specified value from an array.