2013-03-20 22:35:05 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel AnnotationSet class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Annotation set.
|
|
|
|
*
|
|
|
|
* @constructor
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.IndexValueStore} store Index-value store
|
2013-07-26 15:16:33 +00:00
|
|
|
* @param {number[]} [indexes] Array of store indexes
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet = function VeDmAnnotationSet( store, storeIndexes ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
// Parent constructor
|
|
|
|
this.store = store;
|
2013-07-26 15:16:33 +00:00
|
|
|
this.storeIndexes = storeIndexes || [];
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index-value store.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.IndexValueStore} Index-value store
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getStore = function () {
|
|
|
|
return this.store;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a clone.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.AnnotationSet} Copy of annotation set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.clone = function () {
|
2013-04-18 17:56:03 +00:00
|
|
|
return new ve.dm.AnnotationSet( this.getStore(), this.storeIndexes.slice( 0 ) );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an annotation set containing only annotations within the set with a specific name.
|
|
|
|
*
|
|
|
|
* @method
|
2013-04-15 21:35:32 +00:00
|
|
|
* @param {string} name Type name
|
2013-03-20 22:35:05 +00:00
|
|
|
* @returns {ve.dm.AnnotationSet} Copy of annotation set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getAnnotationsByName = function ( name ) {
|
2013-04-15 21:35:32 +00:00
|
|
|
return this.filter( function ( annotation ) { return annotation.name === name; } );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
2013-05-01 18:36:32 +00:00
|
|
|
/**
|
|
|
|
* Get an annotation set containing only annotations within the set which are comparable
|
|
|
|
* to a specific annotation.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to compare to
|
|
|
|
* @returns {ve.dm.AnnotationSet} Copy of annotation set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getComparableAnnotations = function ( annotation ) {
|
|
|
|
return this.filter( function ( a ) {
|
2013-05-28 23:10:19 +00:00
|
|
|
return ve.compare(
|
2013-05-01 18:36:32 +00:00
|
|
|
annotation.getComparableObject(),
|
|
|
|
a.getComparableObject()
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
/**
|
|
|
|
* Check if any annotations in the set have a specific name.
|
|
|
|
*
|
|
|
|
* @method
|
2013-04-15 21:35:32 +00:00
|
|
|
* @param {string} name Type name
|
2013-03-20 22:35:05 +00:00
|
|
|
* @returns {boolean} Annotation of given type exists in the set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.hasAnnotationWithName = function ( name ) {
|
2013-04-15 21:35:32 +00:00
|
|
|
return this.containsMatching( function ( annotation ) { return annotation.name === name; } );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Get an annotation or all annotations from the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-07-26 15:16:33 +00:00
|
|
|
* set.get( 5 ) returns the annotation at offset 5, set.get() returns an array with all annotations
|
2013-05-06 20:08:52 +00:00
|
|
|
* in the entire set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-07-26 15:16:33 +00:00
|
|
|
* @param {number} [offset] If set, only get the annotation at the offset
|
|
|
|
* @returns {ve.dm.Annotation[]|ve.dm.Annotation|undefined} The annotation at offset, or an array of all
|
2013-07-25 02:02:50 +00:00
|
|
|
* annotations in the set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.get = function ( offset ) {
|
|
|
|
if ( offset !== undefined ) {
|
|
|
|
return this.getStore().value( this.getIndex( offset ) );
|
2013-03-20 22:35:05 +00:00
|
|
|
} else {
|
|
|
|
return this.getStore().values( this.getIndexes() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Get store index from offset within annotation set.
|
2013-03-20 22:35:05 +00:00
|
|
|
* @param {number} offset Offset within annotation set
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {number} Store index at specified offset
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getIndex = function ( offset ) {
|
|
|
|
return this.storeIndexes[offset];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Get all store indexes.
|
|
|
|
* @returns {Array} Store indexes
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getIndexes = function () {
|
|
|
|
return this.storeIndexes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the length of the set.
|
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {number} The number of annotations in the set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.getLength = function () {
|
|
|
|
return this.storeIndexes.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the set is empty.
|
|
|
|
*
|
|
|
|
* @method
|
2013-05-05 19:35:34 +00:00
|
|
|
* @returns {boolean} The set is empty
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.isEmpty = function () {
|
|
|
|
return this.getLength() === 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Check whether a given annotation occurs in the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* Annotations are compared by store index.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.Annotation} annotation Annotation
|
|
|
|
* @returns {boolean} There is an annotation in the set with the same hash as annotation
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-04-24 10:02:13 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.contains = function ( annotation ) {
|
2013-07-26 15:16:33 +00:00
|
|
|
return this.offsetOf( annotation ) !== -1;
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
2013-04-25 21:55:43 +00:00
|
|
|
/**
|
|
|
|
* Check whether a given store index occurs in the set.
|
|
|
|
*
|
|
|
|
* @method
|
2013-07-26 15:16:33 +00:00
|
|
|
* @param {number} storeIndex Store index of annotation
|
2013-04-25 21:55:43 +00:00
|
|
|
* @returns {boolean} There is an annotation in the set with this store index
|
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.containsIndex = function ( storeIndex ) {
|
|
|
|
return ve.indexOf( storeIndex, this.getIndexes() ) !== -1;
|
2013-04-25 21:55:43 +00:00
|
|
|
};
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Check whether the set contains any of the annotations in another set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Set to compare the set with
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {boolean} There is at least one annotation in set that is also in the set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.containsAnyOf = function ( set ) {
|
|
|
|
var i, length, setIndexes = set.getIndexes(), thisIndexes = this.getIndexes();
|
|
|
|
for ( i = 0, length = setIndexes.length; i < length; i++ ) {
|
|
|
|
if ( ve.indexOf( setIndexes[i], thisIndexes ) !== -1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Check whether the set contains all of the annotations in another set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Set to compare the set with
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {boolean} All annotations in set are also in the set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.containsAllOf = function ( set ) {
|
|
|
|
var i, length, setIndexes = set.getIndexes(), thisIndexes = this.getIndexes();
|
|
|
|
for ( i = 0, length = setIndexes.length; i < length; i++ ) {
|
|
|
|
if ( ve.indexOf( setIndexes[i], thisIndexes ) === -1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:16:33 +00:00
|
|
|
* Get the offset of a given annotation in the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to search for
|
2013-07-26 15:16:33 +00:00
|
|
|
* @returns {number} Offset of annotation in the set, or -1 if annotation is not in the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.offsetOf = function ( annotation ) {
|
2013-10-15 19:59:14 +00:00
|
|
|
return this.offsetOfIndex( this.store.indexOfHash( OO.getHash( annotation ) ) );
|
2013-07-26 15:41:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the offset of a given annotation in the set by store index.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {number} storeIndex Store index of annotation to search for
|
|
|
|
* @returns {number} Offset of annotation in the set, or -1 if annotation is not in the set.
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.offsetOfIndex = function ( storeIndex ) {
|
|
|
|
return ve.indexOf( storeIndex, this.getIndexes() );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the set by an item property.
|
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* This returns a new set with all annotations in the set for which the callback returned true for.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-15 21:35:32 +00:00
|
|
|
* @param {Function} callback Function that takes an annotation and returns boolean true to include
|
2013-03-20 22:35:05 +00:00
|
|
|
* @param {boolean} [returnBool] For internal use only
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {ve.dm.AnnotationSet} New set containing only the matching annotations
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-04-15 21:35:32 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.filter = function ( callback, returnBool ) {
|
2013-04-24 10:02:13 +00:00
|
|
|
var i, length, result, storeIndex, annotation;
|
2013-04-15 21:35:32 +00:00
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
if ( !returnBool ) {
|
|
|
|
result = this.clone();
|
|
|
|
// TODO: Should we be returning this on all methods that modify the original? Might help
|
|
|
|
// with chainability, but perhaps it's also confusing because most chainable methods return
|
|
|
|
// a new hash set.
|
|
|
|
result.removeAll();
|
|
|
|
}
|
|
|
|
for ( i = 0, length = this.getLength(); i < length; i++ ) {
|
2013-04-18 23:09:55 +00:00
|
|
|
storeIndex = this.getIndex( i );
|
2013-04-24 10:02:13 +00:00
|
|
|
annotation = this.getStore().value( storeIndex );
|
|
|
|
if ( callback( annotation ) ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
if ( returnBool ) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-04-18 23:09:55 +00:00
|
|
|
result.storeIndexes.push( storeIndex );
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnBool ? false : result;
|
|
|
|
};
|
|
|
|
|
2013-05-05 19:35:34 +00:00
|
|
|
/**
|
|
|
|
* Check if the set contains an annotation comparable to the specified one.
|
|
|
|
*
|
|
|
|
* getComparableObject is used to compare the annotations, and should return
|
|
|
|
* true if an annotation is found which is mergeable with the specified one.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to compare to
|
|
|
|
* @returns {boolean} At least one comprable annotation found
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) {
|
|
|
|
return this.filter( function ( a ) {
|
2013-05-28 23:10:19 +00:00
|
|
|
return ve.compare(
|
2013-05-05 19:35:34 +00:00
|
|
|
annotation.getComparableObject(),
|
|
|
|
a.getComparableObject()
|
|
|
|
);
|
|
|
|
}, true );
|
|
|
|
};
|
|
|
|
|
2013-06-25 15:09:51 +00:00
|
|
|
/**
|
|
|
|
* HACK: Check if the set contains an annotation comparable to the specified one
|
|
|
|
* for the purposes of serialization.
|
|
|
|
*
|
|
|
|
* This method uses getComparableObjectForSerialization which also includes
|
|
|
|
* HTML attributes.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to compare to
|
|
|
|
* @returns {boolean} At least one comprable annotation found
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) {
|
|
|
|
return this.filter( function ( a ) {
|
2013-07-12 21:21:20 +00:00
|
|
|
return annotation.compareToForSerialization( a );
|
2013-06-25 15:09:51 +00:00
|
|
|
}, true );
|
|
|
|
};
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Check if the set contains at least one annotation where a given property matches a given filter.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* This is equivalent to (but more efficient than) `!this.filter( .. ).isEmpty()`.
|
|
|
|
*
|
|
|
|
* @see ve.dm.AnnotationSet#filter
|
|
|
|
*
|
|
|
|
* @method
|
2013-04-15 21:35:32 +00:00
|
|
|
* @param {Function} callback Function that takes an annotation and returns boolean true to include
|
2013-05-05 19:35:34 +00:00
|
|
|
* @returns {boolean} At least one matching annotation found
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-04-15 21:35:32 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.containsMatching = function ( callback ) {
|
|
|
|
return this.filter( callback, true );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
2013-05-01 18:36:32 +00:00
|
|
|
/**
|
|
|
|
* Check if the set contains the same annotations as another set.
|
|
|
|
*
|
|
|
|
* Compares annotations by their comparable object value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} annotationSet The annotationSet to compare this one to
|
|
|
|
* @returns {boolean} The annotations are the same
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.compareTo = function ( annotationSet ) {
|
2013-07-26 15:16:33 +00:00
|
|
|
var i, length = this.getIndexes().length;
|
2013-05-01 18:36:32 +00:00
|
|
|
|
|
|
|
if ( length === annotationSet.getLength() ) {
|
|
|
|
for ( i = 0; i < length; i++ ) {
|
|
|
|
if ( !annotationSet.containsComparable( this.get( i ) ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Add an annotation to the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* If the annotation is already present in the set, nothing happens.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-07-26 15:16:33 +00:00
|
|
|
* The annotation will be inserted before the annotation that is currently at the given offset. If offset is
|
|
|
|
* negative, it will be counted from the end (i.e. offset -1 is the last item, -2 the second-to-last,
|
|
|
|
* etc.). If offset is out of bounds, the annotation will be added to the end of the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to add
|
2013-07-26 15:16:33 +00:00
|
|
|
* @param {number} offset Offset to add the annotation at
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.add = function ( annotation, offset ) {
|
2013-04-24 10:02:13 +00:00
|
|
|
var storeIndex = this.getStore().index( annotation );
|
2013-03-20 22:35:05 +00:00
|
|
|
// negative offset
|
2013-07-26 15:16:33 +00:00
|
|
|
if ( offset < 0 ) {
|
|
|
|
offset = this.getLength() + offset;
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
|
|
|
// greater than length, add to end
|
2013-07-26 15:16:33 +00:00
|
|
|
if ( offset >= this.getLength() ) {
|
2013-04-24 10:02:13 +00:00
|
|
|
this.push( annotation );
|
2013-03-20 22:35:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if not in set already, splice in place
|
2013-04-25 21:55:43 +00:00
|
|
|
if ( !this.containsIndex( storeIndex ) ) {
|
2013-07-26 15:16:33 +00:00
|
|
|
this.storeIndexes.splice( offset, 0, storeIndex );
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Add all annotations in the given set to the end of the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* Annotations from the other set that are already in the set will not be added again.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Set to add to the set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.addSet = function ( set ) {
|
2013-10-23 01:26:50 +00:00
|
|
|
this.storeIndexes = OO.simpleArrayUnion( this.getIndexes(), set.getIndexes() );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Add an annotation at the end of the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to add
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-04-24 10:02:13 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.push = function ( annotation ) {
|
2013-07-26 15:41:29 +00:00
|
|
|
this.pushIndex( this.getStore().index( annotation ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an annotation at the end of the set by store index.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {number} storeIndex Store index of annotation to add
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.pushIndex = function ( storeIndex ) {
|
2013-07-05 20:12:39 +00:00
|
|
|
this.storeIndexes.push( storeIndex );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:16:33 +00:00
|
|
|
* Remove the annotation at a given offset.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-07-26 15:16:33 +00:00
|
|
|
* @param {number} offset Offset to remove item at. If negative, the counts from the end, see add()
|
|
|
|
* @throws {Error} Offset out of bounds.
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-07-26 15:16:33 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.removeAt = function ( offset ) {
|
|
|
|
if ( offset < 0 ) {
|
|
|
|
offset = this.getLength() + offset;
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
2013-07-26 15:16:33 +00:00
|
|
|
if ( offset >= this.getLength() ) {
|
|
|
|
throw new Error( 'Offset out of bounds' );
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
2013-07-26 15:16:33 +00:00
|
|
|
this.storeIndexes.splice( offset, 1 );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
2013-07-26 15:41:29 +00:00
|
|
|
/**
|
|
|
|
* Remove a given annotation from the set by store index.
|
|
|
|
*
|
|
|
|
* If the annotation isn't in the set, nothing happens.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {number} storeIndex Store index of annotation to remove
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.removeIndex = function ( storeIndex ) {
|
|
|
|
var offset = this.offsetOfIndex( storeIndex );
|
|
|
|
if ( offset !== -1 ) {
|
|
|
|
this.storeIndexes.splice( offset, 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-20 22:35:05 +00:00
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Remove a given annotation from the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* If the annotation isn't in the set, nothing happens.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-04-24 10:02:13 +00:00
|
|
|
* @param {ve.dm.Annotation} annotation Annotation to remove
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
2013-04-24 10:02:13 +00:00
|
|
|
ve.dm.AnnotationSet.prototype.remove = function ( annotation ) {
|
2013-07-26 15:16:33 +00:00
|
|
|
var offset = this.offsetOf( annotation );
|
|
|
|
if ( offset !== -1 ) {
|
|
|
|
this.storeIndexes.splice( offset, 1 );
|
2013-03-20 22:35:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Remove all annotations.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.removeAll = function () {
|
|
|
|
this.storeIndexes = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Remove all annotations in a given set from the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
2013-04-24 10:02:13 +00:00
|
|
|
* Annotations that aren't in the set are ignored.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Set to remove from the set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.removeSet = function ( set ) {
|
2013-10-23 01:26:50 +00:00
|
|
|
this.storeIndexes = OO.simpleArrayDifference( this.getIndexes(), set.getIndexes() );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-24 10:02:13 +00:00
|
|
|
* Remove all annotations that are not also in a given other set from the set.
|
2013-03-20 22:35:05 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Set to intersect with the set
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.removeNotInSet = function ( set ) {
|
2013-10-23 01:26:50 +00:00
|
|
|
this.storeIndexes = OO.simpleArrayIntersection( this.getIndexes(), set.getIndexes() );
|
2013-03-20 22:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the set.
|
|
|
|
*
|
|
|
|
* This returns a copy, the original set is not modified.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.AnnotationSet} Copy of the set with the order reversed.
|
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.reversed = function () {
|
|
|
|
var newSet = this.clone();
|
|
|
|
newSet.storeIndexes.reverse();
|
|
|
|
return newSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge another set into the set.
|
|
|
|
*
|
|
|
|
* This returns a copy, the original set is not modified.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Other set
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {ve.dm.AnnotationSet} Set containing all annotations in the set as well as all annotations in set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.mergeWith = function ( set ) {
|
|
|
|
var newSet = this.clone();
|
|
|
|
newSet.addSet( set );
|
|
|
|
return newSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the difference between the set and another set.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Other set
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {ve.dm.AnnotationSet} New set containing all annotations that are in the set but not in set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.diffWith = function ( set ) {
|
|
|
|
var newSet = this.clone();
|
|
|
|
newSet.removeSet( set );
|
|
|
|
return newSet;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the intersection of the set with another set.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.AnnotationSet} set Other set
|
2013-04-24 10:02:13 +00:00
|
|
|
* @returns {ve.dm.AnnotationSet} New set containing all annotations that are both in the set and in set
|
2013-03-20 22:35:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.AnnotationSet.prototype.intersectWith = function ( set ) {
|
|
|
|
var newSet = this.clone();
|
|
|
|
newSet.removeNotInSet( set );
|
|
|
|
return newSet;
|
|
|
|
};
|