Version 3.18.1
Show:

File: collection/js/arraylist-add.js

  1. /**
  2. * Collection utilities beyond what is provided in the YUI core
  3. * @module collection
  4. * @submodule arraylist-add
  5. * @deprecated Use ModelList or a custom ArrayList subclass
  6. */
  7. /*
  8. * Adds methods add and remove to Y.ArrayList
  9. */
  10. Y.mix(Y.ArrayList.prototype, {
  11. /**
  12. * Add a single item to the ArrayList. Does not prevent duplicates.
  13. *
  14. * @method add
  15. * @param { mixed } item Item presumably of the same type as others in the
  16. * ArrayList.
  17. * @param {Number} index (Optional.) Number representing the position at
  18. * which the item should be inserted.
  19. * @return {ArrayList} the instance.
  20. * @for ArrayList
  21. * @deprecated Use ModelList or a custom ArrayList subclass
  22. * @chainable
  23. */
  24. add: function(item, index) {
  25. var items = this._items;
  26. if (Y.Lang.isNumber(index)) {
  27. items.splice(index, 0, item);
  28. }
  29. else {
  30. items.push(item);
  31. }
  32. return this;
  33. },
  34. /**
  35. * Removes first or all occurrences of an item to the ArrayList. If a
  36. * comparator is not provided, uses itemsAreEqual method to determine
  37. * matches.
  38. *
  39. * @method remove
  40. * @param { mixed } needle Item to find and remove from the list.
  41. * @param { Boolean } all If true, remove all occurrences.
  42. * @param { Function } comparator optional a/b function to test equivalence.
  43. * @return {ArrayList} the instance.
  44. * @for ArrayList
  45. * @deprecated Use ModelList or a custom ArrayList subclass
  46. * @chainable
  47. */
  48. remove: function(needle, all, comparator) {
  49. comparator = comparator || this.itemsAreEqual;
  50. for (var i = this._items.length - 1; i >= 0; --i) {
  51. if (comparator.call(this, needle, this.item(i))) {
  52. this._items.splice(i, 1);
  53. if (!all) {
  54. break;
  55. }
  56. }
  57. }
  58. return this;
  59. },
  60. /**
  61. * Default comparator for items stored in this list. Used by remove().
  62. *
  63. * @method itemsAreEqual
  64. * @param { mixed } a item to test equivalence with.
  65. * @param { mixed } b other item to test equivalance.
  66. * @return { Boolean } true if items are deemed equivalent.
  67. * @for ArrayList
  68. * @deprecated Use ModelList or a custom ArrayList subclass
  69. */
  70. itemsAreEqual: function(a, b) {
  71. return a === b;
  72. }
  73. });