forked from NigelThorne/workflowish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
268 lines (231 loc) · 6.78 KB
/
index.html
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
<html>
<head>
<script type='text/javascript' src='jquery-1.6.2.min.js'></script>
<script type='text/javascript' src='jquery.tmpl.js'></script>
<script type='text/javascript' src='jquery.jkey-1.2.js'></script>
<style>
#editor {border: 0px; position: absolute; font-size: 1.0em;}
li, .node{font-size: 1.0em;}
</style>
</head>
<body>
<!-- Nodes -->
<form onSubmit="return false;">
<input id="editor"/>
</form>
<script type="text/html" id="pageTemplate" >
<div id="tree" >
<h1>${name}</h1>
<ul>
{{tmpl(nodes) "#nodeTemplate"}}
</ul>
</div>
</script>
<script type="text/html" id="nodeTemplate">
<li class="node">
<span>${name}</span>
<ul>
{{tmpl(nodes) "#nodeTemplate"}}
</ul>
</li>
</script>
<div class="notes">
Use 'ALT' and arrows to change the selection around.
Use 'CTRL' and arrows to move a node around the tree.
</div>
<div class="TODO">
<ul>
<li>Save/Load data from local file store</li>
<li>Expand/Collapse</li>
<li>Change up/down nagivation to respect expanded nodes</li>
</ul>
</div>
<form>
<table>
<tr>
<td>File to load:<input type="file" id="fileToLoad"></td>
<td><a href="javascript:loadFileAsText()">Load Selected File</a><td>
</tr>
<tr>
<td>Filename Save As:<input id="inputFileName"></input></td>
<td><a href="javascript:saveTextAsFile()">Save Results to File</a></td>
</tr>
</table>
</form>
</body>
<script lang='text/javascript'>
function edit_node(node){
if(node === undefined || node.length == 0) return;
var txt = node_text(node);
window.current_node = node;
$("#editor")[0].value = txt;
$("#editor").focus();
move_editor_to_node(node);
};
function node_text(node){
return node.children("span").text();
};
function set_node_text(node, text){
return node.children("span").text(text);
};
function swap_with_next(node){
node.insertAfter(node.next());
move_editor_to_node(node);
};
function swap_with_previous(node){
node.prev().insertAfter(node);
move_editor_to_node(node);
};
function next(node){
// return node.next();
if(node.length === 0) return null
return $(node.next()[0] || next(node.parents(".node").first()));
};
function prev(node){
// return node.prev();
return $(node.prev()[0] || node.parents(".node").first());
};
function outdent(node){
node.insertAfter(parent_node(node));
move_editor_to_node(node);
};
function indent(node){
node.appendTo(items(node.prev()));
move_editor_to_node(node);
};
function remove(node){
edit_node(node.prev() || node.next());
node.remove();
};
function parent_node(node){
return node.parents(".node").first()
};
function items(node){
return node.children("ul");
};
function add_sibling(node){
return create_node().insertAfter(node);
};
function create_node(){
return $( "#nodeTemplate" ).tmpl( {name: "", nodes: []} );
};
<!---- end of node methods ---->
// enter => add following sibling
// ctrl up => swap with prev
// ctrl down => swap with next
// ctrl right => indent
// ctrl left => outdent
/// backspace empty textbox => remove
$("#editor").jkey('enter', function(){
edit_node(add_sibling(window.current_node));
}).jkey('backspace', true, function(){
if(this.value.length == 0) {
remove(window.current_node);
return false;
};
return true;
});
$(document).keydown(function(event){
if(event.ctrlKey){
if(event.keyCode == 37){ // left
outdent(window.current_node);
return false;
};
if(event.keyCode == 39){ // right
indent(window.current_node);
return false;
};
if(event.keyCode == 40){ // down
swap_with_next(window.current_node);
return false;
};
if(event.keyCode == 38){ // up
swap_with_previous(window.current_node);
return false;
};
}
else if(event.altKey){
if(event.keyCode == 37){ // left
edit_node( parent_node(window.current_node));
return false;
};
if(event.keyCode == 39){ // right
edit_node(window.current_node.find(".node").first());
return false;
};
if(event.keyCode == 40){ // down
edit_node(next(window.current_node));
return false;
};
if(event.keyCode == 38){ // up
edit_node(prev(window.current_node));
return false;
};
};
});
function move_editor_to_node(node){
var p = node.position();
$("#editor").css("top",p.top -2).css("left", p.left - 2);
};
function on_span_clicked(){
edit_node($(this).parent());
};
function set_editor_width_from_node(node){
// uses li not span
$("#editor").css("width", window.current_node.css("width"));
}
function update_node_from_editor(){
set_node_text(window.current_node, $("#editor")[0].value);
set_editor_width_from_node(window.current_node);
};
function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
// eval("rootNode = " + textFromFileLoaded); // assumes json
// $("#pageTemplate").tmpl(rootNode).appendTo($("body"));
eval('$("#tree").html(' + textFromFileLoaded + ')');
$(".node > span").live("click", on_span_clicked);
edit_node($(".node").first());
setInterval("update_node_from_editor()",100);
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
function saveTextAsFile()
{
var fileNameToSaveAs = document.getElementById("inputFileName").value;
saveToFile(JSON.stringify($("#tree").html()), fileNameToSaveAs);
}
function saveToFile(textToWrite, fileNameToSaveAs)
{
var textFileAsBlob = new Blob([textToWrite], {type:'text/json'});
var downloadLink = document.createElement("a");
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.download = fileNameToSaveAs;
downloadLink.click();
}
var rootNode = {name: "Things Left to do...", nodes:[
{name: 'Look',nodes:[
{name: 'Colour the scopes',nodes:[]},
]},
{name: 'Feel',nodes:[
{name: 'Expand/Collapse',nodes:[]},
{name: 'Split line with "enter"',nodes:[]},
]},
{name: 'Function',nodes:[
{name: 'Save',nodes:[]},
{name: 'Load',nodes:[]},
{name: 'Undo',nodes:[]},
]},
{name: 'Redo',nodes:[]},
{name: 'Auto-Save',nodes:[]},
]};
$("#pageTemplate").tmpl(rootNode).appendTo($("body"));
$(".node > span").live("click", on_span_clicked);
edit_node($(".node").first());
setInterval("update_node_from_editor()",100);
</script>
</html>