123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- bindEventHandler = function(obj, evtName, func) {
- var args = new Array(arguments.length - 3);
- for (var i = 0; i < args.length; i ++) {
- args[i] = arguments[i + 3];
- }
- if ($.browser.msie) {
- obj.attachEvent("on" + evtName, function(evt){
- args[args.length] = window.event;
- return func.apply(obj, args)}
- );
- } else {
- obj.addEventListener(evtName, function(evt){
- args[args.length] = evt;
- return func.apply(obj, args)}, false);
- }
- }
- findChildByTagName = function(obj, tagName) {
- var children = obj.childNodes;
- for (var i = 0; i < children.length; i++) {
- if (children[i].tagName == tagName) {
- return children[i];
- }
- }
- }
- jQuery.fn.hcksTree = function(options) {
- $(this).each(function(){
- var tree = new HcksTreeview($(this), options);
- if (options && options.loadImmediate) {
- tree.asynInitChildNodes(null);
- }
- });
- };
- jQuery.fn.contextMenu = function(options) {
- var menu = new HcksMenu(options);
- $(this).bind("contextmenu", function(e) {
- menu.show(e);
- return false;
- });
- return this;
- };
- /**
- * 菜单
- * @param {} options
- */
- function HcksMenu(options) {
-
- var extraParams = null;
- var menu = document.createElement("DIV");
- menu.className = "hcks-menu";
- document.body.appendChild(menu);
-
- var $menu = $(menu);
- $menu.bind("click", function(e) { e.stopPropagation(); });
-
- var ul = document.createElement("UL");
- menu.appendChild(ul);
-
- var shadow = document.createElement("DIV");
- shadow.className = "hcks-menu-shadow";
- document.body.appendChild(shadow);
-
- function hide() {
- menu.style.display = "none";
- shadow.style.display = "none";
- }
-
- this.setExtraParams = function(params) {
- extraParams = params;
- }
-
- this.addItem = function(menuItem) {
- var item = document.createElement("LI");
- $(item).attr("command", menuItem.command).hover(
- function() {
- $(this).addClass("menuitem-hover");
- },
- function(){
- $(this).removeClass("menuitem-hover");
- }
- );
- if (options && typeof(options.handler) == 'function') {
- $(item).click(function(evt){
- options.handler.call(this, menuItem.command, extraParams);
- hide();
- extraParams = null;
- });
- }
- var icon = document.createElement("img");
- icon.src = __CONTEXT_PATH + "/resource/images/icons/" + menuItem.icon;
- item.appendChild(icon);
- item.appendChild(document.createTextNode(menuItem.text));
- ul.appendChild(item);
- }
-
- if (options && options.menus) {
- for (var cmd in options.menus) {
- var mi = options.menus[cmd];
- mi.command = cmd;
- this.addItem(mi);
- }
- }
-
- this.show = function(e, commands) {
- var mustShow = true;
- if (commands) {
- mustShow = false;
- $menu.find("li").each(function(){
- if (commands.indexOf($(this).attr("command")) == -1) {
- $(this).hide();
- } else {
- $(this).show();
- mustShow = true;
- }
- });
- }
- if (mustShow) {
- var x = e.pageX ? e.pageX : e.clientX;
- var y = e.pageY ? e.pageY : e.clientY;
- if (!e.pageX) {
- var doc = document.documentElement, body = document.body;
- x = x + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
- y = y + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
- }
- $menu.css({'left':x, 'top':y}).show();
- $(shadow).css({width:$menu.width(),height:$menu.height(),left:x+2,top:y+2}).show();
- $(document).one('click', hide);
- }
- }
- }
- /**
- * 树
- * @param {} container
- * @param {} options
- */
- function HcksTreeview(container, options) {
- var loading = false;
-
- var rootNode = $("<ul></ul>").addClass("x-tree-root-ct");
-
- options.name = options.name || "";
- options.parentName = options.parentName || "parentId";
- options.valueName = options.valueName || "value";
- options.showLine = options.showLine || true;
- options.loadingMessage = options.loadingMessage || "正在加载,请稍候...";
-
- if (options.showLine) {
- rootNode.addClass("x-tree-lines");
- } else {
- rootNode.addClass("x-tree-no-lines");
- }
-
- var loadingMessage = document.createElement("LI");
- loadingMessage.className = "x-tree-loading";
- loadingMessage.innerHTML = options.loadingMessage;
-
- if (typeof(container) == 'string') {
- container = $("#" + container);
- } else {
- container = $(container);
- }
- container.addClass("x-hcks-tree").append(rootNode);
- container.bind("contextmenu", function(e){e.stopPropagation(); return false;});//阻止事件冒泡
- if ($.browser.msie) {
- container.addClass("ext-ie");//不显示超过容器尺寸的内容
- container.get(0).onselectstart = function(){return false}
- }
-
- this.selectedNode = null;
-
- var blankImg = function() {
- var img = document.createElement("IMG");
- img.src = __BLANK_IMAGE;
- return img;
- }
-
- var isLeaf = function(node) {
- return node.children("div").hasClass("x-tree-node-leaf");
- }
-
- var fixNode = function(node) {
- if (!node) return null;
- if (typeof(node) == 'string') {
- node = $("#" + node);
- } else {
- node = $(node);
- }
- return node;
- }
-
- var setToLeaf = function(node) {
- var div = node.children("div:first");//查找节点下首个div块
- var handlerIcon = div.children("img:first");
- if (handlerIcon.hasClass("x-tree-elbow-end-minus") || handlerIcon.hasClass("x-tree-elbow-end-plus")) {
- handlerIcon.removeClass("x-tree-elbow-end-minus").removeClass("x-tree-elbow-end-plus");
- handlerIcon.addClass("x-tree-elbow-end");
- } else if (handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-minus")) {
- handlerIcon.removeClass("x-tree-elbow-plus").removeClass("x-tree-elbow-minus");
- handlerIcon.addClass("x-tree-elbow");
- }
-
- var icon = div.children("img:last");
- if (icon.attr("src") != __BLANK_IMAGE) {
- if (icon.attr("collapsedIcon")) {
- icon.attr("src", icon.attr("collapsedIcon"));
- }
- } else {
- div.removeClass("x-tree-node-expanded").addClass("x-tree-node-collapsed");
- }
- }
-
- this.setToFolder = function(node) {
- node = fixNode(node);
- alert(node.html());
- var div = node.children("div:first");//查找节点下首个div块
- var handlerIcon = div.children("img:first");
-
- if (handlerIcon.hasClass("x-tree-elbow-end")) {
- handlerIcon.removeClass("x-tree-elbow-end");
- handlerIcon.addClass("x-tree-elbow-end-plus");
- } else if (handlerIcon.hasClass("x-tree-elbow") ) {
- handlerIcon.removeClass("x-tree-elbow");
- handlerIcon.addClass("x-tree-elbow-plus");
- }
- // if (handlerIcon.hasClass("x-tree-elbow-end-minus") || handlerIcon.hasClass("x-tree-elbow-end-plus")) {
- // handlerIcon.removeClass("x-tree-elbow-end-minus").removeClass("x-tree-elbow-end-plus");
- // handlerIcon.addClass("x-tree-elbow-end");
- // } else if (handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-minus")) {
- // handlerIcon.removeClass("x-tree-elbow-plus").removeClass("x-tree-elbow-minus");
- // handlerIcon.addClass("x-tree-elbow");
- // }
-
- var icon = div.children("img:last");
- if (icon.attr("src") != __BLANK_IMAGE) {
- if (icon.attr("expandedIcon")) {
- icon.attr("src", icon.attr("expandedIcon"));
- }
- } else {
- div.removeClass("x-tree-node-collapsed").addClass("x-tree-node-expanded");
- }
- }
-
- this.clear = function(node) {
- rootNode.empty();
- rootNode.get(0).innerHTML = "";
- }
-
- this.expanded = function(node) {
- node = fixNode(node);
- var handlerIcon = node.children("div:first").children("img:first");
- return handlerIcon.hasClass("x-tree-elbow-minus") || handlerIcon.hasClass("x-tree-elbow-end-minus");
- }
-
- this.collapsed = function(node) {
- node = fixNode(node);
- var handlerIcon = node.children("div:first").children("img:first");
- return handlerIcon.hasClass("x-tree-elbow-plus") || handlerIcon.hasClass("x-tree-elbow-end-plus");
- }
-
- this.asynInitChildNodes = function(pNode, extraParams, dynamic, asyn, recursive) {
- if (typeof(asyn) == 'undefined') asyn = true;
-
- if (loading && asyn) return;
-
- if (!options.url) return;
-
- var node = fixNode(pNode);
-
- var root = node == null;
- var loadingContainer = root ? rootNode : node.children("ul").show();
- loadingContainer.append(loadingMessage);
-
- var tree = this;
-
- var params = {};
-
- if (!root) {
- params[options.parentName] = this.getNodeAttr(node, "id");
- params[options.valueName] = this.getNodeAttr(node, "value");
- params["checked"] = this.getNodeAttr(node, "checked");
- }
- if (options.extraParams) {
- for (var k in options.extraParams) {
- params[k] = options.extraParams[k];
- }
- }
-
- if (extraParams) {
- for (var k in extraParams) {
- params[k] = extraParams[k];
- }
- }
-
- loading = true;
-
- if (options && typeof(options.beforeAsyn) == 'function') {
- options.beforeAsyn.call(tree, node, params);
- }
-
- $.ajax({
- url:options.url,
- data:params,
- type:'post',
- cache:false,
- async: (typeof(asyn) == 'undefined'? true:asyn),
- success:function(data) {
- loadingContainer.empty();
- loadingContainer.innerHTML = "";
- if (!root) {
- node.attr("init", 'y');
- }
- try {
- var nodesData = eval(data);
-
- if (dynamic == false) {
- tree.initalize(nodesData,
- node == null ? null : node.children("ul:last").get(0),
- node == null ? 0 : parseInt(node.attr("level")) + 1, asyn, recursive);
- } else {
- for(var i = 0; i < nodesData.length; i++) {
- var nodeData = nodesData[i];
- var newNode = tree.addNode(nodeData, node);
-
- if (newNode && newNode.getAttribute("init") == "n" && recursive) {
- tree.asynInitChildNodes(newNode, extraParams, dynamic, asyn, recursive);
- }
- };
- }
-
- if (! root ) {
-
- if (nodesData.length == 0) {
- setToLeaf(node);
- } else {
- tree.expand(node); ;
- }
- }
- } catch (e) {
- alert('程序发生意外错误!\n' + e.description)
- }
- },
- complete:function() {
- tree = null;
- loading = false;
- }
- });
- }
-
- /**
- * 展开当前节点
- * @param {} node
- */
- this.expand = function(node) {
- node = fixNode(node);
- if (node.attr("init") != 'y') {
- this.asynInitChildNodes(node, null, false);
- return;
- }
- var div = node.children("div:first");
- var handlerIcon = div.children("img:first");
- if (handlerIcon.hasClass("x-tree-elbow-end-plus")) {
- handlerIcon.removeClass("x-tree-elbow-end-plus").addClass("x-tree-elbow-end-minus");
- } else if (handlerIcon.hasClass("x-tree-elbow-plus")) {
- handlerIcon.removeClass("x-tree-elbow-plus").addClass("x-tree-elbow-minus");
- }
-
- var icon = div.children("img:last");
- if (icon.attr("src") != __BLANK_IMAGE) {
- if (icon.attr("expandedIcon")) icon.attr("src", icon.attr("expandedIcon"));
- } else {
- div.removeClass("x-tree-node-collapsed").addClass("x-tree-node-expanded");
- }
- node.attr("init", 'y');
- var delay = options && options.delay ? options.delay : null;
- node.children("ul").show(delay);
- }
-
- this.collapse = function(node) {
- node = fixNode(node);
- if (node == null) return;
- var div = node.children("div:first");
- var handlerIcon = div.children("img:first");
- if (handlerIcon.hasClass("x-tree-elbow-end-minus")) {
- handlerIcon.removeClass("x-tree-elbow-end-minus").addClass("x-tree-elbow-end-plus");
- } else if (handlerIcon.hasClass("x-tree-elbow-minus")) {
- handlerIcon.removeClass("x-tree-elbow-minus").addClass("x-tree-elbow-plus");
- }
- var icon = div.children("img:last");
- if (icon.attr("src") != __BLANK_IMAGE) {
- if (icon.attr("collapsedIcon")) icon.attr("src", icon.attr("collapsedIcon"));
- } else {
- div.removeClass("x-tree-node-expanded").addClass("x-tree-node-collapsed");
- }
- var delay = options && options.delay ? options.delay : null;
- node.children("ul").hide(delay);
- }
-
- this.toggle = function(node) {
- if (this.expanded(node)) {
- this.collapse(node);
- } else if (this.collapsed(node)) {
- this.expand(node);
- }
- }
-
- /**
- * 设置节点属性
- * @param {} node 节点
- * @param {} attr 属性名称
- * @param {} value 值
- */
- this.setNodeAttr = function(node, attr, value) {
- node = fixNode(node);
- if (attr == "text") {
- node.children("div:first").children("a").html(value);
- } else if (attr == "href") {
- node.children("div:first").children("a").attr(attr, value);
- } else if (attr == "target") {
- node.children("div:first").children("a").attr(attr, value);
- } else if (attr == "checked") {
- var checkImg = node.children("div:first").children("img:eq(1)");
- if(value == true){
- checkImg.removeClass("x-node-unchecked").addClass("x-node-check-all");
- }else{
- checkImg.removeClass("x-node-check-all").addClass("x-node-unchecked");
- }
- }else {
- node.attr(attr, value);
- }
- }
-
- this.getNodeAttr = function(node, attr) {
- node = fixNode(node);
- if (node == null) return "";
- if (attr == "text") {
- r = node.children("div:first").children("a").html();
- } else if (attr == "value") {
- r = node.attr("val");
- } else if (attr == "href" || attr == "target") {
- r = node.children("div:first").children("a").attr(attr);
- }else if (attr == "checked") {
- var checkImg = node.children("div:first").children("img:eq(1)");
- r = checkImg.hasClass("x-node-check-all") || checkImg.hasClass("x-node-check-partly");
- } else if (attr == "id") {
- r = node.attr(attr).substr(options.name.length);
- } else {
- r = node.attr(attr);
- }
- if (typeof(r) == 'undefined') {
- return "";
- }
- return r;
- }
-
- this.getSelectedNode = function() {
- return this.selectedNode;
- }
-
- this.getCheckedNodes = function(fn) {
- return container.find("li.x-tree-node[canSelect='true']").filter(
- function() {
- var r = (typeof(fn) == 'undefined' || ($.isFunction(fn) && fn(this))) && $(this).children("div:first").children("img:eq(1)").hasClass("x-node-check-all");
- return r;
- }
- );
- }
-
- this.checkAll = function(visible) {
- if (visible)
- container.find("li.x-tree-node:visible img.x-node-unchecked").filter(function(){
- return $(this).is(":visible") && $(this).parents("li.x-tree-node:hidden").length==0;
- }).removeClass("x-node-unchecked").addClass("x-node-check-all");
- else
- container.find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
- }
-
- this.uncheckAll = function(visible) {
- if (visible)
- container.find("li.x-tree-node:visible img.x-node-check-all").filter(function(){
- return $(this).is(":visible") && $(this).parents("li.x-tree-node:hidden").length==0;
- }).removeClass("x-node-check-all").addClass("x-node-unchecked");
- else
- container.find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
- }
-
- this.getParent = function(node) {
- if (node == null || typeof(node) == "undefined")
- return null;
- return fixNode(node).parents("li:first");
- }
-
- this.getAncensor = function(node, attr, val) {
- var p = this.getParent(node)
- while ( p != null && p.length > 0 ) {
- if (this.getNodeAttr(p, attr) == val) {
- return p;
- }
- p = this.getParent(p);
- }
- return null;
- }
-
- this.getChildren = function(node) {
- if (node != null)
- return fixNode(node).children("ul:first").children("li");
- else
- return rootNode.children("li");
- }
-
- this.getNodes = function(level) {
- return rootNode.find("li.x-tree-node[level=" + level + "]");
- }
-
- this.getRoot = function() {
- return rootNode;
- }
-
- this.removeNode = function(node) {
- node = fixNode(node);
- var prev = node.prev();
- var next = node.next();
- //本层最后一个节点
- if (next.length == 0 && prev.length > 0) {
- var preHandler = prev.children("div:first").children("img:first");
- if (preHandler.hasClass("x-tree-elbow-line")) {
- preHandler.removeClass("x-tree-elbow-line").addClass("x-tree-elbow-end");
- } else if (preHandler.hasClass("x-tree-elbow")) {
- preHandler.removeClass("x-tree-elbow").addClass("x-tree-elbow-end");
- } else {
- if (preHandler.hasClass("x-tree-elbow-minus")) {
- preHandler.removeClass("x-tree-elbow-minus").addClass("x-tree-elbow-end-minus");
- } else if (preHandler.hasClass("x-tree-elbow-plus")) {
- preHandler.removeClass("x-tree-elbow-plus").addClass("x-tree-elbow-end-plus");
- }
- }
- prev.find("div > span.x-tree-node-indent").each(function(){
- $(this).children("img:eq(" + node.attr("level") + ")")
- .removeClass("x-tree-elbow-line").addClass("x-tree-icon");
- });
- }
- //仅有的一个子节点
- if (next.length == 0 && prev.length == 0 && node.attr("level") > 0) {
- var p = node.parents("li:first");
- setToLeaf(p);
- }
- if (this.selectedNode == node) this.selectedNode = null;
- node.get(0).innerHTML = "";
- node.remove();
- }
-
- this.addNode = function(n, p, expandParent) {
- if (typeof(p) == 'string') {
- p = $("#" + p);
- }
- if (options && typeof(options.beforeInitNode) == "function") {
- var r = options.beforeInitNode.call(this, n);
- if (r == false) {
- return null;
- }
- }
-
- /*
- <LI id=402881822a6f66dc012a7065ab8d0001 class=x-tree-node val="CATALOG" level="1" init="n" canSelect="true" jQuery1294638645738="35">
- <DIV class="x-tree-node-el x-tree-node-collapsed" jQuery1294638645738="36">
- <SPAN class=x-tree-node-indent>
- <IMG class=x-tree-icon src="/derscm/resource/images/default/s.gif" width=1 height=1>
- </SPAN>
- <IMG class="x-tree-ec-icon x-tree-elbow-plus" src="/derscm/resource/images/default/s.gif" width=1 height=1 jQuery1294638645738="33">
- <IMG class=x-tree-node-icon src="/derscm/resource/images/default/s.gif" width=1 height=1>
- <A class=x-tree-node-anchor jQuery1294638645738="34">大板</A>
- </DIV>
- <UL style="DISPLAY: none" class="x-tree-node-ct x-tree-lines"></UL>
- </LI>
- */
-
- var li = document.createElement("LI");
- li.className = "x-tree-node";
- li.id = options.name + n.id;
- li.setAttribute("init", n.init == true ? 'y' : 'n');//是否初始化属性
- if (n.value) li.setAttribute("val", n.value); //类别
-
- //设置扩展属性
- if (n.extendAttr) {
- for (var key in n.extendAttr) {
- li.setAttribute(key, n.extendAttr[key]);
- }
- }
-
- var div = document.createElement("div");
- div.className = 'x-tree-node-el';
- var indent = $("<span></span>").addClass('x-tree-node-indent');
-
- var level = 0;
- if (p != null) { //父节点存在
- level = p.attr("level") + 1;
- var pIndent = p.find("div:first > span.x-tree-node-indent:first");
- indent.append(pIndent.children().clone()); //继承父节点的line style
-
- //根据情况增加自己的line
- var imgInd = blankImg();
- if(p.next().length == 0) {
- //console.log("1");
- imgInd.className = "x-tree-icon";
- } else {
- //console.log("2");
- imgInd.className = "x-tree-elbow-line";
- }
- indent.append(imgInd);
- div.appendChild(indent.get(0));
- }
- li.setAttribute("level", level);
-
- var handlerIcon = blankImg();
- div.appendChild(handlerIcon);
-
-
- if (n.checkbox) {
- var checkImg = blankImg();
- if (n.checked) {
- checkImg.className = "x-tree-node-icon x-node-check-all";
- } else {
- checkImg.className = "x-tree-node-icon x-node-unchecked";
- }
- $(checkImg).click(function() {
- if ($(this).hasClass("x-node-unchecked")) {
- $(this).removeClass("x-node-unchecked").addClass("x-node-check-all");
- $(li).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
- } else if ($(this).hasClass("x-node-check-all")) {
- $(this).removeClass("x-node-check-all").addClass("x-node-unchecked");
- $(li).find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
- } else if($(this).hasClass("x-node-check-incomplete")){
- $(obj).removeClass("x-node-check-incomplete").addClass("x-node-check-all");
- $(li).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
- $(li).find("img.x-node-check-incomplete").removeClass("x-node-check-incomplete").addClass("x-node-check-all");
- }
- });
- div.appendChild(checkImg);
- this.setParentCheckboxStatus(n);
- }
-
- var icon = document.createElement("IMG");
- icon.className = "x-tree-node-icon";
- if (n.expandedIcon || n.collapsedIcon || n.icon) {
- div.className += " x-tree-node-cusicon";
- icon.src = n.icon;
- icon.expandedIcon = n.expandedIcon;
- icon.collapsedIcon = n.collapsedIcon;
- if (n.expanded && n.expandedIcon) {
- icon.src = n.expandedIcon;
- } else if (n.collapsedIcon) {
- icon.src = n.collapsedIcon;
- }
- li.setAttribute("hasIcon", true);
- } else {
- icon.src = __BLANK_IMAGE;
- if (n.leaf) {
- div.className += " x-tree-node-leaf";
- } else {
- div.className += n.expanded ? " x-tree-node-expanded" : " x-tree-node-collapsed";
- }
- }
- div.appendChild(icon);
-
-
- var txt = document.createElement("a");
- if (n.href) {
- txt.href = n.href;
- if (n.target) txt.target = n.target;
- }
- txt.className = 'x-tree-node-anchor';
- txt.innerHTML = n.text;
- //txt.appendChild(document.createTextNode(n.text));
- div.appendChild(txt);
-
-
- var childUl = document.createElement("UL");
- childUl.className = "x-tree-node-ct x-tree-lines";
- if (!n.expanded) childUl.style.display = "none";
-
- li.appendChild(div);
- li.appendChild(childUl);
-
- if (p != null) {
- p.children("ul:last").append(li);
- } else {
- rootNode.append(li);
- }
-
- var preLi = $(li).prev();
- var preHandler = preLi.children("div").children("img:first");
- if (preHandler.hasClass("x-tree-elbow-end"))
- preHandler.removeClass("x-tree-elbow-end").addClass("x-tree-elbow");
- preLi.find("div > span.x-tree-node-indent").each(function(){
- $(this).children("img:eq(" + level + ")").removeClass("x-tree-icon").addClass("x-tree-elbow-line");
- });
-
- if (n.leaf || ((n.init || n.expanded) && (!n.nodes || n.nodes.length == 0))) {
- handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end";
- } else {
- if (n.expanded) {
- handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end-minus";
- li.setAttribute("init", 'y');
- } else {
- handlerIcon.className = "x-tree-ec-icon x-tree-elbow-end-plus";
- }
- }
- if (preHandler.hasClass("x-tree-elbow-end-minus")) {
- preHandler.removeClass("x-tree-elbow-end-minus").addClass("x-tree-elbow-minus");
- } else if (preHandler.hasClass("x-tree-elbow-end-plus")) {
- preHandler.removeClass("x-tree-elbow-end-plus").addClass("x-tree-elbow-plus");
- }
-
- if (p != null) {
- var pDiv = p.children("div:first");
- pDiv.removeClass("x-tree-node-leaf");//去掉叶子图标
- var pHandler = pDiv.children("img:first");
- if (pHandler.hasClass("x-tree-elbow-end")) {
- pHandler.removeClass("x-tree-elbow-end");
- if (pDiv.hasClass("x-tree-node-expanded")) {
- pHandler.addClass("x-tree-elbow-end-minus");
- } else {
- pHandler.addClass("x-tree-elbow-end-plus");
- }
- } else if (pHandler.hasClass("x-tree-elbow")) {
- pHandler.removeClass("x-tree-elbow");
- if (pDiv.hasClass("x-tree-node-expanded")) {
- pHandler.addClass("x-tree-elbow-minus");
- } else {
- pHandler.addClass("x-tree-elbow-plus");
- }
- } else if (pHandler.hasClass("x-tree-elbow-line")) {
- pHandler.removeClass("x-tree-elbow-line");
- if (pDiv.hasClass("x-tree-node-expanded")) {
- pHandler.addClass("x-tree-elbow-minus");
- } else {
- pHandler.addClass("x-tree-elbow-plus");
- }
- }
- if (!pDiv.hasClass("x-tree-node-expanded")) {
- if (expandParent) this.expand(p);
- }
- p.attr("init", 'y');
- }
-
- $(handlerIcon).bind("click", this, function(evt) {
- evt.data.toggle(li);
- });
-
- if (n.canSelect) {
- $(txt).bind("click", this, function(evt) {
- if (evt.data.selectedNode) {
- evt.data.selectedNode.children("div").children("a").removeClass("x-tree-selected");
- }
- evt.data.selectedNode = $(li);
- $(this).addClass("x-tree-selected");
- if (options && $.isFunction(options.onselect)) {
- options.onselect.call(evt.data.selectedNode, evt.data);
- }
- });
- li.setAttribute("canSelect", "true");
- }
-
- if (n.operators && options && options.contextmenu){
- $(txt).bind("contextmenu", this, function(evt) {
- options.contextmenu.setExtraParams({node:$(li),tree:evt.data});
- options.contextmenu.show(evt, n.operators);
- if (evt.data.selectedNode) {
- evt.data.selectedNode.children("div").children("a").removeClass("x-tree-selected");
- }
- evt.data.selectedNode = $(li);
- $(this).addClass("x-tree-selected");
- return false;
- });
- }
-
- if (!n.leaf) {
- $(txt).bind('dblclick', [this, li], function(evt){
- evt.data[0].toggle(evt.data[1]);
- });
- }
-
- if (n.nodes && n.nodes.length > 0){
- for (var i = 0; i < n.nodes.length; i++){
- this.addNode(n.nodes[i], $(li));
- }
- }
- return li;
- }
-
- this.setParentCheckboxStatus = function(node){
-
- var p = this.getParent(node);
-
- var cnodes = this.getChildren(p);
-
- //alert($(node).find("img.x-node-check-all").length);
-
- if($(node).find("img.x-node-check-all").length == cnodes.length){
-
- }
-
-
- // //当前同级别或低级别的节点是否都选中了
- // var isCheckedComplete = $(".x-node-unchecked", p).length == 0;
- // //当前同级别或低级别的节点是否都没有选中
- // var isCheckedNull = $(".x-node-check-all", p).length == 0;
- // if (isCheckedComplete)
- // {
- // p.prev().find(".l-checkbox")
- // .removeClass("x-node-unchecked x-node-check-incomplete")
- // .addClass("x-node-check-all");
- // }
- // else if (isCheckedNull)
- // {
- // treeitem.parent().prev().find("> .l-checkbox")
- // .removeClass("l-checkbox-checked l-checkbox-incomplete")
- // .addClass("l-checkbox-unchecked");
- // }
- // else
- // {
- // treeitem.parent().prev().find("> .l-checkbox")
- // .removeClass("l-checkbox-unchecked l-checkbox-checked")
- // .addClass("l-checkbox-incomplete");
- // }
- // if (treeitem.parent().parent("li").length > 0)
- // po.setParentCheckboxStatus(treeitem.parent().parent("li"));
- }
-
- this.initalize = function(ncol, p, lev, asyn, recursive) {
- var ncount = ncol.length;
- for (var ci = 0; ci < ncount; ci ++) {
- var n = ncol[ci];
- if (options && typeof(options.beforeInitNode) == "function") {
- var r = options.beforeInitNode.call(this, n);
- if (r == false) {
- continue;
- }
- }
- var end = ci == ncount - 1; //最后一个节点
- var noChild = n.leaf || (n.nodes != null && n.nodes.length == 0);
- var li = document.createElement("LI");
- li.className = "x-tree-node";
- li.id = options.name + n.id;
- li.setAttribute("init", n.init == true || n.nodes != null || n.expanded ? 'y' : 'n');
- li.setAttribute("level", lev);
- if (n.value) li.setAttribute("val", n.value);
- if (n.extendAttr) {
- for (var key in n.extendAttr) {
- li.setAttribute(key, n.extendAttr[key]);
- }
- }
- var div = document.createElement("div");
- div.className = 'x-tree-node-el';
-
- var indent = document.createElement("SPAN");
- indent.className = 'x-tree-node-indent';
- if (p) {
- var pIndent = p.parentNode.firstChild.firstChild.childNodes;
- for (var i = 0; i < pIndent.length; i++) {//继承父节点的line style
- indent.appendChild(pIndent[i].cloneNode(false));
- }
- var imgInd = blankImg();
- if(p.parentNode.nextSibling == null) {
- imgInd.className = "x-tree-icon";
- } else {
- imgInd.className = "x-tree-elbow-line";
- }
- indent.appendChild(imgInd);
- }
- div.appendChild(indent);
-
- var handlerIcon = blankImg();
- if (!end) {
- handlerIcon.className = n.leaf || noChild ? "x-tree-ec-icon x-tree-elbow" :
- (n.expanded ? "x-tree-ec-icon x-tree-elbow-minus" : "x-tree-ec-icon x-tree-elbow-plus");
- } else {
- handlerIcon.className = n.leaf || noChild ? "x-tree-ec-icon x-tree-elbow-end" :
- (n.expanded ? "x-tree-ec-icon x-tree-elbow-end-minus" : "x-tree-ec-icon x-tree-elbow-end-plus");
- }
- div.appendChild(handlerIcon);
-
- if (n.checkbox) {
- var checkImg = blankImg();
- if(n.checked == null){
- checkImg.className = "x-tree-node-icon x-node-check-incomplete";
- } else if (n.checked) {
- checkImg.className = "x-tree-node-icon x-node-check-all";
- } else {
- checkImg.className = "x-tree-node-icon x-node-unchecked";
- }
- bindEventHandler(checkImg, "click", function(tree, node, evt) {
- if ($.isFunction(options.oncheckchanging)) {
- options.oncheckchanging(tree, node);
- }
- if ($(this).hasClass("x-node-unchecked")) {
- $(this).removeClass("x-node-unchecked").addClass("x-node-check-all");
- if (!evt.ctrlKey)
- $(node).find("img.x-node-unchecked").removeClass("x-node-unchecked").addClass("x-node-check-all");
- } else if ($(this).hasClass("x-node-check-all")) {
- $(this).removeClass("x-node-check-all").addClass("x-node-unchecked");
- if (!evt.ctrlKey)
- $(node).find("img.x-node-check-all").removeClass("x-node-check-all").addClass("x-node-unchecked");
- }
-
- if ($.isFunction(options.oncheckchanged)) {
- options.oncheckchanged(tree, node);
- }
- }, this, li);
- div.appendChild(checkImg);
- }
-
- var icon = blankImg();
- icon.className = "x-tree-node-icon";
- if (n.expandedIcon || n.collapsedIcon || n.icon) {
- div.className += " x-tree-node-cusicon";
- icon.src = n.icon;
- icon.expandedIcon = n.expandedIcon;
- icon.collapsedIcon = n.collapsedIcon;
- if (n.expanded && n.expandedIcon) {
- icon.src = n.expandedIcon;
- } else if (n.collapsedIcon) {
- icon.src = n.collapsedIcon;
- }
- li.setAttribute("hasIcon", true);
- } else {
- icon.src = __BLANK_IMAGE;
- if (n.leaf) {
- div.className += " x-tree-node-leaf";
- } else {
- div.className += n.expanded ? " x-tree-node-expanded" : " x-tree-node-collapsed";
- }
- }
- div.appendChild(icon);
-
- var txt = document.createElement("a");
- if (n.href) {
- txt.href = n.href;
- if (n.target) txt.target = n.target;
- }
- txt.className = 'x-tree-node-anchor';
- txt.innerHTML = n.text;
- //txt.appendChild(document.createTextNode(n.text));
-
- if (!noChild) {
- bindEventHandler(handlerIcon, "click", function(tree, node) {tree.toggle(node)}, this, li);
- bindEventHandler(txt, "dblclick", function(tree, node){tree.toggle(node)}, this, li);
- }
- if (n.canSelect) {
- bindEventHandler(txt, "click", function(tree, node) {
- if (tree.selectedNode) {
- tree.selectedNode.children("div").children("a").removeClass("x-tree-selected");
- }
- tree.selectedNode = $(node);
- $(this).addClass("x-tree-selected");
- if (options && $.isFunction(options.onselect)) {
- options.onselect.call(tree.selectedNode, tree);
- }
- }, this, li);
- li.setAttribute("canSelect", "true");
- }
- if (n.operators && options && options.contextmenu) {
- bindEventHandler(txt, "contextmenu", function(tree, node, operators, evt) {
- options.contextmenu.setExtraParams({node:$(li),tree:tree});
- options.contextmenu.show(evt, operators);
- if (tree.selectedNode) {
- tree.selectedNode.children("div").children("a").removeClass("x-tree-selected");
- }
- tree.selectedNode = $(node);
- $(this).addClass("x-tree-selected");
- return false;
- }, this, li, n.operators);
- }
-
- div.appendChild(txt);
-
- var childUl = document.createElement("UL");
- childUl.className = "x-tree-node-ct x-tree-lines";
- if (!n.expanded) childUl.style.display = "none";
-
- li.appendChild(div);
- li.appendChild(childUl);
-
- if (n.nodes != null && n.nodes.length > 0) {
- this.initalize(n.nodes, childUl, parseInt(lev) + 1);
- }
- if (p != null) {
- p.appendChild(li);
- } else {
- rootNode.get(0).appendChild(li);
- }
- if (li.getAttribute("init") == 'n' && recursive) {
- this.asynInitChildNodes(li, null, false, asyn, recursive);
- }
- }
- }
- }
|