-
-
Notifications
You must be signed in to change notification settings - Fork 880
/
Copy pathjsmind.shell.js
124 lines (117 loc) · 3.55 KB
/
jsmind.shell.js
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
/*
* Released under BSD License
* Copyright (c) 2014-2025 [email protected]
*
* Project Home:
* https://github.com/hizzgdev/jsmind/
*/
(function ($w) {
'use strict';
var $d = $w.document;
var __name__ = 'jsMind';
var jsMind = $w[__name__];
if (!jsMind) {
return;
}
if (typeof jsMind.shell != 'undefined') {
return;
}
var options = {
play_delay: 1000,
};
jsMind.shell = function (jm) {
this.jm = jm;
this.step = 0;
this.commands = []; //version
this.delay_handle = 0;
this.playing = false;
this.jm_editable = this.jm.get_editable();
};
jsMind.shell.prototype = {
init: function () {
this.playing = false;
},
record: function (action, obj) {
if (!this.playing) {
var command = { action: action, data: obj.data, node: obj.node };
var prev_command = this.commands[this.step - 1];
if (
command.action === 'update_node' &&
prev_command.action === 'add_node' &&
prev_command.data[2] === 'New Node'
) {
prev_command.data[2] = command.data[1];
this.commands[this.step - 1] = prev_command;
} else {
this.step = this.commands.push(command);
}
}
},
execute: function (command) {
var func = this.jm[command.action];
var node = command.node;
this.jm.enable_edit();
func.apply(this.jm, command.data);
this.jm.disable_edit();
if (!!node) {
this.jm.select_node(node);
}
},
add_command: function (command) {
this.commands.push(command);
play();
},
replay: function () {
this.step = 0;
this.play();
},
play: function () {
this.jm.disable_edit();
this.playing = true;
this._play_stepbystep();
},
_play_stepbystep: function () {
if (this.delay_handle != 0) {
$w.clearTimeout(this.delay_handle);
this.delay_handle = 0;
}
if (this.step < this.commands.length) {
this.execute(this.commands[this.step]);
this.step++;
var js = this;
this.delay_handle = $w.setTimeout(function () {
js.play.call(js);
}, options.play_delay);
} else {
this._play_end();
}
},
_play_end: function () {
this.playing = false;
if (this.jm_editable) {
this.jm.enable_edit();
} else {
this.jm.disable_edit();
}
},
jm_event_handle: function (type, data) {
if (type === jsMind.event_type.show) {
this.record('show', data);
}
if (type === jsMind.event_type.edit) {
var action = data.evt;
delete data.evt;
this.record(action, data);
}
},
};
var shell_plugin = new jsMind.plugin('shell', function (jm) {
var js = new jsMind.shell(jm);
jm.shell = js;
js.init();
jm.add_event_listener(function (type, data) {
js.jm_event_handle.call(js, type, data);
});
});
jsMind.register_plugin(shell_plugin);
})(window);