1 | 'use strict';
|
2 |
|
3 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
4 |
|
5 | function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
6 |
|
7 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
8 |
|
9 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
10 |
|
11 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
12 |
|
13 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
14 |
|
15 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
16 |
|
17 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
18 |
|
19 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
20 |
|
21 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
22 |
|
23 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
24 |
|
25 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
26 |
|
27 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
28 |
|
29 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
30 |
|
31 | var EventEmitter = require('eventemitter3');
|
32 |
|
33 | var Element = require('./element');
|
34 |
|
35 | var LinkedList = Element.LinkedList;
|
36 |
|
37 | var ObjectGenerator = require('./object-generator');
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | var Events = {
|
44 | 'Cancel': 'Document::Cancel'
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | var ID = '_id';
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var Document = function (_EventEmitter) {
|
56 | _inherits(Document, _EventEmitter);
|
57 |
|
58 | var _super = _createSuper(Document);
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function Document(doc, cloned) {
|
67 | var _this;
|
68 |
|
69 | _classCallCheck(this, Document);
|
70 |
|
71 | _this = _super.call(this);
|
72 | _this.doc = doc;
|
73 | _this.cloned = cloned || false;
|
74 | _this.isUpdatable = true;
|
75 | _this.elements = _this._generateElements();
|
76 | return _this;
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | _createClass(Document, [{
|
86 | key: "cancel",
|
87 | value:
|
88 | |
89 |
|
90 |
|
91 | function cancel() {
|
92 | var _iterator = _createForOfIteratorHelper(this.elements),
|
93 | _step;
|
94 |
|
95 | try {
|
96 | for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
97 | var element = _step.value;
|
98 | element.cancel();
|
99 | }
|
100 | } catch (err) {
|
101 | _iterator.e(err);
|
102 | } finally {
|
103 | _iterator.f();
|
104 | }
|
105 |
|
106 | this.emit(Events.Cancel);
|
107 | }
|
108 | }, {
|
109 | key: "generateObject",
|
110 | value: function generateObject() {
|
111 | return ObjectGenerator.generate(this.elements);
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | }, {
|
120 | key: "generateOriginalObject",
|
121 | value: function generateOriginalObject() {
|
122 | return ObjectGenerator.generateOriginal(this.elements);
|
123 | }
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | }, {
|
133 | key: "get",
|
134 | value: function get(key) {
|
135 | return this.elements.get(key);
|
136 | }
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | }, {
|
146 | key: "getChild",
|
147 | value: function getChild(path) {
|
148 | if (!path) {
|
149 | return undefined;
|
150 | }
|
151 |
|
152 | var element = this.currentType === 'Array' ? this.elements.at(path[0]) : this.elements.get(path[0]);
|
153 | var i = 1;
|
154 |
|
155 | while (i < path.length) {
|
156 | if (element === undefined) {
|
157 | return undefined;
|
158 | }
|
159 |
|
160 | element = element.currentType === 'Array' ? element.at(path[i]) : element.get(path[i]);
|
161 | i++;
|
162 | }
|
163 |
|
164 | return element;
|
165 | }
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | }, {
|
173 | key: "getId",
|
174 | value: function getId() {
|
175 | var element = this.get(ID);
|
176 | return element ? element.generateObject() : null;
|
177 | }
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | }, {
|
185 | key: "getStringId",
|
186 | value: function getStringId() {
|
187 | var element = this.get(ID);
|
188 |
|
189 | if (!element) {
|
190 | return null;
|
191 | } else if (element.currentType === 'Array' || element.currentType === 'Object') {
|
192 | return JSON.stringify(element.generateObject());
|
193 | }
|
194 |
|
195 | return '' + element.value;
|
196 | }
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | }, {
|
204 | key: "insertPlaceholder",
|
205 | value: function insertPlaceholder() {
|
206 | return this.insertEnd('', '');
|
207 | }
|
208 | |
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | }, {
|
218 | key: "insertEnd",
|
219 | value: function insertEnd(key, value) {
|
220 | var newElement = this.elements.insertEnd(key, value, true, this);
|
221 | this.emit(Element.Events.Added);
|
222 | return newElement;
|
223 | }
|
224 | |
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | }, {
|
235 | key: "insertAfter",
|
236 | value: function insertAfter(element, key, value) {
|
237 | var newElement = this.elements.insertAfter(element, key, value, true, this);
|
238 | this.emit(Element.Events.Added);
|
239 | return newElement;
|
240 | }
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | }, {
|
248 | key: "isAdded",
|
249 | value: function isAdded() {
|
250 | return false;
|
251 | }
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | }, {
|
259 | key: "isModified",
|
260 | value: function isModified() {
|
261 | var _iterator2 = _createForOfIteratorHelper(this.elements),
|
262 | _step2;
|
263 |
|
264 | try {
|
265 | for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
266 | var element = _step2.value;
|
267 |
|
268 | if (element.isModified()) {
|
269 | return true;
|
270 | }
|
271 | }
|
272 | } catch (err) {
|
273 | _iterator2.e(err);
|
274 | } finally {
|
275 | _iterator2.f();
|
276 | }
|
277 |
|
278 | return false;
|
279 | }
|
280 | |
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | }, {
|
287 | key: "isRemoved",
|
288 | value: function isRemoved() {
|
289 | return false;
|
290 | }
|
291 | |
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | }, {
|
298 | key: "isRoot",
|
299 | value: function isRoot() {
|
300 | return true;
|
301 | }
|
302 | |
303 |
|
304 |
|
305 |
|
306 | }, {
|
307 | key: "next",
|
308 | value: function next() {
|
309 | this.elements.flush();
|
310 | var lastElement = this.elements.lastElement;
|
311 |
|
312 | if (lastElement && lastElement.isAdded()) {
|
313 | if (lastElement.isBlank()) {
|
314 | lastElement.remove();
|
315 | } else {
|
316 | this.insertPlaceholder();
|
317 | }
|
318 | } else {
|
319 | this.insertPlaceholder();
|
320 | }
|
321 | }
|
322 | |
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | }, {
|
329 | key: "_generateElements",
|
330 | value: function _generateElements() {
|
331 | return new LinkedList(this, this.doc);
|
332 | }
|
333 | }]);
|
334 |
|
335 | return Document;
|
336 | }(EventEmitter);
|
337 |
|
338 | module.exports = Document;
|
339 | module.exports.Events = Events; |
\ | No newline at end of file |