[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / red / history.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16 RED.history = (function() {
17     var undo_history = [];
18     
19     return {
20         //TODO: this function is a placeholder until there is a 'save' event that can be listened to
21         markAllDirty: function() {
22             for (var i=0;i<undo_history.length;i++) {
23                 undo_history[i].dirty = true;
24             }
25         },
26         depth: function() {
27             return undo_history.length;
28         },
29         push: function(ev) {
30             undo_history.push(ev);
31         },
32         pop: function() {
33             var ev = undo_history.pop();
34             var i;
35             if (ev) {
36                 if (ev.t == 'add') {
37                     if (ev.nodes) {
38                         for (i=0;i<ev.nodes.length;i++) {
39                             RED.nodes.remove(ev.nodes[i]);
40                         }
41                     }
42                     if (ev.links) {
43                         for (i=0;i<ev.links.length;i++) {
44                             RED.nodes.removeLink(ev.links[i]);
45                         }
46                     }
47                     if (ev.workspaces) {
48                         for (i=0;i<ev.workspaces.length;i++) {
49                             RED.nodes.removeWorkspace(ev.workspaces[i].id);
50                             RED.view.removeWorkspace(ev.workspaces[i]);
51                         }
52                     }
53                 } else if (ev.t == "delete") {
54                     if (ev.workspaces) {
55                         for (i=0;i<ev.workspaces.length;i++) {
56                             RED.nodes.addWorkspace(ev.workspaces[i]);
57                             RED.view.addWorkspace(ev.workspaces[i]);
58                         }
59                     }
60                     if (ev.nodes) {
61                         for (i=0;i<ev.nodes.length;i++) {
62                             RED.nodes.add(ev.nodes[i]);
63                         }
64                     }
65                     if (ev.links) {
66                         for (i=0;i<ev.links.length;i++) {
67                             RED.nodes.addLink(ev.links[i]);
68                         }
69                     }
70                 } else if (ev.t == "move") {
71                     for (i=0;i<ev.nodes.length;i++) {
72                         var n = ev.nodes[i];
73                         n.n.x = n.ox;
74                         n.n.y = n.oy;
75                         n.n.dirty = true;
76                     }
77                 } else if (ev.t == "edit") {
78                     for (i in ev.changes) {
79                         if (ev.changes.hasOwnProperty(i)) {
80                             ev.node[i] = ev.changes[i];
81                         }
82                     }
83                     RED.editor.updateNodeProperties(ev.node);
84                     if (ev.links) {
85                         for (i=0;i<ev.links.length;i++) {
86                             RED.nodes.addLink(ev.links[i]);
87                         }
88                     }
89                     RED.editor.validateNode(ev.node);
90                     ev.node.dirty = true;
91                     ev.node.changed = ev.changed;
92                 }
93                 RED.view.dirty(ev.dirty);
94                 RED.view.redraw();
95             }
96         }
97     }
98
99 })();