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
|
"use strict"
define(['jquery', 'Bacon', 'consts', 'rz_bus', 'rz_core', 'textanalysis', 'view/completer'],
function($, Bacon, consts, rz_bus, rz_core, textanalysis, completer) {
var text = "", // Last text of sentence
element_name = '#textanalyser',
element = $(element_name),
element_raw = element[0],
plus_button = $('.add-button'),
description = consts.description;
var typeselection = function TypeSelectionDialog() {
var e = $('.typeselection'),
e_intro = e.find('#intro'),
e_label = e.find('#chosentypelabel'),
e_name = e.find('#chosentypename'),
e_desc = e.find('#chosentypedesc'),
typeselection = {};
typeselection.analysisNodeStart = function() {
typeselection.show();
}
typeselection.show = function() {
e.css({
top: window.innerHeight / 2 - 115,
left: window.innerWidth / 2 - 325
});
e_label.hide();
e_desc.hide();
e_intro.show();
e.show();
}
typeselection.hide = function() {
e.hide();
}
typeselection.showChosenType = function(nodetype) {
var desc = description[nodetype];
e_intro.hide();
e_label.show();
e_name.html(nodetype);
if (desc) {
e_desc.html(description[nodetype]);
e_desc.show();
} else {
e_desc.hide();
}
}
return typeselection;
}();
var analysisCompleter = completer(element, $('#input-suggestion'), {hideOnTab: false});
function analyzeSentence(sentence, finalize)
{
var ret = textanalysis.textAnalyser(sentence, finalize);
switch (ret.state) {
case textanalysis.ANALYSIS_NODE_START:
typeselection.analysisNodeStart();
break;
case textanalysis.ANALYSIS_LINK:
typeselection.hide();
break;
}
var backend_commit = false;
ret.applyToGraph(rz_core.graph, backend_commit);
if (finalize || sentence.length == 0) {
typeselection.hide();
}
}
function textSelect(inp, s, e) {
e = e || s;
if (inp.createTextRange) {
var r = inp.createTextRange();
r.collapse(true);
r.moveEnd('character', e);
r.moveStart('character', s);
r.select();
}else if(inp.setSelectionRange) {
inp.focus();
inp.setSelectionRange(s, e);
}
}
function changeType(arg) {
var lastnode = textanalysis.lastnode(),
nodetype,
id;
if(!lastnode) {
id = "new node";
} else {
id = lastnode.id;
}
nodetype = (arg === 'up'? textanalysis.selected_type_next() : textanalysis.selected_type_prev());
if (arg === 'up') {
rz_core.graph.editType(id, null, nodetype);
typeselection.showChosenType(nodetype);
rz_core.graph.findCoordinates(id);
} else {
rz_core.graph.editType(id, null, nodetype);
typeselection.showChosenType(nodetype);
rz_core.graph.findCoordinates(id);
}
rz_core.update_view__graph(true);
}
return {
analyzeSentence: analyzeSentence,
main:function () {
if (element.length != 1) {
return;
}
analysisCompleter.options.plug(textanalysis.suggestions_options);
var document_keydown = new Bacon.Bus();
rz_bus.ui_key.plug(document_keydown);
element.keydown(function(e) {
var ret = undefined;
switch (e.keyCode) {
case 13:
if (!analysisCompleter.handleEnter()) {
submitNewSentence();
} else {
analyzeSentence(element.val(), false);
}
ret = false;
break;
case 9: //TAB
if (textanalysis.lastnode()) {
e.preventDefault();
changeType(e.shiftKey ? "up" : "down", textanalysis.lastnode());
ret = false;
}
break;
}
document_keydown.push({where: consts.KEYSTROKE_WHERE_TEXTANALYSIS, keys: [e.keyCode]});
return ret;
});
element.bind('input selectionchange click', function() {
analysisCompleter.oninput(element_raw.value, element_raw.selectionStart);
});
function submitNewSentence() {
text = element.val();
element.val("");
analyzeSentence(text, true);
text = "";
}
// Click is required to prevent the default action - this is a form so that's a post,
// and away we go.
// The mousedown is required because CSS3 transitions eat some events sometimes. This is
// the closest I've come to an explanation:
// http://stackoverflow.com/questions/15786891/browser-sometimes-ignores-a-jquery-click-event-during-a-css3-transform
plus_button.bind("click mousedown", function(e) {
console.dir(e);
submitNewSentence();
e.preventDefault();
});
var input = new Bacon.Bus();
rz_bus.ui_input.plug(input);
if ('oninput' in document.documentElement) {
element.on('input', function(e) {
text = element.val();
analyzeSentence(text, false);
input.push({where: consts.INPUT_WHERE_TEXTANALYSIS, input: text});
});
} else {
console.log('textanalysis.ui: fallback to polling');
window.setInterval(function() {
if (element.val() != text) {
if (text.length * 8 > 500) {
element.css('width', text.length * 8 + 20);
}
// text changed
text = element.val();
analyzeSentence(text, false);
suggestionChange = false;
}
}, 50);
}
}
};
}); // define
|