(function() {
function buildForm(dialog) {
let jq_dialog = $(dialog.object)
for (var form_id in dialog.form) {
let data = dialog.form[form_id]
if (data === '_') {
jq_dialog.append('
')
} else if (data && Condition(data.condition)) {
var bar = $(`
`)
if (data.label) {
bar.append(`${tl(data.label)+(data.nocolon?'':':')} `)
dialog.max_label_width = Math.max(getStringWidth(tl(data.label)), dialog.max_label_width)
}
switch (data.type) {
default:
bar.append(` `)
if (data.list) {
let list = $(` `)
for (let value of data.list) {
list.append(``)
}
bar.append(list)
}
if (data.type == 'password') {
bar.append(`
`)
let input = bar.find('input').attr('type', 'password')
let hidden = true;
let this_bar = bar;
this_bar.find('.password_toggle').click(e => {
hidden = !hidden;
input.attr('type', hidden ? 'password' : 'text');
this_bar.find('.password_toggle i')[0].className = hidden ? 'fas fa-eye-slash' : 'fas fa-eye';
})
}
break;
case 'textarea':
bar.append(``)
break;
case 'select':
var el = $(`
`)
var sel = el.find('select')
for (var key in data.options) {
var name = tl(data.options[key])
sel.append(` ${name} `)
}
bar.append(el)
break;
case 'radio':
var el = $(`
`)
for (var key in data.options) {
var name = tl(data.options[key])
el.append(`
${name}
`)
}
bar.append(el)
break;
case 'info':
data.text = marked(tl(data.text))
bar.append(`${data.text}
`)
bar.addClass('small_text')
break;
case 'number':
bar.append(` `)
break;
case 'vector':
let group = $(`
`)
bar.append(group)
for (var i = 0; i < (data.dimensions || 3); i++) {
group.append(` `)
}
break;
case 'color':
if (!data.colorpicker) {
data.colorpicker = new ColorPicker({
id: 'cp_'+form_id,
name: tl(data.label),
label: false,
private: true
})
}
bar.append(data.colorpicker.getNode())
break;
case 'checkbox':
bar.append(` `)
break;
case 'file':
case 'folder':
case 'save':
if (data.type == 'folder' && !isApp) break;
var input = $(` `);
bar.append(input);
bar.addClass('form_bar_file');
switch (data.type) {
case 'file': bar.append('insert_drive_file '); break;
case 'folder': bar.append('folder '); break;
case 'save': bar.append('save '); break;
}
let remove_button = $('clear
');
bar.append(remove_button);
remove_button.on('click', e => {
e.stopPropagation();
data.value = '';
input.val('');
})
bar.on('click', e => {
function fileCB(files) {
data.value = files[0].path;
input.val(data.value);
}
switch (data.type) {
case 'file':
Blockbench.import({
resource_id: data.resource_id,
extensions: data.extensions,
type: data.filetype,
startpath: data.value
}, fileCB);
break;
case 'folder':
ElecDialogs.showOpenDialog(currentwindow, {
properties: ['openDirectory'],
defaultPath: data.value
}, (filePaths) => {
if (filePaths) fileCB([{ path: filePaths[0] }]);
})
break;
case 'save':
Blockbench.export({
resource_id: data.resource_id,
extensions: data.extensions,
type: data.filetype,
startpath: data.value,
custom_writer: () => {},
}, fileCB);
break;
}
})
}
if (data.readonly) {
bar.find('input').attr('readonly', 'readonly').removeClass('focusable_input')
}
jq_dialog.append(bar)
}
}
}
function buildLines(dialog) {
let jq_dialog = $(dialog.object)
dialog.lines.forEach(l => {
if (typeof l === 'object' && (l.label || l.widget)) {
var bar = $('
')
if (l.label) {
bar.append(''+tl(l.label)+(l.nocolon?'':':')+' ')
dialog.max_label_width = Math.max(getStringWidth(tl(l.label)), dialog.max_label_width)
}
if (l.node) {
bar.append(l.node)
} else if (l.widget) {
var widget = l.widget
if (typeof l.widget === 'string') {
widget = BarItems[l.widget]
} else if (typeof l.widget === 'function') {
widget = l.widget()
}
bar.append(widget.getNode())
dialog.max_label_width = Math.max(getStringWidth(widget.name), dialog.max_label_width)
}
jq_dialog.append(bar)
} else {
jq_dialog.append(l)
}
})
}
window.Dialog = class Dialog {
constructor(options) {
this.id = options.id
this.title = options.title
this.lines = options.lines
this.form = options.form
this.width = options.width
this.fadeTime = options.fadeTime
this.draggable = options.draggable
this.singleButton = options.singleButton
this.buttons = options.buttons
this.fadeTime = options.fadeTime||0;
this.form_first = options.form_first;
this.confirmIndex = options.confirmIndex||0;
this.cancelIndex = options.cancelIndex !== undefined ? options.cancelIndex : 1;
this.confirmEnabled = options.confirmEnabled === false ? false : true
this.cancelEnabled = options.cancelEnabled === false ? false : true
this.onConfirm = options.onConfirm ? options.onConfirm : this.hide
this.onCancel = options.onCancel ? options.onCancel : this.hide
this.onButton = options.onButton;
this.object;
}
confirm() {
$(this.object).find('.confirm_btn:not([disabled])').click()
}
cancel() {
$(this.object).find('.cancel_btn:not([disabled])').click()
}
show() {
let scope = this;
var jq_dialog = $(`${tl(this.title)}
`)
this.object = jq_dialog.get(0)
this.max_label_width = 0;
if (this.form_first) {
if (this.form) buildForm(this);
if (this.lines) buildLines(this);
} else {
if (this.lines) buildLines(this);
if (this.form) buildForm(this);
}
if (this.max_label_width) {
document.styleSheets[0].insertRule('.dialog#'+this.id+' .dialog_bar label {width: '+(this.max_label_width+8)+'px}')
}
if (this.buttons) {
var buttons = []
this.buttons.forEach((b, i) => {
var btn = $(''+tl(b)+' ')
buttons.push(btn)
if (typeof this.onButton == 'function') {
btn.click((event) => {
this.onButton(i);
})
}
})
buttons[this.confirmIndex] && buttons[this.confirmIndex].addClass('confirm_btn')
buttons[this.cancelIndex] && buttons[this.cancelIndex].addClass('cancel_btn')
let bar = $('
');
jq_dialog.append(bar);
buttons.forEach((button, i) => {
if (i) bar.append(' ')
bar.append(button)
})
} else if (this.singleButton) {
jq_dialog.append('' +
''+tl('dialog.close')+' ' +
'
')
} else {
jq_dialog.append(`
${tl('dialog.confirm')}
${tl('dialog.cancel')}
`)
}
jq_dialog.append('clear
')
var confirmFn = function(e) {
var result = {}
if (scope.form) {
for (var form_id in scope.form) {
var data = scope.form[form_id]
if (typeof data === 'object') {
switch (data.type) {
default:
result[form_id] = jq_dialog.find('input#'+form_id).val()
break;
case 'info':
break;
case 'textarea':
result[form_id] = jq_dialog.find('textarea#'+form_id).val()
break;
case 'select':
result[form_id] = jq_dialog.find('select#'+form_id+' > option:selected').attr('id')
break;
case 'radio':
result[form_id] = jq_dialog.find('.form_part_radio#'+form_id+' input:checked').attr('id')
break;
case 'number':
result[form_id] = Math.clamp(parseFloat(jq_dialog.find('input#'+form_id).val())||0, data.min, data.max)
break;
case 'vector':
result[form_id] = [];
for (var i = 0; i < (data.dimensions || 3); i++) {
let num = Math.clamp(parseFloat(jq_dialog.find(`input#${form_id}_${i}`).val())||0, data.min, data.max)
result[form_id].push(num)
}
break;
case 'color':
result[form_id] = data.colorpicker.get();
break;
case 'checkbox':
result[form_id] = jq_dialog.find('input#'+form_id).is(':checked')
break;
}
}
}
}
scope.onConfirm(result, e)
}
confirmFn.bind(this)
if (this.confirmEnabled) $(this.object).find('.confirm_btn').click(confirmFn)
if (this.cancelEnabled) $(this.object).find('.cancel_btn').click(() => {this.onCancel()})
//Draggable
if (this.draggable !== false) {
jq_dialog.addClass('draggable')
jq_dialog.draggable({
handle: ".dialog_handle",
containment: '#page_wrapper'
})
var x = Math.clamp(($(window).width()-540)/2, 0, 2000)
jq_dialog.css('left', x+'px')
jq_dialog.css('position', 'absolute')
}
$('#plugin_dialog_wrapper').append(jq_dialog)
$('.dialog').hide(0)
$('#blackout').fadeIn(scope.fadeTime)
jq_dialog.fadeIn(scope.fadeTime)
jq_dialog.css('top', limitNumber($(window).height()/2-jq_dialog.height()/2, 0, 100)+'px')
if (this.width) {
jq_dialog.css('width', this.width+'px')
}
let first_focus = jq_dialog.find('.focusable_input').first()
if (first_focus) first_focus.focus()
open_dialog = scope.id
open_interface = scope
Prop.active_panel = 'dialog'
return this;
}
hide() {
$('#blackout').fadeOut(this.fadeTime)
$(this.object).fadeOut(this.fadeTime)
.find('.tool').detach()
open_dialog = false;
open_interface = false;
Prop.active_panel = undefined
setTimeout(() => {
$(this.object).remove()
}, this.fadeTime)
}
getFormBar(form_id) {
var bar = $(this.object).find(`.form_bar_${form_id}`)
if (bar.length) return bar;
}
}
})()