Skip to content

Commit

Permalink
Modularize JS
Browse files Browse the repository at this point in the history
TODO: commit message
  • Loading branch information
tbekolay committed Jul 18, 2016
1 parent ab3483a commit 964a4ea
Show file tree
Hide file tree
Showing 48 changed files with 1,248 additions and 1,056 deletions.
2 changes: 1 addition & 1 deletion nengo_gui/components/ace_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def update_client(self, client):
self.last_stdout = stdout

def javascript(self):
return 'Nengo.ace = new Nengo.Ace("%s", {})' % (id(self),)
return 'var editoruid = "%s";' % id(self)

def message(self, msg):
data = json.loads(msg)
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def update_code(self, msg):
pass

def javascript(self):
return 'Nengo.disable_editor();'
return 'utils.disable_editor();'


class NoEditor(Editor):
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/htmlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def update_client(self, client):
def javascript(self):
info = dict(uid=id(self), label=self.label)
json = self.javascript_config(info)
return 'new Nengo.HTMLView(main, sim, %s);' % json
return 'new HTMLView(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
return [uids[self.obj]]
2 changes: 1 addition & 1 deletion nengo_gui/components/netgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def update_client(self, client):
self.expand_network(network, client)

def javascript(self):
return 'new Nengo.NetGraph(main, {uid:"%s"});' % id(self)
return 'var netgraphargs = {uid:"%s"};' % id(self)

def message(self, msg):
try:
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def update_client(self, client):
def javascript(self):
info = dict(uid=id(self), label=self.label)
json = self.javascript_config(info)
return 'new Nengo.Pointer(main, sim, %s);' % json
return 'new Pointer(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
return [uids[self.obj], 'target=%r' % self.target]
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def javascript(self):
info = dict(uid=id(self), label=self.label,
max_neurons=self.max_neurons)
json = self.javascript_config(info)
return 'new Nengo.Raster(main, sim, %s);' % json
return 'new Raster(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
return [uids[self.obj.ensemble]]
Expand Down
7 changes: 2 additions & 5 deletions nengo_gui/components/sim_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ def update_client(self, client):
self.last_status = status
if self.send_config_options:
client.write_text('sims:' + self.backend_options_html())
client.write_text('config'
'Nengo.Toolbar.prototype.config_modal_show();')
client.write_text('confignengo.toolbar.config_modal_show();')
self.send_config_options = False

def get_status(self):
Expand All @@ -184,9 +183,7 @@ def javascript(self):
info = dict(uid=id(self))
fn = json.dumps(self.page.filename)
js = self.javascript_config(info)
return ('sim = new Nengo.SimControl(control, %s);\n'
'toolbar = new Nengo.Toolbar(%s);\n'
'Nengo.sidemenu = new Nengo.SideMenu();' % (js, fn))
return ('var simargs = %s;\nvar filename = %s;' % (js, fn))

def message(self, msg):
if msg == 'pause':
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def javascript(self):
label=self.label,
start_value=[float(x) for x in self.start_value])
json = self.javascript_config(info)
return 'new Nengo.Slider(main, sim, %s);' % json
return 'new Slider(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def update_client(self, client):
while len(self.to_client) > 0:
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/spa_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def javascript(self):
info = dict(uid=id(self), label=self.label, n_lines=len(self.labels),
synapse=0, pointer_labels=self.labels)
json = self.javascript_config(info)
return 'new Nengo.SpaSimilarity(main, sim, %s);' % json
return 'new SpaSimilarity(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def message(self, msg):
"""Message receive function for show_pairs toggling and reset"""
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/spike_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def javascript(self):
info = dict(uid=id(self), label=self.label,
pixels_x=self.pixels_x, pixels_y=self.pixels_y)
json = self.javascript_config(info)
return 'new Nengo.Image(main, sim, %s);' % json
return 'new Image(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
args = [uids[self.obj]]
Expand Down
3 changes: 1 addition & 2 deletions nengo_gui/components/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def javascript(self):
# generate the javascript that will create the client-side object
info = dict(uid=id(self), label=self.label,
n_lines=self.n_lines)

json = self.javascript_config(info)
return 'new Nengo.Value(main, sim, %s);' % json
return 'new Value(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
# generate the list of strings for the .cfg file to save this Component
Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/voltage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def javascript(self):
info = dict(uid=id(self), label=self.label,
n_lines=self.n_neurons, synapse=0)
json = self.javascript_config(info)
return 'new Nengo.Value(main, sim, %s);' % json
return 'new Value(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
return [uids[self.obj.ensemble]]
2 changes: 1 addition & 1 deletion nengo_gui/components/xyvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def update_client(self, client):
def javascript(self):
info = dict(uid=id(self), n_lines=self.n_lines, label=self.label)
json = self.javascript_config(info)
return 'new Nengo.XYValue(main, sim, %s);' % json
return 'new XYValue(nengo.main, nengo.viewport, nengo.sim, %s);' % json

def code_python_args(self, uids):
return [uids[self.obj]]
8 changes: 6 additions & 2 deletions nengo_gui/guibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ def serve_main(self):
html = html.decode("utf-8")

# fill in the javascript needed and return the complete page
components = page.create_javascript()
data = (html % dict(components=components)).encode('utf-8')
main_components, components = page.create_javascript()

data = html % dict(
main_components=main_components, components=components)
data = data.encode('utf-8')

return server.HttpResponse(data)

