123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- 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 = $("<div></div>");
- treePanel.addClass("x-tree-dropdown-panel");
- $("body").append(treePanel);
-
- var treePanelCover = null;
- if ($.browser.msie && $.browser.version < 7) {
- treePanelCover = $("<iframe frameborder='0'></iframe>");
- 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 = $("<table width='100%'><tr><td width=100 align=center></td><td width=1></td><td></td><td align='right'></td></tr></table>").addClass("header");
-
- var okBtn = $("<input type='button' class='button' value='确定'/>");
- var canBtn = $("<input type='button' class='button' value='取消'/>");
- if (opt.selectAll) {
- var selAllBtn = $("<input type='checkbox' />");
- $("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 = $("<input type='text' class='text' size='8'/>");
- $("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 = $("<div style='padding:4px;overflow:auto'></div>")
- .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 = $("<input class='value' type='hidden'/>").val(hv);
- hidValue.attr("name", hn);
- $this.append(hidValue);
- var hidDesc = $("<input type='hidden' class='desc'/>").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(" <br/>");
- // }else{
- // v.append(hd).append(" ; ").append(" ");
- // }
- // }else {
- // if((i+1) % 3 == 0){
- // v.append(hd).append(" ; ").append(" <br/>");
- // }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(" <br/>");
- // }else{
- // v.append(hd).append("; ").append(" ");
- // }
- //
- // }
-
- if ($this.hasClass("checkbox")) {
-
- var cb = $("<input type='checkbox' checked='checked'/>")
- .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($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
- $this.append($("<input class='desc' type='hidden'/>").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($("<IMG alt='清除' class='clear-handler'/>").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($("<IMG alt='关闭' class='close-handler'/>").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 = $("<a class='btn' href='javascript:void(0)'>请选择 </a>");
- var selections = $this.children(":hidden.value");
- if (!$this.hasClass("checkbox")) {
- var linkSelected = $("<a href='javascript:void(0)'>(已选 <span style='font-weight:bold'>" + selections.length + "</span>)</a>");
- var selectedViews = $("(<div id='selView'></div>)");
- $this.prepend(" ").prepend(selectedViews).prepend(" ").prepend(linkSelected).prepend(" ").prepend(btn);
-
- linkSelected.click(function() {
- var pan = $("#__treeChooserSelection");
- if (pan.length == 0) {
- pan = $("<div id='__treeChooserSelection' class='x-dropdowntree-selections'><div class='title'>已选择列表</div><div class='list'><table width='100%' cellpadding=0 cellspacing=0></table></div></div>")
- var chooserSelectionCover = null;
- if ($.browser.msie && $.browser.version < 7) {
- chooserSelectionCover = $("<iframe frameborder='0'></iframe>");
- 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($("<IMG alt='关闭' class='close-handler'/>").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 = $("<tr><td width='1'></td><td align='left'></td></tr>")
- .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 = $("<input type='checkbox' checked='checked'/>")
- .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($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
- $this.append($("<input class='desc' type='hidden'/>").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 = $("<input type='checkbox' checked='checked'/>")
- .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($("<input class='value' type='hidden'/>").attr("name", dataName).attr("value", dataValue));
- $this.append($("<input class='desc' type='hidden'/>").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();
- }
- });
- }
- });
- }
- });
|