diff options
| author | Alon Levy <alon@pobox.com> | 2014-11-29 22:28:29 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-11-29 22:33:44 +0200 |
| commit | 42eacd877b5564961c2dcc491f5d4b7d62f4e753 (patch) | |
| tree | 6fce918b89bc0093671db978a26d5821036e1ade | |
| parent | 372aac3fafedbd517cf26501ef70015f47feb7f9 (diff) | |
use Bacon instead of signal
See https://github.com/baconjs/bacon.js
Usage right now:
we have rz_bus to hold global busses:
- names: for autosuggest
- ui busses:
- ui_key
- ui_input
The events are objects with {where: from consts, keys/input}
(could maybe just use the events and rely on the target element
class/id)
And graph has it's own bus:
graph.diffBus
(camelcase probably not the right style - will fix in later commit)
To publish to a bus you use push.
To insert another stream to a bus you use plug.
To listen to a bus you use onValue (you can use subscribe too to get
errors and end of stream event, but we don't use that yet).
This allows usage of map, filter, flatMap, see github repo for cool
examples.
| -rw-r--r-- | src/consts.js | 3 | ||||
| -rw-r--r-- | src/history.js | 12 | ||||
| -rw-r--r-- | src/main.js | 2 | ||||
| -rw-r--r-- | src/model/graph.js | 19 | ||||
| -rw-r--r-- | src/rz_core.js | 10 | ||||
| -rw-r--r-- | src/signal.js | 22 | ||||
| -rw-r--r-- | src/textanalysis.js | 10 | ||||
| -rw-r--r-- | src/textanalysis.ui.js | 13 |
8 files changed, 37 insertions, 54 deletions
diff --git a/src/consts.js b/src/consts.js index 27460033..310d6153 100644 --- a/src/consts.js +++ b/src/consts.js @@ -3,11 +3,8 @@ define(function() { // TODO: enums, sometime return { - APPLIED_GRAPH_DIFF: 'graph_diff', - KEYSTROKES: 'keystrokes', KEYSTROKE_WHERE_EDIT_NODE: 'keystroke_where_edit_node', KEYSTROKE_WHERE_DOCUMENT: 'keystroke_where_document', KEYSTROKE_WHERE_TEXTANALYSIS: 'keystroke_where_textanalysis', - SUGGESTED_NAME_ADD: 'suggested_name_add', }; }); diff --git a/src/history.js b/src/history.js index 89237033..a6a02ec3 100644 --- a/src/history.js +++ b/src/history.js @@ -16,23 +16,21 @@ // ReferenceError: __commandLineAPI is not defined //var ActionEnum = Enum(); -define(['jquery', 'FileSaver', 'consts', 'signal'], - function($, saveAs, consts, signal) { +define(['jquery', 'FileSaver', 'consts', 'rz_bus'], + function($, saveAs, consts, rz_bus) { /* user - username (string) * svg - svg element for catching zoom events (jquery DOMNode wrapper) */ -function History(user, transform_element) { +function History(user, graph, transform_element) { var that = this; this.records = []; this.user = user; this.transform_element = transform_element; - signal.slot(consts.APPLIED_GRAPH_DIFF, function(obj) { + graph.diffBus.onValue(function (obj) { return that.record_graph_diff(obj) }); - signal.slot(consts.KEYSTROKES, function(obj) { - return that.record_keystrokes(obj); - }); + rz_bus.ui_key.onValue(that.record_keystrokes.bind(that)); // XXX create zoom behavior - then proof to event name change $(window).on('wheel.history', function(obj) { that.record_zoom(obj); diff --git a/src/main.js b/src/main.js index 80837cdb..9b021298 100644 --- a/src/main.js +++ b/src/main.js @@ -49,7 +49,7 @@ function(textanalysis_ui, textanalysis, buttons, history, drag_n_drop, }); // TODO: interaction between the hack above and this model_core.init(rz_config); - textanalysis.init(); + textanalysis.init(rz_core.graph); } return { diff --git a/src/model/graph.js b/src/model/graph.js index 9123b669..6a299b5e 100644 --- a/src/model/graph.js +++ b/src/model/graph.js @@ -1,14 +1,17 @@ "use strict" -define(['signal', 'consts', 'util', 'model/core', 'model/util', 'rz_api_backend', 'rz_api_mesh'], -function (signal, consts, util, model_core, model_util, rz_api_backend, rz_api_mesh) { +define(['Bacon', 'consts', 'util', 'model/core', 'model/util', 'rz_api_backend', 'rz_api_mesh', 'history'], +function (Bacon, consts, util, model_core, model_util, rz_api_backend, rz_api_mesh, history) { var debug = false; function Graph() { var nodes = [], - links = []; + links = [], + diffBus = new Bacon.Bus(); + + this.diffBus = diffBus; /** * add node if no previous node is present whose id equals that of the node being added @@ -59,7 +62,7 @@ function Graph() { nodes.push(node); if (notify) { - signal.signal(consts.APPLIED_GRAPH_DIFF, [{nodes: {add: [node]}}]); + diffBus.push({nodes: {add: [node]}}); } return node; @@ -76,7 +79,7 @@ function Graph() { if (index !== undefined) { nodes.splice(index, 1); } - signal.signal(consts.APPLIED_GRAPH_DIFF, [{nodes: {removed: [id]}}]); + diffBus.push({nodes: {removed: [id]}}); } this.removeNodes = function(state) { @@ -95,7 +98,7 @@ function Graph() { } } if (ns.length > 0) { - signal.signal(consts.APPLIED_GRAPH_DIFF, [{nodes: {removed: ns.map(function(n) { return n.id; })}}]); + diffBus.push({nodes: {removed: ns.map(function(n) { return n.id; })}}); } } @@ -283,7 +286,7 @@ function Graph() { if (!found) { var link = model_core.create_link__set_random_id(src, dst, { name: name, state: state }); links.push(link); - signal.signal(consts.APPLIED_GRAPH_DIFF, [{links: {add: [link]}}]); + diffBus.push({links: {add: [link]}}); } else { found.name = name; found.state = state; @@ -585,7 +588,7 @@ function Graph() { data.links.forEach(function(link) { that.addLink(link.__src, link.__dst, link.name, "perm"); }); - signal.signal(consts.SUGGESTED_NAME_ADD, [added_names]); + rz_bus.names.push(added_names); this.clear_history(); } diff --git a/src/rz_core.js b/src/rz_core.js index 44dd02fb..46fc753a 100644 --- a/src/rz_core.js +++ b/src/rz_core.js @@ -1,7 +1,7 @@ "use strict" -define(['jquery', 'd3', 'consts', 'signal', 'util', 'model/graph', 'model/core', 'view/helpers', 'view/view', 'rz_observer', 'view/selection'], -function($, d3, consts, signal, util, model_graph, model_core, view_helpers, view, rz_observer, selection) { +define(['jquery', 'd3', 'consts', 'rz_bus', 'util', 'model/graph', 'model/core', 'view/helpers', 'view/view', 'rz_observer', 'view/selection'], +function($, d3, consts, rz_bus, util, model_graph, model_core, view_helpers, view, rz_observer, selection) { var addednodes = []; @@ -597,7 +597,7 @@ function mousedown() { } $('#editform').keypress(function(e) { - signal.signal(consts.KEYSTROKES, [{where: consts.KEYSTROKE_WHERE_EDIT_NODE, keys: [e.which]}]); + var ret = undefined; if (e.which == 13) { $('.editinfo').css('top', -100); $('.editinfo').css('left', 0); @@ -606,8 +606,10 @@ $('#editform').keypress(function(e) { var d = element.data().d; graph.editName(d.id, newname); update_view__graph(true); - return false; + ret = false; } + rz_bus.ui_key.push({where: consts.KEYSTROKE_WHERE_EDIT_NODE, keys: [e.which]}); + return ret; }); diff --git a/src/signal.js b/src/signal.js deleted file mode 100644 index e5888b4c..00000000 --- a/src/signal.js +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Signal/slot (i.e. blackboard pattern) for rhizi. - * - * This is a thin wrapper over jquery right now. - * But just keeping it here to make any future change of implementation slightly - * easier. - */ - -define(['jquery'], function($) { - function slot(name, handler) { - $(window).on(name, function(e, args) { - handler(args); - }); - }; - function signal(name, obj) { - $(window).trigger(name, obj); - } - return { - 'slot': slot, - 'signal': signal - }; -}) diff --git a/src/textanalysis.js b/src/textanalysis.js index 9114bbff..f1277050 100644 --- a/src/textanalysis.js +++ b/src/textanalysis.js @@ -1,7 +1,7 @@ "use strict"; -define(['rz_core', 'model/core', 'model/util', 'model/diff', 'signal', 'consts'], -function(rz_core, model_core, model_util, model_diff, signal, consts) { +define(['rz_core', 'model/core', 'model/util', 'model/diff', 'rz_bus', 'consts'], +function(rz_core, model_core, model_util, model_diff, rz_bus, consts) { var typeindex = 0; var nodetypes = ["person", "club", "skill", "interest", "third-internship-proposal", "internship"]; @@ -466,7 +466,7 @@ var textAnalyser = function (newtext, finalize) { return ret; }; -function init() +function init(graph) { function onNodeAdded(diff) { if (!diff || !diff.nodes || diff.nodes.added) { @@ -480,8 +480,8 @@ function init() function onSuggestedNameAdd(names) { names.map(String.toLowerCase).forEach(autoSuggestAddName); } - signal.slot(consts.APPLIED_GRAPH_DIFF, onNodeAdded); - signal.slot(consts.SUGGESTED_NAME_ADD, onSuggestedNameAdd); + graph.diffBus.onValue(onNodeAdded); + rz_bus.names.onValue(onSuggestedNameAdd); } return { diff --git a/src/textanalysis.ui.js b/src/textanalysis.ui.js index 15d3d2e2..380427be 100644 --- a/src/textanalysis.ui.js +++ b/src/textanalysis.ui.js @@ -1,7 +1,7 @@ "use strict" -define(['jquery', 'autocomplete', 'rz_core', 'textanalysis', 'signal', 'consts'], -function($, autocomplete, rz_core, textanalysis, signal, consts) { +define(['jquery', 'Bacon', 'consts', 'rz_bus', 'autocomplete', 'rz_core', 'textanalysis'], +function($, Bacon, consts, rz_bus, autocomplete, rz_core, textanalysis) { var text = ""; // Last text of sentence var element_name = '#textanalyser'; @@ -116,6 +116,9 @@ return { }, }); + var document_keypress = new Bacon.Bus(); + rz_bus.ui_key.plug(document_keypress); + $(document).keypress(function(e) { var ret = undefined; switch (e.keyCode) { @@ -137,10 +140,12 @@ return { suggestionChange = true; break; } - signal.signal(consts.KEYSTROKES, [{where: consts.KEYSTROKE_WHERE_DOCUMENT, keys: [e.keyCode]}]); + document_keypress.push({where: consts.KEYSTROKE_WHERE_DOCUMENT, keys: [e.keyCode]}); return ret; }); + var element_keypress = new Bacon.Bus(); + rz_bus.ui_key.plug(element_keypress); element.keypress(function(e) { var ret = undefined; switch (e.which) { @@ -165,7 +170,7 @@ return { ret = false; break; } - signal.signal(consts.KEYSTROKES, [{where: consts.KEYSTROKE_WHERE_TEXTANALYSIS, keys:[e.which]}]); + element_keypress.push({where: consts.KEYSTROKE_WHERE_TEXTANALYSIS, keys:[e.which]}); return ret; }); |
