Client user script editor
authorDarren <darren@Darrens-MacBook-Pro.local>
Tue, 5 Mar 2013 15:52:17 +0000 (15:52 +0000)
committerDarren <darren@Darrens-MacBook-Pro.local>
Tue, 5 Mar 2013 15:52:17 +0000 (15:52 +0000)
client/assets/dev/app.js
client/assets/dev/applet_scripteditor.js [new file with mode: 0644]
client/assets/dev/build.js
client/assets/dev/index.html.tmpl
client/assets/dev/model_application.js
client/assets/dev/model_gateway.js
client/assets/dev/view.js

index c6d2835ba962a6a1fe01c7d8f01292503129706d..ee1ff68b85ac82be4ea6ec9f9012f2f35561a514 100644 (file)
@@ -52,7 +52,10 @@ _kiwi.global = {
                };\r
 \r
                _.each(funcs, function(gateway_fn, func_name) {\r
-                       obj[func_name] = _kiwi.gateway[gateway_fn];\r
+                       obj[func_name] = function() {\r
+                               var fn_name = gateway_fn;\r
+                               _kiwi.gateway[fn_name].apply(_kiwi.gateway, arguments);\r
+                       };\r
                });\r
 \r
                return obj;\r
diff --git a/client/assets/dev/applet_scripteditor.js b/client/assets/dev/applet_scripteditor.js
new file mode 100644 (file)
index 0000000..8512129
--- /dev/null
@@ -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
index 9db3895fe1cb1242851e4ec34562f3c77f0741a2..e856a85d8401014dd471e89d123e7bd804c3793d 100644 (file)
@@ -46,6 +46,7 @@ var src = concat([
     __dirname + '/applet_settings.js',\r
     __dirname + '/applet_nickserv.js',\r
     __dirname + '/applet_chanlist.js',\r
+    __dirname + '/applet_scripteditor.js',\r
 \r
     __dirname + '/utils.js',\r
     __dirname + '/view.js'\r
index c1a85c241b22d4d96b45eb39e7dbe35b7cb2742a..efe8ea240236bdd6b37481dd9da831e511b870c6 100644 (file)
     </script>\r
 \r
 \r
+    <script type="text/html" id="tmpl_script_editor">\r
+        <div style="height:100%;">\r
+            <style>\r
+                #kiwi .script_editor .toolbar { padding: 10px 20px; }\r
+                #kiwi .script_editor .toolbar span.status { margin-left:2em; font-style:italic; }\r
+                #kiwi .script_editor .toolbar button { height:30px; padding:0 1em; }\r
+                #kiwi .script_editor .toolbar button i { font-size:1.2em; margin-left:3px; }\r
+            </style>\r
+            <div class="script_editor" style="height:100%; position:relative;">\r
+                <div class="toolbar"><button class="btn_save">Save <i class="icon-save"></i></button><span class="status"></span></div>\r
+                <div class="editor" style="position:absolute;top:50px;bottom:0px;left:0px;right:0px;"></div>\r
+            </div>\r
+        </div>\r
+    </script>\r
+\r
+\r
 <script>\r
 /* Script loader (https://github.com/ded/script.js) */\r
 (function(a,b,c){typeof c["module"]!="undefined"&&c.module.exports?c.module.exports=b():typeof c["define"]!="undefined"&&c["define"]=="function"&&c.define.amd?define(a,b):c[a]=b()})("$script",function(){function p(a,b){for(var c=0,d=a.length;c<d;++c)if(!b(a[c]))return j;return 1}function q(a,b){p(a,function(a){return!b(a)})}function r(a,b,i){function o(a){return a.call?a():d[a]}function t(){if(!--n){d[m]=1,l&&l();for(var a in f)p(a.split("|"),o)&&!q(f[a],o)&&(f[a]=[])}}a=a[k]?a:[a];var j=b&&b.call,l=j?b:i,m=j?a.join(""):b,n=a.length;return setTimeout(function(){q(a,function(a){if(h[a])return m&&(e[m]=1),h[a]==2&&t();h[a]=1,m&&(e[m]=1),s(!c.test(a)&&g?g+a+".js":a,t)})},0),r}function s(c,d){var e=a.createElement("script"),f=j;e.onload=e.onerror=e[o]=function(){if(e[m]&&!/^c|loade/.test(e[m])||f)return;e.onload=e[o]=null,f=1,h[c]=2,d()},e.async=1,e.src=c,b.insertBefore(e,b.firstChild)}var a=document,b=a.getElementsByTagName("head")[0],c=/^https?:\/\//,d={},e={},f={},g,h={},i="string",j=!1,k="push",l="DOMContentLoaded",m="readyState",n="addEventListener",o="onreadystatechange";return!a[m]&&a[n]&&(a[n](l,function t(){a.removeEventListener(l,t,j),a[m]="complete"},j),a[m]="loading"),r.get=s,r.order=function(a,b,c){(function d(e){e=a.shift(),a.length?r(e,d):r(e,b,c)})()},r.path=function(a){g=a},r.ready=function(a,b,c){a=a[k]?a:[a];var e=[];return!q(a,function(a){d[a]||e[k](a)})&&p(a,function(a){return d[a]})?b():!function(a){f[a]=f[a]||[],f[a][k](b),c&&c(e)}(a.join("|")),r},r},this)\r
                 [\r
                     'dev/applet_settings.js',\r
                     'dev/applet_nickserv.js',\r
-                    'dev/applet_chanlist.js'\r
+                    'dev/applet_chanlist.js',\r
+                    'dev/applet_scripteditor.js'\r
                 ],\r
 \r
                 [\r
index c6400ba781daef911c8a0f457551d20694db0725..67bf66e58a0c5d0500b0213919308d20fe54205b 100644 (file)
@@ -908,6 +908,7 @@ _kiwi.model.Application = function () {
 \r
             controlbox.on('command:applet', appletCommand);\r
             controlbox.on('command:settings', settingsCommand);\r
+            controlbox.on('command:script', scriptCommand);\r
         };\r
 \r
         // A fallback action. Send a raw command to the server\r
@@ -1067,6 +1068,11 @@ _kiwi.model.Application = function () {
             settings.view.show();\r
         }\r
 \r
+        function scriptCommand (ev) {\r
+            var editor = _kiwi.model.Applet.loadOnce('kiwi_script_editor');\r
+            editor.view.show();\r
+        }\r
+\r
         function appletCommand (ev) {\r
             if (!ev.params[0]) return;\r
 \r
index 61c6a45c6b6105914cd240fa24284af2d1d0b228..31766ea9e2999be4a02859f7a33dfbe43262e950 100644 (file)
@@ -111,6 +111,13 @@ _kiwi.model.Gateway = function () {
             that.trigger('topic:' + event.channel, event);\r
             that.trigger('topic', event);\r
         });\r
+\r
+\r
+        this.on('onjoin', function (event) {\r
+            that.trigger('join:' + event.channel, event);\r
+            that.trigger('join', event);\r
+        });\r
+\r
     };\r
 \r
 \r
index dc7e5540a0ccbc077e55ffcbf8bb66c5873d66d2..0e20aec8ef9c6c2f19641bfe1d2421cc0120205f 100644 (file)
@@ -1131,7 +1131,7 @@ _kiwi.view.Application = Backbone.View.extend({
         }\r
 \r
         // If we're typing into an input box somewhere, ignore\r
-        if ((ev.target.tagName.toLowerCase() === 'input') || $(ev.target).attr('contenteditable')) {\r
+        if ((ev.target.tagName.toLowerCase() === 'input') || (ev.target.tagName.toLowerCase() === 'textarea') || $(ev.target).attr('contenteditable')) {\r
             return;\r
         }\r
 \r