}
*/
static update(data) {
return uAjax.post('utils/handleposition.php', data);
diff --git a/js/src/positiondialogmodel.js b/js/src/positiondialogmodel.js
index 72d26d0..7e78389 100644
--- a/js/src/positiondialogmodel.js
+++ b/js/src/positiondialogmodel.js
@@ -17,13 +17,15 @@
* along with this program; if not, see .
*/
-import { lang as $ } from './initializer.js';
+import { lang as $, config } from './initializer.js';
import ViewModel from './viewmodel.js';
import uAlert from './alert.js';
import uDialog from './dialog.js';
import uObserve from './observe.js';
import uUtils from './utils.js';
+const hiddenClass = 'hidden';
+
/**
* @class PositionDialogModel
*/
@@ -38,14 +40,22 @@ export default class PositionDialogModel extends ViewModel {
onPositionDelete: null,
onPositionUpdate: null,
onCancel: null,
- comment: ''
+ comment: null,
+ image: null,
+ onImageDelete: null
});
this.state = state;
this.positionIndex = positionIndex;
this.position = this.state.currentTrack.positions[positionIndex];
+ this.model.comment = this.position.hasComment() ? this.position.comment : '';
+ this.model.image = this.position.image;
this.model.onPositionDelete = () => this.onPositionDelete();
this.model.onPositionUpdate = () => this.onPositionUpdate();
this.model.onCancel = () => this.onCancel();
+ this.model.onImageDelete = () => this.onImageDelete();
+ this.onChanged('image', (image) => {
+ if (image && image !== this.position.image) { this.readImage(); }
+ });
}
init() {
@@ -53,6 +63,61 @@ export default class PositionDialogModel extends ViewModel {
this.dialog = new uDialog(html);
this.dialog.show();
this.bindAll(this.dialog.element);
+ this.previewEl = this.getBoundElement('imagePreview');
+ this.fileEl = this.getBoundElement('image');
+ this.imageDeleteEl = this.getBoundElement('onImageDelete');
+ this.initReader();
+ }
+
+ initReader() {
+ this.reader = new FileReader();
+ this.reader.addEventListener('load', () => {
+ this.showThumbnail();
+ }, false);
+ this.reader.addEventListener('error', () => {
+ this.model.image = this.position.image;
+ }, false);
+ }
+
+ readImage() {
+ const file = this.fileEl.files[0];
+ if (file) {
+ if (file.size > config.uploadMaxSize) {
+ uAlert.error($._('isizefailure', config.uploadMaxSize));
+ this.model.image = this.position.image;
+ return;
+ }
+ this.reader.readAsDataURL(file);
+ }
+ }
+
+ showThumbnail() {
+ this.previewEl.onload = () => this.toggleImage();
+ this.previewEl.onerror = () => {
+ uAlert.error($._('iuploadfailure'));
+ this.model.image = this.position.image;
+ };
+ this.previewEl.src = this.reader.result;
+ }
+
+ /**
+ * Toggle image visibility
+ */
+ toggleImage() {
+ if (this.previewEl.classList.contains(hiddenClass)) {
+ this.previewEl.classList.remove(hiddenClass);
+ this.imageDeleteEl.classList.remove(hiddenClass);
+ this.fileEl.classList.add(hiddenClass);
+ } else {
+ this.previewEl.classList.add(hiddenClass);
+ this.imageDeleteEl.classList.add(hiddenClass);
+ this.fileEl.classList.remove(hiddenClass);
+ }
+ }
+
+ onImageDelete() {
+ this.model.image = null;
+ this.toggleImage();
}
/**
@@ -64,7 +129,13 @@ export default class PositionDialogModel extends ViewModel {