all files / actions/ validator.js

98.88% Statements 266/269
89.77% Branches 79/88
95.83% Functions 23/24
98.88% Lines 264/267
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   1477× 1477× 1477× 1477× 1477× 1477× 1477× 1477× 1477×   1616× 1616× 1477×   1616× 1616× 1472× 41390× 41390× 36186×   41390× 41390× 41390× 36138×   41390× 41390× 35956×   41390× 35956× 35956× 10437×   35956×         26615× 13398×   13217× 13217× 13217× 13217× 16747× 16747× 16747× 15515×     1232× 3530× 3530×       13217× 13217×   13287× 13287× 13287× 13287× 13287× 13287× 26749× 26749× 57880× 57880×     13287×   1726× 1726× 1726× 1726× 1726× 1726× 1726× 42300× 42300× 42300×     1726× 1726× 1726× 42300× 42300× 42300× 42300× 13239× 13239× 13239× 57658× 57658× 15×   57658× 57658×     42300× 42300×   1726× 1726× 1708×         48× 48× 48× 48× 382× 382× 382× 382× 382×   48×   48× 48× 48× 222× 222×   222×   48×   48× 48× 48×         1726× 1726× 1726× 43× 43× 41×     1726×   24×     24× 17×     18× 18×   18× 18× 41× 41× 41× 41× 41× 41× 41× 41× 41× 41× 41× 41× 24× 24× 24× 24× 24×     24× 24× 24× 24× 24× 24×     24×     17×     41×   41×   41× 18× 18×   1774× 1774× 1774× 1774× 1774× 1774× 42699× 42699×   1774× 1774× 1774× 42705× 42705× 42705× 42705× 42699× 42699× 14979× 14979× 14979× 10598×   4381× 51× 51× 112× 51× 51×     51× 51× 51× 140×   51×         42705× 42705×   1774× 1774× 42699× 42699× 32107×     1774× 1774× 1774× 51× 51×     51× 51× 51× 89× 18×     51× 51× 51× 51× 51×     1774×   1708× 1708× 1708× 42020×   1708× 1708× 1708× 42020× 42020× 42020× 42020× 42020× 14421× 14421× 14421× 14421×   42020×   1708× 1708× 42022× 31680×     1708× 1708× 42020× 42020× 42020× 42020× 42018× 42018× 42018× 14419× 14419× 14419× 14419× 10340×   14419×       1708×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var cyclicValidator = (function () {
        function cyclicValidator(gantt, flatDataCollection) {
            this.idToItem = new Map();
            this.parentToChildren = new Map();
            this.leafCache = new Map();
            this.adjacency = new Map();
            this.resolvedEdgesPerTask = new Map();
            this.cycles = [];
            this.topoOrder = null;
            this.parent = gantt;
            this.buildMaps(flatDataCollection);
        }
        cyclicValidator.prototype.buildMaps = function (flatDataCollection) {
            this.idToItem.clear();
            if (flatDataCollection) {
                flatDataCollection.clear();
            }
            this.parentToChildren.clear();
            if (this.parent.viewType === 'ProjectView') {
                for (var _i = 0, _a = this.parent.flatData; _i < _a.length; _i++) {
                    var item = _a[_i];
                    if (flatDataCollection) {
                        flatDataCollection.set(item.ganttProperties.rowUniqueID.toString(), item);
                    }
                    var id = String(item.ganttProperties.taskId);
                    this.idToItem.set(id, item);
                    if (!this.adjacency.has(id)) {
                        this.adjacency.set(id, new Set());
                    }
                    var parent_1 = void 0;
                    if (item.parentItem && item.parentItem.taskId) {
                        parent_1 = item.parentItem.taskId;
                    }
                    if (parent_1 !== undefined && parent_1 !== null && parent_1 !== '') {
                        var pid = (parent_1);
                        if (!this.parentToChildren.has(pid)) {
                            this.parentToChildren.set(pid, []);
                        }
                        this.parentToChildren.get(pid).push(id);
                    }
                }
            }
        };
        cyclicValidator.prototype.getLeafDescendants = function (nodeId) {
            if (this.leafCache.has(nodeId)) {
                return this.leafCache.get(nodeId);
            }
            var result = [];
            result.push(nodeId);
            var stack = [nodeId];
            while (stack.length) {
                var cur = stack.pop();
                var children = this.parentToChildren.get(cur);
                if (!children || children.length === 0) {
                    result.push(cur);
                }
                else {
                    for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
                        var c = children_1[_i];
                        stack.push(c);
                    }
                }
            }
            this.leafCache.set(nodeId, result);
            return result;
        };
        cyclicValidator.prototype.resolveOnePred = function (p) {
            var fromId = (p.from);
            var toId = (p.to);
            var fromLeaves = this.getLeafDescendants(fromId);
            var toLeaves = this.getLeafDescendants(toId);
            var edges = [];
            for (var _i = 0, fromLeaves_1 = fromLeaves; _i < fromLeaves_1.length; _i++) {
                var f = fromLeaves_1[_i];
                for (var _a = 0, toLeaves_1 = toLeaves; _a < toLeaves_1.length; _a++) {
                    var t = toLeaves_1[_a];
                    edges.push({ fromLeaf: f, toLeaf: t, source: p });
                }
            }
            return edges;
        };
        cyclicValidator.prototype.resolve = function () {
            this.adjacency.clear();
            this.resolvedEdgesPerTask.clear();
            this.leafCache.clear();
            this.cycles = [];
            this.topoOrder = null;
            var keys = Array.from(this.idToItem.keys());
            for (var i = 0; i < keys.length; i++) {
                var id = keys[i];
                Eif (!this.adjacency.has(id)) {
                    this.adjacency.set(id, new Set());
                }
            }
            var iterator = this.idToItem.entries();
            var entry = iterator.next();
            while (!entry.done) {
                var _a = entry.value, id = _a[0], item = _a[1];
                var preds = item.ganttProperties.predecessor || [];
                var resolvedList = [];
                for (var i = 0; i < preds.length; i++) {
                    var p = preds[i];
                    var edges = this.resolveOnePred(p);
                    for (var j = 0; j < edges.length; j++) {
                        var e = edges[j];
                        if (!this.adjacency.has(e.fromLeaf)) {
                            this.adjacency.set(e.fromLeaf, new Set());
                        }
                        this.adjacency.get(e.fromLeaf).add(e.toLeaf);
                        resolvedList.push(e);
                    }
                }
                this.resolvedEdgesPerTask.set(id, resolvedList);
                entry = iterator.next();
            }
            this.cycles = this.detectAllCycles();
            if (this.cycles.length === 0) {
                this.topoOrder = this.computeTopologicalOrder();
            }
        };
        cyclicValidator.prototype.getResolvedPredecessorsForTask = function (taskId) {
            return this.resolvedEdgesPerTask.get(String(taskId)) || [];
        };
        cyclicValidator.prototype.cloneAdjacency = function () {
            var m = new Map();
            var entries = this.adjacency.entries();
            var entryResult = entries.next();
            while (!entryResult.done) {
                var pair = entryResult.value;
                var k = pair[0];
                var set = pair[1];
                m.set(k, new Set(Array.from(set)));
                entryResult = entries.next();
            }
            return m;
        };
        cyclicValidator.prototype.addPredToAdjacencyClone = function (pred) {
            var clone = this.cloneAdjacency();
            var edges = this.resolveOnePred(pred);
            for (var _i = 0, edges_1 = edges; _i < edges_1.length; _i++) {
                var e = edges_1[_i];
                if (clone && !clone.has(e.fromLeaf)) {
                    clone.set(e.fromLeaf, new Set());
                }
                clone.get(e.fromLeaf).add(e.toLeaf);
            }
            return clone;
        };
        cyclicValidator.prototype.wouldCreateCycleWhenAdding = function (pred) {
            var adj = this.addPredToAdjacencyClone(pred);
            var cycles = this.detectCyclesStatic(adj);
            return {
                wouldCreate: cycles.length > 0,
                cycles: cycles
            };
        };
        cyclicValidator.prototype.detectAllCycles = function () {
            var cycles = this.detectCyclesStatic(this.adjacency);
            var result = [];
            for (var _i = 0, cycles_1 = cycles; _i < cycles_1.length; _i++) {
                var cycle = cycles_1[_i];
                if (cycle[0] && cycle[1]) {
                    result.push({ fromId: cycle[0], toId: cycle[1] });
                }
            }
            return result;
        };
        cyclicValidator.prototype.removePredecessor = function (predecessorString, toRemove) {
            Iif (!predecessorString) {
                return '';
            }
            if (predecessorString === toRemove) {
                return '';
            }
            var parts = predecessorString
                .split(',')
                .map(function (s) { return s.trim(); })
                .filter(function (part) { return part !== toRemove && part !== toRemove.trim(); });
            return parts.join(',');
        };
        cyclicValidator.prototype.getCyclesWithDetails = function (cycles) {
            var _this = this;
            var parts = cycles.map(function (cycle) {
                var fromId = cycle['fromId'];
                var toId = cycle['toId'];
                var toTask = _this.idToItem.get(toId);
                var fromTask = _this.idToItem.get(fromId);
                var toPredecessors = toTask.ganttProperties.predecessor;
                var errorLabel = "Task ID " + fromId + " to Task ID " + toId;
                var newPred = [];
                Eif (toPredecessors) {
                    for (var i = 0; i < toPredecessors.length; i++) {
                        var pred = toPredecessors[i];
                        var leaves = _this.getLeafDescendants(pred.from);
                        if (pred.from === fromId || (leaves && leaves.indexOf(fromId) !== -1)) {
                            errorLabel = "Task ID " + pred.from + " to Task ID " + toId;
                            var predType = pred.from + pred.type;
                            predType = _this.parent.predecessorModule['generatePredecessorValue'](pred, predType);
                            var dependencyField = _this.parent.taskFields.dependency;
                            toTask.taskData[dependencyField] = toTask[dependencyField] =
                                toTask.ganttProperties.predecessorsName =
                                    _this.removePredecessor(toTask.ganttProperties.predecessorsName, predType);
                            var removedString = predType.split(',');
                            removedString.forEach(function (str) {
                                var id = str.replace(/(FS|SS|SF|FF).*$/, '');
                                var targetTask = _this.idToItem.get(id);
                                if (targetTask.ganttProperties.predecessor) {
                                    targetTask.ganttProperties.predecessor = targetTask.ganttProperties.predecessor.filter(function (pred) { return pred.to.toString() !== toTask.ganttProperties.taskId.toString(); });
                                }
                            });
                            continue;
                        }
                        else {
                            newPred.push(pred);
                        }
                    }
                    toTask.ganttProperties.predecessor = newPred;
                }
                return errorLabel;
            });
            var uniqueParts = parts.filter(function (value, index, self) { return self.indexOf(value) === index; });
            var list = uniqueParts.join(', ');
            return "Cyclic dependency is detected for the tasks from " + list + ". Please provide valid dependency";
        };
        cyclicValidator.prototype.detectCyclesStatic = function (adj) {
            var WHITE = 0;
            var GRAY = 1;
            var BLACK = 2;
            var state = new Map();
            var keysInit = Array.from(adj.keys());
            for (var i = 0; i < keysInit.length; i++) {
                var k = keysInit[i];
                state.set(k, WHITE);
            }
            var stack = [];
            var cycles = [];
            var dfs = function (node) {
                state.set(node, GRAY);
                stack.push(node);
                var neighbors = adj.get(node);
                if (neighbors) {
                    var neighborArray = Array.from(neighbors);
                    for (var i = 0; i < neighborArray.length; i++) {
                        var nb = neighborArray[i];
                        var s = state.has(nb) ? state.get(nb) : WHITE;
                        if (s === WHITE) {
                            dfs(nb);
                        }
                        else if (s === GRAY) {
                            var idx = -1;
                            for (var j = 0; j < stack.length; j++) {
                                if (stack[j] === nb) {
                                    idx = j;
                                    break;
                                }
                            }
                            Eif (idx !== -1) {
                                var cyclePath = [];
                                for (var k = idx; k < stack.length; k++) {
                                    cyclePath.push(stack[k]);
                                }
                                cycles.push(cyclePath);
                            }
                        }
                    }
                }
                stack.pop();
                state.set(node, BLACK);
            };
            var keys = Array.from(adj.keys());
            for (var i = 0; i < keys.length; i++) {
                var node = keys[i];
                if (state.get(node) === WHITE) {
                    dfs(node);
                }
            }
            var unique = [];
            var seen = new Set();
            for (var _i = 0, cycles_2 = cycles; _i < cycles_2.length; _i++) {
                var c = cycles_2[_i];
                Iif (c.length === 0) {
                    continue;
                }
                var nodes = c.slice();
                var minIdx = 0;
                for (var i = 1; i < nodes.length; i++) {
                    if (nodes[i] < nodes[minIdx]) {
                        minIdx = i;
                    }
                }
                var rot = nodes.slice(minIdx).concat(nodes.slice(0, minIdx));
                var key = rot.join('->');
                Eif (!seen.has(key)) {
                    seen.add(key);
                    unique.push(rot);
                }
            }
            return unique;
        };
        cyclicValidator.prototype.computeTopologicalOrder = function () {
            var indeg = new Map();
            var keys = Array.from(this.adjacency.keys());
            for (var i = 0; i < keys.length; i++) {
                indeg.set(keys[i], 0);
            }
            var entries = this.adjacency.entries();
            var entryResult = entries.next();
            while (!entryResult.done) {
                var pair = entryResult.value;
                var successors = pair[1];
                var successorIter = successors.values();
                var successorResult = successorIter.next();
                while (!successorResult.done) {
                    var to = successorResult.value;
                    var current = indeg.has(to) ? indeg.get(to) : 0;
                    indeg.set(to, current + 1);
                    successorResult = successorIter.next();
                }
                entryResult = entries.next();
            }
            var q = [];
            indeg.forEach(function (degree, taskId) {
                if (degree === 0) {
                    q.push(taskId);
                }
            });
            var order = [];
            while (q.length) {
                var n = q.shift();
                order.push(n);
                var neighbors = this.adjacency.get(n);
                if (neighbors) {
                    var iterator = neighbors.values();
                    var iterNext = iterator.next();
                    while (!iterNext.done) {
                        var nb = iterNext.value;
                        var currentDegree = indeg.has(nb) ? indeg.get(nb) : 0;
                        indeg.set(nb, currentDegree - 1);
                        if (indeg.get(nb) === 0) {
                            q.push(nb);
                        }
                        iterNext = iterator.next();
                    }
                }
            }
            return order;
        };
        return cyclicValidator;
    }());
    exports.cyclicValidator = cyclicValidator;
});