Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Aligned the DocumentColorCollection API to the latest Collection API.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Oct 21, 2019
1 parent 22d20fd commit faf01b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
41 changes: 25 additions & 16 deletions src/documentcolorcollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
* @extends module:utils/collection~Collection
*/
export default class DocumentColorCollection extends Collection {
constructor( options ) {
super( options );
constructor( ...args ) {
super( ...args );

/**
* Indicates whether the document color collection is empty.
Expand All @@ -23,32 +23,43 @@ export default class DocumentColorCollection extends Collection {
* @readonly
* @member {Boolean} #isEmpty
*/
this.set( 'isEmpty', true );
this.set( 'isEmpty', !this.length );
}

/**
* Adds a color to the document color collection.
* Adds a color (or colors) to the document color collection.
*
* This method ensures that no color duplicates are inserted (compared using
* the color value of the {@link module:ui/colorgrid/colorgrid~ColorDefinition}).
*
* If the item does not have an ID, it will be automatically generated and set on the item.
*
* @chainable
* @param {module:ui/colorgrid/colorgrid~ColorDefinition} item
* @param {Number} [index] The position of the item in the collection. The item
* is pushed to the collection when `index` is not specified.
* @param {...(module:ui/colorgrid/colorgrid~ColorDefinition)} items
* @param {Number} [index] The position of the color (or colors) in the collection. Colors
* are pushed to the collection when `index` is not specified.
* @fires add
*/
add( item, index ) {
if ( this.find( element => element.color === item.color ) ) {
// No duplicates are allowed.
return;
add( ...args ) {
let items = args;

// No duplicates are allowed.
items = items.filter( ( item, index ) => {
// Don't filter out the add index (last argument).
if ( index === items.length - 1 && typeof item === 'number' ) {
return true;
}

return !this.find( element => element.color === item.color );
} );

if ( items.length ) {
super.add( ...items );
}

super.add( item, index );
this.set( 'isEmpty', !this.length );

this.set( 'isEmpty', false );
return this;
}

/**
Expand All @@ -57,9 +68,7 @@ export default class DocumentColorCollection extends Collection {
remove( subject ) {
const ret = super.remove( subject );

if ( this.length === 0 ) {
this.set( 'isEmpty', true );
}
this.set( 'isEmpty', !this.length );

return ret;
}
Expand Down
19 changes: 14 additions & 5 deletions tests/documentcolorcollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ describe( 'DocumentColorCollection', () => {
];

beforeEach( () => {
documentColorCollection = new DocumentColorCollection();

colors.forEach( item => {
documentColorCollection.add( item );
} );
documentColorCollection = new DocumentColorCollection( colors );
} );

it( 'constructor()', () => {
Expand Down Expand Up @@ -58,4 +54,17 @@ describe( 'DocumentColorCollection', () => {
expect( documentColorCollection.hasColor( '111' ) ).to.be.true;
expect( documentColorCollection.hasColor( '555' ) ).to.be.false;
} );

describe( 'add()', () => {
it( 'supports adding multiple items at a time', () => {
documentColorCollection = new DocumentColorCollection();

documentColorCollection.add( colors[ 0 ] );
documentColorCollection.add( colors[ 1 ], colors[ 2 ], 0 );

expect( documentColorCollection.map( item => item.color ) ).to.have.ordered.members( [
'222', '333', '111'
] );
} );
} );
} );

0 comments on commit faf01b0

Please sign in to comment.