summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2015-01-06 22:34:38 +0200
committerAlon Levy <alon@pobox.com>2015-01-06 22:34:38 +0200
commit168a9bfb109a4a44c185fa5d9367f6e8aec6c279 (patch)
tree5589a47f92f6a420ae995bbd85a397b87ea44701
parentce130f4cae66a26d9b583d52309de2176803f78e (diff)
client: fix tab switching; move to new tokenize implementation (not strictly ""right""
-rw-r--r--src/client/model/graph.js4
-rw-r--r--src/client/textanalysis.js99
-rw-r--r--src/client/textanalysis.ui.js13
-rw-r--r--src/client/view/graph_view.js1
4 files changed, 59 insertions, 58 deletions
diff --git a/src/client/model/graph.js b/src/client/model/graph.js
index c097fbfd..d7fd1add 100644
--- a/src/client/model/graph.js
+++ b/src/client/model/graph.js
@@ -146,7 +146,7 @@ function Graph(spec) {
existing_node = find_node__by_id(node.id);
if (existing_node) {
- console.log('__addNode: id collision: existing-node.id: \'' + existing_node.id + '\', ' + 'new-node.id: \'' + node.id + '\'');
+ console.log('__addNode: id collision: existing-node.id: \'' + existing_node.id);
return existing_node;
}
@@ -510,7 +510,7 @@ function Graph(spec) {
function new_attr_diff_prop_value(id, prop, value)
{
- var diff = rz_diff.new_attr_diff();
+ var diff = model_diff.new_attr_diff();
diff.add_node_attr_write(id, prop, value);
return diff;
diff --git a/src/client/textanalysis.js b/src/client/textanalysis.js
index aab16c37..3f2408d0 100644
--- a/src/client/textanalysis.js
+++ b/src/client/textanalysis.js
@@ -9,8 +9,23 @@
*
* treats '\\' as a quote for the next char.
*
+ * TODO: should only apply if cursor is actually on token,
+ *
+ * so need tokenise to be fixed to split according to actual tokens,
+ * i.e.:
+ * #"one two" four five #six
+ * is exactly 3 tokens:
+ * #"one two"
+ * four five
+ * #six
+ * or 5 if you assign a token for the '#' char:
+ * #
+ * "one two"
+ * four five
+ * #
+ * six
*/
-function new_tokenize(text, node_token, quote)
+function tokenize(text, node_token, quote)
{
var c,
i,
@@ -50,6 +65,7 @@ function new_tokenize(text, node_token, quote)
default:
if (c == node_token && prev_whitespace) {
tokens.push({start: i, end: i + 1, token: node_token});
+ start = i + 1;
} else {
token.push(c);
}
@@ -64,11 +80,13 @@ function new_tokenize(text, node_token, quote)
define(['rz_core', 'model/core', 'model/util', 'model/diff', 'consts', 'util'],
function(rz_core, model_core, model_util, model_diff, consts, util) {
-var typeindex = 0;
-var nodetypes = consts.nodetypes;
-var typeStack = [];
+var typeindex = 0,
+ nodetypes = consts.nodetypes,
+ typeStack = [];
-var lastnode;
+var _get_lastnode,
+ get_lastnode = function (editgraph) { return _get_lastnode(editgraph); },
+ node_name_to_type = {};
var sugg_name = {},
id_to_name_map = {},
@@ -123,43 +141,6 @@ function auto_suggest__update_from_graph()
suggestions_bus.push(sugg_name);
}
-// TODO: add escape char, i.e. r"bla\"bla" -> ['bla"bla']
-function tokenize(text, node_token, quote)
-{
- var segment = [],
- subsegment = [],
- sentence = [],
- quoteword;
- var j;
-
- segment = text.split(node_token);
- for (j = 0; j < segment.length; j++) {
- if (j !== 0) sentence.push(node_token);
- subsegment = segment[j].split(" ");
- if (subsegment.length === 0) {
- sentence.push(" ");
- }
- for (var k = 0; k < subsegment.length; k++) {
- if (subsegment[k] !== " " && subsegment[k] !== "") {
- if (subsegment[k].charAt(0) === quote) {
- quoteword = "";
- do {
- quoteword += subsegment[k] + ' ';
- if(subsegment[k].charAt(subsegment[k].length-1) !== quote)k++;
- } while (k < subsegment.length && subsegment[k].charAt(subsegment[k].length - 1) !== quote);
- if (subsegment[k] && subsegment[k]!==quoteword.replace(/ /g, "")) {
- quoteword += subsegment[k];
- }
- sentence.push(quoteword.replace(new RegExp(quote, 'g'), ""));
- } else {
- sentence.push(subsegment[k]);
- }
- }
- }
- }
- return sentence;
-}
-
/*
* textAnalyser
*
@@ -183,6 +164,8 @@ var textAnalyser = function (spec) {
var newtext = spec.sentence,
finalize = spec.finalize,
cursor = spec.cursor,
+
+ tokens,
sentence,
token_set_new_node_names = [], // token set representing new node names
token_set_new_link_names = [], // token set representing new link names
@@ -252,9 +235,8 @@ var textAnalyser = function (spec) {
//Sentence Sequencing
//Build the words and cuts the main elements
- sentence = new_tokenize(newtext, '#', '"').map(function (d) {
- return d.token;
- });
+ tokens = tokenize(newtext, '#', '"');
+ sentence = tokens.map(function (d) { return d.token; });
// build new node,link arrays in order of appearance
for (m = 0; m < sentence.length; m++) {
@@ -473,15 +455,29 @@ var textAnalyser = function (spec) {
}
};
- function lookup_node_in_bounds(cursor) {
- // TODO
- return null;
+ function lookup_node_in_bounds(edit_graph) {
+ var i, d, j, name, node;
+
+ // go forward to find cursor location in tokens
+ for (i = 0 ; i < tokens.length; ++i) {
+ d = tokens[i];
+ if (cursor >= d.start && cursor < d.end) {
+ break;
+ }
+ }
+ // go back to find token
+ for (j = i - 1; j >= 0 && tokens[j].token != '#'; --j) {}
+ name = tokens[j + 1] ? tokens[j + 1].token : 'new node';
+ node = edit_graph.find_node__by_name(name);
+ util.assert(node !== undefined, "can't find node");
+ return node;
}
if (finalize) {
typeStack = [];
}
- lastnode = finalize ? null : lookup_node_in_bounds(cursor);
+ _get_lastnode = finalize || tokens.length == 0 ? function () { return null }
+ : lookup_node_in_bounds;
return ret;
};
@@ -501,7 +497,10 @@ return {
ANALYSIS_LINK:ANALYSIS_LINK,
//for the external arrow-type changer
- lastnode: function() { return lastnode; },
+ lastnode: get_lastnode,
+ set_type: function(name, nodetype) {
+ node_name_to_type[name] = nodetype;
+ },
selected_type_next: function() {
typeindex = (typeindex + 1) % 5;
diff --git a/src/client/textanalysis.ui.js b/src/client/textanalysis.ui.js
index a70746e3..2d51efb0 100644
--- a/src/client/textanalysis.ui.js
+++ b/src/client/textanalysis.ui.js
@@ -97,14 +97,16 @@ function textSelect(inp, s, e) {
}
function changeType(arg) {
- var lastnode = textanalysis.lastnode(),
+ var lastnode = textanalysis.lastnode(rz_core.edit_graph),
nodetype,
- id;
+ id,
+ name;
if (!lastnode) {
- id = "new node";
+ name = id = "new node";
} else {
id = lastnode.id;
+ name = lastnode.name;
}
nodetype = (arg === 'up'? textanalysis.selected_type_next() : textanalysis.selected_type_prev());
@@ -117,6 +119,7 @@ function changeType(arg) {
typeselection.showChosenType(nodetype);
rz_core.edit_graph.findCoordinates(id);
}
+ textanalysis.set_type(name, nodetype);
}
return {
@@ -148,9 +151,9 @@ return {
ret = false;
break;
case 9: //TAB
- if (textanalysis.lastnode()) {
+ if (textanalysis.lastnode(rz_core.edit_graph)) {
e.preventDefault();
- changeType(e.shiftKey ? "up" : "down", textanalysis.lastnode());
+ changeType(e.shiftKey ? "up" : "down");
ret = false;
}
break;
diff --git a/src/client/view/graph_view.js b/src/client/view/graph_view.js
index df703892..99af3edf 100644
--- a/src/client/view/graph_view.js
+++ b/src/client/view/graph_view.js
@@ -111,7 +111,6 @@ function GraphView(spec) {
.skipDuplicates()
.onValue(function (r) {
var now = (new Date()).getTime();
- console.log("setting bubble to " + r + "(time = " + now + ")");
gv.layout_animation.bubble_radius.target = r;
gv.layout_animation.bubble_radius.start = gv.bubble_radius;
gv.layout_animation.starttime = now;