import app from './../../app.js'; import config from './../../config.js'; import Base_layers_class from './../../core/base-layers.js'; import Base_gui_class from './../../core/base-gui.js'; import Dialog_class from './../../libs/popup.js'; import Helper_class from './../../libs/helpers.js'; import Clipboard_class from './../../libs/clipboard.js'; import alertify from './../../../../node_modules/alertifyjs/build/alertify.min.js'; import EXIF from './../../../../node_modules/exif-js/exif.js'; import GUI_tools_class from "../../core/gui/gui-tools"; import semver_compare from './../../../../node_modules/semver-compare/'; var instance = null; /** * manages files / open * * @author ViliusL */ class File_open_class { constructor() { //singleton if (instance) { return instance; } instance = this; var _this = this; this.POP = new Dialog_class(); this.Base_layers = new Base_layers_class(); this.Base_gui = new Base_gui_class(); this.Helper = new Helper_class(); this.GUI_tools = new GUI_tools_class(); //clipboard class this.Clipboard_class = new Clipboard_class(function (data, w, h) { _this.on_paste(data, w, h); }); this.events(); this.maybe_file_open_url_handler(); } events() { var _this = this; window.ondrop = function (e) { //drop e.preventDefault(); _this.open_handler(e); }; window.ondragover = function (e) { e.preventDefault(); }; document.addEventListener('keydown', (event) => { var code = event.key.toLowerCase(); if (this.Helper.is_input(event.target)) return; if (code == "o") { //open this.open_file(); event.preventDefault(); } }, false); } on_paste(data, width, height) { var new_layer = { name: 'Paste', type: 'image', data: data, }; app.State.do_action( new app.Actions.Insert_layer_action(new_layer) ); } open_file() { var _this = this; alertify.success('You can also drag and drop items into browser.'); document.getElementById("tmp").innerHTML = ''; var a = document.createElement('input'); a.setAttribute("id", "file_open"); a.type = 'file'; a.multiple = 'multiple'; document.getElementById("tmp").appendChild(a); document.getElementById('file_open').addEventListener('change', function (e) { _this.open_handler(e); }, false); //force click document.querySelector('#file_open').click(); } open_webcam(){ var _this = this; var video = document.createElement('video'); video.autoplay = true; video.style.maxWidth = '100%'; var track = null; function handleSuccess(stream) { track = stream.getTracks()[0]; video.srcObject = stream; } function handleError(error) { alertify.error('Sorry, cold not load getUserMedia() data: ' + error); } var settings = { title: 'Webcam', params: [ {title: "Stream:", html: '
'}, ], on_load: function(params){ document.getElementById('webcam_container').appendChild(video); }, on_finish: function(params){ //capture data var width = video.videoWidth; var height = video.videoHeight; var tmpCanvas = document.createElement('canvas'); var tmpCanvasCtx = tmpCanvas.getContext("2d"); tmpCanvas.width = width; tmpCanvas.height = height; tmpCanvasCtx.drawImage(video, 0, 0); //create requested layer var new_layer = { name: "Webcam #" + _this.Base_layers.auto_increment, type: 'image', data: tmpCanvas.toDataURL("image/png"), width: width, height: height, width_original: width, height_original: height, }; app.State.do_action( new app.Actions.Bundle_action('open_file_webcam', 'Open File Webcam', [ new app.Actions.Insert_layer_action(new_layer), new app.Actions.Autoresize_canvas_action(width, height, null, true, true) ]) ); //destroy if(track != null){ track.stop(); } video.pause(); video.src = ""; video.load(); }, on_cancel: function(params){ if(track != null){ track.stop(); } video.pause(); video.src = ""; video.load(); }, }; this.POP.show(settings); navigator.mediaDevices.getUserMedia({audio: false, video: true}) .then(handleSuccess) .catch(handleError); } open_dir() { var _this = this; document.getElementById("tmp").innerHTML = ''; var a = document.createElement('input'); a.setAttribute("id", "file_open_dir"); a.type = 'file'; a.webkitdirectory = 'webkitdirectory'; document.getElementById("tmp").appendChild(a); document.getElementById('file_open_dir').addEventListener('change', function (e) { _this.open_handler(e); }, false); //force click document.querySelector('#file_open_dir').click(); } /** * opens data URLs, like: "data:image/png;base64,xxxxxx" * * data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAG0lEQVQYV2N89+7df0FBQQbG/////3///j0DAF9wCsg9spQfAAAAAElFTkSuQmCC */ open_data_url() { var _this = this; var settings = { title: 'Open data URL', params: [ {name: "data", title: "Data URL:", type: "textarea", value: ""}, ], on_finish: function (params) { _this.file_open_data_url_handler(params.data); }, }; this.POP.show(settings); } file_open_data_url_handler(data) { var _this = this; if (data == '') return; var img = new Image(); img.crossOrigin = "Anonymous"; img.onload = function () { var new_layer = { name: "Data URL", type: 'image', link: img, width: img.width, height: img.height, width_original: img.width, height_original: img.height, }; app.State.do_action( new app.Actions.Bundle_action('open_file_data_url', 'Open File Data URL', [ new app.Actions.Insert_layer_action(new_layer), new app.Actions.Autoresize_canvas_action(img.width, img.height, null, true, true) ]) ); img.onload = function () { config.need_render = true; }; }; img.onerror = function (ex) { alertify.error('Sorry, image could not be loaded. Try copy image and paste it.'); }; img.src = data; } open_url() { var _this = this; var settings = { title: 'Open URL', params: [ {name: "url", title: "URL:", value: ""}, ], on_finish: function (params) { _this.file_open_url_handler(params); }, }; this.POP.show(settings); } async open_handler(e) { var _this = this; var files = e.target.files; var auto_increment = this.Base_layers.auto_increment; if (files == undefined) { //drag and drop files = e.dataTransfer.files; } //sort var orders = []; for (var i = 0, f; i < files.length; i++) { orders.push(files[i].name); } orders.sort(); var order_map = []; for (var i in orders) { order_map[orders[i]] = parseInt(i); } //check if dropped directory var dir_opened = false; if (e.dataTransfer && e.dataTransfer.items) { var items = e.dataTransfer.items; for (var i=0; i