/
konsole.js
124 lines (98 loc) · 3.4 KB
/
konsole.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
/* global CloudCmd */
/* global Util */
/* global DOM */
/* global Console */
(function(CloudCmd, Util, DOM) {
'use strict';
CloudCmd.Konsole = ConsoleProto;
function ConsoleProto() {
var Name = 'Konsole',
TITLE = 'Console',
Element,
Loaded,
Images = DOM.Images,
Dialog = DOM.Dialog,
exec = Util.exec,
Konsole = this;
function init() {
Images.show.load('top');
Util.exec.series([
CloudCmd.View,
load,
create,
Konsole.show,
]);
Element = DOM.load({
name : 'div',
className : 'console'
});
}
this.hide = function() {
CloudCmd.View.hide();
};
this.clear = function() {
Console.clear();
};
function getPrefix() {
return CloudCmd.PREFIX + '/console';
}
function create(callback) {
var prefix = getPrefix(),
socketPath = CloudCmd.PREFIX;
Console(Element, prefix, socketPath, function(spawn) {
spawn.on('connect', exec.with(authCheck, spawn));
Util.exec(callback);
});
Console.addShortCuts({
'P': function() {
var command = Console.getPromptText(),
path = DOM.getCurrentDirPath();
command += path;
Console.setPromptText(command);
}
});
}
function authCheck(spawn) {
DOM.Files.get('config', function(error, config) {
if (error)
return Dialog.alert(TITLE, error);
if (config.auth) {
spawn.emit('auth', config.username, config.password);
spawn.on('reject', function() {
Dialog.alert(TITLE, 'Wrong credentials!');
});
}
});
}
this.show = function(callback) {
if (Loaded)
CloudCmd.View.show(Element, {
afterShow: function() {
Console.focus();
Util.exec(callback);
}
});
};
function load(callback) {
var prefix = getPrefix(),
url = prefix + '/console.js';
DOM.load.js(url, function(error) {
if (error) {
Dialog.alert(TITLE, error.message);
} else {
Loaded = true;
Util.timeEnd(Name + ' load');
Util.exec(callback);
}
});
Util.time(Name + ' load');
}
DOM.Files.get('config', function(error, config) {
if (error)
return Dialog.alert(TITLE, error);
if (!config.console)
return;
init();
});
}
})(CloudCmd, Util, DOM);