2016-08-20 22:40:25 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const events = require('../events.js');
|
|
|
|
const views = require('../util/views.js');
|
|
|
|
const FileDropperControl = require('../controls/file_dropper_control.js');
|
|
|
|
|
|
|
|
const template = views.getTemplate('post-upload');
|
|
|
|
const rowTemplate = views.getTemplate('post-upload-row');
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
function _mimeTypeToPostType(mimeType) {
|
|
|
|
return {
|
|
|
|
'application/x-shockwave-flash': 'flash',
|
|
|
|
'image/gif': 'image',
|
|
|
|
'image/jpeg': 'image',
|
|
|
|
'image/png': 'image',
|
2019-10-05 16:34:12 +02:00
|
|
|
'image/webp': 'image',
|
2016-08-24 13:29:29 +02:00
|
|
|
'video/mp4': 'video',
|
|
|
|
'video/webm': 'video',
|
|
|
|
}[mimeType] || 'unknown';
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
class Uploadable extends events.EventTarget {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-01-07 11:07:51 +01:00
|
|
|
this.lookalikes = [];
|
|
|
|
this.lookalikesConfirmed = false;
|
2016-08-20 22:40:25 +02:00
|
|
|
this.safety = 'safe';
|
2016-11-27 21:52:40 +01:00
|
|
|
this.flags = [];
|
2017-01-07 11:07:51 +01:00
|
|
|
this.tags = [];
|
|
|
|
this.relations = [];
|
2016-08-20 22:40:25 +02:00
|
|
|
this.anonymous = false;
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:36:54 +02:00
|
|
|
destroy() {
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
get mimeType() {
|
|
|
|
return 'application/octet-stream';
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
get type() {
|
2016-08-24 13:29:29 +02:00
|
|
|
return _mimeTypeToPostType(this.mimeType);
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get key() {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class File extends Uploadable {
|
|
|
|
constructor(file) {
|
|
|
|
super();
|
|
|
|
this.file = file;
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
this._previewUrl = null;
|
2016-08-23 22:36:54 +02:00
|
|
|
if (URL && URL.createObjectURL) {
|
2016-08-24 13:29:29 +02:00
|
|
|
this._previewUrl = URL.createObjectURL(file);
|
2016-08-23 22:36:54 +02:00
|
|
|
} else {
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
reader.addEventListener('load', e => {
|
2016-08-24 13:29:29 +02:00
|
|
|
this._previewUrl = e.target.result;
|
2016-08-23 22:36:54 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent('finish', {detail: {uploadable: this}}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if (URL && URL.createObjectURL && URL.revokeObjectURL) {
|
2016-08-24 13:29:29 +02:00
|
|
|
URL.revokeObjectURL(this._previewUrl);
|
2016-08-23 22:36:54 +02:00
|
|
|
}
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
get mimeType() {
|
|
|
|
return this.file.type;
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
get previewUrl() {
|
|
|
|
return this._previewUrl;
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get key() {
|
|
|
|
return this.file.name + this.file.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this.file.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Url extends Uploadable {
|
|
|
|
constructor(url) {
|
|
|
|
super();
|
|
|
|
this.url = url;
|
|
|
|
this.dispatchEvent(new CustomEvent('finish'));
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
get mimeType() {
|
|
|
|
let mime = {
|
|
|
|
'swf': 'application/x-shockwave-flash',
|
|
|
|
'jpg': 'image/jpeg',
|
|
|
|
'png': 'image/png',
|
|
|
|
'gif': 'image/gif',
|
2019-10-05 16:34:12 +02:00
|
|
|
'webp': 'image/webp',
|
2016-08-24 13:29:29 +02:00
|
|
|
'mp4': 'video/mp4',
|
|
|
|
'webm': 'video/webm',
|
2016-08-20 22:40:25 +02:00
|
|
|
};
|
2016-08-24 13:29:29 +02:00
|
|
|
for (let extension of Object.keys(mime)) {
|
2016-08-20 22:40:25 +02:00
|
|
|
if (this.url.toLowerCase().indexOf('.' + extension) !== -1) {
|
2016-08-24 13:29:29 +02:00
|
|
|
return mime[extension];
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'unknown';
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:29:29 +02:00
|
|
|
get previewUrl() {
|
2016-08-20 22:40:25 +02:00
|
|
|
return this.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
get key() {
|
|
|
|
return this.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this.url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PostUploadView extends events.EventTarget {
|
2016-08-23 21:50:22 +02:00
|
|
|
constructor(ctx) {
|
2016-08-20 22:40:25 +02:00
|
|
|
super();
|
2016-08-23 21:50:22 +02:00
|
|
|
this._ctx = ctx;
|
2016-08-20 22:40:25 +02:00
|
|
|
this._hostNode = document.getElementById('content-holder');
|
2016-09-29 21:36:59 +02:00
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
views.replaceContent(this._hostNode, template());
|
|
|
|
views.syncScrollPosition();
|
|
|
|
|
2016-09-29 21:36:59 +02:00
|
|
|
this._cancelButtonNode.disabled = true;
|
|
|
|
|
2017-01-08 21:30:40 +01:00
|
|
|
this._uploadables = [];
|
|
|
|
this._uploadables.find = u => {
|
|
|
|
return this._uploadables.findIndex(u2 => u.key === u2.key);
|
|
|
|
};
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
this._contentFileDropper = new FileDropperControl(
|
|
|
|
this._contentInputNode,
|
|
|
|
{
|
2017-02-12 10:40:50 +01:00
|
|
|
extraText:
|
|
|
|
'Allowed extensions: .jpg, .png, .gif, .webm, .mp4, .swf',
|
2016-08-20 22:40:25 +02:00
|
|
|
allowUrls: true,
|
|
|
|
allowMultiple: true,
|
|
|
|
lock: false,
|
|
|
|
});
|
|
|
|
this._contentFileDropper.addEventListener(
|
|
|
|
'fileadd', e => this._evtFilesAdded(e));
|
|
|
|
this._contentFileDropper.addEventListener(
|
|
|
|
'urladd', e => this._evtUrlsAdded(e));
|
|
|
|
|
2016-09-29 21:36:59 +02:00
|
|
|
this._cancelButtonNode.addEventListener(
|
|
|
|
'click', e => this._evtCancelButtonClick(e));
|
2016-08-20 22:40:25 +02:00
|
|
|
this._formNode.addEventListener('submit', e => this._evtFormSubmit(e));
|
|
|
|
this._formNode.classList.add('inactive');
|
|
|
|
}
|
|
|
|
|
|
|
|
enableForm() {
|
|
|
|
views.enableForm(this._formNode);
|
2016-09-29 21:36:59 +02:00
|
|
|
this._cancelButtonNode.disabled = true;
|
2016-09-29 22:26:37 +02:00
|
|
|
this._formNode.classList.remove('uploading');
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disableForm() {
|
|
|
|
views.disableForm(this._formNode);
|
2016-09-29 21:36:59 +02:00
|
|
|
this._cancelButtonNode.disabled = false;
|
2016-09-29 22:26:37 +02:00
|
|
|
this._formNode.classList.add('uploading');
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clearMessages() {
|
|
|
|
views.clearMessages(this._hostNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
showSuccess(message) {
|
|
|
|
views.showSuccess(this._hostNode, message);
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
showError(message, uploadable) {
|
|
|
|
this._showMessage(views.showError, message, uploadable);
|
|
|
|
}
|
|
|
|
|
|
|
|
showInfo(message, uploadable) {
|
|
|
|
this._showMessage(views.showInfo, message, uploadable);
|
2017-01-08 02:31:07 +01:00
|
|
|
views.appendExclamationMark();
|
2017-01-06 14:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_showMessage(functor, message, uploadable) {
|
|
|
|
functor(uploadable ? uploadable.rowNode : this._hostNode, message);
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addUploadables(uploadables) {
|
|
|
|
this._formNode.classList.remove('inactive');
|
|
|
|
let duplicatesFound = 0;
|
|
|
|
for (let uploadable of uploadables) {
|
2017-01-08 21:30:40 +01:00
|
|
|
if (this._uploadables.find(uploadable) !== -1) {
|
2016-08-20 22:40:25 +02:00
|
|
|
duplicatesFound++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-08 21:30:40 +01:00
|
|
|
this._uploadables.push(uploadable);
|
2016-08-20 22:40:25 +02:00
|
|
|
this._emit('change');
|
2017-01-06 14:05:54 +01:00
|
|
|
this._renderRowNode(uploadable);
|
2016-08-20 22:40:25 +02:00
|
|
|
uploadable.addEventListener(
|
2017-01-06 14:05:54 +01:00
|
|
|
'finish', e => this._updateThumbnailNode(e.detail.uploadable));
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
if (duplicatesFound) {
|
|
|
|
let message = null;
|
|
|
|
if (duplicatesFound < uploadables.length) {
|
|
|
|
message = 'Some of the files were already added ' +
|
|
|
|
'and have been skipped.';
|
|
|
|
} else if (duplicatesFound === 1) {
|
|
|
|
message = 'This file was already added.';
|
|
|
|
} else {
|
|
|
|
message = 'These files were already added.';
|
|
|
|
}
|
|
|
|
alert(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeUploadable(uploadable) {
|
2017-01-08 21:30:40 +01:00
|
|
|
if (this._uploadables.find(uploadable) === -1) {
|
2016-08-20 22:40:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-23 22:36:54 +02:00
|
|
|
uploadable.destroy();
|
2016-08-20 22:40:25 +02:00
|
|
|
uploadable.rowNode.parentNode.removeChild(uploadable.rowNode);
|
2017-01-08 21:30:40 +01:00
|
|
|
this._uploadables.splice(this._uploadables.find(uploadable), 1);
|
2016-08-20 22:40:25 +02:00
|
|
|
this._emit('change');
|
2017-01-08 21:30:40 +01:00
|
|
|
if (!this._uploadables.length) {
|
2016-08-20 22:40:25 +02:00
|
|
|
this._formNode.classList.add('inactive');
|
2017-01-06 14:05:54 +01:00
|
|
|
this._submitButtonNode.value = 'Upload all';
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 11:07:51 +01:00
|
|
|
updateUploadable(uploadable) {
|
|
|
|
uploadable.lookalikesConfirmed = true;
|
|
|
|
this._renderRowNode(uploadable);
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
_evtFilesAdded(e) {
|
|
|
|
this.addUploadables(e.detail.files.map(file => new File(file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtUrlsAdded(e) {
|
|
|
|
this.addUploadables(e.detail.urls.map(url => new Url(url)));
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:36:59 +02:00
|
|
|
_evtCancelButtonClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this._emit('cancel');
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
_evtFormSubmit(e) {
|
|
|
|
e.preventDefault();
|
2017-01-08 21:30:40 +01:00
|
|
|
for (let uploadable of this._uploadables) {
|
2017-01-06 14:05:54 +01:00
|
|
|
this._updateUploadableFromDom(uploadable);
|
|
|
|
}
|
|
|
|
this._submitButtonNode.value = 'Resume upload';
|
2016-08-20 22:40:25 +02:00
|
|
|
this._emit('submit');
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_updateUploadableFromDom(uploadable) {
|
|
|
|
const rowNode = uploadable.rowNode;
|
2017-03-30 20:50:12 +02:00
|
|
|
|
|
|
|
const safetyNode = rowNode.querySelector('.safety input:checked');
|
|
|
|
if (safetyNode) {
|
|
|
|
uploadable.safety = safetyNode.value;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:51:38 +02:00
|
|
|
const anonymousNode = rowNode.querySelector('.anonymous input:checked');
|
|
|
|
if (anonymousNode) {
|
|
|
|
uploadable.anonymous = true;
|
|
|
|
}
|
2017-03-30 20:50:12 +02:00
|
|
|
|
2017-01-07 11:07:51 +01:00
|
|
|
uploadable.tags = [];
|
|
|
|
uploadable.relations = [];
|
|
|
|
for (let [i, lookalike] of uploadable.lookalikes.entries()) {
|
|
|
|
let lookalikeNode = rowNode.querySelector(
|
|
|
|
`.lookalikes li:nth-child(${i + 1})`);
|
|
|
|
if (lookalikeNode.querySelector('[name=copy-tags]').checked) {
|
2018-03-09 07:19:17 +01:00
|
|
|
uploadable.tags = uploadable.tags.concat(lookalike.post.tagNames);
|
2017-01-07 11:07:51 +01:00
|
|
|
}
|
|
|
|
if (lookalikeNode.querySelector('[name=add-relation]').checked) {
|
|
|
|
uploadable.relations.push(lookalike.post.id);
|
|
|
|
}
|
|
|
|
}
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_evtRemoveClick(e, uploadable) {
|
2016-08-24 12:59:48 +02:00
|
|
|
e.preventDefault();
|
2016-09-29 22:26:37 +02:00
|
|
|
if (this._uploading) {
|
2016-09-29 12:39:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-01-06 14:05:54 +01:00
|
|
|
this.removeUploadable(uploadable);
|
2016-08-24 12:59:48 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_evtMoveClick(e, uploadable, delta) {
|
2016-08-24 12:59:48 +02:00
|
|
|
e.preventDefault();
|
2016-09-29 22:26:37 +02:00
|
|
|
if (this._uploading) {
|
2016-09-29 12:39:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-01-08 21:30:40 +01:00
|
|
|
let index = this._uploadables.find(uploadable);
|
|
|
|
if ((index + delta).between(-1, this._uploadables.length)) {
|
|
|
|
let uploadable1 = this._uploadables[index];
|
|
|
|
let uploadable2 = this._uploadables[index + delta];
|
|
|
|
this._uploadables[index] = uploadable2;
|
|
|
|
this._uploadables[index + delta] = uploadable1;
|
2017-01-06 14:05:54 +01:00
|
|
|
if (delta === 1) {
|
2017-01-08 21:30:40 +01:00
|
|
|
this._listNode.insertBefore(
|
|
|
|
uploadable2.rowNode, uploadable1.rowNode);
|
2017-01-06 14:05:54 +01:00
|
|
|
} else {
|
2017-01-08 21:30:40 +01:00
|
|
|
this._listNode.insertBefore(
|
|
|
|
uploadable1.rowNode, uploadable2.rowNode);
|
2017-01-06 14:05:54 +01:00
|
|
|
}
|
2016-11-11 22:35:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 12:59:48 +02:00
|
|
|
_emit(eventType) {
|
2016-08-20 22:40:25 +02:00
|
|
|
this.dispatchEvent(
|
|
|
|
new CustomEvent(
|
2016-08-24 12:59:48 +02:00
|
|
|
eventType,
|
2016-09-29 22:26:37 +02:00
|
|
|
{detail: {
|
2017-01-08 21:30:40 +01:00
|
|
|
uploadables: this._uploadables,
|
2016-09-29 22:26:37 +02:00
|
|
|
skipDuplicates: this._skipDuplicatesCheckboxNode.checked,
|
|
|
|
}}));
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_renderRowNode(uploadable) {
|
2016-08-23 21:50:22 +02:00
|
|
|
const rowNode = rowTemplate(Object.assign(
|
|
|
|
{}, this._ctx, {uploadable: uploadable}));
|
2017-01-06 14:05:54 +01:00
|
|
|
if (uploadable.rowNode) {
|
|
|
|
uploadable.rowNode.parentNode.replaceChild(
|
|
|
|
rowNode, uploadable.rowNode);
|
|
|
|
} else {
|
|
|
|
this._listNode.appendChild(rowNode);
|
|
|
|
}
|
2016-08-23 21:50:22 +02:00
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
uploadable.rowNode = rowNode;
|
2016-11-11 22:35:58 +01:00
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
rowNode.querySelector('a.remove').addEventListener('click',
|
2016-11-11 22:35:58 +01:00
|
|
|
e => this._evtRemoveClick(e, uploadable));
|
2017-01-06 14:05:54 +01:00
|
|
|
rowNode.querySelector('a.move-up').addEventListener('click',
|
|
|
|
e => this._evtMoveClick(e, uploadable, -1));
|
|
|
|
rowNode.querySelector('a.move-down').addEventListener('click',
|
|
|
|
e => this._evtMoveClick(e, uploadable, 1));
|
2016-08-20 22:40:25 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 14:05:54 +01:00
|
|
|
_updateThumbnailNode(uploadable) {
|
2016-08-23 21:50:22 +02:00
|
|
|
const rowNode = rowTemplate(Object.assign(
|
|
|
|
{}, this._ctx, {uploadable: uploadable}));
|
2016-08-20 22:40:25 +02:00
|
|
|
views.replaceContent(
|
|
|
|
uploadable.rowNode.querySelector('.thumbnail'),
|
|
|
|
rowNode.querySelector('.thumbnail').childNodes);
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:26:37 +02:00
|
|
|
get _uploading() {
|
|
|
|
return this._formNode.classList.contains('uploading');
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
get _listNode() {
|
|
|
|
return this._hostNode.querySelector('.uploadables-container');
|
|
|
|
}
|
|
|
|
|
|
|
|
get _formNode() {
|
|
|
|
return this._hostNode.querySelector('form');
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:26:37 +02:00
|
|
|
get _skipDuplicatesCheckboxNode() {
|
|
|
|
return this._hostNode.querySelector('form [name=skip-duplicates]');
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
get _submitButtonNode() {
|
|
|
|
return this._hostNode.querySelector('form [type=submit]');
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:36:59 +02:00
|
|
|
get _cancelButtonNode() {
|
|
|
|
return this._hostNode.querySelector('form .cancel');
|
|
|
|
}
|
|
|
|
|
2016-08-20 22:40:25 +02:00
|
|
|
get _contentInputNode() {
|
|
|
|
return this._formNode.querySelector('.dropper-container');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostUploadView;
|