blockbench/js/web.js

183 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-10-27 01:00:52 +08:00
(function() {
2018-10-18 01:50:25 +08:00
$.getScript("lib/file_saver.js");
2020-07-16 15:32:59 +08:00
$.getScript('https://rawgit.com/nodeca/pako/master/dist/pako.min.js', function() {
2019-01-09 22:54:35 +08:00
window.zlib = pako
})
2017-10-27 01:00:52 +08:00
})()
2019-12-16 03:04:31 +08:00
function initializeWebApp() {
2019-07-18 00:02:07 +08:00
$(document.body).on('click', 'a[href]', (event) => {
event.preventDefault();
window.open(event.target.href, '_blank');
2018-10-18 01:50:25 +08:00
});
2019-07-18 00:02:07 +08:00
if (!Blockbench.isMobile) {
$('#web_download_button').show()
2017-10-27 01:00:52 +08:00
}
if (Blockbench.browser == 'firefox') {
document.body.style.imageRendering = 'crisp-edges'
}
}
function loadInfoFromURL() {
2019-07-18 00:02:07 +08:00
if (location.hash.substr(1, 8) == 'session=') {
EditSession.dialog()
$('#edit_session_token').val(location.hash.substr(9))
2018-12-27 21:03:04 +08:00
}
2020-03-05 03:56:17 +08:00
if (location.hash.substr(1, 5) == 'load=') {
$.getJSON('https://blockbench.net/api/rawtext.php?url='+location.hash.substr(6), (model) => {
if (showSaveDialog()) {
resetProject();
Codecs.project.load(model, {path: ''});
}
})
} else if (location.hash.substr(1, 5) == 'pbin=') {
$.getJSON('https://blockbench.net/api/rawtext.php?url='+'https://pastebin.com/raw/'+location.hash.substr(6), (model) => {
if (showSaveDialog()) {
resetProject();
Codecs.project.load(model, {path: ''});
}
})
}
2019-12-16 03:04:31 +08:00
}
2019-07-18 00:02:07 +08:00
setInterval(function() {
Prop.zoom = Math.round(devicePixelRatio*100)
}, 500)
2017-10-27 01:00:52 +08:00
//Misc
window.onbeforeunload = function() {
2018-03-29 02:48:11 +08:00
if (Prop.project_saved === false && elements.length > 0) {
2019-07-18 00:02:07 +08:00
return 'Unsaved Changes';
2019-04-13 04:39:53 +08:00
} else {
2020-07-16 15:32:59 +08:00
Blockbench.dispatchEvent('before_closing')
2019-04-13 04:39:53 +08:00
EditSession.quit()
2017-10-27 01:00:52 +08:00
}
}
function showSaveDialog(close) {
2018-10-18 01:50:25 +08:00
var unsaved_textures = 0;
textures.forEach(function(t) {
if (!t.saved) {
unsaved_textures++;
}
})
if ((Prop.project_saved === false && elements.length > 0) || unsaved_textures) {
var answer = confirm(tl('message.close_warning.web'))
2019-12-16 03:04:31 +08:00
return answer;
2018-10-18 01:50:25 +08:00
} else {
return true;
}
2019-07-18 00:02:07 +08:00
}
BARS.defineActions(function() {
if (Blockbench.isMobile) {
2019-12-16 03:04:31 +08:00
var page_wrapper = $('#page_wrapper')
2019-08-18 00:26:14 +08:00
new Action('sidebar_left', {
2019-07-18 00:02:07 +08:00
icon: 'burst_mode',
category: 'view',
condition: () => !Modes.start,
click: function () {
2019-12-16 03:04:31 +08:00
page_wrapper.removeClass('show_right')
page_wrapper.toggleClass('show_left')
var s = page_wrapper.hasClass('show_left')
$('#left_bar').css('margin-left', '-400px')
$('#left_bar').animate({'margin-left': 0}, 160)
2019-07-18 00:02:07 +08:00
this.nodes.forEach(n => {
$(n).toggleClass('sel', s)
})
BarItems.sidebar_right.nodes.forEach(n => {
$(n).removeClass('sel')
})
updateInterfacePanels()
2019-07-19 23:31:22 +08:00
resizeWindow()
2019-07-18 00:02:07 +08:00
}
})
2019-08-18 00:26:14 +08:00
new Action('sidebar_right', {
2019-07-18 00:02:07 +08:00
icon: 'view_list',
category: 'view',
condition: () => !Modes.start,
click: function () {
2019-12-16 03:04:31 +08:00
page_wrapper.removeClass('show_left')
page_wrapper.toggleClass('show_right')
var s = page_wrapper.hasClass('show_right')
$('#right_bar').css('margin-left', '400px')
$('#right_bar').animate({'margin-left': 0}, 160)
2019-07-18 00:02:07 +08:00
this.nodes.forEach(n => {
$(n).toggleClass('sel', s)
})
BarItems.sidebar_left.nodes.forEach(n => {
$(n).removeClass('sel')
})
updateInterfacePanels()
2019-07-19 23:31:22 +08:00
resizeWindow()
2019-07-18 00:02:07 +08:00
}
})
2019-12-16 03:04:31 +08:00
$('.panel#element').detach();
var swiping;
var height = 0;
var start_x = 0;
var edge = 20;
var swipe_min = 60;
document.addEventListener('touchstart', (event) => {
if (event.changedTouches.length == 1) {
var touch = event.changedTouches[0];
height = touch.clientY;
start_x = touch.clientX;
if (touch.clientX < edge) {
swiping = 'left';
} else if (document.body.clientWidth - touch.clientX < edge) {
swiping = 'right';
}
}
}, false)
document.addEventListener('touchend', (event) => {
if (event.changedTouches.length == 1) {
var touch = event.changedTouches[0];
var delta_height = Math.abs(height - touch.clientY);
if (start_x < edge && touch.clientX > swipe_min && delta_height < 30) {
if (page_wrapper.hasClass('show_right')) {
BarItems.sidebar_right.trigger(event);
} else {
BarItems.sidebar_left.trigger(event);
}
} else if (
document.body.clientWidth - start_x < edge &&
(document.body.clientWidth - touch.clientX) > swipe_min &&
delta_height < 30
) {
if (page_wrapper.hasClass('show_left')) {
BarItems.sidebar_left.trigger(event);
} else {
BarItems.sidebar_right.trigger(event);
}
} else if (
document.body.clientWidth - start_x < 40 &&
document.body.clientWidth - touch.clientX < 40 &&
delta_height < 10 &&
event.target == page_wrapper[0] &&
(page_wrapper.hasClass('show_left') || page_wrapper.hasClass('show_right'))
) {
page_wrapper.removeClass('show_left')
page_wrapper.removeClass('show_right')
BarItems.sidebar_left.nodes.forEach(n => {
$(n).removeClass('sel')
})
updateInterfacePanels()
resizeWindow()
}
}
height = 0;
swiping = undefined;
}, false)
2019-07-18 00:02:07 +08:00
}
})