0
1
Fork 0
hedera-web-mindshore/web/js/htk/full-image.js

174 lines
3.6 KiB
JavaScript
Raw Normal View History

Htk.FullImage = new Class
({
Extends: Vn.Object
,img: null
,timeout: 0
,loading: false
,visible: false
,hideCalled: false
2015-02-08 15:38:38 +00:00
,closed: false
,initialize: function (props)
{
this.parent (props);
var div = document.createElement ('div');
2015-03-06 23:33:54 +00:00
div.className = 'htk-full-image';
2015-02-01 03:21:54 +00:00
div.addEventListener ('mouseover', this.cancelHide.bind (this));
div.addEventListener ('mouseout', this.hideNow.bind (this));
var loadingBox = document.createElement ('div');
2015-03-06 23:33:54 +00:00
loadingBox.className = 'htk-full-image-loader';
var loadingImg = document.createElement ('img');
loadingImg.src = 'image/loader-black.gif';
loadingImg.alt = _('Loading');
loadingBox.appendChild (loadingImg);
this.div = div;
this.loadingBox = loadingBox;
this.loadingImg = loadingImg;
}
,getLeft: function (width)
{
2015-03-06 23:33:54 +00:00
return parseInt (Vn.Browser.getPageXOffset () + (Vn.Browser.getInnerWidth () - width) / 2) +'px';
}
,getTop: function (height)
{
2015-03-06 23:33:54 +00:00
return parseInt (Vn.Browser.getPageYOffset () + (Vn.Browser.getInnerHeight () - height) / 2) +'px';
}
,show: function (src)
{
2015-02-08 15:38:38 +00:00
if (this.closed && this.src == src)
2015-09-22 07:20:47 +00:00
{
this.closed = false;
2015-02-08 15:38:38 +00:00
return;
2015-09-22 07:20:47 +00:00
}
2015-02-08 15:38:38 +00:00
2015-02-01 03:21:54 +00:00
this.cancelHide ();
this.loadingBox.style.left = this.getLeft (40);
this.loadingBox.style.top = this.getTop (40);
2015-02-08 15:38:38 +00:00
this.closed = false;
this.src = src
this.img = document.createElement ('img');
2015-02-08 15:38:38 +00:00
this.img.src = src;
this.img.addEventListener ('load', this.imageLoaded.bind (this, this.img));
this.img.addEventListener ('error', this.hideLoading.bind (this));
if (!this.img.complete && !this.loading)
{
document.body.appendChild (this.loadingBox);
this.loading = true;
}
}
,imageLoaded: function (img)
{
if (img != this.img || this.hideCalled)
return;
var scale = 1.0;
var width = img.width;
var height = img.height;
var innerWidth = Vn.Browser.getInnerWidth () - 50;
var innerHeight = Vn.Browser.getInnerHeight () - 50;
if (width > innerWidth)
{
scale = width / innerWidth;
height = parseInt (height / scale);
width = innerWidth;
}
if (height > innerHeight)
{
scale = height / innerHeight;
width = parseInt (width / scale);
height = innerHeight;
}
this.hideLoading ();
this.div.style.left = this.getLeft (width + 2);
this.div.style.top = this.getTop (height + 2);
this.div.style.width = width +'px';
this.div.style.height = height +'px';
if (scale !== 1.0)
{
img.style.width = width +'px';
img.style.height = height +'px';
}
if (this.div.firstChild != null)
this.div.replaceChild (img, this.div.firstChild);
else
this.div.appendChild (img);
2015-02-08 15:38:38 +00:00
img.addEventListener ('click', this.onImageClick.bind (this));
if (!this.visible)
{
document.body.appendChild (this.div);
this.visible = true;
2015-02-01 03:21:54 +00:00
this.onDocumentClickCallback = this.hideNow.bind (this);
document.addEventListener ('click', this.onDocumentClickCallback);
}
}
2015-02-08 15:38:38 +00:00
,onImageClick: function ()
{
this.closed = true;
}
,hide: function ()
{
this.hideCalled = true;
this.hideLoading ();
if (this.visible)
2015-02-01 03:21:54 +00:00
this.timeout = setTimeout (this.hideNow.bind (this), 450);
}
2015-02-01 03:21:54 +00:00
,hideNow: function ()
{
2015-02-01 03:21:54 +00:00
if (this.visible)
{
document.body.removeChild (this.div);
this.hideCalled = true;
this.visible = false;
this.timeout = 0;
}
document.removeEventListener ('click', this.onDocumentClickCallback);
this.onDocumentClickCallback = null;
}
,cancelHide: function ()
{
if (this.timeout)
{
clearTimeout (this.timeout);
this.timeout = 0;
}
this.hideCalled = false;
}
,hideLoading: function ()
{
if (this.loading)
{
document.body.removeChild (this.loadingBox);
this.loading = false;
}
}
});