def serve_favicon(self):
Expand Down
18 changes: 9 additions & 9 deletions nengo_gui/modal_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def infomodal(ng, uid, **args):


def add_modal_title_js(title_text):
return 'Nengo.modal.title("%s");' % (title_text)
return 'nengo.modal.title("%s");' % (title_text)


def add_modal_footer_js(footer_text):
return 'Nengo.modal.footer("%s");' % (footer_text)
return 'nengo.modal.footer("%s");' % (footer_text)


def show_modal_js():
return 'Nengo.modal.show();'
return 'nengo.modal.show();'


def ensemble_infomodal(ng, uid, conn_in_uids, conn_out_uids):
Expand All @@ -50,11 +50,11 @@ def ensemble_infomodal(ng, uid, conn_in_uids, conn_out_uids):

conninfo = conn_infomodal(ng, uid, conn_in_uids, conn_out_uids)

js = ['Nengo.modal.title("Details for \'%s\'");' % ng.page.get_label(ens)]
js.append('Nengo.modal.footer("close");')
js.append('Nengo.modal.ensemble_body("%s", %s, %s, %s);' % (
js = ['nengo.modal.title("Details for \'%s\'");' % ng.page.get_label(ens)]
js.append('nengo.modal.footer("close");')
js.append('nengo.modal.ensemble_body("%s", %s, %s, %s);' % (
uid, json.dumps(params), json.dumps(plots), json.dumps(conninfo)))
js.append('Nengo.modal.show();')
js.append('nengo.modal.show();')
return '\n'.join(js)


Expand All @@ -71,7 +71,7 @@ def node_infomodal(ng, uid, conn_in_uids, conn_out_uids):
js = [add_modal_title_js("Details for \'%s\'" % (
ng.page.get_label(node)))]
js.append(add_modal_footer_js('close'))
js.append('Nengo.modal.node_body("%s", %s, %s, %s);' % (
js.append('nengo.modal.node_body("%s", %s, %s, %s);' % (
uid, json.dumps(params), json.dumps(plots), json.dumps(conninfo)))
js.append(show_modal_js())
return '\n'.join(js)
Expand Down Expand Up @@ -146,7 +146,7 @@ def net_infomodal(ng, uid, conn_in_uids, conn_out_uids):
js = [add_modal_title_js("Details for \'%s\'") % (
ng.page.get_label(net))]
js.append(add_modal_footer_js('close'))
js.append('Nengo.modal.net_body("%s", %s, %s);' % (
js.append('nengo.modal.net_body("%s", %s, %s);' % (
uid, json.dumps(stats), json.dumps(conninfo)))
js.append(show_modal_js())
return '\n'.join(js)
19 changes: 9 additions & 10 deletions nengo_gui/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,18 @@ def modified_config(self):

def create_javascript(self):
"""Generate the javascript for the current model and layout."""
if self.filename is not None:
fn = json.dumps(self.filename)
webpage_title_js = ';document.title = %s;' % fn
else:
webpage_title_js = ''

assert isinstance(self.components[0], nengo_gui.components.SimControl)

component_js = '\n'.join([c.javascript() for c in self.components])
component_js += webpage_title_js
main = (nengo_gui.components.NetGraph,
nengo_gui.components.SimControl,
nengo_gui.components.AceEditor)

main_js = '\n'.join([c.javascript() for c in self.components
if isinstance(c, main)])
component_js = '\n'.join([c.javascript() for c in self.components
if not isinstance(c, main)])
if not self.gui.model_context.writeable:
component_js += "$('#Open_file_button').addClass('deactivated');"
return component_js
return main_js, component_js

def get_label(self, obj, default_labels=None):
"""Return a readable label for an object.
Expand Down
13 changes: 9 additions & 4 deletions nengo_gui/static/components/2d_axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* @param {float} args.min_value - minimum value on y-axis
* @param {float} args.max_value - maximum value on y-axis
*/
Nengo.Axes2D = function(parent, args) {

var d3 = require('d3');

var Axes2D = function(parent, args) {
var self = this;

this.max_y_width = 100;
Expand Down Expand Up @@ -48,7 +51,7 @@ Nengo.Axes2D = function(parent, args) {
.call(this.axis_y);
};

Nengo.Axes2D.prototype.set_axes_geometry = function(width, height) {
Axes2D.prototype.set_axes_geometry = function(width, height) {
scale = parseFloat($('#main').css('font-size'));
this.width = width;
this.height = height;
Expand All @@ -64,7 +67,7 @@ Nengo.Axes2D.prototype.set_axes_geometry = function(width, height) {
/**
* Adjust the graph layout due to changed size
*/
Nengo.Axes2D.prototype.on_resize = function(width, height) {
Axes2D.prototype.on_resize = function(width, height) {
if (width < this.minWidth) {
width = this.minWidth;
}
Expand All @@ -90,7 +93,7 @@ Nengo.Axes2D.prototype.on_resize = function(width, height) {
this.axis_y_g.call(this.axis_y);
};

Nengo.Axes2D.prototype.fit_ticks = function(parent) {
Axes2D.prototype.fit_ticks = function(parent) {
var self = this;
setTimeout(function() {
var ticks = $(parent.div).find('.tick');
Expand All @@ -106,3 +109,5 @@ Nengo.Axes2D.prototype.fit_ticks = function(parent) {
self.on_resize(parent.width, parent.height);
}, 1);
};

module.exports = Axes2D;
Loading

0 comments on commit 964a4ea

Please sign in to comment.