+_.extend(Metadata.prototype, {
+ Add: smartxmlTransformations.createContextTransformation({
+ impl: function(t, rowDesc) {
+ var metadata = this.getMetadata(),
+ row = new Row(rowDesc.key, rowDesc.value, metadata);
+ metadata._rows.push(row);
+ t.rowIdx = row.getIndex();
+ this.triggerChangeEvent('metadataAdded', {row: row});
+ return row;
+ },
+ undo: function(t) {
+ this.getMetadata().at(t.rowIdx).remove();
+ }
+ }),
+
+ Remove: smartxmlTransformations.createContextTransformation({
+ impl: function(t, rowIdx) {
+ var metadata = this.getMetadata();
+ t.rowIdx = rowIdx;
+ t.row = metadata.at(rowIdx);
+ metadata._rows.splice(rowIdx, 1);
+ this.triggerChangeEvent('metadataRemoved', {row: t.row});
+ },
+ undo: function(t) {
+ var metadata = this.getMetadata(),
+ row = new Row(t.row.getKey(), t.row.getValue(), metadata);
+ metadata._rows.splice(t.rowIdx, 0, row);
+ this.triggerChangeEvent('metadataAdded', {row: row});
+ }
+ }),
+
+ _iter: function(method, callback, key) {
+ return this._rows
+ .filter(function(row) { return !key || row.getKey() === key; })
+ [method](function(row) { return callback(row); });
+ },
+ forEach: function(callback, key) {
+ return this._iter('forEach', callback, key);
+ },
+ some: function(callback, key) {
+ return this._iter('some', callback, key);
+ },
+ add: function(rowDesc, options) {
+ var row;
+ options = _.extend({undoable: true}, options);
+ if(options.undoable) {
+ return this.node.transform(this.Add, [rowDesc]);
+ } else {
+ row = new Row(rowDesc.key, rowDesc.value, this);
+ this._rows.push(row);
+ return row;
+ }
+ },
+ at: function(idx) {
+ return this._rows[idx];
+ },
+ indexOf: function(row) {
+ var idx = this._rows.indexOf(row);
+ if(idx !== -1) {
+ return idx;
+ }
+ return undefined;
+ },
+ remove: function(row) {
+ var idx = this.indexOf(row);
+ if(typeof idx !== 'undefined') {
+ this.node.transform(this.Remove, [idx]);
+ }
+ },
+ clone: function(node) {
+ var clone = new Metadata(node);
+ this._rows.forEach(function(row) {
+ clone._rows.push(new Row(row.getKey(), row.getValue(), clone));
+ });
+ return clone;