SProtectLayer.js
Summary
Invisible layer to prevent the user to interact with all objects under a given zIndex.
function SProtectLayer()
{
this._instance = SProtectLayer._instance++;
this.event = {};
this._create();
}
SProtectLayer.prototype._instance = 0;
SProtectLayer.prototype._level = 10;
SProtectLayer._document = document;
SProtectLayer.prototype._event = null;
SProtectLayer.prototype._iframe = null;
SProtectLayer._instance = 0;
SProtectLayer._visibleInstances = {};
SProtectLayer._visibleInstances.length = 0;
SProtectLayer._highestInstance = null;
SProtectLayer.prototype.setLevel = function(level)
{
this._level = level;
}
SProtectLayer.prototype.setEventListener = function(event, listener)
{
this._event[event] = listener;
if (this._event.onclick != null)
this._iframe.contentWindow.document.onclick = this._event.onclick;
if (this._event.ondblclick != null)
this._iframe.contentWindow.document.ondblclick = this._event.ondblclick;
if (this._event.onmousedown != null)
this._iframe.contentWindow.document.onmousedown = this._event.onmousedown;
if (this._event.onmousemove != null)
this._iframe.contentWindow.document.onmousemove = this._event.onmousemove;
if (this._event.onmouseover != null)
this._iframe.contentWindow.document.onmouseover = this._event.onmouseover;
if (this._event.onmouseout != null)
this._iframe.contentWindow.document.onmouseout = this._event.onmouseout;
if (this._event.onmouseup != null)
this._iframe.contentWindow.document.onmouseup = this._event.onmouseup;
if (this._event.onfocus != null)
{
if (SUtilities.IS_IE) this._iframe.onfocus = this._event.onfocus;
else this._iframe.contentWindow.onfocus = this._event.onfocus;
}
}
SProtectLayer.prototype._dispose = function()
{
if (this._iframe != null)
{
this._iframe.contentWindow.document.onclick = null;
this._iframe.contentWindow.document.ondblclick = null;
this._iframe.contentWindow.document.onmousedown = null;
this._iframe.contentWindow.document.onmousemove = null;
this._iframe.contentWindow.document.onmouseover = null;
this._iframe.contentWindow.document.onmouseout = null;
this._iframe.contentWindow.document.onmouseup = null;
if (SUtilities.IS_IE) this._iframe.onfocus = null;
else this._iframe.contentWindow.onfocus = null;
this._event = {};
this._iframe.parentNode.removeChild(this._iframe);
delete SProtectLayer._visibleInstances[this._instance];
SProtectLayer._visibleInstances.length--;
this._iframe = null;
}
}
SProtectLayer.prototype._create = function()
{
this._dispose();
this._iframe = SProtectLayer._document.createElement('iframe');
this._iframe.frameBorder = "0";
this._iframe.scrolling = "no";
this._iframe.style.cssText = "display: none; filter:alpha(opacity=0);-moz-opacity:.0;opacity:.0;-moz-user-focus: normal; position: absolute; top: 0px; left: 0px; width: 0px; height: 0px;";
SProtectLayer._document.body.appendChild(this._iframe);
var _document = this._iframe.contentWindow.document;
_document.open();
_document.write("<html><body style='padding: 0px; margin: 0px;'><div id='alone' style='height: 100%'></div></body></html>");
_document.close();
}
SProtectLayer.prototype.setOpacity = function(value)
{
if (SUtilities.IS_IE)
{
this._iframe.filters.item('alpha').opacity = value;
}
else
{
this._iframe.style.opacity = value/100;
}
}
SProtectLayer.prototype.setStyle = function(attribute, value)
{
if (/height/i.test(attribute))
return;
this._iframe.contentWindow.document.getElementById('alone').style[attribute] = value;
}
SProtectLayer.prototype.hide = function()
{
if (this._iframe.style.display == "none")
{
return;
}
if (this._event.onhide != null)
this._event.onhide();
delete SProtectLayer._visibleInstances[this._instance];
SProtectLayer._visibleInstances.length--;
if (SProtectLayer._visibleInstances.length == 0)
SProtectLayer._uncheckEvents();
this._iframe.style.display = "none";
SProtectLayer._computeHighestInstance();
}
SProtectLayer.prototype.show = function()
{
if (this._iframe.style.display == "block")
{
return;
}
if (this._event.onshow != null)
{
this._event.onshow();
}
if (SProtectLayer._visibleInstances.length == 0)
{
SProtectLayer._checkEvents();
}
SProtectLayer._visibleInstances[this._instance] = this;
SProtectLayer._visibleInstances.length++;
this._resize();
this._scroll();
this._iframe.style.zIndex = this._level;
this._iframe.style.display = "block";
this._iframe.contentWindow.focus();
SProtectLayer._computeHighestInstance();
}
SProtectLayer._computeHighestInstance = function()
{
var highestLevel = 0;
SProtectLayer._highestInstance = null;
for (var i in SProtectLayer._visibleInstances)
{
if (i == "length")
{
continue;
}
var instance = SProtectLayer._visibleInstances[i];
if (instance._level > highestLevel)
{
highestLevel = instance._level;
SProtectLayer._highestInstance = instance;
}
}
}
SProtectLayer._checkEvents = function()
{
if (SUtilities.IS_IE)
{
window.attachEvent("onresize", SProtectLayer._onResize);
window.attachEvent("onscroll", SProtectLayer._onScroll);
SProtectLayer.document.attachEvent("onactivate", SProtectLayer._onFocusChanged);
}
else
{
window.addEventListener("resize", SProtectLayer._onResize, false);
SProtectLayer.document.addEventListener("focus", SProtectLayer._onFocusChanged, true);
}
}
SProtectLayer._uncheckEvents = function()
{
if (SUtilities.IS_IE)
{
window.detachEvent("onresize", SProtectLayer._onResize);
window.detachEvent("onscroll", SProtectLayer._onScroll);
SProtectLayer.document.detachEvent("onactivate", SProtectLayer._onFocusChanged);
}
else
{
window.removeEventListener("resize", SProtectLayer._onResize, false);
SProtectLayer.document.removeEventListener("focus", SProtectLayer._onFocusChanged, true);
}
}
SProtectLayer.prototype._resize = function()
{
if (this._event.onresize != null)
this._event.onresize();
if (SUtilities.IS_IE)
{
var horizontalScrollBar = (SProtectLayer._document.body.scrollWidth > SProtectLayer._document.body.offsetWidth);
var verticalScrollBar = true;
var width = parseInt(SProtectLayer._document.body.offsetWidth) + (verticalScrollBar ? -21 : -4);
var height = parseInt(SProtectLayer._document.body.offsetHeight) + (horizontalScrollBar ? -21 : -4);
}
else
{
var width = parseInt(SProtectLayer._document.body.scrollWidth) - 4;
var height = parseInt(SProtectLayer._document.body.scrollHeight) - 4;
}
this._iframe.style.width = width;
this._iframe.style.height = height;
}
SProtectLayer.prototype._scroll = function()
{
if (SUtilities.IS_IE)
{
this._iframe.style.top = SProtectLayer._document.body.scrollTop;
this._iframe.style.left = SProtectLayer._document.body.scrollLeft;
}
}
SProtectLayer._onResize = function()
{
for (var i in SProtectLayer._visibleInstances)
{
if (i == "length")
continue;
var instance = SProtectLayer._visibleInstances[i];
instance._iframe.style.display = 'none';
}
window.setTimeout(SProtectLayer._onResizeAfter, 1);
}
SProtectLayer._onResizeAfter = function()
{
for (var i in SProtectLayer._visibleInstances)
{
if (i == "length")
continue;
var instance = SProtectLayer._visibleInstances[i];
instance._resize();
instance._iframe.style.display = 'block';
}
}
SProtectLayer._onScroll = function()
{
for (var i in SProtectLayer._visibleInstances)
{
if (i == "length")
continue;
var instance = SProtectLayer._visibleInstances[i];
instance._scroll();
}
}
SProtectLayer._onFocusChanged = function(e)
{
if (SProtectLayer._highestInstance == null)
return;
var highestLevel = SProtectLayer._highestInstance._iframe.style.zIndex;
var element = SUtilities.IS_IE ? window.event.srcElement : e.target;
if (element == null || element.tagName == null)
return;
var elementIsBody;
var elementIsAbsolutlyPositionned;
var elementZIndex;
do
{
elementIsBody = /^body|html$/i.test(element.tagName);
elementIsAbsolutlyPositionned = element.style != null && /absolute/i.test(element.style.position);
elementZIndex = element.style != null && element.style.zIndex;
if (elementIsBody || (elementIsAbsolutlyPositionned && elementZIndex != null))
{
if (elementIsBody || elementZIndex < highestLevel)
{
SProtectLayer._uncheckEvents();
SProtectLayer._highestInstance._iframe.contentWindow.focus();
SProtectLayer._checkEvents();
}
return;
}
element = element.parentNode;
} while (!elementIsBody && !elementIsAbsolutlyPositionned && elementZIndex < highestLevel);
}
try
{
SUtilities;
}
catch (e)
{
alert("SProtectLayer needs SUtilities to be imported.");
}
- STools Copyright 2005 -
Documentation generated by
JSDoc on Tue Jan 24 21:43:37 2006