1/*
2@license
3
4dhtmlxGantt v.6.3.5 Standard
5
6This version of dhtmlxGantt is distributed under GPL 2.0 license and can be legally used in GPL projects.
7
8To use dhtmlxGantt in non-GPL projects (and get Pro version of the product), please obtain Commercial/Enterprise or Ultimate license on our site https://dhtmlx.com/docs/products/dhtmlxGantt/#licensing or contact us at sales@dhtmlx.com
9
10(c) XB Software Ltd.
11
12*/
13(function webpackUniversalModuleDefinition(root, factory) {
14	if(typeof exports === 'object' && typeof module === 'object')
15		module.exports = factory();
16	else if(typeof define === 'function' && define.amd)
17		define("ext/dhtmlxgantt_undo", [], factory);
18	else if(typeof exports === 'object')
19		exports["ext/dhtmlxgantt_undo"] = factory();
20	else
21		root["ext/dhtmlxgantt_undo"] = factory();
22})(window, function() {
23return /******/ (function(modules) { // webpackBootstrap
24/******/ 	// The module cache
25/******/ 	var installedModules = {};
26/******/
27/******/ 	// The require function
28/******/ 	function __webpack_require__(moduleId) {
29/******/
30/******/ 		// Check if module is in cache
31/******/ 		if(installedModules[moduleId]) {
32/******/ 			return installedModules[moduleId].exports;
33/******/ 		}
34/******/ 		// Create a new module (and put it into the cache)
35/******/ 		var module = installedModules[moduleId] = {
36/******/ 			i: moduleId,
37/******/ 			l: false,
38/******/ 			exports: {}
39/******/ 		};
40/******/
41/******/ 		// Execute the module function
42/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
43/******/
44/******/ 		// Flag the module as loaded
45/******/ 		module.l = true;
46/******/
47/******/ 		// Return the exports of the module
48/******/ 		return module.exports;
49/******/ 	}
50/******/
51/******/
52/******/ 	// expose the modules object (__webpack_modules__)
53/******/ 	__webpack_require__.m = modules;
54/******/
55/******/ 	// expose the module cache
56/******/ 	__webpack_require__.c = installedModules;
57/******/
58/******/ 	// define getter function for harmony exports
59/******/ 	__webpack_require__.d = function(exports, name, getter) {
60/******/ 		if(!__webpack_require__.o(exports, name)) {
61/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
62/******/ 		}
63/******/ 	};
64/******/
65/******/ 	// define __esModule on exports
66/******/ 	__webpack_require__.r = function(exports) {
67/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
68/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
69/******/ 		}
70/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
71/******/ 	};
72/******/
73/******/ 	// create a fake namespace object
74/******/ 	// mode & 1: value is a module id, require it
75/******/ 	// mode & 2: merge all properties of value into the ns
76/******/ 	// mode & 4: return value when already ns object
77/******/ 	// mode & 8|1: behave like require
78/******/ 	__webpack_require__.t = function(value, mode) {
79/******/ 		if(mode & 1) value = __webpack_require__(value);
80/******/ 		if(mode & 8) return value;
81/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
82/******/ 		var ns = Object.create(null);
83/******/ 		__webpack_require__.r(ns);
84/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
85/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
86/******/ 		return ns;
87/******/ 	};
88/******/
89/******/ 	// getDefaultExport function for compatibility with non-harmony modules
90/******/ 	__webpack_require__.n = function(module) {
91/******/ 		var getter = module && module.__esModule ?
92/******/ 			function getDefault() { return module['default']; } :
93/******/ 			function getModuleExports() { return module; };
94/******/ 		__webpack_require__.d(getter, 'a', getter);
95/******/ 		return getter;
96/******/ 	};
97/******/
98/******/ 	// Object.prototype.hasOwnProperty.call
99/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
100/******/
101/******/ 	// __webpack_public_path__
102/******/ 	__webpack_require__.p = "/codebase/sources/";
103/******/
104/******/
105/******/ 	// Load entry module and return exports
106/******/ 	return __webpack_require__(__webpack_require__.s = "./sources/ext/undo/index.ts");
107/******/ })
108/************************************************************************/
109/******/ ({
110
111/***/ "./sources/ext/undo/index.ts":
112/*!***********************************!*\
113  !*** ./sources/ext/undo/index.ts ***!
114  \***********************************/
115/*! no static exports found */
116/***/ (function(module, exports, __webpack_require__) {
117
118"use strict";
119
120Object.defineProperty(exports, "__esModule", { value: true });
121var monitor_1 = __webpack_require__(/*! ./monitor */ "./sources/ext/undo/monitor.ts");
122var undo_1 = __webpack_require__(/*! ./undo */ "./sources/ext/undo/undo.ts");
123var _undo = new undo_1.Undo();
124var monitor = new monitor_1.Monitor(_undo);
125gantt.config.undo = true;
126gantt.config.redo = true;
127/**
128 * entities that require different processing for undoing-redoing changes
129 * @type {{link: string, task: string}}
130 */
131gantt.config.undo_types = {
132    link: "link",
133    task: "task"
134};
135/**
136 * types of traced actions
137 * @type {{update: string, remove: string, add: string}}
138 */
139gantt.config.undo_actions = {
140    update: "update",
141    remove: "remove",
142    add: "add",
143    move: "move" // move task in grid
144};
145if (!gantt.ext) {
146    gantt.ext = {};
147}
148gantt.ext.undo = {
149    undo: function () { return _undo.undo(); },
150    redo: function () { return _undo.redo(); },
151    getUndoStack: function () { return _undo.getUndoStack(); },
152    getRedoStack: function () { return _undo.getRedoStack(); },
153    clearUndoStack: function () { return _undo.clearUndoStack(); },
154    clearRedoStack: function () { return _undo.clearRedoStack(); },
155    saveState: function (id, type) { return monitor.store(id, type, true); }
156};
157gantt.undo = gantt.ext.undo.undo;
158gantt.redo = gantt.ext.undo.redo;
159gantt.getUndoStack = gantt.ext.undo.getUndoStack;
160gantt.getRedoStack = gantt.ext.undo.getRedoStack;
161gantt.clearUndoStack = gantt.ext.undo.clearUndoStack;
162gantt.clearRedoStack = gantt.ext.undo.clearRedoStack;
163function updTask(task, oldId, newId) {
164    if (!task) {
165        return;
166    }
167    if (task.id === oldId) {
168        task.id = newId;
169    }
170    if (task.parent === oldId) {
171        task.parent = newId;
172    }
173}
174function changeTaskCommandId(command, oldId, newId) {
175    updTask(command.value, oldId, newId);
176    updTask(command.oldValue, oldId, newId);
177}
178function updLink(link, oldTaskId, newTaskId) {
179    if (!link) {
180        return;
181    }
182    if (link.source === oldTaskId) {
183        link.source = newTaskId;
184    }
185    if (link.target === oldTaskId) {
186        link.target = newTaskId;
187    }
188}
189function changeLinkCommandId(command, oldId, newId) {
190    updLink(command.value, oldId, newId);
191    updLink(command.oldValue, oldId, newId);
192}
193function updateTasksIds(log, oldId, newId) {
194    var undo = _undo;
195    for (var i = 0; i < log.length; i++) {
196        var entry = log[i];
197        for (var j = 0; j < entry.commands.length; j++) {
198            if (entry.commands[j].entity === undo.command.entity.task) {
199                changeTaskCommandId(entry.commands[j], oldId, newId);
200            }
201            else if (entry.commands[j].entity === undo.command.entity.link) {
202                changeLinkCommandId(entry.commands[j], oldId, newId);
203            }
204        }
205    }
206}
207function updateLinksIds(log, oldId, newId) {
208    var undo = _undo;
209    for (var i = 0; i < log.length; i++) {
210        var entry = log[i];
211        for (var j = 0; j < entry.commands.length; j++) {
212            var command = entry.commands[j];
213            if (command.entity === undo.command.entity.link) {
214                if (command.value && command.value.id === oldId) {
215                    command.value.id = newId;
216                }
217                if (command.oldValue && command.oldValue.id === oldId) {
218                    command.oldValue.id = newId;
219                }
220            }
221        }
222    }
223}
224gantt.attachEvent("onTaskIdChange", function (oldId, newId) {
225    var undo = _undo;
226    updateTasksIds(undo.getUndoStack(), oldId, newId);
227    updateTasksIds(undo.getRedoStack(), oldId, newId);
228});
229gantt.attachEvent("onLinkIdChange", function (oldId, newId) {
230    var undo = _undo;
231    updateLinksIds(undo.getUndoStack(), oldId, newId);
232    updateLinksIds(undo.getRedoStack(), oldId, newId);
233});
234gantt.attachEvent("onGanttReady", function () {
235    _undo.updateConfigs();
236});
237
238
239/***/ }),
240
241/***/ "./sources/ext/undo/monitor.ts":
242/*!*************************************!*\
243  !*** ./sources/ext/undo/monitor.ts ***!
244  \*************************************/
245/*! no static exports found */
246/***/ (function(module, exports, __webpack_require__) {
247
248"use strict";
249
250Object.defineProperty(exports, "__esModule", { value: true });
251var noTrack = {
252    onBeforeUndo: "onAfterUndo",
253    onBeforeRedo: "onAfterRedo"
254};
255var batchActions = [
256    "onTaskDragStart",
257    "onAfterTaskUpdate",
258    "onAfterTaskDelete",
259    "onBeforeBatchUpdate"
260];
261var Monitor = /** @class */ (function () {
262    function Monitor(undo) {
263        this._batchAction = null;
264        this._batchMode = false;
265        this._ignore = false;
266        this._ignoreMoveEvents = false;
267        this._initialTasks = {};
268        this._initialLinks = {};
269        this._nestedTasks = {};
270        this._nestedLinks = {};
271        this._undo = undo;
272        this._attachEvents();
273    }
274    Monitor.prototype.store = function (id, type, overwrite) {
275        if (overwrite === void 0) { overwrite = false; }
276        if (type === gantt.config.undo_types.task) {
277            return this._storeTask(id, overwrite);
278        }
279        if (type === gantt.config.undo_types.link) {
280            return this._storeLink(id, overwrite);
281        }
282        return false;
283    };
284    Monitor.prototype.isMoveEventsIgnored = function () {
285        return this._ignoreMoveEvents;
286    };
287    Monitor.prototype.toggleIgnoreMoveEvents = function (newValue) {
288        this._ignoreMoveEvents = newValue || false;
289    };
290    Monitor.prototype.startIgnore = function () {
291        this._ignore = true;
292    };
293    Monitor.prototype.stopIgnore = function () {
294        this._ignore = false;
295    };
296    Monitor.prototype.startBatchAction = function () {
297        var _this = this;
298        // try catching updates made from event handlers using timeout
299        if (this._timeout) {
300            clearTimeout(this._timeout);
301        }
302        this._timeout = setTimeout(function () {
303            _this.stopBatchAction();
304        }, 10);
305        if (this._ignore || this._batchMode) {
306            return;
307        }
308        this._batchMode = true;
309        this._batchAction = this._undo.action.create();
310    };
311    Monitor.prototype.stopBatchAction = function () {
312        if (this._ignore) {
313            return;
314        }
315        var undo = this._undo;
316        if (this._batchAction) {
317            undo.logAction(this._batchAction);
318        }
319        this._batchMode = false;
320        this._batchAction = null;
321    };
322    Monitor.prototype.onTaskAdded = function (task) {
323        if (!this._ignore) {
324            this._storeTaskCommand(task, this._undo.command.type.add);
325        }
326    };
327    Monitor.prototype.onTaskUpdated = function (task) {
328        if (!this._ignore) {
329            this._storeTaskCommand(task, this._undo.command.type.update);
330        }
331    };
332    Monitor.prototype.onTaskMoved = function (task) {
333        if (!this._ignore) {
334            this._storeEntityCommand(task, this.getInitialTask(task.id), this._undo.command.type.move, this._undo.command.entity.task);
335        }
336    };
337    Monitor.prototype.onTaskDeleted = function (task) {
338        if (!this._ignore) {
339            this._storeTaskCommand(task, this._undo.command.type.remove);
340            if (this._nestedTasks[task.id]) {
341                var children = this._nestedTasks[task.id];
342                for (var i = 0; i < children.length; i++) {
343                    this._storeTaskCommand(children[i], this._undo.command.type.remove);
344                }
345            }
346            if (this._nestedLinks[task.id]) {
347                var childrenLinks = this._nestedLinks[task.id];
348                for (var i = 0; i < childrenLinks.length; i++) {
349                    this._storeLinkCommand(childrenLinks[i], this._undo.command.type.remove);
350                }
351            }
352        }
353    };
354    Monitor.prototype.onLinkAdded = function (link) {
355        if (!this._ignore) {
356            this._storeLinkCommand(link, this._undo.command.type.add);
357        }
358    };
359    Monitor.prototype.onLinkUpdated = function (link) {
360        if (!this._ignore) {
361            this._storeLinkCommand(link, this._undo.command.type.update);
362        }
363    };
364    Monitor.prototype.onLinkDeleted = function (link) {
365        if (!this._ignore) {
366            this._storeLinkCommand(link, this._undo.command.type.remove);
367        }
368    };
369    Monitor.prototype.setNestedTasks = function (id, taskIds) {
370        var task = null;
371        var tasks = [];
372        var linkIds = this._getLinks(gantt.getTask(id));
373        for (var i = 0; i < taskIds.length; i++) {
374            task = this.setInitialTask(taskIds[i]);
375            linkIds = linkIds.concat(this._getLinks(task));
376            tasks.push(task);
377        }
378        var uniqueLinks = {};
379        for (var i = 0; i < linkIds.length; i++) {
380            uniqueLinks[linkIds[i]] = true;
381        }
382        var links = [];
383        for (var i in uniqueLinks) {
384            links.push(this.setInitialLink(i));
385        }
386        this._nestedTasks[id] = tasks;
387        this._nestedLinks[id] = links;
388    };
389    Monitor.prototype.setInitialTask = function (id, overwrite) {
390        if (overwrite || (!this._initialTasks[id] || !this._batchMode)) {
391            var task = gantt.copy(gantt.getTask(id));
392            task.$index = gantt.getTaskIndex(id);
393            this.setInitialTaskObject(id, task);
394        }
395        return this._initialTasks[id];
396    };
397    Monitor.prototype.getInitialTask = function (id) {
398        return this._initialTasks[id];
399    };
400    Monitor.prototype.clearInitialTasks = function () {
401        this._initialTasks = {};
402    };
403    Monitor.prototype.setInitialTaskObject = function (id, object) {
404        this._initialTasks[id] = object;
405    };
406    Monitor.prototype.setInitialLink = function (id, overwrite) {
407        if (!this._initialLinks[id] || !this._batchMode) {
408            this._initialLinks[id] = gantt.copy(gantt.getLink(id));
409        }
410        return this._initialLinks[id];
411    };
412    Monitor.prototype.getInitialLink = function (id) {
413        return this._initialLinks[id];
414    };
415    Monitor.prototype.clearInitialLinks = function () {
416        this._initialLinks = {};
417    };
418    Monitor.prototype._attachEvents = function () {
419        var _this = this;
420        var deleteCacheCooldown = null;
421        var saveInitialAll = function () {
422            if (!deleteCacheCooldown) {
423                deleteCacheCooldown = setTimeout(function () {
424                    deleteCacheCooldown = null;
425                });
426                _this.clearInitialTasks();
427                gantt.eachTask(function (task) {
428                    _this.setInitialTask(task.id);
429                });
430                _this.clearInitialLinks();
431                gantt.getLinks().forEach(function (link) {
432                    _this.setInitialLink(link.id);
433                });
434            }
435        };
436        var getMoveObjectByTaskId = function (id) {
437            return gantt.copy(gantt.getTask(id));
438        };
439        for (var i in noTrack) {
440            gantt.attachEvent(i, function () {
441                _this.startIgnore();
442                return true;
443            });
444            gantt.attachEvent(noTrack[i], function () {
445                _this.stopIgnore();
446                return true;
447            });
448        }
449        for (var i = 0; i < batchActions.length; i++) {
450            gantt.attachEvent(batchActions[i], function () {
451                _this.startBatchAction();
452                return true;
453            });
454        }
455        gantt.attachEvent("onParse", function () {
456            _this._undo.clearUndoStack();
457            _this._undo.clearRedoStack();
458            saveInitialAll();
459        });
460        gantt.attachEvent("onAfterTaskAdd", function (id, task) {
461            _this.setInitialTask(id, true);
462            _this.onTaskAdded(task);
463        });
464        gantt.attachEvent("onAfterTaskUpdate", function (id, task) {
465            _this.onTaskUpdated(task);
466        });
467        gantt.attachEvent("onAfterTaskDelete", function (id, task) {
468            _this.onTaskDeleted(task);
469        });
470        gantt.attachEvent("onAfterLinkAdd", function (id, link) {
471            _this.setInitialLink(id, true);
472            _this.onLinkAdded(link);
473        });
474        gantt.attachEvent("onAfterLinkUpdate", function (id, link) {
475            _this.onLinkUpdated(link);
476        });
477        gantt.attachEvent("onAfterLinkDelete", function (id, link) {
478            _this.onLinkDeleted(link);
479        });
480        gantt.attachEvent("onRowDragEnd", function (id, target) {
481            _this.onTaskMoved(getMoveObjectByTaskId(id));
482            _this.toggleIgnoreMoveEvents();
483            return true;
484        });
485        gantt.attachEvent("onBeforeTaskDelete", function (id) {
486            _this.store(id, gantt.config.undo_types.task);
487            var nested = [];
488            // remember task indexes in case their being deleted in a loop, so they could be restored in the correct order
489            saveInitialAll();
490            gantt.eachTask(function (task) {
491                nested.push(task.id);
492            }, id);
493            _this.setNestedTasks(id, nested);
494            return true;
495        });
496        var datastore = gantt.getDatastore("task");
497        datastore.attachEvent("onBeforeItemMove", function (id, parent, tindex) {
498            if (!_this.isMoveEventsIgnored()) {
499                saveInitialAll();
500            }
501            return true;
502        });
503        datastore.attachEvent("onAfterItemMove", function (id, parent, tindex) {
504            if (!_this.isMoveEventsIgnored()) {
505                _this.onTaskMoved(getMoveObjectByTaskId(id));
506            }
507            return true;
508        });
509        gantt.attachEvent("onRowDragStart", function (id, target, e) {
510            _this.toggleIgnoreMoveEvents(true);
511            saveInitialAll();
512            return true;
513        });
514        gantt.attachEvent("onBeforeTaskDrag", function (taskId) { return _this.store(taskId, gantt.config.undo_types.task); });
515        gantt.attachEvent("onLightbox", function (taskId) { return _this.store(taskId, gantt.config.undo_types.task); });
516        gantt.attachEvent("onBeforeTaskAutoSchedule", function (task) {
517            _this.store(task.id, gantt.config.undo_types.task);
518            return true;
519        });
520        if (gantt.ext.inlineEditors) {
521            gantt.ext.inlineEditors.attachEvent("onEditStart", function (state) {
522                _this.store(state.id, gantt.config.undo_types.task);
523            });
524        }
525    };
526    Monitor.prototype._storeCommand = function (command) {
527        var undo = this._undo;
528        undo.updateConfigs();
529        if (!undo.undoEnabled) {
530            return;
531        }
532        if (this._batchMode) {
533            this._batchAction.commands.push(command);
534        }
535        else {
536            var action = undo.action.create([command]);
537            undo.logAction(action);
538        }
539    };
540    Monitor.prototype._storeEntityCommand = function (obj, old, actionType, entityType) {
541        var undo = this._undo;
542        var command = undo.command.create(obj, old, actionType, entityType);
543        this._storeCommand(command);
544    };
545    Monitor.prototype._storeTaskCommand = function (obj, type) {
546        this._storeEntityCommand(obj, this.getInitialTask(obj.id), type, this._undo.command.entity.task);
547    };
548    Monitor.prototype._storeLinkCommand = function (obj, type) {
549        this._storeEntityCommand(obj, this.getInitialLink(obj.id), type, this._undo.command.entity.link);
550    };
551    Monitor.prototype._getLinks = function (task) {
552        return task.$source.concat(task.$target);
553    };
554    Monitor.prototype._storeTask = function (taskId, overwrite) {
555        var _this = this;
556        if (overwrite === void 0) { overwrite = false; }
557        this.setInitialTask(taskId, overwrite);
558        gantt.eachTask(function (child) {
559            _this.setInitialTask(child.id);
560        }, taskId);
561        return true;
562    };
563    Monitor.prototype._storeLink = function (linkId, overwrite) {
564        if (overwrite === void 0) { overwrite = false; }
565        this.setInitialLink(linkId, overwrite);
566        return true;
567    };
568    return Monitor;
569}());
570exports.Monitor = Monitor;
571
572
573/***/ }),
574
575/***/ "./sources/ext/undo/undo.ts":
576/*!**********************************!*\
577  !*** ./sources/ext/undo/undo.ts ***!
578  \**********************************/
579/*! no static exports found */
580/***/ (function(module, exports, __webpack_require__) {
581
582"use strict";
583
584Object.defineProperty(exports, "__esModule", { value: true });
585var MAX_UNDO_STEPS = 10;
586var Undo = /** @class */ (function () {
587    function Undo() {
588        var _this = this;
589        this.maxSteps = MAX_UNDO_STEPS;
590        this.undoEnabled = true;
591        this.redoEnabled = true;
592        this.action = {
593            create: function (commands) {
594                return { commands: (commands ? commands.slice() : []) };
595            },
596            invert: function (action) {
597                var _a;
598                var revert = gantt.copy(action);
599                var commands = _this.command;
600                for (var i = 0; i < action.commands.length; i++) {
601                    var command = revert.commands[i] = commands.invert(revert.commands[i]);
602                    if (command.type === commands.type.update || command.type === commands.type.move) {
603                        _a = [command.oldValue, command.value], command.value = _a[0], command.oldValue = _a[1];
604                    }
605                }
606                return revert;
607            }
608        };
609        this.command = {
610            // entities that require different processing for undoing-redoing changes (gantt.config.undo_types)
611            entity: null,
612            // types of traced actions (gantt.config.undo_actions)
613            type: null,
614            create: function (value, oldValue, type, entity) {
615                return {
616                    entity: entity,
617                    type: type,
618                    value: gantt.copy(value),
619                    oldValue: gantt.copy(oldValue || value)
620                };
621            },
622            invert: function (command) {
623                var revert = gantt.copy(command);
624                revert.type = this.inverseCommands(command.type);
625                return revert;
626            },
627            inverseCommands: function (command) {
628                switch (command) {
629                    case this.type.update:
630                        return this.type.update;
631                    case this.type.remove:
632                        return this.type.add;
633                    case this.type.add:
634                        return this.type.remove;
635                    case this.type.move:
636                        return this.type.move;
637                    default:
638                        gantt.assert(false, "Invalid command " + command);
639                        return null;
640                }
641            }
642        };
643        this._undoStack = [];
644        this._redoStack = [];
645    }
646    Undo.prototype.getUndoStack = function () {
647        return this._undoStack;
648    };
649    Undo.prototype.getRedoStack = function () {
650        return this._redoStack;
651    };
652    Undo.prototype.clearUndoStack = function () {
653        this._undoStack = [];
654    };
655    Undo.prototype.clearRedoStack = function () {
656        this._redoStack = [];
657    };
658    Undo.prototype.updateConfigs = function () {
659        this.maxSteps = gantt.config.undo_steps || MAX_UNDO_STEPS;
660        this.command.entity = gantt.config.undo_types;
661        this.command.type = gantt.config.undo_actions;
662        this.undoEnabled = !!gantt.config.undo;
663        this.redoEnabled = (!!gantt.config.undo) && (!!gantt.config.redo);
664    };
665    Undo.prototype.undo = function () {
666        this.updateConfigs();
667        if (!this.undoEnabled) {
668            return;
669        }
670        var action = this._pop(this._undoStack);
671        if (action) {
672            this._reorderCommands(action);
673        }
674        if (gantt.callEvent("onBeforeUndo", [action]) !== false) {
675            if (action) {
676                this._applyAction(this.action.invert(action));
677                this._push(this._redoStack, gantt.copy(action));
678                gantt.callEvent("onAfterUndo", [action]);
679                return;
680            }
681        }
682        gantt.callEvent("onAfterUndo", [null]);
683    };
684    Undo.prototype.redo = function () {
685        this.updateConfigs();
686        if (!this.redoEnabled) {
687            return;
688        }
689        var action = this._pop(this._redoStack);
690        if (action) {
691            this._reorderCommands(action);
692        }
693        if (gantt.callEvent("onBeforeRedo", [action]) !== false) {
694            if (action) {
695                this._applyAction(action);
696                this._push(this._undoStack, gantt.copy(action));
697                gantt.callEvent("onAfterRedo", [action]);
698                return;
699            }
700        }
701        gantt.callEvent("onAfterRedo", [null]);
702    };
703    // storeUndo:
704    Undo.prototype.logAction = function (action) {
705        this._push(this._undoStack, action);
706        this._redoStack = [];
707    };
708    Undo.prototype._push = function (stack, action) {
709        if (!action.commands.length) {
710            return;
711        }
712        var event = stack === this._undoStack ? "onBeforeUndoStack" : "onBeforeRedoStack";
713        if (gantt.callEvent(event, [action]) === false) {
714            return;
715        }
716        // commands can be removed from event handler
717        if (!action.commands.length) {
718            return;
719        }
720        stack.push(action);
721        while (stack.length > this.maxSteps) {
722            stack.shift();
723        }
724        return action;
725    };
726    Undo.prototype._pop = function (stack) {
727        return stack.pop();
728    };
729    Undo.prototype._reorderCommands = function (action) {
730        // firstly process tasks and only then links
731        // in order to ensure links are added not earlier than their tasks
732        // firstly to 'move' actions and only then updates
733        var weights = { any: 0, link: 1, task: 2 };
734        var actionWeights = { move: 1, any: 0 };
735        action.commands.sort(function (a, b) {
736            if (a.entity === "task" && b.entity === "task") {
737                if (a.type !== b.type) {
738                    return (actionWeights[b.type] || 0) - (actionWeights[a.type] || 0);
739                }
740                else if (a.type === "move" && a.oldValue && b.oldValue && b.oldValue.parent === a.oldValue.parent) {
741                    return a.oldValue.$index - b.oldValue.$index;
742                }
743                else {
744                    return 0;
745                }
746            }
747            else {
748                var weightA = weights[a.entity] || weights.any;
749                var weightB = weights[b.entity] || weights.any;
750                return weightB - weightA;
751            }
752        });
753    };
754    Undo.prototype._applyAction = function (action) {
755        var command = null;
756        var entities = this.command.entity;
757        var actions = this.command.type;
758        var methods = {};
759        methods[entities.task] = {
760            add: "addTask",
761            get: "getTask",
762            update: "updateTask",
763            remove: "deleteTask",
764            move: "moveTask",
765            isExists: "isTaskExists"
766        };
767        methods[entities.link] = {
768            add: "addLink",
769            get: "getLink",
770            update: "updateLink",
771            remove: "deleteLink",
772            isExists: "isLinkExists"
773        };
774        gantt.batchUpdate(function () {
775            for (var i = 0; i < action.commands.length; i++) {
776                command = action.commands[i];
777                var method = methods[command.entity][command.type];
778                var getMethod = methods[command.entity].get;
779                var check = methods[command.entity].isExists;
780                if (command.type === actions.add) {
781                    gantt[method](command.oldValue, command.oldValue.parent, command.oldValue.$index);
782                }
783                else if (command.type === actions.remove) {
784                    if (gantt[check](command.value.id)) {
785                        gantt[method](command.value.id);
786                    }
787                }
788                else if (command.type === actions.update) {
789                    var item = gantt[getMethod](command.value.id);
790                    for (var prop in command.value) {
791                        if (!prop.startsWith("$") && !prop.startsWith("_")) {
792                            item[prop] = command.value[prop];
793                        }
794                    }
795                    gantt[method](command.value.id);
796                }
797                else if (command.type === actions.move) {
798                    gantt[method](command.value.id, command.value.$index, command.value.parent);
799                }
800            }
801        });
802    };
803    return Undo;
804}());
805exports.Undo = Undo;
806
807
808/***/ })
809
810/******/ });
811});