54b3f968dfec9256fbe8e5a10240e9e93c77eeec
[fnpeditor.git] / src / wlxml / extensions / metadata / metadata.js
1 define(function(require) {
2     
3 'use strict';
4
5 var _ = require('libs/underscore'),
6     smartxmlTransformations = require('smartxml/transformations'),
7     metadataKey = 'wlxml.metadata';
8
9
10 var Row = function(key, value, metadata) {
11     this.key = key;
12     this.value  = value;
13     this.metadata = metadata;
14 };
15
16 _.extend(Row.prototype, {
17     ChangeProperty: smartxmlTransformations.createContextTransformation({
18         impl: function(t, rowIndex, propName, value) {
19             var row = this.getMetadata().at(rowIndex);
20             t.rowIndex = rowIndex;
21             t.propName = propName;
22             t.oldValue = row[propName];
23             row[propName] = value;
24             this.triggerChangeEvent('metadataChanged', {row:row});
25         },
26         undo: function(t) {
27             var row = this.getMetadata().at(t.rowIndex);
28             row[t.propName] = t.oldValue;
29             this.triggerChangeEvent('metadataChanged', {row:row});
30         }
31     }),
32
33     setKey: function(key) {
34         return this.metadata.node.transform(this.ChangeProperty, [this.getIndex(), 'key', key]);
35     },
36     getKey: function() {
37         return this.key;
38     },
39     setValue: function(value) {
40         return this.metadata.node.transform(this.ChangeProperty, [this.getIndex(), 'value', value]);
41     },
42     getValue: function() {
43         return this.value;
44     },
45     remove: function() {
46         this.metadata.remove(this);
47     },
48     getIndex: function() {
49         return this.metadata.indexOf(this);
50     }
51 });
52
53
54 var Metadata = function(node) {
55     this._rows = [];
56     Object.defineProperty(this, 'length', {
57         get: function() {
58             return this._rows.length;
59         }
60     });
61     this.node = node;
62 };
63
64 _.extend(Metadata.prototype, {
65     Add: smartxmlTransformations.createContextTransformation({
66         impl: function(t, rowDesc) {
67             var metadata = this.getMetadata(),
68                 row = new Row(rowDesc.key, rowDesc.value, metadata);
69             metadata._rows.push(row);
70             t.rowIdx = row.getIndex();
71             this.triggerChangeEvent('metadataAdded', {row: row});
72             return row;
73         },
74         undo: function(t) {
75             this.getMetadata().at(t.rowIdx).remove();
76         }
77     }),
78
79     Remove: smartxmlTransformations.createContextTransformation({
80         impl: function(t, rowIdx) {
81             var metadata = this.getMetadata();
82             t.rowIdx = rowIdx;
83             t.row = metadata.at(rowIdx);
84             metadata._rows.splice(rowIdx, 1);
85             this.triggerChangeEvent('metadataRemoved', {row: t.row});
86         },
87         undo: function(t) {
88             var metadata = this.getMetadata();
89             metadata._rows.splice(t.rowIdx, 0, new Row(t.row.getKey(), t.row.getValue(), metadata));
90         }
91     }),
92
93     forEach: function(callback) {
94         return this._rows.forEach(callback);
95     },
96     add: function(rowDesc, options) {
97         var row;
98         options = _.extend({undoable: true}, options);
99         if(options.undoable) {
100             return this.node.transform(this.Add, [rowDesc]);
101         } else {
102             row = new Row(rowDesc.key, rowDesc.value, this);
103             this._rows.push(row);
104             return row;
105         }
106     },
107     at: function(idx) {
108         return this._rows[idx];
109     },
110     indexOf: function(row) {
111         var idx = this._rows.indexOf(row);
112         if(idx !== -1) {
113             return idx;
114         }
115         return undefined;
116     },
117     remove: function(row) {
118         var idx = this.indexOf(row);
119         if(typeof idx !== 'undefined') {
120             this.node.transform(this.Remove, [idx]);
121         }
122     },
123     clone: function(node) {
124         var clone = new Metadata(node);
125         this._rows.forEach(function(row) {
126             clone._rows.push(new Row(row.getKey(), row.getValue(), clone));
127         });
128         return clone;
129     }
130 });
131
132
133 return {
134     elementNode: {
135         methods: {
136             getMetadata: function() {
137                 if(!this.getData(metadataKey)) {
138                     this.setData(metadataKey, new Metadata(this));
139                 }
140                 return this.getData(metadataKey);
141             }
142         }
143     }
144 };
145
146 });
147