| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- 'use strict';
- import Attribute from './attribute.js';
- /**
- * List of attributes.
- *
- * @class treeModel.AttributeList
- */
- export default class AttributeList extends Map {
- /**
- * Creates AttributeList. If parameter is passed, initializes created list with passed {@link treeModel.Attribute}s.
- *
- * @constructor
- * @param {Iterable.<treeModel.Attribute>} attrs Attributes to initialize with.
- */
- constructor( attrs ) {
- super();
- if ( attrs ) {
- this.setTo( attrs );
- }
- /**
- * Amount of attributes added to the AttributeList.
- *
- * @property {Number} size
- */
- }
- /**
- * AttributeList iterator. Iterates over all attributes from the list.
- */
- [ Symbol.iterator ]() {
- let it = super[ Symbol.iterator ]();
- return {
- next: () => {
- let step = it.next();
- return {
- value: step.value ? step.value[ 1 ] : undefined,
- done: step.done
- };
- }
- };
- }
- /**
- * Adds attribute to the attributes list. If attribute with the same key already is set, it overwrites its values.
- *
- * @chainable
- * @param {treeModel.Attribute} attr Attribute to add or overwrite.
- * @returns {treeModel.AttributeList} This AttributeList object.
- */
- set( attr ) {
- super.set( attr.key, attr );
- return this;
- }
- /**
- * Removes all attributes from AttributeList and adds given attributes.
- *
- * @param {Iterable.<Attribute>} attrs Iterable object containing attributes to be set.
- */
- setTo( attrs ) {
- this.clear();
- for ( let value of attrs ) {
- this.set( value );
- }
- }
- /**
- * Checks if AttributeList contains attribute {@link treeModel.Attribute#isEqual equal} to given attribute or
- * attribute with given key if string was passed.
- *
- * @param {treeModel.Attribute|String} attrOrKey Attribute or key of attribute to check.
- * @returns {Boolean} `true` if given attribute or attribute with given key exists in AttributeList. `false` otherwise.
- */
- has( attrOrKey ) {
- if ( attrOrKey instanceof Attribute ) {
- let attr = this.get( attrOrKey.key );
- if ( attr ) {
- return attr.isEqual( attrOrKey );
- }
- } else {
- return super.has( attrOrKey );
- }
- return false;
- }
- /**
- * Gets an attribute value by attribute key.
- *
- * @param {String} key Key of attribute to look for.
- * @returns {*} Value of attribute with given key or null if the attribute has not been found in AttributeList
- */
- getValue( key ) {
- let attr = this.get( key );
- return attr ? attr.value : null;
- }
- /**
- * Checks whether this AttributeList has exactly same attributes as given one.
- *
- * @param {treeModel.AttributeList} attrs AttributeList to compare with.
- * @returns {Boolean} `true` if AttributeLists are equal, `false` otherwise.
- */
- isEqual( attrs ) {
- if ( this.size != attrs.size ) {
- return false;
- }
- for ( let attr of attrs ) {
- if ( !this.has( attr ) ) {
- return false;
- }
- }
- return true;
- }
- /**
- * Gets an attribute by its key.
- *
- * @method get
- * @param {String} key Key of attribute to look for.
- * @returns {treeModel.Attribute|null} Attribute with given key or null if the attribute has not been found in
- * AttributeList.
- */
- /**
- * Removes an attribute with given key from AttributeList.
- *
- * @method delete
- * @param {String} key Key of attribute to remove.
- * @returns {Boolean} `true` if the attribute existed in the AttributeList. `false` otherwise.
- */
- /**
- * Removes all attributes from AttributeList.
- *
- * @method clear
- */
- }
|