client/posts: fix reordering uploads

fixes #111
This commit is contained in:
rr- 2017-01-08 21:30:40 +01:00
parent 133ed522da
commit a703195c6c

View file

@ -7,8 +7,6 @@ const FileDropperControl = require('../controls/file_dropper_control.js');
const template = views.getTemplate('post-upload'); const template = views.getTemplate('post-upload');
const rowTemplate = views.getTemplate('post-upload-row'); const rowTemplate = views.getTemplate('post-upload-row');
let globalOrder = 0;
function _mimeTypeToPostType(mimeType) { function _mimeTypeToPostType(mimeType) {
return { return {
'application/x-shockwave-flash': 'flash', 'application/x-shockwave-flash': 'flash',
@ -30,8 +28,6 @@ class Uploadable extends events.EventTarget {
this.tags = []; this.tags = [];
this.relations = []; this.relations = [];
this.anonymous = false; this.anonymous = false;
this.order = globalOrder;
globalOrder++;
} }
destroy() { destroy() {
@ -152,7 +148,11 @@ class PostUploadView extends events.EventTarget {
this._cancelButtonNode.disabled = true; this._cancelButtonNode.disabled = true;
this._uploadables = new Map(); this._uploadables = [];
this._uploadables.find = u => {
return this._uploadables.findIndex(u2 => u.key === u2.key);
};
this._contentFileDropper = new FileDropperControl( this._contentFileDropper = new FileDropperControl(
this._contentInputNode, this._contentInputNode,
{ {
@ -208,11 +208,11 @@ class PostUploadView extends events.EventTarget {
this._formNode.classList.remove('inactive'); this._formNode.classList.remove('inactive');
let duplicatesFound = 0; let duplicatesFound = 0;
for (let uploadable of uploadables) { for (let uploadable of uploadables) {
if (this._uploadables.has(uploadable.key)) { if (this._uploadables.find(uploadable) !== -1) {
duplicatesFound++; duplicatesFound++;
continue; continue;
} }
this._uploadables.set(uploadable.key, uploadable); this._uploadables.push(uploadable);
this._emit('change'); this._emit('change');
this._renderRowNode(uploadable); this._renderRowNode(uploadable);
uploadable.addEventListener( uploadable.addEventListener(
@ -233,15 +233,14 @@ class PostUploadView extends events.EventTarget {
} }
removeUploadable(uploadable) { removeUploadable(uploadable) {
if (!this._uploadables.has(uploadable.key)) { if (this._uploadables.find(uploadable) === -1) {
return; return;
} }
uploadable.destroy(); uploadable.destroy();
uploadable.rowNode.parentNode.removeChild(uploadable.rowNode); uploadable.rowNode.parentNode.removeChild(uploadable.rowNode);
this._uploadables.delete(uploadable.key); this._uploadables.splice(this._uploadables.find(uploadable), 1);
this._normalizeUploadablesOrder();
this._emit('change'); this._emit('change');
if (!this._uploadables.size) { if (!this._uploadables.length) {
this._formNode.classList.add('inactive'); this._formNode.classList.add('inactive');
this._submitButtonNode.value = 'Upload all'; this._submitButtonNode.value = 'Upload all';
} }
@ -267,7 +266,7 @@ class PostUploadView extends events.EventTarget {
_evtFormSubmit(e) { _evtFormSubmit(e) {
e.preventDefault(); e.preventDefault();
for (let uploadable of this._uploadables.values()) { for (let uploadable of this._uploadables) {
this._updateUploadableFromDom(uploadable); this._updateUploadableFromDom(uploadable);
} }
this._submitButtonNode.value = 'Resume upload'; this._submitButtonNode.value = 'Resume upload';
@ -310,40 +309,28 @@ class PostUploadView extends events.EventTarget {
if (this._uploading) { if (this._uploading) {
return; return;
} }
let sortedUploadables = this._getSortedUploadables(); let index = this._uploadables.find(uploadable);
if ((uploadable.order + delta).between(-1, sortedUploadables.length)) { if ((index + delta).between(-1, this._uploadables.length)) {
uploadable.order += delta; let uploadable1 = this._uploadables[index];
const otherUploadable = sortedUploadables[uploadable.order]; let uploadable2 = this._uploadables[index + delta];
otherUploadable.order -= delta; this._uploadables[index] = uploadable2;
this._uploadables[index + delta] = uploadable1;
if (delta === 1) { if (delta === 1) {
uploadable.rowNode.parentNode.insertBefore( this._listNode.insertBefore(
otherUploadable.rowNode, uploadable.rowNode); uploadable2.rowNode, uploadable1.rowNode);
} else { } else {
uploadable.rowNode.parentNode.insertBefore( this._listNode.insertBefore(
uploadable.rowNode, otherUploadable.rowNode); uploadable1.rowNode, uploadable2.rowNode);
} }
} }
} }
_normalizeUploadablesOrder() {
let sortedUploadables = this._getSortedUploadables();
for (let i = 0; i < sortedUploadables.length; i++) {
sortedUploadables[i].order = i;
}
}
_getSortedUploadables() {
let sortedUploadables = [...this._uploadables.values()];
sortedUploadables.sort((a, b) => a.order - b.order);
return sortedUploadables;
}
_emit(eventType) { _emit(eventType) {
this.dispatchEvent( this.dispatchEvent(
new CustomEvent( new CustomEvent(
eventType, eventType,
{detail: { {detail: {
uploadables: this._getSortedUploadables(), uploadables: this._uploadables,
skipDuplicates: this._skipDuplicatesCheckboxNode.checked, skipDuplicates: this._skipDuplicatesCheckboxNode.checked,
}})); }}));
} }