From 22373da674de567a3271832a24dc2e4f734ff109 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 5 Mar 2013 15:52:17 +0000 Subject: [PATCH] Client user script editor --- client/assets/dev/app.js | 5 +- client/assets/dev/applet_scripteditor.js | 94 ++++++++++++++++++++++++ client/assets/dev/build.js | 1 + client/assets/dev/index.html.tmpl | 19 ++++- client/assets/dev/model_application.js | 6 ++ client/assets/dev/model_gateway.js | 7 ++ client/assets/dev/view.js | 2 +- 7 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 client/assets/dev/applet_scripteditor.js diff --git a/client/assets/dev/app.js b/client/assets/dev/app.js index c6d2835..ee1ff68 100644 --- a/client/assets/dev/app.js +++ b/client/assets/dev/app.js @@ -52,7 +52,10 @@ _kiwi.global = { }; _.each(funcs, function(gateway_fn, func_name) { - obj[func_name] = _kiwi.gateway[gateway_fn]; + obj[func_name] = function() { + var fn_name = gateway_fn; + _kiwi.gateway[fn_name].apply(_kiwi.gateway, arguments); + }; }); return obj; diff --git a/client/assets/dev/applet_scripteditor.js b/client/assets/dev/applet_scripteditor.js new file mode 100644 index 0000000..8512129 --- /dev/null +++ b/client/assets/dev/applet_scripteditor.js @@ -0,0 +1,94 @@ + (function () { + var view = Backbone.View.extend({ + events: { + 'click .btn_save': 'onSave' + }, + + initialize: function (options) { + var that = this; + this.$el = $($('#tmpl_script_editor').html()); + + this.model.on('applet_loaded', function () { + that.$el.parent().css('height', '100%'); + $script('http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js', function (){ that.createAce(); }); + }); + }, + + + createAce: function () { + var editor_id = 'editor_' + Math.floor(Math.random()*10000000).toString(); + this.editor_id = editor_id; + + this.$el.find('.editor').attr('id', editor_id); + + this.editor = ace.edit(editor_id); + this.editor.setTheme("ace/theme/monokai"); + this.editor.getSession().setMode("ace/mode/javascript"); + + var script_content = _kiwi.global.settings.get('user_script') || ''; + this.editor.setValue(script_content); + }, + + + onSave: function (event) { + var script_content, user_fn; + + // Build the user script up with some pre-defined components + script_content = 'var network = kiwi.components.Network();\n'; + script_content += 'var input = kiwi.components.ControlInput();\n'; + script_content += this.editor.getValue() + '\n'; + + // Add a dispose method to the user script for cleaning up + script_content += 'this._dispose = function(){ network.off(); if(this.dispose) this.dispose(); }'; + + // Try to compile the user script + try { + user_fn = new Function(script_content); + + // Dispose any existing user script + if (_kiwi.user_script && _kiwi.user_script._dispose) + _kiwi.user_script._dispose(); + + // Create and run the new user script + _kiwi.user_script = new user_fn(); + + } catch (err) { + this.setStatus('Script error. ' + err.toString()); + return; + } + + // If we're this far, no errors occured. Save the user script + _kiwi.global.settings.set('user_script', this.editor.getValue()); + _kiwi.global.settings.save(); + + this.setStatus('Your script has been saved and is now active :)'); + }, + + + setStatus: function (status_text) { + var $status = this.$el.find('.toolbar .status'); + + status_text = status_text || ''; + $status.slideUp('fast', function() { + $status.text(status_text); + $status.slideDown(); + }); + } + }); + + + + var applet = Backbone.Model.extend({ + initialize: function () { + var that = this; + + this.set('title', 'Script Editor'); + this.view = new view({model: this}) + + } + }); + + + _kiwi.model.Applet.register('kiwi_script_editor', applet); + //_kiwi.model.Applet.loadOnce('kiwi_script_editor'); + })(); \ No newline at end of file diff --git a/client/assets/dev/build.js b/client/assets/dev/build.js index 9db3895..e856a85 100644 --- a/client/assets/dev/build.js +++ b/client/assets/dev/build.js @@ -46,6 +46,7 @@ var src = concat([ __dirname + '/applet_settings.js', __dirname + '/applet_nickserv.js', __dirname + '/applet_chanlist.js', + __dirname + '/applet_scripteditor.js', __dirname + '/utils.js', __dirname + '/view.js' diff --git a/client/assets/dev/index.html.tmpl b/client/assets/dev/index.html.tmpl index c1a85c2..efe8ea2 100644 --- a/client/assets/dev/index.html.tmpl +++ b/client/assets/dev/index.html.tmpl @@ -206,6 +206,22 @@ + + +