/*
 * JQuery zTree excheck 3.0 beta
 * http://code.google.com/p/jquerytree/
 *
 * Copyright (c) 2010 Hunter.z (baby666.cn)
 *
 * Licensed same as jquery - MIT License
 * http://www.opensource.org/licenses/mit-license.php
 *
 * email: hunter.z@263.net
 * Date: 2011-09-01
 */
define(function(require){return function(j){var o,p,q,n={event:{CHECK:"ztree_check"},id:{CHECK:"_check"},checkbox:{STYLE:"checkbox",DEFAULT:"chk",FALSE:"false",TRUE:"true",FULL:"full",PART:"part",FOCUS:"focus"},radio:{STYLE:"radio",TYPE_ALL:"all",TYPE_LEVEL:"level"}},s={check:{enable:!1,chkStyle:n.checkbox.STYLE,radioType:n.radio.TYPE_LEVEL,chkboxType:{Y:"ps",N:"ps"}},data:{key:{checked:"checked"}},callback:{beforeCheck:null,onCheck:null}};o=function(c,a){var b=e.getSetting(c.data.treeId),d=b.data.key.checked;if(m.apply(b.callback.beforeCheck,
[b.treeId,a],!0)==!1)return!0;a[d]=!a[d];g.checkNodeRelation(b,a);d=j("#"+a.tId+i.id.CHECK);g.setChkClass(b,d,a);g.repairParentChkClassWithSelf(b,a);b.treeObj.trigger(i.event.CHECK,[b.treeId,a]);return!0};p=function(c,a){var b=e.getSetting(c.data.treeId),d=j("#"+a.tId+i.id.CHECK);a.check_Focus=!0;g.setChkClass(b,d,a);return!0};q=function(c,a){var b=e.getSetting(c.data.treeId),d=j("#"+a.tId+i.id.CHECK);a.check_Focus=!1;g.setChkClass(b,d,a);return!0};j.extend(!0,j.fn.zTree.consts,n);j.extend(!0,j.fn.zTree._z,
{tools:{},view:{checkNodeRelation:function(c,a){var b,d,h,f=c.data.key.childs,k=c.data.key.checked;b=i.radio;if(c.check.chkStyle==b.STYLE){var l=e.getRadioCheckedList(c);if(a[k])if(c.check.radioType==b.TYPE_ALL){for(d=l.length-1;d>=0;d--)b=l[d],b[k]=!1,l.splice(d,1),g.setChkClass(c,j("#"+b.tId+i.id.CHECK),b),b.parentTId!=a.parentTId&&g.repairParentChkClassWithSelf(c,b);l.push(a)}else{l=a.parentTId?a.getParentNode():e.getRoot(c);for(d=0,h=l[f].length;d<h;d++)b=l[f][d],b[k]&&b!=a&&(b[k]=!1,g.setChkClass(c,
j("#"+b.tId+i.id.CHECK),b))}else if(c.check.radioType==b.TYPE_ALL)for(d=0,h=l.length;d<h;d++)if(a==l[d]){l.splice(d,1);break}}else a[k]&&(!a[f]||a[f].length==0||c.check.chkboxType.Y.indexOf("s")>-1)&&g.setSonNodeCheckBox(c,a,!0),a[k]&&c.check.chkboxType.Y.indexOf("p")>-1&&g.setParentNodeCheckBox(c,a,!0),!a[k]&&(!a[f]||a[f].length==0||c.check.chkboxType.N.indexOf("s")>-1)&&g.setSonNodeCheckBox(c,a,!1),!a[k]&&c.check.chkboxType.N.indexOf("p")>-1&&g.setParentNodeCheckBox(c,a,!1)},makeChkClass:function(c,
a){var b=c.data.key.checked,d=i.checkbox,h="",h=c.check.chkStyle==i.radio.STYLE?a.check_Child_State<1?d.FULL:d.PART:a[b]?a.check_Child_State===2||a.check_Child_State===-1?d.FULL:d.PART:a.check_Child_State<1?d.FULL:d.PART,b=c.check.chkStyle+"_"+(a[b]?d.TRUE:d.FALSE)+"_"+h,b=a.check_Focus?b+"_"+d.FOCUS:b;return d.DEFAULT+" "+b},repairAllChk:function(c,a){if(c.check.enable&&c.check.chkStyle===i.checkbox.STYLE)for(var b=c.data.key.checked,d=c.data.key.childs,h=e.getRoot(c),f=0,k=h[d].length;f<k;f++){var l=
h[d][f];l.nocheck!==!0&&(l[b]=a);g.setSonNodeCheckBox(c,l,a)}},repairChkClass:function(c,a){if(a){e.makeChkFlag(c,a);var b=j("#"+a.tId+i.id.CHECK);g.setChkClass(c,b,a)}},repairParentChkClass:function(c,a){if(a&&a.parentTId){var b=a.getParentNode();g.repairChkClass(c,b);g.repairParentChkClass(c,b)}},repairParentChkClassWithSelf:function(c,a){if(a){var b=c.data.key.childs;a[b]&&a[b].length>0?g.repairParentChkClass(c,a[b][0]):g.repairParentChkClass(c,a)}},setChkClass:function(c,a,b){a&&(b.nocheck===
!0?a.hide():a.show(),a.removeClass(),a.addClass(g.makeChkClass(c,b)))},setParentNodeCheckBox:function(c,a,b){var d=c.data.key.childs,h=c.data.key.checked,f=j("#"+a.tId+i.id.CHECK);e.makeChkFlag(c,a);a.nocheck!==!0&&(a[h]=b,g.setChkClass(c,f,a));if(a.parentTId){f=!0;if(!b)for(var d=a.getParentNode()[d],k=0,l=d.length;k<l;k++)if(d[k].nocheck!==!0&&d[k][h]||d[k].nocheck===!0&&d[k].check_Child_State>0){f=!1;break}f&&g.setParentNodeCheckBox(c,a.getParentNode(),b)}},setSonNodeCheckBox:function(c,a,b){if(a){var d=
c.data.key.childs,h=c.data.key.checked,f=j("#"+a.tId+i.id.CHECK);if(a!=e.getRoot(c))a.nocheck!==!0?(a[h]=b,a.check_Child_State=b?2:0):a.check_Child_State=-1,g.setChkClass(c,f,a);if(a[d]){h=0;for(f=a[d].length;h<f;h++)a[d][h]&&g.setSonNodeCheckBox(c,a[d][h],b)}}}},event:{},data:{getRadioCheckedList:function(c){for(var a=e.getRoot(c).radioCheckedList,b=0,d=a.length;b<d;b++)e.getNodeCache(c,a[b].tId)||(a.splice(b,1),b--,d--);return a},getCheckStatus:function(c,a){if(!c.check.enable||a.nocheck)return null;
var b=c.data.key.checked;return{checked:a[b],half:c.check.chkStyle==i.radio.STYLE?a.check_Child_State===2:a[b]?a.check_Child_State>-1&&a.check_Child_State<2:a.check_Child_State>0}},getTreeCheckedNodes:function(c,a,b,d){if(!a)return[];for(var h=c.data.key.childs,f=c.data.key.checked,d=!d?[]:d,k=0,g=a.length;k<g;k++)a[k].nocheck!==!0&&a[k][f]==b&&d.push(a[k]),e.getTreeCheckedNodes(c,a[k][h],b,d);return d},getTreeChangeCheckedNodes:function(c,a,b){if(!a)return[];for(var d=c.data.key.childs,h=c.data.key.checked,
b=!b?[]:b,f=0,g=a.length;f<g;f++)a[f].nocheck!==!0&&a[f][h]!=a[f].checkedOld&&b.push(a[f]),e.getTreeChangeCheckedNodes(c,a[f][d],b);return b},makeChkFlag:function(c,a){if(a){var b=c.data.key.childs,d=c.data.key.checked,h=-1;if(a[b])for(var f=0,g=a[b].length;f<g;f++){var e=a[b][f],j=-1;if(c.check.chkStyle==i.radio.STYLE)if(j=e.nocheck===!0?e.check_Child_State:e.nocheck!==!0&&e[d]?2:e.check_Child_State>0?2:0,j==2){h=2;break}else j==0&&(h=0);else if(c.check.chkStyle==i.checkbox.STYLE)if(j=e.nocheck===
!0?e.check_Child_State:e.nocheck!==!0&&e[d]?e.check_Child_State===-1||e.check_Child_State===2?2:1:e.check_Child_State>0?1:0,j===1){h=1;break}else if(j===2&&f>0&&j!==h){h=1;break}else if(h===2&&j>-1&&j<2){h=1;break}else j>-1&&(h=j)}a.check_Child_State=h}}}});var n=j.fn.zTree,m=n._z.tools,i=n.consts,g=n._z.view,e=n._z.data;e.exSetting(s);e.addInitBind(function(c){var a=c.treeObj,b=i.event;a.unbind(b.CHECK);a.bind(b.CHECK,function(a,b,f){m.apply(c.callback.onCheck,[a,b,f])})});e.addInitCache(function(){});
e.addInitNode(function(c,a,b){if(b)a=c.data.key.checked,typeof b[a]=="string"&&(b[a]=m.eqs(b[a],"true")),b[a]=!!b[a],b.checkedOld=b[a],b.nocheck=!!b.nocheck,b.check_Child_State=-1,b.check_Focus=!1,b.getCheckStatus=function(){return e.getCheckStatus(c,b)}});e.addInitProxy(function(c){var a=c.target,b=e.getSetting(c.data.treeId),d="",h=null,f="",g=null;if(m.eqs(c.type,"mouseover")){if(b.check.enable&&m.eqs(a.tagName,"button")&&a.getAttribute("treeNode"+i.id.CHECK)!==null)d=a.parentNode.id,f="mouseoverCheck"}else if(m.eqs(c.type,
"mouseout")){if(b.check.enable&&m.eqs(a.tagName,"button")&&a.getAttribute("treeNode"+i.id.CHECK)!==null)d=a.parentNode.id,f="mouseoutCheck"}else if(m.eqs(c.type,"click")&&b.check.enable&&m.eqs(a.tagName,"button")&&a.getAttribute("treeNode"+i.id.CHECK)!==null)d=a.parentNode.id,f="checkNode";if(d.length>0)switch(h=e.getNodeCache(b,d),f){case "checkNode":g=o;break;case "mouseoverCheck":g=p;break;case "mouseoutCheck":g=q}return{stop:!1,node:h,nodeEventType:f,nodeEventCallback:g,treeEventType:"",treeEventCallback:null}});
e.addInitRoot(function(c){e.getRoot(c).radioCheckedList=[]});e.addBeforeA(function(c,a,b){var d=c.data.key.checked;c.check.enable&&(e.makeChkFlag(c,a),c.check.chkStyle==i.radio.STYLE&&c.check.radioType==i.radio.TYPE_ALL&&a[d]&&e.getRoot(c).radioCheckedList.push(a),b.push("<button type='button' ID='",a.tId,i.id.CHECK,"' class='",g.makeChkClass(c,a),"' treeNode",i.id.CHECK," onfocus='this.blur();' ",a.nocheck===!0?"style='display:none;'":"","></button>"))});e.addZTreeTools(function(c,a){a.checkNode=
function(a,b,f){var e=this.setting.data.key.checked;if((a[e]!==!!b||f)&&m.apply(this.setting.callback.beforeCheck,[this.setting.treeId,a],!0)!=!1&&m.uCanDo(this.setting)&&this.setting.check.enable&&a.nocheck!==!0)a[e]=!!b,b=j("#"+a.tId+i.id.CHECK),(f||this.setting.check.chkStyle===i.radio.STYLE)&&g.checkNodeRelation(this.setting,a),g.setChkClass(this.setting,b,a),g.repairParentChkClassWithSelf(this.setting,a),c.treeObj.trigger(i.event.CHECK,[c.treeId,a])};a.checkAllNodes=function(a){g.repairAllChk(this.setting,
!!a)};a.getCheckedNodes=function(a){var b=this.setting.data.key.childs;return e.getTreeCheckedNodes(this.setting,e.getRoot(c)[b],a!==!1)};a.getChangeCheckedNodes=function(){var a=this.setting.data.key.childs;return e.getTreeChangeCheckedNodes(this.setting,e.getRoot(c)[a])};var b=a.updateNode;a.updateNode=function(c,e){b&&b.apply(a,arguments);if(c&&this.setting.check.enable&&j("#"+c.tId).get(0)&&m.uCanDo(this.setting)){var f=j("#"+c.tId+i.id.CHECK);(e==!0||this.setting.check.chkStyle===i.radio.STYLE)&&
g.checkNodeRelation(this.setting,c);g.setChkClass(this.setting,f,c);g.repairParentChkClassWithSelf(this.setting,c)}}});var r=g.createNodes;g.createNodes=function(c,a,b,d){r&&r.apply(g,arguments);b&&g.repairParentChkClassWithSelf(c,d)}}});
