/*----------------------------------------------------------------------------\
| XTree 2 PRE RELEASE |
| |
| This is a pre release and redistribution is discouraged. |
| Watch http://webfx.eae.net for the final version |
| |
|-----------------------------------------------------------------------------|
| Created by Erik Arvidsson & Emil A Eklund |
| (http://webfx.eae.net/contact.html#erik) |
| (http://webfx.eae.net/contact.html#emil) |
| For WebFX (http://webfx.eae.net/) |
|-----------------------------------------------------------------------------|
| A tree menu system for IE 5.5+, Mozilla 1.4+, Opera 7, KHTML |
|-----------------------------------------------------------------------------|
| Copyright (c) 1999 - 2005 Erik Arvidsson & Emil A Eklund |
|-----------------------------------------------------------------------------|
| This software is provided "as is", without warranty of any kind, express or |
| implied, including but not limited to the warranties of merchantability, |
| fitness for a particular purpose and noninfringement. In no event shall the |
| authors or copyright holders be liable for any claim, damages or other |
| liability, whether in an action of contract, tort or otherwise, arising |
| from, out of or in connection with the software or the use or other |
| dealings in the software. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| This software is available under the three different licenses mentioned |
| below. To use this software you must chose, and qualify, for one of those. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| The WebFX Non-Commercial License http://webfx.eae.net/license.html |
| Permits anyone the right to use the software in a non-commercial context |
| free of charge. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| The WebFX Commercial license http://webfx.eae.net/commercial.html |
| Permits the license holder the right to use the software in a commercial |
| context. Such license must be specifically obtained, however it's valid for |
| any number of implementations of the licensed software. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| GPL - The GNU General Public License http://www.gnu.org/licenses/gpl.txt |
| Permits anyone the right to use and modify the software without limitations |
| as long as proper credits are given and the original and modified source |
| code are included. Requires that the final product, software derivate from |
| the original source or any software utilizing a GPL component, such as |
| this, is also licensed under the GPL license. |
|-----------------------------------------------------------------------------|
| 2004-02-21 | Pre release distributed to a few selected tester |
| 2005-06-06 | Added single tab index to improve keyboard navigation |
|-----------------------------------------------------------------------------|
| Dependencies: xtree2.css Used to define the look and feel |
|-----------------------------------------------------------------------------|
| Created 2003-??-?? | All changes are in the log above. | Updated 2004-06-06 |
\----------------------------------------------------------------------------*/
//
// WebFXTreePersisitance
function WebFXTreePersistence() {}
var _p = WebFXTreePersistence.prototype;
_p.getExpanded = function (oNode) { return false; };
_p.setExpanded = function (oNode, bOpen) {};
// Cookie handling
function WebFXCookie() {}
_p = WebFXCookie.prototype;
_p.setCookie = function (sName, sValue, nDays) {
var expires = "";
if (typeof nDays == "number") {
var d = new Date();
d.setTime(d.getTime() + nDays * 24 * 60 * 60 * 1000);
expires = "; expires=" + d.toGMTString();
}
document.cookie = sName + "=" + escape(sValue) + expires + "; path=/";
};
_p.getCookie = function (sName) {
var re = new RegExp("(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)");
var res = re.exec(document.cookie);
return res != null ? unescape(res[3]) : null;
};
_p.removeCookie = function (name) {
this.setCookie(name, "", -1);
};
//
// persistence using cookies
//
// This is uses one cookie with the ids of the expanded nodes separated using '+'
//
function WebFXTreeCookiePersistence() {
this._openedMap = {};
this._cookies = new WebFXCookie;
var s = this._cookies.getCookie(this.cookieName);
if (s) {
var a = s.split("+");
for (var i = a.length - 1; i >= 0; i--)
this._openedMap[a[i]] = true;
}
}
_p = WebFXTreeCookiePersistence.prototype = new WebFXTreePersistence;
_p.cookieName = "webfx-tree-cookie-persistence"
_p.getExpanded = function (oNode) {
return oNode.id in this._openedMap;
};
_p.setExpanded = function (oNode, bOpen) {
var old = this.getExpanded(oNode);
if (old != bOpen) {
if (bOpen) {
this._openedMap[oNode.id] = true;
} else {
delete this._openedMap[oNode.id];
}
var res = [];
var i = 0;
for (var id in this._openedMap)
res[i++] = id;
this._cookies.setCookie(this.cookieName, res.join("+"));
}
};
// this object provides a few useful methods when working with arrays
var arrayHelper = {
indexOf: function (a, o) {
for (var i = 0; i < a.length; i++) {
if (a[i] == o) {
return i;
}
}
return -1;
},
insertBefore: function (a, o, o2) {
var i = this.indexOf(a, o2);
if (i == -1) {
a.push(o);
} else {
a.splice(i, 0, o);
}
},
remove: function (a, o) {
var i = this.indexOf(a, o);
if (i != -1) {
a.splice(i, 1);
}
}
};
///////////////////////////////////////////////////////////////////////////////
// WebFX Tree Config object //
///////////////////////////////////////////////////////////////////////////////
var webFXTreeConfig = {
rootIcon : "images/folder.png",
openRootIcon : "images/openfolder.png",
folderIcon : "images/folder.png",
openFolderIcon : "images/openfolder.png",
fileIcon : "images/file.png",
iIcon : "images/I.png",
lIcon : "images/L.png",
lMinusIcon : "images/Lminus.png",
lPlusIcon : "images/Lplus.png",
tIcon : "images/T.png",
tMinusIcon : "images/Tminus.png",
tPlusIcon : "images/Tplus.png",
plusIcon : "images/plus.png",
minusIcon : "images/minus.png",
blankIcon : "images/blank.png",
defaultText : "Tree Item",
defaultAction : null,
defaultBehavior : "classic",
usePersistence : true
};
///////////////////////////////////////////////////////////////////////////////
// WebFX Tree Handler object //
///////////////////////////////////////////////////////////////////////////////
var webFXTreeHandler = {
ie: /msie/i.test(navigator.userAgent),
opera: /opera/i.test(navigator.userAgent),
idCounter: 0,
idPrefix: "wfxt-",
getUniqueId: function () {
return this.idPrefix + this.idCounter++;
},
all: {},
getNodeById: function (sId) {
return all[sId];
},
addNode: function (oNode) {
this.all[oNode.id] = oNode;
},
removeNode: function (oNode) {
delete this.all[oNode.id];
},
handleEvent: function (e) {
var el = e.target || e.srcElement;
while (el != null && !this.all[el.id]) {
el = el.parentNode;
}
if (el == null) {
return false;
}
var node = this.all[el.id];
if (typeof node["_on" + e.type] == "function") {
return node["_on" + e.type](e);
}
return false;
},
dispose: function () {
if (this.disposed) return;
for (var id in this.all) {
this.all[id].dispose();
}
this.disposed = true;
},
htmlToText: function (s) {
return String(s).replace(/\s+|<([^>])+>|&|<|>|"| /gi, this._htmlToText);
},
_htmlToText: function (s) {
switch (s) {
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
case """:
return "\"";
case " ":
return String.fromCharCode(160);
default:
if (/\s+/.test(s)) {
return " ";
}
if (/^
|\n|\"|\u00A0/g, this._textToHtml);
},
_textToHtml: function (s) {
switch (s) {
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
case "\n":
return "
";
case "\"":
return """; // so we can use this in attributes
default:
return " ";
}
},
persistenceManager: new WebFXTreeCookiePersistence()
};
///////////////////////////////////////////////////////////////////////////////
// WebFXTreeAbstractNode
///////////////////////////////////////////////////////////////////////////////
function WebFXTreeAbstractNode(sText, oAction, oIconAction) {
this.childNodes = [];
if (sText) this.text = sText;
if (oAction) this.action = oAction;
if (oIconAction) this.iconAction = oIconAction;
this.id = webFXTreeHandler.getUniqueId();
if (webFXTreeConfig.usePersistence) {
this.open = webFXTreeHandler.persistenceManager.getExpanded(this);
}
webFXTreeHandler.addNode(this);
}
_p = WebFXTreeAbstractNode.prototype;
_p._selected = false;
_p.indentWidth = 19;
_p.open = false;
_p.text = webFXTreeConfig.defaultText;
_p.action = null;
_p.iconAcion = null;
_p.target = null;
_p.toolTip = null;
_p._focused = false;
/* begin tree model */
_p.add = function (oChild, oBefore) {
var oldLast;
var emptyBefore = this.childNodes.length == 0;
var p = oChild.parentNode;
if (oBefore == null) { // append
if (p != null)
p.remove(oChild);
oldLast = this.getLastChild();
this.childNodes.push(oChild);
} else { // insertBefore
if (oBefore.parentNode != this) {
throw new Error("Can only add nodes before siblings");
}
if (p != null) {
p.remove(oChild);
}
arrayHelper.insertBefore(this.childNodes, oChild, oBefore);
}
if (oBefore) {
if (oBefore == this.firstChild) {
this.firstChild = oChild;
}
oChild.previousSibling = oBefore.previousSibling;
oBefore.previousSibling = oChild;
oChild.nextSibling = oBefore;
} else {
if (!this.firstChild) {
this.firstChild = oChild;
}
if (this.lastChild) {
this.lastChild.nextSibling = oChild;
}
oChild.previousSibling = this.lastChild;
this.lastChild = oChild;
}
oChild.parentNode = this;
var t = this.getTree();
if (t) {
oChild.tree = t;
}
var d = this.getDepth();
if (d != null) {
oChild.depth = d + 1;
}
if (this.getCreated() && !t.getSuspendRedraw()) {
var el = this.getChildrenElement();
var newEl = oChild.create();
var refEl = oBefore ? oBefore.getElement() : null;
el.insertBefore(newEl, refEl);
if (oldLast) {
oldLast.updateExpandIcon();
}
if (emptyBefore) {
this.setExpanded(this.getExpanded());
// if we are using classic expand will not update icon
if (t && t.getBehavior() != "classic")
this.updateIcon();
}
}
return oChild;
};
_p.remove = function (oChild) {
// backwards compatible. If no argument remove the node
if (arguments.length == 0) {
if (this.parentNode) {
return this.parentNode.remove(this);
}
return null;
}
// if we remove selected or tree with the selected we should select this
var t = this.getTree();
var si = t ? t.getSelected() : null;
if (si == oChild || oChild.contains(si)) {
if (si.getFocused()) {
this.select();
window.setTimeout("WebFXTreeAbstractNode._onTimeoutFocus(\"" + this.id + "\")", 10);
} else {
this.select();
}
}
if (oChild.parentNode != this) {
throw new Error("Can only remove children");
}
arrayHelper.remove(this.childNodes, oChild);
if (this.lastChild == oChild) {
this.lastChild = oChild.previousSibling;
}
if (this.firstChild == oChild) {
this.firstChild = oChild.nextSibling;
}
if (oChild.previousSibling) {
oChild.previousSibling.nextSibling = oChild.nextSibling;
}
if (oChild.nextSibling) {
oChild.nextSibling.previousSibling = oChild.previousSibling;
}
var wasLast = oChild.isLastSibling();
oChild.parentNode = null;
oChild.tree = null;
oChild.depth = null;
if (t && this.getCreated() && !t.getSuspendRedraw()) {
var el = this.getChildrenElement();
var childEl = oChild.getElement();
el.removeChild(childEl);
if (wasLast) {
var newLast = this.getLastChild();
if (newLast) {
newLast.updateExpandIcon();
}
}
if (!this.hasChildren()) {
el.style.display = "none";
this.updateExpandIcon();
this.updateIcon();
}
}
return oChild;
};
WebFXTreeAbstractNode._onTimeoutFocus = function (sId) {
var jsNode = webFXTreeHandler.all[sId];
jsNode.focus();
};
_p.getId = function () {
return this.id;
};
_p.getTree = function () {
throw new Error("getTree called on Abstract Node");
};
_p.getDepth = function () {
throw new Error("getDepth called on Abstract Node");
};
_p.getCreated = function () {
var t = this.getTree();
return t && t.rendered;
};
_p.getParent = function () {
return this.parentNode;
};
_p.contains = function (oDescendant) {
if (oDescendant == null) return false;
if (oDescendant == this) return true;
var p = oDescendant.parentNode;
return this.contains(p);
};
_p.getChildren = _p.getChildNodes = function () {
return this.childNodes;
};
_p.getFirstChild = function () {
return this.childNodes[0];
};
_p.getLastChild = function () {
return this.childNodes[this.childNodes.length - 1];
};
_p.getPreviousSibling = function () {
return this.previousSibling;
//var p = this.parentNode;
//if (p == null) return null;
//var cs = p.childNodes;
//return cs[arrayHelper.indexOf(cs, this) - 1]
};
_p.getNextSibling = function () {
return this.nextSibling;
//var p = this.parentNode;
//if (p == null) return null;
//var cs = p.childNodes;
//return cs[arrayHelper.indexOf(cs, this) + 1]
};
_p.hasChildren = function () {
return this.childNodes.length > 0;
};
_p.isLastSibling = function () {
return this.nextSibling == null;
//return this.parentNode && this == this.parentNode.getLastChild();
};
_p.findChildByText = function (s, n) {
if (!n) {
n = 0;
}
var isRe = s instanceof RegExp;
for (var i = 0; i < this.childNodes.length; i++) {
if (isRe && s.test(this.childNodes[i].getText()) ||
this.childNodes[i].getText() == s) {
if (n == 0) {
return this.childNodes[i];
}
n--;
}
}
return null;
};
_p.findNodeByText = function (s, n) {
if (!n) {
n = 0;
}
var isRe = s instanceof RegExp;
if (isRe && s.test(this.getText()) || this.getText() == s) {
if (n == 0) {
return this.childNodes[i];
}
n--;
}
var res;
for (var i = 0; i < this.childNodes.length; i++) {
res = this.childNodes[i].findNodeByText(s, n);
if (res) {
return res;
}
}
return null;
};
/* end tree model */
_p.setId = function (sId) {
var el = this.getElement();
webFXTreeHandler.removeNode(this);
this.id = sId;
if (el) {
el.id = sId;
}
webFXTreeHandler.addNode(this);
};
_p.isSelected = function () {
return this._selected;
};
_p.select = function () {
this._setSelected(true);
};
_p.deselect = function () {
this._setSelected(false);
};
_p._setSelected = function (b) {
var t = this.getTree();
if (!t) return;
if (this._selected != b) {
this._selected = b;
var wasFocused = false; // used to keep focus state
var si = t.getSelected();
if (b && si != null && si != this) {
var oldFireChange = t._fireChange;
wasFocused = si._focused;
t._fireChange = false;
si._setSelected(false);
t._fireChange = oldFireChange;
}
var el = this.getRowElement();
if (el) {
el.className = this.getRowClassName();
}
if (b) {
this._setTabIndex(t.tabIndex);
t._selectedItem = this;
t._fireOnChange();
t.setSelected(this);
if (wasFocused) {
this.focus();
}
} else {
this._setTabIndex(-1);
}
if (t.getBehavior() != "classic") {
this.updateIcon();
}
}
};
_p.getExpanded = function () {
return this.open;
};
_p.setExpanded = function (b) {
var ce;
this.open = b;
var t = this.getTree();
if (this.hasChildren()) {
var si = t ? t.getSelected() : null;
if (!b && this.contains(si)) {
this.select();
}
var el = this.getElement();
if (el) {
ce = this.getChildrenElement();
if (ce) {
ce.style.display = b ? "block" : "none";
}
var eie = this.getExpandIconElement();
if (eie) {
eie.src = this.getExpandIconSrc();
}
}
if (webFXTreeConfig.usePersistence) {
webFXTreeHandler.persistenceManager.setExpanded(this, b);
}
} else {
ce = this.getChildrenElement();
if (ce)
ce.style.display = "none";
}
if (t && t.getBehavior() == "classic") {
this.updateIcon();
}
};
_p.toggle = function () {
this.setExpanded(!this.getExpanded());
};
_p.expand = function () {
this.setExpanded(true);
};
_p.collapse = function () {
this.setExpanded(false);
};
_p.collapseChildren = function () {
var cs = this.childNodes;
for (var i = 0; i < cs.length; i++) {
cs[i].collapseAll();
}
};
_p.collapseAll = function () {
this.collapseChildren();
this.collapse();
};
_p.expandChildren = function () {
var cs = this.childNodes;
for (var i = 0; i < cs.length; i++) {
cs[i].expandAll();
}
};
_p.expandAll = function () {
this.expandChildren();
this.expand();
};
_p.reveal = function () {
var p = this.getParent();
if (p) {
p.setExpanded(true);
p.reveal();
}
};
_p.openPath = function (sPath, bSelect, bFocus) {
if (sPath == "") {
if (bSelect) {
this.select();
}
if (bFocus) {
window.setTimeout("WebFXTreeAbstractNode._onTimeoutFocus(\"" + this.id + "\")", 10);
}
return;
}
var parts = sPath.split("/");
var remainingPath = parts.slice(1).join("/");
var t = this.getTree();
if (sPath.charAt(0) == "/") {
if (t) {
t.openPath(remainingPath, bSelect, bFocus);
} else {
throw "Invalid path";
}
} else {
// open
this.setExpanded(true);
parts = sPath.split("/");
var ti = this.findChildByText(parts[0]);
if (!ti) {
throw "Could not find child node with text \"" + parts[0] + "\"";
}
ti.openPath(remainingPath, bSelect, bFocus);
}
};
_p.focus = function () {
var el = this.getLabelElement();
if (el) {
el.focus();
}
};
_p.getFocused = function () {
return this._focused;
};
_p._setTabIndex = function (i) {
var a = this.getLabelElement();
if (a) {
a.setAttribute("tabindex", i);
}
};
// HTML generation
_p.toHtml = function () {
var sb = [];
var cs = this.childNodes;
var l = cs.length;
for (var y = 0; y < l; y++) {
sb[y] = cs[y].toHtml();
}
var t = this.getTree();
var hideLines = !t.getShowLines() || t == this.parentNode && !t.getShowRootLines();
var childrenHtml = "