attribute.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [ 'utils' ], ( utils ) => {
  7. /**
  8. * Attributes can store any additional information for nodes in the data model.
  9. *
  10. * @class treeModel.Attribute
  11. */
  12. class Attribute {
  13. /**
  14. * Creates a new instance of the `Attribute` class. Once attribute is created it is immutable.
  15. *
  16. * @param {String} key Attribute key.
  17. * @param {Mixed} value Attribute value.
  18. * @constructor
  19. */
  20. constructor( key, value ) {
  21. /**
  22. * Attribute key.
  23. *
  24. * @readonly
  25. * @property {String} key
  26. */
  27. this.key = key;
  28. /**
  29. * Attribute value. Note that value may be any type, including objects.
  30. *
  31. * @readonly
  32. * @property {Mixed} value
  33. */
  34. this.value = value;
  35. /**
  36. * Attribute hash. Used to compare attributes. Two attributes with the same key and value will have the same hash.
  37. *
  38. * @readonly
  39. * @private
  40. * @property {String} _hash
  41. */
  42. this._hash = this.key + ': ' + JSON.stringify( this.value, sort );
  43. // If attribute is already registered the registered one should be returned.
  44. if ( Attribute._register[ this._hash ] ) {
  45. return Attribute._register[ this._hash ];
  46. }
  47. // We do not care about the order, so collections with the same elements should return the same hash.
  48. function sort( key, value ) {
  49. if ( !utils.isArray( value ) && utils.isObject( value ) ) {
  50. const sorted = {};
  51. // Sort keys and fill up the sorted object.
  52. Object.keys( value ).sort().forEach( ( key ) => {
  53. sorted[ key ] = value[ key ];
  54. } );
  55. return sorted;
  56. } else {
  57. return value;
  58. }
  59. }
  60. }
  61. /**
  62. * Compares two attributes. Returns `true` if two attributes have the same key and value even if the order of keys
  63. * in the value object is different.
  64. *
  65. * let attr1 = new Attribute( 'foo', { a: 1, b: 2 } );
  66. * let attr2 = new Attribute( 'foo', { b: 2, a: 1 } );
  67. * attr1.isEqual( attr2 ); // true
  68. *
  69. * @param {treeModel.Attribute} otherAttr Attribute to compare with.
  70. * @returns {Boolean} True if attributes are equal to each other.
  71. */
  72. isEqual( otherAttr ) {
  73. return this._hash === otherAttr._hash;
  74. }
  75. /**
  76. * To save memory, commonly used attributes may be registered. If an attribute is registered the constructor will
  77. * always return the same instance of this attribute.
  78. *
  79. * Note that attributes are registered globally.
  80. *
  81. * let attr1 = Attribute.register( 'bold', true );
  82. * let attr2 = Attribute.register( 'bold', true );
  83. * let attr3 = new Attribute( 'bold', true );
  84. * attr1 === attr2 // true
  85. * attr1 === attr3 // true
  86. *
  87. * @static
  88. * @param {String} key Attribute key.
  89. * @param {Mixed} value Attribute value.
  90. * @returns {treeModel.Attribute} Registered attribute.
  91. */
  92. static register( key, value ) {
  93. const attr = new Attribute( key, value );
  94. if ( this._register[ attr._hash ] ) {
  95. return this._register[ attr._hash ];
  96. } else {
  97. this._register[ attr._hash ] = attr;
  98. return attr;
  99. }
  100. }
  101. }
  102. /**
  103. * Register of attributes in which all registered attributes are stored.
  104. *
  105. * @static
  106. * @private
  107. * @property {String} _hash
  108. */
  109. Attribute._register = {};
  110. return Attribute;
  111. } );