attributelist.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Attribute from './attribute.js';
  7. /**
  8. * List of attributes.
  9. *
  10. * @class treeModel.AttributeList
  11. */
  12. export default class AttributeList extends Map {
  13. /**
  14. * Creates AttributeList. If parameter is passed, initializes created list with passed {@link treeModel.Attribute}s.
  15. *
  16. * @constructor
  17. * @param {Iterable.<treeModel.Attribute>} attrs Attributes to initialize with.
  18. */
  19. constructor( attrs ) {
  20. super();
  21. if ( attrs ) {
  22. this.setTo( attrs );
  23. }
  24. /**
  25. * Amount of attributes added to the AttributeList.
  26. *
  27. * @property {Number} size
  28. */
  29. }
  30. /**
  31. * AttributeList iterator. Iterates over all attributes from the list.
  32. */
  33. [ Symbol.iterator ]() {
  34. let it = super[ Symbol.iterator ]();
  35. return {
  36. next: () => {
  37. let step = it.next();
  38. return {
  39. value: step.value ? step.value[ 1 ] : undefined,
  40. done: step.done
  41. };
  42. }
  43. };
  44. }
  45. /**
  46. * Adds attribute to the attributes list. If attribute with the same key already is set, it overwrites its values.
  47. *
  48. * @chainable
  49. * @param {treeModel.Attribute} attr Attribute to add or overwrite.
  50. * @returns {treeModel.AttributeList} This AttributeList object.
  51. */
  52. set( attr ) {
  53. super.set( attr.key, attr );
  54. return this;
  55. }
  56. /**
  57. * Removes all attributes from AttributeList and adds given attributes.
  58. *
  59. * @param {Iterable.<Attribute>} attrs Iterable object containing attributes to be set.
  60. */
  61. setTo( attrs ) {
  62. this.clear();
  63. for ( let value of attrs ) {
  64. this.set( value );
  65. }
  66. }
  67. /**
  68. * Checks if AttributeList contains attribute {@link treeModel.Attribute#isEqual equal} to given attribute or
  69. * attribute with given key if string was passed.
  70. *
  71. * @param {treeModel.Attribute|String} attrOrKey Attribute or key of attribute to check.
  72. * @returns {Boolean} `true` if given attribute or attribute with given key exists in AttributeList. `false` otherwise.
  73. */
  74. has( attrOrKey ) {
  75. if ( attrOrKey instanceof Attribute ) {
  76. let attr = this.get( attrOrKey.key );
  77. if ( attr ) {
  78. return attr.isEqual( attrOrKey );
  79. }
  80. } else {
  81. return super.has( attrOrKey );
  82. }
  83. return false;
  84. }
  85. /**
  86. * Gets an attribute value by attribute key.
  87. *
  88. * @param {String} key Key of attribute to look for.
  89. * @returns {*} Value of attribute with given key or null if the attribute has not been found in AttributeList
  90. */
  91. getValue( key ) {
  92. let attr = this.get( key );
  93. return attr ? attr.value : null;
  94. }
  95. /**
  96. * Checks whether this AttributeList has exactly same attributes as given one.
  97. *
  98. * @param {treeModel.AttributeList} attrs AttributeList to compare with.
  99. * @returns {Boolean} `true` if AttributeLists are equal, `false` otherwise.
  100. */
  101. isEqual( attrs ) {
  102. if ( this.size != attrs.size ) {
  103. return false;
  104. }
  105. for ( let attr of attrs ) {
  106. if ( !this.has( attr ) ) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * Gets an attribute by its key.
  114. *
  115. * @method get
  116. * @param {String} key Key of attribute to look for.
  117. * @returns {treeModel.Attribute|null} Attribute with given key or null if the attribute has not been found in
  118. * AttributeList.
  119. */
  120. /**
  121. * Removes an attribute with given key from AttributeList.
  122. *
  123. * @method delete
  124. * @param {String} key Key of attribute to remove.
  125. * @returns {Boolean} `true` if the attribute existed in the AttributeList. `false` otherwise.
  126. */
  127. /**
  128. * Removes all attributes from AttributeList.
  129. *
  130. * @method clear
  131. */
  132. }