};\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
--- /dev/null
+ (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
__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
</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
\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
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
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
}\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