webpack-bootstrap-ui-kit/src/js_old/_components/_ui.form.croppie.js

259 lines
6.9 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import Events from "../_events";
import SpinnerUI from "./_ui.spinner";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import "croppie/croppie.js";
import "exif-js/exif.js";
2019-06-08 17:20:51 +02:00
const CroppieUI = (($) => {
2021-08-18 20:51:15 +02:00
const NAME = "jsCroppieUI";
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
const G = window;
const D = document;
const jqteOptions = {
color: false,
fsize: false,
2021-08-18 20:51:15 +02:00
funit: "em",
2019-06-08 17:20:51 +02:00
format: false,
rule: false,
source: false,
sub: false,
sup: false,
};
class CroppieUI {
constructor(element) {
const ui = this;
const $el = $(element);
2020-09-02 00:12:38 +02:00
console.log(`${NAME}: init ...`);
2019-06-08 17:20:51 +02:00
ui.$el = $el;
$el.data(DATA_KEY, this);
ui.input = $el.find('input[type="file"]');
//ui.inputData = $('<input type="hidden" class="base64enc" name="' + ui.input.attr('name') + 'base64" />');
2021-08-18 20:51:15 +02:00
ui.width = ui.input.data("width");
ui.height = ui.input.data("height");
2019-06-08 17:20:51 +02:00
$el.append(
'<div class="cropper-wrap"><div class="cropper-container"></div>' +
2021-08-18 20:51:15 +02:00
'<a href="#" class="btn-remove" style="display:none"><i class="fas fa-times"></i> Remove</a></div>'
2019-06-08 17:20:51 +02:00
);
//$el.append(ui.inputData);
2021-08-18 20:51:15 +02:00
ui.uploadCropWrap = $el.find(".cropper-wrap");
ui.uploadCrop = ui.uploadCropWrap.find(".cropper-container");
2019-06-08 17:20:51 +02:00
const ratio = ui.width / (ui.uploadCrop.width() - 32);
ui.uploadCrop.croppie({
enableExif: true,
enforceBoundary: false,
viewport: {
width: ui.width / ratio,
height: ui.height / ratio,
},
});
ui.uploadCrop.hide();
2021-08-18 20:51:15 +02:00
ui.input.on("change", (e) => {
2019-06-08 17:20:51 +02:00
this.readFile(e.currentTarget);
});
2021-08-18 20:51:15 +02:00
ui.$btnRemove = $el.find(".btn-remove");
ui.$btnRemove.on("click", (e) => {
2019-06-08 17:20:51 +02:00
e.preventDefault();
2021-08-18 20:51:15 +02:00
ui.uploadCrop.removeClass("ready");
$el.find(".croppie-image").remove();
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
ui.$el.find('input[type="file"]').val("");
2019-06-08 17:20:51 +02:00
ui.$el.find('input[type="file"]').change();
ui.uploadCropWrap.hide();
});
2019-07-10 20:59:57 +02:00
2021-08-18 20:51:15 +02:00
if (ui.$el.find("img.croppie-image").length) {
2019-07-10 20:59:57 +02:00
ui.$btnRemove.show();
}
2019-06-08 17:20:51 +02:00
}
readFile(input) {
const ui = this;
const $el = ui.$el;
2021-08-18 20:51:15 +02:00
const $form = $el.closest("form");
2019-06-08 17:20:51 +02:00
if (input.files && input.files[0]) {
const reader = new FileReader();
reader.onload = (e) => {
2021-08-18 20:51:15 +02:00
ui.uploadCrop.addClass("ready");
ui.uploadCrop.croppie("bind", {
2019-06-08 17:20:51 +02:00
url: e.target.result,
});
ui.uploadCrop.show();
ui.uploadCropWrap.show();
2019-07-10 20:59:57 +02:00
ui.$btnRemove.show();
2020-09-02 00:12:38 +02:00
};
2019-06-08 17:20:51 +02:00
reader.readAsDataURL(input.files[0]);
2021-08-18 20:51:15 +02:00
$form.off("submit");
$form.on("submit", (e) => {
2020-09-02 00:12:38 +02:00
console.log(`${NAME}: Processing submission ...`);
e.preventDefault();
2021-08-18 20:51:15 +02:00
if ($form.data("locked")) {
console.warn(`${NAME}: Form#${$form.attr("id")} is locked.`);
2020-09-02 00:12:38 +02:00
return false;
}
2021-08-18 20:51:15 +02:00
$form.data("locked", true);
2020-09-02 00:12:38 +02:00
2019-06-08 17:20:51 +02:00
SpinnerUI.show();
2021-08-18 20:51:15 +02:00
if (!ui.uploadCrop.hasClass("ready")) {
2019-06-08 17:20:51 +02:00
return true;
}
2020-09-02 00:12:38 +02:00
ui.uploadCrop
2021-08-18 20:51:15 +02:00
.croppie("result", {
type: "blob",
2020-09-02 00:12:38 +02:00
size: {
width: ui.width,
height: ui.height,
},
2021-08-18 20:51:15 +02:00
format: "png",
2020-09-02 00:12:38 +02:00
})
.then((blob) => {
const form = e.currentTarget;
const data = new FormData(form);
2021-08-18 20:51:15 +02:00
const name = $(input).attr("name");
2020-09-02 00:12:38 +02:00
2021-08-18 20:51:15 +02:00
data.delete("BackURL");
2020-09-02 00:12:38 +02:00
data.delete(name);
data.append(name, blob, `${name}-image.png`);
2021-08-18 20:51:15 +02:00
data.append("ajax", "1");
2020-09-02 00:12:38 +02:00
2021-08-18 20:51:15 +02:00
if (!$(form).data("jsFormValidate").validate()) {
2020-09-02 00:12:38 +02:00
return false;
}
$.ajax({
2021-08-18 20:51:15 +02:00
url: $(form).attr("action"),
2020-09-02 00:12:38 +02:00
data,
processData: false,
contentType: false,
2021-08-18 20:51:15 +02:00
type: $(form).attr("method"),
2020-09-02 00:12:38 +02:00
success: function (data) {
2021-08-18 20:51:15 +02:00
$form.data("locked", false);
2020-09-02 00:12:38 +02:00
let IS_JSON = false;
let json = {};
try {
IS_JSON = true;
json = $.parseJSON(data);
} catch (e) {
IS_JSON = false;
2019-06-08 17:20:51 +02:00
}
2020-09-02 00:12:38 +02:00
if (IS_JSON) {
2021-08-18 20:51:15 +02:00
if (typeof json["status"] !== "undefined") {
if (json["status"] === "success") {
MainUI.alert(json["message"], json["status"]);
2020-09-02 00:12:38 +02:00
2021-08-18 20:51:15 +02:00
if (typeof json["link"] !== "undefined") {
2020-09-02 00:12:38 +02:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: Finished submission > JSON ... Redirecting to ${json["link"]}.`
2020-09-02 00:12:38 +02:00
);
setTimeout(() => {
2021-08-18 20:51:15 +02:00
G.location = json["link"];
2020-09-02 00:12:38 +02:00
}, 2000);
} else {
console.warn(
2021-08-18 20:51:15 +02:00
`${NAME}: Finished submission > JSON no redirect link.`
2020-09-02 00:12:38 +02:00
);
}
2021-08-18 20:51:15 +02:00
} else if (json["status"] === "error") {
MainUI.alert(json["message"], json["status"]);
2020-09-02 00:12:38 +02:00
}
}
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
if (typeof json["form"] !== "undefined") {
2020-09-02 00:12:38 +02:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: Finished submission > JSON. Got new form response.`
2020-09-02 00:12:38 +02:00
);
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
$(form).replaceWith(json["form"]);
2020-09-02 00:12:38 +02:00
$(G).trigger(Events.AJAX);
}
} else {
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: Finished submission > DATA response.`
2020-09-02 00:12:38 +02:00
);
$(form).replaceWith(data);
$(G).trigger(Events.AJAX);
//G.location.reload(false);
}
2019-06-08 17:20:51 +02:00
2020-09-02 00:12:38 +02:00
SpinnerUI.hide();
},
});
2019-06-08 17:20:51 +02:00
2020-09-02 00:12:38 +02:00
//ui.inputData.val(data);
});
2019-06-08 17:20:51 +02:00
});
} else {
2020-09-02 00:12:38 +02:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: Sorry - your browser doesn't support the FileReader API.`
2020-09-02 00:12:38 +02:00
);
2019-06-08 17:20:51 +02:00
}
}
static dispose() {
2020-09-02 00:12:38 +02:00
console.log(`${NAME}: destroying.`);
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
return this.each((i, el) => {
// attach functionality to element
const $el = $(el);
let data = $el.data(DATA_KEY);
if (!data) {
data = new CroppieUI(el);
$el.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = CroppieUI._jQueryInterface;
$.fn[NAME].Constructor = CroppieUI;
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return CroppieUI._jQueryInterface;
};
// auto-apply
2020-09-09 17:40:58 +02:00
$(window).on(`${NAME}.init ${Events.AJAX} ${Events.LOADED}`, () => {
2021-08-18 20:51:15 +02:00
$(".field.croppie").jsCroppieUI();
2019-06-08 17:20:51 +02:00
});
return CroppieUI;
})($);
export default CroppieUI;