HcksTreeview.prototype.loaded = false;
$.fn.extend({
treeInput : function(opt) {
this.each(function() {
var $this = $(this);
var treeName = this.getAttribute("id");
if (!treeName || treeName.length == 0) {
treeName = this.getAttribute("name");
}
var multipleChooser = $this.is("div");
var tree = $this.data("tree");
if (tree == null) {
tree = {};
$this.data("multipleChooser", multipleChooser);
var treePanel = $("
");
treePanel.addClass("x-tree-dropdown-panel");
$("body").append(treePanel);
var treePanelCover = null;
if ($.browser.msie && $.browser.version < 7) {
treePanelCover = $("");
treePanelCover.addClass("x-tree-dropdown-panel").css("z-index", "1000");
$("body").append(treePanelCover);
}
$(document).mouseup(function(evt){
if (treePanel.is(":hidden")) return true;
var p = evt.target;
while(p) {
if (p == treePanel.get(0)) {
break;
}
p = p.parentNode;
}
if (p == null || typeof(p) == 'undefined') {
treePanel.hide();
if (treePanelCover) treePanelCover.hide();
}
});
tree.panel = treePanel;
tree.cover = treePanelCover;
if (multipleChooser) {
var btnTab = $("").addClass("header");
var okBtn = $("");
var canBtn = $("");
if (opt.selectAll) {
var selAllBtn = $("");
$("tr td:first-child", btnTab).append(selAllBtn).append("全选/反选");
selAllBtn.click(function() {
if (this.checked)
tree.tree.checkAll(true);
else
tree.tree.uncheckAll(true);
});
}
if (opt.allowFilter) {
var txtFilterDealer = $("");
$("tr td:nth-child(2)", btnTab).append(txtFilterDealer);
txtFilterDealer.keyup(function(evt){
if (this.getAttribute('lastWord') != this.value) {
var nodes = tree.tree.getRoot().get(0).getElementsByTagName("li");
if (this.value.length > 0) {
var filterText = this.value;
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
var filterResult = opt.filter.call(node, tree.tree, filterText, $this);
if (filterResult >= 0) {
var vis = filterResult == 1;
if (vis) {
if (node.style.display == 'none') node.style.display = 'block';
} else {
if (node.style.display != 'none') node.style.display = 'none';
}
if (node.getAttribute("level") == "0")
continue;
while (!node.nextSibling) {
if (!vis) {
var prevSibling = node.previousSibling;
while(prevSibling != null) {
if (prevSibling.style.display != 'none') {
vis = true;
break;
}
prevSibling = prevSibling.previousSibling;
}
}
node = node.parentNode.parentNode;
if (node.getAttribute("level") == null) {
break;
}
node.style.display = vis ? 'block' : 'none';
}
}
}
} else {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].style.display == 'none')
nodes[i].style.display = 'block';
}
}
}
this.setAttribute('lastWord', this.value);
});
}
$("tr td:last-child", btnTab).append(okBtn).append(" ").append(canBtn);
var mainPanel = $("")
.css({height:treePanel.height() - 40});
if ($.browser.msie && $.browser.version < 7.0)
mainPanel.css({width:'98%'});
treePanel.css({"overflow":"hidden"});
treePanel.append(btnTab);
treePanel.append(mainPanel);
canBtn.click(function(){
tree.hide();
});
okBtn.click(function(){
$this.contents().not("a").not($("#selView")[0]).remove();
var nodes = tree.tree.getCheckedNodes();
var v = $("#selView");
v.html("");
nodes.each(function(i){
var hn = $this.attr("name");
if ($.isFunction(opt.getSubmitName)) {
hn = opt.getSubmitName.call($this, this, tree.tree);
}
var hd = tree.tree.getNodeAttr(this, "text");
if ($.isFunction(opt.getSelectDesc)) {
hd = opt.getSelectDesc.call($this, this, tree.tree);
}
var hv = tree.tree.getNodeAttr(this, "id");
if ($(":checkbox[name='" + hn + "'][value='" + hv + "']", $this).length > 0) {
return;
}
var hidValue = $("").val(hv);
hidValue.attr("name", hn);
$this.append(hidValue);
var hidDesc = $("").val(hd);
hidDesc.attr("name", hn + "_DESC");
$this.append(hidDesc);
// //ls add
// if(i==0){
// v.append(hd).append(" ; ").append(" ");
// }else if( i== nodes.length-1){
// if((i+1) % 3 == 0){
// v.append(hd).append(" ; ").append("
");
// }else{
// v.append(hd).append(" ; ").append(" ");
// }
// }else {
// if((i+1) % 3 == 0){
// v.append(hd).append(" ; ").append("
");
// }else{
// v.append(hd).append(" ; ").append(" ");
// }
// }
// if( i== nodes.length-1){
// if(i % 3 == 0 && i !=0 ){
// v.append(hd);
// }else{
//
// }
// }else{
// if(i % 3 == 0 && i !=0 ){
// v.append(hd).append("; ").append("
");
// }else{
// v.append(hd).append("; ").append(" ");
// }
//
// }
if ($this.hasClass("checkbox")) {
var cb = $("")
.attr("dataName", hn)
.attr("dataValue", hidValue.val())
.attr("title", hd);
cb.bind("click", function(e) {
var dataName = this.getAttribute("dataName");
var dataValue = this.getAttribute("dataValue");
if (this.checked) {
$this.append($("").attr("name", dataName).attr("value", dataValue));
$this.append($("").attr("name", dataName + "_DESC").attr("value", this.title));
} else {
$this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
$this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
}
});
$this.append(cb);
$this.append(cb.attr("title"));
}
});
if (!$this.hasClass("checkbox")) {
$("span", $this).text(nodes.length);
}
if (opt && opt.onSelected) {
opt.onSelected.call($this, true, nodes, tree.tree);
}
tree.hide();
});
} else {
treePanel.prepend($("").attr("src", __BLANK_IMAGE).bind("click", tree, function(e) {
$this.val("");
if (opt && $.isFunction(opt.onClear)) {
opt.onClear.call($this);
}
e.data.hide();
}));
treePanel.prepend($("").attr("src", __BLANK_IMAGE).bind("click", tree, function(e) {
e.data.hide();
}));
}
tree.selectNode = function(t) {
if (opt && $.isFunction(opt.onSelected)) {
opt.onSelected.call($this, false, t.getSelectedNode(), t);
if(!multipleChooser){
tree.hide();
}
}
var selectedV = t.getNodeAttr(t.getSelectedNode(), "id");
if ($this.attr("onselectedchange") && $this.attr("selectedValue") != selectedV) {
var changeHandler = eval($this.attr("onselectedchange"));
if ($.isFunction(changeHandler))
changeHandler(selectedV, t.getNodeAttr(t.getSelectedNode(), "text"));
}
$this.attr("selectedValue", selectedV);
}
var treeView = new HcksTreeview(
multipleChooser ? treePanel.get(0).lastChild : treePanel, {
url: __CONTEXT_PATH + opt.ajaxUrl,
onselect:tree.selectNode,
oncheckchanging : function(t, n){
if(t.getNodeAttr(n, 'init') == 'n') {
t.asynInitChildNodes(n, null, false, false, true);
}
},
beforeAsyn : function(n, p) {
if ($.isFunction(opt.extraParams)) {
var params = opt.extraParams($this);
for (var k in params) {
p[k] = params[k];
}
}
}
});
tree.tree = treeView;
tree.show = function(x, y) {
this.panel.css("left", x).css("top", y);
this.panel.show();
if (this.cover) this.cover.css("left", x).css("top", y).show();
if (this.tree.loaded == false) {
this.tree.asynInitChildNodes(null, null, false);
this.tree.loaded = true;
}
}
tree.tryLoad = function() {
if (this.tree.loaded == false) {
this.tree.asynInitChildNodes(null, null, false);
this.tree.loaded = true;
}
}
tree.hide = function() {
this.panel.hide();
if (this.cover) this.cover.hide();
}
tree.isHidden = function() {
return this.panel.is(":hidden");
}
tree.refresh = function() {
$this.flushCache();
return this.tree.asynInitChildNodes(null, null, false);
}
$this.data("tree", tree);
}
if (!multipleChooser) {
$this.click(function(e) {
var pos = $this.offset();
if (pos.left + $this.width() - e.clientX < 18) {
if (tree.isHidden()) {
tree.show(pos.left, pos.top + $this.height() + 5);
} else {
tree.hide();
}
}
});
$this.hover(
function(){$this.addClass("chooserMouseOver")},
function(){$this.removeClass("chooserMouseOver")}
);
} else {
var btn = $("请选择 ");
var selections = $this.children(":hidden.value");
if (!$this.hasClass("checkbox")) {
var linkSelected = $("(已选 " + selections.length + ")");
var selectedViews = $("()");
$this.prepend(" ").prepend(selectedViews).prepend(" ").prepend(linkSelected).prepend(" ").prepend(btn);
linkSelected.click(function() {
var pan = $("#__treeChooserSelection");
if (pan.length == 0) {
pan = $("")
var chooserSelectionCover = null;
if ($.browser.msie && $.browser.version < 7) {
chooserSelectionCover = $("");
chooserSelectionCover.addClass("x-dropdowntree-selections").css("z-index", "1000");
$("body").append(chooserSelectionCover);
}
pan.dblclick(function(){
$(this).hide()
if (chooserSelectionCover) chooserSelectionCover.hide();
});
pan.children("div.title").prepend($("").attr("src", __BLANK_IMAGE).bind("click", function(e) {
pan.hide();
if (chooserSelectionCover) chooserSelectionCover.hide();
}));
$(document.body).append(pan);
}
var tab = $("table:first", pan);
tab.empty();
selections = $this.children(":hidden.value");
var selectionTitles = $this.children(":hidden.desc");
for (var i = 0; i < selections.length; i ++) {
if ($(":checkbox[name='" + selections.get(i).name + "'][value='" + selections.get(i).value + "']", $this).length > 0) {
continue;
}
var sr = $(" | |
")
.css("background-color", i % 2 == 0 ? "#fff":"#eee");
var title = "";
if (selectionTitles.length == selections.length) {
title = selectionTitles.get(i).value;
} else {
var tn = document.getElementById(selections.get(i).value);
if (typeof(tn) != 'undefined' && tn.level) {
title = tree.tree.getNodeAttr(tn, "text");
}
}
var cb = $("")
.attr("dataName", selections.get(i).name)
.attr("dataValue", selections.get(i).value)
.attr("title", title);
var counter = selections.length;
cb.bind("click", function(e) {
var dataName = this.getAttribute("dataName");
var dataValue = this.getAttribute("dataValue");
if (this.checked) {
$this.append($("").attr("name", dataName).attr("value", dataValue));
$this.append($("").attr("name", dataName + "_DESC").attr("value", this.title));
counter ++;
} else {
$this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
$this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
counter--;
}
$("span", $this).text(counter.toString());
});
sr.children(":first-child").append(cb);
sr.children(":last-child").html(cb.attr("title"));
tab.append(sr);
}
var spos = $(this).offset();
pan.css({"left":spos.left, "top":spos.top}).show();
if (chooserSelectionCover) chooserSelectionCover.css({"left":spos.left, "top":spos.top}).show();
});
} else {
$this.prepend(btn);
var selections = $this.children(":hidden.value");
var selectionTitles = $this.children(":hidden.desc");
for (var i = 0; i < selections.length; i++) {
var f = ":checkbox[name='" + selections.get(i).name + "'][value='" + selections.get(i).value + "']";
if ($(f).length > 0) {
continue;
}
var title = "";
if (selectionTitles.length == selections.length) {
title = selectionTitles.get(i).value;
} else {
var tn = document.getElementById(selections.get(i).value);
if (typeof(tn) != 'undefined' && tn.level) {
title = tree.tree.getNodeAttr(tn, "text");
}
}
var cb = $("")
.attr("dataName", selections.get(i).name)
.attr("dataValue", selections.get(i).value)
.attr("title", title);
cb.bind("click", function(e) {
var dataName = this.getAttribute("dataName");
var dataValue = this.getAttribute("dataValue");
if (this.checked) {
$this.append($("").attr("name", dataName).attr("value", dataValue));
$this.append($("").attr("name", dataName + "_DESC").attr("value", this.title));
} else {
$this.children("input[name='" + dataName + "'][value='" + dataValue + "']").remove();
$this.children("input[name='" + dataName + "_DESC'][value='" + this.title + "']").remove()
}
});
$this.append(cb);
$this.append(cb.attr("title"));
}
}
btn.click (function() {
if (tree.isHidden()) {
var pos = $(this).offset();
tree.show(pos.left, pos.top);
} else {
tree.hide();
}
});
}
});
}
});