2015-09-16 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Class to display or edit an image. Also it allows to show it's full version.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
module.exports = new Class({
|
2015-11-09 08:14:33 +00:00
|
|
|
Extends: Htk.Field
|
2015-01-23 13:09:30 +00:00
|
|
|
,Tag: 'htk-image'
|
2015-03-09 08:36:54 +00:00
|
|
|
,Properties:
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The directory where the images are allocated.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
directory: {
|
2015-03-09 08:36:54 +00:00
|
|
|
type: String
|
2019-05-27 13:38:06 +00:00
|
|
|
,set: function(x) {
|
2015-03-09 08:36:54 +00:00
|
|
|
this._directory = x;
|
2019-05-27 13:38:06 +00:00
|
|
|
this._refreshSrc();
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
2019-05-27 13:38:06 +00:00
|
|
|
,get: function() {
|
2015-03-09 08:36:54 +00:00
|
|
|
return this._directory;
|
|
|
|
}
|
2015-09-16 16:11:15 +00:00
|
|
|
},
|
|
|
|
/**
|
2016-05-02 13:05:49 +00:00
|
|
|
* The subdirectory where the images are allocated.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
subdir: {
|
2015-09-16 16:11:15 +00:00
|
|
|
type: String
|
2019-05-27 13:38:06 +00:00
|
|
|
,set: function(x) {
|
2015-09-16 16:11:15 +00:00
|
|
|
this._subdir = x;
|
2019-05-27 13:38:06 +00:00
|
|
|
this._refreshSrc();
|
2015-09-16 16:11:15 +00:00
|
|
|
}
|
2019-05-27 13:38:06 +00:00
|
|
|
,get: function() {
|
2015-09-16 16:11:15 +00:00
|
|
|
return this._subdir;
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 13:38:06 +00:00
|
|
|
/**
|
|
|
|
* The timestamp field of the last update, used for caching purposes.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
stampColumn: {
|
|
|
|
type: String
|
|
|
|
,set: function(x) {
|
|
|
|
this._stampColumn = x;
|
|
|
|
this._refreshSrc();
|
|
|
|
}
|
|
|
|
,get: function() {
|
|
|
|
return this._stampColumn;
|
|
|
|
}
|
|
|
|
},
|
2015-09-16 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Whether to show the full image when mouse hover.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
fullDir: {
|
2015-09-16 16:11:15 +00:00
|
|
|
type: String
|
2019-05-27 13:38:06 +00:00
|
|
|
,set: function(x) {
|
2015-09-16 16:11:15 +00:00
|
|
|
this._fullDir = x;
|
2019-05-27 13:38:06 +00:00
|
|
|
this._refreshClick();
|
2015-09-16 16:11:15 +00:00
|
|
|
}
|
2019-05-27 13:38:06 +00:00
|
|
|
,get: function() {
|
2015-09-16 16:11:15 +00:00
|
|
|
return this._fullDir;
|
|
|
|
}
|
2016-09-24 14:32:31 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* The REST connection used to upload the image.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2019-05-27 13:38:06 +00:00
|
|
|
conn: {
|
2016-09-24 14:32:31 +00:00
|
|
|
type: Vn.JsonConnection
|
2015-03-09 08:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
,_directory: null
|
2015-09-16 16:11:15 +00:00
|
|
|
,_subdir: null
|
|
|
|
,_fullDir: null
|
2016-05-02 13:05:49 +00:00
|
|
|
,_error: false
|
2015-12-10 13:48:43 +00:00
|
|
|
,_editable: false
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,render: function() {
|
|
|
|
var node = this.createRoot('div');
|
2016-05-02 13:05:49 +00:00
|
|
|
node.className = 'htk-image';
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
var img = this.img = this.createElement('img');
|
|
|
|
img.addEventListener('error', this._onImageError.bind(this));
|
|
|
|
node.appendChild(img);
|
2016-05-02 13:05:49 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
this.setEditable();
|
|
|
|
this._refreshClick();
|
|
|
|
this._refreshSrc();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_refreshClick: function() {
|
2016-05-02 13:05:49 +00:00
|
|
|
if (!this.node)
|
|
|
|
return;
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
if (this.clickHandler) {
|
|
|
|
Vn.Node.removeClass(this.node, 'clickable');
|
|
|
|
this.node.removeEventListener('click', this.clickHandler);
|
2016-05-02 13:05:49 +00:00
|
|
|
this.clickHander = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
if (this._fullDir) {
|
|
|
|
this.clickHandler = this._onClick.bind(this);
|
|
|
|
this.node.addEventListener('click', this.clickHandler);
|
|
|
|
Vn.Node.addClass(this.node, 'clickable');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,setEditable: function() {
|
2016-05-02 13:05:49 +00:00
|
|
|
if (!this.node)
|
2015-12-10 13:48:43 +00:00
|
|
|
return;
|
2016-05-02 13:05:49 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
if (this.editButton) {
|
|
|
|
this.node.removeChild(this.editButton);
|
2016-05-02 13:05:49 +00:00
|
|
|
this.editButton = null;
|
|
|
|
}
|
2019-05-27 13:38:06 +00:00
|
|
|
if (this._editable) {
|
|
|
|
var button = this.createElement('button');
|
|
|
|
button.addEventListener('click', this._onEditClick.bind(this));
|
2016-05-02 13:05:49 +00:00
|
|
|
button.title = _('UpdateImage');
|
2019-05-27 13:38:06 +00:00
|
|
|
this.node.appendChild(button);
|
2016-05-02 13:05:49 +00:00
|
|
|
|
2022-05-28 00:37:24 +00:00
|
|
|
var icon = new Htk.Icon({name: 'add_a_photo'});
|
2019-05-27 13:38:06 +00:00
|
|
|
button.appendChild(icon.node);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2016-05-02 13:05:49 +00:00
|
|
|
this.editButton = button;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2016-05-02 13:05:49 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_makeSrc: function(subdir) {
|
2017-12-20 11:34:04 +00:00
|
|
|
var src = Vn.Config.imageUrl +'/';
|
2015-09-16 16:11:15 +00:00
|
|
|
|
2016-05-02 13:05:49 +00:00
|
|
|
if (this._directory)
|
|
|
|
src += this._directory +'/';
|
|
|
|
if (subdir)
|
|
|
|
src += subdir +'/';
|
|
|
|
|
|
|
|
src += this._value;
|
2019-05-27 13:38:06 +00:00
|
|
|
|
2015-09-16 16:11:15 +00:00
|
|
|
if (this._stamp)
|
|
|
|
src += '?'+ this._stamp;
|
2019-05-27 13:38:06 +00:00
|
|
|
else if (this._stampColumn && this.form)
|
2022-05-28 15:49:46 +00:00
|
|
|
src += '?'+ this.form.$[this._stampColumn];
|
2015-09-16 16:11:15 +00:00
|
|
|
|
2016-05-02 13:05:49 +00:00
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_refreshSrc: function() {
|
2016-05-02 13:05:49 +00:00
|
|
|
if (!this.node)
|
|
|
|
return;
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
if (this._value) {
|
2016-05-02 13:05:49 +00:00
|
|
|
this._error = false;
|
2019-05-27 13:38:06 +00:00
|
|
|
this.img.src = this._makeSrc(this._subdir);
|
|
|
|
} else
|
2016-05-02 13:05:49 +00:00
|
|
|
delete this.img.src;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,putValue: function() {
|
|
|
|
this._refreshSrc();
|
2015-09-16 16:11:15 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_onImageError: function() {
|
2016-05-02 13:05:49 +00:00
|
|
|
this._error = true;
|
|
|
|
}
|
|
|
|
|
2022-04-13 11:31:52 +00:00
|
|
|
,_onClick: function(event) {
|
2022-05-28 00:37:24 +00:00
|
|
|
if (!this._fullDir || !this._value || this._error
|
|
|
|
|| event.defaultPrevented)
|
2015-12-10 13:48:43 +00:00
|
|
|
return;
|
2022-04-13 11:31:52 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
2019-05-27 13:38:06 +00:00
|
|
|
(new Htk.FullImage()).show(this._makeSrc(this._fullDir));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_onEditClick: function(event) {
|
2022-05-28 00:37:24 +00:00
|
|
|
if (event.defaultPrevented) return
|
|
|
|
event.preventDefault();
|
2016-10-11 14:45:10 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
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);
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
this.popup = new Htk.Popup({
|
2015-12-15 16:08:24 +00:00
|
|
|
modal: true,
|
|
|
|
child: editor
|
|
|
|
});
|
2019-05-27 13:38:06 +00:00
|
|
|
this.popup.show(this.node);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-12-10 13:48:43 +00:00
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_onNameChange: function(editor, value) {
|
2015-12-10 13:48:43 +00:00
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_onFileUpload: function(cell) {
|
|
|
|
this._stamp = new Date().getTime();
|
|
|
|
this._refreshSrc(cell);
|
|
|
|
this.popup.hide();
|
2015-12-10 13:48:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:38:06 +00:00
|
|
|
,_onEditorClose: function(editor) {
|
|
|
|
editor.disconnectByInstance(this);
|
2015-12-10 13:48:43 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|