2018-08-29 20:17:49 +03:00
|
|
|
import Component from '@ember/component';
|
2022-02-03 21:50:49 +03:00
|
|
|
import EmberObject, {action, computed, set} from '@ember/object';
|
|
|
|
import classic from 'ember-classic-decorator';
|
2018-08-29 20:17:49 +03:00
|
|
|
import {
|
|
|
|
IMAGE_EXTENSIONS,
|
|
|
|
IMAGE_MIME_TYPES
|
|
|
|
} from 'ghost-admin/components/gh-image-uploader';
|
2019-04-16 10:20:59 +03:00
|
|
|
import {utils as ghostHelperUtils} from '@tryghost/helpers';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2018-08-29 20:17:49 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
|
|
|
import {run} from '@ember/runloop';
|
2018-12-11 13:05:59 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
const MAX_IMAGES = 9;
|
|
|
|
const MAX_PER_ROW = 3;
|
2019-04-16 10:20:59 +03:00
|
|
|
const {countWords} = ghostHelperUtils;
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@classic
|
|
|
|
export default class KoenigCardGallery extends Component {
|
2022-02-03 22:04:43 +03:00
|
|
|
@service koenigDragDropHandler;
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2018-08-29 20:17:49 +03:00
|
|
|
// attrs
|
2022-02-03 21:50:49 +03:00
|
|
|
files = null;
|
|
|
|
images = null;
|
|
|
|
payload = null;
|
|
|
|
isSelected = false;
|
|
|
|
isEditing = false;
|
|
|
|
imageExtensions = IMAGE_EXTENSIONS;
|
|
|
|
imageMimeTypes = IMAGE_MIME_TYPES;
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
// properties
|
2022-02-03 21:50:49 +03:00
|
|
|
errorMessage = null;
|
|
|
|
handlesDragDrop = true;
|
|
|
|
_dragDropContainer = null;
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2018-08-29 20:17:49 +03:00
|
|
|
// closure actions
|
2022-02-03 21:50:49 +03:00
|
|
|
selectCard() { }
|
|
|
|
deselectCard() { }
|
|
|
|
editCard() { }
|
|
|
|
saveCard() { }
|
|
|
|
deleteCard() { }
|
|
|
|
moveCursorToNextSection() { }
|
|
|
|
moveCursorToPrevSection() { }
|
|
|
|
addParagraphAfterCard() { }
|
|
|
|
registerComponent() { }
|
|
|
|
|
|
|
|
@computed('payload.{caption,payload.images.[]}')
|
|
|
|
get counts() {
|
2018-08-29 20:17:49 +03:00
|
|
|
let wordCount = 0;
|
|
|
|
let imageCount = this.payload.images.length;
|
|
|
|
|
|
|
|
if (this.payload.caption) {
|
2019-04-16 10:20:59 +03:00
|
|
|
wordCount += countWords(this.payload.caption);
|
2018-08-29 20:17:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {wordCount, imageCount};
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@computed('images.[]')
|
|
|
|
get toolbar() {
|
2019-06-24 18:33:21 +03:00
|
|
|
if (isEmpty(this.images)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2019-06-24 18:33:21 +03:00
|
|
|
return {
|
|
|
|
items: [{
|
2018-08-29 20:17:49 +03:00
|
|
|
title: 'Add images',
|
|
|
|
icon: 'koenig/kg-add',
|
|
|
|
iconClass: 'fill-white',
|
2022-05-26 13:05:14 +03:00
|
|
|
action: this.triggerFileDialog
|
2019-06-24 18:33:21 +03:00
|
|
|
}]
|
|
|
|
};
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@computed('images.@each.{src,previewSrc,width,height,row}')
|
|
|
|
get imageRows() {
|
2018-08-29 20:17:49 +03:00
|
|
|
let rows = [];
|
|
|
|
let noOfImages = this.images.length;
|
2018-12-11 13:05:59 +03:00
|
|
|
// 3 images per row unless last row would have a single image in which
|
|
|
|
// case the last 2 rows will have 2 images
|
|
|
|
let maxImagesInRow = function (idx) {
|
|
|
|
return noOfImages > 1 && (noOfImages % 3 === 1) && (idx === (noOfImages - 2));
|
|
|
|
};
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
this.images.forEach((image, idx) => {
|
|
|
|
let row = image.row;
|
2018-12-11 15:13:15 +03:00
|
|
|
let classes = [];
|
|
|
|
let overlayClasses = [];
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2018-12-11 13:05:59 +03:00
|
|
|
// start a new display row if necessary
|
|
|
|
if (maxImagesInRow(idx)) {
|
2018-08-29 20:17:49 +03:00
|
|
|
row = row + 1;
|
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
// apply classes to the image containers
|
2018-08-29 20:17:49 +03:00
|
|
|
if (!rows[row]) {
|
2018-12-11 13:05:59 +03:00
|
|
|
// first image in row
|
2018-08-29 20:17:49 +03:00
|
|
|
rows[row] = [];
|
2018-12-11 15:13:15 +03:00
|
|
|
classes.push('pr2');
|
2018-12-11 16:01:22 +03:00
|
|
|
overlayClasses.push('mr2');
|
2018-12-11 13:05:59 +03:00
|
|
|
} else if (((idx + 1) % 3 === 0) || maxImagesInRow(idx + 1) || idx + 1 === noOfImages) {
|
|
|
|
// last image in row
|
2018-12-11 15:13:15 +03:00
|
|
|
classes.push('pl2');
|
|
|
|
overlayClasses.push('ml2');
|
2018-08-29 20:17:49 +03:00
|
|
|
} else {
|
2018-12-11 13:05:59 +03:00
|
|
|
// middle of row
|
2018-12-11 15:13:15 +03:00
|
|
|
classes.push('pl2', 'pr2');
|
|
|
|
overlayClasses.push('ml2', 'mr2');
|
2018-08-29 20:17:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (row > 0) {
|
|
|
|
classes.push('mt4');
|
|
|
|
}
|
|
|
|
|
|
|
|
let styledImage = Object.assign({}, image);
|
|
|
|
let aspectRatio = (image.width || 1) / (image.height || 1);
|
|
|
|
styledImage.style = htmlSafe(`flex: ${aspectRatio} 1 0%`);
|
2018-12-11 15:13:15 +03:00
|
|
|
styledImage.classes = htmlSafe(classes.join(' '));
|
|
|
|
styledImage.overlayClasses = htmlSafe(overlayClasses.join(' '));
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
rows[row].push(styledImage);
|
|
|
|
});
|
|
|
|
|
|
|
|
return rows;
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
init() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.init(...arguments);
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
if (!this.payload || isEmpty(this.payload.images)) {
|
|
|
|
this._updatePayloadAttr('images', []);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:36:19 +03:00
|
|
|
this._buildImages();
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
this.registerComponent(this);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2018-12-11 13:05:59 +03:00
|
|
|
willDestroyElement() {
|
2022-02-03 21:50:49 +03:00
|
|
|
super.willDestroyElement(...arguments);
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
if (this._dragDropContainer) {
|
|
|
|
this._dragDropContainer.destroy();
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
addImage(file) {
|
|
|
|
let count = this.images.length + 1;
|
|
|
|
let row = Math.ceil(count / MAX_PER_ROW) - 1;
|
2018-08-31 14:01:08 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
let image = this._readDataFromImageFile(file);
|
|
|
|
image.row = row;
|
|
|
|
this.images.pushObject(image);
|
|
|
|
}
|
2018-08-31 14:01:08 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
setImageSrc(uploadResult) {
|
2023-03-01 11:50:56 +03:00
|
|
|
if (!uploadResult.fileName && !uploadResult.url) {
|
|
|
|
return; // upload failed
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
let image = this.images.findBy('fileName', uploadResult.fileName);
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
image.set('src', uploadResult.url);
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
this._buildAndSaveImagesPayload();
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
setFiles(files) {
|
|
|
|
this._startUpload(files);
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
deleteImage(image) {
|
|
|
|
let localImage = this.images.findBy('fileName', image.fileName);
|
|
|
|
this.images.removeObject(localImage);
|
|
|
|
this._recalculateImageRows();
|
|
|
|
this._buildAndSaveImagesPayload();
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
updateCaption(caption) {
|
|
|
|
this._updatePayloadAttr('caption', caption);
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
triggerFileDialog(event) {
|
2022-05-26 13:05:14 +03:00
|
|
|
const target = event?.target || this.element;
|
|
|
|
|
|
|
|
const cardElem = target.closest('.__mobiledoc-card');
|
|
|
|
const fileInput = cardElem?.querySelector('input[type="file"]');
|
|
|
|
|
|
|
|
fileInput?.click();
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
uploadFailed(uploadResult) {
|
|
|
|
let image = this.images.findBy('fileName', uploadResult.fileName);
|
|
|
|
this.images.removeObject(image);
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
this._buildAndSaveImagesPayload();
|
|
|
|
let fileName = (uploadResult.fileName.length > 20) ? `${uploadResult.fileName.substr(0, 20)}...` : uploadResult.fileName;
|
|
|
|
this.set('errorMessage', `${fileName} failed to upload`);
|
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2022-02-03 21:50:49 +03:00
|
|
|
@action
|
|
|
|
handleErrors(errors) {
|
|
|
|
let errorMssg = ((errors[0] && errors[0].message)) || 'Some images failed to upload';
|
|
|
|
this.set('errorMessage', errorMssg);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
clearErrorMessage() {
|
|
|
|
this.set('errorMessage', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
didSelect() {
|
|
|
|
if (this._dragDropContainer) {
|
|
|
|
// add a delay when enabling reorder drag/drop so that the card
|
|
|
|
// must be selected before a reorder drag can be initiated
|
|
|
|
// - allows for cards to be drag and dropped themselves
|
|
|
|
run.later(this, function () {
|
|
|
|
if (!this.isDestroyed && !this.isDestroying) {
|
|
|
|
this._dragDropContainer.enableDrag();
|
|
|
|
}
|
|
|
|
}, 100);
|
2018-08-29 20:17:49 +03:00
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
didDeselect() {
|
|
|
|
if (this._dragDropContainer) {
|
|
|
|
this._dragDropContainer.disableDrag();
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2018-08-31 12:36:19 +03:00
|
|
|
// Ember event handlers ----------------------------------------------------
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
dragOver(event) {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is needed to work around inconsistencies with dropping files
|
|
|
|
// from Chrome's downloads bar
|
|
|
|
if (navigator.userAgent.indexOf('Chrome') > -1) {
|
|
|
|
let eA = event.dataTransfer.effectAllowed;
|
|
|
|
event.dataTransfer.dropEffect = (eA === 'move' || eA === 'linkMove') ? 'move' : 'copy';
|
|
|
|
}
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.set('isDraggedOver', true);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
dragLeave(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('isDraggedOver', false);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
|
|
|
drop(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.set('isDraggedOver', false);
|
|
|
|
|
|
|
|
if (event.dataTransfer.files) {
|
|
|
|
this._startUpload(event.dataTransfer.files);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2018-08-31 12:36:19 +03:00
|
|
|
// Private methods ---------------------------------------------------------
|
|
|
|
|
2018-12-11 13:05:59 +03:00
|
|
|
_recalculateImageRows() {
|
|
|
|
this.images.forEach((image, idx) => {
|
|
|
|
image.set('row', Math.ceil((idx + 1) / MAX_PER_ROW) - 1);
|
|
|
|
});
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2018-08-31 12:36:19 +03:00
|
|
|
_startUpload(files = []) {
|
|
|
|
let currentCount = this.images.length;
|
|
|
|
let allowedCount = (MAX_IMAGES - currentCount);
|
|
|
|
|
|
|
|
let strippedFiles = Array.prototype.slice.call(files, 0, allowedCount);
|
|
|
|
if (strippedFiles.length < files.length) {
|
2018-08-31 13:10:14 +03:00
|
|
|
this.set('errorMessage', 'Galleries are limited to 9 images');
|
2018-08-31 12:36:19 +03:00
|
|
|
}
|
|
|
|
this.set('files', strippedFiles);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-31 12:36:19 +03:00
|
|
|
|
|
|
|
_readDataFromImageFile(file) {
|
2018-12-05 23:16:03 +03:00
|
|
|
let url = URL.createObjectURL(file);
|
2018-08-31 12:36:19 +03:00
|
|
|
let image = EmberObject.create({
|
2018-12-05 23:16:03 +03:00
|
|
|
fileName: file.name,
|
|
|
|
previewSrc: url
|
2018-08-31 12:36:19 +03:00
|
|
|
});
|
|
|
|
|
2018-12-05 23:16:03 +03:00
|
|
|
let imageElem = new Image();
|
|
|
|
imageElem.onload = () => {
|
|
|
|
// update current display images
|
|
|
|
image.set('width', imageElem.naturalWidth);
|
|
|
|
image.set('height', imageElem.naturalHeight);
|
2018-08-31 12:36:19 +03:00
|
|
|
|
2018-12-05 23:16:03 +03:00
|
|
|
// ensure width/height makes it into the payload images
|
|
|
|
this._buildAndSaveImagesPayload();
|
2018-08-31 12:36:19 +03:00
|
|
|
};
|
2018-12-05 23:16:03 +03:00
|
|
|
imageElem.src = url;
|
2018-08-31 12:36:19 +03:00
|
|
|
|
|
|
|
return image;
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-31 12:36:19 +03:00
|
|
|
|
|
|
|
_buildAndSaveImagesPayload() {
|
|
|
|
let payloadImages = [];
|
|
|
|
|
|
|
|
let isValidImage = image => image.fileName
|
|
|
|
&& image.src
|
|
|
|
&& image.width
|
|
|
|
&& image.height;
|
|
|
|
|
|
|
|
this.images.forEach((image, idx) => {
|
|
|
|
if (isValidImage(image)) {
|
|
|
|
let payloadImage = Object.assign({}, image, {previewSrc: undefined});
|
|
|
|
payloadImage.row = Math.ceil((idx + 1) / MAX_PER_ROW) - 1;
|
|
|
|
|
|
|
|
payloadImages.push(payloadImage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._updatePayloadAttr('images', payloadImages);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-31 12:36:19 +03:00
|
|
|
|
|
|
|
_buildImages() {
|
|
|
|
this.images = this.payload.images.map(image => EmberObject.create(image));
|
2018-12-11 13:05:59 +03:00
|
|
|
this._registerOrRefreshDragDropHandler();
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-31 12:36:19 +03:00
|
|
|
|
2018-08-29 20:17:49 +03:00
|
|
|
_updatePayloadAttr(attr, value) {
|
|
|
|
let payload = this.payload;
|
|
|
|
let save = this.saveCard;
|
|
|
|
|
|
|
|
set(payload, attr, value);
|
|
|
|
|
|
|
|
// update the mobiledoc and stay in edit mode
|
|
|
|
save(payload, false);
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
this._registerOrRefreshDragDropHandler();
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-08-29 20:17:49 +03:00
|
|
|
|
2018-12-11 13:05:59 +03:00
|
|
|
// - rename container so that it's more explicit when we have an initial file
|
|
|
|
// drop container vs a drag reorder+file drop container?
|
|
|
|
_registerOrRefreshDragDropHandler() {
|
|
|
|
if (this._dragDropContainer) {
|
2021-09-06 13:57:42 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this._refreshDragDropContainer);
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('afterRender', this, this._registerDragDropContainer);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
|
|
|
|
_refreshDragDropContainer() {
|
|
|
|
const galleryElem = this.element.querySelector('[data-gallery]');
|
|
|
|
|
|
|
|
// gallery element can change when switching from placeholder to gallery
|
|
|
|
// make sure the DnD container is updated to match
|
|
|
|
if (this._dragDropContainer.element !== galleryElem) {
|
|
|
|
this._dragDropContainer.destroy();
|
|
|
|
this._registerDragDropContainer();
|
2018-12-11 13:05:59 +03:00
|
|
|
} else {
|
2021-09-06 13:57:42 +03:00
|
|
|
this._dragDropContainer.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEmpty(this.images) && !this._dragDropContainer.isDragEnabled) {
|
|
|
|
this._dragDropContainer.enableDrag();
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
|
|
|
|
_registerDragDropContainer() {
|
|
|
|
const galleryElem = this.element.querySelector('[data-gallery]');
|
|
|
|
if (galleryElem) {
|
|
|
|
this._dragDropContainer = this.koenigDragDropHandler.registerContainer(
|
|
|
|
galleryElem,
|
|
|
|
{
|
|
|
|
draggableSelector: '[data-image]',
|
|
|
|
droppableSelector: '[data-image]',
|
|
|
|
isDragEnabled: !isEmpty(this.images),
|
|
|
|
onDragStart: run.bind(this, this._dragStart),
|
|
|
|
onDragEnd: run.bind(this, this._dragEnd),
|
|
|
|
onDragEnterContainer: run.bind(this, this._dragEnter),
|
|
|
|
onDragLeaveContainer: run.bind(this, this._dragLeave),
|
|
|
|
getDraggableInfo: run.bind(this, this._getDraggableInfo),
|
|
|
|
getIndicatorPosition: run.bind(this, this._getDropIndicatorPosition),
|
|
|
|
onDrop: run.bind(this, this._onDrop),
|
|
|
|
onDropEnd: run.bind(this, this._onDropEnd)
|
2018-12-11 13:05:59 +03:00
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
);
|
2018-12-11 13:05:59 +03:00
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2019-06-27 18:34:23 +03:00
|
|
|
_dragStart(draggableInfo) {
|
|
|
|
this.element.querySelector('figure').classList.remove('kg-card-selected');
|
|
|
|
|
|
|
|
// enable dropping when an image is dragged in from outside of this card
|
|
|
|
let isImageDrag = draggableInfo.type === 'image' || draggableInfo.cardName === 'image';
|
|
|
|
if (isImageDrag && draggableInfo.payload.src && this.images.length !== MAX_IMAGES) {
|
|
|
|
this._dragDropContainer.enableDrag();
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2019-06-27 18:34:23 +03:00
|
|
|
|
|
|
|
_dragEnd() {
|
|
|
|
if (this.isSelected) {
|
|
|
|
this.element.querySelector('figure').classList.add('kg-card-selected');
|
|
|
|
} else {
|
|
|
|
this._dragDropContainer.disableDrag();
|
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
|
|
|
|
if (this.isDraggedOver) {
|
|
|
|
this.set('isDraggedOver', false);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
|
|
|
|
_dragEnter() {
|
|
|
|
if (this.images.length === 0) {
|
|
|
|
this.set('isDraggedOver', true);
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2021-09-06 13:57:42 +03:00
|
|
|
|
|
|
|
_dragLeave() {
|
|
|
|
this.set('isDraggedOver', false);
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2019-06-27 18:34:23 +03:00
|
|
|
|
2018-12-11 13:05:59 +03:00
|
|
|
_getDraggableInfo(draggableElement) {
|
|
|
|
let src = draggableElement.querySelector('img').getAttribute('src');
|
|
|
|
let image = this.images.findBy('src', src) || this.images.findBy('previewSrc', src);
|
2019-06-27 18:34:23 +03:00
|
|
|
let payload = image && image.getProperties('fileName', 'src', 'row', 'width', 'height', 'caption');
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
if (image) {
|
|
|
|
return {
|
|
|
|
type: 'image',
|
|
|
|
payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2019-07-05 16:50:05 +03:00
|
|
|
_onDrop(draggableInfo/*, droppableElem, position*/) {
|
2019-06-27 18:34:23 +03:00
|
|
|
// do not allow dropping of non-images
|
|
|
|
if (draggableInfo.type !== 'image' && draggableInfo.cardName !== 'image') {
|
2018-12-11 13:05:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:50:05 +03:00
|
|
|
let {insertIndex} = draggableInfo;
|
2018-12-11 13:05:59 +03:00
|
|
|
let droppables = Array.from(this.element.querySelectorAll('[data-image]'));
|
|
|
|
let draggableIndex = droppables.indexOf(draggableInfo.element);
|
|
|
|
|
2021-09-06 13:57:42 +03:00
|
|
|
if (!this.images.length) {
|
|
|
|
insertIndex = 0;
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:50:05 +03:00
|
|
|
if (this._isDropAllowed(draggableIndex, insertIndex)) {
|
2019-06-27 18:34:23 +03:00
|
|
|
if (draggableIndex === -1) {
|
|
|
|
// external image being added
|
|
|
|
let {payload} = draggableInfo;
|
|
|
|
let img = draggableInfo.element.querySelector(`img[src="${payload.src}"]`);
|
|
|
|
|
|
|
|
// image card payloads may not have all of the details we need but we can fill them in
|
|
|
|
payload.width = payload.width || img.naturalWidth;
|
|
|
|
payload.height = payload.height || img.naturalHeight;
|
|
|
|
if (!payload.fileName) {
|
2021-09-06 16:30:03 +03:00
|
|
|
let url = new URL(payload.src || img.src);
|
2019-06-27 18:34:23 +03:00
|
|
|
let fileName = url.pathname.match(/\/([^/]*)$/)[1];
|
|
|
|
payload.fileName = fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.images.insertAt(insertIndex, EmberObject.create(payload));
|
|
|
|
} else {
|
|
|
|
// internal image being re-ordered
|
|
|
|
let draggedImage = this.images.findBy('src', draggableInfo.payload.src);
|
2019-07-05 16:50:05 +03:00
|
|
|
let accountForRemoval = draggableIndex < insertIndex && insertIndex ? -1 : 0;
|
2019-06-27 18:34:23 +03:00
|
|
|
this.images.removeObject(draggedImage);
|
2019-07-05 16:50:05 +03:00
|
|
|
this.images.insertAt(insertIndex + accountForRemoval, draggedImage);
|
2019-06-27 18:34:23 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
this._recalculateImageRows();
|
2019-06-27 18:34:23 +03:00
|
|
|
this._buildAndSaveImagesPayload();
|
|
|
|
this._dragDropContainer.refresh();
|
|
|
|
|
|
|
|
this._skipOnDragEnd = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2019-06-27 18:34:23 +03:00
|
|
|
|
|
|
|
// if an image is dragged out of a gallery we need to remove it
|
|
|
|
_onDropEnd(draggableInfo, success) {
|
|
|
|
if (this._skipOnDragEnd || !success) {
|
|
|
|
this._skipOnDragEnd = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
2019-06-27 18:34:23 +03:00
|
|
|
let image = this.images.findBy('src', draggableInfo.payload.src);
|
|
|
|
if (image) {
|
|
|
|
this.images.removeObject(image);
|
|
|
|
this._recalculateImageRows();
|
2018-12-11 13:05:59 +03:00
|
|
|
this._buildAndSaveImagesPayload();
|
|
|
|
this._dragDropContainer.refresh();
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
// returns {
|
|
|
|
// direction: 'horizontal' TODO: use a constant?
|
|
|
|
// position: 'left'/'right' TODO: use constants?
|
|
|
|
// beforeElems: array of elems to left of indicator
|
|
|
|
// afterElems: array of elems to right of indicator
|
|
|
|
// droppableIndex:
|
|
|
|
// }
|
|
|
|
_getDropIndicatorPosition(draggableInfo, droppableElem, position) {
|
2019-06-27 18:34:23 +03:00
|
|
|
// do not allow dropping of non-images
|
|
|
|
if (draggableInfo.type !== 'image' && draggableInfo.cardName !== 'image') {
|
2018-12-11 13:05:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let row = droppableElem.closest('[data-row]');
|
|
|
|
let droppables = Array.from(this.element.querySelectorAll('[data-image]'));
|
|
|
|
let draggableIndex = droppables.indexOf(draggableInfo.element);
|
|
|
|
let droppableIndex = droppables.indexOf(droppableElem);
|
|
|
|
|
|
|
|
if (row && this._isDropAllowed(draggableIndex, droppableIndex, position)) {
|
|
|
|
let rowImages = Array.from(row.querySelectorAll('[data-image]'));
|
|
|
|
let rowDroppableIndex = rowImages.indexOf(droppableElem);
|
|
|
|
let insertIndex = droppableIndex;
|
|
|
|
let beforeElems = [];
|
|
|
|
let afterElems = [];
|
|
|
|
|
|
|
|
rowImages.forEach((image, index) => {
|
|
|
|
if (index < rowDroppableIndex) {
|
|
|
|
beforeElems.push(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index === rowDroppableIndex) {
|
|
|
|
if (position.match(/left/)) {
|
|
|
|
afterElems.push(image);
|
|
|
|
} else {
|
|
|
|
beforeElems.push(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index > rowDroppableIndex) {
|
|
|
|
afterElems.push(image);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-05 16:50:05 +03:00
|
|
|
if (position.match(/right/)) {
|
2018-12-11 13:05:59 +03:00
|
|
|
insertIndex += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
direction: 'horizontal',
|
|
|
|
position: position.match(/left/) ? 'left' : 'right',
|
|
|
|
beforeElems,
|
|
|
|
afterElems,
|
|
|
|
insertIndex
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|
2018-12-11 13:05:59 +03:00
|
|
|
|
|
|
|
// we don't allow an image to be dropped where it would end up in the
|
|
|
|
// same position within the gallery
|
|
|
|
_isDropAllowed(draggableIndex, droppableIndex, position = '') {
|
2019-06-27 18:34:23 +03:00
|
|
|
// external images can always be dropped
|
|
|
|
if (draggableIndex === -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:13:51 +03:00
|
|
|
// can't drop on itself or when droppableIndex doesn't exist
|
|
|
|
if (draggableIndex === droppableIndex || typeof droppableIndex === 'undefined') {
|
2018-12-11 13:05:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// account for dropping at beginning or end of a row
|
|
|
|
if (position.match(/left/)) {
|
|
|
|
droppableIndex -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position.match(/right/)) {
|
|
|
|
droppableIndex += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return droppableIndex !== draggableIndex;
|
2018-08-29 20:17:49 +03:00
|
|
|
}
|
2022-02-03 21:50:49 +03:00
|
|
|
}
|