-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path$Interface.mbm
More file actions
265 lines (230 loc) · 8.93 KB
/
$Interface.mbm
File metadata and controls
265 lines (230 loc) · 8.93 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
/*
* Process module for JsMobileBasic.
* Help: https://vk.com/page-123026568_53604956
* Author: PROPHESSOR
* Supports:
* JsMB-interpreter & nw.js nw.gui [+]
* Node [-]
* JsOS [-]
* Browser pseudo-fs [-]
*/
'use strict';
/** Модуль для работы с интерфейсом
* Поддерживает только JsMB-Interpreter и NW.JS
* @module $Interface
* @author PROPHESOR
* @namespace $Interface
*/
{
const $Interface = {
'$Menu': {'$Bar': {}},
// Контекстное меню
/** Добавить пункт в контекстное меню
* @param {string} name - ID название меню
* @param {string} title - Текст пункта меню
* @param {function} onClick - Функция выполнится при нажатии на пункт меню
* @param {string} [type=undefined] - Тип меню (subMenu, checkbox, separator)
* @param {string} [fortype=undefined] - Родительское меню для subMenu
* @returns {this}
*/
menuAdd(name, title, onClick, type, fortype) {
if (this.$NW) {
if (this.$Menu[name] == undefined) {
this.$Menu[name] = new this.$NW.Menu();
}
switch (type) {
case undefined:
this.$Menu[name].append(new this.$NW.MenuItem({
'label': title,
'click': onClick
}));
break;
case 'subMenu':
this.$Menu[name].append(new this.$NW.MenuItem({
'label': title,
'submenu': fortype
}));
break;
case 'checkbox':
this.$Menu[name].append(new this.$NW.MenuItem({
'label': title,
'type': 'checkbox',
'click': onClick
}));
break;
case 'separator':
this.$Menu[name].append(new this.$NW.MenuItem({'type': 'separator'}));
break;
default:
console.warn('Неверный тип меню!');
break;
}
return this;
}
this.debug('Создание меню невозможно!');
return false;
},
/** Отобразить контекстное меню
* @param {string} name - ID название меню
* @param {number} x - XY координата
* @param {number} y - XY координата
* @returns {this}
*/
menuShow(name, x, y) {
if (this.$NW) {
this.$Menu[name].popup(x, y);
return this;
}
this.debug('Отображение меню невозможно!');
return false;
},
/** Добавляет разделитель в меню
* @param {string} name - ID название меню
* @returns {this}
*/
menuAddSeparator(name) {
if (this.$NW) {
if (this.$Menu[name] == undefined) {
this.$Menu[name] = new this.$NW.Menu();
}
this.$Menu[name].append(new this.$NW.MenuItem({'type': 'separator'}));
return this;
}
this.debug('Создание меню невозможно!');
return false;
},
// Menu bar
/** Добавляет меню в меню-бар
* @param {string} name - ID название меню-бара
* @param {string} title - Заголовок меню
* @param {string} subMenu - ID название меню
* @returns {this}
*/
menuBarAdd(name, title, subMenu) {
if (this.$NW) {
if (this.$Menu.$Bar[name] == undefined) {
this.$Menu.$Bar[name] = new this.$NW.Menu({
'type': 'menubar',
title
});
}
this.$Menu.$Bar[name].append(new this.$NW.MenuItem({
'label': title,
'submenu': this.$Menu[subMenu]
}));
return this;
}
this.debug('Создание меню невозможно!');
return false;
},
/** Отобразить меню-бар на экране
* @param {string} name - ID название меню-бара
* @returns {this}
*/
menuBarShow(name) {
if (this.$NW) {
this.$NW.Window.get().menu = this.$Menu.$Bar[name];
return this;
}
this.debug('Отображение меню невозможно!');
return false;
},
// clipboard
/** Получить данные из буфера обмена
* @param {string} [type='text'] - Тип получаемых данных
* @returns {string}
*/
getClipboard(type = 'text') {
if (this.$NW) {
const clipboard = this.$NW.Clipboard.get();
return clipboard.get(type);
}
this.debug('Работа с буфером обмена невозможна!');
return false;
},
/** Поместить данные в буфер обмена
* @param {string} value - Данные для помещения
* @param {string} [type='text'] - Тип данных
* @returns {this}
*/
setClipboard(value, type = 'text') {
if (this.$NW) {
const clipboard = this.$NW.Clipboard.get();
clipboard.set(value, type);
return this;
}
this.debug('Работа с буфером обмена невозможна!');
return false;
},
/** Очищает буфер обмена
* @returns {this}
*/
clearClipboard() {
if (this.$NW) {
const clipboard = this.$NW.Clipboard.get();
clipboard.clear();
return this;
}
this.debug('Работа с буфером обмена невозможна!');
return false;
},
// tray
/** Добавить пункт в трэй
* @param {string} [name] - ID название трэя
* @param {string} title - Текст при удерживании мыши
* @param {string} icon - Иконка
* @param {string} menu - ID название открывающегося меню
* @returns {this}
*/
menuTrayAdd(name, title, icon, menu) {
if (this.$NW) {
const tray = new this.$NW.Tray({
title,
icon,
'alticon': icon
});
tray.menu = this.$Menu[menu];
return this;
}
this.debug('Работа с треем невозможна!');
return false;
},
/** Переключить полноэкранный режим
* @param {bool} mode - true - включить, false - отключить
* @returns {this}
*/
fullScreen(mode) {
if (this.$NW) {
if (mode) {
const tmp = this.$NW.Window.get();
tmp.enterFullscreen();
} else {
const tmp = this.$NW.Window.get();
tmp.leaveFullscreen();
}
return this;
}
this.debug('Работа с интерфейсом невозможна!');
return false;
}
};
if (typeof require === 'function') { // JsMB
try {
$Interface.$NW = require('nw.gui');
} catch (e) {
throw new Error('Your system doesn\'t support native interface');
}
} else if (typeof localStorage !== 'undefined') { // eslint-disable-line
// Browser
// TODO: Add browser support
throw new Error('The browser doesn\'t support native interface!');
} else {
throw new Error('Your system doesn\'t support native interface');
}
// Bind this
for (const i in $Interface) {
if (typeof $Interface[i] === 'function') $Interface[i] = $Interface[i].bind(JsMB);
}
Object.assign(JsMB, $Interface);
Object.assign(window, $Interface);
}