dtree.js 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. /**
  2. @Name:dtree 树形组件
  3. @Author:智慧的小西瓜
  4. @Site:http://www.wisdomelon.com/DTreeHelper/
  5. @License:LAYUI
  6. */
  7. layui.define(['jquery', 'layer', 'form'], function (exports) {
  8. var $ = layui.$,
  9. layer = layui.layer,
  10. form = layui.form;
  11. // 树的公共定义样式汇总
  12. var LI_NAV_CHILD = "dtree-nav-ul-sid", LI_NAV_ITEM = "dtree-nav-item",
  13. LI_DIV_ITEM = "dtree-nav-div", DTREEFONT = "dtreefont", DTREEFONTSPECIAL = "dtreefont-special",
  14. LI_DIV_MENUBAR = "dtree-menubar", LI_DIV_MENUBAR_DOWN = "dtree-icon-move-down",
  15. LI_DIV_MENUBAR_UP = "dtree-icon-move-up", LI_DIV_MENUBAR_REFRESH = "dtree-icon-refresh",
  16. LI_DIV_MENUBAR_DELETE = "dtree-icon-delete1", LI_DIV_MENUBAR_SEARCH = "dtree-icon-search_list_light",
  17. LI_DIV_TOOLBAR = "dtree-toolbar", TOOLBAR_TOOL = "dtree-toolbar-tool",
  18. LI_DIV_TOOLBAR_ADD = "dtree-icon-roundadd", LI_DIV_TOOLBAR_EDIT = "dtree-icon-bianji",
  19. LI_DIV_TOOLBAR_DEL = "dtree-icon-roundclose",
  20. LI_DIV_SPREAD_LAST = "dtree-icon-dian",
  21. LI_DIV_CHECKBAR = "dtree-nav-checkbox-div", LI_DIV_CHECKBAR_ON = "dtree-icon-fuxuankuangxuanzhong",
  22. LI_DIV_CHECKBAR_OUT = "dtree-icon-fuxuankuang", LI_DIV_CHECKBAR_NOALL = "dtree-icon-fuxuankuang-banxuan",
  23. LI_CLICK_CHECKBAR = "d-click-checkbar", //绑定点击复选框时需要用到
  24. LI_DIV_TEXT_CLASS = "t-click", UL_ROOT = "dtree";
  25. // 树的自定义样式
  26. var DTREE = "dtree-", //自定义样式前缀
  27. ITEMTHIS = "-item-this", //自定义样式当前行选中后缀
  28. ITEM = "-item", //自定义样式当前行后缀
  29. DFONT = "-dtreefont", //自定义样式图标样式后缀
  30. FICON = "-ficon", //自定义样式一级图标样式后缀
  31. ICON = "-icon", //自定义样式二级图标样式后缀
  32. CBOX = "-checkbox", //自定义样式复选框样式后缀
  33. CHS = "-choose"; //自定义样式复选框选中样式后缀
  34. // 树的公共指定
  35. var NAV_THIS = "dtree-nav-this", //当前节点
  36. NAV_SHOW = "dtree-nav-show", //显示子节点
  37. ICON_HIDE = "dtree-icon-hide", //隐藏dot图标
  38. $BODY = $("body"), //body选择器
  39. MOD_NAME = "dtree", //模块名称
  40. VERSION = "v2.4.5_finally_beta", //版本
  41. DTrees = {}; //当前被实例化的树的集合
  42. // 树的一级节点图标集合
  43. var firstIconArray = {
  44. "-1": {"open": "dtree-icon-null-open", "close": "dtree-icon-null-close"}, //未指定
  45. "0": {"open": "dtree-icon-jian", "close": "dtree-icon-jia"},
  46. "1": {"open": "dtree-icon-xiangxia1", "close": "dtree-icon-xiangyou"}
  47. };
  48. // 树的二级节点图标集合
  49. var nodeIconArray = {
  50. "-1": {"open": "dtree-icon-null-open", "close": "dtree-icon-null-close"}, //未指定
  51. "0": {"open": "dtree-icon-wenjianjiazhankai", "close": "dtree-icon-weibiaoti5"}
  52. };
  53. var leafIconArray = {
  54. "-1": "dtree-icon-null", //未指定
  55. "0": "dtree-icon-weibiaoti5", //文件夹
  56. "1": "dtree-icon-yonghu", //人员
  57. "2": "dtree-icon-fenzhijigou", //机构
  58. "3": "dtree-icon-fenguangbaobiao", //报表
  59. "4": "dtree-icon-xinxipilu", //信息
  60. "5": "dtree-icon-shuye1", //叶子
  61. "6": "dtree-icon-caidan_xunzhang", //勋章
  62. "7": "dtree-icon-normal-file" //文件
  63. };
  64. // 树自定义操作事件名称集合 绑定dtree-click的事件
  65. var eventName = {
  66. checkNodeClick: "checkNodeClick", //点击复选框
  67. itemNodeClick: "itemNodeClick" //点击子节点div
  68. };
  69. // 树默认toolbar提供的功能集合 绑定dtree-tool的事件
  70. var defaultTool = {
  71. addToolbar: "addToolbar", //点击toolbar新增
  72. editToolbar: "editToolbar", //点击toolbar编辑
  73. delToolbar: "delToolbar" //点击toolbar删除
  74. };
  75. // 树默认menubar提供的功能集合 绑定dtree-menu的事件
  76. var defaultMenu = {
  77. moveDown: "moveDown", //menubar展开节点
  78. moveUp: "moveUp", //menubar收缩节点
  79. refresh: "refresh", //menubar刷新树
  80. remove: "remove", //menubar删除选中节点
  81. searchNode: "searchNode" //menubar查询节点
  82. };
  83. // 树的公共事件
  84. var event = {
  85. getElemId: function (options) { // 根据传入的参数获取ID
  86. var elem = options.elem || "";
  87. var obj = options.obj || $(elem);
  88. if (obj.length == 0) { //页面中未找到绑定id
  89. return "";
  90. } else {
  91. return $(obj)[0].id;
  92. }
  93. },
  94. escape: function (html) {
  95. if (typeof html !== 'string') return '';
  96. return html.replace(entityReg.escape, function (match) {
  97. return entityMap.escape[match];
  98. });
  99. },
  100. unescape: function (str) {
  101. if (typeof str !== 'string') return '';
  102. return str.replace(entityReg.unescape, function (match) {
  103. return entityMap.unescape[match];
  104. });
  105. },
  106. cloneObj: function (obj, filter) { //深复制对象方法
  107. var newObj = {};
  108. if (obj instanceof Array) {
  109. newObj = [];
  110. }
  111. var str = "";
  112. if (typeof filter !== 'undefined') {
  113. str = filter.join(",");
  114. }
  115. for (var key in obj) {
  116. if (str.indexOf(key) == -1) {
  117. var val = obj[key];
  118. newObj[key] = typeof val === 'object' ? event.cloneObj(val, typeof filter !== undefined ? filter : []) : val;
  119. }
  120. }
  121. return newObj;
  122. }
  123. };
  124. // 特殊符号转义
  125. var keys = Object.keys || function (obj) {
  126. obj = Object(obj);
  127. var arr = [];
  128. for (var a in obj) arr.push(a);
  129. return arr;
  130. };
  131. var invert = function (obj) {
  132. obj = Object(obj);
  133. var result = {};
  134. for (var a in obj) result[obj[a]] = a;
  135. return result;
  136. };
  137. var entityMap = {
  138. escape: {
  139. "&": "&",
  140. "<": "&lt;",
  141. ">": "&gt;",
  142. "'": "&quo;"
  143. }
  144. };
  145. entityMap.unescape = invert(entityMap.escape);
  146. var entityReg = {
  147. escape: RegExp('[' + keys(entityMap.escape).join('') + ']', 'g'),
  148. unescape: RegExp('(' + keys(entityMap.unescape).join('|') + ')', 'g')
  149. };
  150. //异步加载接口
  151. var AjaxHelper = {
  152. request: function (config) {
  153. var data = config.data ? config.data : {};
  154. var async = (typeof (config.async) === "boolean") ? config.async : true;
  155. $.ajax({
  156. type: config.type ? config.type : "POST",
  157. headers: config.headers,
  158. url: config.url,
  159. dataType: config.dataType ? config.dataType : "json",
  160. data: data,
  161. async: async,
  162. success: config.success,
  163. error: function (XMLHttpRequest, textStatus, errorThrown) {
  164. if (typeof (config.error) === "function") {
  165. config.error();
  166. } else {
  167. layer.msg('系统异常导致操作失败, 请联系管理员。', {icon: 5, shift: 6});
  168. }
  169. },
  170. statusCode: {
  171. 404: function () {
  172. layer.msg('未找到指定请求,请检查访问路径!', {icon: 5, shift: 6});
  173. },
  174. 500: function () {
  175. layer.msg('系统错误,请联系管理员。', {icon: 5, shift: 6});
  176. }
  177. },
  178. complete: function (XMLHttpRequest, textStatus) {
  179. if (typeof (config.complete) === "function") {
  180. config.complete(XMLHttpRequest, textStatus);
  181. }
  182. }
  183. });
  184. },
  185. serialize: function (param) { //json序列化 key=value&key1=value1
  186. var p = "?";
  187. for (var key in param) {
  188. p += key + "=" + param[key] + "&";
  189. }
  190. p = p.substring(0, p.length - 1);
  191. return p;
  192. }
  193. };
  194. // 树类
  195. var DTree = function (options) {
  196. /** 默认赋值**/
  197. this.response = { // 树返回的json格式
  198. statusName: "code", //返回标识
  199. statusCode: 200, //返回码
  200. message: "message", //返回信息
  201. rootName: "data", //根节点名称
  202. treeId: "id", //节点ID
  203. parentId: "parentId", //父节点ID
  204. title: "title", //节点名称
  205. iconClass: "iconClass", //自定义图标
  206. childName: "children", //子节点名称
  207. isLast: "isLast", //是否最后一级节点
  208. // level: "level", //层级
  209. spread: "spread", //展开
  210. disabled: "disabled", //禁用
  211. checkArr: "checkArr", //复选框列表
  212. isChecked: "isChecked", //是否选中
  213. type: "type", //复选框标记
  214. basicData: "basicData" //表示用户自定义需要存储在树节点中的数据
  215. };
  216. this.defaultRequest = { // 树的默认发起请求参数格式,最后会将value作为参数名称传递
  217. nodeId: "nodeId", //节点ID
  218. parentId: "parentId", //父节点ID
  219. context: "context", //节点内容
  220. isLeaf: "isLeaf", //是否叶子节点
  221. level: "level", //层级
  222. spread: "spread", //节点展开状态
  223. dataType: "dataType", //节点标记
  224. ischecked: "ischecked", //节点复选框选中状态
  225. initchecked: "initchecked", //节点复选框初始状态
  226. basicData: "basicData", //用户自定义的记录节点数据
  227. recordData: "recordData", //当前data数据(排除basicData和children字段)
  228. };
  229. this.toolbarFun = {
  230. addTreeNode: function (param, $div) { //添加树节点后调用的函数,用于用户自定义,如未指定则树不会发生变化
  231. return;
  232. },
  233. editTreeNode: function (param, $div) { //编辑树节点后调用的函数,用于用户自定义,如未指定则树不会发生变化
  234. return;
  235. },
  236. editTreeLoad: function (param) { // 编辑树的数据回显,用于打开编辑时,回填数据
  237. return;
  238. },
  239. delTreeNode: function (param, $div) { //删除树后调用的函数,用于用户自定义,如未指定则树不会发生变化
  240. return;
  241. },
  242. loadToolbarBefore: function (buttons, param, $div) { // 右键菜单加载前的函数
  243. return buttons;
  244. }
  245. };
  246. this.toolbarStyle = {
  247. title: "节点",
  248. area: ["60%", "80%"]
  249. };
  250. this.menubarFun = {
  251. remove: function (checkbarNodes) { //删除复选框选中节点,需要用户自定义,如未指定则树只是页面上做了修改
  252. return true;
  253. }
  254. };
  255. this.menubarTips = {
  256. toolbar: [],
  257. group: [defaultMenu.moveDown, defaultMenu.moveUp, defaultMenu.refresh, defaultMenu.remove, defaultMenu.searchNode],
  258. freedom: []
  259. };
  260. this.checkbarFun = {
  261. chooseBefore: function ($i, node) { // 复选框点击前回调
  262. return true;
  263. },
  264. chooseDone: function (checkbarNodesParam) { //复选框点击事件完毕后,返回该树关于复选框操作的全部信息,用于用户自定义,如未指定则树只是页面上做了修改
  265. return;
  266. }
  267. };
  268. this.iframe = { // 树点击节点时,打开iframe页面参数配置
  269. iframeElem: "", //iframe的ID
  270. iframeUrl: "", //树关联的frame地址
  271. iframeLoad: "leaf", //点击哪一层加载frame: node:所有节点, leaf:默认,最后一级
  272. iframeDefaultRequest: { //iframe的默认参数,目的是与加载树的参数不一样
  273. nodeId: "nodeId", //节点ID
  274. parentId: "parentId", //父节点ID
  275. context: "context", //节点内容
  276. isLeaf: "isLeaf", //是否叶子节点
  277. level: "level", //层级
  278. spread: "spread", //节点展开状态
  279. dataType: "dataType", //节点标记
  280. ischecked: "ischecked", //节点复选框选中状态
  281. initchecked: "initchecked", //节点复选框初始状态
  282. basicData: "basicData", //用户自定义的记录节点数据
  283. recordData: "recordData", //当前data数据(排除basicData和children字段)
  284. },
  285. iframeRequest: {} //iframe的自定义参数
  286. };
  287. this.iframeFun = {
  288. iframeDone: function (iframeParam) { //iframe加载完毕后,用于用户自定义事件
  289. return;
  290. }
  291. };
  292. this.style = {
  293. item: "",
  294. itemThis: "",
  295. dfont: "",
  296. icon: "",
  297. cbox: "",
  298. chs: ""
  299. };
  300. /** 数据绑定**/
  301. this.node = { // 树节点选中时,包含当前节点的全部信息
  302. nodeId: "", //节点ID
  303. parentId: "", //父节点ID
  304. context: "", //节点内容
  305. isLeaf: "", //是否叶子节点
  306. level: "", //层级
  307. spread: "", //节点展开状态
  308. dataType: "", //节点标记
  309. ischecked: "", //节点复选框选中状态
  310. initchecked: "", //节点复选框初始状态
  311. basicData: "", //用户自定义的记录节点数据
  312. recordData: "", //当前data数据(排除basicData和children字段)
  313. };
  314. this.toolbarMenu = {}; // 工具栏右键菜单绑定的所有元素
  315. this.checkbarNode = []; // 复选框标记的全部节点数据
  316. this.checkArrLen = 0; //添加节点的时判断复选框个数
  317. this.temp = []; // 临时变量
  318. this.setting(options);
  319. };
  320. /******************** 初始参数加载 ********************/
  321. // 设置值
  322. DTree.prototype.setting = function (options) {
  323. this.options = options || {};
  324. /** 绑定元素参数(必填,2个参数项必填一个)**/
  325. this.elem = this.options.elem || ""; //树绑定的元素ID:#elem
  326. this.obj = this.options.obj || $(this.elem); //树绑定的jquery元素,用于当元素是延迟加载出来的话,可以用这个找到
  327. /** 基本参数**/
  328. this.initLevel = this.options.initLevel || 2; //默认展开节点 2节
  329. this.type = this.options.type || "load"; // 树的加载方式 all,全量树, load,增量树,默认load
  330. this.cache = (typeof (this.options.cache) === "boolean") ? this.options.cache : true; //开启数据缓存
  331. this.record = (typeof (this.options.record) === "boolean") ? this.options.record : false; //开启数据记录模式
  332. this.load = (typeof (this.options.load) === "boolean") ? this.options.load : true; //开启加载动画
  333. /** 样式相关参数**/
  334. this.firstIconArray = $.extend(firstIconArray, this.options.firstIconArray) || firstIconArray; //用户自定义一级图标集合,node
  335. this.nodeIconArray = $.extend(nodeIconArray, this.options.nodeIconArray) || nodeIconArray; //用户自定义二级图标集合,node
  336. this.leafIconArray = $.extend(leafIconArray, this.options.leafIconArray) || leafIconArray; //用户自定义二级图标集合,leaf
  337. this.skin = this.options.skin || "theme"; // 自定义样式
  338. if (this.skin == "layui") { // layui主题
  339. this.ficon = this.options.ficon || "1"; // 一级图标样式,0:+,-
  340. this.dot = (typeof (this.options.dot) === "boolean") ? this.options.dot : false; //是否显示一级图标的小圆点,默认不显示
  341. this.icon = this.options.icon || "7"; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章, -1:不显示二级图标。默认'1'
  342. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标
  343. } else { // 默认主题 或者自定义主题
  344. this.ficon = this.options.ficon || "0"; // 一级图标样式,0:+,-
  345. this.dot = (typeof (this.options.dot) === "boolean") ? this.options.dot : true; //是否显示一级图标的小圆点,默认显示
  346. this.icon = this.options.icon || "5"; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章, -1:不显示二级图标。默认'5'
  347. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "0") : this.icon[0]; // 二级图标中的node节点图标
  348. }
  349. /** 内置样式属性*/
  350. this.ficonOpen = this.firstIconArray[this.ficon]["open"]; // 一级图标中的node节点open图标
  351. this.ficonClose = this.firstIconArray[this.ficon]["close"]; // 一级图标中的node节点close图标
  352. this.nodeIconOpen = this.nodeIconArray[this.nodeIcon]["open"]; // 二级图标中的node节点open图标
  353. this.nodeIconClose = this.nodeIconArray[this.nodeIcon]["close"]; // 二级图标中的node节点close图标
  354. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  355. this.leafIconShow = this.leafIconArray[this.leafIcon]; // 二级图标中的leaf节点图标
  356. this.style.item = DTREE + this.skin + ITEM;
  357. this.style.itemThis = DTREE + this.skin + ITEMTHIS;
  358. this.style.dfont = DTREE + this.skin + DFONT;
  359. this.style.ficon = DTREE + this.skin + FICON;
  360. this.style.icon = DTREE + this.skin + ICON;
  361. this.style.cbox = DTREE + this.skin + CBOX;
  362. this.style.chs = DTREE + this.skin + CHS;
  363. /** 数据加载参数**/
  364. this.url = this.options.url || ""; //请求地址
  365. this.async = (typeof (this.options.async) === "boolean") ? this.options.async : true; //异步同步加载,默认异步加载
  366. this.headers = this.options.headers || {}; // ajax header属性
  367. this.method = this.options.method || "post"; //请求类型
  368. this.dataType = this.options.dataType || "json"; //参数类型
  369. this.defaultRequest = $.extend(this.defaultRequest, this.options.defaultRequest) || this.defaultRequest; //默认请求参数
  370. this.filterRequest = this.options.filterRequest || []; //过滤请求参数
  371. this.request = this.options.request || {}; //用户自定义请求参数
  372. this.response = $.extend(this.response, this.options.response) || this.response; //返回json格式
  373. this.data = this.options.data || null; //初始化指定该参数,则不会访问异步接口
  374. this.dataFormat = this.options.dataFormat || "levelRelationship"; //用于用户配置的data数据格式,list:列表, levelRelationship:层级关系,默认
  375. this.dataStyle = this.options.dataStyle || "defaultStyle"; //用于用户配置layui通用的json数据风格,layuiStyle:layui风格,defaultStyle:默认风格
  376. this.success = this.options.success || function (data, obj) {
  377. }; //树加载完毕后执行解析树之前的回调(仅限异步加载)
  378. this.done = this.options.done || function (data, obj) {
  379. }; //树加载完毕后的回调(仅限异步加载)
  380. /** 工具栏参数**/
  381. this.toolbar = this.options.toolbar || false; //是否开启可编辑模式
  382. this.toolbarStyle = $.extend(this.toolbarStyle, this.options.toolbarStyle) || this.toolbarStyle; //toolbar的自定义风格,标题,弹框大小
  383. this.toolbarScroll = this.options.toolbarScroll || this.elem; //树的上级div容器,让树可以显示滚动条的div容器
  384. this.toolbarLoad = this.options.toolbarLoad || "node"; //toolbar作用范围:node:所有节点,noleaf:非最后一级节点,leaf:最后一级
  385. this.toolbarShow = this.options.toolbarShow || ["add", "edit", "delete"]; // toolbar三个按钮自定义加载
  386. this.toolbarBtn = this.options.toolbarBtn || null; // toolbar增删改中内容的自定义加载
  387. this.toolbarExt = this.options.toolbarExt || []; // toolbar按钮扩展
  388. this.toolbarFun = $.extend(this.toolbarFun, this.options.toolbarFun) || this.toolbarFun; // toolbar事件加载
  389. /** 菜单栏参数**/
  390. this.menubar = this.options.menubar || false; //是否打开菜单栏
  391. this.menubarTips = $.extend(this.menubarTips, this.options.menubarTips) || this.menubarTips; // 菜单栏吸附, toolbar:依附在工具栏,group:依附在按钮组,freedom,自由
  392. this.menubarFun = $.extend(this.menubarFun, this.options.menubarFun) || this.menubarFun; //menubar事件加载
  393. /** 复选框参数**/
  394. this.checkbar = this.options.checkbar || false; //是否开启复选框模式
  395. this.checkbarLoad = this.options.checkbarLoad || "node"; // 复选框作用范围,node:所有节点, leaf:最后一级;默认所有节点
  396. this.checkbarType = this.options.checkbarType || "all"; //复选框选中形式 all:子集选中父级也选中, no-all:子集选中父级半选中,子集全选父级选中,p-casc:父级选中子集全选,子集无法改变父级选中状态, self:没有任何级联关系,only:只能选中一个复选框。 默认all
  397. this.checkbarData = this.options.checkbarData || "choose"; //复选框记录数据类型形式, change表示记录变更数据,choose表示记录选中数据,all,记录全部数据,默认choose
  398. this.checkbarFun = $.extend(this.checkbarFun, this.options.checkbarFun) || this.checkbarFun; // checkbar事件加载
  399. /** iframe模式参数**/
  400. this.useIframe = this.options.useIframe || false; // 是否加载iframe 默认false,
  401. this.iframe = $.extend(this.iframe, this.options.iframe) || this.iframe; //iframe配置
  402. this.iframeFun = $.extend(this.iframeFun, this.options.iframeFun) || this.iframeFun; //iframe事件加载
  403. };
  404. // 设置值
  405. DTree.prototype.reloadSetting = function (options) {
  406. this.options = $.extend(this.options, options) || this.options;
  407. /** 绑定元素参数**/
  408. this.elem = this.options.elem || this.elem; //树绑定的元素ID:#elem
  409. if (typeof this.options.obj === 'undefined') {
  410. if (this.elem) {
  411. if ($(this.elem).length > 0) {
  412. this.obj = $(this.elem);
  413. }
  414. }
  415. } else {
  416. this.obj = this.options.obj || this.obj; //树绑定的jquery元素,用于当元素是延迟加载出来的话,可以用这个找到
  417. }
  418. /** 基本参数**/
  419. this.initLevel = this.options.initLevel || this.initLevel; //默认展开节点 2节
  420. this.type = this.options.type || this.type; // 树的加载方式 all,全量树, load,增量树,默认load
  421. this.cache = (typeof (this.options.cache) === "boolean") ? this.options.cache : this.cache; //开启数据缓存
  422. this.record = (typeof (this.options.record) === "boolean") ? this.options.record : this.record; //开启数据记录模式
  423. this.load = (typeof (this.options.load) === "boolean") ? this.options.load : this.load; //开启加载动画
  424. /** 样式相关参数**/
  425. this.firstIconArray = $.extend(firstIconArray, this.options.firstIconArray) || this.firstIconArray; //用户自定义一级图标集合,node
  426. this.nodeIconArray = $.extend(nodeIconArray, this.options.nodeIconArray) || this.nodeIconArray; //用户自定义二级图标集合,node
  427. this.leafIconArray = $.extend(leafIconArray, this.options.leafIconArray) || this.leafIconArray; //用户自定义二级图标集合,leaf
  428. this.skin = this.options.skin || this.skin; // 自定义样式
  429. if (this.skin == "layui") { // layui主题
  430. this.ficon = this.options.ficon || this.ficon; // 一级图标样式,0:+,-
  431. this.dot = (typeof (this.options.dot) === "boolean") ? this.options.dot : false; //是否显示一级图标的小圆点,默认不显示
  432. this.icon = this.options.icon || this.icon; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章, -1:不显示二级图标。默认'1'
  433. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标
  434. } else { // 默认主题 或者自定义主题
  435. this.ficon = this.options.ficon || this.ficon; // 一级图标样式,0:+,-
  436. this.dot = (typeof (this.options.dot) === "boolean") ? this.options.dot : true; //是否显示一级图标的小圆点,默认显示
  437. this.icon = this.options.icon || this.icon; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章, -1:不显示二级图标。默认'5'
  438. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "0") : this.icon[0]; // 二级图标中的node节点图标
  439. }
  440. /** 内置样式属性*/
  441. this.ficonOpen = this.firstIconArray[this.ficon]["open"]; // 一级图标中的node节点open图标
  442. this.ficonClose = this.firstIconArray[this.ficon]["close"]; // 一级图标中的node节点close图标
  443. this.nodeIconOpen = this.nodeIconArray[this.nodeIcon]["open"]; // 二级图标中的node节点open图标
  444. this.nodeIconClose = this.nodeIconArray[this.nodeIcon]["close"]; // 二级图标中的node节点close图标
  445. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  446. this.leafIconShow = this.leafIconArray[this.leafIcon]; // 二级图标中的leaf节点图标
  447. this.style.item = DTREE + this.skin + ITEM;
  448. this.style.itemThis = DTREE + this.skin + ITEMTHIS;
  449. this.style.dfont = DTREE + this.skin + DFONT;
  450. this.style.ficon = DTREE + this.skin + FICON;
  451. this.style.icon = DTREE + this.skin + ICON;
  452. this.style.cbox = DTREE + this.skin + CBOX;
  453. this.style.chs = DTREE + this.skin + CHS;
  454. /** 数据加载参数**/
  455. this.url = this.options.url || this.url; //请求地址
  456. this.async = (typeof (this.options.async) === "boolean") ? this.options.async : this.async; //异步同步加载,默认异步加载
  457. this.headers = this.options.headers || this.headers; // ajax header属性
  458. this.method = this.options.method || this.method; //请求类型
  459. this.dataType = this.options.dataType || this.dataType; //参数类型
  460. this.defaultRequest = $.extend(this.defaultRequest, this.options.defaultRequest) || this.defaultRequest; //默认请求参数
  461. this.filterRequest = this.options.filterRequest || this.filterRequest; //过滤请求参数
  462. this.request = this.options.request || this.request; //用户自定义请求参数
  463. this.response = $.extend(this.response, this.options.response) || this.response; //返回json格式
  464. this.data = this.options.data || this.data; //初始化指定该参数,则不会访问异步接口
  465. this.dataFormat = this.options.dataFormat || this.dataFormat; //用于用户配置的data数据格式,list:列表, levelRelationship:层级关系,默认
  466. this.dataStyle = this.options.dataStyle || this.dataStyle; //用于用户配置layui通用的json数据风格,layuiStyle:layui风格,defaultStyle:默认风格
  467. this.success = this.options.success || this.success; //树加载完毕后执行解析树之前的回调(仅限异步加载)
  468. this.done = this.options.done || this.done; //树加载完毕后的回调(仅限异步加载)
  469. /** 可编辑模式参数**/
  470. this.toolbar = this.options.toolbar || this.toolbar; //是否开启可编辑模式
  471. this.toolbarStyle = $.extend(this.toolbarStyle, this.options.toolbarStyle) || this.toolbarStyle; //toolbar的自定义风格,标题,弹框大小
  472. this.toolbarScroll = this.options.toolbarScroll || this.toolbarScroll; //树的上级div容器,让树可以显示滚动条的div容器
  473. this.toolbarLoad = this.options.toolbarLoad || this.toolbarLoad; //toolbar作用范围:node:所有节点,noleaf:非最后一级节点,leaf:最后一级
  474. this.toolbarShow = this.options.toolbarShow || this.toolbarShow; // toolbar三个按钮
  475. this.toolbarBtn = this.options.toolbarBtn || this.toolbarBtn; // toolbar增删改中内容的自定义加载
  476. this.toolbarExt = this.options.toolbarExt || this.toolbarExt; // toolbar按钮扩展
  477. this.toolbarFun = $.extend(this.toolbarFun, this.options.toolbarFun) || this.toolbarFun; // toolbar事件加载
  478. /** 菜单栏参数**/
  479. this.menubar = this.options.menubar || this.menubar; //是否打开菜单栏
  480. this.menubarTips = $.extend(this.menubarTips, this.options.menubarTips) || this.menubarTips; // 菜单栏吸附, toolbar:依附在工具栏,group:依附在按钮组,freedom,自由
  481. this.menubarFun = $.extend(this.menubarFun, this.options.menubarFun) || this.menubarFun; //menubar事件加载
  482. /** 复选框参数**/
  483. this.checkbar = this.options.checkbar || this.checkbar; //是否开启复选框模式
  484. this.checkbarLoad = this.options.checkbarLoad || this.checkbarLoad; // 复选框作用范围,node:所有节点, leaf:最后一级;默认所有节点
  485. this.checkbarType = this.options.checkbarType || this.checkbarType; //复选框选中形式 all:子集选中父级也选中, no-all:子集选中父级半选中,子集全选父级选中,p-casc:父级选中子集全选,子集无法改变父级选中状态, self:没有任何级联关系,only:只能选中一个复选框。 默认all
  486. this.checkbarData = this.options.checkbarData || this.checkbarData; //复选框记录数据类型形式, change表示记录变更数据,choose表示记录选中数据,all,记录全部数据,默认choose
  487. this.checkbarFun = $.extend(this.checkbarFun, this.options.checkbarFun) || this.checkbarFun; // checkbar事件加载
  488. /** iframe模式参数**/
  489. this.useIframe = this.options.useIframe || this.useIframe; // 是否加载iframe 默认false,
  490. this.iframe = $.extend(this.iframe, this.options.iframe) || this.iframe; //iframe配置
  491. this.iframeFun = $.extend(this.iframeFun, this.options.iframeFun) || this.iframeFun; //iframe事件加载
  492. };
  493. /******************** 初始化数据区域 ********************/
  494. // 重载树
  495. DTree.prototype.reload = function (options) {
  496. var _this = this;
  497. _this.reloadSetting(options);
  498. _this.init();
  499. };
  500. // 初始化树
  501. DTree.prototype.init = function () {
  502. var _this = this;
  503. if (typeof _this !== "object") {
  504. layer.msg("树组件未成功加载,请检查配置", {icon: 5});
  505. return;
  506. }
  507. if (_this.data) {
  508. if (typeof _this.data.length === 'undefined') {
  509. layer.msg("数据解析异常,data数据格式不正确", {icon: 5});
  510. return;
  511. }
  512. //先将ul中的元素清空
  513. _this.obj.html("");
  514. // 加载完毕后执行树解析前的回调
  515. _this.success(_this.data, _this.obj);
  516. // 第一次解析树
  517. if (_this.dataFormat == 'list') {
  518. //1.识别根节点ul中的data-id标签,判断顶级父节点
  519. var pid = _this.obj.attr("data-id");
  520. //2.构建一个存放节点的树组
  521. var rootListData = _this.queryListTreeByPid(pid, _this.data);
  522. _this.loadListTree(rootListData, _this.data, 1);
  523. } else {
  524. _this.loadTree(_this.data, 1);
  525. }
  526. // 加载完毕后的回调
  527. _this.done(_this.data, _this.obj);
  528. } else {
  529. if (!_this.url) {
  530. layer.msg("数据请求异常,url参数未指定", {icon: 5});
  531. return;
  532. }
  533. //先将ul中的元素清空
  534. _this.obj.html("");
  535. var index = _this.load ? layer.load(1) : "";
  536. AjaxHelper.request({
  537. async: _this.async,
  538. headers: _this.headers,
  539. type: _this.method,
  540. url: _this.url,
  541. dataType: _this.dataType,
  542. data: _this.getFilterRequestParam(_this.getRequestParam()),
  543. success: function (result) {
  544. if (typeof result === 'string') {
  545. result = $.parseJSON(result);
  546. }
  547. var code = "";
  548. if (_this.dataStyle == 'layuiStyle') {
  549. code = result[_this.response.statusName];
  550. } else {
  551. code = result.status[_this.response.statusName];
  552. }
  553. if (code == _this.response.statusCode) {
  554. // 加载完毕后执行树解析前的回调
  555. _this.success(result, _this.obj);
  556. // 第一次解析树
  557. if (_this.dataFormat == 'list') {
  558. //1.识别根节点ul中的data-id标签,判断顶级父节点
  559. var pid = _this.obj.attr("data-id");
  560. //2.构建一个存放节点的树组
  561. var rootListData = _this.queryListTreeByPid(pid, result[_this.response.rootName]);
  562. _this.loadListTree(rootListData, result[_this.response.rootName], 1);
  563. } else {
  564. _this.loadTree(result[_this.response.rootName], 1);
  565. }
  566. // 加载完毕后的回调
  567. _this.done(result, _this.obj);
  568. } else {
  569. if (_this.dataStyle == 'layuiStyle') {
  570. layer.msg(result[_this.response.message], {icon: 2});
  571. } else {
  572. layer.msg(result.status[_this.response.message], {icon: 2});
  573. }
  574. }
  575. },
  576. complete: function () {
  577. if (_this.load) {
  578. layer.close(index);
  579. }
  580. }
  581. });
  582. }
  583. };
  584. // 加载子节点
  585. DTree.prototype.getChild = function ($div, data) {
  586. var _this = this,
  587. $ul = $div.next("ul");
  588. _this.setNodeParam($div);
  589. if (typeof data !== 'undefined') {
  590. if (typeof data.length === 'undefined') {
  591. layer.msg("数据解析异常,data数据格式不正确", {icon: 5});
  592. return;
  593. }
  594. //先将ul中的元素清空
  595. $ul.html("");
  596. // 解析树
  597. if (_this.dataFormat == 'list') {
  598. var pid = _this.node.nodeId;
  599. var level = parseInt(_this.node.level) + 1;
  600. var listData = _this.queryListTreeByPid(pid, data);
  601. _this.loadListTree(listData, _this.data, level);
  602. } else {
  603. _this.loadTree(data, level);
  604. }
  605. } else {
  606. if (!_this.url) {
  607. layer.msg("数据请求异常,url参数未指定", {icon: 5});
  608. return;
  609. }
  610. $ul.html("");
  611. var index = _this.load ? layer.load(1) : "";
  612. AjaxHelper.request({
  613. async: _this.async,
  614. headers: _this.headers,
  615. type: _this.method,
  616. url: _this.url,
  617. dataType: _this.dataType,
  618. data: _this.getFilterRequestParam(_this.getRequestParam()),
  619. success: function (result) {
  620. if (typeof result === 'string') {
  621. result = $.parseJSON(result);
  622. }
  623. var code = "";
  624. if (_this.dataStyle == 'layuiStyle') {
  625. code = result[_this.response.statusName];
  626. } else {
  627. code = result.status[_this.response.statusName];
  628. }
  629. if (code == _this.response.statusCode) {
  630. // 解析树
  631. var pid = _this.node.nodeId;
  632. var level = parseInt(_this.node.level) + 1;
  633. if (_this.dataFormat == 'list') {
  634. var pListData = _this.queryListTreeByPid(pid, result[_this.response.rootName]);
  635. _this.loadListTree(pListData, result[_this.response.rootName], level, $ul);
  636. } else {
  637. _this.loadTree(result[_this.response.rootName], level, $ul);
  638. }
  639. $ul.addClass(NAV_SHOW);
  640. } else {
  641. if (_this.dataStyle == 'layuiStyle') {
  642. layer.msg(result[_this.response.message], {icon: 2});
  643. } else {
  644. layer.msg(result.status[_this.response.message], {icon: 2});
  645. }
  646. }
  647. },
  648. complete: function () {
  649. if (_this.load) {
  650. layer.close(index);
  651. }
  652. }
  653. });
  654. }
  655. };
  656. // 初始化树或者拼接树
  657. DTree.prototype.loadListTree = function (pListData, listData, level, $ul) {
  658. var _this = this;
  659. $ul = $ul || _this.getNowNodeUl(); //当前选中的节点或根节点
  660. if (pListData.length > 0) {
  661. for (var i = 0; i < pListData.length; i++) {
  662. // 1.获取已知节点的全部数据
  663. var data = pListData[i];
  664. if (typeof data !== "object") continue;
  665. var parseData = _this.parseData(data);
  666. var childListData = _this.queryListTreeByPid(parseData.treeId(), listData); // 根据已知数据的id判断该条数据是否还有子数据
  667. // 3. 页面元素加载数据
  668. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.isLast(childListData.length), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(level), parseData.disabled(), parseData.basicData(), parseData.recordData(), ($ul.hasClass(UL_ROOT) ? "root" : "item")));
  669. // 4.有子数据的元素加载子节点
  670. if (childListData.length > 0) {
  671. var cLevel = parseInt(level) + 1;
  672. _this.loadListTree(childListData, listData, cLevel, _this.obj.find("ul[data-id='" + parseData.treeId() + "']"));
  673. }
  674. }
  675. }
  676. };
  677. // 根据父ID查找list数据中匹配的元素
  678. DTree.prototype.queryListTreeByPid = function (pid, listData) {
  679. var _this = this;
  680. var rootListData = [];
  681. if (listData) {
  682. for (var i = 0; i < listData.length; i++) {
  683. var data = listData[i];
  684. if (typeof data !== "object") continue;
  685. if (pid == "null" || pid == null) {
  686. if (data[_this.response.parentId] == null) {
  687. rootListData.push(data);
  688. }
  689. } else {
  690. if (data[_this.response.parentId] == pid) {
  691. rootListData.push(data);
  692. }
  693. }
  694. }
  695. }
  696. return rootListData;
  697. };
  698. // 初始化树或者拼接树
  699. DTree.prototype.loadTree = function (root, level, $ul) {
  700. var _this = this;
  701. if (root) {
  702. $ul = $ul || _this.getNowNodeUl(); //当前选中的节点或根节点
  703. for (var i = 0; i < root.length; i++) { // 遍历跟节点或追加的跟节点
  704. var data = root[i];
  705. if (typeof data !== "object") continue;
  706. var parseData = _this.parseData(data);
  707. var children = parseData.children();
  708. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.isLast(children.length), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(level), parseData.disabled(), parseData.basicData(), parseData.recordData(), ($ul.hasClass(UL_ROOT) ? "root" : "item")));
  709. if (children.length != 0) {
  710. var cLevel = parseInt(level) + 1;
  711. _this.loadTree(children, cLevel, _this.obj.find("ul[data-id='" + parseData.treeId() + "']"));
  712. }
  713. }
  714. }
  715. };
  716. // 解析data数据
  717. DTree.prototype.parseData = function (data) {
  718. var _this = this;
  719. return {
  720. treeId: function () {
  721. return data[_this.response.treeId];
  722. },
  723. parentId: function () {
  724. return data[_this.response.parentId];
  725. },
  726. title: function () {
  727. return data[_this.response.title] || "";
  728. },
  729. level: function () {
  730. return data[_this.response.level] || "";
  731. },
  732. iconClass: function () {
  733. return data[_this.response.iconClass] || "";
  734. },
  735. isLast: function (len) {
  736. return ((len == 0) ?
  737. ((typeof (data[_this.response.isLast]) === "boolean") ? data[_this.response.isLast] : true) :
  738. ((typeof (data[_this.response.isLast]) === "boolean") ? data[_this.response.isLast] : false));
  739. },
  740. spread: function (level) {
  741. return ((level < _this.initLevel) ?
  742. ((typeof (data[_this.response.spread]) === "boolean") ? data[_this.response.spread] : true) :
  743. ((typeof (data[_this.response.spread]) === "boolean") ? data[_this.response.spread] : false));
  744. },
  745. disabled: function () {
  746. return (typeof (data[_this.response.disabled]) === "boolean") ? data[_this.response.disabled] : false;
  747. },
  748. checkArr: function () {
  749. var checkArr = [];
  750. var checkArrData = data[_this.response.checkArr];
  751. if (typeof checkArrData === 'string') {
  752. if (checkArrData.indexOf("{") > -1 && checkArrData.indexOf("}") > -1) {
  753. checkArrData = JSON.parse(checkArrData);
  754. } else {
  755. checkArrData = {"type": "0", "isChecked": checkArrData};
  756. }
  757. }
  758. if (typeof checkArrData === 'object') {
  759. if (typeof checkArrData.length === 'undefined') {
  760. checkArr.push(checkArrData);
  761. } else {
  762. checkArr = checkArrData;
  763. }
  764. }
  765. if (checkArr.length > 0 && checkArr.length > _this.checkArrLen) {
  766. _this.checkArrLen = checkArr.length; // 获取复选框个数
  767. }
  768. return checkArr;
  769. },
  770. children: function () {
  771. return data[_this.response.childName] || [];
  772. },
  773. basicData: function () {
  774. return event.escape(JSON.stringify(data[_this.response.basicData])) || JSON.stringify({});
  775. },
  776. recordData: function () {
  777. var recordData = _this.record ? event.cloneObj(data, [_this.response.basicData, _this.response.childName]) : {};
  778. return event.escape(JSON.stringify(recordData));
  779. },
  780. data: function () {
  781. return event.escape(JSON.stringify(data));
  782. }
  783. }
  784. };
  785. //新增节点的dom值
  786. DTree.prototype.getDom = function (treeId, parentId, title, isLast, iconClass, checkArr, level, spread, disabled) {
  787. var _this = this,
  788. rootId = _this.obj[0].id,
  789. toolbar = _this.toolbar,
  790. checkbar = _this.checkbar;
  791. return {
  792. fnode: function () { // + - 图标
  793. // 获取图标的变量
  794. var ficon = _this.ficon,
  795. ficonOpen = _this.ficonOpen,
  796. ficonClose = _this.ficonClose,
  797. dot = _this.dot;
  798. if (ficon != "-1" && dot) { // 都加载
  799. return isLast ? "<i class='" + DTREEFONT + " " + LI_DIV_SPREAD_LAST + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  800. (spread ? "<i class='" + DTREEFONT + " " + ficonOpen + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + ficonClose + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  801. }
  802. if (ficon != "-1" && !dot) { // 加载node 隐藏leaf
  803. return isLast ? "<i class='" + DTREEFONT + " " + LI_DIV_SPREAD_LAST + " " + ICON_HIDE + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  804. (spread ? "<i class='" + DTREEFONT + " " + ficonOpen + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + ficonClose + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  805. }
  806. if (ficon == "-1" && dot) { // 隐藏node 加载leaf
  807. return isLast ? "<i class='" + DTREEFONT + " " + LI_DIV_SPREAD_LAST + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  808. (spread ? "<i class='" + DTREEFONT + " " + ficonOpen + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + ficonClose + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  809. }
  810. if (ficon == "-1" && !dot) { // 都隐藏
  811. return isLast ? "<i class='" + DTREEFONT + " " + LI_DIV_SPREAD_LAST + " " + ICON_HIDE + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "' style='display:none;'></i>" :
  812. (spread ? "<i class='" + DTREEFONT + " " + ficonOpen + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + ficonClose + " " + _this.style.dfont + " " + _this.style.ficon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  813. }
  814. },
  815. node: function () { // 二级图标样式
  816. // 获取图标的变量
  817. var nodeIcon = _this.nodeIcon,
  818. leafIcon = _this.leafIcon;
  819. var leafIconShow = _this.leafIconShow,
  820. nodeIconOpen = _this.nodeIconOpen,
  821. nodeIconClose = _this.nodeIconClose;
  822. if (iconClass) {
  823. leafIconShow = iconClass;
  824. nodeIconOpen = iconClass;
  825. nodeIconClose = iconClass;
  826. }
  827. if (nodeIcon != "-1" && leafIcon != "-1") { // 都加载
  828. return isLast ? "<i class='" + DTREEFONT + " " + leafIconShow + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  829. (spread ? "<i class='" + DTREEFONT + " " + nodeIconOpen + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + nodeIconClose + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  830. }
  831. if (nodeIcon != "-1" && leafIcon == "-1") { // 加载node 隐藏leaf
  832. return isLast ? "<i class='" + DTREEFONT + " " + leafIconShow + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  833. (spread ? "<i class='" + DTREEFONT + " " + nodeIconOpen + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + nodeIconClose + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  834. }
  835. if (nodeIcon == "-1" && leafIcon != "-1") { // 隐藏node 加载leaf
  836. return isLast ? "<i class='" + DTREEFONT + " " + leafIconShow + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  837. (spread ? "<i class='" + DTREEFONT + " " + nodeIconOpen + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + nodeIconClose + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  838. }
  839. if (nodeIcon == "-1" && leafIcon == "-1") { // 都隐藏
  840. return isLast ? "<i class='" + DTREEFONT + " " + leafIconShow + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='last' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" :
  841. (spread ? "<i class='" + DTREEFONT + " " + nodeIconOpen + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='open' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>" : "<i class='" + DTREEFONT + " " + nodeIconClose + " " + DTREEFONTSPECIAL + " " + _this.style.dfont + " " + _this.style.icon + "' data-spread='close' data-id='" + treeId + "' dtree-id='" + rootId + "'></i>");
  842. }
  843. },
  844. checkbox: function () { // 复选框
  845. var flag = false;
  846. if (_this.checkbarLoad == "node") {
  847. if (checkbar) {
  848. flag = true;
  849. }
  850. } else {
  851. if (isLast) {
  852. if (checkbar) {
  853. flag = true;
  854. }
  855. }
  856. }
  857. if (flag) {
  858. var result = "<div class='" + LI_DIV_CHECKBAR + "' data-id='" + treeId + "' dtree-id='" + rootId + "'>";
  859. if (checkArr && checkArr.length > 0) {
  860. for (var i = 0; i < checkArr.length; i++) {
  861. var checkData = checkArr[i];
  862. var isChecked = checkData.isChecked;
  863. var CHOOSE_CLASS = LI_DIV_CHECKBAR_OUT;
  864. if (isChecked == "2") { //半选择
  865. CHOOSE_CLASS = LI_DIV_CHECKBAR_NOALL + " " + _this.style.chs;
  866. } else if (isChecked == "1") { //选择
  867. CHOOSE_CLASS = LI_DIV_CHECKBAR_ON + " " + _this.style.chs;
  868. } else { //未选择或者无值
  869. CHOOSE_CLASS = LI_DIV_CHECKBAR_OUT;
  870. }
  871. result += "<i class='" + DTREEFONT + " " + _this.style.dfont + " " + _this.style.cbox + " " + CHOOSE_CLASS + "' data-id='" + treeId + "' dtree-id='" + rootId + "' data-checked='" + checkData.isChecked + "' data-initchecked='" + checkData.isChecked + "' data-type='" + checkData.type + "' dtree-click='" + eventName.checkNodeClick + "' data-par='." + LI_CLICK_CHECKBAR + "'></i>";
  872. }
  873. }
  874. result += "</div>";
  875. return result;
  876. }
  877. return "";
  878. },
  879. text: function () { // 文字显示
  880. return "<cite class='" + LI_DIV_TEXT_CLASS + "' data-id='" + treeId + "' data-leaf='" + (isLast ? "leaf" : "node") + "'>" + title + "</cite>";
  881. },
  882. ul: function () { //子节点ul
  883. return isLast ? "<ul class='" + LI_NAV_CHILD + "' data-id='" + treeId + "' dtree-id='" + rootId + "'></ul>" :
  884. (spread ? "<ul class='" + LI_NAV_CHILD + " " + NAV_SHOW + "' data-id='" + treeId + "' dtree-id='" + rootId + "'></ul>" : "<ul class='" + LI_NAV_CHILD + "' data-id='" + treeId + "' dtree-id='" + rootId + "'></ul>");
  885. }
  886. };
  887. };
  888. // 获取拼接好的li
  889. DTree.prototype.getLiItemDom = function (treeId, parentId, title, isLast, iconClass, checkArr, level, spread, disabled, basicData, recordData, flag) {
  890. var _this = this,
  891. rootId = _this.obj[0].id;
  892. var dom = _this.getDom(treeId, parentId, title, isLast, iconClass, checkArr, level, spread, disabled);
  893. basicData = (basicData == "{}") ? "" : basicData;
  894. recordData = (recordData == "{}") ? "" : recordData;
  895. var div = "<div class='" + LI_DIV_ITEM + " " + _this.style.item + "' data-id='" + treeId + "' dtree-id='" + rootId + "' dtree-click='" + eventName.itemNodeClick + "' data-basic='" + basicData + "' data-record='" + recordData + "' ";
  896. if (_this.toolbar) {
  897. if (_this.toolbarLoad == "node") {
  898. div += " d-contextmenu='true'>";
  899. }
  900. if (_this.toolbarLoad == "noleaf") {
  901. if (!isLast) {
  902. div += " d-contextmenu='true'>";
  903. } else {
  904. div += " d-contextmenu='false'>";
  905. }
  906. }
  907. if (_this.toolbarLoad == "leaf") {
  908. if (isLast) {
  909. div += " d-contextmenu='true'>";
  910. } else {
  911. div += " d-contextmenu='false'>";
  912. }
  913. }
  914. } else {
  915. div += " d-contextmenu='false'>";
  916. }
  917. var li = ["<li " + "class='" + LI_CLICK_CHECKBAR + " " + LI_NAV_ITEM + "'" + "data-id='" + treeId + "'" + "data-pid='" + (flag == "root" ? (parentId ? parentId : "-1") : parentId) + "'" + "dtree-id='" + rootId + "'" + "data-index='" + level + "'" + ">" +
  918. div,
  919. dom.fnode(),
  920. dom.node(),
  921. dom.checkbox(),
  922. dom.text(),
  923. "</div>", dom.ul(), "</li>"].join("");
  924. return li;
  925. };
  926. // 初始化节点,用于数据回显
  927. DTree.prototype.dataInit = function (chooseId) {
  928. var _this = this;
  929. var $div = _this.obj.find("div[data-id='" + chooseId + "']");
  930. $div.parent().find("." + NAV_THIS).removeClass(NAV_THIS);
  931. $div.parent().find("." + _this.style.itemThis).removeClass(_this.style.itemThis);
  932. $div.addClass(NAV_THIS);
  933. $div.addClass(_this.style.itemThis);
  934. _this.setNodeParam($div);
  935. // 将该节点的父节点全部展开
  936. var $li_parents = $div.parents("." + LI_NAV_ITEM);
  937. $li_parents.children("ul").addClass(NAV_SHOW);
  938. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.ficonClose).addClass(_this.ficonOpen);
  939. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.ficonClose).removeClass(_this.ficonClose);
  940. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.nodeIconClose).addClass(_this.nodeIconOpen);
  941. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.nodeIconClose).removeClass(_this.nodeIconClose);
  942. return _this.getNowParam();
  943. };
  944. /******************** 基础事件区域 ********************/
  945. // 展开或隐藏节点 作用点: div
  946. DTree.prototype.clickSpread = function ($div) {
  947. var $i_spread = $div.find("i[data-spread]").eq(0),
  948. $i_node = $div.find("i[data-spread]").eq(1),
  949. i_node_class = $i_node.attr("class"),
  950. $cite = $div.find("cite[data-leaf]").eq(0),
  951. spread = $i_spread.attr("data-spread"),
  952. $ul = $div.next("ul");
  953. var _this = this;
  954. if ($ul.length > 0) {
  955. if (spread == "close") {
  956. if (_this.type == "load") { //增加加载
  957. if (_this.cache) { //开启缓存
  958. if ($ul.html()) {
  959. $ul.addClass(NAV_SHOW);
  960. } else { //加载节点
  961. _this.getChild($div);
  962. }
  963. } else { //每次取新的数据
  964. $ul.html("");
  965. _this.getChild($div);
  966. }
  967. } else { // 全量加载
  968. $ul.addClass(NAV_SHOW);
  969. }
  970. $div.find("i[data-spread]").attr("data-spread", "open");
  971. $i_spread.removeClass(_this.ficonClose);
  972. $i_spread.addClass(_this.ficonOpen);
  973. var node_class = _this.nodeIconClose;
  974. if (i_node_class.indexOf(node_class) > 0) {
  975. $i_node.removeClass(_this.nodeIconClose);
  976. $i_node.addClass(_this.nodeIconOpen);
  977. }
  978. } else if (spread == "open") {
  979. $ul.removeClass(NAV_SHOW);
  980. $div.find("i[data-spread]").attr("data-spread", "close");
  981. $i_spread.removeClass(_this.ficonOpen);
  982. $i_spread.addClass(_this.ficonClose);
  983. var node_class = _this.nodeIconOpen;
  984. if (i_node_class.indexOf(node_class) > 0) {
  985. $i_node.removeClass(_this.nodeIconOpen);
  986. $i_node.addClass(_this.nodeIconClose);
  987. }
  988. }
  989. }
  990. };
  991. // 数据格式化
  992. DTree.prototype.escape = function (html) {
  993. return event.escape(html);
  994. };
  995. // 格式化数据转回正常数据
  996. DTree.prototype.unescape = function (str) {
  997. return event.unescape(str);
  998. };
  999. /******************** 工具栏及菜单栏区域 ********************/
  1000. // 初始化菜单栏和工具栏的div
  1001. DTree.prototype.initTreePlus = function () {
  1002. var _this = this;
  1003. // 初始化菜单栏和工具栏的div
  1004. _this.obj.prevAll('div#dtree_menubar_' + _this.obj[0].id).remove();
  1005. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).remove();
  1006. _this.toolbarMenu = {};
  1007. if (_this.menubar && _this.menubarTips.group && _this.menubarTips.group.length > 0) _this.obj.before("<div class='" + LI_DIV_MENUBAR + "' id='dtree_menubar_" + _this.obj[0].id + "'><div class='layui-btn-group'></div></div>");
  1008. if (_this.toolbar) _this.obj.before("<div class='" + LI_DIV_TOOLBAR + " layui-nav' id='dtree_toolbar_" + _this.obj[0].id + "'><div class='layui-nav-item'><dl class='layui-nav-child layui-anim'></dl></div></div>");
  1009. };
  1010. // 开启工具栏和菜单栏
  1011. DTree.prototype.openTreePlus = function () {
  1012. var _this = this;
  1013. // 先对工具栏做处理,因为菜单栏可能会与工具栏产生关联。
  1014. var ggMenu = [];
  1015. if (_this.toolbar) _this.getToolbarDom();
  1016. if (_this.menubar) {
  1017. var menubarTips = _this.menubarTips,
  1018. mtbar = menubarTips.toolbar,
  1019. group = menubarTips.group,
  1020. freedom = menubarTips.freedom;
  1021. if (mtbar && mtbar.length > 0) {
  1022. // 菜单栏吸附工具栏上
  1023. for (var i = 0; i < mtbar.length; i++) {
  1024. var mt = mtbar[i];
  1025. if (typeof mt === 'string') {
  1026. _this.getMenubarToolDom(mt);
  1027. }
  1028. if (typeof mt === 'object') {
  1029. _this.getExtMenubarToolDom(mt);
  1030. }
  1031. }
  1032. }
  1033. if (group && group.length > 0) {
  1034. // 菜单栏吸附在上方的按钮组div中
  1035. for (var i = 0; i < group.length; i++) {
  1036. var gg = group[i];
  1037. if (typeof gg === 'string') {
  1038. ggMenu.push(_this.getMenubarDom(gg));
  1039. }
  1040. if (typeof gg === 'object') {
  1041. ggMenu.push(_this.getExtMenubarDom(gg));
  1042. }
  1043. }
  1044. _this.obj.prevAll('div#dtree_menubar_' + _this.obj[0].id).children('div.layui-btn-group').append(ggMenu.join(""));
  1045. }
  1046. }
  1047. };
  1048. /******************** 菜单栏区域 ********************/
  1049. // 获取菜单栏
  1050. DTree.prototype.getMenubarDom = function (menu) {
  1051. var _this = this;
  1052. var rootId = _this.obj[0].id;
  1053. var gg = "";
  1054. switch (menu) {
  1055. case defaultMenu.moveDown:
  1056. gg = "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + rootId + "' d-menu='" + defaultMenu.moveDown + "' title='展开节点'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_DOWN + "'></i></button>";
  1057. break;
  1058. case defaultMenu.moveUp:
  1059. gg = "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + rootId + "' d-menu='" + defaultMenu.moveUp + "' title='收缩节点'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_UP + "'></i></button>";
  1060. break;
  1061. case defaultMenu.refresh:
  1062. gg = "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + rootId + "' d-menu='" + defaultMenu.refresh + "' title='刷新'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_REFRESH + "'></i></button>";
  1063. break;
  1064. case defaultMenu.remove:
  1065. gg = (_this.checkbar) ? "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + rootId + "' d-menu='" + defaultMenu.remove + "' title='删除选中节点'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_DELETE + "'></i></button>" : "";
  1066. break;
  1067. case defaultMenu.searchNode:
  1068. gg = "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + rootId + "' d-menu='" + defaultMenu.searchNode + "' title='查询节点'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_SEARCH + "'></i></button>";
  1069. break;
  1070. }
  1071. return gg;
  1072. };
  1073. // 获取扩展菜单栏
  1074. DTree.prototype.getExtMenubarDom = function (menu) {
  1075. var _this = this;
  1076. return "<button class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='" + _this.obj[0].id + "' d-menu='" + menu.menubarId + "' title='" + menu.title + "'><i class='" + DTREEFONT + " " + menu.icon + "'></i></button>";
  1077. };
  1078. // 获取依附在工具栏的菜单栏
  1079. DTree.prototype.getMenubarToolDom = function (menu) {
  1080. var _this = this;
  1081. var rootId = _this.obj[0].id;
  1082. switch (menu) {
  1083. case defaultMenu.moveDown:
  1084. _this.toolbarMenu[defaultMenu.moveDown] = "<dd><a dtree-id='" + rootId + "' d-menu='" + defaultMenu.moveDown + "'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_DOWN + "'></i>&nbsp;展开" + _this.toolbarStyle.title + "</a></dd>";
  1085. break;
  1086. case defaultMenu.moveUp:
  1087. _this.toolbarMenu[defaultMenu.moveUp] = "<dd><a dtree-id='" + rootId + "' d-menu='" + defaultMenu.moveUp + "'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_UP + "'></i>&nbsp;收缩" + _this.toolbarStyle.title + "</a></dd>";
  1088. break;
  1089. case defaultMenu.refresh:
  1090. _this.toolbarMenu[defaultMenu.refresh] = "<dd><a dtree-id='" + rootId + "' d-menu='" + defaultMenu.refresh + "'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_REFRESH + "'></i>&nbsp;刷新</a></dd>";
  1091. break;
  1092. case defaultMenu.remove:
  1093. if (_this.checkbar)
  1094. _this.toolbarMenu[defaultMenu.remove] = "<dd><a dtree-id='" + rootId + "' d-menu='" + defaultMenu.remove + "'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_DELETE + "'></i>&nbsp;删除选中" + _this.toolbarStyle.title + "</a></dd>";
  1095. break;
  1096. case defaultMenu.searchNode:
  1097. _this.toolbarMenu[defaultMenu.searchNode] = "<dd><a dtree-id='" + rootId + "' d-menu='" + defaultMenu.searchNode + "'><i class='" + DTREEFONT + " " + LI_DIV_MENUBAR_SEARCH + "'></i>&nbsp;查询" + _this.toolbarStyle.title + "</a></dd>";
  1098. break;
  1099. }
  1100. };
  1101. // 获取依附在工具栏的扩展菜单栏
  1102. DTree.prototype.getExtMenubarToolDom = function (menu) {
  1103. var _this = this;
  1104. _this.toolbarMenu[menu.menubarId] = "<dd><a dtree-id='" + _this.obj[0].id + "' d-menu='" + menu.menubarId + "'><i class='" + DTREEFONT + " " + menu.icon + "'></i>&nbsp;" + menu.title + "</a></dd>";
  1105. };
  1106. // menubar内置方法
  1107. DTree.prototype.menubarMethod = function () {
  1108. var _this = this;
  1109. return {
  1110. openAllNode: function (obj) { // 展开所有节点
  1111. var $ulNode = obj || _this.obj.children("li").children("ul");
  1112. // 遍历所有ul子节点
  1113. for (var i = 0; i < $ulNode.length; i++) {
  1114. // 获取当前节点的信息
  1115. var $ul = $($ulNode[i]),
  1116. $div = $ul.prev("div"),
  1117. $i_spread = $div.find("i[data-spread]").eq(0),
  1118. $i_node = $div.find("i[data-spread]").eq(1),
  1119. i_node_class = $i_node.attr("class"),
  1120. $cite = $div.find("cite[data-leaf]").eq(0),
  1121. spread = $i_spread.attr("data-spread"),
  1122. leaf = $cite.attr("data-leaf");
  1123. if (leaf == "leaf") {
  1124. continue;
  1125. } // 说明是叶子了,则继续循环下一个
  1126. if (spread == "open") {
  1127. // 说明该节点已经展开了,则进行子节点循环
  1128. } else {
  1129. if (_this.type == "load") { //是否全量加载
  1130. if (_this.cache) { //是否开启缓存
  1131. if ($ul.html()) {
  1132. $ul.addClass(NAV_SHOW);
  1133. } else { //加载节点
  1134. _this.getChild($div);
  1135. }
  1136. } else { //每次取新的数据
  1137. $ul.html("");
  1138. _this.getChild($div);
  1139. }
  1140. } else { // 全量加载
  1141. $ul.addClass(NAV_SHOW);
  1142. }
  1143. $div.find("i[data-spread]").attr("data-spread", "open");
  1144. $i_spread.removeClass(_this.ficonClose);
  1145. $i_spread.addClass(_this.ficonOpen);
  1146. var node_class = _this.nodeIconClose;
  1147. if (i_node_class.indexOf(node_class) > 0) {
  1148. $i_node.removeClass(_this.nodeIconClose);
  1149. $i_node.addClass(_this.nodeIconOpen);
  1150. }
  1151. }
  1152. var $childUl = $ul.children("li").children("ul");
  1153. _this.menubarMethod().openAllNode($childUl);
  1154. }
  1155. },
  1156. closeAllNode: function () { //收缩所有节点
  1157. _this.obj.find("." + LI_NAV_CHILD).each(function () {
  1158. // 获取当前节点的信息
  1159. var $ul = $(this),
  1160. $div = $ul.prev("div"),
  1161. $i_spread = $div.find("i[data-spread]").eq(0),
  1162. $i_node = $div.find("i[data-spread]").eq(1),
  1163. i_node_class = $i_node.attr("class"),
  1164. $cite = $div.find("cite[data-leaf]").eq(0),
  1165. spread = $i_spread.attr("data-spread"),
  1166. leaf = $cite.attr("data-leaf");
  1167. $ul.removeClass(NAV_SHOW);
  1168. $div.find("i[data-spread]").attr("data-spread", "close");
  1169. $i_spread.removeClass(_this.ficonOpen);
  1170. $i_spread.addClass(_this.ficonClose);
  1171. var node_class = _this.nodeIconOpen;
  1172. if (i_node_class.indexOf(node_class) > 0) {
  1173. $i_node.removeClass(_this.nodeIconOpen);
  1174. $i_node.addClass(_this.nodeIconClose);
  1175. }
  1176. });
  1177. },
  1178. refreshTree: function () {// 刷新树
  1179. _this.obj.html(""); // 清空树结构
  1180. _this.initNodeParam(); // 清空参数
  1181. _this.init(); //执行初始化方法
  1182. },
  1183. remove: function () {// 删除选中节点
  1184. var len = _this.obj.find("i[data-par][data-checked='1']").length;
  1185. if (len == 0) {
  1186. layer.msg("请至少选中一个节点", {icon: 2});
  1187. } else {
  1188. //操作前先清空
  1189. _this.checkbarNode = [];
  1190. // 选择所有复选框节点
  1191. var i_node = {};
  1192. _this.obj.find("i[data-par][data-checked='1']").each(function () {
  1193. var $i = $(this), $div = $i.closest("." + LI_DIV_ITEM);
  1194. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  1195. });
  1196. layer.confirm('确定要删除选中节点?', {icon: 3, title: '删除选中节点'}, function (index1) {
  1197. var flag = _this.menubarFun.remove(_this.checkbarNode);
  1198. if (flag) {
  1199. _this.obj.find("i[data-par][data-checked='1']").closest("." + LI_DIV_ITEM).next("ul").remove();
  1200. _this.obj.find("i[data-par][data-checked='1']").closest("." + LI_DIV_ITEM).remove();
  1201. _this.checkbarNode = [];
  1202. }
  1203. layer.close(index1);
  1204. });
  1205. }
  1206. },
  1207. searchNode: function () {//模糊查询该值,展开该值节点
  1208. layer.prompt({
  1209. formType: 0,
  1210. value: "",
  1211. title: '查询节点'
  1212. }, function (value, index1, elem) {
  1213. if (value) {
  1214. var flag = _this.searchNode(value);
  1215. if (!flag) {
  1216. layer.msg("该名称节点不存在!", {icon: 5});
  1217. }
  1218. } else {
  1219. layer.msg("未指定查询节点名称", {icon: 5});
  1220. }
  1221. layer.close(index1);
  1222. });
  1223. },
  1224. extMethod: function (menuId, $div, flag) {
  1225. if (_this.menubar && _this.menubarTips.group && _this.menubarTips.group.length > 0 && flag == "group") {
  1226. for (var i = 0; i < _this.menubarTips.group.length; i++) {
  1227. var ext = _this.menubarTips.group[i];
  1228. if (menuId == ext.menubarId) {
  1229. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. if (_this.menubar && _this.menubarTips.toolbar && _this.menubarTips.toolbar.length > 0 && flag == "toolbar") {
  1235. for (var i = 0; i < _this.menubarTips.toolbar.length; i++) {
  1236. var ext = _this.menubarTips.toolbar[i];
  1237. if (menuId == ext.menubarId) {
  1238. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. if (_this.menubar && _this.menubarTips.freedom && _this.menubarTips.freedom.length > 0 && flag == "freedom") {
  1244. for (var i = 0; i < _this.menubarTips.freedom.length; i++) {
  1245. var ext = _this.menubarTips.freedom[i];
  1246. if (menuId == ext.menubarId) {
  1247. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. };
  1254. };
  1255. // menubar监听方法
  1256. DTree.prototype.menubarListener = function (menuId, flag) {
  1257. var _this = this;
  1258. var $div = _this.getNowNode();
  1259. switch (menuId) {
  1260. case defaultMenu.moveDown: // 展开节点
  1261. _this.menubarMethod().openAllNode();
  1262. break;
  1263. case defaultMenu.moveUp: // 收缩节点
  1264. _this.menubarMethod().closeAllNode();
  1265. break;
  1266. case defaultMenu.refresh:
  1267. _this.menubarMethod().refreshTree(); // 刷新树
  1268. break;
  1269. case defaultMenu.remove:
  1270. _this.menubarMethod().remove();
  1271. break;
  1272. case defaultMenu.searchNode:
  1273. _this.menubarMethod().searchNode();
  1274. break;
  1275. default:
  1276. _this.menubarMethod().extMethod(menuId, $div, flag);
  1277. break;
  1278. }
  1279. };
  1280. //模糊查询该值,展开该值节点
  1281. DTree.prototype.searchNode = function (value) {
  1282. var _this = this;
  1283. var b = false;
  1284. var $lis = [];
  1285. _this.obj.find("cite[data-leaf]").each(function () {
  1286. var $nthis = $(this);
  1287. var html = $nthis.html();
  1288. if (html.indexOf(value) > -1) {
  1289. if ($nthis.attr("data-leaf") == "leaf") {
  1290. // 叶子节点提供包含父节点的所有信息
  1291. var title = "";
  1292. $nthis.parents("li").each(function () {
  1293. title = "-" + $(this).find("cite[data-leaf]").html() + title;
  1294. });
  1295. title = title.substring(1, title.length);
  1296. $nthis.attr("title", title);
  1297. }
  1298. // 保存当前cite所在的li及父li中包含该值,则只保留父的
  1299. var i = 0;
  1300. $nthis.parents("li").each(function () {
  1301. var html2 = $(this).find("cite[data-leaf]").html();
  1302. if (html2.indexOf(value) > -1) {
  1303. i++;
  1304. }
  1305. if (i >= 2) {
  1306. return true;
  1307. }
  1308. });
  1309. if (i < 2) {
  1310. $lis.push($nthis.closest("li").prop("outerHTML"));
  1311. }
  1312. }
  1313. });
  1314. if ($lis.length > 0) {
  1315. b = true;
  1316. // 1.将树节点清空
  1317. _this.obj.html("");
  1318. // 2.遍历所有cite节点,展开当前cite节点
  1319. for (var i = 0; i < $lis.length; i++) {
  1320. _this.obj.append($lis[i]);
  1321. }
  1322. }
  1323. return b;
  1324. };
  1325. /******************** 工具栏区域 ********************/
  1326. // 获取工具栏
  1327. DTree.prototype.getToolbarDom = function () {
  1328. var _this = this;
  1329. var toolbarShow = _this.toolbarShow;
  1330. var toolbarExt = _this.toolbarExt;
  1331. if (toolbarShow.length > 0) {
  1332. for (var i = 0; i < toolbarShow.length; i++) {
  1333. var show = toolbarShow[i];
  1334. if (show == "add") {
  1335. _this.toolbarMenu[defaultTool.addToolbar] = "<dd><a dtree-tool='" + defaultTool.addToolbar + "'><i class='" + DTREEFONT + " " + LI_DIV_TOOLBAR_ADD + "'></i>&nbsp;新增" + _this.toolbarStyle.title + "</a></dd>";
  1336. }
  1337. if (show == "edit") {
  1338. _this.toolbarMenu[defaultTool.editToolbar] = "<dd><a dtree-tool='" + defaultTool.editToolbar + "'><i class='" + DTREEFONT + " " + LI_DIV_TOOLBAR_EDIT + "'></i>&nbsp;编辑" + _this.toolbarStyle.title + "</a></dd>";
  1339. }
  1340. if (show == "delete") {
  1341. _this.toolbarMenu[defaultTool.delToolbar] = "<dd><a dtree-tool='" + defaultTool.delToolbar + "'><i class='" + DTREEFONT + " " + LI_DIV_TOOLBAR_DEL + "'></i>&nbsp;删除" + _this.toolbarStyle.title + "</a></dd>";
  1342. }
  1343. }
  1344. }
  1345. if (toolbarExt.length > 0) {
  1346. for (var i = 0; i < toolbarExt.length; i++) {
  1347. var ext = toolbarExt[i];
  1348. _this.toolbarMenu[ext.toolbarId] = "<dd><a dtree-tool='" + ext.toolbarId + "'><i class='" + DTREEFONT + " " + ext.icon + "'></i>&nbsp;" + ext.title + "</a></dd>";
  1349. }
  1350. }
  1351. };
  1352. // 设置工具栏按钮
  1353. DTree.prototype.setToolbarDom = function (toolbarMenu) {
  1354. var _this = this;
  1355. if (toolbarMenu) {
  1356. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).find('div.layui-nav-item>dl.layui-nav-child').html("");
  1357. for (var key in toolbarMenu) {
  1358. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).find('div.layui-nav-item>dl.layui-nav-child').append(toolbarMenu[key]);
  1359. }
  1360. }
  1361. }
  1362. // 加载toolBar中的内容
  1363. DTree.prototype.loadToolBar = function (title, name) {
  1364. var _this = this;
  1365. var toolbarShow = _this.toolbarShow;
  1366. var nodeBarContents = _this.toolbarBtn;
  1367. var html = "";
  1368. switch (name) {
  1369. case defaultTool.addToolbar:
  1370. //1. 必须加载的节点内容
  1371. var nowNode = ['<div class="layui-form-item">',
  1372. '<label class="layui-form-label">当前选中:</label>',
  1373. '<div class="layui-input-block f-input-par">',
  1374. '<input type="text" name="nodeTitle" class="layui-input f-input" value="' + title + '" readonly/>',
  1375. '</div>',
  1376. '</div>'].join('');
  1377. var addNodeName = ['<div class="layui-form-item">',
  1378. '<label class="layui-form-label">新增' + _this.toolbarStyle.title + ':</label>',
  1379. '<div class="layui-input-block f-input-par">',
  1380. '<input type="text" name="addNodeName" class="layui-input f-input" value="" lay-verify="required"/>',
  1381. '</div>',
  1382. '</div>'].join('');
  1383. var addNodeBtn = ['<div class="layui-form-item">',
  1384. '<div class="layui-input-block" style="margin-left:0px;text-align:center;">',
  1385. '<button type="button" class="layui-btn layui-btn-normal btn-w100" lay-submit lay-filter="dtree_addNode_form">确认添加</button>',
  1386. '</div>',
  1387. '</div>'].join('');
  1388. //2. 用户自定义的节点内容
  1389. var addNodeBar = ['<div class="' + TOOLBAR_TOOL + '"><form class="layui-form layui-form-pane" lay-filter="dtree_addNode_form">', nowNode, addNodeName];
  1390. if (nodeBarContents != null && nodeBarContents.length > 0) {
  1391. if (nodeBarContents[0] != null && nodeBarContents[0] != undefined && nodeBarContents[0].length > 0) {
  1392. var addNodeBarContents = nodeBarContents[0];
  1393. for (var j = 0; j < addNodeBarContents.length; j++) {
  1394. var type = addNodeBarContents[j].type;
  1395. if (!type) {
  1396. type = "text";
  1397. }
  1398. switch (type) {
  1399. case "text":
  1400. addNodeBar.push(_this.loadToolBarDetail().text(addNodeBarContents[j]));
  1401. break;
  1402. case "textarea":
  1403. addNodeBar.push(_this.loadToolBarDetail().textarea(addNodeBarContents[j]));
  1404. break;
  1405. case "select":
  1406. addNodeBar.push(_this.loadToolBarDetail().select(addNodeBarContents[j]));
  1407. break;
  1408. case "hidden":
  1409. addNodeBar.push(_this.loadToolBarDetail().hidden(addNodeBarContents[j]));
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. }
  1415. addNodeBar.push(addNodeBtn);
  1416. addNodeBar.push('</form></div>');
  1417. html = addNodeBar.join('');
  1418. break;
  1419. case defaultTool.editToolbar:
  1420. //1. 必须加载的节点内容
  1421. var nowNode = ['<div class="layui-form-item">',
  1422. '<label class="layui-form-label">当前选中:</label>',
  1423. '<div class="layui-input-block f-input-par">',
  1424. '<input type="text" name="nodeTitle" class="layui-input f-input" value="' + title + '" readonly/>',
  1425. '</div>',
  1426. '</div>'].join('');
  1427. var editNodeName = ['<div class="layui-form-item">',
  1428. '<label class="layui-form-label">编辑' + _this.toolbarStyle.title + ':</label>',
  1429. '<div class="layui-input-block f-input-par">',
  1430. '<input type="text" name="editNodeName" class="layui-input f-input" value="' + title + '" lay-verify="required"/>',
  1431. '</div>',
  1432. '</div>'].join('');
  1433. var editNodeBtn = ['<div class="layui-form-item">',
  1434. '<div class="layui-input-block" style="margin-left:0px;text-align:center;">',
  1435. '<button type="button" class="layui-btn layui-btn-normal btn-w100" lay-submit lay-filter="dtree_editNode_form">确认编辑</button>',
  1436. '</div>',
  1437. '</div>'].join('');
  1438. var editNodeBar = ['<div class="' + TOOLBAR_TOOL + '"><form class="layui-form layui-form-pane" lay-filter="dtree_editNode_form">', nowNode, editNodeName];
  1439. //2. 用户自定义的节点内容
  1440. if (nodeBarContents != null && nodeBarContents.length > 0) {
  1441. if (nodeBarContents[1] != null && nodeBarContents[1] != undefined && nodeBarContents[1].length > 0) {
  1442. var editNodeBarContents = nodeBarContents[1];
  1443. for (var j = 0; j < editNodeBarContents.length; j++) {
  1444. var type = editNodeBarContents[j].type;
  1445. if (!type) {
  1446. type = "text";
  1447. }
  1448. switch (type) {
  1449. case "text":
  1450. editNodeBar.push(_this.loadToolBarDetail().text(editNodeBarContents[j]));
  1451. break;
  1452. case "textarea":
  1453. editNodeBar.push(_this.loadToolBarDetail().textarea(editNodeBarContents[j]));
  1454. break;
  1455. case "select":
  1456. editNodeBar.push(_this.loadToolBarDetail().select(editNodeBarContents[j]));
  1457. break;
  1458. case "hidden":
  1459. editNodeBar.push(_this.loadToolBarDetail().hidden(editNodeBarContents[j]));
  1460. break;
  1461. }
  1462. }
  1463. }
  1464. }
  1465. editNodeBar.push(editNodeBtn);
  1466. editNodeBar.push('</form></div>');
  1467. html = editNodeBar.join('');
  1468. break;
  1469. }
  1470. return html;
  1471. };
  1472. // 获取toolbar详细的标签信息
  1473. DTree.prototype.loadToolBarDetail = function () {
  1474. var _this = this;
  1475. return {
  1476. text: function (nodeBarContents) {
  1477. return ['<div class="layui-form-item">',
  1478. '<label class="layui-form-label" title="' + nodeBarContents.label + '">' + nodeBarContents.label + ':</label>',
  1479. '<div class="layui-input-block f-input-par">',
  1480. '<input type="text" name="' + nodeBarContents.name + '" class="layui-input f-input" value="' + (nodeBarContents.value ? nodeBarContents.value : "") + '"/>',
  1481. '</div>',
  1482. '</div>'].join('');
  1483. },
  1484. textarea: function (nodeBarContents) {
  1485. return ['<div class="layui-form-item layui-form-text">',
  1486. '<label class="layui-form-label">' + nodeBarContents.label + ':</label>',
  1487. '<div class="layui-input-block f-input-par">',
  1488. '<textarea name="' + nodeBarContents.name + '" class="layui-textarea f-input">' + (nodeBarContents.value ? nodeBarContents.value : "") + '</textarea>',
  1489. '</div>',
  1490. '</div>'].join('');
  1491. },
  1492. hidden: function (nodeBarContents) {
  1493. return ['<input type="hidden" name="' + nodeBarContents.name + '" class="layui-input f-input" value="' + (nodeBarContents.value ? nodeBarContents.value : "") + '"/>'].join('');
  1494. },
  1495. select: function (nodeBarContents) {
  1496. var optionsData = nodeBarContents.optionsData;
  1497. var options = "";
  1498. var defaultValue = nodeBarContents.value ? nodeBarContents.value : "";
  1499. for (var key in optionsData) {
  1500. if (defaultValue == optionsData[key]) {
  1501. options += "<option value='" + key + "' selected>" + optionsData[key] + "</option>";
  1502. } else {
  1503. options += "<option value='" + key + "'>" + optionsData[key] + "</option>";
  1504. }
  1505. }
  1506. return ['<div class="layui-form-item">',
  1507. '<label class="layui-form-label" title="' + nodeBarContents.label + '">' + nodeBarContents.label + ':</label>',
  1508. '<div class="layui-input-block f-input-par">',
  1509. '<select name="' + nodeBarContents.name + '">',
  1510. options,
  1511. '</select>',
  1512. '</div>',
  1513. '</div>'].join('');
  1514. }
  1515. }
  1516. };
  1517. // 新增节点后改变节点内容
  1518. DTree.prototype.changeTreeNodeAdd = function (returnID) {
  1519. var _this = this;
  1520. var temp = _this.temp;
  1521. var id = temp[0], $ul = temp[1], $div = temp[2], level = temp[3];
  1522. if (returnID) {
  1523. var $thisDiv = _this.obj.find("[data-id='" + id + "']");
  1524. if (typeof returnID === "object") {
  1525. // 如果是JSON格式数据,则将当前DIV删除,重新建造DIV
  1526. $thisDiv.remove();
  1527. var parseData = _this.parseData(returnID);
  1528. if (parseData.treeId()) {
  1529. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.isLast(0), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(), parseData.disabled(), parseData.basicData(), parseData.recordData(), "item"));
  1530. // 建造完毕后,选中该DIV
  1531. var $addDiv = $ul.find("div[data-id='" + returnID.id + "']");
  1532. _this.setNodeParam($addDiv)
  1533. } else {
  1534. layer.msg("添加失败,节点ID为undefined!", {icon: 5});
  1535. // 将li节点删除
  1536. $ul.find("li[data-id='" + id + "']").remove();
  1537. // 重新赋值
  1538. _this.setNodeParam($div);
  1539. // 临时变量制空
  1540. _this.temp = [];
  1541. return;
  1542. }
  1543. } else if (typeof returnID === "string" || typeof this.icon === 'number') {
  1544. $thisDiv.attr("data-id", returnID);
  1545. // 将li节点展示
  1546. $ul.find("li[data-id='" + returnID + "']").show();
  1547. var $addDiv = $ul.find("div[data-id='" + returnID + "']");
  1548. _this.setNodeParam($addDiv)
  1549. }
  1550. // 判断当前点击的节点是否是最后一级节点,如果是,则需要修改节点的样式
  1551. var $icon_i = $div.find("i[data-spread]");
  1552. if ($icon_i.eq(0).attr("data-spread") == "last") {
  1553. $icon_i.attr("data-spread", "open");
  1554. $icon_i.eq(0).removeClass(LI_DIV_SPREAD_LAST);
  1555. $icon_i.eq(0).removeClass(ICON_HIDE);
  1556. $icon_i.eq(0).addClass(_this.ficonOpen);
  1557. $icon_i.eq(1).removeClass(leafIconArray[_this.leafIcon]);
  1558. $icon_i.eq(1).addClass(_this.nodeIconOpen);
  1559. } else { //如果不是,也要修改节点样式
  1560. $icon_i.attr("data-spread", "open");
  1561. $icon_i.eq(0).removeClass(_this.ficonClose);
  1562. $icon_i.eq(0).addClass(_this.ficonOpen);
  1563. $icon_i.eq(1).removeClass(_this.nodeIconClose);
  1564. $icon_i.eq(1).addClass(_this.nodeIconOpen);
  1565. // _this.clickSpread($div);
  1566. }
  1567. $ul.addClass(NAV_SHOW); //展开UL
  1568. } else {
  1569. // 将li节点删除
  1570. $ul.find("li[data-id='" + id + "']").remove();
  1571. // 重新赋值
  1572. _this.setNodeParam($div);
  1573. }
  1574. _this.temp = []; // 临时变量制空
  1575. };
  1576. // 修改节点后改变节点内容
  1577. DTree.prototype.changeTreeNodeEdit = function (flag) {
  1578. var _this = this;
  1579. var temp = _this.temp;
  1580. var $cite = temp[0],
  1581. $div = temp[1];
  1582. if (!flag) {
  1583. $cite.html(title);
  1584. node = _this.getNodeParam($div);
  1585. }
  1586. _this.temp = []; // 临时变量制空
  1587. };
  1588. // 编辑页打开后显示编辑页内容
  1589. DTree.prototype.changeTreeNodeDone = function (param) {
  1590. var _this = this;
  1591. form.val('dtree_editNode_form', param);
  1592. form.render();
  1593. };
  1594. // 删除节点后改变节点内容
  1595. DTree.prototype.changeTreeNodeDel = function (flag) {
  1596. var _this = this;
  1597. var temp = _this.temp;
  1598. var $p_li = temp[0],
  1599. $p_ul = $p_li.parent("ul"),
  1600. $p_div = temp[1];
  1601. if (flag) {
  1602. $p_li.remove();
  1603. // 判断父级ul中是否还存在li,如果不存在,则需要修改节点的样式
  1604. if ($p_ul.children("li").length == 0) {
  1605. var $icon_i = $p_div.find("i[data-spread]");
  1606. $icon_i.attr("data-spread", "last");
  1607. $icon_i.eq(0).removeClass(_this.ficonOpen);
  1608. $icon_i.eq(0).removeClass(_this.ficonClose);
  1609. if (!_this.dot) {
  1610. $icon_i.eq(0).addClass(ICON_HIDE);
  1611. }
  1612. $icon_i.eq(0).addClass(LI_DIV_SPREAD_LAST);
  1613. $icon_i.eq(1).removeClass(_this.nodeIconOpen);
  1614. $icon_i.eq(1).removeClass(_this.nodeIconClose);
  1615. $icon_i.eq(1).addClass(leafIconArray[_this.leafIcon]);
  1616. }
  1617. _this.initNodeParam();
  1618. }
  1619. _this.temp = []; // 临时变量制空
  1620. };
  1621. /******************** 复选框区域 ********************/
  1622. // 初始化复选框的值
  1623. DTree.prototype.chooseDataInit = function (chooseIds) {
  1624. var _this = this;
  1625. var chooseId = chooseIds.split(",");
  1626. for (var i = 0; i < chooseId.length; i++) {
  1627. _this.obj.find("i[dtree-click='" + eventName.checkNodeClick + "']").each(function () {
  1628. if ($(this).attr("data-id") == chooseId[i]) {
  1629. _this.checkStatus($(this)).check();
  1630. }
  1631. });
  1632. }
  1633. // 展开选中节点的父节点
  1634. var $li_parents = _this.obj.find("i[dtree-click='" + eventName.checkNodeClick + "'][data-checked='1']").parents("." + LI_NAV_ITEM);
  1635. $li_parents.children("ul").addClass(NAV_SHOW);
  1636. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.ficonClose).addClass(_this.ficonOpen);
  1637. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.ficonClose).removeClass(_this.ficonClose);
  1638. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.nodeIconClose).addClass(_this.nodeIconOpen);
  1639. $li_parents.children("." + LI_DIV_ITEM).children("i[data-spread]." + _this.nodeIconClose).removeClass(_this.nodeIconClose);
  1640. return _this.getCheckbarNodesParam();
  1641. };
  1642. //实现复选框点击,子集选中父级也选中
  1643. DTree.prototype.checkAllOrNot = function ($i) {
  1644. var _this = this;
  1645. //$i 当前点击的checkbox
  1646. var dataPar = $i.attr("data-par"),
  1647. dataType = $i.attr("data-type"),
  1648. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1649. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1650. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1651. if ($i.attr("data-checked") == "1") {
  1652. // 处理当前节点的选中状态
  1653. _this.checkStatus($i).noCheck();
  1654. // 处理子级节点的选中状态
  1655. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1656. _this.checkStatus($child_li_i).noCheck();
  1657. // 处理父级节点的选中状态
  1658. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1659. var flag = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "'][data-checked='1']").length;
  1660. if (flag == 0) {
  1661. //把父级去掉选中
  1662. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1663. _this.checkStatus($item_i).noCheck();
  1664. }
  1665. }
  1666. } else {
  1667. // 处理当前节点的选中状态
  1668. _this.checkStatus($i).check();
  1669. // 处理子级节点的选中状态
  1670. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1671. _this.checkStatus($child_li_i).check();
  1672. // 处理父级节点的选中状态
  1673. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1674. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1675. //把父级选中
  1676. _this.checkStatus($item_i).check();
  1677. }
  1678. }
  1679. };
  1680. //实现复选框点击, no-all 子集选中父级半选中,子集全选父级选中
  1681. DTree.prototype.checkAllOrNoallOrNot = function ($i) {
  1682. var _this = this;
  1683. //$i 当前点击的checkbox
  1684. var $div = $i.closest("." + LI_DIV_ITEM),
  1685. dataPar = $i.attr("data-par"),
  1686. dataType = $i.attr("data-type"),
  1687. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1688. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1689. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1690. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  1691. // 处理当前节点的选中状态
  1692. _this.checkStatus($i).noCheck();
  1693. // 处理子级节点的选中状态
  1694. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1695. _this.checkStatus($child_li_i).noCheck();
  1696. // 处理父级节点的选中状态
  1697. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1698. var flag = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "'][data-checked='1']").length;
  1699. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1700. if (flag == 0) {
  1701. //把父级去掉选中
  1702. _this.checkStatus($item_i).noCheck();
  1703. } else {
  1704. //把父级半选
  1705. _this.checkStatus($item_i).noallCheck();
  1706. }
  1707. }
  1708. } else { //当前复选框为未选中状态,点击后变为选中状态
  1709. // 处理当前节点的选中状态
  1710. _this.checkStatus($i).check();
  1711. // 处理子级节点的选中状态
  1712. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1713. _this.checkStatus($child_li_i).check();
  1714. // 处理父级节点的选中状态
  1715. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1716. var flag1 = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "'][data-checked='1']").length;
  1717. var flag2 = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']").length;
  1718. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1719. if (flag1 != flag2) {
  1720. // 父级复选框半选
  1721. _this.checkStatus($item_i).noallCheck();
  1722. } else {
  1723. // 父级复选框全选
  1724. _this.checkStatus($item_i).check();
  1725. }
  1726. }
  1727. }
  1728. };
  1729. //实现复选框点击,p-casc:父级选中子集全选,子集无法改变父级选中状态
  1730. DTree.prototype.checkAllOrPcascOrNot = function ($i) {
  1731. var _this = this;
  1732. //$i 当前点击的checkbox
  1733. var $div = $i.closest("." + LI_DIV_ITEM),
  1734. dataPar = $i.attr("data-par"),
  1735. dataType = $i.attr("data-type"),
  1736. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1737. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1738. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1739. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  1740. // 处理当前节点的选中状态
  1741. _this.checkStatus($i).noCheck();
  1742. // 处理子级节点的选中状态
  1743. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1744. _this.checkStatus($child_li_i).noCheck();
  1745. } else { //当前复选框为未选中状态,点击后变为选中状态
  1746. // 处理当前节点的选中状态
  1747. _this.checkStatus($i).check();
  1748. // 处理子级节点的选中状态
  1749. var $child_li_i = $child_li.find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1750. _this.checkStatus($child_li_i).check();
  1751. }
  1752. };
  1753. //实现复选框点击,self:各自选中互不影响
  1754. DTree.prototype.checkOrNot = function ($i) {
  1755. var _this = this;
  1756. //$i 当前点击的checkbox
  1757. var $div = $i.closest("." + LI_DIV_ITEM),
  1758. dataPar = $i.attr("data-par"),
  1759. dataType = $i.attr("data-type"),
  1760. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1761. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1762. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1763. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  1764. // 处理当前节点的选中状态
  1765. _this.checkStatus($i).noCheck();
  1766. } else { //当前复选框为未选中状态,点击后变为选中状态
  1767. // 处理当前节点的选中状态
  1768. _this.checkStatus($i).check();
  1769. }
  1770. };
  1771. //实现复选框点击,only:只能选中1个复选框
  1772. DTree.prototype.checkOnly = function ($i) {
  1773. var _this = this;
  1774. //$i 当前点击的checkbox
  1775. var $div = $i.closest("." + LI_DIV_ITEM),
  1776. dataPar = $i.attr("data-par"),
  1777. dataType = $i.attr("data-type"),
  1778. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1779. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1780. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1781. var checked = $i.attr("data-checked");
  1782. // 将全部节点全部设为未选中状态
  1783. var $all_i = _this.obj.find("i[data-checked]");
  1784. _this.checkStatus($all_i).noCheck();
  1785. if (checked != "1") { //当前复选框为未选中状态,点击后变为选中状态
  1786. // 处理当前节点的选中状态
  1787. _this.checkStatus($i).check();
  1788. }
  1789. };
  1790. //实现复选框点击
  1791. DTree.prototype.changeCheck = function () {
  1792. var _this = this;
  1793. var temp = _this.temp;
  1794. var $i = temp[0];
  1795. // 复选框选中事件
  1796. if (_this.checkbarType == "all") {
  1797. _this.checkAllOrNot($i);
  1798. } else if (_this.checkbarType == "no-all") {
  1799. _this.checkAllOrNoallOrNot($i);
  1800. } else if (_this.checkbarType == "p-casc") {
  1801. _this.checkAllOrPcascOrNot($i);
  1802. } else if (_this.checkbarType == "self") {
  1803. _this.checkOrNot($i);
  1804. } else if (_this.checkbarType == "only") {
  1805. _this.checkOnly($i);
  1806. } else {
  1807. _this.checkAllOrNot($i);
  1808. }
  1809. // 获取复选框选中节点的内容
  1810. var checkbarNodes = _this.setAndGetCheckbarNodesParam();
  1811. // 用户自定义想做的事情
  1812. _this.checkbarFun.chooseDone(checkbarNodes);
  1813. layui.event.call(this, MOD_NAME, "chooseDone(" + $(_this.obj)[0].id + ")", {"checkbarParams": checkbarNodes});
  1814. _this.temp = [];
  1815. };
  1816. //复选框半选状态初始化设置
  1817. DTree.prototype.initNoAllCheck = function () {
  1818. var _this = this;
  1819. //1.获取所有选中节点
  1820. var $is = _this.obj.find("i[data-checked='1']");
  1821. if ($is.length > 0) {
  1822. for (var key = 0; key < $is.length; key++) {
  1823. var $i = $($is[key]),
  1824. dataPar = $i.attr("data-par"),
  1825. dataType = $i.attr("data-type"),
  1826. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1827. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1828. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1829. // 处理父级节点的选中状态
  1830. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1831. var flag1 = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "'][data-checked='1']").length;
  1832. var flag2 = item.eq(i).find(">." + LI_NAV_CHILD + " ." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']").length;
  1833. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1834. if (flag1 != flag2) {
  1835. // 父级复选框半选
  1836. _this.checkStatus($item_i).noallCheck();
  1837. } else {
  1838. // 父级复选框全选
  1839. _this.checkStatus($item_i).check();
  1840. }
  1841. }
  1842. }
  1843. }
  1844. };
  1845. //复选框选中状态初始化设置
  1846. DTree.prototype.initAllCheck = function () {
  1847. var _this = this;
  1848. //1.获取所有选中节点
  1849. var $is = _this.obj.find("i[data-checked='1']");
  1850. if ($is.length > 0) {
  1851. for (var key = 0; key < $is.length; key++) {
  1852. var $i = $($is[key]),
  1853. dataPar = $i.attr("data-par"),
  1854. dataType = $i.attr("data-type"),
  1855. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  1856. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  1857. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  1858. // 处理父级节点的选中状态
  1859. for (var i = 1, item = $parent_li; i < item.length; i++) {
  1860. var $item_i = item.eq(i).find(">." + LI_DIV_ITEM + ">." + LI_DIV_CHECKBAR + ">i[data-type='" + dataType + "']");
  1861. // 父级复选框全选
  1862. _this.checkStatus($item_i).check();
  1863. }
  1864. }
  1865. }
  1866. };
  1867. // 设置复选框选中/未选中/半选 _this.checkStatus($i).check(); _this.checkStatus($i).noCheck(); _this.checkStatus($i).noallCheck();
  1868. DTree.prototype.checkStatus = function ($i) {
  1869. var _this = this;
  1870. return {
  1871. check: function () {
  1872. $i.removeClass(LI_DIV_CHECKBAR_OUT);
  1873. $i.removeClass(LI_DIV_CHECKBAR_NOALL);
  1874. $i.addClass(LI_DIV_CHECKBAR_ON);
  1875. $i.addClass(_this.style.chs);
  1876. $i.attr("data-checked", "1");
  1877. },
  1878. noCheck: function () {
  1879. $i.removeClass(LI_DIV_CHECKBAR_NOALL);
  1880. $i.removeClass(LI_DIV_CHECKBAR_ON);
  1881. $i.removeClass(_this.style.chs);
  1882. $i.addClass(LI_DIV_CHECKBAR_OUT);
  1883. $i.attr("data-checked", "0");
  1884. },
  1885. noallCheck: function () {
  1886. $i.removeClass(LI_DIV_CHECKBAR_OUT);
  1887. $i.removeClass(LI_DIV_CHECKBAR_ON);
  1888. $i.addClass(LI_DIV_CHECKBAR_NOALL);
  1889. $i.addClass(_this.style.chs);
  1890. $i.attr("data-checked", "2");
  1891. }
  1892. }
  1893. };
  1894. // 设置树的复选框操作值的全部参数,并获取
  1895. DTree.prototype.setAndGetCheckbarNodesParam = function () {
  1896. var _this = this;
  1897. //操作前先清空
  1898. _this.checkbarNode = [];
  1899. // 选择所有复选框节点
  1900. if (_this.checkbarData == "change") { //记录变更数据
  1901. _this.obj.find("i[data-par]").each(function () {
  1902. var $i = $(this), $div = $i.closest("." + LI_DIV_ITEM);
  1903. if ($i.attr("data-checked") != $i.attr("data-initchecked")) {
  1904. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  1905. }
  1906. });
  1907. } else if (_this.checkbarData == "all") { //记录全部数据
  1908. _this.obj.find("i[data-par][data-checked]").each(function () {
  1909. var $i = $(this), $div = $i.closest("." + LI_DIV_ITEM);
  1910. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  1911. });
  1912. } else { //记录选中数据
  1913. _this.obj.find("i[data-par][data-checked='1']").each(function () {
  1914. var $i = $(this), $div = $i.closest("." + LI_DIV_ITEM);
  1915. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  1916. });
  1917. }
  1918. return _this.checkbarNode;
  1919. };
  1920. // 获取树的复选框操作值的全部参数
  1921. DTree.prototype.getCheckbarNodesParam = function () {
  1922. var _this = this;
  1923. return _this.setAndGetCheckbarNodesParam();
  1924. };
  1925. // 获取树的一个复选框的参数
  1926. DTree.prototype.getCheckbarNodeParam = function ($div, $i) {
  1927. var _this = this;
  1928. var temp_node = {};
  1929. temp_node.nodeId = $div.attr("data-id");
  1930. temp_node.parentId = $div.parent().attr("data-pid");
  1931. temp_node.context = $div.find("cite[data-leaf]").eq(0).text();
  1932. temp_node.isLeaf = $div.find("cite[data-leaf]").eq(0).attr("data-leaf") == "leaf" ? true : false;
  1933. temp_node.level = $div.parent().attr("data-index");
  1934. temp_node.spread = $div.find("i[data-spread]").eq(0).attr("data-spread") == "open" ? true : false;
  1935. temp_node.basicData = $div.attr("data-basic");
  1936. temp_node.recordData = $div.attr("data-record");
  1937. temp_node.dataType = $i.attr("data-type");
  1938. temp_node.ischecked = $i.attr("data-checked");
  1939. temp_node.initchecked = $i.attr("data-initchecked");
  1940. return temp_node;
  1941. };
  1942. //判断复选框是否发生变更
  1943. DTree.prototype.changeCheckbarNodes = function () {
  1944. var flag = false;
  1945. var _this = this;
  1946. _this.obj.find("i[data-par]").each(function () {
  1947. var $i = $(this);
  1948. $div = $i.closest("." + LI_DIV_ITEM);
  1949. if ($i.attr("data-checked") != $i.attr("data-initchecked")) {
  1950. flag = true;
  1951. return true;
  1952. }
  1953. });
  1954. return flag;
  1955. };
  1956. /******************** iframe区域 ********************/
  1957. // 加载iframe
  1958. DTree.prototype.loadIframe = function ($div, iframeParam) {
  1959. var _this = this;
  1960. var $cite = $div.find("cite[data-leaf]").eq(0);
  1961. if (!_this.useIframe) { // 启用iframe
  1962. return false;
  1963. }
  1964. var iframeElem = _this.iframe.iframeElem,
  1965. iframeUrl = _this.iframe.iframeUrl,
  1966. iframeLoad = _this.iframe.iframeLoad;
  1967. var flag = iframeLoad == "leaf" ? (($cite.attr("data-leaf") == "leaf") ? true : false) : true;
  1968. if (flag) {
  1969. if ($(iframeElem).length > 0) { //iframe存在
  1970. if (!iframeUrl) {
  1971. layer.msg("数据请求异常,iframeUrl参数未指定", {icon: 5});
  1972. return false;
  1973. }
  1974. var param = AjaxHelper.serialize(iframeParam);
  1975. if (iframeUrl.indexOf("?") > -1) {
  1976. param = "&" + param.substring(1, param.length);
  1977. }
  1978. var url = iframeUrl + param;
  1979. $(iframeElem).attr("src", url);
  1980. } else {
  1981. layer.msg("iframe绑定异常,请确认页面中是否有iframe页对应的容器", {icon: 5});
  1982. return false;
  1983. }
  1984. }
  1985. return flag;
  1986. };
  1987. // 获取传递出去的参数,根据iframe.iframeDefaultRequest、iframe.iframeRequest和node拼出发出请求的参数
  1988. DTree.prototype.getIframeRequestParam = function (nodes) {
  1989. var _this = this;
  1990. var request = _this.iframe.iframeRequest,
  1991. defaultRequestNames = _this.iframe.iframeDefaultRequest,
  1992. node = nodes || _this.node,
  1993. requestParam = {};
  1994. // 先拼用户自定义的,在拼树生成的,这样的话用户可以自定义当树未生成时的节点的初始值
  1995. for (var key in request) {
  1996. requestParam[key] = request[key];
  1997. }
  1998. for (var key in defaultRequestNames) {
  1999. var paramName = defaultRequestNames[key];
  2000. var paramValue = node[key];
  2001. if (typeof paramValue === "boolean") {
  2002. requestParam[paramName] = paramValue;
  2003. } else {
  2004. if (paramValue) {
  2005. requestParam[paramName] = paramValue;
  2006. }
  2007. }
  2008. }
  2009. // 解决传递中文的乱码问题
  2010. var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/; //正则匹配中文
  2011. for (var key in requestParam) {
  2012. if (reg.test(requestParam[key])) {
  2013. var str = requestParam[key];
  2014. requestParam[key] = encodeURI(encodeURI(str));
  2015. }
  2016. }
  2017. return requestParam;
  2018. };
  2019. /******************** 数据回调区域 ********************/
  2020. // 获取当前选中节点下一个UL 或根节点。为了将新节点放入ul下
  2021. DTree.prototype.getNowNodeUl = function () {
  2022. var _this = this;
  2023. return (_this.obj.find("div[data-id]").parent().find("." + NAV_THIS).length == 0) ? _this.obj : _this.obj.find("div[data-id]").parent().find("." + NAV_THIS).next("ul");
  2024. };
  2025. // 获取当前选中节点 或根节点。
  2026. DTree.prototype.getNowNode = function () {
  2027. var _this = this;
  2028. return (_this.obj.find("div[data-id]").parent().find("." + NAV_THIS).length == 0) ? _this.obj.children("li").eq(0).children("div").eq(0) : _this.obj.find("div[data-id]").parent().find("." + NAV_THIS);
  2029. };
  2030. // 设置当前选中节点的全部参数
  2031. DTree.prototype.setNodeParam = function ($div) {
  2032. var _this = this;
  2033. _this.node.nodeId = $div.attr("data-id");
  2034. _this.node.parentId = $div.parent().attr("data-pid");
  2035. _this.node.context = $div.find("cite[data-leaf]").eq(0).text();
  2036. _this.node.isLeaf = $div.find("cite[data-leaf]").eq(0).attr("data-leaf") == "leaf" ? true : false;
  2037. _this.node.level = $div.parent().attr("data-index");
  2038. _this.node.spread = $div.find("i[data-spread]").eq(0).attr("data-spread") == "open" ? true : false;
  2039. _this.node.basicData = $div.attr("data-basic");
  2040. _this.node.recordData = $div.attr("data-record");
  2041. if ($div.find("i[data-par]")) {
  2042. var dataTypes = "", ischeckeds = "", initcheckeds = "";
  2043. $div.find("i[data-par]").each(function () {
  2044. dataTypes += $(this).attr("data-type") + ",";
  2045. ischeckeds += $(this).attr("data-checked") + ",";
  2046. initcheckeds += $(this).attr("data-initchecked") + ",";
  2047. });
  2048. dataTypes = dataTypes.substring(0, dataTypes.length - 1);
  2049. ischeckeds = ischeckeds.substring(0, ischeckeds.length - 1);
  2050. initcheckeds = initcheckeds.substring(0, initcheckeds.length - 1);
  2051. _this.node.dataType = dataTypes;
  2052. _this.node.ischecked = ischeckeds;
  2053. _this.node.initchecked = initcheckeds;
  2054. }
  2055. };
  2056. // 获取当前选中节点的全部参数
  2057. DTree.prototype.getNodeParam = function ($div) {
  2058. var _this = this;
  2059. if ($div) {
  2060. _this.setNodeParam($div);
  2061. } else {
  2062. if (_this.obj.find("div[data-id]").parent().find("." + NAV_THIS).length == 0) {
  2063. _this.initNodeParam();
  2064. }
  2065. }
  2066. return this.node;
  2067. };
  2068. // 获取一个临时的node参数
  2069. DTree.prototype.getTempNodeParam = function ($div) {
  2070. var _this = this;
  2071. var temp_node = {};
  2072. temp_node.nodeId = $div.attr("data-id");
  2073. temp_node.parentId = $div.parent().attr("data-pid");
  2074. temp_node.context = $div.find("cite[data-leaf]").eq(0).text();
  2075. temp_node.isLeaf = $div.find("cite[data-leaf]").eq(0).attr("data-leaf") == "leaf" ? true : false;
  2076. temp_node.level = $div.parent().attr("data-index");
  2077. temp_node.spread = $div.find("i[data-spread]").eq(0).attr("data-spread") == "open" ? true : false;
  2078. temp_node.basicData = $div.attr("data-basic");
  2079. temp_node.recordData = $div.attr("data-record");
  2080. if ($div.find("i[data-par]")) {
  2081. var dataTypes = "", ischeckeds = "", initcheckeds = "";
  2082. $div.find("i[data-par]").each(function () {
  2083. dataTypes += $(this).attr("data-type") + ",";
  2084. ischeckeds += $(this).attr("data-checked") + ",";
  2085. initcheckeds += $(this).attr("data-initchecked") + ",";
  2086. });
  2087. dataTypes = dataTypes.substring(0, dataTypes.length - 1);
  2088. ischeckeds = ischeckeds.substring(0, ischeckeds.length - 1);
  2089. initcheckeds = initcheckeds.substring(0, initcheckeds.length - 1);
  2090. temp_node.dataType = dataTypes;
  2091. temp_node.ischecked = ischeckeds;
  2092. temp_node.initchecked = initcheckeds;
  2093. }
  2094. return temp_node;
  2095. };
  2096. // 重置参数
  2097. DTree.prototype.initNodeParam = function () {
  2098. var _this = this;
  2099. _this.node.nodeId = "";
  2100. _this.node.parentId = "";
  2101. _this.node.context = "";
  2102. _this.node.isLeaf = "";
  2103. _this.node.level = "";
  2104. _this.node.spread = "";
  2105. _this.node.dataType = "";
  2106. _this.node.ischecked = "";
  2107. _this.node.initchecked = "";
  2108. _this.node.basicData = "";
  2109. };
  2110. // 获取传递出去的参数,根据defaultRequest、request和node拼出发出请求的参数
  2111. DTree.prototype.getRequestParam = function (nodes) {
  2112. var _this = this;
  2113. var request = _this.request,
  2114. defaultRequestNames = _this.defaultRequest,
  2115. node = nodes || _this.node,
  2116. requestParam = {};
  2117. // 先拼用户自定义的,在拼树生成的,这样的话用户可以自定义当树未生成时的节点的初始值
  2118. for (var key in request) {
  2119. requestParam[key] = request[key];
  2120. }
  2121. for (var key in defaultRequestNames) {
  2122. var paramName = defaultRequestNames[key];
  2123. var paramValue = node[key];
  2124. if (typeof paramValue === "boolean") {
  2125. requestParam[paramName] = paramValue;
  2126. } else {
  2127. if (paramValue) {
  2128. requestParam[paramName] = paramValue;
  2129. }
  2130. }
  2131. }
  2132. return requestParam;
  2133. };
  2134. // 获取filterParam过滤后的requestParam
  2135. DTree.prototype.getFilterRequestParam = function (requestParam) {
  2136. var _this = this;
  2137. var filterRequest = _this.filterRequest;
  2138. return event.cloneObj(requestParam, filterRequest);
  2139. };
  2140. // 获取当前选中值
  2141. DTree.prototype.getNowParam = function () {
  2142. var _this = this;
  2143. return _this.getRequestParam(_this.getNodeParam());
  2144. };
  2145. // 获取参数的上级节点
  2146. DTree.prototype.getParentParam = function (id) {
  2147. var _this = this;
  2148. var $div = _this.obj.find("div[data-id='" + id + "']");
  2149. if ($div.length > 0) {
  2150. return _this.callbackData().parentNode($div);
  2151. } else {
  2152. return {};
  2153. }
  2154. };
  2155. // 获取参数的下级节点
  2156. DTree.prototype.getChildParam = function (id) {
  2157. var _this = this;
  2158. var $div = _this.obj.find("div[data-id='" + id + "']");
  2159. if ($div.length > 0) {
  2160. return _this.callbackData().childNode($div);
  2161. } else {
  2162. return [];
  2163. }
  2164. };
  2165. // 获取回调数据
  2166. DTree.prototype.callbackData = function () {
  2167. var _this = this;
  2168. return {
  2169. dom: function ($dom) { // 获取dom
  2170. return $dom;
  2171. },
  2172. node: function (node) { // 获取当前节点值
  2173. return _this.getRequestParam(node);
  2174. },
  2175. childNode: function ($div) { // 获取下级节点值
  2176. var $childDivs = $div.next("ul").find("li." + LI_NAV_ITEM + " div." + LI_DIV_ITEM);
  2177. var childNode = [];
  2178. if ($childDivs && $childDivs.length > 0) {
  2179. $childDivs.each(function () {
  2180. var $cDiv = $(this);
  2181. childNode.push(_this.getRequestParam(_this.getTempNodeParam($cDiv)));
  2182. });
  2183. }
  2184. return childNode;
  2185. },
  2186. parentNode: function ($div) { // 获取上级节点值
  2187. var pId = $div.parent().attr("data-pid");
  2188. var $pdiv = _this.obj.find("div[data-id='" + pId + "']");
  2189. if ($pdiv.length > 0) {
  2190. return _this.getRequestParam(_this.getTempNodeParam($pdiv));
  2191. } else {
  2192. return {};
  2193. }
  2194. }
  2195. }
  2196. };
  2197. /******************** 事件回调区域 ********************/
  2198. // 绑定浏览器事件
  2199. DTree.prototype.bindBrowserEvent = function () {
  2200. var _this = this;
  2201. // 绑定文件夹展开/收缩的图标的点击事件,点击时给当前节点的div添加选中class
  2202. _this.obj.on("click", "i[data-spread]", function (event) {
  2203. event.stopPropagation();
  2204. var $i = $(this),
  2205. $div = $i.parent("div"),
  2206. $cite = $div.find("cite"),
  2207. node = _this.getNodeParam($div),
  2208. $ul = $div.next("ul"),
  2209. $p_li = $div.parent("li[data-index]"), //当前选中节点的顶级li节点
  2210. $p_ul = $p_li.parent("ul");
  2211. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2212. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2213. _this.obj.find("div[data-id]").parent().find("." + NAV_THIS).removeClass(NAV_THIS);
  2214. _this.obj.find("div[data-id]").parent().find("." + _this.style.itemThis).removeClass(_this.style.itemThis);
  2215. $div.addClass(NAV_THIS);
  2216. $div.addClass(_this.style.itemThis);
  2217. _this.clickSpread($div); // 展开或隐藏节点
  2218. // 树状态改变后,用户自定义想做的事情
  2219. layui.event.call(this, MOD_NAME, "changeTree(" + $(_this.obj)[0].id + ")", {
  2220. param: _this.callbackData().node(node),
  2221. dom: _this.callbackData().dom($i),
  2222. show: _this.callbackData().dom($i).attr("data-spread") == "open" ? true : false
  2223. });
  2224. });
  2225. // 绑定所有子节点div的单击事件,点击时触发加载iframe或用户自定义想做的事情
  2226. _this.obj.on("click", "div[dtree-click='" + eventName.itemNodeClick + "']", function (event) {
  2227. event.stopPropagation();
  2228. var $div = $(this),
  2229. $cite = $div.find("cite"),
  2230. node = _this.getNodeParam($div),
  2231. $ul = $div.next("ul"),
  2232. $p_li = $div.parent("li[data-index]"), //当前选中节点的顶级li节点
  2233. $p_ul = $p_li.parent("ul");
  2234. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2235. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2236. _this.obj.find("div[data-id]").parent().find("." + NAV_THIS).removeClass(NAV_THIS);
  2237. _this.obj.find("div[data-id]").parent().find("." + _this.style.itemThis).removeClass(_this.style.itemThis);
  2238. $div.addClass(NAV_THIS);
  2239. $div.addClass(_this.style.itemThis);
  2240. if (_this.useIframe) {
  2241. var iframeParam = _this.getFilterRequestParam(_this.getIframeRequestParam(node));
  2242. var flag = _this.loadIframe($div, iframeParam); // 加载iframe
  2243. if (flag) {
  2244. // iframe加载完毕后,用户自定义想做的事情
  2245. _this.iframeFun.iframeDone(iframeParam);
  2246. layui.event.call(this, MOD_NAME, "iframeDone(" + $(_this.obj)[0].id + ")", {
  2247. "iframeParam": iframeParam,
  2248. dom: _this.callbackData().dom($div)
  2249. });
  2250. }
  2251. } else {
  2252. // 单击事件执行完毕后,用户自定义想做的事情
  2253. layui.event.call(this, MOD_NAME, "node(" + $(_this.obj)[0].id + ")", {
  2254. param: _this.callbackData().node(node),
  2255. childParams: _this.callbackData().childNode($div),
  2256. parentParam: _this.callbackData().parentNode($div),
  2257. dom: _this.callbackData().dom($div)
  2258. });
  2259. }
  2260. });
  2261. // 绑定所有子节点div的双击事件,暴露on给用户自定义
  2262. _this.obj.on("dblclick", "div[dtree-click='" + eventName.itemNodeClick + "']", function (event) {
  2263. event.stopPropagation();
  2264. var $div = $(this),
  2265. $cite = $div.find("cite"),
  2266. node = _this.getNodeParam($div),
  2267. $ul = $div.next("ul"),
  2268. $p_li = $div.parent("li[data-index]"), //当前选中节点的顶级li节点
  2269. $p_ul = $p_li.parent("ul");
  2270. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2271. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2272. _this.obj.find("div[data-id]").parent().find("." + NAV_THIS).removeClass(NAV_THIS);
  2273. _this.obj.find("div[data-id]").parent().find("." + _this.style.itemThis).removeClass(_this.style.itemThis);
  2274. $div.addClass(NAV_THIS);
  2275. $div.addClass(_this.style.itemThis);
  2276. // 双击事件执行完毕后,用户自定义想做的事情
  2277. layui.event.call(this, MOD_NAME, "nodedblclick(" + $(_this.obj)[0].id + ")", {
  2278. param: _this.callbackData().node(node),
  2279. childParams: _this.callbackData().childNode($div),
  2280. parentParam: _this.callbackData().parentNode($div),
  2281. dom: _this.callbackData().dom($div)
  2282. });
  2283. });
  2284. //绑定所有子节点div的右键点击事件,用于显示toolbar
  2285. _this.obj.on("contextmenu", "div[dtree-click='" + eventName.itemNodeClick + "'][d-contextmenu]", function (e) {
  2286. var $div = $(this),
  2287. node = _this.getNodeParam($div),
  2288. contextmenu = $div.attr("d-contextmenu");
  2289. if (_this.toolbar) {
  2290. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2291. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2292. // toolbar加载前执行的方法,执行完毕之后创建按钮
  2293. _this.setToolbarDom(_this.toolbarFun.loadToolbarBefore(event.cloneObj(_this.toolbarMenu), _this.getRequestParam(node), $div));
  2294. var e = e || window.event,
  2295. mx = e.pageX - $div.offset().left + 45,
  2296. my = $div.offset().top - _this.obj.closest(_this.toolbarScroll).offset().top + 15;
  2297. if (contextmenu == "true") {
  2298. _this.obj.find("div[data-id]").parent().find("." + NAV_THIS).removeClass(NAV_THIS);
  2299. _this.obj.find("div[data-id]").parent().find("." + _this.style.itemThis).removeClass(_this.style.itemThis);
  2300. $div.addClass(NAV_THIS);
  2301. $div.addClass(_this.style.itemThis);
  2302. $toolBarDiv.find(".layui-nav-child").addClass('layui-anim-fadein layui-show');
  2303. $toolBarDiv.css({'left': mx + 'px', 'top': my + 'px'});
  2304. }
  2305. }
  2306. e.stopPropagation();
  2307. return false;
  2308. });
  2309. // 绑定装载树的上层出现滚动条的容器,让toolbar隐藏
  2310. _this.obj.closest(_this.toolbarScroll).scroll(function () {
  2311. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2312. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2313. });
  2314. // 绑定toolbar的点击事件
  2315. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).on("click", "a[dtree-tool]", function (event) {
  2316. event.stopPropagation();
  2317. var $div = _this.getNowNode(),
  2318. node = _this.getNodeParam($div),
  2319. $ul = $div.next("ul"),
  2320. $p_li = $div.parent("li[data-index]"), //当前选中节点的顶级li节点
  2321. $p_ul = $p_li.parent("ul"), //当前选中节点的顶级li节点的父级ul
  2322. $p_div = $p_ul.prev("div"), //当前选中节点的顶级li节点的父级ul的前一个div
  2323. $cite = $div.children("cite"), //当前选中节点的text
  2324. title = $cite.html();
  2325. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2326. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2327. var tool = $(this).attr("dtree-tool");
  2328. switch (tool) {
  2329. case defaultTool.addToolbar:
  2330. var content = _this.loadToolBar(title, defaultTool.addToolbar);
  2331. layer.open({
  2332. title: "新增" + _this.toolbarStyle.title,
  2333. type: 1,
  2334. area: _this.toolbarStyle.area,
  2335. content: content,
  2336. success: function (layero, index) {
  2337. form.render();
  2338. form.on("submit(dtree_addNode_form)", function (data) {
  2339. var data = data.field;
  2340. var parentId = $div.attr("data-id"),
  2341. id = $div.attr("data-id") + "_node_" + $ul[0].childNodes.length,
  2342. isLeaf = true,
  2343. isChecked = "0",
  2344. level = parseInt($p_li.attr("data-index")) + 1;
  2345. // 创建子节点的DOM,添加子节点
  2346. var checkArr = [];
  2347. if (_this.checkArrLen > 0) {
  2348. for (var i = 0; i < _this.checkArrLen; i++) {
  2349. checkArr.push({"type": i, "isChecked": "0"});
  2350. }
  2351. }
  2352. $ul.append(_this.getLiItemDom(id, parentId, data.addNodeName, true, "", checkArr, level, false, false, "", "", "item"));
  2353. // 先将li节点隐藏
  2354. $ul.find("li[data-id='" + id + "']").hide();
  2355. // 重新赋值
  2356. var $addDiv = $ul.find("div[data-id='" + id + "']");
  2357. node = _this.getNodeParam($addDiv);
  2358. //获取组装后的requestNode,组合参数
  2359. var requestNode = _this.getRequestParam(node);
  2360. requestNode = $.extend(requestNode, data);
  2361. _this.temp = [id, $ul, $div, level];
  2362. // 用户自定义想做的事情
  2363. _this.toolbarFun.addTreeNode(requestNode, $div);
  2364. layer.close(index);
  2365. return false;
  2366. });
  2367. }
  2368. });
  2369. break;
  2370. case defaultTool.editToolbar:
  2371. var content = _this.loadToolBar(title, defaultTool.editToolbar);
  2372. layer.open({
  2373. title: "编辑" + _this.toolbarStyle.title,
  2374. type: 1,
  2375. area: _this.toolbarStyle.area,
  2376. content: content,
  2377. success: function (layero, index) {
  2378. _this.toolbarFun.editTreeLoad(_this.getRequestParam(node));
  2379. form.render();
  2380. form.on("submit(dtree_editNode_form)", function (data) {
  2381. var data = data.field;
  2382. $cite.html(data.editNodeName);
  2383. node = _this.getNodeParam($div);
  2384. var requestNode = _this.getRequestParam(node);
  2385. requestNode = $.extend(requestNode, data);
  2386. _this.temp = [$cite, $div];
  2387. _this.toolbarFun.editTreeNode(requestNode, $div);
  2388. layer.close(index);
  2389. });
  2390. }
  2391. });
  2392. break;
  2393. case defaultTool.delToolbar:
  2394. layer.confirm('确定要删除该' + _this.toolbarStyle.title + '?', {
  2395. icon: 3,
  2396. title: '删除' + _this.toolbarStyle.title
  2397. }, function (index) {
  2398. var node = _this.getNodeParam($div);
  2399. _this.temp = [$p_li, $p_div];
  2400. _this.toolbarFun.delTreeNode(_this.getRequestParam(node), $div);
  2401. layer.close(index);
  2402. });
  2403. break;
  2404. default:
  2405. var toolbarId = $(this).attr("dtree-tool");
  2406. if (_this.toolbarExt.length > 0) {
  2407. for (var i = 0; i < _this.toolbarExt.length; i++) {
  2408. var ext = _this.toolbarExt[i];
  2409. if (toolbarId == ext.toolbarId) {
  2410. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  2411. break;
  2412. }
  2413. }
  2414. }
  2415. break;
  2416. }
  2417. });
  2418. // 绑定menubar的点击事件
  2419. _this.obj.prevAll('div#dtree_menubar_' + _this.obj[0].id).on("click", "button[d-menu]", function (event) {
  2420. event.stopPropagation();
  2421. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2422. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2423. _this.menubarListener($(this).attr("d-menu"), "group");
  2424. });
  2425. // 绑定menubar的点击事件
  2426. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).on("click", "a[d-menu]", function (event) {
  2427. event.stopPropagation();
  2428. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2429. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2430. _this.menubarListener($(this).attr("d-menu"), "toolbar");
  2431. });
  2432. // 绑定menubar的点击按钮事件
  2433. _this.obj.closest('body').find("*[dtree-id='" + _this.obj[0].id + "'][dtree-menu]").on("click", function (event) {
  2434. event.stopPropagation();
  2435. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2436. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2437. _this.menubarListener($(this).attr("dtree-menu"), "freedom");
  2438. });
  2439. // 绑定cheboxbar的节点复选框
  2440. _this.obj.on("click", "i[dtree-click='" + eventName.checkNodeClick + "']", function (event) {
  2441. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id);
  2442. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2443. var $i = $(this),
  2444. $div = $i.closest("div[dtree-click='" + eventName.itemNodeClick + "']"),
  2445. node = _this.getNodeParam($div);
  2446. // 复选框选中前的回调
  2447. var flag = _this.checkbarFun.chooseBefore($i, _this.getRequestParam(node));
  2448. _this.temp = [$i];
  2449. if (flag) {
  2450. _this.changeCheck();
  2451. }
  2452. event.stopPropagation();
  2453. });
  2454. };
  2455. // 绑定body的单击,让本页面所有的toolbar隐藏
  2456. $BODY.on("click", function (event) {
  2457. $("div." + LI_DIV_TOOLBAR).find(".layui-show").removeClass('layui-anim-fadein layui-show');
  2458. });
  2459. // 解绑浏览器事件
  2460. DTree.prototype.unbindBrowserEvent = function () {
  2461. var _this = this;
  2462. // 本身事件解绑
  2463. _this.obj.unbind();
  2464. // 菜单栏解绑
  2465. if (_this.menubar) {
  2466. _this.obj.prevAll('div#dtree_menubar_' + _this.obj[0].id).unbind();
  2467. if (_this.obj.closest('body').find("*[dtree-id='" + _this.obj[0].id + "'][dtree-menu]").length > 0) {
  2468. _this.obj.closest('body').find("*[dtree-id='" + _this.obj[0].id + "'][dtree-menu]").unbind();
  2469. }
  2470. }
  2471. // 工具栏解绑
  2472. if (_this.toolbar) {
  2473. _this.obj.prevAll('div#dtree_toolbar_' + _this.obj[0].id).unbind();
  2474. if (_this.obj.closest(_this.toolbarScroll).length > 0) {
  2475. _this.obj.closest(_this.toolbarScroll).unbind();
  2476. }
  2477. }
  2478. };
  2479. /** 外部访问 **/
  2480. var dtree = {
  2481. render: function (options) { // 初始化树
  2482. var dTree = null;
  2483. var id = event.getElemId(options);
  2484. if (id == "") {
  2485. layer.msg("页面中未找到绑定id", {icon: 5});
  2486. } else {
  2487. dTree = DTrees[id];
  2488. if (typeof dTree === 'object') {
  2489. dTree.reloadSetting(options);
  2490. dTree.initTreePlus();
  2491. dTree.openTreePlus();
  2492. dTree.init();
  2493. dTree.unbindBrowserEvent();
  2494. dTree.bindBrowserEvent();
  2495. } else {
  2496. // 创建树
  2497. dTree = new DTree(options);
  2498. // 添加到树数组中去
  2499. DTrees[id] = dTree;
  2500. dTree.initTreePlus();
  2501. dTree.openTreePlus();
  2502. dTree.init();
  2503. dTree.bindBrowserEvent();
  2504. }
  2505. }
  2506. return dTree;
  2507. },
  2508. reload: function (dTree, options) {
  2509. if (typeof dTree === "string") {
  2510. dTree = DTrees[dTree];
  2511. }
  2512. if (typeof dTree === "undefined") {
  2513. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2514. return;
  2515. }
  2516. dTree.reloadSetting(options);
  2517. dTree.initTreePlus();
  2518. dTree.openTreePlus();
  2519. dTree.init();
  2520. dTree.unbindBrowserEvent();
  2521. dTree.bindBrowserEvent();
  2522. },
  2523. on: function (events, callback) { // 绑定事件
  2524. if (events.indexOf("'") > 0) {
  2525. events = events.replace(/'/g, "");
  2526. }
  2527. if (events.indexOf('"') > 0) {
  2528. events = events.replace(/"/g, "");
  2529. }
  2530. return layui.onevent.call(this, MOD_NAME, events, callback);
  2531. },
  2532. getNowParam: function (dTree) {
  2533. if (typeof dTree === "string") {
  2534. dTree = DTrees[dTree];
  2535. }
  2536. if (typeof dTree === "undefined") {
  2537. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2538. return;
  2539. }
  2540. return dTree.getNowParam(); // 获取当前选中值
  2541. },
  2542. getParentParam: function (dTree, id) { // 获取参数的上级节点
  2543. if (typeof dTree === "string") {
  2544. dTree = DTrees[dTree];
  2545. }
  2546. if (typeof dTree === "undefined") {
  2547. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2548. return;
  2549. }
  2550. return dTree.getParentParam(id);
  2551. },
  2552. getChildParam: function (dTree, id) { // 获取参数的全部下级节点
  2553. if (typeof dTree === "string") {
  2554. dTree = DTrees[dTree];
  2555. }
  2556. if (typeof dTree === "undefined") {
  2557. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2558. return;
  2559. }
  2560. return dTree.getChildParam(id);
  2561. },
  2562. getCheckbarNodesParam: function (dTree) {
  2563. if (typeof dTree === "string") {
  2564. dTree = DTrees[dTree];
  2565. }
  2566. if (typeof dTree === "undefined") {
  2567. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2568. return {};
  2569. }
  2570. return dTree.getCheckbarNodesParam(); // 获取复选框选中值
  2571. },
  2572. dataInit: function (dTree, chooseId) { // 初始化选中树,针对数据返选
  2573. if (typeof dTree === "string") {
  2574. dTree = DTrees[dTree];
  2575. }
  2576. if (typeof dTree === "undefined") {
  2577. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2578. return;
  2579. }
  2580. if (chooseId) {
  2581. return dTree.dataInit(chooseId);
  2582. }
  2583. },
  2584. chooseDataInit: function (dTree, chooseIds) { // 初始化复选框的值
  2585. if (typeof dTree === "string") {
  2586. dTree = DTrees[dTree];
  2587. }
  2588. if (typeof dTree === "undefined") {
  2589. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2590. return;
  2591. }
  2592. if (chooseIds) {
  2593. return dTree.chooseDataInit(chooseIds);
  2594. }
  2595. },
  2596. changeCheckbarNodes: function (dTree) { //判断复选框是否发生变更
  2597. if (typeof dTree === "string") {
  2598. dTree = DTrees[dTree];
  2599. }
  2600. if (typeof dTree === "undefined") {
  2601. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2602. return;
  2603. }
  2604. return dTree.changeCheckbarNodes();
  2605. },
  2606. refreshTree: function (dTree) { //刷新树,并具有数据回显的功能,自动识别复选框or单选(未完成)
  2607. if (typeof dTree === "string") {
  2608. dTree = DTrees[dTree];
  2609. }
  2610. if (typeof dTree === "undefined") {
  2611. layer.msg("方法获取失败,请检查ID或对象传递是否正确", {icon: 2});
  2612. return;
  2613. }
  2614. },
  2615. escape: function (html) {
  2616. return event.escape(html);
  2617. },
  2618. unescape: function (str) {
  2619. return event.unescape(str);
  2620. },
  2621. version: function () {
  2622. return VERSION;
  2623. }
  2624. };
  2625. exports('dtree', dtree);
  2626. });