scinote-web/app/javascript/vue/shared/content/mixins/attachments.js

157 lines
5.5 KiB
JavaScript
Raw Normal View History

/* global ActiveStorage GLOBAL_CONSTANTS Promise I18n */
2022-05-19 14:51:39 +08:00
export default {
2022-05-19 17:13:34 +08:00
data() {
return {
viewModeOrder: {
inline: 0,
thumbnail: 1,
list: 2
}
};
},
computed: {
attachmentsParent() {
return this.step || this.result;
},
attachmentsParentName() {
return this.step ? 'step' : 'result';
}
},
2022-05-19 14:51:39 +08:00
methods: {
2022-05-24 21:54:22 +08:00
dropFile(e) {
if (!this.showFileModal && e.dataTransfer && e.dataTransfer.files.length) {
this.dragingFile = false;
this.uploadFiles(e.dataTransfer.files);
}
},
2023-07-26 22:26:39 +08:00
openLoadFromComputer() {
this.$refs.fileSelector.click();
},
loadFromComputer() {
this.uploadFiles(this.$refs.fileSelector.files);
},
openMarvinJsModal(button) {
2023-10-05 21:44:41 +08:00
MarvinJsEditor.initNewButton('.new-marvinjs-upload-button', this.loadAttachments);
2023-07-26 22:26:39 +08:00
button.click();
},
openWopiFileModal() {
this.initWopiFileModal(this.attachmentsParent, (_e, data, status) => {
2023-07-26 22:26:39 +08:00
if (status === 'success') {
this.addAttachment(data)
} else {
HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger');
}
});
},
addAttachment(attachment) {
this.attachments.push(attachment);
this.showFileModal = false;
},
2022-05-19 14:51:39 +08:00
uploadFiles(files) {
const filesToUploadCntr = files.length;
let filesUploadedCntr = 0;
this.showFileModal = false;
if (!this.attachmentsParent.attributes.urls.upload_attachment_url) return false;
2022-05-19 14:51:39 +08:00
return new Promise((resolve, reject) => {
$(files).each((_, file) => {
const fileObject = {
attributes: {
progress: 0,
view_mode: this.attachmentsParent.attributes.assets_view_mode,
file_name: file.name,
uploading: true,
asset_order: this.viewModeOrder[this.attachmentsParent.attributes.assets_view_mode]
},
directUploadWillStoreFileWithXHR(request) {
request.upload.addEventListener('progress', (e) => {
// Progress checking
this.attributes.progress = parseInt((e.loaded / e.total) * 100, 10);
});
}
};
2022-05-19 14:51:39 +08:00
if (file.size > GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB * 1024 * 1024) {
2023-08-11 20:48:20 +08:00
fileObject.error = I18n.t('attachments.new.file_too_big');
this.attachments.push(fileObject);
2022-05-19 14:51:39 +08:00
return;
}
const storageLimit = this.attachmentsParent.attributes.storage_limit &&
this.attachmentsParent.attributes.storage_limit.total > 0 &&
this.attachmentsParent.attributes.storage_limit.used >= this.attachmentsParent.attributes.storage_limit.total;
if (storageLimit) {
2023-08-11 20:48:20 +08:00
fileObject.error = I18n.t('attachments.new.no_more_space');
this.attachments.push(fileObject);
return;
}
const upload = new ActiveStorage.DirectUpload(file, this.attachmentsParent.attributes.urls.direct_upload_url, fileObject);
fileObject.isNewUpload = true;
this.attachments.push(fileObject);
const filePosition = this.attachments.length - 1;
2022-05-19 14:51:39 +08:00
upload.create((error, blob) => {
if (error) {
2023-08-11 20:48:20 +08:00
fileObject.error = I18n.t('attachments.new.general_error');
this.attachments.splice(filePosition, 1);
setTimeout(() => {
this.attachments.push(fileObject);
}, 0);
2022-05-19 14:51:39 +08:00
reject(error);
} else {
const signedId = blob.signed_id;
$.post(this.attachmentsParent.attributes.urls.upload_attachment_url, {
2022-05-19 14:51:39 +08:00
signed_blob_id: signedId
}, (result) => {
fileObject.id = result.data.id;
fileObject.attributes = result.data.attributes;
this.attachments.splice(filePosition, 1);
setTimeout(() => {
this.attachments.push(fileObject);
}, 0);
}).fail(() => {
2023-08-11 20:48:20 +08:00
fileObject.error = I18n.t('attachments.new.general_error');
this.attachments.splice(filePosition, 1);
setTimeout(() => {
this.attachments.push(fileObject);
}, 0);
2022-05-19 14:51:39 +08:00
});
filesUploadedCntr += 1;
if (filesUploadedCntr === filesToUploadCntr) {
setTimeout(() => {
this.$emit(`${this.attachmentsParentName}Updated`);
}, 1000);
2022-05-19 14:51:39 +08:00
resolve('done');
}
}
});
});
});
2022-05-19 17:13:34 +08:00
},
changeAttachmentsOrder(order) {
this.attachmentsParent.attributes.assets_order = order;
$.post(this.attachmentsParent.attributes.urls.update_view_state_url, {
2022-05-19 17:13:34 +08:00
assets: { order }
});
},
changeAttachmentsViewMode(viewMode) {
this.attachmentsParent.attributes.assets_view_mode = viewMode;
2022-05-19 17:13:34 +08:00
this.attachments.forEach((attachment) => {
attachment.attributes['view_mode'] = viewMode;
attachment.attributes['asset_order'] = this.viewModeOrder[viewMode];
2022-05-19 17:13:34 +08:00
});
$.post(this.attachmentsParent.attributes.urls.update_asset_view_mode_url, {
2022-05-19 17:13:34 +08:00
assets_view_mode: viewMode
});
},
updateAttachmentViewMode(id, viewMode) {
const attachment = this.attachments.find(e => e.id === id);
attachment.attributes['view_mode'] = viewMode;
attachment.attributes['asset_order'] = this.viewModeOrder[viewMode];
2022-05-19 14:51:39 +08:00
}
}
};