-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcomponent.js
143 lines (113 loc) · 4.28 KB
/
component.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
Polymer({
is: 'web-cli',
properties: {
pluginsUrl: String
},
ready: function () {
var self = this;
console.log(this.localName + '#' + this.id + ' ready called');
(function () {
var cliInitialized = false;
var bridgeEvents = {};
function emitBridgeEvent(event, data) {
$(bridgeEvents).trigger(event, data);
}
var pluginsUrl = self.getAttribute('pluginsUrl'),
plugins = [],
cli;
// disable global ajax caching
$.ajaxSetup({ cache: false });
// invoked by the client to initialize the console
function init(opts) {
if (cliInitialized) return console.warn('cli already initialized');
opts = opts || {};
opts.plugins = opts.plugins || [];
addPlugins(opts.plugins);
console.log('initializing console');
getPlugins(function (err) {
if (err) return console.error('error loading plugins', err);
opts.plugins = plugins;
opts.welcomeMessage = opts.welcomeMessage || "Welcome! Type <b>help</b> to start exploring the commands currently supported!";
cli = $(self).cli(opts);
// get environment events broker
var ebEnv = cli.cli('envEventsBroker');
// bind to environment change events
// proxy internal cli state change to client envChanged listener
$(ebEnv).bind({
state: function (e, state) {
emitBridgeEvent('env', state);
}
});
$(bridgeEvents).bind({
env: function (e, data) {
self.fire('envChanged', data);
}
});
cliInitialized = true;
});
function addPlugins(plgins) {
for (var i = 0; i < plgins.length; i++) {
var api = plgins[i];
if (api.console && api.console.autoLoad) {
plugins.push({ url: api.route + '/!!' });
}
}
}
function getPlugins(cb) {
if (!pluginsUrl) return cb();
getJson(pluginsUrl, function (err, response) {
if (err) return cb(err);
addPlugins(response.apis);
return cb();
});
}
}
function getJson(url, cb) {
console.log('getting json from url:', url);
$.getJSON(url, function (data, success, xhr) {
if (xhr.status != 200) return cb(new Error('got status code:' + xhr.status + '; response:' + xhr.responseText));
if (!data) return cb(new Error('response without data: status:' + xhr.statusText));
return cb(null, data);
})
.fail(function (xhr) {
return cb(new Error('error: status code:' + xhr.status + ' status text:' + xhr.statusText + ' response text:' + xhr.responseText));
});
}
// proxy method for accessing cli env
function cli_env(setting, val, appOriginated, setEmpty) {
return cli.cli('env', setting, val, appOriginated, setEmpty);
}
// proxy method for accessing cli prompt
function cli_prompt(val) {
return cli.cli('prompt', val);
}
// proxy to the cli's addPluginHandler method
function addPluginHandler(name, handler) {
cli.cli('addPluginHandler', name, handler);
}
// proxy to the cli's loadCss method
function loadCss(url) {
cli.cli('loadCss', url);
}
// proxy to the cli's addCss method
function addCss(css) {
cli.cli('addCss', css);
}
self.init = init;
self.env = cli_env;
self.prompt = cli_prompt;
self.addPluginHandler = addPluginHandler;
self.loadCss = loadCss;
self.addCss = addCss;
var interval = setInterval(function () {
console.log('checking onReady handler');
if (typeof self.onReady === 'function') {
console.log('running onReady callback');
clearInterval(interval);
return self.onReady();
};
}, 100);
})();
console.log(self.localName + '#' + self.id + ' ready ended');
}
});