2019-09-13 22:47:07 +12:00
|
|
|
(function(window) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
window.ls.container.get("view").add({
|
|
|
|
selector: "data-forms-upload",
|
2020-04-29 20:36:41 +12:00
|
|
|
controller: function(element, container, alerts, expression, env, search) {
|
2019-09-13 22:47:07 +12:00
|
|
|
var scope = element.dataset["scope"];
|
2020-04-28 06:38:06 +12:00
|
|
|
var project = expression.parse(element.dataset["project"] || "console");
|
2019-09-13 22:47:07 +12:00
|
|
|
var labelButton = element.dataset["labelButton"] || "Upload";
|
|
|
|
var labelLoading = element.dataset["labelLoading"] || "Uploading...";
|
|
|
|
var previewWidth = element.dataset["previewWidth"] || 200;
|
|
|
|
var previewHeight = element.dataset["previewHeight"] || 200;
|
|
|
|
var accept = element.dataset["accept"] || "";
|
2020-04-29 20:36:41 +12:00
|
|
|
var searchButton = (element.dataset["search"] || 0);
|
2019-09-13 22:47:07 +12:00
|
|
|
var required = element.dataset["required"] || false;
|
|
|
|
var className = element.dataset["class"] || "upload";
|
|
|
|
var max = parseInt(element.dataset["max"] || 4);
|
2020-04-28 20:35:51 +12:00
|
|
|
var sdk = scope === "sdk" ? container.get("sdk") : container.get("console");
|
2020-02-01 00:57:25 +13:00
|
|
|
var output = element.value || null;
|
2019-09-13 22:47:07 +12:00
|
|
|
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
var input = document.createElement("input");
|
|
|
|
var upload = document.createElement("div"); // Fake button
|
|
|
|
var preview = document.createElement("ul");
|
|
|
|
var progress = document.createElement("div");
|
|
|
|
var count = document.createElement("div");
|
|
|
|
|
|
|
|
wrapper.className = className;
|
|
|
|
|
|
|
|
input.type = "file";
|
|
|
|
input.accept = accept;
|
|
|
|
input.required = required;
|
|
|
|
input.tabIndex = -1;
|
|
|
|
|
|
|
|
count.className = "count";
|
|
|
|
|
|
|
|
upload.className = "button reverse margin-bottom";
|
|
|
|
upload.innerHTML = '<i class="icon icon-upload"></i> ' + labelButton;
|
|
|
|
upload.tabIndex = 0;
|
|
|
|
|
|
|
|
preview.className = "preview";
|
|
|
|
|
|
|
|
progress.className = "progress";
|
|
|
|
progress.style.width = "0%";
|
|
|
|
progress.style.display = "none";
|
|
|
|
|
|
|
|
var onComplete = function(message) {
|
|
|
|
alerts.remove(message);
|
2019-05-12 19:24:53 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
input.disabled = false;
|
|
|
|
upload.classList.remove("disabled");
|
|
|
|
progress.style.width = "0%";
|
|
|
|
progress.style.display = "none";
|
|
|
|
};
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
var render = function(result) {
|
2019-09-13 22:47:07 +12:00
|
|
|
preview.innerHTML = "";
|
|
|
|
|
|
|
|
count.innerHTML = "0 / " + max;
|
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
if(!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var file = document.createElement("li");
|
|
|
|
var image = document.createElement("img");
|
|
|
|
|
|
|
|
image.src = image.src =
|
|
|
|
env.API +
|
|
|
|
"/storage/files/" +
|
|
|
|
result +
|
|
|
|
"/preview?width=" +
|
|
|
|
previewWidth +
|
|
|
|
"&height=" +
|
|
|
|
previewHeight +
|
2020-04-28 06:38:06 +12:00
|
|
|
"&project="+project +
|
|
|
|
"&mode=admin";
|
2019-09-13 22:47:07 +12:00
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
file.className = "file avatar";
|
|
|
|
file.tabIndex = 0;
|
|
|
|
file.appendChild(image);
|
2019-09-13 22:47:07 +12:00
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
preview.appendChild(file);
|
2019-09-13 22:47:07 +12:00
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
var remove = (function(result) {
|
|
|
|
return function(event) {
|
2020-02-17 20:16:11 +13:00
|
|
|
render(result.$id);
|
2020-02-01 00:57:25 +13:00
|
|
|
};
|
|
|
|
})(result);
|
2019-09-13 22:47:07 +12:00
|
|
|
|
2020-02-01 00:57:25 +13:00
|
|
|
file.addEventListener("click", remove);
|
|
|
|
file.addEventListener("keypress", remove);
|
|
|
|
|
|
|
|
element.value = result;
|
2019-09-13 22:47:07 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
input.addEventListener("change", function() {
|
|
|
|
var message = alerts.add({ text: labelLoading, class: "" }, 0);
|
|
|
|
var files = input.files;
|
|
|
|
var read = JSON.parse(
|
|
|
|
expression.parse(element.dataset["read"] || "[]")
|
|
|
|
);
|
|
|
|
var write = JSON.parse(
|
|
|
|
expression.parse(element.dataset["write"] || "[]")
|
|
|
|
);
|
|
|
|
|
|
|
|
sdk.storage.createFile(files[0], read, write, 1).then(
|
|
|
|
function(response) {
|
|
|
|
onComplete(message);
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
render(response.$id);
|
2019-09-13 22:47:07 +12:00
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
alerts.add({ text: "An error occurred!", class: "" }, 3000); // File(s) uploaded.
|
|
|
|
onComplete(message);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
input.disabled = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
element.addEventListener("change", function() {
|
|
|
|
if (!element.value) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-29 20:36:41 +12:00
|
|
|
render(element.value);
|
|
|
|
|
|
|
|
wrapper.scrollIntoView();
|
2019-09-13 22:47:07 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
upload.addEventListener("keypress", function() {
|
|
|
|
input.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
element.parentNode.insertBefore(wrapper, element);
|
|
|
|
|
|
|
|
wrapper.appendChild(preview);
|
|
|
|
wrapper.appendChild(progress);
|
|
|
|
wrapper.appendChild(upload);
|
|
|
|
|
|
|
|
upload.appendChild(input);
|
|
|
|
|
|
|
|
render(output);
|
2020-04-28 20:35:51 +12:00
|
|
|
|
2020-04-29 20:36:41 +12:00
|
|
|
if(searchButton) {
|
2020-04-28 20:35:51 +12:00
|
|
|
let searchOpen = document.createElement("button");
|
|
|
|
|
|
|
|
searchOpen.type = 'button';
|
|
|
|
searchOpen.innerHTML = '<i class="icon icon-search"></i> Search';
|
|
|
|
searchOpen.classList.add('reverse');
|
|
|
|
|
2020-04-29 20:36:41 +12:00
|
|
|
let path = container.scope(searchButton);
|
|
|
|
|
|
|
|
searchOpen.addEventListener('click', function() {
|
|
|
|
search.selected = element.value;
|
|
|
|
search.path = path;
|
|
|
|
|
|
|
|
document.dispatchEvent(
|
|
|
|
new CustomEvent("open-file-serach", {
|
|
|
|
bubbles: false,
|
|
|
|
cancelable: true
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2020-04-28 20:35:51 +12:00
|
|
|
wrapper.appendChild(searchOpen);
|
|
|
|
}
|
2019-09-13 22:47:07 +12:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})(window);
|