forked from verdnatura/hedera-web
204 lines
3.8 KiB
JavaScript
204 lines
3.8 KiB
JavaScript
require('./style.scss');
|
|
|
|
/**
|
|
* Class to display or edit an image. Also it allows to show it's full version.
|
|
*/
|
|
module.exports = new Class({
|
|
Extends: Htk.Field
|
|
,Tag: 'htk-image'
|
|
,Properties:
|
|
{
|
|
/**
|
|
* The directory where the images are allocated.
|
|
*/
|
|
directory: {
|
|
type: String
|
|
,set(x) {
|
|
this._directory = x;
|
|
this._refreshSrc();
|
|
}
|
|
,get() {
|
|
return this._directory;
|
|
}
|
|
},
|
|
/**
|
|
* The subdirectory where the images are allocated.
|
|
*/
|
|
subdir: {
|
|
type: String
|
|
,set(x) {
|
|
this._subdir = x;
|
|
this._refreshSrc();
|
|
}
|
|
,get() {
|
|
return this._subdir;
|
|
}
|
|
},
|
|
/**
|
|
* The timestamp field of the last update, used for caching purposes.
|
|
*/
|
|
stampColumn: {
|
|
type: String
|
|
,set(x) {
|
|
this._stampColumn = x;
|
|
this._refreshSrc();
|
|
}
|
|
,get() {
|
|
return this._stampColumn;
|
|
}
|
|
},
|
|
/**
|
|
* Whether to show the full image when mouse hover.
|
|
*/
|
|
fullDir: {
|
|
type: String
|
|
,set(x) {
|
|
this._fullDir = x;
|
|
this._refreshClick();
|
|
}
|
|
,get() {
|
|
return this._fullDir;
|
|
}
|
|
},
|
|
/**
|
|
* The REST connection used to upload the image.
|
|
*/
|
|
conn: {
|
|
type: Vn.JsonConnection
|
|
}
|
|
}
|
|
|
|
,_directory: null
|
|
,_subdir: null
|
|
,_fullDir: null
|
|
,_error: false
|
|
,_editable: false
|
|
|
|
,render() {
|
|
var node = this.createRoot('div');
|
|
|
|
var img = this.img = this.createElement('img');
|
|
img.addEventListener('error', this._onImageError.bind(this));
|
|
node.appendChild(img);
|
|
|
|
this.setEditable();
|
|
this._refreshClick();
|
|
this._refreshSrc();
|
|
}
|
|
|
|
,_refreshClick() {
|
|
if (!this.node)
|
|
return;
|
|
|
|
if (this.clickHandler) {
|
|
Vn.Node.removeClass(this.node, 'clickable');
|
|
this.node.removeEventListener('click', this.clickHandler);
|
|
this.clickHander = null;
|
|
}
|
|
|
|
if (this._fullDir) {
|
|
this.clickHandler = this._onClick.bind(this);
|
|
this.node.addEventListener('click', this.clickHandler);
|
|
Vn.Node.addClass(this.node, 'clickable');
|
|
}
|
|
}
|
|
|
|
,setEditable() {
|
|
if (!this.node)
|
|
return;
|
|
|
|
if (this.editButton) {
|
|
this.node.removeChild(this.editButton);
|
|
this.editButton = null;
|
|
}
|
|
if (this._editable) {
|
|
var button = this.createElement('button');
|
|
button.addEventListener('click', this._onEditClick.bind(this));
|
|
button.title = _('UpdateImage');
|
|
this.node.appendChild(button);
|
|
|
|
var icon = new Htk.Icon({name: 'add_a_photo'});
|
|
button.appendChild(icon.node);
|
|
|
|
this.editButton = button;
|
|
}
|
|
}
|
|
|
|
,_makeSrc(subdir) {
|
|
var src = Vn.Config.imageUrl +'/';
|
|
|
|
if (this._directory)
|
|
src += this._directory +'/';
|
|
if (subdir)
|
|
src += subdir +'/';
|
|
|
|
src += this._value;
|
|
|
|
if (this._stamp)
|
|
src += '?'+ this._stamp;
|
|
else if (this._stampColumn && this.form)
|
|
src += '?'+ this.form.$[this._stampColumn];
|
|
|
|
return src;
|
|
}
|
|
|
|
,_refreshSrc() {
|
|
if (!this.node)
|
|
return;
|
|
|
|
if (this._value) {
|
|
this._error = false;
|
|
this.img.src = this._makeSrc(this._subdir);
|
|
} else
|
|
delete this.img.src;
|
|
}
|
|
|
|
,putValue() {
|
|
this._refreshSrc();
|
|
}
|
|
|
|
,_onImageError() {
|
|
this._error = true;
|
|
}
|
|
|
|
,_onClick(event) {
|
|
if (!this._fullDir || !this._value || this._error
|
|
|| event.defaultPrevented)
|
|
return;
|
|
|
|
event.preventDefault();
|
|
(new Htk.FullImage()).show(this._makeSrc(this._fullDir));
|
|
}
|
|
|
|
,_onEditClick(event) {
|
|
if (event.defaultPrevented) return
|
|
event.preventDefault();
|
|
|
|
var editor = new Htk.ImageEditor({conn: this.conn});
|
|
editor.setData(this.value, this._directory);
|
|
editor.on('name-changed', this._onNameChange, this);
|
|
editor.on('file-uploaded', this._onFileUpload, this);
|
|
editor.on('closed', this._onEditorClose, this);
|
|
|
|
this.popup = new Htk.Popup({
|
|
modal: true,
|
|
child: editor
|
|
});
|
|
this.popup.show(this.node);
|
|
}
|
|
|
|
,_onNameChange(editor, value) {
|
|
this.value = value;
|
|
}
|
|
|
|
,_onFileUpload(cell) {
|
|
this._stamp = Date.vnNew().getTime();
|
|
this._refreshSrc(cell);
|
|
this.popup.hide();
|
|
}
|
|
|
|
,_onEditorClose(editor) {
|
|
editor.disconnectByInstance(this);
|
|
}
|
|
});
|