-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_host.js
173 lines (161 loc) · 4.14 KB
/
plugin_host.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
// interface for plugin
function PluginHost(core, options) {
//private members
var m_options = options || {};
var m_debug = core.debug;
var m_plugins = [];
var m_loaded_scripts = {};
var query = {};//GetQueryString();
//private functions
function handle_command(cmd, callback, ext) {
}
//public members / functions
var self = {
get_plugin: function(name) {
for (var i = 0; i < m_plugins.length; i++) {
if (name == m_plugins[i].name) {
return m_plugins[i];
}
}
return null;
},
send_command: function(cmd, callback, ext) {
for (var i = 0; i < m_plugins.length; i++) {
if (m_plugins[i].command_handler) {
m_plugins[i].command_handler(cmd, callback, ext);
}
}
handle_command(cmd, callback, ext);
},
send_event: function(sender, event, ext) {
for (var i = 0; i < m_plugins.length; i++) {
if (m_plugins[i].event_handler) {
m_plugins[i].event_handler(sender, event, ext);
}
}
},
add_plugin_from_script: function(name, options, chunk_array, callback) {
var script = document
.createElement('script');
script.onload = function() {
console.log("loaded : " + name);
if (create_plugin) {
var plugin = create_plugin(self);
m_plugins.push(plugin);
create_plugin = null;
}
if (callback) {
callback();
}
};
console.log("loding : " + name);
var blob = new Blob(chunk_array, {
type: "text/javascript"
});
if(1){
//for debug ability
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
script.src = reader.result;
document.head.appendChild(script);
};
}else{
script.src = url.createObjectURL(blob);
}
},
init_plugins: function() {
return new Promise((fullfill,reject) => {
if (!m_options.plugin_paths || m_options.plugin_paths.length == 0) {
fullfill();
return;
}
function load_plugin(idx) {
self.getFileUrl(m_options.plugin_paths[idx], function(
url) {
var script = document
.createElement('script');
script.onload = function() {
console.log("loaded : " +
m_options.plugin_paths[idx]);
if (create_plugin) {
var plugin = create_plugin(self);
m_plugins.push(plugin);
create_plugin = null;
}
if (idx + 1 < m_options.plugin_paths.length) {
load_plugin(idx + 1);
} else {
for (var i = 0; i < m_plugins.length; i++) {
if (m_plugins[i].init_options) {
m_plugins[i].init_options(m_options[m_plugins[i].name] || {});
}
}
fullfill();
}
};
console.log("loding : " +
m_options.plugin_paths[idx]);
script.src = url;
document.head.appendChild(script);
});
}
load_plugin(0);
});
},
log: function(str, level) {
if (level && level <= m_debug) {
console.log(str);
}
},
getFile: function(path, callback) {
var req = new XMLHttpRequest();
req.responseType = "arraybuffer";
req.open("get", path, true);
req.send(null);
req.onload = function() {
callback([new Uint8Array(req.response)]);
}
},
getFileUrl: function(path, callback) {
callback(path);
},
loadScript: (path) => {
return new Promise((resolve, reject) => {
if(m_loaded_scripts[path]){
console.log("already loaded : ", path);
resolve();
}else{
self.getFileUrl(path, function(url) {
var script = document
.createElement('script');
script.onload = resolve;
script.src = url;
m_loaded_scripts[path] = script;
document.head.appendChild(script);
});
}
});
},
refresh_app_menu: function() {
for (var i = 0; i < m_plugins.length; i++) {
if (m_plugins[i].on_refresh_app_menu) {
m_plugins[i].on_refresh_app_menu(app.menu);
}
}
},
restore_app_menu: function() {
app.menu.setMenuPage("menu.html", {
callback: function() {
for (var i = 0; i < m_plugins.length; i++) {
if (m_plugins[i].on_restore_app_menu) {
m_plugins[i].on_restore_app_menu(app.menu);
}
}
self.refresh_app_menu();
}
});
},
};
return self;
};