UNPKG

11.4 kBJavaScriptView Raw
1'use strict';
2
3function _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
5function _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
7function _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
9function _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
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13function _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
15function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
16
17function _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
19function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
20
21function _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
23function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
24
25function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
26
27function _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
29function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
30
31var EventEmitter = require('eventemitter3');
32
33var Element = require('./element');
34
35var LinkedList = Element.LinkedList;
36
37var ObjectGenerator = require('./object-generator');
38/**
39 * The event constant.
40 */
41
42
43var Events = {
44 'Cancel': 'Document::Cancel'
45};
46/**
47 * The id field.
48 */
49
50var ID = '_id';
51/**
52 * Represents a document.
53 */
54
55var Document = /*#__PURE__*/function (_EventEmitter) {
56 _inherits(Document, _EventEmitter);
57
58 var _super = _createSuper(Document);
59
60 /**
61 * Create the new document from the provided object.
62 *
63 * @param {Object} doc - The document.
64 * @param {boolean} cloned - If it is a cloned document.
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 * Generate the javascript object for this document.
80 *
81 * @returns {Object} The javascript object.
82 */
83
84
85 _createClass(Document, [{
86 key: "cancel",
87 value:
88 /**
89 * Send cancel event.
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 * Generate the javascript object with the original elements in this document.
115 *
116 * @returns {Object} The original javascript object.
117 */
118
119 }, {
120 key: "generateOriginalObject",
121 value: function generateOriginalObject() {
122 return ObjectGenerator.generateOriginal(this.elements);
123 }
124 /**
125 * Get an element by its key.
126 *
127 * @param {String} key
128 *
129 * @returns {Element} The element.
130 */
131
132 }, {
133 key: "get",
134 value: function get(key) {
135 return this.elements.get(key);
136 }
137 /**
138 * Get an element by a series of segment names.
139 *
140 * @param {Array} path - The series of fieldnames. Cannot be empty.
141 *
142 * @returns {Element} The element.
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 * Get the _id value for the document.
168 *
169 * @returns {Object} The id.
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 * Get the _id value as a string. Required if _id is not always an ObjectId.
180 *
181 * @returns {String} The string id.
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 * Insert a placeholder element at the end of the document.
199 *
200 * @returns {Element} The placeholder element.
201 */
202
203 }, {
204 key: "insertPlaceholder",
205 value: function insertPlaceholder() {
206 return this.insertEnd('', '');
207 }
208 /**
209 * Add a new element to this document.
210 *
211 * @param {String} key - The element key.
212 * @param {Object} value - The value.
213 *
214 * @returns {Element} The new element.
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 * Insert an element after the provided element.
226 *
227 * @param {Element} element - The element to insert after.
228 * @param {String} key - The key.
229 * @param {Object} value - The value.
230 *
231 * @returns {Element} The new element.
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 * A document always exists, is never added.
243 *
244 * @returns {false} Always false.
245 */
246
247 }, {
248 key: "isAdded",
249 value: function isAdded() {
250 return false;
251 }
252 /**
253 * Determine if the element is modified at all.
254 *
255 * @returns {Boolean} If the element is modified.
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 * A document is never removed
282 *
283 * @returns {false} Always false.
284 */
285
286 }, {
287 key: "isRemoved",
288 value: function isRemoved() {
289 return false;
290 }
291 /**
292 * The document object is always the root object.
293 *
294 * @returns {true} Always true.
295 */
296
297 }, {
298 key: "isRoot",
299 value: function isRoot() {
300 return true;
301 }
302 /**
303 * Handle the next element in the document.
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 * Generates a sequence of elements.
324 *
325 * @returns {Array} The elements.
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
338module.exports = Document;
339module.exports.Events = Events;
\No newline at end of file