-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcodebot.js
152 lines (123 loc) · 5.01 KB
/
codebot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
The MIT License (MIT)
Copyright (c) 2013 Fernando Bevilacqua
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var CODEBOT = new function() {
var mShortcuts = null;
var mUI = null;
var mIO = null;
var mEditors = null;
var mSettings = null;
var mSignals = null;
var mJobs = null;
var mPlugins = null;
var mSelf;
// Contain configuration parameters loaded from the app.json file. This file
// is loaded before everything and it can guide how Codebot should init and
// bootstrap itself.
this.STATIC_APP_CONFIG = {};
/**
* Get config params in the "codebot" section of the content loaded with the app.json file.
*
* @param {string} thePropertyName Name of the config property being read.
* @param {[type]} theDefaultValue If the informed property does not exist in the config content, return this value instead.
*/
this.config = function(thePropertyName, theDefaultValue) {
var aEntries = this.STATIC_APP_CONFIG && this.STATIC_APP_CONFIG['codebot'] ? this.STATIC_APP_CONFIG['codebot'] : {};
return (thePropertyName in aEntries) ? aEntries[thePropertyName] : theDefaultValue;
};
var handleError = function(theMsg, theUrl, theLineNumber) {
// Show some nice information to the user
mUI.toast(Codebot.UI.TOAST_ERROR, '<h2>An error just occured</h2><p>' + theMsg + '</p>');
// Log the error and run in circles \o/
console.error("Error occured: " + theMsg);
return false;
};
this.loadScript = function(thePath) {
console.log('CODEBOT [core] Loading script: ' + thePath);
$('body').append('<script type="text/javascript" src="' + thePath + '"></script>');
};
this.loadStyle = function(thePath) {
console.log('CODEBOT [core] Loading style: ' + thePath);
$('body').append('<link href="' + thePath + '" rel="stylesheet" type="text/css">');
};
this.writeTabToDisk = function(theTab) {
var aEditor = theTab.editor;
if(aEditor) {
mIO.writeFile(theTab.node, aEditor.getContent(), function() { theTab.setDirty(false); console.log('Tab data written to disk!');} );
}
};
this.showDebugger= function() {
if(typeof(require) == 'function') {
var aGui = require('nw.gui');
var aWin = aGui.Window.get();
aWin.showDevTools();
}
};
this.setIODriver = function(theIODriver) {
mIO = theIODriver;
console.log('CODEBOT [IO driver] ' + mIO.driver);
mIO.init();
};
this.setLoadingScreenVisibility = function(theStatus) {
if(theStatus) {
$('#loading').fadeIn();
$('#wrapper').hide();
} else {
$('#loading').fadeOut();
$('#wrapper').show();
}
};
this.init = function(theIODriver) {
console.log('CODEBOT [core] Initializing...');
mSelf = this;
// Make Codebot catch all erros.
window.onerror = handleError;
mSelf.setIODriver(theIODriver || new CodebotIO());
mJobs = new CodebotJobs();
mEditors = new Codebot.Editors();
mShortcuts = new Codebot.Shortcuts();
mUI = new CodebotUI();
mSettings = new Codebot.Settings();
mSignals = new CodebotSignals();
mPlugins = new Codebot.Plugins();
mJobs.init();
mSettings.init(mSelf);
mSettings.load(function() {
mEditors.init(mSelf);
mUI.init(mSelf);
mPlugins.init(mSelf);
mPlugins.load();
mShortcuts.init(mSelf);
console.log('CODEBOT [core] Done, ready to rock!');
mSignals.ready.dispatch();
if(!mSelf.config('keepLoadingScreen', false)) {
mSelf.setLoadingScreenVisibility(false);
}
mSelf.showDebugger();
});
};
// getters
this.__defineGetter__("editors", function() { return mEditors; });
this.__defineGetter__("ui", function() { return mUI; });
this.__defineGetter__("io", function() { return mIO; });
this.__defineGetter__("settings", function() { return mSettings; });
this.__defineGetter__("shortcuts", function() { return mShortcuts; });
this.__defineGetter__("signals", function() { return mSignals; });
this.__defineGetter__("jobs", function() { return mJobs; });
this.__defineGetter__("plugins", function() { return mPlugins; });
};