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

267 lines
7.1 KiB
JavaScript
Raw Normal View History

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