blockbench/js/api.js

698 lines
18 KiB
JavaScript
Raw Normal View History

2019-07-18 00:02:07 +08:00
const Blockbench = {
isWeb: !isApp,
isMobile: window.innerWidth <= 480,
version: appVersion,
platform: 'web',
flags: [],
drag_handlers: {},
events: {},
openTime: new Date(),
get elements() {
console.warn('Blockbench.elements is deprecated. Please use Outliner.elements instead.')
return Outliner.elements
},
get selection() {
console.warn('Blockbench.selection is deprecated. Please use Cube.selected or Outliner.selected instead.')
return Cube.selected
},
get textures() {
console.warn('Blockbench.textures is deprecated. Please just use textures instead.')
return textures;
},
2018-10-18 01:50:25 +08:00
edit(aspects, cb) {
Undo.initEdit(aspects)
cb()
Undo.finishEdit()
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
reload() {
2019-04-08 00:53:33 +08:00
localStorage.removeItem('backup_model')
2018-10-18 01:50:25 +08:00
if (isApp) {
2019-07-18 00:02:07 +08:00
Blockbench.addFlag('allow_closing')
2018-10-18 01:50:25 +08:00
Blockbench.flags.push('allow_reload')
currentwindow.reload()
} else {
location.reload()
}
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
registerEdit() {
2019-02-04 04:09:35 +08:00
console.warn('Blockbench.registerEdit is outdated. Please use Undo.initEdit and Undo.finishEdit')
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
//Interface
getIconNode(icon, color) {
var jq;
if (typeof icon === 'function') {
icon = icon()
}
if (icon === undefined) {
2018-12-16 23:18:20 +08:00
//Missing
2018-10-18 01:50:25 +08:00
jq = $('<i class="material-icons icon">help_outline</i>')
} else if (icon instanceof HTMLElement) {
2018-12-16 23:18:20 +08:00
//Node
2018-10-18 01:50:25 +08:00
jq = $(icon)
} else if (icon.substr(0, 2) === 'fa') {
2018-12-16 23:18:20 +08:00
//Font Awesome
2019-07-18 00:02:07 +08:00
if (icon.substr(3, 1) == '.') {
jq = $(`<i class="icon ${icon.substr(0, 3)} fa_big ${icon.substr(4)}"></i>`)
} else {
jq = $('<i class="icon fa fa_big ' + icon + '"></i>')
}
2018-10-18 01:50:25 +08:00
} else if (icon.substr(0, 5) === 'icon-') {
2018-12-16 23:18:20 +08:00
//Icomoon
2018-10-18 01:50:25 +08:00
jq = $('<i class="' + icon + '"></i>')
} else if (icon.substr(0, 14) === 'data:image/png') {
2018-12-16 23:18:20 +08:00
//Data URL
2018-10-18 01:50:25 +08:00
jq = $('<img class="icon" src="'+icon+'">')
} else {
2018-12-16 23:18:20 +08:00
//Material Icon
2018-10-18 01:50:25 +08:00
jq = $('<i class="material-icons icon">' + icon + '</i>')
}
if (color) {
if (color === 'x') {
jq.addClass('color_x')
} else if (color === 'y') {
jq.addClass('color_y')
} else if (color === 'z') {
jq.addClass('color_z')
} else if (typeof color === 'string') {
jq.css('color', color)
2017-10-27 01:00:52 +08:00
}
}
2018-10-18 01:50:25 +08:00
return jq.get(0)
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
showQuickMessage(message, time) {
2018-12-27 21:03:04 +08:00
$('#quick_message_box').remove()
2018-10-18 01:50:25 +08:00
var quick_message_box = $('<div id="quick_message_box" class="hidden"></div>')
$('body').append(quick_message_box)
quick_message_box.text(tl(message))
2019-07-18 00:02:07 +08:00
quick_message_box.fadeIn(0)
2018-10-18 01:50:25 +08:00
setTimeout(function() {
2019-07-18 00:02:07 +08:00
quick_message_box.fadeOut(0)
2018-10-18 01:50:25 +08:00
setTimeout(function() {
quick_message_box.remove()
2019-07-18 00:02:07 +08:00
}, 1)
2018-10-18 01:50:25 +08:00
}, time ? time : 1000)
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
showStatusMessage(message, time) {
2018-11-12 04:19:08 +08:00
Blockbench.setStatusBarText(tl(message))
setTimeout(function() {
Blockbench.setStatusBarText()
2019-07-18 00:02:07 +08:00
}, time ? time : 800)
},
2018-11-12 04:19:08 +08:00
setStatusBarText(text) {
if (text) {
Prop.file_name = text
} else {
Prop.file_name = Prop.file_name_alt||''
}
2019-07-18 00:02:07 +08:00
},
2018-12-27 21:03:04 +08:00
setProgress(progress, time, bar) {
setProgressBar(bar, progress||0, time)
2019-07-18 00:02:07 +08:00
},
2017-10-27 01:00:52 +08:00
showMessage(message, location) {
if (location === 'status_bar') {
2018-10-18 01:50:25 +08:00
Blockbench.showStatusMessage(message)
2017-10-27 01:00:52 +08:00
} else if (location === 'center') {
2018-10-18 01:50:25 +08:00
Blockbench.showQuickMessage(message)
2017-10-27 01:00:52 +08:00
}
2019-07-18 00:02:07 +08:00
},
2017-11-17 05:23:41 +08:00
showMessageBox(options, cb) {
if (options.confirm === undefined) options.confirm = 0
if (options.cancel === undefined) options.cancel = 0
2018-10-18 01:50:25 +08:00
if (!options.buttons) options.buttons = [tl('dialog.ok')]
if (options.translateKey) {
if (!options.title) options.title = tl('message.'+options.translateKey+'.title')
if (!options.message) options.message = tl('message.'+options.translateKey+'.message')
}
2017-11-17 05:23:41 +08:00
2019-07-18 00:02:07 +08:00
var jq_dialog = $('<dialog class="dialog paddinged" style="width: auto;" id="message_box"><div class="dialog_handle">'+options.title+'</div></dialog>')
2017-11-17 05:23:41 +08:00
jq_dialog.append('<div class="dialog_bar" style="height: auto; min-height: 56px; margin-bottom: 16px;">'+
2019-07-18 00:02:07 +08:00
marked(tl(options.message))+'</div>'
2017-11-17 05:23:41 +08:00
)
2018-10-18 01:50:25 +08:00
if (options.icon) {
jq_dialog.find('.dialog_bar').prepend($(Blockbench.getIconNode(options.icon)).addClass('message_box_icon'))
}
2017-11-17 05:23:41 +08:00
var buttons = []
options.buttons.forEach(function(b, i) {
2019-07-18 00:02:07 +08:00
var btn = $('<button type="button">'+tl(b)+'</button>')
2017-11-17 05:23:41 +08:00
btn.click(function(e) {
hideDialog()
setTimeout(function() {
2018-10-18 01:50:25 +08:00
jq_dialog.remove()
},200)
if (cb) {
2018-03-29 02:48:11 +08:00
cb(i)
2018-10-18 01:50:25 +08:00
}
2017-11-17 05:23:41 +08:00
})
buttons.push(btn)
})
2018-10-18 01:50:25 +08:00
jq_dialog.hide = function() {
$(jq_dialog.find('button').get(options.cancel)).click()
}
2017-11-17 05:23:41 +08:00
buttons[options.confirm].addClass('confirm_btn')
buttons[options.cancel].addClass('cancel_btn')
2018-12-03 02:37:06 +08:00
jq_dialog.append($('<div class="dialog_bar button_bar"></div>').append(buttons))
2019-08-18 00:26:14 +08:00
buttons.forEach(b => {
b.after('&nbsp;')
})
2017-11-17 05:23:41 +08:00
2018-10-18 01:50:25 +08:00
jq_dialog.addClass('draggable')
jq_dialog.draggable({
2019-09-06 06:16:54 +08:00
handle: ".dialog_handle",
containment: '#page_wrapper'
2018-10-18 01:50:25 +08:00
})
var x = ($(window).width()-540)/2
jq_dialog.css('left', x+'px')
jq_dialog.css('position', 'absolute')
2017-11-17 05:23:41 +08:00
$('#plugin_dialog_wrapper').append(jq_dialog)
2019-07-18 00:02:07 +08:00
$('.dialog').hide()
$('#blackout').show()
jq_dialog.show()
2018-10-18 01:50:25 +08:00
2019-07-18 00:02:07 +08:00
jq_dialog.css('top', limitNumber($(window).height()/2-jq_dialog.height()/2 - 140, 0, 2000)+'px')
2018-10-18 01:50:25 +08:00
if (options.width) {
jq_dialog.css('width', options.width+'px')
} else {
jq_dialog.css('width', limitNumber(options.buttons.length*170+44, 380, 894)+'px')
}
open_dialog = 'message_box'
open_interface = 'message_box'
return jq_dialog
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
textPrompt(title, value, callback) {
showDialog('text_input')
2018-11-12 04:19:08 +08:00
$('#text_input h2').text(tl(title))
2018-10-18 01:50:25 +08:00
$('#text_input input#text_input_field').val(value).select()
$('#text_input button.confirm_btn').off()
$('#text_input button.confirm_btn').click(function() {
var s = $('#text_input input#text_input_field').val()
if (callback !== undefined) {
callback(s)
}
})
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
addMenuEntry(name, icon, click) {
2019-08-18 00:26:14 +08:00
var action = new Action(name, {icon: icon, name: name, click: click})
2019-02-04 04:09:35 +08:00
MenuBar.addAction(action, 'filter')
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
removeMenuEntry(name) {
MenuBar.removeAction('filter.'+name)
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
openLink(link) {
if (isApp) {
shell.openExternal(link)
} else {
window.open(link)
}
2019-07-18 00:02:07 +08:00
},
notification(title, text, icon) {
Notification.requestPermission().then(status => {
if (status == 'granted') {
let n = new Notification(title, {body: text, icon: icon||'favicon.png'})
n.onclick = function() {
if (isApp) {
currentwindow.focus();
} else {
window.focus();
}
}
}
})
},
2018-10-18 01:50:25 +08:00
//IO
import(options, cb) {
if (typeof options !== 'object') {options = {}}
//extensions
//type
//readtype
//multiple
//startpath
//title
//errorbox
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
if (isApp) {
var properties = []
if (options.multiple) {
properties.push('multiSelections')
}
if (options.extensions[0] === 'image/*') {
options.type = 'Images'
options.extensions = ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'tif', 'gif']
}
2019-09-04 15:37:38 +08:00
ElecDialogs.showOpenDialog(
2018-10-18 01:50:25 +08:00
currentwindow,
{
title: options.title ? options.title : '',
2019-09-06 06:16:54 +08:00
dontAddToRecent: true,
2018-10-18 01:50:25 +08:00
filters: [{
name: options.type ? options.type : options.extensions[0],
extensions: options.extensions
}],
2018-10-24 03:49:04 +08:00
properties: (properties.length && Blockbench.platform !== 'darwin')?properties:undefined,
2018-10-18 01:50:25 +08:00
defaultPath: options.startpath
},
function (fileNames) {
2018-12-16 23:18:20 +08:00
Blockbench.read(fileNames, options, cb)
2017-10-27 01:00:52 +08:00
})
} else {
2018-10-18 01:50:25 +08:00
$('<input '+
'type="file'+
'" accept=".'+(options.extensions ? options.extensions.join(',.'): '')+
'" multiple="'+(options.multiple === true)+
'">').change(function(e) {
var input = this;
var results = [];
var result_count = 0;
var i = 0;
while (i < input.files.length) {
(function() {
var file = input.files[i]
var reader = new FileReader()
reader.i = i
reader.onloadend = function() {
if (reader.result.byteLength) {
var arr = new Uint8Array(reader.result)
var targa_loader = new Targa()
targa_loader.load(arr)
var result = targa_loader.getDataURL()
} else {
var result = reader.result
}
results[this.i] = {
name: file.name,
path: file.name,
content: result
}
result_count++;
if (result_count === input.files.length) {
cb(results)
}
}
if (options.readtype === 'image') {
if (pathToExtension(file.name) === 'tga') {
reader.readAsArrayBuffer(file)
} else {
reader.readAsDataURL(file)
}
2018-12-27 21:03:04 +08:00
} else if (options.readtype === 'buffer') {
reader.readAsArrayBuffer(file)
2018-10-18 01:50:25 +08:00
} else /*text*/ {
reader.readAsText(file)
}
i++;
})()
}
}).click()
2017-10-27 01:00:52 +08:00
}
2019-07-18 00:02:07 +08:00
},
2018-12-16 23:18:20 +08:00
read(paths, options, cb) {
if (!isApp || paths == undefined) return false;
2019-07-22 05:17:36 +08:00
if (typeof paths == 'string') paths = [paths];
2018-12-16 23:18:20 +08:00
var results = [];
var result_count = 0;
var i = 0;
var errant;
while (i < paths.length) {
(function() {
var this_i = i;
var file = paths[i]
if (options.readtype === 'image') {
//
var extension = pathToExtension(file)
if (extension === 'tga') {
var targa_loader = new Targa()
targa_loader.open(file, () => {
results[this_i] = {
name: pathToName(file, true),
path: file,
content: targa_loader.getDataURL()
}
result_count++;
if (result_count === paths.length) {
cb(results)
}
})
} else {
results[this_i] = {
name: pathToName(file, true),
path: file
}
result_count++;
if (result_count === paths.length) {
cb(results)
}
}
} else /*text*/ {
fs.readFile(file, 'utf-8', function (err, data) {
if (err) {
console.log(err)
if (!errant && options.errorbox !== false) {
Blockbench.showMessageBox({
translateKey: 'file_not_found',
icon: 'error_outline'
})
}
errant = true
return;
}
if (data.charCodeAt(0) === 0xFEFF) {
data = data.substr(1)
}
results[this_i] = {
name: pathToName(file, true),
path: file,
content: data
}
result_count++;
if (result_count === paths.length) {
cb(results)
}
})
}
})()
i++;
}
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
export(options, cb) {
if (!options) return;
/*
type
extensions
name
content
startpath
savetype
2018-12-27 21:03:04 +08:00
project_file
2018-10-18 01:50:25 +08:00
custom_writer
*/
2017-10-27 01:00:52 +08:00
if (Blockbench.isWeb) {
2018-10-18 01:50:25 +08:00
var file_name = options.name + (options.extensions ? '.'+options.extensions[0] : '')
2018-10-24 03:49:04 +08:00
if (options.custom_writer) {
options.custom_writer(options.content, file_name)
} else if (options.savetype === 'image') {
2018-10-18 01:50:25 +08:00
var download = document.createElement('a');
2018-10-24 03:49:04 +08:00
download.href = options.content
download.download = file_name;
2018-10-18 01:50:25 +08:00
if (Blockbench.browser === 'firefox') document.body.appendChild(download);
download.click();
if (Blockbench.browser === 'firefox') document.body.removeChild(download);
2019-03-10 05:06:35 +08:00
} else if (options.savetype === 'zip') {
saveAs(options.content, file_name)
2018-10-18 01:50:25 +08:00
} else {
var blob = new Blob([options.content], {type: "text/plain;charset=utf-8"});
saveAs(blob, file_name, {autoBOM: true})
}
2019-07-18 00:02:07 +08:00
if (typeof cb === 'function') {
cb(options)
2018-10-18 01:50:25 +08:00
}
2017-10-27 01:00:52 +08:00
} else {
2019-09-04 15:37:38 +08:00
ElecDialogs.showSaveDialog(currentwindow, {
2019-09-06 06:16:54 +08:00
dontAddToRecent: true,
2018-10-18 01:50:25 +08:00
filters: [ {
name: options.type,
extensions: options.extensions
} ],
2018-10-24 03:49:04 +08:00
defaultPath: (options.startpath && options.startpath !== 'Unknown')
? options.startpath.replace(/\.\w+$/, '')
: options.name
2018-10-18 01:50:25 +08:00
}, function (file_path) {
2019-08-18 00:26:14 +08:00
if (!file_path) return;
2019-07-22 05:17:36 +08:00
var extension = pathToExtension(file_path);
if (!extension && options.extensions && options.extensions[0]) {
file_path += '.'+options.extensions[0]
}
2018-12-27 21:03:04 +08:00
Blockbench.writeFile(file_path, options, cb)
})
}
2019-07-18 00:02:07 +08:00
},
2018-12-27 21:03:04 +08:00
writeFile(file_path, options, cb) {
/*
content
savetype
project_file
custom_writer
*/
2019-08-18 00:26:14 +08:00
if (!isApp || !file_path) {
2018-12-27 21:03:04 +08:00
return;
}
if (options.savetype === 'image' && typeof options.content === 'string') {
if (options.content.substr(0, 10) === 'data:image') {
options.content = nativeImage.createFromDataURL(options.content).toPNG()
} else {
2019-07-18 00:02:07 +08:00
options.content = options.content.replace(/\?\d+$/, '');
2018-12-27 21:03:04 +08:00
options.content = nativeImage.createFromPath(options.content).toPNG()
}
2019-07-18 00:02:07 +08:00
}
if (options.savetype === 'zip') {
2019-04-08 00:53:33 +08:00
var fileReader = new FileReader();
fileReader.onload = function(event) {
var buffer = Buffer.from(new Uint8Array(this.result));
fs.writeFileSync(file_path, buffer)
if (cb) {
cb(file_path)
}
};
fileReader.readAsArrayBuffer(options.content);
} else if (options.custom_writer) {
2018-12-27 21:03:04 +08:00
options.custom_writer(options.content, file_path)
} else {
2019-02-04 04:09:35 +08:00
fs.writeFileSync(file_path, options.content)
if (cb) {
cb(file_path)
}
2018-10-18 01:50:25 +08:00
}
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
//Flags
addFlag(flag) {
2018-12-03 02:37:06 +08:00
this.flags[flag] = true
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
removeFlag(flag) {
2018-12-03 02:37:06 +08:00
delete this.flags[flag]
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
hasFlag(flag) {
2018-12-03 02:37:06 +08:00
return this.flags[flag]
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
//Events
2019-04-08 00:53:33 +08:00
dispatchEvent(event_name, data) {
var list = this.events[event_name]
if (!list) return;
for (var i = 0; i < list.length; i++) {
if (typeof list[i] === 'function') {
list[i](data)
2017-10-27 01:00:52 +08:00
}
}
2019-07-18 00:02:07 +08:00
},
2017-10-27 01:00:52 +08:00
addListener(event_name, cb) {
2019-04-08 00:53:33 +08:00
if (!this.events[event_name]) {
this.events[event_name] = []
2017-10-27 01:00:52 +08:00
}
2019-04-08 00:53:33 +08:00
this.events[event_name].safePush(cb)
2019-07-18 00:02:07 +08:00
},
2019-04-08 00:53:33 +08:00
on(event_name, cb) {
return Blockbench.addListener(event_name, cb)
2019-07-18 00:02:07 +08:00
},
2017-10-27 01:00:52 +08:00
removeListener(event_name, cb) {
2019-04-08 00:53:33 +08:00
if (!this.events[event_name]) return;
this.events[event_name].remove(cb);
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
//File Drag
addDragHandler(id, options, cb) {
var entry = {
cb: cb
2017-10-27 01:00:52 +08:00
}
2018-10-18 01:50:25 +08:00
if (options.extensions && options.extensions.length) {
entry.extensions = options.extensions
}
if (options.addClass !== false) entry.addClass = true;
if (options.propagate) entry.propagate = true;
if (options.readtype) entry.readtype = options.readtype;
if (options.errorbox) entry.errorbox = true;
if (options.element) entry.element = options.element;
this.drag_handlers[id] = entry
2019-07-18 00:02:07 +08:00
},
2018-10-18 01:50:25 +08:00
removeDragHandler(id) {
delete this.drag_handlers[id]
2019-07-18 00:02:07 +08:00
},
2019-09-06 07:56:13 +08:00
};
2019-09-06 07:47:49 +08:00
(function() {
var last_welcome = localStorage.getItem('welcomed_version');
if (!last_welcome || last_welcome.replace(/.\d+$/, '') != appVersion.replace(/.\d+$/, '')) {
Blockbench.addFlag('after_update');
}
localStorage.setItem('welcomed_version', appVersion);
})();
2019-07-18 00:02:07 +08:00
if (isApp) {
Blockbench.platform = process.platform;
switch (Blockbench.platform) {
case 'win32': Blockbench.operating_system = 'Windows'; break;
case 'darwin': Blockbench.operating_system = 'macOS'; break;
default: Blockbench.operating_system = 'Linux'; break;
}
if (Blockbench.platform.includes('win32') === true) osfs = '\\';
2017-10-27 01:00:52 +08:00
}
2018-10-18 01:50:25 +08:00
document.ondragover = function(event) {
event.preventDefault()
}
document.body.ondrop = function(event) {
event.preventDefault()
forDragHandlers(event, function(handler, el) {
var fileNames = event.dataTransfer.files
var input = this;
var results = [];
var result_count = 0;
var i = 0;
var errant;
while (i < fileNames.length) {
if (isApp) {
if (handler.readtype === 'image') {
var path = fileNames[i].path
//
results[i] = {
name: pathToName(path, true),
path: path
}
result_count++;
if (result_count === fileNames.length) {
handler.cb(results, event)
}
} else /*text*/ {
(function() {
var path = fileNames[i].path
var this_i = i;
2019-02-04 04:09:35 +08:00
var data;
try {
data = fs.readFileSync(path, 'utf-8')
} catch (err) {
console.log(err)
if (!errant && handler.errorbox !== false) {
Blockbench.showMessageBox({
translateKey: 'file_not_found',
icon: 'error_outline'
})
2018-10-18 01:50:25 +08:00
}
2019-02-04 04:09:35 +08:00
errant = true
}
if (data) {
2018-10-18 01:50:25 +08:00
results[this_i] = {
name: pathToName(path, true),
path: path,
content: data
}
result_count++;
if (result_count === fileNames.length) {
handler.cb(results, event)
}
2019-02-04 04:09:35 +08:00
}
2018-10-18 01:50:25 +08:00
})()
}
2017-10-27 01:00:52 +08:00
} else {
2018-10-18 01:50:25 +08:00
(function() {
var file = fileNames[i]
var reader = new FileReader()
reader.i = i
reader.onloadend = function() {
results[this.i] = {
name: file.name,
path: file.name,
content: reader.result
}
result_count++;
if (result_count === fileNames.length) {
handler.cb(results, event)
}
}
if (handler.readtype === 'image') {
reader.readAsDataURL(file)
} else /*text*/ {
reader.readAsText(file)
}
})()
2017-10-27 01:00:52 +08:00
}
2018-10-18 01:50:25 +08:00
i++;
2017-10-27 01:00:52 +08:00
}
2018-10-18 01:50:25 +08:00
})
}
document.body.ondragenter = function(event) {
event.preventDefault()
forDragHandlers(event, function(handler, el) {
//$(el).css('background-color', 'red')
})
}
document.body.ondragleave = function(event) {
event.preventDefault()
forDragHandlers(event, function(handler, el) {
//$(el).css('background-color', '')
})
}
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
function forDragHandlers(event, cb) {
if (event.dataTransfer == undefined) {
return;
2017-10-27 01:00:52 +08:00
}
2018-10-18 01:50:25 +08:00
for (var id in Blockbench.drag_handlers) {
var handler = Blockbench.drag_handlers[id]
var el = undefined;
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
if (!handler.element) {
el = $('body').get(0)
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
} else if ($(handler.element).get(0) === event.target) {
el = event.target
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
} else if (typeof handler.element === 'string' && $(event.target).is(handler.element)) {
el = event.target
2017-10-27 01:00:52 +08:00
2018-10-18 01:50:25 +08:00
} else if (typeof handler.element === 'function') {
var result = handler.element()
if (result === true) {
el = $(event.target)
} else if ($(result).length) {
el = $(result).get(0)
}
} else if (handler.propagate) {
var parent = $(handler.element)
if (typeof handler.element === 'function' && !result) {
parent = $(handler.element())
}
if (parent && parent.has(event.target).length) {
el = parent
}
}
if (el &&
event.dataTransfer.files.length > 0 &&
event.dataTransfer.files[0].name &&
handler.extensions.includes( pathToExtension(event.dataTransfer.files[0].name) )
) {
cb(handler, el)
}
}
2017-10-27 01:00:52 +08:00
}