This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequireeditor.js
324 lines (261 loc) · 10.6 KB
/
requireeditor.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
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
// Copyright (c) 2014 M-Way Solutions GmbH
// https://github.com/mwaylabs/brackets-quick-require/blob/master/LICENCE
define(function (require, exports, module) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash");
var EditorManager = brackets.getModule("editor/EditorManager");
var Dialogs = brackets.getModule("widgets/Dialogs");
var Strings = require("strings");
var requireEditorTemplate = require("text!html/requireeditor.html");
var moduleNameList = require("text!assets/moduleList.json");
var requireNpmbridge = require("npmbridge");
var animator = require("animator/example");
var INDICATOR_ID = 'install-npm-module';
var INDICATOR_ID2 = 'installing-busy';
var parsedModuleList = null;
var searchInEntireWord = false;
var StatusBar = brackets.getModule("widgets/StatusBar");
var quickrequire = require("quickrequire");
var select2 = require("select2");
var currentTimestamp = null;
var selectedModulName = null;
var hideSaveFlag = false;
/**
* Creates a new RequireEditor,
* appends the html to the Editor
*
* @Constructor
* @param {Array} $parent editor
* @param {String} moduleName
*/
function RequireEditor(opt) {
if(!opt)
throw new Error('Options are not defined!');
hideSaveFlag = opt.hideSaveFlag || false;
var $parent = opt.$parent
var moduleName = opt.moduleName;
npmInstall = typeof opt.npmInstall === 'function' ? opt.npmInstall : npmInstall;
this.timestamp = new Date().getTime();
if (!$parent)
throw new Error('$parent is not defined');
if(!parsedModuleList) {
parsedModuleList = JSON.parse(moduleNameList);
}
if (moduleName || moduleName === '') {
var matches = this.filterModules(moduleName);
var templateVars = {
Strings: Strings,
matches: matches,
timestamp: this.timestamp,
hideSaveFlag: hideSaveFlag
};
var template = _.template(requireEditorTemplate, templateVars);
this.parentElement = $parent;
this.$element = $(template);
this.parentElement.append(this.$element);
this.parentElement.data('timestamp', this.timestamp);
if (moduleName !== '') {
registerClickEvent(this.$element);
}
this.setTooltipListener();
} else {
throw new Error('moduleName is not defined');
}
}
RequireEditor.prototype.setTooltipListener = function () {
setTimeout(function () {
// Configure twipsy
var options = {
placement: "above",
trigger: "hover",
title: function () {
return Strings.SAVE_IN_PACKAGE_JSON_TOOLTIP;
}
};
// Show the twipsy with the explanation
$(this.$element).find('#flagTooltip').twipsy(options);
}, 1000);
};
RequireEditor.prototype.getRootElement = function () {
return this.$element;
};
/**
* Updates (replaces) the module-list shown in the inline-editor
*
* @param {String} moduleName
*/
RequireEditor.prototype.updateList = function (moduleName) {
//this.$element
searchInEntireWord = $(this.$element).find('#search-algo').is(':checked');
var that = this;
var matches = this.filterModules(moduleName);
var templateVars = {
Strings: Strings,
matches: matches,
hideSaveFlag: hideSaveFlag
};
// Limit the amount of shown items
if (matches.aaData.length > 200) {
matches.aaData.splice(200, matches.aaData.length - 1);
}
var template = _.template(requireEditorTemplate, templateVars);
var $element = $(template);
unregisterEvent(this.$element);
$(this.$element).replaceWith($element);
this.$element = $element;
registerClickEvent(this.$element);
//$('.require-editor').replaceWith($element);
var $searchAlgoCheckbox = $(this.$element).find('#search-algo');
$searchAlgoCheckbox.on('change', function () {
that.updateList(moduleName);
});
if (searchInEntireWord) {
$searchAlgoCheckbox.prop('checked', true);
}
this.setTooltipListener();
};
function npmInstall(opt) {
// open the waiting dialog
quickrequire.openNpmInstallDialog(currentTimestamp);
// open socket.io connection
quickrequire.openSocketIoConnection(currentTimestamp);
//run npm install with the selectedModulName
requireNpmbridge.callNpmInstall(opt, opt.save, notifyUserCallback);
}
function npmInstallCallback() {
var $parentInlineEditor = $(this).parents('.inline-widget');
currentTimestamp = $parentInlineEditor.data('timestamp');
var savePackage = $(this).parents('.inline-widget').find('#save-package').is(':checked');
selectedModulName = _getClickedModuleName(this);
var selectedVersion = _getSelectedVersion(this);
npmInstall({
save: savePackage,
timestamp: currentTimestamp,
module: selectedModulName,
version: selectedVersion
});
}
function registerClickEvent($element) {
$($element).find('.install-module-btn').on('click', npmInstallCallback);
$($element).find('.ext-version .select-version').select2();
}
function unregisterEvent($element) {
$($element).find('.install-module-btn').off('click');
}
function _getClickedModuleName(clickedEl) {
return $(clickedEl.parentElement.parentElement).find('.ext-name').html();
}
function _getSelectedVersion(clickedEl) {
return $(clickedEl.parentElement.parentElement).find('select').val();
}
function _showErrorMsg(err) {
var $modalHtml = $('.npm-install-dialog .modal-body');
var errorContentDialog = '<div class="status error"><p>' + Strings.NOTIFICATON_ERROR_TITLE + ': ' + Strings.NOTIFICATON_ERROR_MESSAGE_PAST + ' ' + Strings.NOTIFICATION_ERROR_DURING_NPMINSTALL + '</p><p> ' + err.errno + ': ' + err.code + ' </p> </div>'
$modalHtml.html(errorContentDialog);
$modalHtml.parent().find('.primary').remove();
}
function _showErrorTwipsy($tempTwipsyDiv) {
var templateContent = '<div class="tooltip-arrow"></div><div class="tooltip-innerQuickRequire">' + Strings.NOTIFICATON_ERROR_TITLE + ': ' + Strings.NOTIFICATON_ERROR_MESSAGE_PAST + '</div>';
var options = {
placement: "above",
trigger: "manual",
autoHideDelay: 3000,
template: function () {
return templateContent;
}
};
//Show twipsy with errormessage
$tempTwipsyDiv.data('twipsy', null);
$tempTwipsyDiv.twipsy(options).twipsy("show");
}
function _closeInstallDialog() {
// Close the shown "openNpmInstallDialog-dialog"
Dialogs.cancelModalDialogIfOpen('npm-install-dialog');
$(document).find('.modal-wrapper').remove();
}
/**
* give user feedback whether module-installation
* was successfull or it has failed
*/
var notifyUserCallback = function (err, data) {
var $tempTwipsyDiv = $('#install-npm-module');
var templateContent = null;
if (err) {
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-errors", err);
StatusBar.hideBusyIndicator(INDICATOR_ID2);
_showErrorMsg(err);
_showErrorTwipsy($tempTwipsyDiv);
return;
} else {
_closeInstallDialog();
quickrequire.removeAndCloseByTimestamp(currentTimestamp);
if (data) {
var installedModuleName = data[data.length - 1][0];
quickrequire.completeLogger(data);
templateContent = '<div class="tooltip-arrow"></div><div class="tooltip-innerQuickRequire">' + installedModuleName + ' ' + Strings.NOTIFICATON_INSTALL_NPMMODULE_END + '</div>';
//configure twipsy
var options = {
placement: "above",
trigger: "manual",
autoHideDelay: 3000,
template: function () {
return templateContent;
}
};
$tempTwipsyDiv.data('twipsy', null);
$tempTwipsyDiv.twipsy(options).twipsy('show');
//Trigger the success-event
$(document).trigger('quickrequire-npm-installed', [data, selectedModulName, selectedModulName]);
StatusBar.updateIndicator(INDICATOR_ID, true, "inspection-valid", installedModuleName + ' ' + Strings.NOTIFICATON_INSTALL_NPMMODULE_END);
animator.show();
}
$(document).undelegate('.install-module-btn', 'click');
}
StatusBar.hideBusyIndicator(INDICATOR_ID2);
};
/**
* returns a array with the matching modules
*
* @param {Object} module (requireEditor)
*/
RequireEditor.prototype.filterModules = function (module) {
var array = {};
if (module.length <= 0) {
array.aaData = [];
array.initial = true;
return array;
}
var matches = [];
if (!searchInEntireWord) {
_.each(parsedModuleList['rows'], function (element) {
var index = element[0].indexOf(module);
if (index === 0) {
if (element [1] && element[1].length > 53) {
element[1] = element[1].slice(0, 50);
element[1] = element[1] + '...';
}
matches.push(element);
}
});
} else {
matches = _.filter(parsedModuleList['rows'], function (element) {
var a = element[0].search(module);
/*var b = element[1].search(module);*/
if (a >= 0 /*|| b >= 0*/) {
if (element [1] && element[1].length > 53) {
element[1] = element[1].slice(0, 50);
element[1] = element[1] + '...';
}
return element;
}
});
}
array = {
aaData: matches
};
return array;
};
RequireEditor.prototype.setListeners = function () {
};
exports.RequireEditor = RequireEditor;
});