Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
459 lines (433 loc) · 16.5 KB
/
Copy pathpopup.js
File metadata and controls
459 lines (433 loc) · 16.5 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* global MAIN, HELPER, POP, LAYER, EFFECTS, GUI */
/* global canvas_front, WIDTH, HEIGHT */
var POP = new popup();
/**
* user dialogs library
*
* @author ViliusL
*
* Usage:
* var POP = new popup();
* POP.add({name: "param1", title: "value1:" });
* POP.add(...);
* POP.show('title', main_handler, preview_handler, onload_handler);
*
* POP.add() parameters:
* - name type example
* - ---------------------------------------------------------------
* - name string 'parameter1'
* - title string 'enter value:'
* - type string 'select', 'textarea', 'color'
* - value string '314'
* - values array fo strings ['one', 'two', 'three']
* - range numbers interval [0, 255]
* - step int/float 1
* - placeholder text 'enter number here'
* - html html text '<b>bold</b>'
* - function function 'cutom_function'
* - onchange function 'class.onchange_function()'
*/
function popup() {
this.active = false;
this.handler = '';
this.preview = false;
this.onload = false;
this.width_mini = 184;
this.height_mini = 195;
this.preview_in_main = false;
this.effects = false;
this.id = 0;
var parameters = [];
var layer_active_small = document.createElement("canvas");
var layer_active_small_ctx = layer_active_small.getContext("2d");
//add parameter
this.add = function (object) {
parameters.push(object);
};
/**
* show popup window.
* used strings: "Ok", "Cancel", "Preview"
*
* @param string title
* @param function handler
* @param function preview_handler
* @param function onload_handler
*/
this.show = function (title, handler, preview_handler, onload_handler) {
POP.id = HELPER.getRandomInt(0, 999999999);
if (this.active == true) {
this.hide();
return false;
}
this.active = true;
this.handler = handler;
if (preview_handler != undefined)
this.preview = preview_handler;
if (onload_handler != undefined)
this.onload = onload_handler;
var html = '';
var dim = HELPER.get_dimensions();
popup = document.getElementById('popup');
popup.style.top = 150 + 'px';
popup.style.left = Math.round(dim[0] / 2) + 'px';
if (this.effects == true) {
var index;
for (var i = 0; i < EFFECTS.FILTERS_LIST.length; i++) {
if (EFFECTS.FILTERS_LIST[i].name == GUI.last_menu) {
index = i;
break;
}
}
var prev_index = index - 1;
if (prev_index < 0) {
prev_index = 0;
}
var next_index = index + 1;
if (next_index > EFFECTS.FILTERS_LIST.length - 1) {
next_index = EFFECTS.FILTERS_LIST.length - 1;
}
html += '<span style="float:right;">';
html += '<input id="previous_filter" type="button" value="<"> ';
html += '<select id="effect_browser">';
html += '<option class="trn" value="">--- Select effect ---</option>';
for (var i = 0; i < EFFECTS.FILTERS_LIST.length; i++) {
var selected = '';
if (EFFECTS.FILTERS_LIST[i].name == GUI.last_menu)
var selected = 'selected';
html += ' <option ' + selected + ' value="' + i + '">' + EFFECTS.FILTERS_LIST[i].title + '</option>';
}
html += '</select>';
html += ' <input id="next_filter" onclick="" type="button" value=">"> ';
html += '</span>';
}
html += '<h2 id="popup_drag" class="trn">' + title + '</h2>';
//preview area
if (this.preview !== false && this.preview_in_main == false) {
html += '<div style="margin-top:15px;margin-bottom:15px;">';
html += '<canvas style="position:relative;float:left;margin-right:5px;border:1px solid #393939;" width="' + POP.width_mini + '" height="' + POP.height_mini + '" id="pop_pre"></canvas>';
html += '<canvas style="position:relative;border:1px solid #393939;background-color:#ffffff;" width="' + POP.width_mini + '" height="' + POP.height_mini + '" id="pop_post"></canvas>';
html += '</div>';
}
//settings
html += '<table style="width:99%;">';
for (var i in parameters) {
var parameter = parameters[i];
html += '<tr id="popup-tr-'+parameters[i].name+'">';
if (title != 'Error')
html += '<td style="font-weight:bold;padding-right:3px;width:130px;" class="trn">' + parameter.title + '</td>';
if (parameter.name != undefined) {
if (parameter.values != undefined) {
var onchange = '';
if (parameter.onchange != undefined)
onchange = ' onchange="' + parameter.onchange + ';" ';
if (parameter.values.length > 10 || parameter.type == 'select') {
//drop down
if (onchange == '' && preview_handler != undefined)
onchange = ' onchange="POP.view();" ';
html += '<td colspan="2"><select ' + onchange + ' style="font-size:12px;" id="pop_data_' + parameter.name + '">';
var k = 0;
for (var j in parameter.values) {
var sel = '';
if (parameter.value == parameter.values[j])
sel = 'selected="selected"';
if (parameter.value == undefined && k == 0)
sel = 'selected="selected"';
html += '<option ' + sel + ' name="' + parameter.values[j] + '">' + parameter.values[j] + '</option>';
k++;
}
html += '</select></td>';
}
else {
//radio
html += '<td colspan="2">';
if (parameter.values.length > 2)
html += '<div class="group">';
var k = 0;
for (var j in parameter.values) {
var ch = '';
if (parameter.value == parameter.values[j])
ch = 'checked="checked"';
if (parameter.value == undefined && k == 0)
ch = 'checked="checked"';
if (onchange == '' && preview_handler != undefined)
onchange = ' onchange="POP.view();" ';
html += '<input type="radio" ' + onchange + ' ' + ch + ' name="' + parameter.name + '" id="pop_data_' + parameter.name + "_poptmp" + j + '" value="' + parameter.values[j] + '">';
html += '<label style="margin-right:20px;" class="trn" for="pop_data_' + parameter.name + "_poptmp" + j + '">' + parameter.values[j] + '</label>';
if (parameter.values.length > 2)
html += '<br />';
k++;
}
if (parameter.values.length > 2)
html += '</div>';
html += '</td>';
}
}
else if (parameter.value != undefined) {
//input, range, textarea, color
var step = 1;
if (parameter.step != undefined)
step = parameter.step;
if (parameter.range != undefined) {
//range
var preview_code = '';
if (this.preview !== false)
preview_code = 'POP.view();';
html += '<td><input type="range" id="pop_data_' + parameter.name + '" value="' + parameter.value + '" min="' + parameter.range[0] + '" max="' + parameter.range[1] + '" step="' + step + '" " oninput="document.getElementById(\'pv' + i + '\').innerHTML=Math.round(this.value*100)/100;' + preview_code + '" /></td>';
html += '<td style="padding-left:10px;width:50px;" id="pv' + i + '">' + parameter.value + '</td>';
}
else if (parameter.type == 'color') {
//color
var preview_code = '';
if (this.preview !== false)
preview_code = 'POP.view();';
html += '<td><input type="color" id="pop_data_' + parameter.name + '" value="' + parameter.value + '" onchange="'+preview_code+'" /></td>';
}
else {
//input or textarea
if (parameter.placeholder == undefined)
parameter.placeholder = '';
if (parameter.type == 'textarea'){
html += '<td><textarea style="height:80px;" id="pop_data_' + parameter.name + '" placeholder="' + parameter.placeholder + '">' + parameter.value + '</textarea></td>';
}
else{
var input_type="text";
if(parameter.placeholder != undefined && parameter.placeholder != '' && !isNaN(parameter.placeholder))
input_type = 'number';
if(parameter.value != undefined && typeof parameter.value == 'number')
input_type = 'number';
html += '<td colspan="2"><input type="'+input_type+'" id="pop_data_' + parameter.name + '" value="' + parameter.value + '" placeholder="' + parameter.placeholder + '" onkeyup="POP.validate(this);" /></td>';
}
}
}
}
else if (parameter.function != undefined) {
//custom function
var result;
if (typeof parameter.function == 'string')
result = window[parameter.function]();
else
result = parameter.function();
html += '<td colspan="3">' + result + '</td>';
}
else if (parameter.html != undefined) {
//html
html += '<td style="padding-bottom:3px;padding-top:3px;" colspan="2">' + parameter.html + '</td>';
}
else {
//locked fields without name
str = "" + parameter.value;
var id_tmp = parameter.title.toLowerCase().replace(/[^\w]+/g, '').replace(/ +/g, '-');
id_tmp = id_tmp.substring(0, 10);
if (str.length < 40)
html += '<td colspan="2"><div class="trn" id="pop_data_'+id_tmp+'" style="padding: 2px 0px;">' + parameter.value + '</div></td>';
else
html += '<td style="font-size:11px;" colspan="2"><textarea disabled="disabled">' + parameter.value + '</textarea></td>';
}
html += '</tr>';
}
html += '</table>';
//action buttons
html += '<div style="text-align:center;margin-top:20px;margin-bottom:15px;">';
html += '<button onclick="POP.save();" class="button trn">Ok</button>';
html += '<button onclick="POP.hide();" class="button trn">Cancel</button>';
if (this.preview_in_main !== false)
html += '<button onclick="POP.view();" class="button trn">Preview</button>';
html += '</div>';
document.getElementById("popup").innerHTML = html;
document.getElementById("popup").style.display = "block";
if (parameters.length > 15)
document.getElementById("popup").style.overflowY = "scroll";
else
document.getElementById("popup").style.overflowY = 'hidden';
//onload
if (this.onload != '') {
if (typeof this.onload == "string")
window[this.onload]();
else
this.onload();
}
//some events for effects browser
if (this.effects == true) {
document.getElementById('previous_filter').disabled = false;
document.getElementById('next_filter').disabled = false;
if (index == 0) {
document.getElementById('previous_filter').disabled = true;
}
if (index == EFFECTS.FILTERS_LIST.length - 1) {
document.getElementById('next_filter').disabled = true;
}
//previous
document.getElementById('previous_filter').addEventListener('click', function (event) {
POP.hide();
GUI.last_menu = EFFECTS.FILTERS_LIST[prev_index].name;
call_menu(EFFECTS, EFFECTS.FILTERS_LIST[prev_index].name);
});
//next
document.getElementById('next_filter').addEventListener('click', function (event) {
POP.hide();
GUI.last_menu = EFFECTS.FILTERS_LIST[next_index].name;
call_menu(EFFECTS, EFFECTS.FILTERS_LIST[next_index].name);
});
//onchange
var effect_browser = document.getElementById('effect_browser');
effect_browser.addEventListener('change', function (event) {
var value = effect_browser.options[effect_browser.selectedIndex].value;
POP.hide();
GUI.last_menu = EFFECTS.FILTERS_LIST[value].name;
call_menu(EFFECTS, EFFECTS.FILTERS_LIST[value].name);
});
}
//load preview?
if (this.preview !== false && this.preview_in_main == false) {
//original
var pop_pre = document.getElementById("pop_pre").getContext("2d");
pop_pre.rect(0, 0, POP.width_mini, POP.height_mini);
pop_pre.fillStyle = "#ffffff";
pop_pre.fill();
GUI.draw_background(pop_pre, POP.width_mini, POP.height_mini, 5);
pop_pre.drawImage(document.getElementById(LAYER.layers[LAYER.layer_active].name), 0, 0, POP.width_mini, POP.height_mini);
//copy
pop_post = document.getElementById("pop_post").getContext("2d");
pop_post.rect(0, 0, POP.width_mini, POP.height_mini);
pop_post.fillStyle = "#ffffff";
pop_post.fill();
GUI.draw_background(pop_post, POP.width_mini, POP.height_mini, 5);
pop_post.drawImage(document.getElementById(LAYER.layers[LAYER.layer_active].name), 0, 0, POP.width_mini, POP.height_mini);
//prepare temp canvas
layer_active_small.width = POP.width_mini;
layer_active_small.height = POP.height_mini;
layer_active_small_ctx.drawImage(document.getElementById(LAYER.layers[LAYER.layer_active].name), 0, 0, POP.width_mini, POP.height_mini);
POP.view();
}
//call translation again to translate popup
HELP.help_translate(LANG);
};
//hide popup
this.hide = function () {
document.getElementById('popup').style.display = 'none';
parameters = [];
this.handler = '';
this.active = false;
this.preview = false;
this.onload = false;
this.preview_in_main = false;
this.effects = false;
canvas_front.clearRect(0, 0, WIDTH, HEIGHT);
};
//renders preview. If input=range supported, is called on every param update - must be fast...
this.view = function () {
if (this.preview !== false) {
if (this.preview_in_main == false) {
//reset mini view
pop_post.clearRect(0, 0, POP.width_mini, POP.height_mini);
pop_post.drawImage(layer_active_small, 0, 0);
}
//prepare
var response = {};
inputs = document.getElementsByTagName('input');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].id.substr(0, 9) == 'pop_data_') {
var key = inputs[i].id.substr(9);
if (HELPER.strpos(key, "_poptmp") != false)
key = key.substring(0, HELPER.strpos(key, "_poptmp"));
var value = inputs[i].value;
if (inputs[i].type == 'radio') {
if (inputs[i].checked == true)
response[key] = value;
}
else
response[key] = value;
}
}
selects = document.getElementsByTagName('select');
for (i = 0; i < selects.length; i++) {
if (selects[i].id.substr(0, 9) == 'pop_data_') {
var key = selects[i].id.substr(9);
var value = selects[i].value;
response[key] = value;
}
}
textareas = document.getElementsByTagName('textarea');
for (i = 0; i < textareas.length; i++) {
if (textareas[i].id.substr(0, 9) == 'pop_data_') {
var key = textareas[i].id.substr(9);
var value = textareas[i].value;
response[key] = value;
}
}
//call handler
if (this.preview_in_main == false)
this.preview(response, pop_post, POP.width_mini, POP.height_mini);
else
this.preview(response);
}
};
//OK pressed - prepare data and call handlers
this.save = function () {
this.active = false;
document.getElementById("popup").style.display = "none";
var response = {};
inputs = document.getElementsByTagName('input');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].id.substr(0, 9) == 'pop_data_') {
var key = inputs[i].id.substr(9);
if (HELPER.strpos(key, "_poptmp") != false)
key = key.substring(0, HELPER.strpos(key, "_poptmp"));
var value = inputs[i].value;
if (inputs[i].type == 'radio') {
if (inputs[i].checked == true)
response[key] = value;
}
else
response[key] = value;
}
}
selects = document.getElementsByTagName('select');
for (i = 0; i < selects.length; i++) {
if (selects[i].id.substr(0, 9) == 'pop_data_') {
var key = selects[i].id.substr(9);
var value = selects[i].value;
response[key] = value;
}
}
textareas = document.getElementsByTagName('textarea');
for (i = 0; i < textareas.length; i++) {
if (textareas[i].id.substr(0, 9) == 'pop_data_') {
var key = textareas[i].id.substr(9);
var value = textareas[i].value;
response[key] = value;
}
}
parameters = [];
this.preview = false;
this.onload = false;
this.preview_in_main = false;
this.effects = false;
if (this.handler != '') {
if (typeof this.handler == "object")
this.handler[0][this.handler[1]](response);
else if (typeof this.handler == "function")
this.handler(response);
else
console.log('error: wrong function type: ' + this.handler);
}
this.handler = '';
};
//validate input field, unless browser supports input=range
this.validate = function (field) {
for (var i in parameters) {
var parameter = parameters[i];
if ("pop_data_" + parameter.name == field.id && parameter.range != undefined) {
if (field.value == '-' || field.value == '')
return true;
var value = parseFloat(field.value);
if (isNaN(value) || value != field.value)
field.value = parameter.value; //not number
if (value < parameter.range[0])
field.value = parameter.range[0]; //less then min
else if (value > parameter.range[1])
field.value = parameter.range[1]; //more then max
}
}
};
}