This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
475 lines (418 loc) · 11.3 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* Abstract implementation of edit tree interface.
* Edit tree is a named container of editable “name-value” child elements,
* parsed from <code>source</code>. This container provides convenient methods
* for editing/adding/removing child elements. All these update actions are
* instantly reflected in the <code>source</code> code with respect of formatting.
* <br><br>
* For example, developer can create an edit tree from CSS rule and add or
* remove properties from it–all changes will be immediately reflected in the
* original source.
* <br><br>
* All classes defined in this module should be extended the same way as in
* Backbone framework: using <code>extend</code> method to create new class and
* <code>initialize</code> method to define custom class constructor.
*
* @example
* <pre><code>
* var MyClass = require('editTree/base').EditElement.extend({
* initialize: function() {
* // constructor code here
* }
* });
*
* var elem = new MyClass();
* </code></pre>
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var range = require('../assets/range');
var utils = require('../utils/common');
var klass = require('../vendor/klass');
/**
* Named container of edited source
* @type EditContainer
* @param {String} source
* @param {Object} options
*/
function EditContainer(source, options) {
this.options = utils.extend({offset: 0}, options);
/**
* Source code of edited structure. All changes in the structure are
* immediately reflected into this property
*/
this.source = source;
/**
* List of all editable children
* @private
*/
this._children = [];
/**
* Hash of all positions of container
* @private
*/
this._positions = {
name: 0
};
this.initialize.apply(this, arguments);
}
/**
* The self-propagating extend function for classes.
* @type Function
*/
EditContainer.extend = klass.extend;
EditContainer.prototype = {
type: 'container',
/**
* Child class constructor
*/
initialize: function() {},
/**
* Make position absolute
* @private
* @param {Number} num
* @param {Boolean} isAbsolute
* @returns {Boolean}
*/
_pos: function(num, isAbsolute) {
return num + (isAbsolute ? this.options.offset : 0);
},
/**
* Replace substring of tag's source
* @param {String} value
* @param {Number} start
* @param {Number} end
* @private
*/
_updateSource: function(value, start, end) {
// create modification range
var r = range.create(start, typeof end === 'undefined' ? 0 : end - start);
var delta = value.length - r.length();
var update = function(obj) {
Object.keys(obj).forEach(function(k) {
if (obj[k] >= r.end) {
obj[k] += delta;
}
});
};
// update affected positions of current container
update(this._positions);
// update affected positions of children
var recursiveUpdate = function(items) {
items.forEach(function(item) {
update(item._positions);
if (item.type == 'container') {
recursiveUpdate(item.list());
}
});
};
recursiveUpdate(this.list());
this.source = utils.replaceSubstring(this.source, value, r);
},
/**
* Adds new attribute
* @param {String} name Property name
* @param {String} value Property value
* @param {Number} pos Position at which to insert new property. By
* default the property is inserted at the end of rule
* @returns {EditElement} Newly created element
*/
add: function(name, value, pos) {
// this is abstract implementation
var item = new EditElement(name, value);
this._children.push(item);
return item;
},
/**
* Returns attribute object
* @param {String} name Attribute name or its index
* @returns {EditElement}
*/
get: function(name) {
if (typeof name === 'number') {
return this.list()[name];
}
if (typeof name === 'string') {
return utils.find(this.list(), function(prop) {
return prop.name() === name;
});
}
return name;
},
/**
* Returns all children by name or indexes
* @param {Object} name Element name(s) or indexes (<code>String</code>,
* <code>Array</code>, <code>Number</code>)
* @returns {Array}
*/
getAll: function(name) {
if (!Array.isArray(name))
name = [name];
// split names and indexes
var names = [], indexes = [];
name.forEach(function(item) {
if (typeof item === 'string') {
names.push(item);
} else if (typeof item === 'number') {
indexes.push(item);
}
});
return this.list().filter(function(attribute, i) {
return ~indexes.indexOf(i) || ~names.indexOf(attribute.name());
});
},
/**
* Returns list of all editable child elements
* @returns {Array}
*/
list: function() {
return this._children;
},
/**
* Remove child element
* @param {String} name Property name or its index
*/
remove: function(name) {
var element = this.get(name);
if (element) {
this._updateSource('', element.fullRange());
var ix = this._children.indexOf(element);
if (~ix) {
this._children.splice(ix, 1);
}
}
},
/**
* Returns index of editble child in list
* @param {Object} item
* @returns {Number}
*/
indexOf: function(item) {
return this.list().indexOf(this.get(item));
},
/**
* Returns or updates element value. If such element doesn't exists,
* it will be created automatically and added at the end of child list.
* @param {String} name Element name or its index
* @param {String} value New element value
* @returns {String}
*/
value: function(name, value, pos) {
var element = this.get(name);
if (element)
return element.value(value);
if (typeof value !== 'undefined') {
// no such element — create it
return this.add(name, value, pos);
}
},
/**
* Returns all values of child elements found by <code>getAll()</code>
* method
* @param {Object} name Element name(s) or indexes (<code>String</code>,
* <code>Array</code>, <code>Number</code>)
* @returns {Array}
*/
values: function(name) {
return this.getAll(name).map(function(element) {
return element.value();
});
},
/**
* Sets or gets container name
* @param {String} val New name. If not passed, current
* name is returned
* @return {String}
*/
name: function(val) {
if (typeof val !== 'undefined' && this._name !== (val = String(val))) {
this._updateSource(val, this._positions.name, this._positions.name + this._name.length);
this._name = val;
}
return this._name;
},
/**
* Returns name range object
* @param {Boolean} isAbsolute Return absolute range (with respect of
* rule offset)
* @returns {Range}
*/
nameRange: function(isAbsolute) {
return range.create(this._positions.name + (isAbsolute ? this.options.offset : 0), this.name());
},
/**
* Returns range of current source
* @param {Boolean} isAbsolute
*/
range: function(isAbsolute) {
return range.create(isAbsolute ? this.options.offset : 0, this.valueOf());
},
/**
* Returns element that belongs to specified position
* @param {Number} pos
* @param {Boolean} isAbsolute
* @returns {EditElement}
*/
itemFromPosition: function(pos, isAbsolute) {
return utils.find(this.list(), function(elem) {
return elem.range(isAbsolute).inside(pos);
});
},
/**
* Returns source code of current container
* @returns {String}
*/
toString: function() {
return this.valueOf();
},
valueOf: function() {
return this.source;
}
};
/**
* @param {EditContainer} parent
* @param {Object} nameToken
* @param {Object} valueToken
*/
function EditElement(parent, nameToken, valueToken) {
/** @type EditContainer */
this.parent = parent;
this._name = nameToken.value;
this._value = valueToken ? valueToken.value : '';
this._positions = {
name: nameToken.start,
value: valueToken ? valueToken.start : -1
};
this.initialize.apply(this, arguments);
}
/**
* The self-propagating extend function for classes.
* @type Function
*/
EditElement.extend = klass.extend;
EditElement.prototype = {
type: 'element',
/**
* Child class constructor
*/
initialize: function() {},
/**
* Make position absolute
* @private
* @param {Number} num
* @param {Boolean} isAbsolute
* @returns {Boolean}
*/
_pos: function(num, isAbsolute) {
return num + (isAbsolute ? this.parent.options.offset : 0);
},
/**
* Sets of gets element value
* @param {String} val New element value. If not passed, current
* value is returned
* @returns {String}
*/
value: function(val) {
if (typeof val !== 'undefined' && this._value !== (val = String(val))) {
this.parent._updateSource(val, this.valueRange());
this._value = val;
}
return this._value;
},
/**
* Sets of gets element name
* @param {String} val New element name. If not passed, current
* name is returned
* @returns {String}
*/
name: function(val) {
if (typeof val !== 'undefined' && this._name !== (val = String(val))) {
this.parent._updateSource(val, this.nameRange());
this._name = val;
}
return this._name;
},
/**
* Returns position of element name token
* @param {Boolean} isAbsolute Return absolute position
* @returns {Number}
*/
namePosition: function(isAbsolute) {
return this._pos(this._positions.name, isAbsolute);
},
/**
* Returns position of element value token
* @param {Boolean} isAbsolute Return absolute position
* @returns {Number}
*/
valuePosition: function(isAbsolute) {
return this._pos(this._positions.value, isAbsolute);
},
/**
* Returns element name
* @param {Boolean} isAbsolute Return absolute range
* @returns {Range}
*/
range: function(isAbsolute) {
return range.create(this.namePosition(isAbsolute), this.valueOf());
},
/**
* Returns full element range, including possible indentation
* @param {Boolean} isAbsolute Return absolute range
* @returns {Range}
*/
fullRange: function(isAbsolute) {
return this.range(isAbsolute);
},
/**
* Returns element name range
* @param {Boolean} isAbsolute Return absolute range
* @returns {Range}
*/
nameRange: function(isAbsolute) {
return range.create(this.namePosition(isAbsolute), this.name());
},
/**
* Returns element value range
* @param {Boolean} isAbsolute Return absolute range
* @returns {Range}
*/
valueRange: function(isAbsolute) {
return range.create(this.valuePosition(isAbsolute), this.value());
},
/**
* Returns current element string representation
* @returns {String}
*/
toString: function() {
return this.valueOf();
},
valueOf: function() {
return this.name() + this.value();
}
};
return {
EditContainer: EditContainer,
EditElement: EditElement,
/**
* Creates token that can be fed to <code>EditElement</code>
* @param {Number} start
* @param {String} value
* @param {String} type
* @returns
*/
createToken: function(start, value, type) {
var obj = {
start: start || 0,
value: value || '',
type: type
};
obj.end = obj.start + obj.value.length;
return obj;
}
};
});