This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
324 lines (281 loc) · 10 KB
/
init.js
File metadata and controls
324 lines (281 loc) · 10 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//# sourceMappingURL=init.map
// Generated by CoffeeScript 1.10.0
/*
Copyright (c) 2013, dev-rke
*/
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
codiad.LessCompiler = (function() {
var ignoreSaveEvent, settings;
LessCompiler.instance = null;
settings = null;
ignoreSaveEvent = false;
/*
basic plugin environment initialization
*/
function LessCompiler(global, jQuery) {
this.showDialog = bind(this.showDialog, this);
this.getFileNameWithoutExtension = bind(this.getFileNameWithoutExtension, this);
this.getBaseDir = bind(this.getBaseDir, this);
this.saveFile = bind(this.saveFile, this);
this.compileLessAndSave = bind(this.compileLessAndSave, this);
this.addOpenHandler = bind(this.addOpenHandler, this);
this.addSaveHandler = bind(this.addSaveHandler, this);
this.preloadLibrariesAndSettings = bind(this.preloadLibrariesAndSettings, this);
this.init = bind(this.init, this);
this.codiad = global.codiad;
this.amplify = global.amplify;
this.jQuery = jQuery;
this.scripts = document.getElementsByTagName('script');
this.path = this.scripts[this.scripts.length - 1].src.split('?')[0];
this.curpath = this.path.split('/').slice(0, -1).join('/') + '/';
this.workspaceUrl = 'workspace/';
LessCompiler.instance = this;
this.jQuery((function(_this) {
return function() {
return _this.init();
};
})(this));
}
/*
main plugin initialization
*/
LessCompiler.prototype.init = function() {
this.preloadLibrariesAndSettings();
this.addSaveHandler();
return this.addOpenHandler();
};
/*
load less compiler and settings
*/
LessCompiler.prototype.preloadLibrariesAndSettings = function() {
if (typeof window.less === 'undefined') {
this.jQuery.loadScript(this.curpath + "less.min.js");
}
if (typeof window.sourceMap === 'undefined') {
this.jQuery.loadScript(this.curpath + "source-map-0.1.31.js");
}
this.jQuery.getJSON(this.curpath + "controller.php?action=load", (function(_this) {
return function(json) {
return _this.settings = json;
};
})(this));
return this.jQuery.getJSON(this.curpath + "controller.php?action=getWorkspaceUrl", (function(_this) {
return function(json) {
return _this.workspaceUrl = json.workspaceUrl;
};
})(this));
};
/*
Add new compiler procedure to save handler
*/
LessCompiler.prototype.addSaveHandler = function() {
return this.amplify.subscribe('active.onSave', (function(_this) {
return function() {
if (!_this.ignoreSaveEvent) {
return _this.compileLessAndSave();
}
};
})(this));
};
/*
Add hotkey binding for manual compiling
*/
LessCompiler.prototype.addOpenHandler = function() {
return this.amplify.subscribe('active.onOpen', (function(_this) {
return function() {
var editorInstance, manager;
editorInstance = _this.codiad.editor.getActive();
if (!editorInstance) {
return;
}
manager = editorInstance.commands;
return manager.addCommand({
name: "Compile Less",
bindKey: {
win: "Ctrl-Alt-Y",
mac: "Command-Alt-Y"
},
exec: function() {
return _this.compileLessAndSave();
}
});
};
})(this));
};
/*
compiles Less and saves it to the same name
with a different file extension
*/
LessCompiler.prototype.compileLessAndSave = function() {
var content, currentFile, error, exception, ext, fileName, options;
if (!this.settings.less.compile_less) {
return;
}
currentFile = this.codiad.active.getPath();
console.log(currentFile);
ext = this.codiad.filemanager.getExtension(currentFile);
if (ext.toLowerCase() === 'less') {
content = this.codiad.editor.getContent();
fileName = this.getFileNameWithoutExtension(currentFile);
options = this.settings.less;
options.filename = this.workspaceUrl + currentFile;
try {
window.lessoptions = options;
return less.render(content, options, (function(_this) {
return function(err, output) {
if (err) {
throw err;
}
_this.saveFile(fileName + "css", output.css);
return _this.codiad.message.success('Less compiled successfully.');
};
})(this));
} catch (error) {
exception = error;
return this.codiad.message.error('Less compilation failed: ' + exception);
}
}
};
/*
saves a file, creates one if it does not exist
*/
LessCompiler.prototype.saveFile = function(fileName, fileContent) {
var baseDir, error, exception, instance;
try {
if (instance = this.codiad.active.sessions[fileName]) {
instance.setValue(fileContent);
this.ignoreSaveEvent = true;
this.codiad.active.save(fileName);
this.ignoreSaveEvent = false;
return;
}
baseDir = this.getBaseDir(fileName);
if (!this.codiad.filemanager.getType(fileName)) {
this.jQuery.ajax({
url: this.codiad.filemanager.controller + '?action=create&path=' + fileName + '&type=file',
success: (function(_this) {
return function(data) {
var createResponse, shortName;
createResponse = _this.codiad.jsend.parse(data);
if (createResponse !== 'error') {
shortName = _this.codiad.filemanager.getShortName(fileName);
_this.codiad.filemanager.createObject(baseDir, fileName, 'file');
return _this.amplify.publish('filemanager.onCreate', {
createPath: fileName,
path: baseDir,
shortName: shortName,
type: 'file'
});
}
};
})(this),
async: false
});
}
return this.codiad.filemanager.saveFile(fileName, fileContent, {
error: (function(_this) {
return function() {
return _this.codiad.message.error('Cannot save file.');
};
})(this)
});
} catch (error) {
exception = error;
return this.codiad.message.error('Cannot save file: ' + exception);
}
};
/*
Get base dir of a path
*/
LessCompiler.prototype.getBaseDir = function(filepath) {
return filepath.substring(0, filepath.lastIndexOf("/"));
};
/*
Get filename without file extension of a file
*/
LessCompiler.prototype.getFileNameWithoutExtension = function(filepath) {
return filepath.substr(0, filepath.lastIndexOf(".") + 1);
};
/*
shows settings dialog
*/
LessCompiler.prototype.showDialog = function() {
var generateCheckbox, html, label, lessLabels, lessRules, name, value;
generateCheckbox = (function(_this) {
return function(name, label, enabled, title) {
if (enabled == null) {
enabled = false;
}
if (title == null) {
title = "";
}
return " <input type=\"checkbox\" id=\"" + name + "\" " + (enabled ? 'checked="checked"' : void 0) + " />\n<label for=\"" + name + "\" title=\"" + title + "\">" + label + "</label><br />";
};
})(this);
lessLabels = {
'compile_less': 'Compile Less on save',
'compress': 'Compress css',
'ieCompat': 'enable Internet Explorer Compatibility Mode'
};
lessRules = (function() {
var ref, results;
ref = this.settings.less;
results = [];
for (name in ref) {
value = ref[name];
label = lessLabels[name];
if (!label) {
continue;
}
results.push(generateCheckbox(name, label, value));
}
return results;
}).call(this);
html = "<div id=\"less-settings\">\n <h2>Less Compiler Settings</h2>\n <div id=\"less-container\">\n " + (lessRules.join('')) + "\n </div>\n <button id=\"modal_close\">Save Settings</button>\n </div>";
this.jQuery('#modal-content').append(this.jQuery(html));
this.jQuery('#modal').show().draggable({
handle: '#drag-handle'
});
settings = this.settings;
this.jQuery('#modal-content').on('click', 'input', (function(_this) {
return function(target) {
var isActive;
name = $(target.currentTarget).attr('id');
isActive = $(target.currentTarget).prop('checked');
if (name in settings.less) {
settings.less[name] = isActive;
}
return true;
};
})(this));
return this.jQuery('#modal_close').on('click', (function(_this) {
return function() {
var json;
_this.codiad.modal.unload();
_this.jQuery('#modal-content').off();
_this.settings = settings;
json = JSON.stringify(settings);
return _this.jQuery.post(_this.curpath + "controller.php?action=save", {
settings: json
}, function(data) {
json = JSON.parse(data);
if (json.status === "error") {
return _this.codiad.message.error(json.message);
} else {
return _this.codiad.message.success(json.message);
}
});
};
})(this));
};
/*
Static wrapper to call showDialog outside of the object
*/
LessCompiler.showDialogWrapper = function() {
return LessCompiler.instance.showDialog();
};
return LessCompiler;
})();
new codiad.LessCompiler(this, jQuery);
}).call(this);