chrome.js
Summary
Widget Chrome Library
Library to make resizable widget chrome.
Version: 0.2
The user does not need to do anything for the chrome to be visible.
Only include the script and the skin file and it should work.
, 0.1
Initial implementation
Author: Vivek Jishtu
|
Class Summary
|
| WidgetChrome |
A class which paints the chrome and handles resizing. |
var WidgetChrome = new function()
{
var _chromePiecesId = ["chrome_middle_content", "chrome_top_left", "chrome_top_middle",
"chrome_top_right", "chrome_middle_left", "chrome_middle_right",
"chrome_bottom_left", "chrome_bottom_middle", "chrome_bottom_right"];
var _chromePieces = {};
var _rootElement = null;
var _chromeRoot;
var _sizeControl;
var _self = this;
this.minWidth = 50;
this.minHeight = 100;
this.ButtonConfig = createButton("btnConfig");
this.ButtonClose = createButton("btnClose");
this.ButtonClose.onclick = function(event)
{
window.close();
}
this.ButtonConfig.onclick = function(event)
{
opera.postError("Config clicked");
}
function $(id)
{
return document.getElementById(id);
}
this.showScrollBars = function(state)
{
var contentArea = $("chrome_middle_content");
if (state == true) contentArea.style.overflow = "auto";
else contentArea.style.overflow = "hidden";
}
this.setSkin = function(skinName)
{
var head = document.getElementsByTagName('head').item(0);
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.id = skinName;
link.title = skinName;
link.media = "screen";
link.href = skinName + "/skin.css";
head.appendChild(link);
}
function init()
{
_rootElement = document.body;
_chromeRoot = createDiv("__widget_chrome_root__");
_chromeRoot.setAttribute("data-not-chrome-content", "true");
addChromePiecesToRoot();
_rootElement.insertBefore(_chromeRoot, _rootElement.firstChild);
sizerControls();
addWidgetControls();
$("lblTitle").innerHTML = document.title;
addBodyToContentArea();
_self.redraw();
}
function addBodyToContentArea()
{
var contentArea = $("chrome_middle_content");
var currentNode = document.body.firstChild;
var nextNode;
while (currentNode)
{
nextNode = currentNode.nextSibling;
if (currentNode.getAttribute && currentNode.getAttribute("data-not-chrome-content"))
{
}
else
{
contentArea.appendChild(currentNode);
}
currentNode = nextNode;
}
}
function addWidgetControls()
{
_chromeRoot.appendChild(createDiv("lblTitle"));
_chromeRoot.appendChild(_self.ButtonConfig);
_chromeRoot.appendChild(_self.ButtonClose);
}
function sizerControls()
{
_sizeControl = createDiv("sizerControl");
_sizeControl.setAttribute("data-not-chrome-content", "true");
with(_sizeControl.style)
{
position = "absolute";
top = "0px";
left = "0px";
border = "1px dotted black";
display = "none";
}
document.body.appendChild(_sizeControl);
var windowX = document.body.clientWidth;
var windowY = document.body.clientHeight;
function resizer_MouseDown(event)
{
var x = event.clientX;
var y = event.clientY;
var oldWindowX = document.body.clientWidth;
var oldWindowY = document.body.clientHeight;
$("sizerControl").style.display = "block";
$("sizerControl").style.height = window.innerHeight - 5 + "px";
$("sizerControl").style.width = window.innerWidth - 5 + "px";
window.resizeTo(window.innerWidth + 5, window.innerHeight + 5);
function window_MouseMove(event)
{
window.scrollTo(0, 0);
windowX = oldWindowX + event.clientX - x;
windowY = oldWindowY + event.clientY - y;
if (windowX > window.innerWidth) window.resizeTo(windowX + 5, window.innerHeight);
if (windowY > window.innerHeight) window.resizeTo(window.innerWidth, windowY + 5);
if (_self.minWidth > windowX) return;
if (_self.minHeight > windowY) return;
document.body.style.height = document.body.clientHeight - 2 + "px";
document.body.style.width = document.body.clientWidth - 2 + "px";
$("sizerControl").style.height = windowY - 3 + "px";
$("sizerControl").style.width = windowX - 3 + "px";
}
function window_MouseUp(event)
{
if (_self.minWidth > windowX) windowX = _self.minWidth;
if (_self.minHeight > windowY) windowY = _self.minHeight;
window.resizeTo(windowX, windowY);
document.body.style.height = window.innerHeight + "px";
document.body.style.width = window.innerWidth + "px";
$("sizerControl").style.display = "none";
_self.redraw();
window.removeEventListener("mousemove", window_MouseMove, true);
window.removeEventListener("mouseup", window_MouseUp, true);
}
window.addEventListener("mousemove", window_MouseMove, true);
window.addEventListener("mouseup", window_MouseUp, true);
}
$("chrome_bottom_right").onmousedown = resizer_MouseDown;
}
function addChromePiecesToRoot()
{
for (var x in _chromePiecesId)
{
var id = _chromePiecesId[x];
_chromePieces[id] = createDiv(id);
_chromeRoot.appendChild(_chromePieces[id]);
}
}
function createDiv(id)
{
var div = document.createElement("div");
div.id = id;
return div;
}
function createButton(id)
{
var button = document.createElement("button");
button.setAttribute("type", "button");
button.id = id;
return button;
}
this.redraw = function()
{
window.scrollTo(0, 0);
document.body.style.top = "0px";
document.body.style.left = "0px";
document.body.style.width = window.innerWidth + "px";
document.body.style.height = window.innerHeight + "px";
_chromeRoot.style.width = document.body.clientWidth + "px";
_chromeRoot.style.height = document.body.clientHeight + "px";
var _width = _chromeRoot.clientWidth;
var _height = _chromeRoot.clientHeight;
_chromePieces['chrome_top_middle'].style.width = _width - (_chromePieces['chrome_top_left'].clientWidth + _chromePieces['chrome_top_right'].clientWidth) + "px";
_chromePieces['chrome_top_middle'].style.left = _chromePieces['chrome_top_left'].clientWidth + "px";
_chromePieces['chrome_bottom_middle'].style.width = _width - (_chromePieces['chrome_bottom_left'].clientWidth + _chromePieces['chrome_bottom_right'].clientWidth) + "px";
_chromePieces['chrome_bottom_middle'].style.left = _chromePieces['chrome_bottom_left'].clientWidth + "px";
_chromePieces['chrome_middle_left'].style.height = _height - (_chromePieces['chrome_top_left'].clientHeight + _chromePieces['chrome_bottom_left'].clientHeight) + "px";
_chromePieces['chrome_middle_right'].style.height = _height - (_chromePieces['chrome_top_right'].clientHeight + _chromePieces['chrome_bottom_right'].clientHeight) + "px";
_content = _chromePieces['chrome_middle_content'];
_content.style.top = _chromePieces['chrome_top_middle'].clientHeight + "px";
_content.style.left = _chromePieces['chrome_middle_left'].clientWidth + "px";
_content.style.height = _height - (_chromePieces['chrome_top_middle'].clientHeight + _chromePieces['chrome_bottom_middle'].clientHeight) + "px";
_content.style.width = _width - (_chromePieces['chrome_middle_left'].clientWidth + _chromePieces['chrome_middle_right'].clientWidth) + "px";
}
this.autoResize = function(element)
{
if (typeof element == "string") element = document.getElementById(element);
if (!element) throw "Cannot find the element to autoResize";
with(element.style)
{
position = "absolute";
top = "0px";
left = "0px";
height = "100%";
width = "100%";
overflow = "auto";
}
}
this.addElementToChrome = function(element)
{
if (typeof element == "string") element = document.getElementById(element);
if (!element) throw "Cannot add the element to the Widget Chrome";
_chromeRoot.appendChild(element);
}
this.setFullScreen = function()
{
window.moveTo(0, 0);
window.resizeTo(screen.availWidth, screen.availHeight);
_self.redraw();
}
this.enableStyleSheet = function(title)
{
var stylesheets = document.getElementsByTagName("link");
var x,
stylesheet;
for (x = 0; x < stylesheets.length; x++)
{
stylesheet = stylesheets[x];
if (stylesheet.getAttribute("rel").indexOf("stylesheet") != -1)
{
if (stylesheet.getAttribute("title") == title) stylesheet.disabled = false;
}
}
_self.redraw();
}
this.disableStyleSheet = function(title)
{
var stylesheets = document.getElementsByTagName("link");
var x,
stylesheet;
for (x = 0; x < stylesheets.length; x++)
{
stylesheet = stylesheets[x];
if (stylesheet.getAttribute("rel").indexOf("stylesheet") != -1)
{
if (stylesheet.getAttribute("title") == title) stylesheet.disabled = true;
}
}
_self.redraw();
}
this.setTitle = function(title)
{
document.title = title.replace(/<\/?[a-z][a-z0-9]*[^<>]*>/g, "");
$("lblTitle").innerHTML = title;
}
if (document.body) init();
else window.addEventListener("DOMContentLoaded", init, false);
} ();;
Documentation generated by
JSDoc on Tue Jul 22 11:23:57 2008