hedera-web/web/js/htk/field/image.js

217 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-09-16 16:11:15 +00:00
/**
* Class to display or edit an image. Also it allows to show it's full version.
**/
var fullImage = null;
Htk.Image = new Class
({
2015-11-09 08:14:33 +00:00
Extends: Htk.Field
,Tag: 'htk-image'
2015-03-09 08:36:54 +00:00
,Properties:
{
/**
* The directory where the images are allocated.
**/
directory:
{
type: String
,set: function (x)
{
this._directory = x;
2015-09-16 16:11:15 +00:00
this._basedir = Vn.Config['image_dir'] +'/'+ x;
2015-12-10 13:48:43 +00:00
this._setSrc ();
2015-03-09 08:36:54 +00:00
}
,get: function ()
{
return this._directory;
}
2015-09-16 16:11:15 +00:00
},
/**
* The directory where the images are allocated.
**/
subdir:
{
type: String
,set: function (x)
{
this._subdir = x;
2015-12-10 13:48:43 +00:00
this._setSrc ();
2015-09-16 16:11:15 +00:00
}
,get: function ()
{
return this._subdir;
}
},
/**
* Whether to show the full image when mouse hover.
**/
showFull:
{
type: Boolean
,set: function (x)
{
if (x && !this._fullImage)
{
if (!fullImage)
fullImage = new Htk.FullImage ();
this._fullImage = fullImage;
2015-12-10 13:48:43 +00:00
this.node.addEventListener ('mouseover', this._onMouseOver.bind (this));
this.node.addEventListener ('mouseout', this._onMouseOut.bind (this));
2015-09-16 16:11:15 +00:00
}
this._showFull = x;
}
,get: function ()
{
return this._showFull;
}
},
/**
* Subdirectory where full images are allocated.
**/
fullDir:
{
type: String
,set: function (x)
{
this._fullDir = x;
}
,get: function ()
{
return this._fullDir;
}
2015-03-09 08:36:54 +00:00
}
}
,_directory: null
2015-09-16 16:11:15 +00:00
,_subdir: null
,_basedir: null
2015-12-10 13:48:43 +00:00
,_error: false
2015-09-16 16:11:15 +00:00
,_showFull: false
,_fullDir: null
2015-12-10 13:48:43 +00:00
,_editable: false
,initialize: function (props)
{
2015-03-09 08:36:54 +00:00
this.createElement ('img');
2015-12-10 13:48:43 +00:00
this.node.className = 'htk-image';
this.node.addEventListener ('error', this._onImageError.bind (this));
2015-11-09 08:14:33 +00:00
2015-12-10 13:48:43 +00:00
this.setEditable (this._editable);
2015-11-09 08:14:33 +00:00
this.parent (props);
}
2015-12-10 13:48:43 +00:00
,setEditable: function (editable)
{
2015-12-10 13:48:43 +00:00
if (editable)
{
2015-12-10 13:48:43 +00:00
Vn.Node.addClass (this.node, 'editable');
this.node.addEventListener ('dblclick', this._onDoubleClick.bind (this));
}
2015-12-10 13:48:43 +00:00
else
Vn.Node.removeClass (this.node, 'editable');
this._setEmpty ();
}
2015-12-10 13:48:43 +00:00
,_setSrc: function ()
{
2015-03-09 08:36:54 +00:00
if (this._value)
{
2015-03-09 08:36:54 +00:00
var url = '';
2015-09-16 16:11:15 +00:00
if (this._basedir)
url += this._basedir +'/';
if (this._subdir)
url += this._subdir +'/';
2015-03-09 08:36:54 +00:00
url += this._value;
2015-12-10 13:48:43 +00:00
if (this._stamp)
url += '?'+ this._stamp;
2015-12-10 13:48:43 +00:00
this._error = false;
2015-03-09 08:36:54 +00:00
this.node.src = url;
2015-07-17 14:34:42 +00:00
this.node.style.display = '';
}
else
2015-12-10 13:48:43 +00:00
this._setEmpty ();
}
,_setEmpty: function ()
{
if (this._value !== null && this._value !== '')
return;
if (this._editable)
this.node.src = 'image/add-photo.svg';
else
2015-07-17 14:34:42 +00:00
delete this.node.src;
}
2015-03-09 08:36:54 +00:00
,putValue: function (value)
{
2015-12-10 13:48:43 +00:00
this._setSrc ();
}
2015-12-10 13:48:43 +00:00
,_onMouseOver: function ()
{
2015-12-10 13:48:43 +00:00
if (!this._showFull || !this._value || this._error)
2015-09-16 16:11:15 +00:00
return;
var src = this._fullDir ? this._fullDir : 'full';
src += '/'+ this._value;
if (this._stamp)
src += '?'+ this._stamp;
2015-12-10 13:48:43 +00:00
this._fullImage.show (this._basedir +'/'+ src);
2015-09-16 16:11:15 +00:00
}
2015-12-10 13:48:43 +00:00
,_onMouseOut: function ()
2015-09-16 16:11:15 +00:00
{
this._fullImage.hide ();
}
2015-12-10 13:48:43 +00:00
,_onImageError: function ()
{
2015-12-10 13:48:43 +00:00
if (this._error)
return;
this._error = true;
}
2015-12-10 13:48:43 +00:00
,_onDoubleClick: function ()
{
2015-03-09 08:36:54 +00:00
var editor = new Htk.ImageEditor ();
2015-12-10 13:48:43 +00:00
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-03-09 08:36:54 +00:00
this.popup = new Htk.Popup ();
this.popup.setChild (editor);
this.popup.show (this.node);
}
2015-12-10 13:48:43 +00:00
,_onNameChange: function (editor, value)
{
this.value = value;
}
,_onFileUpload: function (cell, editor)
{
this._stamp = new Date ().getTime ();
this.setSrc (cell);
this.popup.hide ();
}
,_onEditorClose: function (editor)
{
editor.disconnectByInstance (this);
}
});