jquery.ztree.all-3.5.js 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  1. /*
  2. * JQuery zTree core v3.5.16-beta.1
  3. * http://zTree.me/
  4. *
  5. * Copyright (c) 2010 Hunter.z
  6. *
  7. * Licensed same as jquery - MIT License
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * email: hunter.z@263.net
  11. * Date: 2014-03-28
  12. */
  13. (function($){
  14. var settings = {}, roots = {}, caches = {},
  15. //default consts of core
  16. _consts = {
  17. className: {
  18. BUTTON: "button",
  19. LEVEL: "level",
  20. ICO_LOADING: "ico_loading",
  21. SWITCH: "switch"
  22. },
  23. event: {
  24. NODECREATED: "ztree_nodeCreated",
  25. CLICK: "ztree_click",
  26. EXPAND: "ztree_expand",
  27. COLLAPSE: "ztree_collapse",
  28. ASYNC_SUCCESS: "ztree_async_success",
  29. ASYNC_ERROR: "ztree_async_error",
  30. REMOVE: "ztree_remove"
  31. },
  32. id: {
  33. A: "_a",
  34. ICON: "_ico",
  35. SPAN: "_span",
  36. SWITCH: "_switch",
  37. UL: "_ul"
  38. },
  39. line: {
  40. ROOT: "root",
  41. ROOTS: "roots",
  42. CENTER: "center",
  43. BOTTOM: "bottom",
  44. NOLINE: "noline",
  45. LINE: "line"
  46. },
  47. folder: {
  48. OPEN: "open",
  49. CLOSE: "close",
  50. DOCU: "docu"
  51. },
  52. node: {
  53. CURSELECTED: "curSelectedNode"
  54. }
  55. },
  56. //default setting of core
  57. _setting = {
  58. treeId: "",
  59. treeObj: null,
  60. view: {
  61. addDiyDom: null,
  62. autoCancelSelected: true,
  63. dblClickExpand: true,
  64. expandSpeed: "fast",
  65. fontCss: {},
  66. nameIsHTML: false,
  67. selectedMulti: true,
  68. showIcon: true,
  69. showLine: true,
  70. showTitle: true,
  71. txtSelectedEnable: false
  72. },
  73. data: {
  74. key: {
  75. children: "children",
  76. name: "name",
  77. title: "",
  78. url: "url"
  79. },
  80. simpleData: {
  81. enable: false,
  82. idKey: "id",
  83. pIdKey: "pId",
  84. rootPId: null
  85. },
  86. keep: {
  87. parent: false,
  88. leaf: false
  89. }
  90. },
  91. async: {
  92. enable: false,
  93. contentType: "application/x-www-form-urlencoded",
  94. type: "post",
  95. dataType: "text",
  96. url: "",
  97. autoParam: [],
  98. otherParam: [],
  99. dataFilter: null
  100. },
  101. callback: {
  102. beforeAsync:null,
  103. beforeClick:null,
  104. beforeDblClick:null,
  105. beforeRightClick:null,
  106. beforeMouseDown:null,
  107. beforeMouseUp:null,
  108. beforeExpand:null,
  109. beforeCollapse:null,
  110. beforeRemove:null,
  111. onAsyncError:null,
  112. onAsyncSuccess:null,
  113. onNodeCreated:null,
  114. onClick:null,
  115. onDblClick:null,
  116. onRightClick:null,
  117. onMouseDown:null,
  118. onMouseUp:null,
  119. onExpand:null,
  120. onCollapse:null,
  121. onRemove:null
  122. }
  123. },
  124. //default root of core
  125. //zTree use root to save full data
  126. _initRoot = function (setting) {
  127. var r = data.getRoot(setting);
  128. if (!r) {
  129. r = {};
  130. data.setRoot(setting, r);
  131. }
  132. r[setting.data.key.children] = [];
  133. r.expandTriggerFlag = false;
  134. r.curSelectedList = [];
  135. r.noSelection = true;
  136. r.createdNodes = [];
  137. r.zId = 0;
  138. r._ver = (new Date()).getTime();
  139. },
  140. //default cache of core
  141. _initCache = function(setting) {
  142. var c = data.getCache(setting);
  143. if (!c) {
  144. c = {};
  145. data.setCache(setting, c);
  146. }
  147. c.nodes = [];
  148. c.doms = [];
  149. },
  150. //default bindEvent of core
  151. _bindEvent = function(setting) {
  152. var o = setting.treeObj,
  153. c = consts.event;
  154. o.bind(c.NODECREATED, function (event, treeId, node) {
  155. tools.apply(setting.callback.onNodeCreated, [event, treeId, node]);
  156. });
  157. o.bind(c.CLICK, function (event, srcEvent, treeId, node, clickFlag) {
  158. tools.apply(setting.callback.onClick, [srcEvent, treeId, node, clickFlag]);
  159. });
  160. o.bind(c.EXPAND, function (event, treeId, node) {
  161. tools.apply(setting.callback.onExpand, [event, treeId, node]);
  162. });
  163. o.bind(c.COLLAPSE, function (event, treeId, node) {
  164. tools.apply(setting.callback.onCollapse, [event, treeId, node]);
  165. });
  166. o.bind(c.ASYNC_SUCCESS, function (event, treeId, node, msg) {
  167. tools.apply(setting.callback.onAsyncSuccess, [event, treeId, node, msg]);
  168. });
  169. o.bind(c.ASYNC_ERROR, function (event, treeId, node, XMLHttpRequest, textStatus, errorThrown) {
  170. tools.apply(setting.callback.onAsyncError, [event, treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  171. });
  172. o.bind(c.REMOVE, function (event, treeId, treeNode) {
  173. tools.apply(setting.callback.onRemove, [event, treeId, treeNode]);
  174. });
  175. },
  176. _unbindEvent = function(setting) {
  177. var o = setting.treeObj,
  178. c = consts.event;
  179. o.unbind(c.NODECREATED)
  180. .unbind(c.CLICK)
  181. .unbind(c.EXPAND)
  182. .unbind(c.COLLAPSE)
  183. .unbind(c.ASYNC_SUCCESS)
  184. .unbind(c.ASYNC_ERROR)
  185. .unbind(c.REMOVE);
  186. },
  187. //default event proxy of core
  188. _eventProxy = function(event) {
  189. var target = event.target,
  190. setting = data.getSetting(event.data.treeId),
  191. tId = "", node = null,
  192. nodeEventType = "", treeEventType = "",
  193. nodeEventCallback = null, treeEventCallback = null,
  194. tmp = null;
  195. if (tools.eqs(event.type, "mousedown")) {
  196. treeEventType = "mousedown";
  197. } else if (tools.eqs(event.type, "mouseup")) {
  198. treeEventType = "mouseup";
  199. } else if (tools.eqs(event.type, "contextmenu")) {
  200. treeEventType = "contextmenu";
  201. } else if (tools.eqs(event.type, "click")) {
  202. if (tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.SWITCH) !== null) {
  203. tId = tools.getNodeMainDom(target).id;
  204. nodeEventType = "switchNode";
  205. } else {
  206. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  207. if (tmp) {
  208. tId = tools.getNodeMainDom(tmp).id;
  209. nodeEventType = "clickNode";
  210. }
  211. }
  212. } else if (tools.eqs(event.type, "dblclick")) {
  213. treeEventType = "dblclick";
  214. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  215. if (tmp) {
  216. tId = tools.getNodeMainDom(tmp).id;
  217. nodeEventType = "switchNode";
  218. }
  219. }
  220. if (treeEventType.length > 0 && tId.length == 0) {
  221. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  222. if (tmp) {tId = tools.getNodeMainDom(tmp).id;}
  223. }
  224. // event to node
  225. if (tId.length>0) {
  226. node = data.getNodeCache(setting, tId);
  227. switch (nodeEventType) {
  228. case "switchNode" :
  229. if (!node.isParent) {
  230. nodeEventType = "";
  231. } else if (tools.eqs(event.type, "click")
  232. || (tools.eqs(event.type, "dblclick") && tools.apply(setting.view.dblClickExpand, [setting.treeId, node], setting.view.dblClickExpand))) {
  233. nodeEventCallback = handler.onSwitchNode;
  234. } else {
  235. nodeEventType = "";
  236. }
  237. break;
  238. case "clickNode" :
  239. nodeEventCallback = handler.onClickNode;
  240. break;
  241. }
  242. }
  243. // event to zTree
  244. switch (treeEventType) {
  245. case "mousedown" :
  246. treeEventCallback = handler.onZTreeMousedown;
  247. break;
  248. case "mouseup" :
  249. treeEventCallback = handler.onZTreeMouseup;
  250. break;
  251. case "dblclick" :
  252. treeEventCallback = handler.onZTreeDblclick;
  253. break;
  254. case "contextmenu" :
  255. treeEventCallback = handler.onZTreeContextmenu;
  256. break;
  257. }
  258. var proxyResult = {
  259. stop: false,
  260. node: node,
  261. nodeEventType: nodeEventType,
  262. nodeEventCallback: nodeEventCallback,
  263. treeEventType: treeEventType,
  264. treeEventCallback: treeEventCallback
  265. };
  266. return proxyResult
  267. },
  268. //default init node of core
  269. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  270. if (!n) return;
  271. var r = data.getRoot(setting),
  272. childKey = setting.data.key.children;
  273. n.level = level;
  274. n.tId = setting.treeId + "_" + (++r.zId);
  275. n.parentTId = parentNode ? parentNode.tId : null;
  276. n.open = (typeof n.open == "string") ? tools.eqs(n.open, "true") : !!n.open;
  277. if (n[childKey] && n[childKey].length > 0) {
  278. n.isParent = true;
  279. n.zAsync = true;
  280. } else {
  281. n.isParent = (typeof n.isParent == "string") ? tools.eqs(n.isParent, "true") : !!n.isParent;
  282. n.open = (n.isParent && !setting.async.enable) ? n.open : false;
  283. n.zAsync = !n.isParent;
  284. }
  285. n.isFirstNode = isFirstNode;
  286. n.isLastNode = isLastNode;
  287. n.getParentNode = function() {return data.getNodeCache(setting, n.parentTId);};
  288. n.getPreNode = function() {return data.getPreNode(setting, n);};
  289. n.getNextNode = function() {return data.getNextNode(setting, n);};
  290. n.isAjaxing = false;
  291. data.fixPIdKeyValue(setting, n);
  292. },
  293. _init = {
  294. bind: [_bindEvent],
  295. unbind: [_unbindEvent],
  296. caches: [_initCache],
  297. nodes: [_initNode],
  298. proxys: [_eventProxy],
  299. roots: [_initRoot],
  300. beforeA: [],
  301. afterA: [],
  302. innerBeforeA: [],
  303. innerAfterA: [],
  304. zTreeTools: []
  305. },
  306. //method of operate data
  307. data = {
  308. addNodeCache: function(setting, node) {
  309. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = node;
  310. },
  311. getNodeCacheId: function(tId) {
  312. return tId.substring(tId.lastIndexOf("_")+1);
  313. },
  314. addAfterA: function(afterA) {
  315. _init.afterA.push(afterA);
  316. },
  317. addBeforeA: function(beforeA) {
  318. _init.beforeA.push(beforeA);
  319. },
  320. addInnerAfterA: function(innerAfterA) {
  321. _init.innerAfterA.push(innerAfterA);
  322. },
  323. addInnerBeforeA: function(innerBeforeA) {
  324. _init.innerBeforeA.push(innerBeforeA);
  325. },
  326. addInitBind: function(bindEvent) {
  327. _init.bind.push(bindEvent);
  328. },
  329. addInitUnBind: function(unbindEvent) {
  330. _init.unbind.push(unbindEvent);
  331. },
  332. addInitCache: function(initCache) {
  333. _init.caches.push(initCache);
  334. },
  335. addInitNode: function(initNode) {
  336. _init.nodes.push(initNode);
  337. },
  338. addInitProxy: function(initProxy, isFirst) {
  339. if (!!isFirst) {
  340. _init.proxys.splice(0,0,initProxy);
  341. } else {
  342. _init.proxys.push(initProxy);
  343. }
  344. },
  345. addInitRoot: function(initRoot) {
  346. _init.roots.push(initRoot);
  347. },
  348. addNodesData: function(setting, parentNode, nodes) {
  349. var childKey = setting.data.key.children;
  350. if (!parentNode[childKey]) parentNode[childKey] = [];
  351. if (parentNode[childKey].length > 0) {
  352. parentNode[childKey][parentNode[childKey].length - 1].isLastNode = false;
  353. view.setNodeLineIcos(setting, parentNode[childKey][parentNode[childKey].length - 1]);
  354. }
  355. parentNode.isParent = true;
  356. parentNode[childKey] = parentNode[childKey].concat(nodes);
  357. },
  358. addSelectedNode: function(setting, node) {
  359. var root = data.getRoot(setting);
  360. if (!data.isSelectedNode(setting, node)) {
  361. root.curSelectedList.push(node);
  362. }
  363. },
  364. addCreatedNode: function(setting, node) {
  365. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  366. var root = data.getRoot(setting);
  367. root.createdNodes.push(node);
  368. }
  369. },
  370. addZTreeTools: function(zTreeTools) {
  371. _init.zTreeTools.push(zTreeTools);
  372. },
  373. exSetting: function(s) {
  374. $.extend(true, _setting, s);
  375. },
  376. fixPIdKeyValue: function(setting, node) {
  377. if (setting.data.simpleData.enable) {
  378. node[setting.data.simpleData.pIdKey] = node.parentTId ? node.getParentNode()[setting.data.simpleData.idKey] : setting.data.simpleData.rootPId;
  379. }
  380. },
  381. getAfterA: function(setting, node, array) {
  382. for (var i=0, j=_init.afterA.length; i<j; i++) {
  383. _init.afterA[i].apply(this, arguments);
  384. }
  385. },
  386. getBeforeA: function(setting, node, array) {
  387. for (var i=0, j=_init.beforeA.length; i<j; i++) {
  388. _init.beforeA[i].apply(this, arguments);
  389. }
  390. },
  391. getInnerAfterA: function(setting, node, array) {
  392. for (var i=0, j=_init.innerAfterA.length; i<j; i++) {
  393. _init.innerAfterA[i].apply(this, arguments);
  394. }
  395. },
  396. getInnerBeforeA: function(setting, node, array) {
  397. for (var i=0, j=_init.innerBeforeA.length; i<j; i++) {
  398. _init.innerBeforeA[i].apply(this, arguments);
  399. }
  400. },
  401. getCache: function(setting) {
  402. return caches[setting.treeId];
  403. },
  404. getNextNode: function(setting, node) {
  405. if (!node) return null;
  406. var childKey = setting.data.key.children,
  407. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  408. for (var i=0, l=p[childKey].length-1; i<=l; i++) {
  409. if (p[childKey][i] === node) {
  410. return (i==l ? null : p[childKey][i+1]);
  411. }
  412. }
  413. return null;
  414. },
  415. getNodeByParam: function(setting, nodes, key, value) {
  416. if (!nodes || !key) return null;
  417. var childKey = setting.data.key.children;
  418. for (var i = 0, l = nodes.length; i < l; i++) {
  419. if (nodes[i][key] == value) {
  420. return nodes[i];
  421. }
  422. var tmp = data.getNodeByParam(setting, nodes[i][childKey], key, value);
  423. if (tmp) return tmp;
  424. }
  425. return null;
  426. },
  427. getNodeCache: function(setting, tId) {
  428. if (!tId) return null;
  429. var n = caches[setting.treeId].nodes[data.getNodeCacheId(tId)];
  430. return n ? n : null;
  431. },
  432. getNodeName: function(setting, node) {
  433. var nameKey = setting.data.key.name;
  434. return "" + node[nameKey];
  435. },
  436. getNodeTitle: function(setting, node) {
  437. var t = setting.data.key.title === "" ? setting.data.key.name : setting.data.key.title;
  438. return "" + node[t];
  439. },
  440. getNodes: function(setting) {
  441. return data.getRoot(setting)[setting.data.key.children];
  442. },
  443. getNodesByParam: function(setting, nodes, key, value) {
  444. if (!nodes || !key) return [];
  445. var childKey = setting.data.key.children,
  446. result = [];
  447. for (var i = 0, l = nodes.length; i < l; i++) {
  448. if (nodes[i][key] == value) {
  449. result.push(nodes[i]);
  450. }
  451. result = result.concat(data.getNodesByParam(setting, nodes[i][childKey], key, value));
  452. }
  453. return result;
  454. },
  455. getNodesByParamFuzzy: function(setting, nodes, key, value) {
  456. if (!nodes || !key) return [];
  457. var childKey = setting.data.key.children,
  458. result = [];
  459. value = value.toLowerCase();
  460. for (var i = 0, l = nodes.length; i < l; i++) {
  461. if (typeof nodes[i][key] == "string" && nodes[i][key].toLowerCase().indexOf(value)>-1) {
  462. result.push(nodes[i]);
  463. }
  464. result = result.concat(data.getNodesByParamFuzzy(setting, nodes[i][childKey], key, value));
  465. }
  466. return result;
  467. },
  468. getNodesByFilter: function(setting, nodes, filter, isSingle, invokeParam) {
  469. if (!nodes) return (isSingle ? null : []);
  470. var childKey = setting.data.key.children,
  471. result = isSingle ? null : [];
  472. for (var i = 0, l = nodes.length; i < l; i++) {
  473. if (tools.apply(filter, [nodes[i], invokeParam], false)) {
  474. if (isSingle) {return nodes[i];}
  475. result.push(nodes[i]);
  476. }
  477. var tmpResult = data.getNodesByFilter(setting, nodes[i][childKey], filter, isSingle, invokeParam);
  478. if (isSingle && !!tmpResult) {return tmpResult;}
  479. result = isSingle ? tmpResult : result.concat(tmpResult);
  480. }
  481. return result;
  482. },
  483. getPreNode: function(setting, node) {
  484. if (!node) return null;
  485. var childKey = setting.data.key.children,
  486. p = node.parentTId ? node.getParentNode() : data.getRoot(setting);
  487. for (var i=0, l=p[childKey].length; i<l; i++) {
  488. if (p[childKey][i] === node) {
  489. return (i==0 ? null : p[childKey][i-1]);
  490. }
  491. }
  492. return null;
  493. },
  494. getRoot: function(setting) {
  495. return setting ? roots[setting.treeId] : null;
  496. },
  497. getRoots: function() {
  498. return roots;
  499. },
  500. getSetting: function(treeId) {
  501. return settings[treeId];
  502. },
  503. getSettings: function() {
  504. return settings;
  505. },
  506. getZTreeTools: function(treeId) {
  507. var r = this.getRoot(this.getSetting(treeId));
  508. return r ? r.treeTools : null;
  509. },
  510. initCache: function(setting) {
  511. for (var i=0, j=_init.caches.length; i<j; i++) {
  512. _init.caches[i].apply(this, arguments);
  513. }
  514. },
  515. initNode: function(setting, level, node, parentNode, preNode, nextNode) {
  516. for (var i=0, j=_init.nodes.length; i<j; i++) {
  517. _init.nodes[i].apply(this, arguments);
  518. }
  519. },
  520. initRoot: function(setting) {
  521. for (var i=0, j=_init.roots.length; i<j; i++) {
  522. _init.roots[i].apply(this, arguments);
  523. }
  524. },
  525. isSelectedNode: function(setting, node) {
  526. var root = data.getRoot(setting);
  527. for (var i=0, j=root.curSelectedList.length; i<j; i++) {
  528. if(node === root.curSelectedList[i]) return true;
  529. }
  530. return false;
  531. },
  532. removeNodeCache: function(setting, node) {
  533. var childKey = setting.data.key.children;
  534. if (node[childKey]) {
  535. for (var i=0, l=node[childKey].length; i<l; i++) {
  536. arguments.callee(setting, node[childKey][i]);
  537. }
  538. }
  539. data.getCache(setting).nodes[data.getNodeCacheId(node.tId)] = null;
  540. },
  541. removeSelectedNode: function(setting, node) {
  542. var root = data.getRoot(setting);
  543. for (var i=0, j=root.curSelectedList.length; i<j; i++) {
  544. if(node === root.curSelectedList[i] || !data.getNodeCache(setting, root.curSelectedList[i].tId)) {
  545. root.curSelectedList.splice(i, 1);
  546. i--;j--;
  547. }
  548. }
  549. },
  550. setCache: function(setting, cache) {
  551. caches[setting.treeId] = cache;
  552. },
  553. setRoot: function(setting, root) {
  554. roots[setting.treeId] = root;
  555. },
  556. setZTreeTools: function(setting, zTreeTools) {
  557. for (var i=0, j=_init.zTreeTools.length; i<j; i++) {
  558. _init.zTreeTools[i].apply(this, arguments);
  559. }
  560. },
  561. transformToArrayFormat: function (setting, nodes) {
  562. if (!nodes) return [];
  563. var childKey = setting.data.key.children,
  564. r = [];
  565. if (tools.isArray(nodes)) {
  566. for (var i=0, l=nodes.length; i<l; i++) {
  567. r.push(nodes[i]);
  568. if (nodes[i][childKey])
  569. r = r.concat(data.transformToArrayFormat(setting, nodes[i][childKey]));
  570. }
  571. } else {
  572. r.push(nodes);
  573. if (nodes[childKey])
  574. r = r.concat(data.transformToArrayFormat(setting, nodes[childKey]));
  575. }
  576. return r;
  577. },
  578. transformTozTreeFormat: function(setting, sNodes) {
  579. var i,l,
  580. key = setting.data.simpleData.idKey,
  581. parentKey = setting.data.simpleData.pIdKey,
  582. childKey = setting.data.key.children;
  583. if (!key || key=="" || !sNodes) return [];
  584. if (tools.isArray(sNodes)) {
  585. var r = [];
  586. var tmpMap = [];
  587. for (i=0, l=sNodes.length; i<l; i++) {
  588. tmpMap[sNodes[i][key]] = sNodes[i];
  589. }
  590. for (i=0, l=sNodes.length; i<l; i++) {
  591. if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
  592. if (!tmpMap[sNodes[i][parentKey]][childKey])
  593. tmpMap[sNodes[i][parentKey]][childKey] = [];
  594. tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
  595. } else {
  596. r.push(sNodes[i]);
  597. }
  598. }
  599. return r;
  600. }else {
  601. return [sNodes];
  602. }
  603. }
  604. },
  605. //method of event proxy
  606. event = {
  607. bindEvent: function(setting) {
  608. for (var i=0, j=_init.bind.length; i<j; i++) {
  609. _init.bind[i].apply(this, arguments);
  610. }
  611. },
  612. unbindEvent: function(setting) {
  613. for (var i=0, j=_init.unbind.length; i<j; i++) {
  614. _init.unbind[i].apply(this, arguments);
  615. }
  616. },
  617. bindTree: function(setting) {
  618. var eventParam = {
  619. treeId: setting.treeId
  620. },
  621. o = setting.treeObj;
  622. if (!setting.view.txtSelectedEnable) {
  623. // for can't select text
  624. o.bind('selectstart', function(e){
  625. var node
  626. var n = e.originalEvent.srcElement.nodeName.toLowerCase();
  627. return (n === "input" || n === "textarea" );
  628. }).css({
  629. "-moz-user-select":"-moz-none"
  630. });
  631. }
  632. o.bind('click', eventParam, event.proxy);
  633. o.bind('dblclick', eventParam, event.proxy);
  634. o.bind('mouseover', eventParam, event.proxy);
  635. o.bind('mouseout', eventParam, event.proxy);
  636. o.bind('mousedown', eventParam, event.proxy);
  637. o.bind('mouseup', eventParam, event.proxy);
  638. o.bind('contextmenu', eventParam, event.proxy);
  639. },
  640. unbindTree: function(setting) {
  641. var o = setting.treeObj;
  642. o.unbind('click', event.proxy)
  643. .unbind('dblclick', event.proxy)
  644. .unbind('mouseover', event.proxy)
  645. .unbind('mouseout', event.proxy)
  646. .unbind('mousedown', event.proxy)
  647. .unbind('mouseup', event.proxy)
  648. .unbind('contextmenu', event.proxy);
  649. },
  650. doProxy: function(e) {
  651. var results = [];
  652. for (var i=0, j=_init.proxys.length; i<j; i++) {
  653. var proxyResult = _init.proxys[i].apply(this, arguments);
  654. results.push(proxyResult);
  655. if (proxyResult.stop) {
  656. break;
  657. }
  658. }
  659. return results;
  660. },
  661. proxy: function(e) {
  662. var setting = data.getSetting(e.data.treeId);
  663. if (!tools.uCanDo(setting, e)) return true;
  664. var results = event.doProxy(e),
  665. r = true, x = false;
  666. for (var i=0, l=results.length; i<l; i++) {
  667. var proxyResult = results[i];
  668. if (proxyResult.nodeEventCallback) {
  669. x = true;
  670. r = proxyResult.nodeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  671. }
  672. if (proxyResult.treeEventCallback) {
  673. x = true;
  674. r = proxyResult.treeEventCallback.apply(proxyResult, [e, proxyResult.node]) && r;
  675. }
  676. }
  677. return r;
  678. }
  679. },
  680. //method of event handler
  681. handler = {
  682. onSwitchNode: function (event, node) {
  683. var setting = data.getSetting(event.data.treeId);
  684. if (node.open) {
  685. if (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false) return true;
  686. data.getRoot(setting).expandTriggerFlag = true;
  687. view.switchNode(setting, node);
  688. } else {
  689. if (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false) return true;
  690. data.getRoot(setting).expandTriggerFlag = true;
  691. view.switchNode(setting, node);
  692. }
  693. return true;
  694. },
  695. onClickNode: function (event, node) {
  696. var setting = data.getSetting(event.data.treeId),
  697. clickFlag = ( (setting.view.autoCancelSelected && (event.ctrlKey || event.metaKey)) && data.isSelectedNode(setting, node)) ? 0 : (setting.view.autoCancelSelected && (event.ctrlKey || event.metaKey) && setting.view.selectedMulti) ? 2 : 1;
  698. if (tools.apply(setting.callback.beforeClick, [setting.treeId, node, clickFlag], true) == false) return true;
  699. if (clickFlag === 0) {
  700. view.cancelPreSelectedNode(setting, node);
  701. } else {
  702. view.selectNode(setting, node, clickFlag === 2);
  703. }
  704. setting.treeObj.trigger(consts.event.CLICK, [event, setting.treeId, node, clickFlag]);
  705. return true;
  706. },
  707. onZTreeMousedown: function(event, node) {
  708. var setting = data.getSetting(event.data.treeId);
  709. if (tools.apply(setting.callback.beforeMouseDown, [setting.treeId, node], true)) {
  710. tools.apply(setting.callback.onMouseDown, [event, setting.treeId, node]);
  711. }
  712. return true;
  713. },
  714. onZTreeMouseup: function(event, node) {
  715. var setting = data.getSetting(event.data.treeId);
  716. if (tools.apply(setting.callback.beforeMouseUp, [setting.treeId, node], true)) {
  717. tools.apply(setting.callback.onMouseUp, [event, setting.treeId, node]);
  718. }
  719. return true;
  720. },
  721. onZTreeDblclick: function(event, node) {
  722. var setting = data.getSetting(event.data.treeId);
  723. if (tools.apply(setting.callback.beforeDblClick, [setting.treeId, node], true)) {
  724. tools.apply(setting.callback.onDblClick, [event, setting.treeId, node]);
  725. }
  726. return true;
  727. },
  728. onZTreeContextmenu: function(event, node) {
  729. var setting = data.getSetting(event.data.treeId);
  730. if (tools.apply(setting.callback.beforeRightClick, [setting.treeId, node], true)) {
  731. tools.apply(setting.callback.onRightClick, [event, setting.treeId, node]);
  732. }
  733. return (typeof setting.callback.onRightClick) != "function";
  734. }
  735. },
  736. //method of tools for zTree
  737. tools = {
  738. apply: function(fun, param, defaultValue) {
  739. if ((typeof fun) == "function") {
  740. return fun.apply(zt, param?param:[]);
  741. }
  742. return defaultValue;
  743. },
  744. canAsync: function(setting, node) {
  745. var childKey = setting.data.key.children;
  746. return (setting.async.enable && node && node.isParent && !(node.zAsync || (node[childKey] && node[childKey].length > 0)));
  747. },
  748. clone: function (obj){
  749. if (obj === null) return null;
  750. var o = tools.isArray(obj) ? [] : {};
  751. for(var i in obj){
  752. o[i] = (obj[i] instanceof Date) ? new Date(obj[i].getTime()) : (typeof obj[i] === "object" ? arguments.callee(obj[i]) : obj[i]);
  753. }
  754. return o;
  755. },
  756. eqs: function(str1, str2) {
  757. return str1.toLowerCase() === str2.toLowerCase();
  758. },
  759. isArray: function(arr) {
  760. return Object.prototype.toString.apply(arr) === "[object Array]";
  761. },
  762. $: function(node, exp, setting) {
  763. if (!!exp && typeof exp != "string") {
  764. setting = exp;
  765. exp = "";
  766. }
  767. if (typeof node == "string") {
  768. return $(node, setting ? setting.treeObj.get(0).ownerDocument : null);
  769. } else {
  770. return $("#" + node.tId + exp, setting ? setting.treeObj : null);
  771. }
  772. },
  773. getMDom: function (setting, curDom, targetExpr) {
  774. if (!curDom) return null;
  775. while (curDom && curDom.id !== setting.treeId) {
  776. for (var i=0, l=targetExpr.length; curDom.tagName && i<l; i++) {
  777. if (tools.eqs(curDom.tagName, targetExpr[i].tagName) && curDom.getAttribute(targetExpr[i].attrName) !== null) {
  778. return curDom;
  779. }
  780. }
  781. curDom = curDom.parentNode;
  782. }
  783. return null;
  784. },
  785. getNodeMainDom:function(target) {
  786. return ($(target).parent("li").get(0) || $(target).parentsUntil("li").parent().get(0));
  787. },
  788. isChildOrSelf: function(dom, parentId) {
  789. return ( $(dom).closest("#" + parentId).length> 0 );
  790. },
  791. uCanDo: function(setting, e) {
  792. return true;
  793. }
  794. },
  795. //method of operate ztree dom
  796. view = {
  797. addNodes: function(setting, parentNode, newNodes, isSilent) {
  798. if (setting.data.keep.leaf && parentNode && !parentNode.isParent) {
  799. return;
  800. }
  801. if (!tools.isArray(newNodes)) {
  802. newNodes = [newNodes];
  803. }
  804. if (setting.data.simpleData.enable) {
  805. newNodes = data.transformTozTreeFormat(setting, newNodes);
  806. }
  807. if (parentNode) {
  808. var target_switchObj = $$(parentNode, consts.id.SWITCH, setting),
  809. target_icoObj = $$(parentNode, consts.id.ICON, setting),
  810. target_ulObj = $$(parentNode, consts.id.UL, setting);
  811. if (!parentNode.open) {
  812. view.replaceSwitchClass(parentNode, target_switchObj, consts.folder.CLOSE);
  813. view.replaceIcoClass(parentNode, target_icoObj, consts.folder.CLOSE);
  814. parentNode.open = false;
  815. target_ulObj.css({
  816. "display": "none"
  817. });
  818. }
  819. data.addNodesData(setting, parentNode, newNodes);
  820. view.createNodes(setting, parentNode.level + 1, newNodes, parentNode);
  821. if (!isSilent) {
  822. view.expandCollapseParentNode(setting, parentNode, true);
  823. }
  824. } else {
  825. data.addNodesData(setting, data.getRoot(setting), newNodes);
  826. view.createNodes(setting, 0, newNodes, null);
  827. }
  828. },
  829. appendNodes: function(setting, level, nodes, parentNode, initFlag, openFlag) {
  830. if (!nodes) return [];
  831. var html = [],
  832. childKey = setting.data.key.children;
  833. for (var i = 0, l = nodes.length; i < l; i++) {
  834. var node = nodes[i];
  835. if (initFlag) {
  836. var tmpPNode = (parentNode) ? parentNode: data.getRoot(setting),
  837. tmpPChild = tmpPNode[childKey],
  838. isFirstNode = ((tmpPChild.length == nodes.length) && (i == 0)),
  839. isLastNode = (i == (nodes.length - 1));
  840. data.initNode(setting, level, node, parentNode, isFirstNode, isLastNode, openFlag);
  841. data.addNodeCache(setting, node);
  842. }
  843. var childHtml = [];
  844. if (node[childKey] && node[childKey].length > 0) {
  845. //make child html first, because checkType
  846. childHtml = view.appendNodes(setting, level + 1, node[childKey], node, initFlag, openFlag && node.open);
  847. }
  848. if (openFlag) {
  849. view.makeDOMNodeMainBefore(html, setting, node);
  850. view.makeDOMNodeLine(html, setting, node);
  851. data.getBeforeA(setting, node, html);
  852. view.makeDOMNodeNameBefore(html, setting, node);
  853. data.getInnerBeforeA(setting, node, html);
  854. view.makeDOMNodeIcon(html, setting, node);
  855. data.getInnerAfterA(setting, node, html);
  856. view.makeDOMNodeNameAfter(html, setting, node);
  857. data.getAfterA(setting, node, html);
  858. if (node.isParent && node.open) {
  859. view.makeUlHtml(setting, node, html, childHtml.join(''));
  860. }
  861. view.makeDOMNodeMainAfter(html, setting, node);
  862. data.addCreatedNode(setting, node);
  863. }
  864. }
  865. return html;
  866. },
  867. appendParentULDom: function(setting, node) {
  868. var html = [],
  869. nObj = $$(node, setting);
  870. if (!nObj.get(0) && !!node.parentTId) {
  871. view.appendParentULDom(setting, node.getParentNode());
  872. nObj = $$(node, setting);
  873. }
  874. var ulObj = $$(node, consts.id.UL, setting);
  875. if (ulObj.get(0)) {
  876. ulObj.remove();
  877. }
  878. var childKey = setting.data.key.children,
  879. childHtml = view.appendNodes(setting, node.level+1, node[childKey], node, false, true);
  880. view.makeUlHtml(setting, node, html, childHtml.join(''));
  881. nObj.append(html.join(''));
  882. },
  883. asyncNode: function(setting, node, isSilent, callback) {
  884. var i, l;
  885. if (node && !node.isParent) {
  886. tools.apply(callback);
  887. return false;
  888. } else if (node && node.isAjaxing) {
  889. return false;
  890. } else if (tools.apply(setting.callback.beforeAsync, [setting.treeId, node], true) == false) {
  891. tools.apply(callback);
  892. return false;
  893. }
  894. if (node) {
  895. node.isAjaxing = true;
  896. var icoObj = $$(node, consts.id.ICON, setting);
  897. icoObj.attr({"style":"", "class":consts.className.BUTTON + " " + consts.className.ICO_LOADING});
  898. }
  899. var tmpParam = {};
  900. for (i = 0, l = setting.async.autoParam.length; node && i < l; i++) {
  901. var pKey = setting.async.autoParam[i].split("="), spKey = pKey;
  902. if (pKey.length>1) {
  903. spKey = pKey[1];
  904. pKey = pKey[0];
  905. }
  906. tmpParam[spKey] = node[pKey];
  907. }
  908. if (tools.isArray(setting.async.otherParam)) {
  909. for (i = 0, l = setting.async.otherParam.length; i < l; i += 2) {
  910. tmpParam[setting.async.otherParam[i]] = setting.async.otherParam[i + 1];
  911. }
  912. } else {
  913. for (var p in setting.async.otherParam) {
  914. tmpParam[p] = setting.async.otherParam[p];
  915. }
  916. }
  917. var _tmpV = data.getRoot(setting)._ver;
  918. $.ajax({
  919. contentType: setting.async.contentType,
  920. type: setting.async.type,
  921. url: tools.apply(setting.async.url, [setting.treeId, node], setting.async.url),
  922. data: tmpParam,
  923. dataType: setting.async.dataType,
  924. success: function(msg) {
  925. if (_tmpV != data.getRoot(setting)._ver) {
  926. return;
  927. }
  928. var newNodes = [];
  929. try {
  930. if (!msg || msg.length == 0) {
  931. newNodes = [];
  932. } else if (typeof msg == "string") {
  933. newNodes = eval("(" + msg + ")");
  934. } else {
  935. newNodes = msg;
  936. }
  937. } catch(err) {
  938. newNodes = msg;
  939. }
  940. if (node) {
  941. node.isAjaxing = null;
  942. node.zAsync = true;
  943. }
  944. view.setNodeLineIcos(setting, node);
  945. if (newNodes && newNodes !== "") {
  946. newNodes = tools.apply(setting.async.dataFilter, [setting.treeId, node, newNodes], newNodes);
  947. view.addNodes(setting, node, !!newNodes ? tools.clone(newNodes) : [], !!isSilent);
  948. } else {
  949. view.addNodes(setting, node, [], !!isSilent);
  950. }
  951. setting.treeObj.trigger(consts.event.ASYNC_SUCCESS, [setting.treeId, node, msg]);
  952. tools.apply(callback);
  953. },
  954. error: function(XMLHttpRequest, textStatus, errorThrown) {
  955. if (_tmpV != data.getRoot(setting)._ver) {
  956. return;
  957. }
  958. if (node) node.isAjaxing = null;
  959. view.setNodeLineIcos(setting, node);
  960. setting.treeObj.trigger(consts.event.ASYNC_ERROR, [setting.treeId, node, XMLHttpRequest, textStatus, errorThrown]);
  961. }
  962. });
  963. return true;
  964. },
  965. cancelPreSelectedNode: function (setting, node) {
  966. var list = data.getRoot(setting).curSelectedList;
  967. for (var i=0, j=list.length-1; j>=i; j--) {
  968. if (!node || node === list[j]) {
  969. $$(list[j], consts.id.A, setting).removeClass(consts.node.CURSELECTED);
  970. if (node) {
  971. data.removeSelectedNode(setting, node);
  972. break;
  973. }
  974. }
  975. }
  976. if (!node) data.getRoot(setting).curSelectedList = [];
  977. },
  978. createNodeCallback: function(setting) {
  979. if (!!setting.callback.onNodeCreated || !!setting.view.addDiyDom) {
  980. var root = data.getRoot(setting);
  981. while (root.createdNodes.length>0) {
  982. var node = root.createdNodes.shift();
  983. tools.apply(setting.view.addDiyDom, [setting.treeId, node]);
  984. if (!!setting.callback.onNodeCreated) {
  985. setting.treeObj.trigger(consts.event.NODECREATED, [setting.treeId, node]);
  986. }
  987. }
  988. }
  989. },
  990. createNodes: function(setting, level, nodes, parentNode) {
  991. if (!nodes || nodes.length == 0) return;
  992. var root = data.getRoot(setting),
  993. childKey = setting.data.key.children,
  994. openFlag = !parentNode || parentNode.open || !!$$(parentNode[childKey][0], setting).get(0);
  995. root.createdNodes = [];
  996. var zTreeHtml = view.appendNodes(setting, level, nodes, parentNode, true, openFlag);
  997. if (!parentNode) {
  998. setting.treeObj.append(zTreeHtml.join(''));
  999. } else {
  1000. var ulObj = $$(parentNode, consts.id.UL, setting);
  1001. if (ulObj.get(0)) {
  1002. ulObj.append(zTreeHtml.join(''));
  1003. }
  1004. }
  1005. view.createNodeCallback(setting);
  1006. },
  1007. destroy: function(setting) {
  1008. if (!setting) return;
  1009. data.initCache(setting);
  1010. data.initRoot(setting);
  1011. event.unbindTree(setting);
  1012. event.unbindEvent(setting);
  1013. setting.treeObj.empty();
  1014. delete settings[setting.treeId];
  1015. },
  1016. expandCollapseNode: function(setting, node, expandFlag, animateFlag, callback) {
  1017. var root = data.getRoot(setting),
  1018. childKey = setting.data.key.children;
  1019. if (!node) {
  1020. tools.apply(callback, []);
  1021. return;
  1022. }
  1023. if (root.expandTriggerFlag) {
  1024. var _callback = callback;
  1025. callback = function(){
  1026. if (_callback) _callback();
  1027. if (node.open) {
  1028. setting.treeObj.trigger(consts.event.EXPAND, [setting.treeId, node]);
  1029. } else {
  1030. setting.treeObj.trigger(consts.event.COLLAPSE, [setting.treeId, node]);
  1031. }
  1032. };
  1033. root.expandTriggerFlag = false;
  1034. }
  1035. if (!node.open && node.isParent && ((!$$(node, consts.id.UL, setting).get(0)) || (node[childKey] && node[childKey].length>0 && !$$(node[childKey][0], setting).get(0)))) {
  1036. view.appendParentULDom(setting, node);
  1037. view.createNodeCallback(setting);
  1038. }
  1039. if (node.open == expandFlag) {
  1040. tools.apply(callback, []);
  1041. return;
  1042. }
  1043. var ulObj = $$(node, consts.id.UL, setting),
  1044. switchObj = $$(node, consts.id.SWITCH, setting),
  1045. icoObj = $$(node, consts.id.ICON, setting);
  1046. if (node.isParent) {
  1047. node.open = !node.open;
  1048. if (node.iconOpen && node.iconClose) {
  1049. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1050. }
  1051. if (node.open) {
  1052. view.replaceSwitchClass(node, switchObj, consts.folder.OPEN);
  1053. view.replaceIcoClass(node, icoObj, consts.folder.OPEN);
  1054. if (animateFlag == false || setting.view.expandSpeed == "") {
  1055. ulObj.show();
  1056. tools.apply(callback, []);
  1057. } else {
  1058. if (node[childKey] && node[childKey].length > 0) {
  1059. ulObj.slideDown(setting.view.expandSpeed, callback);
  1060. } else {
  1061. ulObj.show();
  1062. tools.apply(callback, []);
  1063. }
  1064. }
  1065. } else {
  1066. view.replaceSwitchClass(node, switchObj, consts.folder.CLOSE);
  1067. view.replaceIcoClass(node, icoObj, consts.folder.CLOSE);
  1068. if (animateFlag == false || setting.view.expandSpeed == "" || !(node[childKey] && node[childKey].length > 0)) {
  1069. ulObj.hide();
  1070. tools.apply(callback, []);
  1071. } else {
  1072. ulObj.slideUp(setting.view.expandSpeed, callback);
  1073. }
  1074. }
  1075. } else {
  1076. tools.apply(callback, []);
  1077. }
  1078. },
  1079. expandCollapseParentNode: function(setting, node, expandFlag, animateFlag, callback) {
  1080. if (!node) return;
  1081. if (!node.parentTId) {
  1082. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback);
  1083. return;
  1084. } else {
  1085. view.expandCollapseNode(setting, node, expandFlag, animateFlag);
  1086. }
  1087. if (node.parentTId) {
  1088. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, animateFlag, callback);
  1089. }
  1090. },
  1091. expandCollapseSonNode: function(setting, node, expandFlag, animateFlag, callback) {
  1092. var root = data.getRoot(setting),
  1093. childKey = setting.data.key.children,
  1094. treeNodes = (node) ? node[childKey]: root[childKey],
  1095. selfAnimateSign = (node) ? false : animateFlag,
  1096. expandTriggerFlag = data.getRoot(setting).expandTriggerFlag;
  1097. data.getRoot(setting).expandTriggerFlag = false;
  1098. if (treeNodes) {
  1099. for (var i = 0, l = treeNodes.length; i < l; i++) {
  1100. if (treeNodes[i]) view.expandCollapseSonNode(setting, treeNodes[i], expandFlag, selfAnimateSign);
  1101. }
  1102. }
  1103. data.getRoot(setting).expandTriggerFlag = expandTriggerFlag;
  1104. view.expandCollapseNode(setting, node, expandFlag, animateFlag, callback );
  1105. },
  1106. makeDOMNodeIcon: function(html, setting, node) {
  1107. var nameStr = data.getNodeName(setting, node),
  1108. name = setting.view.nameIsHTML ? nameStr : nameStr.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  1109. html.push("<span id='", node.tId, consts.id.ICON,
  1110. "' title='' treeNode", consts.id.ICON," class='", view.makeNodeIcoClass(setting, node),
  1111. "' style='", view.makeNodeIcoStyle(setting, node), "'></span><span id='", node.tId, consts.id.SPAN,
  1112. "'>",name,"</span>");
  1113. },
  1114. makeDOMNodeLine: function(html, setting, node) {
  1115. html.push("<span id='", node.tId, consts.id.SWITCH, "' title='' class='", view.makeNodeLineClass(setting, node), "' treeNode", consts.id.SWITCH,"></span>");
  1116. },
  1117. makeDOMNodeMainAfter: function(html, setting, node) {
  1118. html.push("</li>");
  1119. },
  1120. makeDOMNodeMainBefore: function(html, setting, node) {
  1121. html.push("<li id='", node.tId, "' class='", consts.className.LEVEL, node.level,"' tabindex='0' hidefocus='true' treenode>");
  1122. },
  1123. makeDOMNodeNameAfter: function(html, setting, node) {
  1124. html.push("</a>");
  1125. },
  1126. makeDOMNodeNameBefore: function(html, setting, node) {
  1127. var title = data.getNodeTitle(setting, node),
  1128. url = view.makeNodeUrl(setting, node),
  1129. fontcss = view.makeNodeFontCss(setting, node),
  1130. fontStyle = [];
  1131. for (var f in fontcss) {
  1132. fontStyle.push(f, ":", fontcss[f], ";");
  1133. }
  1134. html.push("<a id='", node.tId, consts.id.A, "' class='", consts.className.LEVEL, node.level,"' treeNode", consts.id.A," onclick=\"", (node.click || ''),
  1135. "\" ", ((url != null && url.length > 0) ? "href='" + url + "'" : ""), " target='",view.makeNodeTarget(node),"' style='", fontStyle.join(''),
  1136. "'");
  1137. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle) && title) {html.push("title='", title.replace(/'/g,"&#39;").replace(/</g,'&lt;').replace(/>/g,'&gt;'),"'");}
  1138. html.push(">");
  1139. },
  1140. makeNodeFontCss: function(setting, node) {
  1141. var fontCss = tools.apply(setting.view.fontCss, [setting.treeId, node], setting.view.fontCss);
  1142. return (fontCss && ((typeof fontCss) != "function")) ? fontCss : {};
  1143. },
  1144. makeNodeIcoClass: function(setting, node) {
  1145. var icoCss = ["ico"];
  1146. if (!node.isAjaxing) {
  1147. icoCss[0] = (node.iconSkin ? node.iconSkin + "_" : "") + icoCss[0];
  1148. if (node.isParent) {
  1149. icoCss.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1150. } else {
  1151. icoCss.push(consts.folder.DOCU);
  1152. }
  1153. }
  1154. return consts.className.BUTTON + " " + icoCss.join('_');
  1155. },
  1156. makeNodeIcoStyle: function(setting, node) {
  1157. var icoStyle = [];
  1158. if (!node.isAjaxing) {
  1159. var icon = (node.isParent && node.iconOpen && node.iconClose) ? (node.open ? node.iconOpen : node.iconClose) : node.icon;
  1160. if (icon) icoStyle.push("background:url(", icon, ") 0 0 no-repeat;");
  1161. if (setting.view.showIcon == false || !tools.apply(setting.view.showIcon, [setting.treeId, node], true)) {
  1162. icoStyle.push("width:0px;height:0px;");
  1163. }
  1164. }
  1165. return icoStyle.join('');
  1166. },
  1167. makeNodeLineClass: function(setting, node) {
  1168. var lineClass = [];
  1169. if (setting.view.showLine) {
  1170. if (node.level == 0 && node.isFirstNode && node.isLastNode) {
  1171. lineClass.push(consts.line.ROOT);
  1172. } else if (node.level == 0 && node.isFirstNode) {
  1173. lineClass.push(consts.line.ROOTS);
  1174. } else if (node.isLastNode) {
  1175. lineClass.push(consts.line.BOTTOM);
  1176. } else {
  1177. lineClass.push(consts.line.CENTER);
  1178. }
  1179. } else {
  1180. lineClass.push(consts.line.NOLINE);
  1181. }
  1182. if (node.isParent) {
  1183. lineClass.push(node.open ? consts.folder.OPEN : consts.folder.CLOSE);
  1184. } else {
  1185. lineClass.push(consts.folder.DOCU);
  1186. }
  1187. return view.makeNodeLineClassEx(node) + lineClass.join('_');
  1188. },
  1189. makeNodeLineClassEx: function(node) {
  1190. return consts.className.BUTTON + " " + consts.className.LEVEL + node.level + " " + consts.className.SWITCH + " ";
  1191. },
  1192. makeNodeTarget: function(node) {
  1193. return (node.target || "_blank");
  1194. },
  1195. makeNodeUrl: function(setting, node) {
  1196. var urlKey = setting.data.key.url;
  1197. return node[urlKey] ? node[urlKey] : null;
  1198. },
  1199. makeUlHtml: function(setting, node, html, content) {
  1200. html.push("<ul id='", node.tId, consts.id.UL, "' class='", consts.className.LEVEL, node.level, " ", view.makeUlLineClass(setting, node), "' style='display:", (node.open ? "block": "none"),"'>");
  1201. html.push(content);
  1202. html.push("</ul>");
  1203. },
  1204. makeUlLineClass: function(setting, node) {
  1205. return ((setting.view.showLine && !node.isLastNode) ? consts.line.LINE : "");
  1206. },
  1207. removeChildNodes: function(setting, node) {
  1208. if (!node) return;
  1209. var childKey = setting.data.key.children,
  1210. nodes = node[childKey];
  1211. if (!nodes) return;
  1212. for (var i = 0, l = nodes.length; i < l; i++) {
  1213. data.removeNodeCache(setting, nodes[i]);
  1214. }
  1215. data.removeSelectedNode(setting);
  1216. delete node[childKey];
  1217. if (!setting.data.keep.parent) {
  1218. node.isParent = false;
  1219. node.open = false;
  1220. var tmp_switchObj = $$(node, consts.id.SWITCH, setting),
  1221. tmp_icoObj = $$(node, consts.id.ICON, setting);
  1222. view.replaceSwitchClass(node, tmp_switchObj, consts.folder.DOCU);
  1223. view.replaceIcoClass(node, tmp_icoObj, consts.folder.DOCU);
  1224. $$(node, consts.id.UL, setting).remove();
  1225. } else {
  1226. $$(node, consts.id.UL, setting).empty();
  1227. }
  1228. },
  1229. setFirstNode: function(setting, parentNode) {
  1230. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1231. if ( childLength > 0) {
  1232. parentNode[childKey][0].isFirstNode = true;
  1233. }
  1234. },
  1235. setLastNode: function(setting, parentNode) {
  1236. var childKey = setting.data.key.children, childLength = parentNode[childKey].length;
  1237. if ( childLength > 0) {
  1238. parentNode[childKey][childLength - 1].isLastNode = true;
  1239. }
  1240. },
  1241. removeNode: function(setting, node) {
  1242. var root = data.getRoot(setting),
  1243. childKey = setting.data.key.children,
  1244. parentNode = (node.parentTId) ? node.getParentNode() : root;
  1245. node.isFirstNode = false;
  1246. node.isLastNode = false;
  1247. node.getPreNode = function() {return null;};
  1248. node.getNextNode = function() {return null;};
  1249. if (!data.getNodeCache(setting, node.tId)) {
  1250. return;
  1251. }
  1252. $$(node, setting).remove();
  1253. data.removeNodeCache(setting, node);
  1254. data.removeSelectedNode(setting, node);
  1255. for (var i = 0, l = parentNode[childKey].length; i < l; i++) {
  1256. if (parentNode[childKey][i].tId == node.tId) {
  1257. parentNode[childKey].splice(i, 1);
  1258. break;
  1259. }
  1260. }
  1261. view.setFirstNode(setting, parentNode);
  1262. view.setLastNode(setting, parentNode);
  1263. var tmp_ulObj,tmp_switchObj,tmp_icoObj,
  1264. childLength = parentNode[childKey].length;
  1265. //repair nodes old parent
  1266. if (!setting.data.keep.parent && childLength == 0) {
  1267. //old parentNode has no child nodes
  1268. parentNode.isParent = false;
  1269. parentNode.open = false;
  1270. tmp_ulObj = $$(parentNode, consts.id.UL, setting);
  1271. tmp_switchObj = $$(parentNode, consts.id.SWITCH, setting);
  1272. tmp_icoObj = $$(parentNode, consts.id.ICON, setting);
  1273. view.replaceSwitchClass(parentNode, tmp_switchObj, consts.folder.DOCU);
  1274. view.replaceIcoClass(parentNode, tmp_icoObj, consts.folder.DOCU);
  1275. tmp_ulObj.css("display", "none");
  1276. } else if (setting.view.showLine && childLength > 0) {
  1277. //old parentNode has child nodes
  1278. var newLast = parentNode[childKey][childLength - 1];
  1279. tmp_ulObj = $$(newLast, consts.id.UL, setting);
  1280. tmp_switchObj = $$(newLast, consts.id.SWITCH, setting);
  1281. tmp_icoObj = $$(newLast, consts.id.ICON, setting);
  1282. if (parentNode == root) {
  1283. if (parentNode[childKey].length == 1) {
  1284. //node was root, and ztree has only one root after move node
  1285. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.ROOT);
  1286. } else {
  1287. var tmp_first_switchObj = $$(parentNode[childKey][0], consts.id.SWITCH, setting);
  1288. view.replaceSwitchClass(parentNode[childKey][0], tmp_first_switchObj, consts.line.ROOTS);
  1289. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1290. }
  1291. } else {
  1292. view.replaceSwitchClass(newLast, tmp_switchObj, consts.line.BOTTOM);
  1293. }
  1294. tmp_ulObj.removeClass(consts.line.LINE);
  1295. }
  1296. },
  1297. replaceIcoClass: function(node, obj, newName) {
  1298. if (!obj || node.isAjaxing) return;
  1299. var tmpName = obj.attr("class");
  1300. if (tmpName == undefined) return;
  1301. var tmpList = tmpName.split("_");
  1302. switch (newName) {
  1303. case consts.folder.OPEN:
  1304. case consts.folder.CLOSE:
  1305. case consts.folder.DOCU:
  1306. tmpList[tmpList.length-1] = newName;
  1307. break;
  1308. }
  1309. obj.attr("class", tmpList.join("_"));
  1310. },
  1311. replaceSwitchClass: function(node, obj, newName) {
  1312. if (!obj) return;
  1313. var tmpName = obj.attr("class");
  1314. if (tmpName == undefined) return;
  1315. var tmpList = tmpName.split("_");
  1316. switch (newName) {
  1317. case consts.line.ROOT:
  1318. case consts.line.ROOTS:
  1319. case consts.line.CENTER:
  1320. case consts.line.BOTTOM:
  1321. case consts.line.NOLINE:
  1322. tmpList[0] = view.makeNodeLineClassEx(node) + newName;
  1323. break;
  1324. case consts.folder.OPEN:
  1325. case consts.folder.CLOSE:
  1326. case consts.folder.DOCU:
  1327. tmpList[1] = newName;
  1328. break;
  1329. }
  1330. obj.attr("class", tmpList.join("_"));
  1331. if (newName !== consts.folder.DOCU) {
  1332. obj.removeAttr("disabled");
  1333. } else {
  1334. obj.attr("disabled", "disabled");
  1335. }
  1336. },
  1337. selectNode: function(setting, node, addFlag) {
  1338. if (!addFlag) {
  1339. view.cancelPreSelectedNode(setting);
  1340. }
  1341. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED);
  1342. data.addSelectedNode(setting, node);
  1343. },
  1344. setNodeFontCss: function(setting, treeNode) {
  1345. var aObj = $$(treeNode, consts.id.A, setting),
  1346. fontCss = view.makeNodeFontCss(setting, treeNode);
  1347. if (fontCss) {
  1348. aObj.css(fontCss);
  1349. }
  1350. },
  1351. setNodeLineIcos: function(setting, node) {
  1352. if (!node) return;
  1353. var switchObj = $$(node, consts.id.SWITCH, setting),
  1354. ulObj = $$(node, consts.id.UL, setting),
  1355. icoObj = $$(node, consts.id.ICON, setting),
  1356. ulLine = view.makeUlLineClass(setting, node);
  1357. if (ulLine.length==0) {
  1358. ulObj.removeClass(consts.line.LINE);
  1359. } else {
  1360. ulObj.addClass(ulLine);
  1361. }
  1362. switchObj.attr("class", view.makeNodeLineClass(setting, node));
  1363. if (node.isParent) {
  1364. switchObj.removeAttr("disabled");
  1365. } else {
  1366. switchObj.attr("disabled", "disabled");
  1367. }
  1368. icoObj.removeAttr("style");
  1369. icoObj.attr("style", view.makeNodeIcoStyle(setting, node));
  1370. icoObj.attr("class", view.makeNodeIcoClass(setting, node));
  1371. },
  1372. setNodeName: function(setting, node) {
  1373. var title = data.getNodeTitle(setting, node),
  1374. nObj = $$(node, consts.id.SPAN, setting);
  1375. nObj.empty();
  1376. if (setting.view.nameIsHTML) {
  1377. nObj.html(data.getNodeName(setting, node));
  1378. } else {
  1379. nObj.text(data.getNodeName(setting, node));
  1380. }
  1381. if (tools.apply(setting.view.showTitle, [setting.treeId, node], setting.view.showTitle)) {
  1382. var aObj = $$(node, consts.id.A, setting);
  1383. aObj.attr("title", !title ? "" : title);
  1384. }
  1385. },
  1386. setNodeTarget: function(setting, node) {
  1387. var aObj = $$(node, consts.id.A, setting);
  1388. aObj.attr("target", view.makeNodeTarget(node));
  1389. },
  1390. setNodeUrl: function(setting, node) {
  1391. var aObj = $$(node, consts.id.A, setting),
  1392. url = view.makeNodeUrl(setting, node);
  1393. if (url == null || url.length == 0) {
  1394. aObj.removeAttr("href");
  1395. } else {
  1396. aObj.attr("href", url);
  1397. }
  1398. },
  1399. switchNode: function(setting, node) {
  1400. if (node.open || !tools.canAsync(setting, node)) {
  1401. view.expandCollapseNode(setting, node, !node.open);
  1402. } else if (setting.async.enable) {
  1403. if (!view.asyncNode(setting, node)) {
  1404. view.expandCollapseNode(setting, node, !node.open);
  1405. return;
  1406. }
  1407. } else if (node) {
  1408. view.expandCollapseNode(setting, node, !node.open);
  1409. }
  1410. }
  1411. };
  1412. // zTree defind
  1413. $.fn.zTree = {
  1414. consts : _consts,
  1415. _z : {
  1416. tools: tools,
  1417. view: view,
  1418. event: event,
  1419. data: data
  1420. },
  1421. getZTreeObj: function(treeId) {
  1422. var o = data.getZTreeTools(treeId);
  1423. return o ? o : null;
  1424. },
  1425. destroy: function(treeId) {
  1426. if (!!treeId && treeId.length > 0) {
  1427. view.destroy(data.getSetting(treeId));
  1428. } else {
  1429. for(var s in settings) {
  1430. view.destroy(settings[s]);
  1431. }
  1432. }
  1433. },
  1434. init: function(obj, zSetting, zNodes) {
  1435. var setting = tools.clone(_setting);
  1436. $.extend(true, setting, zSetting);
  1437. setting.treeId = obj.attr("id");
  1438. setting.treeObj = obj;
  1439. setting.treeObj.empty();
  1440. settings[setting.treeId] = setting;
  1441. //For some older browser,(e.g., ie6)
  1442. if(typeof document.body.style.maxHeight === "undefined") {
  1443. setting.view.expandSpeed = "";
  1444. }
  1445. data.initRoot(setting);
  1446. var root = data.getRoot(setting),
  1447. childKey = setting.data.key.children;
  1448. zNodes = zNodes ? tools.clone(tools.isArray(zNodes)? zNodes : [zNodes]) : [];
  1449. if (setting.data.simpleData.enable) {
  1450. root[childKey] = data.transformTozTreeFormat(setting, zNodes);
  1451. } else {
  1452. root[childKey] = zNodes;
  1453. }
  1454. data.initCache(setting);
  1455. event.unbindTree(setting);
  1456. event.bindTree(setting);
  1457. event.unbindEvent(setting);
  1458. event.bindEvent(setting);
  1459. var zTreeTools = {
  1460. setting : setting,
  1461. addNodes : function(parentNode, newNodes, isSilent) {
  1462. if (!newNodes) return null;
  1463. if (!parentNode) parentNode = null;
  1464. if (parentNode && !parentNode.isParent && setting.data.keep.leaf) return null;
  1465. var xNewNodes = tools.clone(tools.isArray(newNodes)? newNodes: [newNodes]);
  1466. function addCallback() {
  1467. view.addNodes(setting, parentNode, xNewNodes, (isSilent==true));
  1468. }
  1469. if (tools.canAsync(setting, parentNode)) {
  1470. view.asyncNode(setting, parentNode, isSilent, addCallback);
  1471. } else {
  1472. addCallback();
  1473. }
  1474. return xNewNodes;
  1475. },
  1476. cancelSelectedNode : function(node) {
  1477. view.cancelPreSelectedNode(setting, node);
  1478. },
  1479. destroy : function() {
  1480. view.destroy(setting);
  1481. },
  1482. expandAll : function(expandFlag) {
  1483. expandFlag = !!expandFlag;
  1484. view.expandCollapseSonNode(setting, null, expandFlag, true);
  1485. return expandFlag;
  1486. },
  1487. expandNode : function(node, expandFlag, sonSign, focus, callbackFlag) {
  1488. if (!node || !node.isParent) return null;
  1489. if (expandFlag !== true && expandFlag !== false) {
  1490. expandFlag = !node.open;
  1491. }
  1492. callbackFlag = !!callbackFlag;
  1493. if (callbackFlag && expandFlag && (tools.apply(setting.callback.beforeExpand, [setting.treeId, node], true) == false)) {
  1494. return null;
  1495. } else if (callbackFlag && !expandFlag && (tools.apply(setting.callback.beforeCollapse, [setting.treeId, node], true) == false)) {
  1496. return null;
  1497. }
  1498. if (expandFlag && node.parentTId) {
  1499. view.expandCollapseParentNode(setting, node.getParentNode(), expandFlag, false);
  1500. }
  1501. if (expandFlag === node.open && !sonSign) {
  1502. return null;
  1503. }
  1504. data.getRoot(setting).expandTriggerFlag = callbackFlag;
  1505. if (!tools.canAsync(setting, node) && sonSign) {
  1506. view.expandCollapseSonNode(setting, node, expandFlag, true, function() {
  1507. if (focus !== false) {try{$$(node, setting).focus().blur();}catch(e){}}
  1508. });
  1509. } else {
  1510. node.open = !expandFlag;
  1511. view.switchNode(this.setting, node);
  1512. if (focus !== false) {try{$$(node, setting).focus().blur();}catch(e){}}
  1513. }
  1514. return expandFlag;
  1515. },
  1516. getNodes : function() {
  1517. return data.getNodes(setting);
  1518. },
  1519. getNodeByParam : function(key, value, parentNode) {
  1520. if (!key) return null;
  1521. return data.getNodeByParam(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1522. },
  1523. getNodeByTId : function(tId) {
  1524. return data.getNodeCache(setting, tId);
  1525. },
  1526. getNodesByParam : function(key, value, parentNode) {
  1527. if (!key) return null;
  1528. return data.getNodesByParam(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1529. },
  1530. getNodesByParamFuzzy : function(key, value, parentNode) {
  1531. if (!key) return null;
  1532. return data.getNodesByParamFuzzy(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), key, value);
  1533. },
  1534. getNodesByFilter: function(filter, isSingle, parentNode, invokeParam) {
  1535. isSingle = !!isSingle;
  1536. if (!filter || (typeof filter != "function")) return (isSingle ? null : []);
  1537. return data.getNodesByFilter(setting, parentNode?parentNode[setting.data.key.children]:data.getNodes(setting), filter, isSingle, invokeParam);
  1538. },
  1539. getNodeIndex : function(node) {
  1540. if (!node) return null;
  1541. var childKey = setting.data.key.children,
  1542. parentNode = (node.parentTId) ? node.getParentNode() : data.getRoot(setting);
  1543. for (var i=0, l = parentNode[childKey].length; i < l; i++) {
  1544. if (parentNode[childKey][i] == node) return i;
  1545. }
  1546. return -1;
  1547. },
  1548. getSelectedNodes : function() {
  1549. var r = [], list = data.getRoot(setting).curSelectedList;
  1550. for (var i=0, l=list.length; i<l; i++) {
  1551. r.push(list[i]);
  1552. }
  1553. return r;
  1554. },
  1555. isSelectedNode : function(node) {
  1556. return data.isSelectedNode(setting, node);
  1557. },
  1558. reAsyncChildNodes : function(parentNode, reloadType, isSilent) {
  1559. if (!this.setting.async.enable) return;
  1560. var isRoot = !parentNode;
  1561. if (isRoot) {
  1562. parentNode = data.getRoot(setting);
  1563. }
  1564. if (reloadType=="refresh") {
  1565. var childKey = this.setting.data.key.children;
  1566. for (var i = 0, l = parentNode[childKey] ? parentNode[childKey].length : 0; i < l; i++) {
  1567. data.removeNodeCache(setting, parentNode[childKey][i]);
  1568. }
  1569. data.removeSelectedNode(setting);
  1570. parentNode[childKey] = [];
  1571. if (isRoot) {
  1572. this.setting.treeObj.empty();
  1573. } else {
  1574. var ulObj = $$(parentNode, consts.id.UL, setting);
  1575. ulObj.empty();
  1576. }
  1577. }
  1578. view.asyncNode(this.setting, isRoot? null:parentNode, !!isSilent);
  1579. },
  1580. refresh : function() {
  1581. this.setting.treeObj.empty();
  1582. var root = data.getRoot(setting),
  1583. nodes = root[setting.data.key.children]
  1584. data.initRoot(setting);
  1585. root[setting.data.key.children] = nodes
  1586. data.initCache(setting);
  1587. view.createNodes(setting, 0, root[setting.data.key.children]);
  1588. },
  1589. removeChildNodes : function(node) {
  1590. if (!node) return null;
  1591. var childKey = setting.data.key.children,
  1592. nodes = node[childKey];
  1593. view.removeChildNodes(setting, node);
  1594. return nodes ? nodes : null;
  1595. },
  1596. removeNode : function(node, callbackFlag) {
  1597. if (!node) return;
  1598. callbackFlag = !!callbackFlag;
  1599. if (callbackFlag && tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return;
  1600. view.removeNode(setting, node);
  1601. if (callbackFlag) {
  1602. this.setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  1603. }
  1604. },
  1605. selectNode : function(node, addFlag) {
  1606. if (!node) return;
  1607. if (tools.uCanDo(setting)) {
  1608. addFlag = setting.view.selectedMulti && addFlag;
  1609. if (node.parentTId) {
  1610. view.expandCollapseParentNode(setting, node.getParentNode(), true, false, function() {
  1611. try{$$(node, setting).focus().blur();}catch(e){}
  1612. });
  1613. } else {
  1614. try{$$(node, setting).focus().blur();}catch(e){}
  1615. }
  1616. view.selectNode(setting, node, addFlag);
  1617. }
  1618. },
  1619. transformTozTreeNodes : function(simpleNodes) {
  1620. return data.transformTozTreeFormat(setting, simpleNodes);
  1621. },
  1622. transformToArray : function(nodes) {
  1623. return data.transformToArrayFormat(setting, nodes);
  1624. },
  1625. updateNode : function(node, checkTypeFlag) {
  1626. if (!node) return;
  1627. var nObj = $$(node, setting);
  1628. if (nObj.get(0) && tools.uCanDo(setting)) {
  1629. view.setNodeName(setting, node);
  1630. view.setNodeTarget(setting, node);
  1631. view.setNodeUrl(setting, node);
  1632. view.setNodeLineIcos(setting, node);
  1633. view.setNodeFontCss(setting, node);
  1634. }
  1635. }
  1636. }
  1637. root.treeTools = zTreeTools;
  1638. data.setZTreeTools(setting, zTreeTools);
  1639. if (root[childKey] && root[childKey].length > 0) {
  1640. view.createNodes(setting, 0, root[childKey]);
  1641. } else if (setting.async.enable && setting.async.url && setting.async.url !== '') {
  1642. view.asyncNode(setting);
  1643. }
  1644. return zTreeTools;
  1645. }
  1646. };
  1647. var zt = $.fn.zTree,
  1648. $$ = tools.$,
  1649. consts = zt.consts;
  1650. })(jQuery);
  1651. /*
  1652. * JQuery zTree excheck v3.5.16-beta.1
  1653. * http://zTree.me/
  1654. *
  1655. * Copyright (c) 2010 Hunter.z
  1656. *
  1657. * Licensed same as jquery - MIT License
  1658. * http://www.opensource.org/licenses/mit-license.php
  1659. *
  1660. * email: hunter.z@263.net
  1661. * Date: 2014-03-28
  1662. */
  1663. (function($){
  1664. //default consts of excheck
  1665. var _consts = {
  1666. event: {
  1667. CHECK: "ztree_check"
  1668. },
  1669. id: {
  1670. CHECK: "_check"
  1671. },
  1672. checkbox: {
  1673. STYLE: "checkbox",
  1674. DEFAULT: "chk",
  1675. DISABLED: "disable",
  1676. FALSE: "false",
  1677. TRUE: "true",
  1678. FULL: "full",
  1679. PART: "part",
  1680. FOCUS: "focus"
  1681. },
  1682. radio: {
  1683. STYLE: "radio",
  1684. TYPE_ALL: "all",
  1685. TYPE_LEVEL: "level"
  1686. }
  1687. },
  1688. //default setting of excheck
  1689. _setting = {
  1690. check: {
  1691. enable: false,
  1692. autoCheckTrigger: false,
  1693. chkStyle: _consts.checkbox.STYLE,
  1694. nocheckInherit: false,
  1695. chkDisabledInherit: false,
  1696. radioType: _consts.radio.TYPE_LEVEL,
  1697. chkboxType: {
  1698. "Y": "ps",
  1699. "N": "ps"
  1700. }
  1701. },
  1702. data: {
  1703. key: {
  1704. checked: "checked"
  1705. }
  1706. },
  1707. callback: {
  1708. beforeCheck:null,
  1709. onCheck:null
  1710. }
  1711. },
  1712. //default root of excheck
  1713. _initRoot = function (setting) {
  1714. var r = data.getRoot(setting);
  1715. r.radioCheckedList = [];
  1716. },
  1717. //default cache of excheck
  1718. _initCache = function(treeId) {},
  1719. //default bind event of excheck
  1720. _bindEvent = function(setting) {
  1721. var o = setting.treeObj,
  1722. c = consts.event;
  1723. o.bind(c.CHECK, function (event, srcEvent, treeId, node) {
  1724. event.srcEvent = srcEvent;
  1725. tools.apply(setting.callback.onCheck, [event, treeId, node]);
  1726. });
  1727. },
  1728. _unbindEvent = function(setting) {
  1729. var o = setting.treeObj,
  1730. c = consts.event;
  1731. o.unbind(c.CHECK);
  1732. },
  1733. //default event proxy of excheck
  1734. _eventProxy = function(e) {
  1735. var target = e.target,
  1736. setting = data.getSetting(e.data.treeId),
  1737. tId = "", node = null,
  1738. nodeEventType = "", treeEventType = "",
  1739. nodeEventCallback = null, treeEventCallback = null;
  1740. if (tools.eqs(e.type, "mouseover")) {
  1741. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1742. tId = tools.getNodeMainDom(target).id;
  1743. nodeEventType = "mouseoverCheck";
  1744. }
  1745. } else if (tools.eqs(e.type, "mouseout")) {
  1746. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1747. tId = tools.getNodeMainDom(target).id;
  1748. nodeEventType = "mouseoutCheck";
  1749. }
  1750. } else if (tools.eqs(e.type, "click")) {
  1751. if (setting.check.enable && tools.eqs(target.tagName, "span") && target.getAttribute("treeNode"+ consts.id.CHECK) !== null) {
  1752. tId = tools.getNodeMainDom(target).id;
  1753. nodeEventType = "checkNode";
  1754. }
  1755. }
  1756. if (tId.length>0) {
  1757. node = data.getNodeCache(setting, tId);
  1758. switch (nodeEventType) {
  1759. case "checkNode" :
  1760. nodeEventCallback = _handler.onCheckNode;
  1761. break;
  1762. case "mouseoverCheck" :
  1763. nodeEventCallback = _handler.onMouseoverCheck;
  1764. break;
  1765. case "mouseoutCheck" :
  1766. nodeEventCallback = _handler.onMouseoutCheck;
  1767. break;
  1768. }
  1769. }
  1770. var proxyResult = {
  1771. stop: nodeEventType === "checkNode",
  1772. node: node,
  1773. nodeEventType: nodeEventType,
  1774. nodeEventCallback: nodeEventCallback,
  1775. treeEventType: treeEventType,
  1776. treeEventCallback: treeEventCallback
  1777. };
  1778. return proxyResult
  1779. },
  1780. //default init node of excheck
  1781. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  1782. if (!n) return;
  1783. var checkedKey = setting.data.key.checked;
  1784. if (typeof n[checkedKey] == "string") n[checkedKey] = tools.eqs(n[checkedKey], "true");
  1785. n[checkedKey] = !!n[checkedKey];
  1786. n.checkedOld = n[checkedKey];
  1787. if (typeof n.nocheck == "string") n.nocheck = tools.eqs(n.nocheck, "true");
  1788. n.nocheck = !!n.nocheck || (setting.check.nocheckInherit && parentNode && !!parentNode.nocheck);
  1789. if (typeof n.chkDisabled == "string") n.chkDisabled = tools.eqs(n.chkDisabled, "true");
  1790. n.chkDisabled = !!n.chkDisabled || (setting.check.chkDisabledInherit && parentNode && !!parentNode.chkDisabled);
  1791. if (typeof n.halfCheck == "string") n.halfCheck = tools.eqs(n.halfCheck, "true");
  1792. n.halfCheck = !!n.halfCheck;
  1793. n.check_Child_State = -1;
  1794. n.check_Focus = false;
  1795. n.getCheckStatus = function() {return data.getCheckStatus(setting, n);};
  1796. if (setting.check.chkStyle == consts.radio.STYLE && setting.check.radioType == consts.radio.TYPE_ALL && n[checkedKey] ) {
  1797. var r = data.getRoot(setting);
  1798. r.radioCheckedList.push(n);
  1799. }
  1800. },
  1801. //add dom for check
  1802. _beforeA = function(setting, node, html) {
  1803. var checkedKey = setting.data.key.checked;
  1804. if (setting.check.enable) {
  1805. data.makeChkFlag(setting, node);
  1806. html.push("<span ID='", node.tId, consts.id.CHECK, "' class='", view.makeChkClass(setting, node), "' treeNode", consts.id.CHECK, (node.nocheck === true?" style='display:none;'":""),"></span>");
  1807. }
  1808. },
  1809. //update zTreeObj, add method of check
  1810. _zTreeTools = function(setting, zTreeTools) {
  1811. zTreeTools.checkNode = function(node, checked, checkTypeFlag, callbackFlag) {
  1812. var checkedKey = this.setting.data.key.checked;
  1813. if (node.chkDisabled === true) return;
  1814. if (checked !== true && checked !== false) {
  1815. checked = !node[checkedKey];
  1816. }
  1817. callbackFlag = !!callbackFlag;
  1818. if (node[checkedKey] === checked && !checkTypeFlag) {
  1819. return;
  1820. } else if (callbackFlag && tools.apply(this.setting.callback.beforeCheck, [this.setting.treeId, node], true) == false) {
  1821. return;
  1822. }
  1823. if (tools.uCanDo(this.setting) && this.setting.check.enable && node.nocheck !== true) {
  1824. node[checkedKey] = checked;
  1825. var checkObj = $$(node, consts.id.CHECK, this.setting);
  1826. if (checkTypeFlag || this.setting.check.chkStyle === consts.radio.STYLE) view.checkNodeRelation(this.setting, node);
  1827. view.setChkClass(this.setting, checkObj, node);
  1828. view.repairParentChkClassWithSelf(this.setting, node);
  1829. if (callbackFlag) {
  1830. this.setting.treeObj.trigger(consts.event.CHECK, [null, this.setting.treeId, node]);
  1831. }
  1832. }
  1833. }
  1834. zTreeTools.checkAllNodes = function(checked) {
  1835. view.repairAllChk(this.setting, !!checked);
  1836. }
  1837. zTreeTools.getCheckedNodes = function(checked) {
  1838. var childKey = this.setting.data.key.children;
  1839. checked = (checked !== false);
  1840. return data.getTreeCheckedNodes(this.setting, data.getRoot(this.setting)[childKey], checked);
  1841. }
  1842. zTreeTools.getChangeCheckedNodes = function() {
  1843. var childKey = this.setting.data.key.children;
  1844. return data.getTreeChangeCheckedNodes(this.setting, data.getRoot(this.setting)[childKey]);
  1845. }
  1846. zTreeTools.setChkDisabled = function(node, disabled, inheritParent, inheritChildren) {
  1847. disabled = !!disabled;
  1848. inheritParent = !!inheritParent;
  1849. inheritChildren = !!inheritChildren;
  1850. view.repairSonChkDisabled(this.setting, node, disabled, inheritChildren);
  1851. view.repairParentChkDisabled(this.setting, node.getParentNode(), disabled, inheritParent);
  1852. }
  1853. var _updateNode = zTreeTools.updateNode;
  1854. zTreeTools.updateNode = function(node, checkTypeFlag) {
  1855. if (_updateNode) _updateNode.apply(zTreeTools, arguments);
  1856. if (!node || !this.setting.check.enable) return;
  1857. var nObj = $$(node, this.setting);
  1858. if (nObj.get(0) && tools.uCanDo(this.setting)) {
  1859. var checkObj = $$(node, consts.id.CHECK, this.setting);
  1860. if (checkTypeFlag == true || this.setting.check.chkStyle === consts.radio.STYLE) view.checkNodeRelation(this.setting, node);
  1861. view.setChkClass(this.setting, checkObj, node);
  1862. view.repairParentChkClassWithSelf(this.setting, node);
  1863. }
  1864. }
  1865. },
  1866. //method of operate data
  1867. _data = {
  1868. getRadioCheckedList: function(setting) {
  1869. var checkedList = data.getRoot(setting).radioCheckedList;
  1870. for (var i=0, j=checkedList.length; i<j; i++) {
  1871. if(!data.getNodeCache(setting, checkedList[i].tId)) {
  1872. checkedList.splice(i, 1);
  1873. i--; j--;
  1874. }
  1875. }
  1876. return checkedList;
  1877. },
  1878. getCheckStatus: function(setting, node) {
  1879. if (!setting.check.enable || node.nocheck || node.chkDisabled) return null;
  1880. var checkedKey = setting.data.key.checked,
  1881. r = {
  1882. checked: node[checkedKey],
  1883. half: node.halfCheck ? node.halfCheck : (setting.check.chkStyle == consts.radio.STYLE ? (node.check_Child_State === 2) : (node[checkedKey] ? (node.check_Child_State > -1 && node.check_Child_State < 2) : (node.check_Child_State > 0)))
  1884. };
  1885. return r;
  1886. },
  1887. getTreeCheckedNodes: function(setting, nodes, checked, results) {
  1888. if (!nodes) return [];
  1889. var childKey = setting.data.key.children,
  1890. checkedKey = setting.data.key.checked,
  1891. onlyOne = (checked && setting.check.chkStyle == consts.radio.STYLE && setting.check.radioType == consts.radio.TYPE_ALL);
  1892. results = !results ? [] : results;
  1893. for (var i = 0, l = nodes.length; i < l; i++) {
  1894. if (nodes[i].nocheck !== true && nodes[i].chkDisabled !== true && nodes[i][checkedKey] == checked) {
  1895. results.push(nodes[i]);
  1896. if(onlyOne) {
  1897. break;
  1898. }
  1899. }
  1900. data.getTreeCheckedNodes(setting, nodes[i][childKey], checked, results);
  1901. if(onlyOne && results.length > 0) {
  1902. break;
  1903. }
  1904. }
  1905. return results;
  1906. },
  1907. getTreeChangeCheckedNodes: function(setting, nodes, results) {
  1908. if (!nodes) return [];
  1909. var childKey = setting.data.key.children,
  1910. checkedKey = setting.data.key.checked;
  1911. results = !results ? [] : results;
  1912. for (var i = 0, l = nodes.length; i < l; i++) {
  1913. if (nodes[i].nocheck !== true && nodes[i].chkDisabled !== true && nodes[i][checkedKey] != nodes[i].checkedOld) {
  1914. results.push(nodes[i]);
  1915. }
  1916. data.getTreeChangeCheckedNodes(setting, nodes[i][childKey], results);
  1917. }
  1918. return results;
  1919. },
  1920. makeChkFlag: function(setting, node) {
  1921. if (!node) return;
  1922. var childKey = setting.data.key.children,
  1923. checkedKey = setting.data.key.checked,
  1924. chkFlag = -1;
  1925. if (node[childKey]) {
  1926. for (var i = 0, l = node[childKey].length; i < l; i++) {
  1927. var cNode = node[childKey][i];
  1928. var tmp = -1;
  1929. if (setting.check.chkStyle == consts.radio.STYLE) {
  1930. if (cNode.nocheck === true || cNode.chkDisabled === true) {
  1931. tmp = cNode.check_Child_State;
  1932. } else if (cNode.halfCheck === true) {
  1933. tmp = 2;
  1934. } else if (cNode[checkedKey]) {
  1935. tmp = 2;
  1936. } else {
  1937. tmp = cNode.check_Child_State > 0 ? 2:0;
  1938. }
  1939. if (tmp == 2) {
  1940. chkFlag = 2; break;
  1941. } else if (tmp == 0){
  1942. chkFlag = 0;
  1943. }
  1944. } else if (setting.check.chkStyle == consts.checkbox.STYLE) {
  1945. if (cNode.nocheck === true || cNode.chkDisabled === true) {
  1946. tmp = cNode.check_Child_State;
  1947. } else if (cNode.halfCheck === true) {
  1948. tmp = 1;
  1949. } else if (cNode[checkedKey] ) {
  1950. tmp = (cNode.check_Child_State === -1 || cNode.check_Child_State === 2) ? 2 : 1;
  1951. } else {
  1952. tmp = (cNode.check_Child_State > 0) ? 1 : 0;
  1953. }
  1954. if (tmp === 1) {
  1955. chkFlag = 1; break;
  1956. } else if (tmp === 2 && chkFlag > -1 && i > 0 && tmp !== chkFlag) {
  1957. chkFlag = 1; break;
  1958. } else if (chkFlag === 2 && tmp > -1 && tmp < 2) {
  1959. chkFlag = 1; break;
  1960. } else if (tmp > -1) {
  1961. chkFlag = tmp;
  1962. }
  1963. }
  1964. }
  1965. }
  1966. node.check_Child_State = chkFlag;
  1967. }
  1968. },
  1969. //method of event proxy
  1970. _event = {
  1971. },
  1972. //method of event handler
  1973. _handler = {
  1974. onCheckNode: function (event, node) {
  1975. if (node.chkDisabled === true) return false;
  1976. var setting = data.getSetting(event.data.treeId),
  1977. checkedKey = setting.data.key.checked;
  1978. if (tools.apply(setting.callback.beforeCheck, [setting.treeId, node], true) == false) return true;
  1979. node[checkedKey] = !node[checkedKey];
  1980. view.checkNodeRelation(setting, node);
  1981. var checkObj = $$(node, consts.id.CHECK, setting);
  1982. view.setChkClass(setting, checkObj, node);
  1983. view.repairParentChkClassWithSelf(setting, node);
  1984. setting.treeObj.trigger(consts.event.CHECK, [event, setting.treeId, node]);
  1985. return true;
  1986. },
  1987. onMouseoverCheck: function(event, node) {
  1988. if (node.chkDisabled === true) return false;
  1989. var setting = data.getSetting(event.data.treeId),
  1990. checkObj = $$(node, consts.id.CHECK, setting);
  1991. node.check_Focus = true;
  1992. view.setChkClass(setting, checkObj, node);
  1993. return true;
  1994. },
  1995. onMouseoutCheck: function(event, node) {
  1996. if (node.chkDisabled === true) return false;
  1997. var setting = data.getSetting(event.data.treeId),
  1998. checkObj = $$(node, consts.id.CHECK, setting);
  1999. node.check_Focus = false;
  2000. view.setChkClass(setting, checkObj, node);
  2001. return true;
  2002. }
  2003. },
  2004. //method of tools for zTree
  2005. _tools = {
  2006. },
  2007. //method of operate ztree dom
  2008. _view = {
  2009. checkNodeRelation: function(setting, node) {
  2010. var pNode, i, l,
  2011. childKey = setting.data.key.children,
  2012. checkedKey = setting.data.key.checked,
  2013. r = consts.radio;
  2014. if (setting.check.chkStyle == r.STYLE) {
  2015. var checkedList = data.getRadioCheckedList(setting);
  2016. if (node[checkedKey]) {
  2017. if (setting.check.radioType == r.TYPE_ALL) {
  2018. for (i = checkedList.length-1; i >= 0; i--) {
  2019. pNode = checkedList[i];
  2020. if (pNode[checkedKey] && pNode != node) {
  2021. pNode[checkedKey] = false;
  2022. checkedList.splice(i, 1);
  2023. view.setChkClass(setting, $$(pNode, consts.id.CHECK, setting), pNode);
  2024. if (pNode.parentTId != node.parentTId) {
  2025. view.repairParentChkClassWithSelf(setting, pNode);
  2026. }
  2027. }
  2028. }
  2029. checkedList.push(node);
  2030. } else {
  2031. var parentNode = (node.parentTId) ? node.getParentNode() : data.getRoot(setting);
  2032. for (i = 0, l = parentNode[childKey].length; i < l; i++) {
  2033. pNode = parentNode[childKey][i];
  2034. if (pNode[checkedKey] && pNode != node) {
  2035. pNode[checkedKey] = false;
  2036. view.setChkClass(setting, $$(pNode, consts.id.CHECK, setting), pNode);
  2037. }
  2038. }
  2039. }
  2040. } else if (setting.check.radioType == r.TYPE_ALL) {
  2041. for (i = 0, l = checkedList.length; i < l; i++) {
  2042. if (node == checkedList[i]) {
  2043. checkedList.splice(i, 1);
  2044. break;
  2045. }
  2046. }
  2047. }
  2048. } else {
  2049. if (node[checkedKey] && (!node[childKey] || node[childKey].length==0 || setting.check.chkboxType.Y.indexOf("s") > -1)) {
  2050. view.setSonNodeCheckBox(setting, node, true);
  2051. }
  2052. if (!node[checkedKey] && (!node[childKey] || node[childKey].length==0 || setting.check.chkboxType.N.indexOf("s") > -1)) {
  2053. view.setSonNodeCheckBox(setting, node, false);
  2054. }
  2055. if (node[checkedKey] && setting.check.chkboxType.Y.indexOf("p") > -1) {
  2056. view.setParentNodeCheckBox(setting, node, true);
  2057. }
  2058. if (!node[checkedKey] && setting.check.chkboxType.N.indexOf("p") > -1) {
  2059. view.setParentNodeCheckBox(setting, node, false);
  2060. }
  2061. }
  2062. },
  2063. makeChkClass: function(setting, node) {
  2064. var checkedKey = setting.data.key.checked,
  2065. c = consts.checkbox, r = consts.radio,
  2066. fullStyle = "";
  2067. if (node.chkDisabled === true) {
  2068. fullStyle = c.DISABLED;
  2069. } else if (node.halfCheck) {
  2070. fullStyle = c.PART;
  2071. } else if (setting.check.chkStyle == r.STYLE) {
  2072. fullStyle = (node.check_Child_State < 1)? c.FULL:c.PART;
  2073. } else {
  2074. fullStyle = node[checkedKey] ? ((node.check_Child_State === 2 || node.check_Child_State === -1) ? c.FULL:c.PART) : ((node.check_Child_State < 1)? c.FULL:c.PART);
  2075. }
  2076. var chkName = setting.check.chkStyle + "_" + (node[checkedKey] ? c.TRUE : c.FALSE) + "_" + fullStyle;
  2077. chkName = (node.check_Focus && node.chkDisabled !== true) ? chkName + "_" + c.FOCUS : chkName;
  2078. return consts.className.BUTTON + " " + c.DEFAULT + " " + chkName;
  2079. },
  2080. repairAllChk: function(setting, checked) {
  2081. if (setting.check.enable && setting.check.chkStyle === consts.checkbox.STYLE) {
  2082. var checkedKey = setting.data.key.checked,
  2083. childKey = setting.data.key.children,
  2084. root = data.getRoot(setting);
  2085. for (var i = 0, l = root[childKey].length; i<l ; i++) {
  2086. var node = root[childKey][i];
  2087. if (node.nocheck !== true && node.chkDisabled !== true) {
  2088. node[checkedKey] = checked;
  2089. }
  2090. view.setSonNodeCheckBox(setting, node, checked);
  2091. }
  2092. }
  2093. },
  2094. repairChkClass: function(setting, node) {
  2095. if (!node) return;
  2096. data.makeChkFlag(setting, node);
  2097. if (node.nocheck !== true) {
  2098. var checkObj = $$(node, consts.id.CHECK, setting);
  2099. view.setChkClass(setting, checkObj, node);
  2100. }
  2101. },
  2102. repairParentChkClass: function(setting, node) {
  2103. if (!node || !node.parentTId) return;
  2104. var pNode = node.getParentNode();
  2105. view.repairChkClass(setting, pNode);
  2106. view.repairParentChkClass(setting, pNode);
  2107. },
  2108. repairParentChkClassWithSelf: function(setting, node) {
  2109. if (!node) return;
  2110. var childKey = setting.data.key.children;
  2111. if (node[childKey] && node[childKey].length > 0) {
  2112. view.repairParentChkClass(setting, node[childKey][0]);
  2113. } else {
  2114. view.repairParentChkClass(setting, node);
  2115. }
  2116. },
  2117. repairSonChkDisabled: function(setting, node, chkDisabled, inherit) {
  2118. if (!node) return;
  2119. var childKey = setting.data.key.children;
  2120. if (node.chkDisabled != chkDisabled) {
  2121. node.chkDisabled = chkDisabled;
  2122. }
  2123. view.repairChkClass(setting, node);
  2124. if (node[childKey] && inherit) {
  2125. for (var i = 0, l = node[childKey].length; i < l; i++) {
  2126. var sNode = node[childKey][i];
  2127. view.repairSonChkDisabled(setting, sNode, chkDisabled, inherit);
  2128. }
  2129. }
  2130. },
  2131. repairParentChkDisabled: function(setting, node, chkDisabled, inherit) {
  2132. if (!node) return;
  2133. if (node.chkDisabled != chkDisabled && inherit) {
  2134. node.chkDisabled = chkDisabled;
  2135. }
  2136. view.repairChkClass(setting, node);
  2137. view.repairParentChkDisabled(setting, node.getParentNode(), chkDisabled, inherit);
  2138. },
  2139. setChkClass: function(setting, obj, node) {
  2140. if (!obj) return;
  2141. if (node.nocheck === true) {
  2142. obj.hide();
  2143. } else {
  2144. obj.show();
  2145. }
  2146. obj.attr('class', view.makeChkClass(setting, node));
  2147. },
  2148. setParentNodeCheckBox: function(setting, node, value, srcNode) {
  2149. var childKey = setting.data.key.children,
  2150. checkedKey = setting.data.key.checked,
  2151. checkObj = $$(node, consts.id.CHECK, setting);
  2152. if (!srcNode) srcNode = node;
  2153. data.makeChkFlag(setting, node);
  2154. if (node.nocheck !== true && node.chkDisabled !== true) {
  2155. node[checkedKey] = value;
  2156. view.setChkClass(setting, checkObj, node);
  2157. if (setting.check.autoCheckTrigger && node != srcNode) {
  2158. setting.treeObj.trigger(consts.event.CHECK, [null, setting.treeId, node]);
  2159. }
  2160. }
  2161. if (node.parentTId) {
  2162. var pSign = true;
  2163. if (!value) {
  2164. var pNodes = node.getParentNode()[childKey];
  2165. for (var i = 0, l = pNodes.length; i < l; i++) {
  2166. if ((pNodes[i].nocheck !== true && pNodes[i].chkDisabled !== true && pNodes[i][checkedKey])
  2167. || ((pNodes[i].nocheck === true || pNodes[i].chkDisabled === true) && pNodes[i].check_Child_State > 0)) {
  2168. pSign = false;
  2169. break;
  2170. }
  2171. }
  2172. }
  2173. if (pSign) {
  2174. view.setParentNodeCheckBox(setting, node.getParentNode(), value, srcNode);
  2175. }
  2176. }
  2177. },
  2178. setSonNodeCheckBox: function(setting, node, value, srcNode) {
  2179. if (!node) return;
  2180. var childKey = setting.data.key.children,
  2181. checkedKey = setting.data.key.checked,
  2182. checkObj = $$(node, consts.id.CHECK, setting);
  2183. if (!srcNode) srcNode = node;
  2184. var hasDisable = false;
  2185. if (node[childKey]) {
  2186. for (var i = 0, l = node[childKey].length; i < l && node.chkDisabled !== true; i++) {
  2187. var sNode = node[childKey][i];
  2188. view.setSonNodeCheckBox(setting, sNode, value, srcNode);
  2189. if (sNode.chkDisabled === true) hasDisable = true;
  2190. }
  2191. }
  2192. if (node != data.getRoot(setting) && node.chkDisabled !== true) {
  2193. if (hasDisable && node.nocheck !== true) {
  2194. data.makeChkFlag(setting, node);
  2195. }
  2196. if (node.nocheck !== true && node.chkDisabled !== true) {
  2197. node[checkedKey] = value;
  2198. if (!hasDisable) node.check_Child_State = (node[childKey] && node[childKey].length > 0) ? (value ? 2 : 0) : -1;
  2199. } else {
  2200. node.check_Child_State = -1;
  2201. }
  2202. view.setChkClass(setting, checkObj, node);
  2203. if (setting.check.autoCheckTrigger && node != srcNode && node.nocheck !== true && node.chkDisabled !== true) {
  2204. setting.treeObj.trigger(consts.event.CHECK, [null, setting.treeId, node]);
  2205. }
  2206. }
  2207. }
  2208. },
  2209. _z = {
  2210. tools: _tools,
  2211. view: _view,
  2212. event: _event,
  2213. data: _data
  2214. };
  2215. $.extend(true, $.fn.zTree.consts, _consts);
  2216. $.extend(true, $.fn.zTree._z, _z);
  2217. var zt = $.fn.zTree,
  2218. tools = zt._z.tools,
  2219. consts = zt.consts,
  2220. view = zt._z.view,
  2221. data = zt._z.data,
  2222. event = zt._z.event,
  2223. $$ = tools.$;
  2224. data.exSetting(_setting);
  2225. data.addInitBind(_bindEvent);
  2226. data.addInitUnBind(_unbindEvent);
  2227. data.addInitCache(_initCache);
  2228. data.addInitNode(_initNode);
  2229. data.addInitProxy(_eventProxy, true);
  2230. data.addInitRoot(_initRoot);
  2231. data.addBeforeA(_beforeA);
  2232. data.addZTreeTools(_zTreeTools);
  2233. var _createNodes = view.createNodes;
  2234. view.createNodes = function(setting, level, nodes, parentNode) {
  2235. if (_createNodes) _createNodes.apply(view, arguments);
  2236. if (!nodes) return;
  2237. view.repairParentChkClassWithSelf(setting, parentNode);
  2238. }
  2239. var _removeNode = view.removeNode;
  2240. view.removeNode = function(setting, node) {
  2241. var parentNode = node.getParentNode();
  2242. if (_removeNode) _removeNode.apply(view, arguments);
  2243. if (!node || !parentNode) return;
  2244. view.repairChkClass(setting, parentNode);
  2245. view.repairParentChkClass(setting, parentNode);
  2246. }
  2247. var _appendNodes = view.appendNodes;
  2248. view.appendNodes = function(setting, level, nodes, parentNode, initFlag, openFlag) {
  2249. var html = "";
  2250. if (_appendNodes) {
  2251. html = _appendNodes.apply(view, arguments);
  2252. }
  2253. if (parentNode) {
  2254. data.makeChkFlag(setting, parentNode);
  2255. }
  2256. return html;
  2257. }
  2258. })(jQuery);
  2259. /*
  2260. * JQuery zTree exedit v3.5.16-beta.1
  2261. * http://zTree.me/
  2262. *
  2263. * Copyright (c) 2010 Hunter.z
  2264. *
  2265. * Licensed same as jquery - MIT License
  2266. * http://www.opensource.org/licenses/mit-license.php
  2267. *
  2268. * email: hunter.z@263.net
  2269. * Date: 2014-03-28
  2270. */
  2271. (function($){
  2272. //default consts of exedit
  2273. var _consts = {
  2274. event: {
  2275. DRAG: "ztree_drag",
  2276. DROP: "ztree_drop",
  2277. RENAME: "ztree_rename",
  2278. DRAGMOVE:"ztree_dragmove"
  2279. },
  2280. id: {
  2281. EDIT: "_edit",
  2282. INPUT: "_input",
  2283. REMOVE: "_remove"
  2284. },
  2285. move: {
  2286. TYPE_INNER: "inner",
  2287. TYPE_PREV: "prev",
  2288. TYPE_NEXT: "next"
  2289. },
  2290. node: {
  2291. CURSELECTED_EDIT: "curSelectedNode_Edit",
  2292. TMPTARGET_TREE: "tmpTargetzTree",
  2293. TMPTARGET_NODE: "tmpTargetNode"
  2294. }
  2295. },
  2296. //default setting of exedit
  2297. _setting = {
  2298. edit: {
  2299. enable: false,
  2300. editNameSelectAll: false,
  2301. showRemoveBtn: true,
  2302. showRenameBtn: true,
  2303. removeTitle: "remove",
  2304. renameTitle: "rename",
  2305. drag: {
  2306. autoExpandTrigger: false,
  2307. isCopy: true,
  2308. isMove: true,
  2309. prev: true,
  2310. next: true,
  2311. inner: true,
  2312. minMoveSize: 5,
  2313. borderMax: 10,
  2314. borderMin: -5,
  2315. maxShowNodeNum: 5,
  2316. autoOpenTime: 500
  2317. }
  2318. },
  2319. view: {
  2320. addHoverDom: null,
  2321. removeHoverDom: null
  2322. },
  2323. callback: {
  2324. beforeDrag:null,
  2325. beforeDragOpen:null,
  2326. beforeDrop:null,
  2327. beforeEditName:null,
  2328. beforeRename:null,
  2329. onDrag:null,
  2330. onDragMove:null,
  2331. onDrop:null,
  2332. onRename:null
  2333. }
  2334. },
  2335. //default root of exedit
  2336. _initRoot = function (setting) {
  2337. var r = data.getRoot(setting), rs = data.getRoots();
  2338. r.curEditNode = null;
  2339. r.curEditInput = null;
  2340. r.curHoverNode = null;
  2341. r.dragFlag = 0;
  2342. r.dragNodeShowBefore = [];
  2343. r.dragMaskList = new Array();
  2344. rs.showHoverDom = true;
  2345. },
  2346. //default cache of exedit
  2347. _initCache = function(treeId) {},
  2348. //default bind event of exedit
  2349. _bindEvent = function(setting) {
  2350. var o = setting.treeObj;
  2351. var c = consts.event;
  2352. o.bind(c.RENAME, function (event, treeId, treeNode, isCancel) {
  2353. tools.apply(setting.callback.onRename, [event, treeId, treeNode, isCancel]);
  2354. });
  2355. o.bind(c.DRAG, function (event, srcEvent, treeId, treeNodes) {
  2356. tools.apply(setting.callback.onDrag, [srcEvent, treeId, treeNodes]);
  2357. });
  2358. o.bind(c.DRAGMOVE,function(event, srcEvent, treeId, treeNodes){
  2359. tools.apply(setting.callback.onDragMove,[srcEvent, treeId, treeNodes]);
  2360. });
  2361. o.bind(c.DROP, function (event, srcEvent, treeId, treeNodes, targetNode, moveType, isCopy) {
  2362. tools.apply(setting.callback.onDrop, [srcEvent, treeId, treeNodes, targetNode, moveType, isCopy]);
  2363. });
  2364. },
  2365. _unbindEvent = function(setting) {
  2366. var o = setting.treeObj;
  2367. var c = consts.event;
  2368. o.unbind(c.RENAME);
  2369. o.unbind(c.DRAG);
  2370. o.unbind(c.DRAGMOVE);
  2371. o.unbind(c.DROP);
  2372. },
  2373. //default event proxy of exedit
  2374. _eventProxy = function(e) {
  2375. var target = e.target,
  2376. setting = data.getSetting(e.data.treeId),
  2377. relatedTarget = e.relatedTarget,
  2378. tId = "", node = null,
  2379. nodeEventType = "", treeEventType = "",
  2380. nodeEventCallback = null, treeEventCallback = null,
  2381. tmp = null;
  2382. if (tools.eqs(e.type, "mouseover")) {
  2383. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2384. if (tmp) {
  2385. tId = tools.getNodeMainDom(tmp).id;
  2386. nodeEventType = "hoverOverNode";
  2387. }
  2388. } else if (tools.eqs(e.type, "mouseout")) {
  2389. tmp = tools.getMDom(setting, relatedTarget, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2390. if (!tmp) {
  2391. tId = "remove";
  2392. nodeEventType = "hoverOutNode";
  2393. }
  2394. } else if (tools.eqs(e.type, "mousedown")) {
  2395. tmp = tools.getMDom(setting, target, [{tagName:"a", attrName:"treeNode"+consts.id.A}]);
  2396. if (tmp) {
  2397. tId = tools.getNodeMainDom(tmp).id;
  2398. nodeEventType = "mousedownNode";
  2399. }
  2400. }
  2401. if (tId.length>0) {
  2402. node = data.getNodeCache(setting, tId);
  2403. switch (nodeEventType) {
  2404. case "mousedownNode" :
  2405. nodeEventCallback = _handler.onMousedownNode;
  2406. break;
  2407. case "hoverOverNode" :
  2408. nodeEventCallback = _handler.onHoverOverNode;
  2409. break;
  2410. case "hoverOutNode" :
  2411. nodeEventCallback = _handler.onHoverOutNode;
  2412. break;
  2413. }
  2414. }
  2415. var proxyResult = {
  2416. stop: false,
  2417. node: node,
  2418. nodeEventType: nodeEventType,
  2419. nodeEventCallback: nodeEventCallback,
  2420. treeEventType: treeEventType,
  2421. treeEventCallback: treeEventCallback
  2422. };
  2423. return proxyResult
  2424. },
  2425. //default init node of exedit
  2426. _initNode = function(setting, level, n, parentNode, isFirstNode, isLastNode, openFlag) {
  2427. if (!n) return;
  2428. n.isHover = false;
  2429. n.editNameFlag = false;
  2430. },
  2431. //update zTreeObj, add method of edit
  2432. _zTreeTools = function(setting, zTreeTools) {
  2433. zTreeTools.cancelEditName = function(newName) {
  2434. var root = data.getRoot(this.setting);
  2435. if (!root.curEditNode) return;
  2436. view.cancelCurEditNode(this.setting, newName?newName:null, true);
  2437. }
  2438. zTreeTools.copyNode = function(targetNode, node, moveType, isSilent) {
  2439. if (!node) return null;
  2440. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) return null;
  2441. var _this = this,
  2442. newNode = tools.clone(node);
  2443. if (!targetNode) {
  2444. targetNode = null;
  2445. moveType = consts.move.TYPE_INNER;
  2446. }
  2447. if (moveType == consts.move.TYPE_INNER) {
  2448. function copyCallback() {
  2449. view.addNodes(_this.setting, targetNode, [newNode], isSilent);
  2450. }
  2451. if (tools.canAsync(this.setting, targetNode)) {
  2452. view.asyncNode(this.setting, targetNode, isSilent, copyCallback);
  2453. } else {
  2454. copyCallback();
  2455. }
  2456. } else {
  2457. view.addNodes(this.setting, targetNode.parentNode, [newNode], isSilent);
  2458. view.moveNode(this.setting, targetNode, newNode, moveType, false, isSilent);
  2459. }
  2460. return newNode;
  2461. }
  2462. zTreeTools.editName = function(node) {
  2463. if (!node || !node.tId || node !== data.getNodeCache(this.setting, node.tId)) return;
  2464. if (node.parentTId) view.expandCollapseParentNode(this.setting, node.getParentNode(), true);
  2465. view.editNode(this.setting, node)
  2466. }
  2467. zTreeTools.moveNode = function(targetNode, node, moveType, isSilent) {
  2468. if (!node) return node;
  2469. if (targetNode && !targetNode.isParent && this.setting.data.keep.leaf && moveType === consts.move.TYPE_INNER) {
  2470. return null;
  2471. } else if (targetNode && ((node.parentTId == targetNode.tId && moveType == consts.move.TYPE_INNER) || $$(node, this.setting).find("#" + targetNode.tId).length > 0)) {
  2472. return null;
  2473. } else if (!targetNode) {
  2474. targetNode = null;
  2475. }
  2476. var _this = this;
  2477. function moveCallback() {
  2478. view.moveNode(_this.setting, targetNode, node, moveType, false, isSilent);
  2479. }
  2480. if (tools.canAsync(this.setting, targetNode) && moveType === consts.move.TYPE_INNER) {
  2481. view.asyncNode(this.setting, targetNode, isSilent, moveCallback);
  2482. } else {
  2483. moveCallback();
  2484. }
  2485. return node;
  2486. }
  2487. zTreeTools.setEditable = function(editable) {
  2488. this.setting.edit.enable = editable;
  2489. return this.refresh();
  2490. }
  2491. },
  2492. //method of operate data
  2493. _data = {
  2494. setSonNodeLevel: function(setting, parentNode, node) {
  2495. if (!node) return;
  2496. var childKey = setting.data.key.children;
  2497. node.level = (parentNode)? parentNode.level + 1 : 0;
  2498. if (!node[childKey]) return;
  2499. for (var i = 0, l = node[childKey].length; i < l; i++) {
  2500. if (node[childKey][i]) data.setSonNodeLevel(setting, node, node[childKey][i]);
  2501. }
  2502. }
  2503. },
  2504. //method of event proxy
  2505. _event = {
  2506. },
  2507. //method of event handler
  2508. _handler = {
  2509. onHoverOverNode: function(event, node) {
  2510. var setting = data.getSetting(event.data.treeId),
  2511. root = data.getRoot(setting);
  2512. if (root.curHoverNode != node) {
  2513. _handler.onHoverOutNode(event);
  2514. }
  2515. root.curHoverNode = node;
  2516. view.addHoverDom(setting, node);
  2517. },
  2518. onHoverOutNode: function(event, node) {
  2519. var setting = data.getSetting(event.data.treeId),
  2520. root = data.getRoot(setting);
  2521. if (root.curHoverNode && !data.isSelectedNode(setting, root.curHoverNode)) {
  2522. view.removeTreeDom(setting, root.curHoverNode);
  2523. root.curHoverNode = null;
  2524. }
  2525. },
  2526. onMousedownNode: function(eventMouseDown, _node) {
  2527. var i,l,
  2528. setting = data.getSetting(eventMouseDown.data.treeId),
  2529. root = data.getRoot(setting), roots = data.getRoots();
  2530. //right click can't drag & drop
  2531. if (eventMouseDown.button == 2 || !setting.edit.enable || (!setting.edit.drag.isCopy && !setting.edit.drag.isMove)) return true;
  2532. //input of edit node name can't drag & drop
  2533. var target = eventMouseDown.target,
  2534. _nodes = data.getRoot(setting).curSelectedList,
  2535. nodes = [];
  2536. if (!data.isSelectedNode(setting, _node)) {
  2537. nodes = [_node];
  2538. } else {
  2539. for (i=0, l=_nodes.length; i<l; i++) {
  2540. if (_nodes[i].editNameFlag && tools.eqs(target.tagName, "input") && target.getAttribute("treeNode"+consts.id.INPUT) !== null) {
  2541. return true;
  2542. }
  2543. nodes.push(_nodes[i]);
  2544. if (nodes[0].parentTId !== _nodes[i].parentTId) {
  2545. nodes = [_node];
  2546. break;
  2547. }
  2548. }
  2549. }
  2550. view.editNodeBlur = true;
  2551. view.cancelCurEditNode(setting);
  2552. var doc = $(setting.treeObj.get(0).ownerDocument),
  2553. body = $(setting.treeObj.get(0).ownerDocument.body), curNode, tmpArrow, tmpTarget,
  2554. isOtherTree = false,
  2555. targetSetting = setting,
  2556. sourceSetting = setting,
  2557. preNode, nextNode,
  2558. preTmpTargetNodeId = null,
  2559. preTmpMoveType = null,
  2560. tmpTargetNodeId = null,
  2561. moveType = consts.move.TYPE_INNER,
  2562. mouseDownX = eventMouseDown.clientX,
  2563. mouseDownY = eventMouseDown.clientY,
  2564. startTime = (new Date()).getTime();
  2565. if (tools.uCanDo(setting)) {
  2566. doc.bind("mousemove", _docMouseMove);
  2567. }
  2568. function _docMouseMove(event) {
  2569. //avoid start drag after click node
  2570. if (root.dragFlag == 0 && Math.abs(mouseDownX - event.clientX) < setting.edit.drag.minMoveSize
  2571. && Math.abs(mouseDownY - event.clientY) < setting.edit.drag.minMoveSize) {
  2572. return true;
  2573. }
  2574. var i, l, tmpNode, tmpDom, tmpNodes,
  2575. childKey = setting.data.key.children;
  2576. body.css("cursor", "pointer");
  2577. if (root.dragFlag == 0) {
  2578. if (tools.apply(setting.callback.beforeDrag, [setting.treeId, nodes], true) == false) {
  2579. _docMouseUp(event);
  2580. return true;
  2581. }
  2582. for (i=0, l=nodes.length; i<l; i++) {
  2583. if (i==0) {
  2584. root.dragNodeShowBefore = [];
  2585. }
  2586. tmpNode = nodes[i];
  2587. if (tmpNode.isParent && tmpNode.open) {
  2588. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  2589. root.dragNodeShowBefore[tmpNode.tId] = true;
  2590. } else {
  2591. root.dragNodeShowBefore[tmpNode.tId] = false;
  2592. }
  2593. }
  2594. root.dragFlag = 1;
  2595. roots.showHoverDom = false;
  2596. tools.showIfameMask(setting, true);
  2597. //sort
  2598. var isOrder = true, lastIndex = -1;
  2599. if (nodes.length>1) {
  2600. var pNodes = nodes[0].parentTId ? nodes[0].getParentNode()[childKey] : data.getNodes(setting);
  2601. tmpNodes = [];
  2602. for (i=0, l=pNodes.length; i<l; i++) {
  2603. if (root.dragNodeShowBefore[pNodes[i].tId] !== undefined) {
  2604. if (isOrder && lastIndex > -1 && (lastIndex+1) !== i) {
  2605. isOrder = false;
  2606. }
  2607. tmpNodes.push(pNodes[i]);
  2608. lastIndex = i;
  2609. }
  2610. if (nodes.length === tmpNodes.length) {
  2611. nodes = tmpNodes;
  2612. break;
  2613. }
  2614. }
  2615. }
  2616. if (isOrder) {
  2617. preNode = nodes[0].getPreNode();
  2618. nextNode = nodes[nodes.length-1].getNextNode();
  2619. }
  2620. //set node in selected
  2621. curNode = $$("<ul class='zTreeDragUL'></ul>", setting);
  2622. for (i=0, l=nodes.length; i<l; i++) {
  2623. tmpNode = nodes[i];
  2624. tmpNode.editNameFlag = false;
  2625. view.selectNode(setting, tmpNode, i>0);
  2626. view.removeTreeDom(setting, tmpNode);
  2627. if (i > setting.edit.drag.maxShowNodeNum-1) {
  2628. continue;
  2629. }
  2630. tmpDom = $$("<li id='"+ tmpNode.tId +"_tmp'></li>", setting);
  2631. tmpDom.append($$(tmpNode, consts.id.A, setting).clone());
  2632. tmpDom.css("padding", "0");
  2633. tmpDom.children("#" + tmpNode.tId + consts.id.A).removeClass(consts.node.CURSELECTED);
  2634. curNode.append(tmpDom);
  2635. if (i == setting.edit.drag.maxShowNodeNum-1) {
  2636. tmpDom = $$("<li id='"+ tmpNode.tId +"_moretmp'><a> ... </a></li>", setting);
  2637. curNode.append(tmpDom);
  2638. }
  2639. }
  2640. curNode.attr("id", nodes[0].tId + consts.id.UL + "_tmp");
  2641. curNode.addClass(setting.treeObj.attr("class"));
  2642. curNode.appendTo(body);
  2643. tmpArrow = $$("<span class='tmpzTreeMove_arrow'></span>", setting);
  2644. tmpArrow.attr("id", "zTreeMove_arrow_tmp");
  2645. tmpArrow.appendTo(body);
  2646. setting.treeObj.trigger(consts.event.DRAG, [event, setting.treeId, nodes]);
  2647. }
  2648. if (root.dragFlag == 1) {
  2649. if (tmpTarget && tmpArrow.attr("id") == event.target.id && tmpTargetNodeId && (event.clientX + doc.scrollLeft()+2) > ($("#" + tmpTargetNodeId + consts.id.A, tmpTarget).offset().left)) {
  2650. var xT = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget);
  2651. event.target = (xT.length > 0) ? xT.get(0) : event.target;
  2652. } else if (tmpTarget) {
  2653. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  2654. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  2655. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  2656. }
  2657. tmpTarget = null;
  2658. tmpTargetNodeId = null;
  2659. //judge drag & drop in multi ztree
  2660. isOtherTree = false;
  2661. targetSetting = setting;
  2662. var settings = data.getSettings();
  2663. for (var s in settings) {
  2664. if (settings[s].treeId && settings[s].edit.enable && settings[s].treeId != setting.treeId
  2665. && (event.target.id == settings[s].treeId || $(event.target).parents("#" + settings[s].treeId).length>0)) {
  2666. isOtherTree = true;
  2667. targetSetting = settings[s];
  2668. }
  2669. }
  2670. var docScrollTop = doc.scrollTop(),
  2671. docScrollLeft = doc.scrollLeft(),
  2672. treeOffset = targetSetting.treeObj.offset(),
  2673. scrollHeight = targetSetting.treeObj.get(0).scrollHeight,
  2674. scrollWidth = targetSetting.treeObj.get(0).scrollWidth,
  2675. dTop = (event.clientY + docScrollTop - treeOffset.top),
  2676. dBottom = (targetSetting.treeObj.height() + treeOffset.top - event.clientY - docScrollTop),
  2677. dLeft = (event.clientX + docScrollLeft - treeOffset.left),
  2678. dRight = (targetSetting.treeObj.width() + treeOffset.left - event.clientX - docScrollLeft),
  2679. isTop = (dTop < setting.edit.drag.borderMax && dTop > setting.edit.drag.borderMin),
  2680. isBottom = (dBottom < setting.edit.drag.borderMax && dBottom > setting.edit.drag.borderMin),
  2681. isLeft = (dLeft < setting.edit.drag.borderMax && dLeft > setting.edit.drag.borderMin),
  2682. isRight = (dRight < setting.edit.drag.borderMax && dRight > setting.edit.drag.borderMin),
  2683. isTreeInner = dTop > setting.edit.drag.borderMin && dBottom > setting.edit.drag.borderMin && dLeft > setting.edit.drag.borderMin && dRight > setting.edit.drag.borderMin,
  2684. isTreeTop = (isTop && targetSetting.treeObj.scrollTop() <= 0),
  2685. isTreeBottom = (isBottom && (targetSetting.treeObj.scrollTop() + targetSetting.treeObj.height()+10) >= scrollHeight),
  2686. isTreeLeft = (isLeft && targetSetting.treeObj.scrollLeft() <= 0),
  2687. isTreeRight = (isRight && (targetSetting.treeObj.scrollLeft() + targetSetting.treeObj.width()+10) >= scrollWidth);
  2688. if (event.target && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  2689. //get node <li> dom
  2690. var targetObj = event.target;
  2691. while (targetObj && targetObj.tagName && !tools.eqs(targetObj.tagName, "li") && targetObj.id != targetSetting.treeId) {
  2692. targetObj = targetObj.parentNode;
  2693. }
  2694. var canMove = true;
  2695. //don't move to self or children of self
  2696. for (i=0, l=nodes.length; i<l; i++) {
  2697. tmpNode = nodes[i];
  2698. if (targetObj.id === tmpNode.tId) {
  2699. canMove = false;
  2700. break;
  2701. } else if ($$(tmpNode, setting).find("#" + targetObj.id).length > 0) {
  2702. canMove = false;
  2703. break;
  2704. }
  2705. }
  2706. if (canMove && event.target && tools.isChildOrSelf(event.target, targetObj.id + consts.id.A)) {
  2707. tmpTarget = $(targetObj);
  2708. tmpTargetNodeId = targetObj.id;
  2709. }
  2710. }
  2711. //the mouse must be in zTree
  2712. tmpNode = nodes[0];
  2713. if (isTreeInner && tools.isChildOrSelf(event.target, targetSetting.treeId)) {
  2714. //judge mouse move in root of ztree
  2715. if (!tmpTarget && (event.target.id == targetSetting.treeId || isTreeTop || isTreeBottom || isTreeLeft || isTreeRight) && (isOtherTree || (!isOtherTree && tmpNode.parentTId))) {
  2716. tmpTarget = targetSetting.treeObj;
  2717. }
  2718. //auto scroll top
  2719. if (isTop) {
  2720. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()-10);
  2721. } else if (isBottom) {
  2722. targetSetting.treeObj.scrollTop(targetSetting.treeObj.scrollTop()+10);
  2723. }
  2724. if (isLeft) {
  2725. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()-10);
  2726. } else if (isRight) {
  2727. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+10);
  2728. }
  2729. //auto scroll left
  2730. if (tmpTarget && tmpTarget != targetSetting.treeObj && tmpTarget.offset().left < targetSetting.treeObj.offset().left) {
  2731. targetSetting.treeObj.scrollLeft(targetSetting.treeObj.scrollLeft()+ tmpTarget.offset().left - targetSetting.treeObj.offset().left);
  2732. }
  2733. }
  2734. curNode.css({
  2735. "top": (event.clientY + docScrollTop + 3) + "px",
  2736. "left": (event.clientX + docScrollLeft + 3) + "px"
  2737. });
  2738. var dX = 0;
  2739. var dY = 0;
  2740. if (tmpTarget && tmpTarget.attr("id")!=targetSetting.treeId) {
  2741. var tmpTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId),
  2742. isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy),
  2743. isPrev = !!(preNode && tmpTargetNodeId === preNode.tId),
  2744. isNext = !!(nextNode && tmpTargetNodeId === nextNode.tId),
  2745. isInner = (tmpNode.parentTId && tmpNode.parentTId == tmpTargetNodeId),
  2746. canPrev = (isCopy || !isNext) && tools.apply(targetSetting.edit.drag.prev, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.prev),
  2747. canNext = (isCopy || !isPrev) && tools.apply(targetSetting.edit.drag.next, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.next),
  2748. canInner = (isCopy || !isInner) && !(targetSetting.data.keep.leaf && !tmpTargetNode.isParent) && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, tmpTargetNode], !!targetSetting.edit.drag.inner);
  2749. if (!canPrev && !canNext && !canInner) {
  2750. tmpTarget = null;
  2751. tmpTargetNodeId = "";
  2752. moveType = consts.move.TYPE_INNER;
  2753. tmpArrow.css({
  2754. "display":"none"
  2755. });
  2756. if (window.zTreeMoveTimer) {
  2757. clearTimeout(window.zTreeMoveTimer);
  2758. window.zTreeMoveTargetNodeTId = null
  2759. }
  2760. } else {
  2761. var tmpTargetA = $("#" + tmpTargetNodeId + consts.id.A, tmpTarget),
  2762. tmpNextA = tmpTargetNode.isLastNode ? null : $("#" + tmpTargetNode.getNextNode().tId + consts.id.A, tmpTarget.next()),
  2763. tmpTop = tmpTargetA.offset().top,
  2764. tmpLeft = tmpTargetA.offset().left,
  2765. prevPercent = canPrev ? (canInner ? 0.25 : (canNext ? 0.5 : 1) ) : -1,
  2766. nextPercent = canNext ? (canInner ? 0.75 : (canPrev ? 0.5 : 0) ) : -1,
  2767. dY_percent = (event.clientY + docScrollTop - tmpTop)/tmpTargetA.height();
  2768. if ((prevPercent==1 ||dY_percent<=prevPercent && dY_percent>=-.2) && canPrev) {
  2769. dX = 1 - tmpArrow.width();
  2770. dY = tmpTop - tmpArrow.height()/2;
  2771. moveType = consts.move.TYPE_PREV;
  2772. } else if ((nextPercent==0 || dY_percent>=nextPercent && dY_percent<=1.2) && canNext) {
  2773. dX = 1 - tmpArrow.width();
  2774. dY = (tmpNextA == null || (tmpTargetNode.isParent && tmpTargetNode.open)) ? (tmpTop + tmpTargetA.height() - tmpArrow.height()/2) : (tmpNextA.offset().top - tmpArrow.height()/2);
  2775. moveType = consts.move.TYPE_NEXT;
  2776. }else {
  2777. dX = 5 - tmpArrow.width();
  2778. dY = tmpTop;
  2779. moveType = consts.move.TYPE_INNER;
  2780. }
  2781. tmpArrow.css({
  2782. "display":"block",
  2783. "top": dY + "px",
  2784. "left": (tmpLeft + dX) + "px"
  2785. });
  2786. tmpTargetA.addClass(consts.node.TMPTARGET_NODE + "_" + moveType);
  2787. if (preTmpTargetNodeId != tmpTargetNodeId || preTmpMoveType != moveType) {
  2788. startTime = (new Date()).getTime();
  2789. }
  2790. if (tmpTargetNode && tmpTargetNode.isParent && moveType == consts.move.TYPE_INNER) {
  2791. var startTimer = true;
  2792. if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId !== tmpTargetNode.tId) {
  2793. clearTimeout(window.zTreeMoveTimer);
  2794. window.zTreeMoveTargetNodeTId = null;
  2795. }else if (window.zTreeMoveTimer && window.zTreeMoveTargetNodeTId === tmpTargetNode.tId) {
  2796. startTimer = false;
  2797. }
  2798. if (startTimer) {
  2799. window.zTreeMoveTimer = setTimeout(function() {
  2800. if (moveType != consts.move.TYPE_INNER) return;
  2801. if (tmpTargetNode && tmpTargetNode.isParent && !tmpTargetNode.open && (new Date()).getTime() - startTime > targetSetting.edit.drag.autoOpenTime
  2802. && tools.apply(targetSetting.callback.beforeDragOpen, [targetSetting.treeId, tmpTargetNode], true)) {
  2803. view.switchNode(targetSetting, tmpTargetNode);
  2804. if (targetSetting.edit.drag.autoExpandTrigger) {
  2805. targetSetting.treeObj.trigger(consts.event.EXPAND, [targetSetting.treeId, tmpTargetNode]);
  2806. }
  2807. }
  2808. }, targetSetting.edit.drag.autoOpenTime+50);
  2809. window.zTreeMoveTargetNodeTId = tmpTargetNode.tId;
  2810. }
  2811. }
  2812. }
  2813. } else {
  2814. moveType = consts.move.TYPE_INNER;
  2815. if (tmpTarget && tools.apply(targetSetting.edit.drag.inner, [targetSetting.treeId, nodes, null], !!targetSetting.edit.drag.inner)) {
  2816. tmpTarget.addClass(consts.node.TMPTARGET_TREE);
  2817. } else {
  2818. tmpTarget = null;
  2819. }
  2820. tmpArrow.css({
  2821. "display":"none"
  2822. });
  2823. if (window.zTreeMoveTimer) {
  2824. clearTimeout(window.zTreeMoveTimer);
  2825. window.zTreeMoveTargetNodeTId = null;
  2826. }
  2827. }
  2828. preTmpTargetNodeId = tmpTargetNodeId;
  2829. preTmpMoveType = moveType;
  2830. setting.treeObj.trigger(consts.event.DRAGMOVE, [event, setting.treeId, nodes]);
  2831. }
  2832. return false;
  2833. }
  2834. doc.bind("mouseup", _docMouseUp);
  2835. function _docMouseUp(event) {
  2836. if (window.zTreeMoveTimer) {
  2837. clearTimeout(window.zTreeMoveTimer);
  2838. window.zTreeMoveTargetNodeTId = null;
  2839. }
  2840. preTmpTargetNodeId = null;
  2841. preTmpMoveType = null;
  2842. doc.unbind("mousemove", _docMouseMove);
  2843. doc.unbind("mouseup", _docMouseUp);
  2844. doc.unbind("selectstart", _docSelect);
  2845. body.css("cursor", "auto");
  2846. if (tmpTarget) {
  2847. tmpTarget.removeClass(consts.node.TMPTARGET_TREE);
  2848. if (tmpTargetNodeId) $("#" + tmpTargetNodeId + consts.id.A, tmpTarget).removeClass(consts.node.TMPTARGET_NODE + "_" + consts.move.TYPE_PREV)
  2849. .removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_NEXT).removeClass(consts.node.TMPTARGET_NODE + "_" + _consts.move.TYPE_INNER);
  2850. }
  2851. tools.showIfameMask(setting, false);
  2852. roots.showHoverDom = true;
  2853. if (root.dragFlag == 0) return;
  2854. root.dragFlag = 0;
  2855. var i, l, tmpNode;
  2856. for (i=0, l=nodes.length; i<l; i++) {
  2857. tmpNode = nodes[i];
  2858. if (tmpNode.isParent && root.dragNodeShowBefore[tmpNode.tId] && !tmpNode.open) {
  2859. view.expandCollapseNode(setting, tmpNode, !tmpNode.open);
  2860. delete root.dragNodeShowBefore[tmpNode.tId];
  2861. }
  2862. }
  2863. if (curNode) curNode.remove();
  2864. if (tmpArrow) tmpArrow.remove();
  2865. var isCopy = ((event.ctrlKey || event.metaKey) && setting.edit.drag.isMove && setting.edit.drag.isCopy) || (!setting.edit.drag.isMove && setting.edit.drag.isCopy);
  2866. if (!isCopy && tmpTarget && tmpTargetNodeId && nodes[0].parentTId && tmpTargetNodeId==nodes[0].parentTId && moveType == consts.move.TYPE_INNER) {
  2867. tmpTarget = null;
  2868. }
  2869. if (tmpTarget) {
  2870. var dragTargetNode = tmpTargetNodeId == null ? null: data.getNodeCache(targetSetting, tmpTargetNodeId);
  2871. if (tools.apply(setting.callback.beforeDrop, [targetSetting.treeId, nodes, dragTargetNode, moveType, isCopy], true) == false) {
  2872. view.selectNodes(sourceSetting, nodes);
  2873. return;
  2874. }
  2875. var newNodes = isCopy ? tools.clone(nodes) : nodes;
  2876. function dropCallback() {
  2877. if (isOtherTree) {
  2878. if (!isCopy) {
  2879. for(var i=0, l=nodes.length; i<l; i++) {
  2880. view.removeNode(setting, nodes[i]);
  2881. }
  2882. }
  2883. if (moveType == consts.move.TYPE_INNER) {
  2884. view.addNodes(targetSetting, dragTargetNode, newNodes);
  2885. } else {
  2886. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  2887. if (moveType == consts.move.TYPE_PREV) {
  2888. for (i=0, l=newNodes.length; i<l; i++) {
  2889. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  2890. }
  2891. } else {
  2892. for (i=-1, l=newNodes.length-1; i<l; l--) {
  2893. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  2894. }
  2895. }
  2896. }
  2897. } else {
  2898. if (isCopy && moveType == consts.move.TYPE_INNER) {
  2899. view.addNodes(targetSetting, dragTargetNode, newNodes);
  2900. } else {
  2901. if (isCopy) {
  2902. view.addNodes(targetSetting, dragTargetNode.getParentNode(), newNodes);
  2903. }
  2904. if (moveType != consts.move.TYPE_NEXT) {
  2905. for (i=0, l=newNodes.length; i<l; i++) {
  2906. view.moveNode(targetSetting, dragTargetNode, newNodes[i], moveType, false);
  2907. }
  2908. } else {
  2909. for (i=-1, l=newNodes.length-1; i<l; l--) {
  2910. view.moveNode(targetSetting, dragTargetNode, newNodes[l], moveType, false);
  2911. }
  2912. }
  2913. }
  2914. }
  2915. view.selectNodes(targetSetting, newNodes);
  2916. $$(newNodes[0], setting).focus().blur();
  2917. setting.treeObj.trigger(consts.event.DROP, [event, targetSetting.treeId, newNodes, dragTargetNode, moveType, isCopy]);
  2918. }
  2919. if (moveType == consts.move.TYPE_INNER && tools.canAsync(targetSetting, dragTargetNode)) {
  2920. view.asyncNode(targetSetting, dragTargetNode, false, dropCallback);
  2921. } else {
  2922. dropCallback();
  2923. }
  2924. } else {
  2925. view.selectNodes(sourceSetting, nodes);
  2926. setting.treeObj.trigger(consts.event.DROP, [event, setting.treeId, nodes, null, null, null]);
  2927. }
  2928. }
  2929. doc.bind("selectstart", _docSelect);
  2930. function _docSelect() {
  2931. return false;
  2932. }
  2933. //Avoid FireFox's Bug
  2934. //If zTree Div CSS set 'overflow', so drag node outside of zTree, and event.target is error.
  2935. if(eventMouseDown.preventDefault) {
  2936. eventMouseDown.preventDefault();
  2937. }
  2938. return true;
  2939. }
  2940. },
  2941. //method of tools for zTree
  2942. _tools = {
  2943. getAbs: function (obj) {
  2944. var oRect = obj.getBoundingClientRect(),
  2945. scrollTop = document.body.scrollTop+document.documentElement.scrollTop,
  2946. scrollLeft = document.body.scrollLeft+document.documentElement.scrollLeft;
  2947. return [oRect.left+scrollLeft,oRect.top+scrollTop];
  2948. },
  2949. inputFocus: function(inputObj) {
  2950. if (inputObj.get(0)) {
  2951. inputObj.focus();
  2952. tools.setCursorPosition(inputObj.get(0), inputObj.val().length);
  2953. }
  2954. },
  2955. inputSelect: function(inputObj) {
  2956. if (inputObj.get(0)) {
  2957. inputObj.focus();
  2958. inputObj.select();
  2959. }
  2960. },
  2961. setCursorPosition: function(obj, pos){
  2962. if(obj.setSelectionRange) {
  2963. obj.focus();
  2964. obj.setSelectionRange(pos,pos);
  2965. } else if (obj.createTextRange) {
  2966. var range = obj.createTextRange();
  2967. range.collapse(true);
  2968. range.moveEnd('character', pos);
  2969. range.moveStart('character', pos);
  2970. range.select();
  2971. }
  2972. },
  2973. showIfameMask: function(setting, showSign) {
  2974. var root = data.getRoot(setting);
  2975. //clear full mask
  2976. while (root.dragMaskList.length > 0) {
  2977. root.dragMaskList[0].remove();
  2978. root.dragMaskList.shift();
  2979. }
  2980. if (showSign) {
  2981. //show mask
  2982. var iframeList = $$("iframe", setting);
  2983. for (var i = 0, l = iframeList.length; i < l; i++) {
  2984. var obj = iframeList.get(i),
  2985. r = tools.getAbs(obj),
  2986. dragMask = $$("<div id='zTreeMask_" + i + "' class='zTreeMask' style='top:" + r[1] + "px; left:" + r[0] + "px; width:" + obj.offsetWidth + "px; height:" + obj.offsetHeight + "px;'></div>", setting);
  2987. dragMask.appendTo($$("body", setting));
  2988. root.dragMaskList.push(dragMask);
  2989. }
  2990. }
  2991. }
  2992. },
  2993. //method of operate ztree dom
  2994. _view = {
  2995. addEditBtn: function(setting, node) {
  2996. if (node.editNameFlag || $$(node, consts.id.EDIT, setting).length > 0) {
  2997. return;
  2998. }
  2999. if (!tools.apply(setting.edit.showRenameBtn, [setting.treeId, node], setting.edit.showRenameBtn)) {
  3000. return;
  3001. }
  3002. var aObj = $$(node, consts.id.A, setting),
  3003. editStr = "<span class='" + consts.className.BUTTON + " edit' id='" + node.tId + consts.id.EDIT + "' title='"+tools.apply(setting.edit.renameTitle, [setting.treeId, node], setting.edit.renameTitle)+"' treeNode"+consts.id.EDIT+" style='display:none;'></span>";
  3004. aObj.append(editStr);
  3005. $$(node, consts.id.EDIT, setting).bind('click',
  3006. function() {
  3007. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeEditName, [setting.treeId, node], true) == false) return false;
  3008. view.editNode(setting, node);
  3009. return false;
  3010. }
  3011. ).show();
  3012. },
  3013. addRemoveBtn: function(setting, node) {
  3014. if (node.editNameFlag || $$(node, consts.id.REMOVE, setting).length > 0) {
  3015. return;
  3016. }
  3017. if (!tools.apply(setting.edit.showRemoveBtn, [setting.treeId, node], setting.edit.showRemoveBtn)) {
  3018. return;
  3019. }
  3020. var aObj = $$(node, consts.id.A, setting),
  3021. removeStr = "<span class='" + consts.className.BUTTON + " remove' id='" + node.tId + consts.id.REMOVE + "' title='"+tools.apply(setting.edit.removeTitle, [setting.treeId, node], setting.edit.removeTitle)+"' treeNode"+consts.id.REMOVE+" style='display:none;'></span>";
  3022. aObj.append(removeStr);
  3023. $$(node, consts.id.REMOVE, setting).bind('click',
  3024. function() {
  3025. if (!tools.uCanDo(setting) || tools.apply(setting.callback.beforeRemove, [setting.treeId, node], true) == false) return false;
  3026. view.removeNode(setting, node);
  3027. setting.treeObj.trigger(consts.event.REMOVE, [setting.treeId, node]);
  3028. return false;
  3029. }
  3030. ).bind('mousedown',
  3031. function(eventMouseDown) {
  3032. return true;
  3033. }
  3034. ).show();
  3035. },
  3036. addHoverDom: function(setting, node) {
  3037. if (data.getRoots().showHoverDom) {
  3038. node.isHover = true;
  3039. if (setting.edit.enable) {
  3040. view.addEditBtn(setting, node);
  3041. view.addRemoveBtn(setting, node);
  3042. }
  3043. tools.apply(setting.view.addHoverDom, [setting.treeId, node]);
  3044. }
  3045. },
  3046. cancelCurEditNode: function (setting, forceName, isCancel) {
  3047. var root = data.getRoot(setting),
  3048. nameKey = setting.data.key.name,
  3049. node = root.curEditNode;
  3050. if (node) {
  3051. var inputObj = root.curEditInput,
  3052. newName = forceName ? forceName:(isCancel ? node[nameKey]: inputObj.val());
  3053. if (tools.apply(setting.callback.beforeRename, [setting.treeId, node, newName, isCancel], true) === false) {
  3054. return false;
  3055. } else {
  3056. node[nameKey] = newName;
  3057. setting.treeObj.trigger(consts.event.RENAME, [setting.treeId, node, isCancel]);
  3058. }
  3059. var aObj = $$(node, consts.id.A, setting);
  3060. aObj.removeClass(consts.node.CURSELECTED_EDIT);
  3061. inputObj.unbind();
  3062. view.setNodeName(setting, node);
  3063. node.editNameFlag = false;
  3064. root.curEditNode = null;
  3065. root.curEditInput = null;
  3066. view.selectNode(setting, node, false);
  3067. }
  3068. root.noSelection = true;
  3069. return true;
  3070. },
  3071. editNode: function(setting, node) {
  3072. var root = data.getRoot(setting);
  3073. view.editNodeBlur = false;
  3074. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  3075. setTimeout(function() {tools.inputFocus(root.curEditInput);}, 0);
  3076. return;
  3077. }
  3078. var nameKey = setting.data.key.name;
  3079. node.editNameFlag = true;
  3080. view.removeTreeDom(setting, node);
  3081. view.cancelCurEditNode(setting);
  3082. view.selectNode(setting, node, false);
  3083. $$(node, consts.id.SPAN, setting).html("<input type=text class='rename' id='" + node.tId + consts.id.INPUT + "' treeNode" + consts.id.INPUT + " >");
  3084. var inputObj = $$(node, consts.id.INPUT, setting);
  3085. inputObj.attr("value", node[nameKey]);
  3086. if (setting.edit.editNameSelectAll) {
  3087. tools.inputSelect(inputObj);
  3088. } else {
  3089. tools.inputFocus(inputObj);
  3090. }
  3091. inputObj.bind('blur', function(event) {
  3092. if (!view.editNodeBlur) {
  3093. view.cancelCurEditNode(setting);
  3094. }
  3095. }).bind('keydown', function(event) {
  3096. if (event.keyCode=="13") {
  3097. view.editNodeBlur = true;
  3098. view.cancelCurEditNode(setting);
  3099. } else if (event.keyCode=="27") {
  3100. view.cancelCurEditNode(setting, null, true);
  3101. }
  3102. }).bind('click', function(event) {
  3103. return false;
  3104. }).bind('dblclick', function(event) {
  3105. return false;
  3106. });
  3107. $$(node, consts.id.A, setting).addClass(consts.node.CURSELECTED_EDIT);
  3108. root.curEditInput = inputObj;
  3109. root.noSelection = false;
  3110. root.curEditNode = node;
  3111. },
  3112. moveNode: function(setting, targetNode, node, moveType, animateFlag, isSilent) {
  3113. var root = data.getRoot(setting),
  3114. childKey = setting.data.key.children;
  3115. if (targetNode == node) return;
  3116. if (setting.data.keep.leaf && targetNode && !targetNode.isParent && moveType == consts.move.TYPE_INNER) return;
  3117. var oldParentNode = (node.parentTId ? node.getParentNode(): root),
  3118. targetNodeIsRoot = (targetNode === null || targetNode == root);
  3119. if (targetNodeIsRoot && targetNode === null) targetNode = root;
  3120. if (targetNodeIsRoot) moveType = consts.move.TYPE_INNER;
  3121. var targetParentNode = (targetNode.parentTId ? targetNode.getParentNode() : root);
  3122. if (moveType != consts.move.TYPE_PREV && moveType != consts.move.TYPE_NEXT) {
  3123. moveType = consts.move.TYPE_INNER;
  3124. }
  3125. if (moveType == consts.move.TYPE_INNER) {
  3126. if (targetNodeIsRoot) {
  3127. //parentTId of root node is null
  3128. node.parentTId = null;
  3129. } else {
  3130. if (!targetNode.isParent) {
  3131. targetNode.isParent = true;
  3132. targetNode.open = !!targetNode.open;
  3133. view.setNodeLineIcos(setting, targetNode);
  3134. }
  3135. node.parentTId = targetNode.tId;
  3136. }
  3137. }
  3138. //move node Dom
  3139. var targetObj, target_ulObj;
  3140. if (targetNodeIsRoot) {
  3141. targetObj = setting.treeObj;
  3142. target_ulObj = targetObj;
  3143. } else {
  3144. if (!isSilent && moveType == consts.move.TYPE_INNER) {
  3145. view.expandCollapseNode(setting, targetNode, true, false);
  3146. } else if (!isSilent) {
  3147. view.expandCollapseNode(setting, targetNode.getParentNode(), true, false);
  3148. }
  3149. targetObj = $$(targetNode, setting);
  3150. target_ulObj = $$(targetNode, consts.id.UL, setting);
  3151. if (!!targetObj.get(0) && !target_ulObj.get(0)) {
  3152. var ulstr = [];
  3153. view.makeUlHtml(setting, targetNode, ulstr, '');
  3154. targetObj.append(ulstr.join(''));
  3155. }
  3156. target_ulObj = $$(targetNode, consts.id.UL, setting);
  3157. }
  3158. var nodeDom = $$(node, setting);
  3159. if (!nodeDom.get(0)) {
  3160. nodeDom = view.appendNodes(setting, node.level, [node], null, false, true).join('');
  3161. } else if (!targetObj.get(0)) {
  3162. nodeDom.remove();
  3163. }
  3164. if (target_ulObj.get(0) && moveType == consts.move.TYPE_INNER) {
  3165. target_ulObj.append(nodeDom);
  3166. } else if (targetObj.get(0) && moveType == consts.move.TYPE_PREV) {
  3167. targetObj.before(nodeDom);
  3168. } else if (targetObj.get(0) && moveType == consts.move.TYPE_NEXT) {
  3169. targetObj.after(nodeDom);
  3170. }
  3171. //repair the data after move
  3172. var i,l,
  3173. tmpSrcIndex = -1,
  3174. tmpTargetIndex = 0,
  3175. oldNeighbor = null,
  3176. newNeighbor = null,
  3177. oldLevel = node.level;
  3178. if (node.isFirstNode) {
  3179. tmpSrcIndex = 0;
  3180. if (oldParentNode[childKey].length > 1 ) {
  3181. oldNeighbor = oldParentNode[childKey][1];
  3182. oldNeighbor.isFirstNode = true;
  3183. }
  3184. } else if (node.isLastNode) {
  3185. tmpSrcIndex = oldParentNode[childKey].length -1;
  3186. oldNeighbor = oldParentNode[childKey][tmpSrcIndex - 1];
  3187. oldNeighbor.isLastNode = true;
  3188. } else {
  3189. for (i = 0, l = oldParentNode[childKey].length; i < l; i++) {
  3190. if (oldParentNode[childKey][i].tId == node.tId) {
  3191. tmpSrcIndex = i;
  3192. break;
  3193. }
  3194. }
  3195. }
  3196. if (tmpSrcIndex >= 0) {
  3197. oldParentNode[childKey].splice(tmpSrcIndex, 1);
  3198. }
  3199. if (moveType != consts.move.TYPE_INNER) {
  3200. for (i = 0, l = targetParentNode[childKey].length; i < l; i++) {
  3201. if (targetParentNode[childKey][i].tId == targetNode.tId) tmpTargetIndex = i;
  3202. }
  3203. }
  3204. if (moveType == consts.move.TYPE_INNER) {
  3205. if (!targetNode[childKey]) targetNode[childKey] = new Array();
  3206. if (targetNode[childKey].length > 0) {
  3207. newNeighbor = targetNode[childKey][targetNode[childKey].length - 1];
  3208. newNeighbor.isLastNode = false;
  3209. }
  3210. targetNode[childKey].splice(targetNode[childKey].length, 0, node);
  3211. node.isLastNode = true;
  3212. node.isFirstNode = (targetNode[childKey].length == 1);
  3213. } else if (targetNode.isFirstNode && moveType == consts.move.TYPE_PREV) {
  3214. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  3215. newNeighbor = targetNode;
  3216. newNeighbor.isFirstNode = false;
  3217. node.parentTId = targetNode.parentTId;
  3218. node.isFirstNode = true;
  3219. node.isLastNode = false;
  3220. } else if (targetNode.isLastNode && moveType == consts.move.TYPE_NEXT) {
  3221. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  3222. newNeighbor = targetNode;
  3223. newNeighbor.isLastNode = false;
  3224. node.parentTId = targetNode.parentTId;
  3225. node.isFirstNode = false;
  3226. node.isLastNode = true;
  3227. } else {
  3228. if (moveType == consts.move.TYPE_PREV) {
  3229. targetParentNode[childKey].splice(tmpTargetIndex, 0, node);
  3230. } else {
  3231. targetParentNode[childKey].splice(tmpTargetIndex + 1, 0, node);
  3232. }
  3233. node.parentTId = targetNode.parentTId;
  3234. node.isFirstNode = false;
  3235. node.isLastNode = false;
  3236. }
  3237. data.fixPIdKeyValue(setting, node);
  3238. data.setSonNodeLevel(setting, node.getParentNode(), node);
  3239. //repair node what been moved
  3240. view.setNodeLineIcos(setting, node);
  3241. view.repairNodeLevelClass(setting, node, oldLevel)
  3242. //repair node's old parentNode dom
  3243. if (!setting.data.keep.parent && oldParentNode[childKey].length < 1) {
  3244. //old parentNode has no child nodes
  3245. oldParentNode.isParent = false;
  3246. oldParentNode.open = false;
  3247. var tmp_ulObj = $$(oldParentNode, consts.id.UL, setting),
  3248. tmp_switchObj = $$(oldParentNode, consts.id.SWITCH, setting),
  3249. tmp_icoObj = $$(oldParentNode, consts.id.ICON, setting);
  3250. view.replaceSwitchClass(oldParentNode, tmp_switchObj, consts.folder.DOCU);
  3251. view.replaceIcoClass(oldParentNode, tmp_icoObj, consts.folder.DOCU);
  3252. tmp_ulObj.css("display", "none");
  3253. } else if (oldNeighbor) {
  3254. //old neigbor node
  3255. view.setNodeLineIcos(setting, oldNeighbor);
  3256. }
  3257. //new neigbor node
  3258. if (newNeighbor) {
  3259. view.setNodeLineIcos(setting, newNeighbor);
  3260. }
  3261. //repair checkbox / radio
  3262. if (!!setting.check && setting.check.enable && view.repairChkClass) {
  3263. view.repairChkClass(setting, oldParentNode);
  3264. view.repairParentChkClassWithSelf(setting, oldParentNode);
  3265. if (oldParentNode != node.parent)
  3266. view.repairParentChkClassWithSelf(setting, node);
  3267. }
  3268. //expand parents after move
  3269. if (!isSilent) {
  3270. view.expandCollapseParentNode(setting, node.getParentNode(), true, animateFlag);
  3271. }
  3272. },
  3273. removeEditBtn: function(setting, node) {
  3274. $$(node, consts.id.EDIT, setting).unbind().remove();
  3275. },
  3276. removeRemoveBtn: function(setting, node) {
  3277. $$(node, consts.id.REMOVE, setting).unbind().remove();
  3278. },
  3279. removeTreeDom: function(setting, node) {
  3280. node.isHover = false;
  3281. view.removeEditBtn(setting, node);
  3282. view.removeRemoveBtn(setting, node);
  3283. tools.apply(setting.view.removeHoverDom, [setting.treeId, node]);
  3284. },
  3285. repairNodeLevelClass: function(setting, node, oldLevel) {
  3286. if (oldLevel === node.level) return;
  3287. var liObj = $$(node, setting),
  3288. aObj = $$(node, consts.id.A, setting),
  3289. ulObj = $$(node, consts.id.UL, setting),
  3290. oldClass = consts.className.LEVEL + oldLevel,
  3291. newClass = consts.className.LEVEL + node.level;
  3292. liObj.removeClass(oldClass);
  3293. liObj.addClass(newClass);
  3294. aObj.removeClass(oldClass);
  3295. aObj.addClass(newClass);
  3296. ulObj.removeClass(oldClass);
  3297. ulObj.addClass(newClass);
  3298. },
  3299. selectNodes : function(setting, nodes) {
  3300. for (var i=0, l=nodes.length; i<l; i++) {
  3301. view.selectNode(setting, nodes[i], i>0);
  3302. }
  3303. }
  3304. },
  3305. _z = {
  3306. tools: _tools,
  3307. view: _view,
  3308. event: _event,
  3309. data: _data
  3310. };
  3311. $.extend(true, $.fn.zTree.consts, _consts);
  3312. $.extend(true, $.fn.zTree._z, _z);
  3313. var zt = $.fn.zTree,
  3314. tools = zt._z.tools,
  3315. consts = zt.consts,
  3316. view = zt._z.view,
  3317. data = zt._z.data,
  3318. event = zt._z.event,
  3319. $$ = tools.$;
  3320. data.exSetting(_setting);
  3321. data.addInitBind(_bindEvent);
  3322. data.addInitUnBind(_unbindEvent);
  3323. data.addInitCache(_initCache);
  3324. data.addInitNode(_initNode);
  3325. data.addInitProxy(_eventProxy);
  3326. data.addInitRoot(_initRoot);
  3327. data.addZTreeTools(_zTreeTools);
  3328. var _cancelPreSelectedNode = view.cancelPreSelectedNode;
  3329. view.cancelPreSelectedNode = function (setting, node) {
  3330. var list = data.getRoot(setting).curSelectedList;
  3331. for (var i=0, j=list.length; i<j; i++) {
  3332. if (!node || node === list[i]) {
  3333. view.removeTreeDom(setting, list[i]);
  3334. if (node) break;
  3335. }
  3336. }
  3337. if (_cancelPreSelectedNode) _cancelPreSelectedNode.apply(view, arguments);
  3338. }
  3339. var _createNodes = view.createNodes;
  3340. view.createNodes = function(setting, level, nodes, parentNode) {
  3341. if (_createNodes) {
  3342. _createNodes.apply(view, arguments);
  3343. }
  3344. if (!nodes) return;
  3345. if (view.repairParentChkClassWithSelf) {
  3346. view.repairParentChkClassWithSelf(setting, parentNode);
  3347. }
  3348. }
  3349. var _makeNodeUrl = view.makeNodeUrl;
  3350. view.makeNodeUrl = function(setting, node) {
  3351. return setting.edit.enable ? null : (_makeNodeUrl.apply(view, arguments));
  3352. }
  3353. var _removeNode = view.removeNode;
  3354. view.removeNode = function(setting, node) {
  3355. var root = data.getRoot(setting);
  3356. if (root.curEditNode === node) root.curEditNode = null;
  3357. if (_removeNode) {
  3358. _removeNode.apply(view, arguments);
  3359. }
  3360. }
  3361. var _selectNode = view.selectNode;
  3362. view.selectNode = function(setting, node, addFlag) {
  3363. var root = data.getRoot(setting);
  3364. if (data.isSelectedNode(setting, node) && root.curEditNode == node && node.editNameFlag) {
  3365. return false;
  3366. }
  3367. if (_selectNode) _selectNode.apply(view, arguments);
  3368. view.addHoverDom(setting, node);
  3369. return true;
  3370. }
  3371. var _uCanDo = tools.uCanDo;
  3372. tools.uCanDo = function(setting, e) {
  3373. var root = data.getRoot(setting);
  3374. if (e && (tools.eqs(e.type, "mouseover") || tools.eqs(e.type, "mouseout") || tools.eqs(e.type, "mousedown") || tools.eqs(e.type, "mouseup"))) {
  3375. return true;
  3376. }
  3377. if (root.curEditNode) {
  3378. view.editNodeBlur = false;
  3379. root.curEditInput.focus();
  3380. }
  3381. return (!root.curEditNode) && (_uCanDo ? _uCanDo.apply(view, arguments) : true);
  3382. }
  3383. })(jQuery);