2 var view
= Backbone
.View
.extend({
4 'click .btn_save': 'onSave'
7 initialize: function (options
) {
9 this.$el
= $($('#tmpl_script_editor').html().trim());
11 this.model
.on('applet_loaded', function () {
12 that
.$el
.parent().css('height', '100%');
13 $script(_kiwi
.app
.get('base_path') + '/assets/libs/ace/ace.js', function (){ that
.createAce(); });
18 createAce: function () {
19 var editor_id
= 'editor_' + Math
.floor(Math
.random()*10000000).toString();
20 this.editor_id
= editor_id
;
22 this.$el
.find('.editor').attr('id', editor_id
);
24 this.editor
= ace
.edit(editor_id
);
25 this.editor
.setTheme("ace/theme/monokai");
26 this.editor
.getSession().setMode("ace/mode/javascript");
28 var script_content
= _kiwi
.global
.settings
.get('user_script') || '';
29 this.editor
.setValue(script_content
);
33 onSave: function (event
) {
34 var script_content
, user_fn
;
36 // Build the user script up with some pre-defined components
37 script_content
= 'var network = kiwi.components.Network();\n';
38 script_content
+= 'var input = kiwi.components.ControlInput();\n';
39 script_content
+= this.editor
.getValue() + '\n';
41 // Add a dispose method to the user script for cleaning up
42 script_content
+= 'this._dispose = function(){ network.off(); if(this.dispose) this.dispose(); }';
44 // Try to compile the user script
46 user_fn
= new Function(script_content
);
48 // Dispose any existing user script
49 if (_kiwi
.user_script
&& _kiwi
.user_script
._dispose
)
50 _kiwi
.user_script
._dispose();
52 // Create and run the new user script
53 _kiwi
.user_script
= new user_fn();
56 this.setStatus('Script error. ' + err
.toString());
60 // If we're this far, no errors occured. Save the user script
61 _kiwi
.global
.settings
.set('user_script', this.editor
.getValue());
62 _kiwi
.global
.settings
.save();
64 this.setStatus('Your script has been saved and is now active :)');
68 setStatus: function (status_text
) {
69 var $status
= this.$el
.find('.toolbar .status');
71 status_text
= status_text
|| '';
72 $status
.slideUp('fast', function() {
73 $status
.text(status_text
);
81 var applet
= Backbone
.Model
.extend({
82 initialize: function () {
85 this.set('title', 'Script Editor');
86 this.view
= new view({model
: this})
92 _kiwi
.model
.Applet
.register('kiwi_script_editor', applet
);
93 //_kiwi.model.Applet.loadOnce('kiwi_script_editor');