dtree.js 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533
  1. /**
  2. *@Name dtree 树形组件
  3. *@Author 智慧的小西瓜
  4. *@DOCS http://www.wisdomelon.com/DTreeHelper/
  5. *@License https://www.layui.com/
  6. *@LASTTIME 2019/06/14
  7. *@VERSION v2.5.4
  8. */
  9. layui.define(['jquery','layer','form'], function(exports) {
  10. var $ = layui.$,
  11. layer = layui.layer,
  12. form = layui.form;
  13. // 树的公共定义样式汇总
  14. var LI_NAV_CHILD = "dtree-nav-ul-sid", LI_NAV_ITEM = "dtree-nav-item",
  15. LI_DIV_ITEM = "dtree-nav-div", DTREEFONTSPECIAL="dtreefont-special", NONETITLE="dtree-none-text",
  16. LI_DIV_MENUBAR = "dtree-menubar",
  17. LI_DIV_TOOLBAR = "dtree-toolbar", TOOLBAR_TOOL = "dtree-toolbar-tool", TOOLBAR_TOOL_EM = "dtree-toolbar-fixed",
  18. LI_DIV_CHECKBAR = "dtree-nav-checkbox-div",
  19. LI_CLICK_CHECKBAR = "d-click-checkbar", //绑定点击复选框时需要用到
  20. LI_DIV_TEXT_CLASS = "t-click", UL_ROOT="dtree",
  21. LI_NAV_FIRST_LINE = "dtree-nav-first-line", LI_NAV_LINE = "dtree-nav-line", LI_NAV_LAST_LINE = "dtree-nav-last-line";
  22. // 树的公共指定
  23. var NAV_THIS = "dtree-nav-this", //当前节点
  24. NAV_SHOW = "dtree-nav-show", //显示子节点
  25. NAV_HIDE = "dtree-nav-hide", //隐藏节点
  26. NAV_DIS = "dtree-disabled", //禁用节点
  27. ICON_HIDE = "dtree-icon-hide", //隐藏图标
  28. $BODY = $("body"), //body选择器
  29. $WIN = $(window), //window窗口
  30. $DOC = $(document), //当前文档
  31. MOD_NAME = "dtree", //模块名称
  32. VERSION = "v2.5.0", //版本
  33. DTrees = {}; //当前被实例化的树的集合
  34. // 树的自定义图标
  35. var DTREEFONT = "dtreefont", //默认使用图标字体
  36. LI_DIV_CHECKBAR_ON = "dtree-icon-fuxuankuangxuanzhong", //复选框选中图标
  37. LI_DIV_CHECKBAR_OUT = "dtree-icon-fuxuankuang", //复选框未选中图标
  38. LI_DIV_CHECKBAR_NOALL = "dtree-icon-fuxuankuang-banxuan", //复选框半选图标
  39. LI_DIV_MENUBAR_DOWN = "dtree-icon-move-down", //menubar的展开全部的图标
  40. LI_DIV_MENUBAR_UP = "dtree-icon-move-up", //menubar的收缩全部的图标
  41. LI_DIV_MENUBAR_REFRESH = "dtree-icon-refresh", //menubar的刷新图标
  42. LI_DIV_MENUBAR_CHECKALL = "dtree-icon-roundcheckfill", //menubar的全选图标
  43. LI_DIV_MENUBAR_UNCHECKALL = "dtree-icon-roundclosefill", //menubar的全不选图标
  44. LI_DIV_MENUBAR_INVERTALL = "dtree-icon-roundcheck", //menubar的反选图标
  45. LI_DIV_MENUBAR_DELETE = "dtree-icon-delete1", //menubar的删除图标
  46. LI_DIV_MENUBAR_SEARCH = "dtree-icon-search_list_light", //menubar的搜索图标
  47. LI_DIV_TOOLBAR_PULLDOWN = "dtree-icon-pulldown", //toolbar的展开图标
  48. LI_DIV_TOOLBAR_PULLUP = "dtree-icon-pullup", //toolbar的收缩图标
  49. LI_DIV_TOOLBAR_ADD = "dtree-icon-roundadd", //toolbar的新增图标
  50. LI_DIV_TOOLBAR_EDIT = "dtree-icon-bianji", //toolbar的编辑图标
  51. LI_DIV_TOOLBAR_DEL = "dtree-icon-roundclose"; //toolbar的删除图标
  52. // 树的非叶子节点图标集合
  53. var nodeIconArray = {
  54. "-1": {"open": "dtree-icon-null-open", "close": "dtree-icon-null-close"}, //未指定
  55. "0" : {"open": "dtree-icon-wenjianjiazhankai", "close": "dtree-icon-weibiaoti5"}, //文件夹(二级图标默认样式)
  56. "1" : {"open": "dtree-icon-jian", "close": "dtree-icon-jia"}, //+-图标(一级图标默认样式)
  57. "2" : {"open": "dtree-icon-xiangxia1", "close": "dtree-icon-xiangyou"} //箭头图标
  58. };
  59. // 树的叶子节点图标集合
  60. var leafIconArray = {
  61. "-1": "dtree-icon-null", //未指定
  62. "0" : "dtree-icon-weibiaoti5", //文件夹
  63. "1" : "dtree-icon-yonghu", //人员
  64. "2" : "dtree-icon-fenzhijigou", //机构
  65. "3" : "dtree-icon-fenguangbaobiao", //报表
  66. "4" : "dtree-icon-xinxipilu", //信息
  67. "5" : "dtree-icon-shuye1", //叶子(二级图标默认样式)
  68. "6" : "dtree-icon-caidan_xunzhang", //勋章
  69. "7" : "dtree-icon-normal-file", //文件
  70. "8" : "dtree-icon-dian", //小圆点(一级图标默认样式)
  71. "9" : "dtree-icon-set-sm", //齿轮
  72. "10" : "dtree-icon-rate" //星星
  73. };
  74. // 树的自定义样式
  75. var DTREE = "dtree-", //自定义样式前缀
  76. ITEMTHIS = "-item-this", //自定义样式当前行选中后缀
  77. ITEM = "-item", //自定义样式当前行后缀
  78. DFONT = "-dtreefont", //自定义样式图标样式后缀
  79. FICON = "-ficon", //自定义样式一级图标样式后缀
  80. ICON = "-icon", //自定义样式二级图标样式后缀
  81. CBOX = "-checkbox", //自定义样式复选框样式后缀
  82. CHS = "-choose"; //自定义样式复选框选中样式后缀
  83. // 树自定义操作事件名称集合 绑定dtree-click的事件
  84. var eventName = {
  85. checkNodeClick: "checkNodeClick", //点击复选框
  86. itemNodeClick: "itemNodeClick" //点击子节点div
  87. };
  88. // 树默认toolbar提供的功能集合 绑定dtree-tool的事件
  89. var defaultTool = {
  90. pulldown: "pulldown", //点击展开当前节点下的全部节点
  91. pullup: "pullup", //点击收缩当前节点下的全部节点
  92. addTool: "addToolbar", //点击toolbar新增
  93. editTool: "editToolbar", //点击toolbar编辑
  94. delTool: "delToolbar" //点击toolbar删除
  95. };
  96. // 树默认menubar提供的功能集合 绑定dtree-menu的事件
  97. var defaultMenu = {
  98. moveDown: "moveDown", //menubar展开全部节点
  99. moveUp: "moveUp", //menubar收缩全部节点
  100. refresh: "refresh", //menubar刷新树
  101. checkAll: "checkAll", //menubar全选
  102. unCheckAll: "unCheckAll", //menubar全不选
  103. invertAll: "invertAll", //menubar反选
  104. remove: "remove", //menubar删除选中节点
  105. searchNode: "searchNode" //menubar查询节点
  106. };
  107. // 树的公共事件
  108. var event = {
  109. getElemId: function(options){ // 根据传入的参数获取ID
  110. var elem = options.elem || "";
  111. var obj = options.obj || $(elem);
  112. if (obj.length == 0) { //页面中未找到绑定id
  113. return "";
  114. } else {
  115. return $(obj)[0].id;
  116. }
  117. },
  118. escape: function(html){
  119. if(typeof html !== 'string') return '';
  120. return html.replace(entityReg.escape, function(match){return entityMap.escape[match];});
  121. },
  122. unescape: function(str){
  123. if(typeof str !== 'string') return '';
  124. return str.replace(entityReg.unescape, function(match){return entityMap.unescape[match];});
  125. },
  126. cloneObj: function (obj, filter) { //深复制对象方法
  127. var newObj = {};
  128. if (obj instanceof Array) {
  129. newObj = [];
  130. }
  131. var str = "";
  132. if(typeof filter !== 'undefined') {str = filter.join(",");}
  133. for (var key in obj) {
  134. if(str.indexOf(key) == -1){
  135. var val = obj[key];
  136. newObj[key] = typeof val === 'object' ? event.cloneObj(val, typeof filter !== undefined ? filter : []): val;
  137. }
  138. }
  139. return newObj;
  140. },
  141. trimToDot: function(str){
  142. return str.replace(/ /g, ".");
  143. }
  144. };
  145. // 特殊符号转义
  146. var keys = Object.keys || function(obj) {
  147. obj = Object(obj);
  148. var arr = [];
  149. for(var a in obj) arr.push(a);
  150. return arr;
  151. };
  152. var invert = function(obj){
  153. obj = Object(obj);
  154. var result = {};
  155. for(var a in obj) result[obj[a]] = a;
  156. return result;
  157. };
  158. var entityMap = {
  159. escape: {
  160. "&" : "&",
  161. "<" : "&lt;",
  162. ">" : "&gt;",
  163. "'" : "&quo;"
  164. }
  165. };
  166. entityMap.unescape = invert(entityMap.escape);
  167. var entityReg = {
  168. escape: RegExp('[' + keys(entityMap.escape).join('') + ']', 'g'),
  169. unescape: RegExp('(' + keys(entityMap.unescape).join('|') + ')', 'g')
  170. };
  171. //异步加载接口
  172. var AjaxHelper = {
  173. request : function(config) {
  174. var data = config.data ? config.data : {};
  175. var async = (typeof (config.async) === "boolean") ? config.async : true;
  176. $.ajax({
  177. type : config.type ? config.type : "POST",
  178. headers : config.headers,
  179. url : config.url,
  180. dataType : config.dataType ? config.dataType : "json",
  181. data : data,
  182. async : async,
  183. contentType : config.contentType ? config.contentType : "application/x-www-form-urlencoded",
  184. success : config.success,
  185. error : function(XMLHttpRequest, textStatus, errorThrown) {
  186. if (typeof (config.error) === "function") {
  187. config.error(XMLHttpRequest, textStatus, errorThrown);
  188. } else {
  189. layer.msg("异步加载失败: " + textStatus,{icon:5, shift:6});
  190. }
  191. },
  192. statusCode : {
  193. 404 : function() {
  194. layer.msg('未找到指定请求,请检查访问路径!',{icon:5, shift:6});
  195. },
  196. 500 : function() {
  197. layer.msg('系统错误!',{icon:5, shift:6});
  198. }
  199. },
  200. complete : function(XMLHttpRequest, textStatus) {
  201. if (typeof (config.complete) === "function") {
  202. config.complete(XMLHttpRequest, textStatus);
  203. }
  204. }
  205. });
  206. },
  207. serialize: function(param){ //json序列化 key=value&key1=value1
  208. var p = "?";
  209. for (var key in param) {
  210. p += key + "=" + param[key] + "&";
  211. }
  212. p = p.substring(0, p.length-1);
  213. return p;
  214. }
  215. };
  216. // 树类
  217. var DTree = function(options){
  218. var _this = this;
  219. /** 默认赋值**/
  220. this.formatter = { // 数据过滤
  221. title: false // 文字,默认不开启
  222. };
  223. this.response = { // 树返回的json格式
  224. statusName: "code", //返回标识
  225. statusCode: 200, //返回码
  226. message: "message", //返回信息
  227. rootName: "data", //根节点名称
  228. treeId: "id", //节点ID
  229. parentId: "parentId", //父节点ID
  230. title: "title", //节点名称
  231. ficonClass: "ficonClass", //自定义一级图标
  232. iconClass: "iconClass", //自定义二级图标
  233. childName: "children", //子节点名称
  234. last: "last", //是否最后一级节点
  235. // level: "level", //层级
  236. spread: "spread", //展开
  237. disabled: "disabled", //禁用
  238. hide: "hide", //隐藏
  239. checkArr: "checkArr", //复选框列表
  240. checked: "checked", //是否选中
  241. type: "type", //复选框标记
  242. basicData: "basicData" //表示用户自定义需要存储在树节点中的数据
  243. };
  244. this.defaultRequest = { // 树的默认发起请求参数格式,最后会将value作为参数名称传递
  245. nodeId: "nodeId", //节点ID
  246. parentId: "parentId", //父节点ID
  247. context: "context", //节点内容
  248. leaf: "leaf", //是否叶子节点
  249. level: "level", //层级
  250. spread: "spread", //节点展开状态
  251. dataType: "dataType", //节点标记
  252. checked: "checked", //节点复选框选中状态
  253. initchecked: "initchecked", //节点复选框初始状态
  254. basicData: "basicData", //用户自定义的记录节点数据
  255. recordData: "recordData", //当前data数据(排除basicData和children字段)
  256. };
  257. this.toolbarFun = {
  258. addTreeNode: function(param, $div) { //添加树节点后调用的函数,用于用户自定义,如未指定则树不会发生变化
  259. return ;
  260. },
  261. editTreeNode: function(param, $div) { //编辑树节点后调用的函数,用于用户自定义,如未指定则树不会发生变化
  262. return ;
  263. },
  264. editTreeLoad: function(param){ // 编辑树的数据回显,用于打开编辑时,回填数据
  265. return ;
  266. },
  267. delTreeNode: function(param, $div){ //删除树后调用的函数,用于用户自定义,如未指定则树不会发生变化
  268. return ;
  269. },
  270. loadToolbarBefore: function(buttons, param, $div){ // 右键菜单加载前的函数
  271. return buttons;
  272. }
  273. };
  274. this.toolbarStyle = {
  275. title: "节点",
  276. area: ["60%","80%"]
  277. };
  278. this.menubarFun = {
  279. remove: function(checkbarNodes){ //删除复选框选中节点,需要用户自定义,如未指定则树只是页面上做了修改
  280. return true;
  281. }
  282. };
  283. this.menubarTips = {
  284. toolbar: [],
  285. group: [defaultMenu.moveDown, defaultMenu.moveUp, defaultMenu.refresh, defaultMenu.checkAll, defaultMenu.unCheckAll, defaultMenu.invertAll, defaultMenu.remove, defaultMenu.searchNode],
  286. freedom: []
  287. };
  288. this.checkbarFun = {
  289. chooseBefore: function($i, node){ // 复选框点击前回调
  290. return true;
  291. },
  292. chooseDone: function(checkbarNodesParam) { //复选框点击事件完毕后,返回该树关于复选框操作的全部信息,用于用户自定义,如未指定则树只是页面上做了修改
  293. return ;
  294. }
  295. };
  296. this.iframe = { // 树点击节点时,打开iframe页面参数配置
  297. iframeElem: "", //iframe的ID
  298. iframeUrl: "", //树关联的frame地址
  299. iframeLoad: "leaf", //点击哪一层加载frame: node:所有节点, leaf:默认,最后一级
  300. iframeDefaultRequest: { //iframe的默认参数,目的是与加载树的参数不一样
  301. nodeId: "nodeId", //节点ID
  302. parentId: "parentId", //父节点ID
  303. context: "context", //节点内容
  304. leaf: "leaf", //是否叶子节点
  305. level: "level", //层级
  306. spread: "spread", //节点展开状态
  307. dataType: "dataType", //节点标记
  308. checked: "checked", //节点复选框选中状态
  309. initchecked: "initchecked", //节点复选框初始状态
  310. basicData: "basicData", //用户自定义的记录节点数据
  311. recordData: "recordData", //当前data数据(排除basicData和children字段)
  312. },
  313. iframeRequest: {} //iframe的自定义参数
  314. };
  315. this.iframeFun = {
  316. iframeDone: function(iframeParam){ //iframe加载完毕后,用于用户自定义事件
  317. return ;
  318. }
  319. };
  320. this.style = { //树最终使用的样式集合
  321. item: "", //每一项div的样式
  322. itemThis: "", //选中div的样式
  323. dfont: "", //一级图标的样式
  324. icon: "", //二级图标的样式
  325. cbox: "", //复选框的样式
  326. chs: "" //复选框选中的样式
  327. };
  328. this.usefontStyle = { //树最终使用的图标集合
  329. fnode:{ //一级节点
  330. node:{ //非叶子节点
  331. open:"", //节点展开
  332. close:"" //节点关闭
  333. },
  334. leaf:"" //叶子节点
  335. },
  336. snode:{ //二级节点
  337. node:{ //非叶子节点
  338. open:"", //节点展开
  339. close:"" //节点关闭
  340. },
  341. leaf:"" //叶子节点
  342. },
  343. checkbox:{ //复选框
  344. on:"", //复选框选中
  345. out:"", //未选中
  346. noall:"" //半选
  347. },
  348. menubar:{ //菜单栏
  349. movedown:"", //全部展开
  350. moveup:"", //全部收缩
  351. refresh:"", //刷新
  352. checkAll:"", //全选
  353. unCheckAll:"", //全不选
  354. invertAll:"", //反选
  355. remove:"", //删除
  356. search:"" //搜索
  357. },
  358. menubarExt:"", //扩展菜单栏
  359. toolbar:{ //工具栏
  360. menubar:{ //依附在菜单栏的工具栏
  361. movedown:"", //全部展开
  362. moveup:"", //全部收缩
  363. refresh:"", //刷新
  364. checkAll:"", //全选
  365. unCheckAll:"", //全不选
  366. invertAll:"", //反选
  367. remove:"", //删除
  368. search:"" //搜索
  369. },
  370. menubarExt:"", //依附在菜单栏的扩展菜单栏
  371. pulldown:"", //展开
  372. pullup:"", //收缩
  373. add:"", //添加
  374. edit:"", //编辑
  375. del:"" //删除
  376. },
  377. toolbarExt:"" //扩展工具栏
  378. }
  379. /** 数据绑定**/
  380. this.node = { // 树节点选中时,包含当前节点的全部信息
  381. nodeId: "", //节点ID
  382. parentId: "", //父节点ID
  383. context: "", //节点内容
  384. leaf: "", //是否叶子节点
  385. level: "", //层级
  386. spread: "", //节点展开状态
  387. dataType: "", //节点标记
  388. checked: "", //节点复选框选中状态
  389. initchecked: "", //节点复选框初始状态
  390. basicData: "", //用户自定义的记录节点数据
  391. recordData: "", //当前data数据(排除basicData和children字段)
  392. };
  393. this.toolbarMenu = {}; // 工具栏右键菜单绑定的所有元素
  394. this.checkbarNode = []; // 复选框标记的全部节点数据
  395. this.errData = []; // 记录在渲染节点时有问题的数据
  396. this.checkArrLen = 0; //添加节点的时判断复选框个数
  397. this.temp = []; // 临时变量
  398. this.setting(options);
  399. };
  400. /******************** 初始参数加载 ********************/
  401. // 设置值
  402. DTree.prototype.setting = function(options) {
  403. this.options = options || {};
  404. /** 绑定元素参数(必填,2个参数项必填一个)**/
  405. this.elem = this.options.elem || ""; //树绑定的元素ID:#elem
  406. this.obj = this.options.obj || $(this.elem); //树绑定的jquery元素,用于当元素是延迟加载出来的话,可以用这个找到
  407. /** 基本参数**/
  408. this.accordion = (typeof (this.options.accordion) === "boolean") ? this.options.accordion : false; //开启手风琴加载
  409. if(this.accordion) {
  410. this.initLevel = 1; //默认展开节点 1节
  411. } else {
  412. this.initLevel = this.options.initLevel || 2; //默认展开节点 2节
  413. }
  414. this.type = this.options.type || "load"; // 树的加载方式 all,全量树, load,增量树,默认load
  415. this.cache = (typeof (this.options.cache) === "boolean") ? this.options.cache : true; //开启数据缓存
  416. this.record = (typeof (this.options.record) === "boolean") ? this.options.record : false; //开启数据记录模式
  417. this.load = (typeof (this.options.load) === "boolean") ? this.options.load : true; //开启加载动画
  418. this.none = this.options.none || "无数据"; // 初始加载无记录时显示文字
  419. if(this.options.height) { // 设置高度
  420. if(/^full-\d+$/.test(this.options.height)) {
  421. this.fullHeightGap = options.height.split('-')[1];
  422. this.height = $WIN.height() - this.fullHeightGap;
  423. console.log("$WIN.height(): " + $WIN.height());
  424. } else {
  425. this.fullHeightGap = this.options.height;
  426. this.height = this.options.height;
  427. }
  428. } else {
  429. this.fullHeightGap = "";
  430. this.height = "";
  431. }
  432. /** 样式相关参数**/
  433. this.iconfont = this.options.iconfont || DTREEFONT; // 默认图标字体 dtreefont
  434. this.iconfontStyle = this.options.iconfontStyle || {}; // 用于自定义树的每个关键部位使用的图标
  435. this.nodeIconArray = $.extend(nodeIconArray, this.options.nodeIconArray) || nodeIconArray; //用户自定义非叶子节点图标集合,node
  436. this.leafIconArray = $.extend(leafIconArray, this.options.leafIconArray) || leafIconArray; //用户自定义叶子节点图标集合,leaf
  437. this.skin = this.options.skin || "theme"; // 自定义样式
  438. if(this.skin == "layui"){ // layui主题
  439. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : true; // 开启树线,默认开启
  440. this.ficon = this.options.ficon || "7"; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'7'
  441. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "1") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示一级图标,默认'1'
  442. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  443. this.icon = this.options.icon || "-1"; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'-1'
  444. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'-1'
  445. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  446. } else if(this.skin == "laySimple"){ // laySimple主题
  447. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : false; // 开启树线,默认不开启
  448. this.ficon = this.options.ficon || ["2","-1"]; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'-1'
  449. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "2") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示一级图标,默认'2'
  450. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  451. this.icon = this.options.icon || "-1"; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'-1'
  452. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'-1'
  453. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  454. } else { // 默认主题 或者自定义主题
  455. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : false; // 开启树线,默认不开启
  456. this.ficon = this.options.ficon || "8"; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'8'
  457. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "1") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示一级图标,默认'1'
  458. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  459. this.icon = this.options.icon || "5"; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'5'
  460. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "0") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'0'
  461. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  462. }
  463. /** 数据加载参数**/
  464. this.url = this.options.url || ""; //请求地址
  465. this.async = (typeof (this.options.async) === "boolean") ? this.options.async : true; //异步同步加载,默认异步加载
  466. this.headers = this.options.headers || {}; // ajax header属性
  467. this.method = this.options.method || "post"; //请求类型
  468. this.dataType = this.options.dataType || "json"; //参数类型
  469. this.contentType = this.options.contentType || "application/x-www-form-urlencoded"; //发送信息至服务器时内容编码类型
  470. this.defaultRequest = $.extend(this.defaultRequest, this.options.defaultRequest) || this.defaultRequest; //默认请求参数
  471. this.filterRequest = this.options.filterRequest || []; //过滤请求参数
  472. this.request = this.options.request || {}; //用户自定义请求参数
  473. this.response = $.extend(this.response, this.options.response) || this.response; //返回json格式
  474. this.data = this.options.data || null; //初始化指定该参数,则不会访问异步接口
  475. this.dataFormat = this.options.dataFormat || "levelRelationship"; //用于用户配置的data数据格式,list:列表, levelRelationship:层级关系,默认
  476. this.dataStyle = this.options.dataStyle || "defaultStyle"; //用于用户配置layui通用的json数据风格,layuiStyle:layui风格,defaultStyle:默认风格
  477. this.errDataShow = (typeof (this.options.errDataShow) === "boolean") ? this.options.errDataShow : false; //是否在递归数据出现错误后,显示错误信息
  478. this.success = this.options.success || function(data, obj){}; //树加载完毕后执行解析树之前的回调(仅限异步加载)
  479. this.done = this.options.done || function(data, obj){}; //树加载完毕后的回调(仅限异步加载)
  480. this.formatter = $.extend(this.formatter, this.options.formatter)|| this.formatter ; // 数据过滤
  481. this.error = this.options.error || function(XMLHttpRequest, textStatus, errorThrown){layer.msg(textStatus, {icon:2});}; // 异步加载异常回调
  482. this.complete = this.options.complete || function(XMLHttpRequest, textStatus){}; // 异步加载完成回调
  483. /** 复选框参数**/
  484. this.checkbar = this.options.checkbar || false; //是否开启复选框模式
  485. this.checkbarLoad = this.options.checkbarLoad || "node"; // 复选框作用范围,node:所有节点, leaf:最后一级;默认所有节点
  486. this.checkbarType = this.options.checkbarType || "all" ; //复选框选中形式 all:子集选中父级也选中, no-all:子集选中父级半选中,子集全选父级选中,p-casc:父级选中子集全选,子集无法改变父级选中状态, self:没有任何级联关系,only:只能选中一个复选框。 默认all
  487. this.checkbarData = this.options.checkbarData || "choose" ; //复选框记录数据类型形式, change表示记录变更数据,choose表示记录选中数据,all记录全部数据,halfChoose记录选中和半选中的数据,默认choose
  488. this.checkbarFun = $.extend(this.checkbarFun, this.options.checkbarFun) || this.checkbarFun; // checkbar事件加载
  489. /** 菜单栏参数**/
  490. this.menubar = this.options.menubar || false; //是否打开菜单栏
  491. this.menubarTips = $.extend(this.menubarTips, this.options.menubarTips) || this.menubarTips; // 菜单栏吸附, toolbar:依附在工具栏,group:依附在按钮组,freedom,自由
  492. this.menubarFun = $.extend(this.menubarFun, this.options.menubarFun) || this.menubarFun; //menubar事件加载
  493. /** 工具栏参数**/
  494. this.toolbar = this.options.toolbar || false; //是否开启可编辑模式
  495. this.toolbarWay = this.options.toolbarWay || "contextmenu"; //工具栏显示方式,contextmenu:右键,follow:跟随节点,fixed:固定在节点右侧
  496. this.toolbarStyle = $.extend(this.toolbarStyle, this.options.toolbarStyle) || this.toolbarStyle; //toolbar的自定义风格,标题,弹框大小
  497. this.toolbarScroll = this.options.toolbarScroll || this.elem; //树的上级div容器,让树可以显示滚动条的div容器的ID
  498. this.toolbarLoad = this.options.toolbarLoad || "node"; //toolbar作用范围:node:所有节点,noleaf:非最后一级节点,leaf:最后一级
  499. this.toolbarShow = this.options.toolbarShow || ["add","edit","delete"]; // toolbar三个按钮自定义加载
  500. this.toolbarBtn = this.options.toolbarBtn || null; // toolbar增删改中内容的自定义加载
  501. this.toolbarExt = this.options.toolbarExt || []; // toolbar按钮扩展
  502. this.toolbarFun = $.extend(this.toolbarFun, this.options.toolbarFun) || this.toolbarFun; // toolbar事件加载
  503. /** iframe模式参数**/
  504. this.useIframe = this.options.useIframe || false; // 是否加载iframe 默认false,
  505. if(this.options.iframe) {
  506. this.iframe.iframeElem = this.options.iframe.iframeElem || this.iframe.iframeElem; //iframe配置
  507. this.iframe.iframeUrl = this.options.iframe.iframeUrl || this.iframe.iframeUrl; //iframe配置
  508. this.iframe.iframeLoad = this.options.iframe.iframeLoad || this.iframe.iframeLoad; //iframe配置
  509. this.iframe.iframeDefaultRequest = $.extend(this.iframe.iframeDefaultRequest, this.options.iframe.iframeDefaultRequest) || this.iframe.iframeDefaultRequest; //iframe配置
  510. this.iframe.iframeRequest = $.extend(this.iframe.iframeRequest, this.options.iframe.iframeRequest) || this.iframe.iframeRequest; //iframe配置
  511. }
  512. this.iframeFun = $.extend(this.iframeFun, this.options.iframeFun) || this.iframeFun; //iframe事件加载
  513. /** 拖拽模式参数*/
  514. this.drawable = (typeof (this.options.drawable) === "boolean") ? this.options.drawable : false; // 是否开启拖拽模式,默认false
  515. /** 调用确认最终主题方法*/
  516. this.ensureTheme();
  517. };
  518. // 设置值
  519. DTree.prototype.reloadSetting = function(options) {
  520. this.options = $.extend(this.options, options) || this.options;
  521. /** 绑定元素参数**/
  522. this.elem = this.options.elem || this.elem; //树绑定的元素ID:#elem
  523. if(typeof this.options.obj === 'undefined'){
  524. if(this.elem) {
  525. if($(this.elem).length > 0) {
  526. this.obj = $(this.elem);
  527. }
  528. }
  529. } else {
  530. this.obj = this.options.obj || this.obj; //树绑定的jquery元素,用于当元素是延迟加载出来的话,可以用这个找到
  531. }
  532. /** 基本参数**/
  533. this.accordion = (typeof (this.options.accordion) === "boolean") ? this.options.accordion : this.accordion; //开启手风琴加载
  534. if(this.accordion) {
  535. this.initLevel = 1; //默认展开节点 1节
  536. } else {
  537. this.initLevel = this.options.initLevel || this.initLevel; //默认展开节点 2节
  538. }
  539. this.type = this.options.type || this.type; // 树的加载方式 all,全量树, load,增量树,默认load
  540. this.cache = (typeof (this.options.cache) === "boolean") ? this.options.cache : this.cache; //开启数据缓存
  541. this.record = (typeof (this.options.record) === "boolean") ? this.options.record : this.record; //开启数据记录模式
  542. this.load = (typeof (this.options.load) === "boolean") ? this.options.load : this.load; //开启加载动画
  543. this.none = this.options.none || this.none; // 初始节点加载无数据时显示文字
  544. if(this.options.height) { // 设置高度
  545. if(/^full-\d+$/.test(this.options.height)) {
  546. this.fullHeightGap = options.height.split('-')[1];
  547. this.height = $WIN.height() - this.fullHeightGap;
  548. } else {
  549. this.fullHeightGap = this.options.height;
  550. this.height = this.options.height;
  551. }
  552. }
  553. /** 样式相关参数**/
  554. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : this.line; // 开启树线,默认不开启
  555. this.iconfont = this.options.iconfont || this.iconfont; // 默认图标字体 dtreefont
  556. this.iconfontStyle = this.options.iconfontStyle || this.iconfontStyle; // 用于自定义树的每个关键部位使用的图标
  557. this.nodeIconArray = $.extend(nodeIconArray, this.options.nodeIconArray) || this.nodeIconArray; //用户自定义非叶子节点图标集合,node
  558. this.leafIconArray = $.extend(leafIconArray, this.options.leafIconArray) || this.leafIconArray; //用户自定义叶子节点图标集合,leaf
  559. this.skin = this.options.skin || this.skin; // 自定义样式
  560. if(this.skin == "layui"){ // layui主题
  561. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : true; // 开启树线,默认开启
  562. this.ficon = this.options.ficon || this.ficon; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'7'
  563. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "1") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'1'
  564. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  565. this.icon = this.options.icon || this.icon; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'-1'
  566. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'-1'
  567. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  568. } else if(this.skin == "laySimple"){ // laySimple主题
  569. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : false; // 开启树线,默认不开启
  570. this.ficon = this.options.ficon || this.ficon; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'-1'
  571. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "2") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'2'
  572. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  573. this.icon = this.options.icon || this.icon; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'-1'
  574. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "-1") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'-1'
  575. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  576. } else { // 默认主题 或者自定义主题
  577. this.line = (typeof (this.options.line) === "boolean") ? this.options.line : false; // 开启树线,默认不开启
  578. this.ficon = this.options.ficon || this.ficon; //一级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示一级图标。默认'8'
  579. this.fnodeIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? (this.ficon == "-1" ? "-1" : "1") : this.ficon[0]; // 一级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'1'
  580. this.fleafIcon = (typeof this.ficon === 'string' || typeof this.ficon === 'number') ? this.ficon : this.ficon[1]; // 一级图标中的leaf节点图标
  581. this.icon = this.options.icon || this.icon; //二级图标样式,0:文件夹,1:人员,2:机构,3:报表,4:信息,5:叶子,6:勋章,7:文件,8:小圆点,9:齿轮,10:星星, -1:不显示二级图标。默认'5'
  582. this.nodeIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? (this.icon == "-1" ? "-1" : "0") : this.icon[0]; // 二级图标中的node节点图标。0:文件夹,1:+、-,2:三角形, -1:不显示二级图标,默认'0'
  583. this.leafIcon = (typeof this.icon === 'string' || typeof this.icon === 'number') ? this.icon : this.icon[1]; // 二级图标中的leaf节点图标
  584. }
  585. /** 数据加载参数**/
  586. this.url = this.options.url || this.url; //请求地址
  587. this.async = (typeof (this.options.async) === "boolean") ? this.options.async : this.async; //异步同步加载,默认异步加载
  588. this.headers = this.options.headers || this.headers; // ajax header属性
  589. this.method = this.options.method || this.method; //请求类型
  590. this.dataType = this.options.dataType || this.dataType; //参数类型
  591. this.contentType = this.options.contentType || this.contentType; //发送信息至服务器时内容编码类型
  592. this.defaultRequest = $.extend(this.defaultRequest, this.options.defaultRequest) || this.defaultRequest; //默认请求参数
  593. this.filterRequest = this.options.filterRequest || this.filterRequest; //过滤请求参数
  594. this.request = this.options.request || this.request; //用户自定义请求参数
  595. this.response = $.extend(this.response, this.options.response) || this.response; //返回json格式
  596. this.data = this.options.data || this.data; //初始化指定该参数,则不会访问异步接口
  597. this.dataFormat = this.options.dataFormat || this.dataFormat; //用于用户配置的data数据格式,list:列表, levelRelationship:层级关系,默认
  598. this.dataStyle = this.options.dataStyle || this.dataStyle; //用于用户配置layui通用的json数据风格,layuiStyle:layui风格,defaultStyle:默认风格
  599. this.errDataShow = (typeof (this.options.errDataShow) === "boolean") ? this.options.errDataShow : this.errDataShow; //是否在使用list模式递归数据出现错误时,显示错误信息
  600. this.success = this.options.success || this.success; //树加载完毕后执行解析树之前的回调(仅限异步加载)
  601. this.done = this.options.done || this.done; //树加载完毕后的回调(仅限异步加载)
  602. this.formatter = $.extend(this.formatter, this.options.formatter)|| this.formatter ; // 数据过滤
  603. this.error = this.options.error || this.error; // 异步加载异常回调
  604. this.complete = this.options.complete || this.complete; // 异步加载完成回调
  605. /** 复选框参数**/
  606. this.checkbar = this.options.checkbar || this.checkbar; //是否开启复选框模式
  607. this.checkbarLoad = this.options.checkbarLoad || this.checkbarLoad; // 复选框作用范围,node:所有节点, leaf:最后一级;默认所有节点
  608. this.checkbarType = this.options.checkbarType || this.checkbarType ; //复选框选中形式 all:子集选中父级也选中, no-all:子集选中父级半选中,子集全选父级选中,p-casc:父级选中子集全选,子集无法改变父级选中状态, self:没有任何级联关系,only:只能选中一个复选框。 默认all
  609. this.checkbarData = this.options.checkbarData || this.checkbarData ; //复选框记录数据类型形式, change表示记录变更数据,choose表示记录选中数据,all记录全部数据,halfChoose记录选中和半选中的数据,默认choose
  610. this.checkbarFun = $.extend(this.checkbarFun, this.options.checkbarFun)|| this.checkbarFun ; // checkbar事件加载
  611. /** 菜单栏参数**/
  612. this.menubar = this.options.menubar || this.menubar; //是否打开菜单栏
  613. this.menubarTips = $.extend(this.menubarTips, this.options.menubarTips) || this.menubarTips; // 菜单栏吸附, toolbar:依附在工具栏,group:依附在按钮组,freedom,自由
  614. this.menubarFun = $.extend(this.menubarFun, this.options.menubarFun) || this.menubarFun; //menubar事件加载
  615. /** 工具栏参数**/
  616. this.toolbar = this.options.toolbar || this.toolbar; //是否开启工具栏
  617. this.toolbarWay = this.options.toolbarWay || this.toolbarWay; //工具栏显示方式,contextmenu:右键,follow:跟随节点,fixed:固定在节点右侧
  618. this.toolbarStyle = $.extend(this.toolbarStyle, this.options.toolbarStyle) || this.toolbarStyle; //toolbar的自定义风格,标题,弹框大小
  619. this.toolbarScroll = this.options.toolbarScroll || this.toolbarScroll; //树的上级div容器,让树可以显示滚动条的div容器
  620. this.toolbarLoad = this.options.toolbarLoad || this.toolbarLoad; //toolbar作用范围:node:所有节点,noleaf:非最后一级节点,leaf:最后一级
  621. this.toolbarShow = this.options.toolbarShow || this.toolbarShow; // toolbar三个按钮
  622. this.toolbarBtn = this.options.toolbarBtn || this.toolbarBtn; // toolbar增删改中内容的自定义加载
  623. this.toolbarExt = this.options.toolbarExt || this.toolbarExt; // toolbar按钮扩展
  624. this.toolbarFun = $.extend(this.toolbarFun, this.options.toolbarFun) || this.toolbarFun; // toolbar事件加载
  625. /** iframe模式参数**/
  626. this.useIframe = this.options.useIframe || this.useIframe; // 是否加载iframe 默认false,
  627. if(this.options.iframe) {
  628. this.iframe.iframeElem = this.options.iframe.iframeElem || this.iframe.iframeElem; //iframe配置
  629. this.iframe.iframeUrl = this.options.iframe.iframeUrl || this.iframe.iframeUrl; //iframe配置
  630. this.iframe.iframeLoad = this.options.iframe.iframeLoad || this.iframe.iframeLoad; //iframe配置
  631. this.iframe.iframeDefaultRequest = $.extend(this.iframe.iframeDefaultRequest, this.options.iframe.iframeDefaultRequest) || this.iframe.iframeDefaultRequest; //iframe配置
  632. this.iframe.iframeRequest = $.extend(this.iframe.iframeRequest, this.options.iframe.iframeRequest) || this.iframe.iframeRequest; //iframe配置
  633. }
  634. this.iframeFun = $.extend(this.iframeFun, this.options.iframeFun) || this.iframeFun; //iframe事件加载
  635. /** 拖拽模式参数*/
  636. this.drawable = (typeof (this.options.drawable) === "boolean") ? this.options.drawable : this.drawable; // 是否开启拖拽模式,默认false
  637. /** 调用确认最终主题方法*/
  638. this.ensureTheme();
  639. };
  640. /******************** 字体及图标区域 ********************/
  641. // 确认最终主题
  642. DTree.prototype.ensureTheme = function(){
  643. var _this = this;
  644. // 确认style
  645. this.style.item = DTREE + this.skin + ITEM;
  646. this.style.itemThis = DTREE + this.skin + ITEMTHIS;
  647. this.style.dfont = DTREE + this.skin + DFONT;
  648. this.style.ficon = DTREE + this.skin + FICON;
  649. this.style.icon = DTREE + this.skin + ICON;
  650. this.style.cbox = DTREE + this.skin + CBOX;
  651. this.style.chs = DTREE + this.skin + CHS;
  652. // 确认usefontStyle
  653. var iconfont = this.iconfont;
  654. var iconfonts = [];
  655. if(typeof iconfont === 'string') {
  656. iconfonts.push(iconfont);
  657. } else {
  658. iconfonts = iconfont;
  659. }
  660. var iconfontStyle = this.iconfontStyle;
  661. var iconfontStyles = [];
  662. if(iconfontStyle.length == undefined) {
  663. iconfontStyles.push(iconfontStyle);
  664. } else {
  665. iconfontStyles = iconfontStyle;
  666. }
  667. for(var i=0; i<iconfonts.length; i++){
  668. var ifont = iconfonts[i];
  669. var ifontStyle = iconfontStyles[i];
  670. if(typeof ifontStyle !== 'undefined') {
  671. // 判断,赋值
  672. this.useDefaultOrUserDefineFnodeStyle(ifont, ifontStyle.fnode);
  673. this.useDefaultOrUserDefineSnodeStyle(ifont, ifontStyle.snode);
  674. this.useDefaultOrUserDefineCheckboxStyle(ifont, ifontStyle.checkbox);
  675. this.useDefaultOrUserDefineMenubarStyle(ifont, ifontStyle.menubar);
  676. this.useDefaultOrUserDefineMenubarExtStyle(ifont, ifontStyle.menubarExt);
  677. this.useDefaultOrUserDefineToolbarStyle(ifont, ifontStyle.toolbar);
  678. this.useDefaultOrUserDefineToolbarExtStyle(ifont, ifontStyle.toolbarExt);
  679. }
  680. }
  681. };
  682. // 赋值一级图标
  683. DTree.prototype.useDefaultOrUserDefineFnodeStyle = function(ifont, fnode){
  684. var _this = this;
  685. var tempOpen = this.usefontStyle.fnode.node.open;
  686. var tempClose = this.usefontStyle.fnode.node.close;
  687. var tempLeaf = this.usefontStyle.fnode.leaf;
  688. if(typeof fnode === 'undefined'){
  689. this.usefontStyle.fnode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["open"]) : tempOpen; // 一级图标中的node节点open图标
  690. this.usefontStyle.fnode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["close"]) : tempClose; // 一级图标中的node节点close图标
  691. this.usefontStyle.fnode.leaf = (tempLeaf == "") ? (ifont + " " + this.leafIconArray[this.fleafIcon]) : tempLeaf; // 一级图标中的node节点的leaf图标
  692. } else {
  693. var node = fnode.node;
  694. var leaf = fnode.leaf;
  695. if(typeof node === 'undefined'){
  696. this.usefontStyle.fnode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["open"]) : tempOpen; // 一级图标中的node节点open图标
  697. this.usefontStyle.fnode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["close"]) : tempClose; // 一级图标中的node节点close图标
  698. } else {
  699. var open = node.open;
  700. var close = node.close;
  701. if(typeof open === 'undefined'){
  702. this.usefontStyle.fnode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["open"]) : tempOpen; // 一级图标中的node节点open图标
  703. } else {
  704. this.usefontStyle.fnode.node.open = ifont + " " + open;
  705. }
  706. if(typeof close === 'undefined') {
  707. this.usefontStyle.fnode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.fnodeIcon]["close"]) : tempClose; // 一级图标中的node节点close图标
  708. } else {
  709. this.usefontStyle.fnode.node.close = ifont + " " + close;
  710. }
  711. }
  712. if(typeof leaf === 'undefined'){
  713. this.usefontStyle.fnode.leaf = (tempLeaf == "") ? (ifont + " " + this.leafIconArray[this.fleafIcon]) : tempLeaf; // 一级图标中的node节点的leaf图标
  714. } else {
  715. this.usefontStyle.fnode.leaf = ifont + " " + leaf;
  716. }
  717. }
  718. };
  719. // 赋值二级图标
  720. DTree.prototype.useDefaultOrUserDefineSnodeStyle = function(ifont, snode){
  721. var _this = this;
  722. var tempOpen = this.usefontStyle.snode.node.open;
  723. var tempClose = this.usefontStyle.snode.node.close;
  724. var tempLeaf = this.usefontStyle.snode.leaf;
  725. if(typeof snode === 'undefined'){
  726. this.usefontStyle.snode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["open"]) : tempOpen; // 二级图标中的node节点open图标
  727. this.usefontStyle.snode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["close"]) : tempClose; // 二级图标中的node节点close图标
  728. this.usefontStyle.snode.leaf = (tempLeaf == "") ? (ifont + " " + this.leafIconArray[this.leafIcon]) : tempLeaf; // 二级图标中的leaf节点图标
  729. } else {
  730. var node = snode.node;
  731. var leaf = snode.leaf;
  732. if(typeof node === 'undefined') {
  733. this.usefontStyle.snode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["open"]) : tempOpen; // 二级图标中的node节点open图标
  734. this.usefontStyle.snode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["close"]) : tempClose; // 二级图标中的node节点close图标
  735. } else {
  736. var open = node.open;
  737. var close = node.close;
  738. if(typeof open === 'undefined'){
  739. this.usefontStyle.snode.node.open = (tempOpen == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["open"]) : tempOpen; // 二级图标中的node节点open图标
  740. } else {
  741. this.usefontStyle.snode.node.open = ifont + " " + open;
  742. }
  743. if(typeof close === 'undefined') {
  744. this.usefontStyle.snode.node.close = (tempClose == "") ? (ifont + " " + this.nodeIconArray[this.nodeIcon]["close"]) : tempClose; // 二级图标中的node节点close图标
  745. } else {
  746. this.usefontStyle.snode.node.close = ifont + " " + close;
  747. }
  748. }
  749. if(typeof leaf === 'undefined') {
  750. this.usefontStyle.snode.leaf = (tempLeaf == "") ? (ifont + " " + this.leafIconArray[this.leafIcon]) : tempLeaf; // 二级图标中的leaf节点图标
  751. } else {
  752. this.usefontStyle.snode.leaf = ifont + " " + leaf;
  753. }
  754. }
  755. };
  756. // 赋值复选框图标
  757. DTree.prototype.useDefaultOrUserDefineCheckboxStyle = function(ifont, checkbox){
  758. var _this = this;
  759. var tempOn = this.usefontStyle.checkbox.on;
  760. var tempOut = this.usefontStyle.checkbox.out;
  761. var tempNoall = this.usefontStyle.checkbox.noall;
  762. if(typeof checkbox === 'undefined'){
  763. this.usefontStyle.checkbox.on = (tempOn == "") ? (ifont + " " + LI_DIV_CHECKBAR_ON) : tempOn;
  764. this.usefontStyle.checkbox.out = (tempOut == "") ? (ifont + " " + LI_DIV_CHECKBAR_OUT) : tempOut;
  765. this.usefontStyle.checkbox.noall = (tempNoall == "") ? (ifont + " " + LI_DIV_CHECKBAR_NOALL) : tempNoall;
  766. } else {
  767. var on = checkbox.on;
  768. var out = checkbox.out;
  769. var noall = checkbox.noall;
  770. if(typeof on === 'undefined') {
  771. this.usefontStyle.checkbox.on = (tempOn == "") ? (ifont + " " + LI_DIV_CHECKBAR_ON) : tempOn;
  772. } else {
  773. this.usefontStyle.checkbox.on = ifont + " " + on;
  774. }
  775. if(typeof out === 'undefined') {
  776. this.usefontStyle.checkbox.out = (tempOut == "") ? (ifont + " " + LI_DIV_CHECKBAR_OUT) : tempOut;
  777. } else {
  778. this.usefontStyle.checkbox.out = ifont + " " + out;
  779. }
  780. if(typeof noall === 'undefined') {
  781. this.usefontStyle.checkbox.noall = (tempNoall == "") ? (ifont + " " + LI_DIV_CHECKBAR_NOALL) : tempNoall;
  782. } else {
  783. this.usefontStyle.checkbox.noall = ifont + " " + noall;
  784. }
  785. }
  786. };
  787. // 赋值菜单栏图标
  788. DTree.prototype.useDefaultOrUserDefineMenubarStyle = function(ifont, menubar){
  789. var _this = this;
  790. var tempMovedown = this.usefontStyle.menubar.movedown;
  791. var tempMoveup = this.usefontStyle.menubar.moveup;
  792. var tempRefresh = this.usefontStyle.menubar.refresh;
  793. var tempCheckAll = this.usefontStyle.menubar.checkAll;
  794. var tempUncheckAll = this.usefontStyle.menubar.unCheckAll;
  795. var tempInvertAll = this.usefontStyle.menubar.invertAll;
  796. var tempRemove = this.usefontStyle.menubar.remove;
  797. var tempSearch = this.usefontStyle.menubar.search;
  798. if(typeof menubar === 'undefined'){
  799. this.usefontStyle.menubar.movedown = (tempMovedown == "") ? (ifont + " " + LI_DIV_MENUBAR_DOWN) : tempMovedown;
  800. this.usefontStyle.menubar.moveup = (tempMoveup == "") ? (ifont + " " + LI_DIV_MENUBAR_UP) : tempMoveup;
  801. this.usefontStyle.menubar.refresh = (tempRefresh == "") ? (ifont + " " + LI_DIV_MENUBAR_REFRESH) : tempRefresh;
  802. this.usefontStyle.menubar.checkAll = (tempCheckAll == "") ? (ifont + " " + LI_DIV_MENUBAR_CHECKALL) : tempCheckAll;
  803. this.usefontStyle.menubar.unCheckAll = (tempUncheckAll == "") ? (ifont + " " + LI_DIV_MENUBAR_UNCHECKALL) : tempUncheckAll;
  804. this.usefontStyle.menubar.invertAll = (tempInvertAll == "") ? (ifont + " " + LI_DIV_MENUBAR_INVERTALL) : tempInvertAll;
  805. this.usefontStyle.menubar.remove = (tempRemove == "") ? (ifont + " " + LI_DIV_MENUBAR_DELETE) : tempRemove;
  806. this.usefontStyle.menubar.search = (tempSearch == "") ? (ifont + " " + LI_DIV_MENUBAR_SEARCH) : tempSearch;
  807. } else {
  808. var movedown = menubar.movedown;
  809. var moveup = menubar.moveup;
  810. var refresh = menubar.refresh;
  811. var checkAll = menubar.checkAll;
  812. var unCheckAll = menubar.unCheckAll;
  813. var invertAll = menubar.invertAll;
  814. var remove = menubar.remove;
  815. var search = menubar.search;
  816. if(typeof movedown === 'undefined') {
  817. this.usefontStyle.menubar.movedown = (tempMovedown == "") ? (ifont + " " + LI_DIV_MENUBAR_DOWN) : tempMovedown;
  818. } else {
  819. this.usefontStyle.menubar.movedown = ifont + " " + movedown;
  820. }
  821. if(typeof moveup === 'undefined') {
  822. this.usefontStyle.menubar.moveup = (tempMoveup == "") ? (ifont + " " + LI_DIV_MENUBAR_UP) : tempMoveup;
  823. } else {
  824. this.usefontStyle.menubar.moveup = ifont + " " + moveup;
  825. }
  826. if(typeof refresh === 'undefined') {
  827. this.usefontStyle.menubar.refresh = (tempRefresh == "") ? (ifont + " " + LI_DIV_MENUBAR_REFRESH) : tempRefresh;
  828. } else {
  829. this.usefontStyle.menubar.refresh = ifont + " " + refresh;
  830. }
  831. if(typeof checkAll === 'undefined') {
  832. this.usefontStyle.menubar.checkAll = (tempCheckAll == "") ? (ifont + " " + LI_DIV_MENUBAR_CHECKALL) : tempCheckAll;
  833. } else {
  834. this.usefontStyle.menubar.checkAll = ifont + " " + checkAll;
  835. }
  836. if(typeof unCheckAll === 'undefined') {
  837. this.usefontStyle.menubar.unCheckAll = (tempUncheckAll == "") ? (ifont + " " + LI_DIV_MENUBAR_UNCHECKALL) : tempUncheckAll;
  838. } else {
  839. this.usefontStyle.menubar.unCheckAll = ifont + " " + unCheckAll;
  840. }
  841. if(typeof invertAll === 'undefined') {
  842. this.usefontStyle.menubar.invertAll = (tempInvertAll == "") ? (ifont + " " + LI_DIV_MENUBAR_INVERTALL) : tempInvertAll;
  843. } else {
  844. this.usefontStyle.menubar.invertAll = ifont + " " + invertAll;
  845. }
  846. if(typeof remove === 'undefined') {
  847. this.usefontStyle.menubar.remove = (tempRemove == "") ? (ifont + " " + LI_DIV_MENUBAR_DELETE) : tempRemove;
  848. } else {
  849. this.usefontStyle.menubar.remove = ifont + " " + remove;
  850. }
  851. if(typeof search === 'undefined') {
  852. this.usefontStyle.menubar.search = (tempSearch == "") ? (ifont + " " + LI_DIV_MENUBAR_SEARCH) : tempSearch;
  853. } else {
  854. this.usefontStyle.menubar.search = ifont + " " + search;
  855. }
  856. }
  857. };
  858. // 赋值扩展菜单栏图标
  859. DTree.prototype.useDefaultOrUserDefineMenubarExtStyle = function(ifont, menubarExt){
  860. var _this = this;
  861. var tempExt = this.usefontStyle.menubarExt;
  862. if(typeof menubarExt === 'undefined'){
  863. this.usefontStyle.menubarExt = (tempExt == "") ? ifont : tempExt;
  864. } else {
  865. this.usefontStyle.menubarExt = menubarExt;
  866. }
  867. };
  868. // 赋值工具栏图标
  869. DTree.prototype.useDefaultOrUserDefineToolbarStyle = function(ifont, toolbar){
  870. var _this = this;
  871. var tempMovedown = this.usefontStyle.toolbar.menubar.movedown;
  872. var tempMoveup = this.usefontStyle.toolbar.menubar.moveup;
  873. var tempRefresh = this.usefontStyle.toolbar.menubar.refresh;
  874. var tempCheckAll = this.usefontStyle.toolbar.menubar.checkAll;
  875. var tempUnCheckAll = this.usefontStyle.toolbar.menubar.unCheckAll;
  876. var tempInvertAll = this.usefontStyle.toolbar.menubar.invertAll;
  877. var tempRemove = this.usefontStyle.toolbar.menubar.remove;
  878. var tempSearch = this.usefontStyle.toolbar.menubar.search;
  879. var tempExt = this.usefontStyle.toolbar.menubarExt;
  880. var tempPulldown = this.usefontStyle.toolbar.pulldown;
  881. var tempPullup = this.usefontStyle.toolbar.pullup;
  882. var tempAdd = this.usefontStyle.toolbar.add;
  883. var tempEdit = this.usefontStyle.toolbar.edit;
  884. var tempDel = this.usefontStyle.toolbar.del;
  885. if(typeof toolbar === 'undefined'){
  886. this.usefontStyle.toolbar.menubar.movedown = (tempMovedown == "") ? (ifont + " " + this.usefontStyle.menubar.movedown) : tempMovedown;
  887. this.usefontStyle.toolbar.menubar.moveup = (tempMoveup == "") ? (ifont + " " + this.usefontStyle.menubar.moveup) : tempMoveup;
  888. this.usefontStyle.toolbar.menubar.refresh = (tempRefresh == "") ? (ifont + " " + this.usefontStyle.menubar.refresh) : tempRefresh;
  889. this.usefontStyle.toolbar.menubar.checkAll = (tempCheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.checkAll) : tempCheckAll;
  890. this.usefontStyle.toolbar.menubar.unCheckAll = (tempUnCheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.unCheckAll) : tempUnCheckAll;
  891. this.usefontStyle.toolbar.menubar.invertAll = (tempInvertAll == "") ? (ifont + " " + this.usefontStyle.menubar.invertAll) : tempInvertAll;
  892. this.usefontStyle.toolbar.menubar.remove = (tempRemove == "") ? (ifont + " " + this.usefontStyle.menubar.remove) : tempRemove;
  893. this.usefontStyle.toolbar.menubar.search = (tempSearch == "") ? (ifont + " " + this.usefontStyle.menubar.search) : tempSearch;
  894. this.usefontStyle.toolbar.menubarExt = (tempExt == "") ? this.usefontStyle.menubarExt : tempExt;
  895. this.usefontStyle.toolbar.pulldown = (tempPulldown == "") ? (ifont + " " + LI_DIV_TOOLBAR_PULLDOWN) : tempPulldown;
  896. this.usefontStyle.toolbar.pullup = (tempPullup == "") ? (ifont + " " + LI_DIV_TOOLBAR_PULLUP) : tempPullup;
  897. this.usefontStyle.toolbar.add = (tempAdd == "") ? (ifont + " " + LI_DIV_TOOLBAR_ADD) : tempAdd;
  898. this.usefontStyle.toolbar.edit = (tempEdit == "") ? (ifont + " " + LI_DIV_TOOLBAR_EDIT) : tempEdit;
  899. this.usefontStyle.toolbar.del = (tempDel == "") ? (ifont + " " + LI_DIV_TOOLBAR_DEL) : tempDel;
  900. } else {
  901. var menubar = toolbar.menubar;
  902. var menubarExt = toolbar.menubarExt;
  903. var pulldown = toolbar.pulldown;
  904. var pullup = toolbar.pullup;
  905. var add = toolbar.add;
  906. var edit = toolbar.edit;
  907. var del = toolbar.del;
  908. if(typeof menubar === 'undefined'){
  909. this.usefontStyle.toolbar.menubar.movedown = (tempMovedown == "") ? (ifont + " " + this.usefontStyle.menubar.movedown) : tempMovedown;
  910. this.usefontStyle.toolbar.menubar.moveup = (tempMoveup == "") ? (ifont + " " + this.usefontStyle.menubar.moveup) : tempMoveup;
  911. this.usefontStyle.toolbar.menubar.refresh = (tempRefresh == "") ? (ifont + " " + this.usefontStyle.menubar.refresh) : tempRefresh;
  912. this.usefontStyle.toolbar.menubar.checkAll = (tempCheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.checkAll) : tempCheckAll;
  913. this.usefontStyle.toolbar.menubar.unCheckAll = (tempUncheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.unCheckAll) : tempUncheckAll;
  914. this.usefontStyle.toolbar.menubar.invertAll = (tempInvertAll == "") ? (ifont + " " + this.usefontStyle.menubar.invertAll) : tempInvertAll;
  915. this.usefontStyle.toolbar.menubar.remove = (tempRemove == "") ? (ifont + " " + this.usefontStyle.menubar.remove) : tempRemove;
  916. this.usefontStyle.toolbar.menubar.search = (tempSearch == "") ? (ifont + " " + this.usefontStyle.menubar.search) : tempSearch;
  917. } else {
  918. var movedown = menubar.movedown;
  919. var moveup = menubar.moveup;
  920. var refresh = menubar.refresh;
  921. var checkAll = menubar.checkAll;
  922. var unCheckAll = menubar.unCheckAll;
  923. var invertAll = menubar.invertAll;
  924. var remove = menubar.remove;
  925. var search = menubar.search;
  926. if(typeof movedown === 'undefined') {
  927. this.usefontStyle.toolbar.menubar.movedown = (tempMovedown == "") ? (ifont + " " + this.usefontStyle.menubar.movedown) : tempMovedown;
  928. } else {
  929. this.usefontStyle.toolbar.menubar.movedown = ifont + " " + movedown;
  930. }
  931. if(typeof moveup === 'undefined') {
  932. this.usefontStyle.toolbar.menubar.moveup = (tempMoveup == "") ? (ifont + " " + this.usefontStyle.menubar.moveup) : tempMoveup;
  933. } else {
  934. this.usefontStyle.toolbar.menubar.moveup = ifont + " " + moveup;
  935. }
  936. if(typeof refresh === 'undefined') {
  937. this.usefontStyle.toolbar.menubar.refresh = (tempRefresh == "") ? (ifont + " " + this.usefontStyle.menubar.refresh) : tempRefresh;
  938. } else {
  939. this.usefontStyle.toolbar.menubar.refresh = ifont + " " + refresh;
  940. }
  941. if(typeof checkAll === 'undefined') {
  942. this.usefontStyle.toolbar.menubar.checkAll = (tempCheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.checkAll) : tempCheckAll;
  943. } else {
  944. this.usefontStyle.toolbar.menubar.checkAll = ifont + " " + checkAll;
  945. }
  946. if(typeof unCheckAll === 'undefined') {
  947. this.usefontStyle.toolbar.menubar.unCheckAll = (tempUncheckAll == "") ? (ifont + " " + this.usefontStyle.menubar.unCheckAll) : tempUncheckAll;
  948. } else {
  949. this.usefontStyle.toolbar.menubar.unCheckAll = ifont + " " + unCheckAll;
  950. }
  951. if(typeof invertAll === 'undefined') {
  952. this.usefontStyle.toolbar.menubar.invertAll = (tempInvertAll == "") ? (ifont + " " + this.usefontStyle.menubar.invertAll) : tempInvertAll;
  953. } else {
  954. this.usefontStyle.toolbar.menubar.invertAll = ifont + " " + invertAll;
  955. }
  956. if(typeof remove === 'undefined') {
  957. this.usefontStyle.toolbar.menubar.remove = (tempRemove == "") ? (ifont + " " + this.usefontStyle.menubar.remove) : tempRemove;
  958. } else {
  959. this.usefontStyle.toolbar.menubar.remove = ifont + " " + remove;
  960. }
  961. if(typeof search === 'undefined') {
  962. this.usefontStyle.toolbar.menubar.search = (tempSearch == "") ? (ifont + " " + this.usefontStyle.menubar.search) : tempSearch;
  963. } else {
  964. this.usefontStyle.toolbar.menubar.search = ifont + " " + search;
  965. }
  966. }
  967. if(typeof menubarExt === 'undefined'){
  968. this.usefontStyle.toolbar.menubarExt = (tempExt == "") ? this.usefontStyle.menubarExt : tempExt;
  969. } else {
  970. this.usefontStyle.toolbar.menubarExt = menubarExt;
  971. }
  972. if(typeof pulldown === 'undefined'){
  973. this.usefontStyle.toolbar.pulldown = (tempPulldown == "") ? (ifont + " " + LI_DIV_TOOLBAR_PULLDOWN) : tempPulldown;
  974. } else {
  975. this.usefontStyle.toolbar.pulldown = ifont + " " + pulldown;
  976. }
  977. if(typeof pullup === 'undefined'){
  978. this.usefontStyle.toolbar.pullup = (tempPullup == "") ? (ifont + " " + LI_DIV_TOOLBAR_PULLUP) : tempPullup;
  979. } else {
  980. this.usefontStyle.toolbar.pullup = ifont + " " + pullup;
  981. }
  982. if(typeof add === 'undefined'){
  983. this.usefontStyle.toolbar.add = (tempAdd == "") ? (ifont + " " + LI_DIV_TOOLBAR_ADD) : tempAdd;
  984. } else {
  985. this.usefontStyle.toolbar.add = ifont + " " + add;
  986. }
  987. if(typeof edit === 'undefined'){
  988. this.usefontStyle.toolbar.edit = (tempEdit == "") ? (ifont + " " + LI_DIV_TOOLBAR_EDIT) : tempEdit;
  989. } else {
  990. this.usefontStyle.toolbar.edit = ifont + " " + edit;
  991. }
  992. if(typeof del === 'undefined'){
  993. this.usefontStyle.toolbar.del = (tempDel == "") ? (ifont + " " + LI_DIV_TOOLBAR_DEL) : tempDel;
  994. } else {
  995. this.usefontStyle.toolbar.del = ifont + " " + del;
  996. }
  997. }
  998. };
  999. // 赋值扩展工具栏图标
  1000. DTree.prototype.useDefaultOrUserDefineToolbarExtStyle = function(ifont, toolbarExt){
  1001. var _this = this;
  1002. var tempExt = this.usefontStyle.toolbarExt;
  1003. if(typeof toolbarExt === 'undefined'){
  1004. this.usefontStyle.toolbarExt = (tempExt == "") ? ifont : tempExt;
  1005. } else {
  1006. this.usefontStyle.toolbarExt = toolbarExt;
  1007. }
  1008. };
  1009. // 设置图标的展开关闭,以及展开时/关闭时是最后一级图标的处理
  1010. DTree.prototype.operateIcon = function($i_spread, $i_node){
  1011. var _this = this;
  1012. var ficonClass = $i_spread.attr("data-iconClass");
  1013. var iconClass = $i_node.attr("data-iconClass");
  1014. return{
  1015. open: function(){
  1016. $i_spread.attr("data-spread","open");
  1017. $i_node.attr("data-spread","open");
  1018. if(!ficonClass) {
  1019. $i_spread.removeClass(_this.usefontStyle.fnode.node.close);
  1020. $i_spread.addClass(_this.usefontStyle.fnode.node.open);
  1021. }
  1022. if(!iconClass) {
  1023. $i_node.removeClass(_this.usefontStyle.snode.node.close);
  1024. $i_node.addClass(_this.usefontStyle.snode.node.open);
  1025. }
  1026. },
  1027. close: function(){
  1028. $i_spread.attr("data-spread","close");
  1029. $i_node.attr("data-spread","close");
  1030. if(!ficonClass) {
  1031. $i_spread.removeClass(_this.usefontStyle.fnode.node.open);
  1032. $i_spread.addClass(_this.usefontStyle.fnode.node.close);
  1033. }
  1034. if(!iconClass) {
  1035. $i_node.removeClass(_this.usefontStyle.snode.node.open);
  1036. $i_node.addClass(_this.usefontStyle.snode.node.close);
  1037. }
  1038. },
  1039. openWithLeaf: function(){
  1040. $i_spread.attr("data-spread","open");
  1041. $i_node.attr("data-spread","open");
  1042. if(!ficonClass) {
  1043. $i_spread.removeClass(_this.usefontStyle.fnode.leaf);
  1044. $i_spread.addClass(_this.usefontStyle.fnode.node.open);
  1045. }
  1046. if(!iconClass) {
  1047. $i_node.removeClass(_this.usefontStyle.snode.leaf);
  1048. $i_node.addClass(_this.usefontStyle.snode.node.open);
  1049. }
  1050. },
  1051. closeWithLeaf: function(){
  1052. $i_spread.attr("data-spread","last");
  1053. $i_node.attr("data-spread","last");
  1054. if(!ficonClass) {
  1055. $i_spread.removeClass(_this.usefontStyle.fnode.node.open);
  1056. $i_spread.removeClass(_this.usefontStyle.fnode.node.close);
  1057. $i_spread.addClass(_this.usefontStyle.fnode.leaf);
  1058. }
  1059. if(!iconClass) {
  1060. $i_node.removeClass(_this.usefontStyle.snode.node.open);
  1061. $i_node.removeClass(_this.usefontStyle.snode.node.close);
  1062. $i_node.addClass(_this.usefontStyle.snode.leaf);
  1063. }
  1064. }
  1065. }
  1066. };
  1067. // 显示树线
  1068. DTree.prototype.showLine = function($lis){
  1069. var _this = this;
  1070. if(_this.line){
  1071. if($lis && $lis.length > 0) {
  1072. $lis.each(function(){
  1073. _this.showLineLi($(this));
  1074. });
  1075. } else {
  1076. _this.obj.find("li[data-id]").each(function(){
  1077. _this.showLineLi($(this));
  1078. });
  1079. }
  1080. }
  1081. }
  1082. // 真正显示树线的方法
  1083. DTree.prototype.showLineLi = function($li){
  1084. var _this = this;
  1085. var $div = $li.children("div"),
  1086. $nextLi = $li.next("li"),
  1087. $ul = $li.parent("ul");
  1088. if($ul[0].id == _this.obj[0].id) {
  1089. // 根节点下的节点
  1090. $li.removeClass(LI_NAV_LINE);
  1091. $li.removeClass(LI_NAV_LAST_LINE);
  1092. $li.addClass(LI_NAV_FIRST_LINE);
  1093. } else {
  1094. // 非根节点下的节点
  1095. var $pnextLi = $ul.parent("li").next("li");
  1096. if($pnextLi.length == 0) {
  1097. if($nextLi.length == 0){
  1098. $li.removeClass(LI_NAV_LINE);
  1099. $li.removeClass(LI_NAV_FIRST_LINE);
  1100. $li.addClass(LI_NAV_LAST_LINE);
  1101. } else {
  1102. $li.removeClass(LI_NAV_FIRST_LINE);
  1103. $li.removeClass(LI_NAV_LAST_LINE);
  1104. $li.addClass(LI_NAV_LINE);
  1105. }
  1106. }else {
  1107. var $pnextdiv = $pnextLi.children("div");
  1108. if($nextLi.length == 0 && $div.children("cite").attr("data-leaf") == "leaf" && $pnextdiv.children("cite").attr("data-leaf") == "leaf") {
  1109. $li.removeClass(LI_NAV_FIRST_LINE);
  1110. $li.removeClass(LI_NAV_LINE);
  1111. $li.addClass(LI_NAV_LAST_LINE);
  1112. } else {
  1113. $li.removeClass(LI_NAV_FIRST_LINE);
  1114. $li.removeClass(LI_NAV_LAST_LINE);
  1115. $li.addClass(LI_NAV_LINE);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. /******************** 初始化数据区域 ********************/
  1121. // 设置高度
  1122. DTree.prototype.autoHeight = function(){
  1123. var _this = this;
  1124. var height = _this.height;
  1125. console.log(height);
  1126. if(height != "") {
  1127. if(_this.elem == _this.toolbarScroll){
  1128. _this.obj.parent().css("height", height + "px");
  1129. } else {
  1130. var $toolbarDiv = _this.obj.closest(_this.toolbarScroll);
  1131. $toolbarDiv.css("height", height + "px");
  1132. }
  1133. }
  1134. };
  1135. // 重载树
  1136. DTree.prototype.reload = function(options){
  1137. var _this = this;
  1138. _this.reloadSetting(options);
  1139. _this.init();
  1140. };
  1141. // 初始化树
  1142. DTree.prototype.init = function(){
  1143. var _this = this;
  1144. if (typeof _this !== "object") {
  1145. layer.msg("树组件未成功加载,请检查配置", {icon:5});
  1146. return ;
  1147. }
  1148. // 设置组件高度
  1149. _this.autoHeight();
  1150. if(_this.data) {
  1151. if(typeof _this.data.length === 'undefined'){
  1152. layer.msg("数据解析异常,data数据格式不正确", {icon:5});
  1153. return ;
  1154. }
  1155. if(_this.data.length == 0) {
  1156. _this.obj.html(_this.getNoneDom().text());
  1157. return ;
  1158. }
  1159. //先将ul中的元素清空
  1160. _this.obj.html("");
  1161. setTimeout(function () {
  1162. // 加载完毕后执行树解析前的回调
  1163. _this.success(_this.data, _this.obj);
  1164. // 第一次解析树
  1165. if (_this.dataFormat == 'list'){
  1166. //1.识别根节点ul中的data-id标签,判断顶级父节点
  1167. var pid = _this.obj.attr("data-id");
  1168. //2.构建一个存放节点的树组
  1169. var rootListData = _this.queryListTreeByPid(pid, _this.data);
  1170. _this.loadListTree(rootListData, _this.data, 1);
  1171. } else {
  1172. _this.loadTree(_this.data, 1);
  1173. }
  1174. // 显示树线
  1175. _this.showLine();
  1176. // 这种情况下需要一开始就将toolbar显示在页面上
  1177. if(_this.toolbar && _this.toolbarWay != 'contextmenu') {
  1178. _this.setToolbarDom().setToolbarPlace(_this.toolbarMenu);
  1179. }
  1180. // 判断是否存在错误数据,并是否打印错误数据
  1181. _this.msgErrData();
  1182. // 加载完毕后的回调
  1183. _this.done(_this.data, _this.obj);
  1184. }, 100);
  1185. } else {
  1186. if (!_this.url) {
  1187. // layer.msg("数据请求异常,url参数未指定", {icon:5});
  1188. return ;
  1189. }
  1190. //先将ul中的元素清空
  1191. _this.obj.html("");
  1192. var index = _this.load ? layer.load(1) : "";
  1193. AjaxHelper.request({
  1194. async: _this.async,
  1195. headers: _this.headers,
  1196. type: _this.method,
  1197. url: _this.url,
  1198. dataType: _this.dataType,
  1199. contentType: _this.contentType,
  1200. data: _this.getFilterRequestParam(_this.getRequestParam()),
  1201. success: function(result) {
  1202. if (typeof result === 'string') {
  1203. result = $.parseJSON(result);
  1204. }
  1205. var code = "";
  1206. if (_this.dataStyle == 'layuiStyle'){
  1207. code = result[_this.response.statusName];
  1208. } else {
  1209. code = result.status[_this.response.statusName];
  1210. }
  1211. if (code == _this.response.statusCode) {
  1212. var d = result[_this.response.rootName];
  1213. if(typeof d.length === 'undefined'){
  1214. layer.msg("数据解析异常,url回调后的数据格式不正确", {icon:5});
  1215. return ;
  1216. }
  1217. if(d.length == 0) {
  1218. _this.obj.html(_this.getNoneDom().text());
  1219. return ;
  1220. }
  1221. // 加载完毕后执行树解析前的回调
  1222. _this.success(result, _this.obj);
  1223. // 第一次解析树
  1224. if (_this.dataFormat == 'list'){
  1225. //1.识别根节点ul中的data-id标签,判断顶级父节点
  1226. var pid = _this.obj.attr("data-id");
  1227. //2.构建一个存放节点的树组
  1228. var rootListData = _this.queryListTreeByPid(pid, d);
  1229. _this.loadListTree(rootListData, d, 1);
  1230. } else {
  1231. _this.loadTree(d, 1);
  1232. }
  1233. // 显示树线
  1234. _this.showLine();
  1235. // 这种情况下需要一开始就将toolbar显示在页面上
  1236. if(_this.toolbar && _this.toolbarWay != 'contextmenu') {
  1237. _this.setToolbarDom().setToolbarPlace(_this.toolbarMenu);
  1238. }
  1239. // 判断是否存在错误数据,并是否打印错误数据
  1240. _this.msgErrData();
  1241. // 加载完毕后的回调
  1242. _this.done(result, _this.obj);
  1243. } else {
  1244. // 如果打印不出任何信息说明是在这里,用了错误的数据格式
  1245. if (_this.dataStyle == 'layuiStyle'){
  1246. layer.msg(result[_this.response.message], {icon:2});
  1247. } else {
  1248. layer.msg(result.status[_this.response.message], {icon:2});
  1249. }
  1250. }
  1251. },
  1252. error: function(XMLHttpRequest, textStatus, errorThrown){// 异步加载异常回调
  1253. _this.error(XMLHttpRequest, textStatus, errorThrown);
  1254. },
  1255. complete: function(XMLHttpRequest, textStatus){// 异步加载完成回调
  1256. if(_this.load){layer.close(index);}
  1257. _this.complete(XMLHttpRequest, textStatus);
  1258. }
  1259. });
  1260. }
  1261. };
  1262. // 加载子节点
  1263. DTree.prototype.getChild = function($div, data) {
  1264. var _this = this, $ul = $div.next("ul");
  1265. _this.setNodeParam($div);
  1266. if(typeof data !== 'undefined') {
  1267. if(typeof data.length === 'undefined'){
  1268. layer.msg("数据解析异常,data数据格式不正确", {icon:5});
  1269. return ;
  1270. }
  1271. //先将ul中的元素清空
  1272. $ul.html("");
  1273. // 解析树
  1274. if (_this.dataFormat == 'list'){
  1275. var pid = _this.node.nodeId;
  1276. var level = parseInt(_this.node.level)+1;
  1277. var listData = _this.queryListTreeByPid(pid, data);
  1278. _this.loadListTree(listData, _this.data, level);
  1279. } else {
  1280. _this.loadTree(data, level);
  1281. }
  1282. // 显示树线
  1283. _this.showLine();
  1284. // 这种情况下需要一开始就将toolbar显示在页面上
  1285. if(_this.toolbar && _this.toolbarWay != 'contextmenu') {
  1286. _this.setToolbarDom().setToolbarPlace(_this.toolbarMenu);
  1287. }
  1288. // 判断是否存在错误数据,并是否打印错误数据
  1289. _this.msgErrData();
  1290. } else {
  1291. if (!_this.url) {
  1292. // layer.msg("数据请求异常,url参数未指定", {icon:5});
  1293. return ;
  1294. }
  1295. $ul.html("");
  1296. var index = _this.load ? layer.load(1) : "";
  1297. AjaxHelper.request({
  1298. async: _this.async,
  1299. headers: _this.headers,
  1300. type: _this.method,
  1301. url: _this.url,
  1302. dataType: _this.dataType,
  1303. data: _this.getFilterRequestParam(_this.getRequestParam()),
  1304. success: function(result) {
  1305. if (typeof result === 'string') {
  1306. result = $.parseJSON(result);
  1307. }
  1308. var code = "";
  1309. if (_this.dataStyle == 'layuiStyle'){
  1310. code = result[_this.response.statusName];
  1311. } else {
  1312. code = result.status[_this.response.statusName];
  1313. }
  1314. if (code == _this.response.statusCode) {
  1315. // 解析树
  1316. var pid = _this.node.nodeId;
  1317. var level = parseInt(_this.node.level)+1;
  1318. if (_this.dataFormat == 'list'){
  1319. var pListData = _this.queryListTreeByPid(pid, result[_this.response.rootName]);
  1320. _this.loadListTree(pListData, result[_this.response.rootName], level, $ul);
  1321. } else {
  1322. _this.loadTree(result[_this.response.rootName], level, $ul);
  1323. }
  1324. // 显示树线
  1325. _this.showLine();
  1326. // 这种情况下需要一开始就将toolbar显示在页面上
  1327. if(_this.toolbar && _this.toolbarWay != 'contextmenu') {
  1328. _this.setToolbarDom().setToolbarPlace(_this.toolbarMenu);
  1329. }
  1330. // 判断是否存在错误数据,并是否打印错误数据
  1331. _this.msgErrData();
  1332. $ul.addClass(NAV_SHOW);
  1333. } else {
  1334. if (_this.dataStyle == 'layuiStyle'){
  1335. layer.msg(result[_this.response.message], {icon:2});
  1336. } else {
  1337. layer.msg(result.status[_this.response.message], {icon:2});
  1338. }
  1339. }
  1340. },
  1341. error: function(XMLHttpRequest, textStatus, errorThrown){// 异步加载异常回调
  1342. _this.error();
  1343. },
  1344. complete: function(XMLHttpRequest, textStatus){// 异步加载完成回调
  1345. if(_this.load){layer.close(index);}
  1346. _this.complete();
  1347. }
  1348. });
  1349. }
  1350. };
  1351. // 初始化树或者拼接树
  1352. DTree.prototype.loadListTree = function(pListData, listData, level, $ul){
  1353. var _this = this;
  1354. $ul = $ul || _this.getNodeDom().nowOrRootUl(); //当前选中的节点或根节点
  1355. if (pListData.length > 0){
  1356. for (var i = 0; i < pListData.length; i++) {
  1357. // 1.获取已知节点的全部数据
  1358. var data = pListData[i];
  1359. if(typeof data !== "object") continue;
  1360. var parseData = _this.parseData(data);
  1361. var childListData = _this.queryListTreeByPid(parseData.treeId(), listData); // 根据已知数据的id判断该条数据是否还有子数据
  1362. // 3. 页面元素加载数据
  1363. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.fmtTitle(), parseData.last(childListData.length), parseData.ficonClass(), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(level), parseData.disabled(), parseData.hide(), parseData.basicData(), parseData.recordData(), ($ul.hasClass(UL_ROOT) ? "root" : "item")));
  1364. // 4.有子数据的元素加载子节点
  1365. if(childListData.length > 0){
  1366. var cLevel = parseInt(level)+1;
  1367. _this.loadListTree(childListData, listData, cLevel, _this.obj.find("ul[data-id='"+parseData.treeId()+"']"));
  1368. }
  1369. }
  1370. }
  1371. };
  1372. // 根据父ID查找list数据中匹配的元素
  1373. DTree.prototype.queryListTreeByPid = function(pid, listData){
  1374. var _this = this;
  1375. var rootListData = [];
  1376. if (listData) {
  1377. for (var i = 0; i < listData.length; i++) {
  1378. var data = listData[i];
  1379. if(typeof data !== "object") continue;
  1380. if(pid == "null" || pid == null){
  1381. if(data[_this.response.parentId] == null) { rootListData.push(data); }
  1382. } else {
  1383. if (data[_this.response.parentId] == pid){
  1384. if (data[_this.response.treeId] == pid){
  1385. _this.errData.push(data);
  1386. } else {
  1387. rootListData.push(data);
  1388. }
  1389. }
  1390. }
  1391. }
  1392. }
  1393. return rootListData;
  1394. };
  1395. // 初始化树或者拼接树
  1396. DTree.prototype.loadTree = function(root, level, $ul){
  1397. var _this = this;
  1398. if (root) {
  1399. $ul = $ul || _this.getNodeDom().nowOrRootUl(); //当前选中的节点或根节点
  1400. for (var i = 0; i < root.length; i++) { // 遍历跟节点或追加的跟节点
  1401. var data = root[i];
  1402. if(typeof data !== "object") continue;
  1403. if(data[_this.response.treeId] == data[_this.response.parentId]) { _this.errData.push(data); }
  1404. var parseData = _this.parseData(data);
  1405. var children = parseData.children();
  1406. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.fmtTitle(), parseData.last(children.length), parseData.ficonClass(), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(level), parseData.disabled(), parseData.hide(), parseData.basicData(), parseData.recordData(), ($ul.hasClass(UL_ROOT) ? "root" : "item")));
  1407. if (children.length != 0) {
  1408. var cLevel = parseInt(level)+1;
  1409. _this.loadTree(children, cLevel, _this.obj.find("ul[data-id='"+parseData.treeId()+"']"));
  1410. }
  1411. }
  1412. }
  1413. };
  1414. // 判断在数据加载时是否存在错误数据,并是否打印错误数据
  1415. DTree.prototype.msgErrData = function() {
  1416. var _this = this;
  1417. if(_this.errData.length > 0 && _this.errDataShow) {
  1418. var title = "";
  1419. for(var i=0; i<_this.errData.length; i++) {
  1420. var edata = _this.errData[i];
  1421. title += "数据:【"+edata[_this.response.title]+"】中节点id和上级id值一致! \n";
  1422. }
  1423. layer.msg(title, {icon:2,time:5000});
  1424. }
  1425. // 显示之后,将错误数据制空
  1426. _this.errData = [];
  1427. };
  1428. // 解析data数据
  1429. DTree.prototype.parseData = function(data) {
  1430. var _this = this;
  1431. return {
  1432. treeId: function(){
  1433. return data[_this.response.treeId];
  1434. },
  1435. parentId: function(){
  1436. return data[_this.response.parentId];
  1437. },
  1438. fmtTitle: function(){
  1439. if(typeof _this.formatter.title === 'function'){
  1440. var ftitle = _this.formatter.title(data);
  1441. var tt = data[_this.response.title];
  1442. tt = (ftitle == "" || ftitle == undefined || ftitle == null) ? tt : ftitle;
  1443. return tt || "";
  1444. }
  1445. return data[_this.response.title];
  1446. },
  1447. title: function(){
  1448. return data[_this.response.title];
  1449. },
  1450. level: function(){
  1451. return data[_this.response.level] || "";
  1452. },
  1453. ficonClass: function(){
  1454. return data[_this.response.ficonClass] || "";
  1455. },
  1456. iconClass: function(){
  1457. return data[_this.response.iconClass] || "";
  1458. },
  1459. last: function(len){
  1460. return ((len == 0) ?
  1461. ((typeof (data[_this.response.last]) === "boolean") ? data[_this.response.last] : true) :
  1462. ((typeof (data[_this.response.last]) === "boolean") ? data[_this.response.last] : false));
  1463. },
  1464. spread: function(level){
  1465. return ((level < _this.initLevel) ?
  1466. ((typeof (data[_this.response.spread]) === "boolean") ? data[_this.response.spread] : true) :
  1467. ((typeof (data[_this.response.spread]) === "boolean") ? data[_this.response.spread] : false));
  1468. },
  1469. disabled: function(){
  1470. return (typeof (data[_this.response.disabled]) === "boolean") ? data[_this.response.disabled] : false;
  1471. },
  1472. hide: function(){
  1473. return (typeof (data[_this.response.hide]) === "boolean") ? data[_this.response.hide] : false;
  1474. },
  1475. checkArr: function(){
  1476. var checkArr = [];
  1477. var checkArrData = data[_this.response.checkArr];
  1478. if(typeof checkArrData === 'string'){
  1479. if(checkArrData.indexOf("{") > -1 && checkArrData.indexOf("}") > -1){
  1480. checkArrData = JSON.parse(checkArrData);
  1481. } else {
  1482. checkArrData = {"type":"0","checked":checkArrData};
  1483. }
  1484. }
  1485. if(typeof checkArrData === 'object'){
  1486. if(typeof checkArrData.length === 'undefined'){
  1487. checkArr.push(checkArrData);
  1488. } else {
  1489. checkArr = checkArrData;
  1490. }
  1491. }
  1492. if(checkArr.length > 0 && checkArr.length > _this.checkArrLen){
  1493. _this.checkArrLen = checkArr.length; // 获取复选框个数
  1494. }
  1495. return checkArr;
  1496. },
  1497. children: function(){
  1498. return data[_this.response.childName] || [];
  1499. },
  1500. basicData: function(){
  1501. return event.escape(JSON.stringify(data[_this.response.basicData])) || JSON.stringify({});
  1502. },
  1503. recordData: function(){
  1504. //var recordData = _this.record ? event.cloneObj(data, [_this.response.basicData, _this.response.childName]) : {};
  1505. var recordData = _this.record ? event.cloneObj(data, [_this.response.treeId,
  1506. _this.response.parentId,
  1507. _this.response.title,
  1508. _this.response.iconClass,
  1509. _this.response.childName,
  1510. _this.response.last,
  1511. _this.response.spread,
  1512. _this.response.disabled,
  1513. _this.response.hide,
  1514. _this.response.checkArr,
  1515. _this.response.checked,
  1516. _this.response.type,
  1517. _this.response.basicData]) : {};
  1518. return event.escape(JSON.stringify(recordData));
  1519. },
  1520. data: function(){
  1521. return data;
  1522. }
  1523. }
  1524. };
  1525. //当无节点数据时显示dom
  1526. DTree.prototype.getNoneDom = function(){
  1527. var _this = this,
  1528. rootId = _this.obj[0].id,
  1529. noneTitle = _this.none;
  1530. return {
  1531. text: function(){
  1532. return "<div class='"+NONETITLE+"' dtree-id='"+rootId+"'>"+noneTitle+"</div>";
  1533. }
  1534. }
  1535. };
  1536. //新增节点的dom值
  1537. DTree.prototype.getDom = function(treeId, parentId, title, fmtTitle, last, ficonClass, iconClass, checkArr, level, spread, disabled, hide) {
  1538. var _this = this,
  1539. rootId = _this.obj[0].id,
  1540. toolbar = _this.toolbar,
  1541. checkbar = _this.checkbar;
  1542. return {
  1543. fnode: function() { // + - 图标
  1544. // 获取图标的变量
  1545. var fnodeIcon = _this.fnodeIcon,
  1546. fleafIcon = _this.fleafIcon;
  1547. var fleafIconLeaf = _this.usefontStyle.fnode.leaf,
  1548. fnodeIconOpen = _this.usefontStyle.fnode.node.open,
  1549. fnodeIconClose = _this.usefontStyle.fnode.node.close;
  1550. if(ficonClass){
  1551. var iconfont = _this.iconfont;
  1552. if(typeof iconfont === 'string') {
  1553. fleafIconLeaf = iconfont + " " + ficonClass;
  1554. fnodeIconOpen = iconfont + " " + ficonClass;
  1555. fnodeIconClose = iconfont + " " + ficonClass;
  1556. } else {
  1557. fleafIconLeaf = iconfont[0] + " " + ficonClass;
  1558. fnodeIconOpen = iconfont[0] + " " + ficonClass;
  1559. fnodeIconClose = iconfont[0] + " " + ficonClass;
  1560. }
  1561. }
  1562. if(fnodeIcon != "-1" && fleafIcon != "-1"){ // 都加载
  1563. return last ? "<i class='"+fleafIconLeaf+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" :
  1564. (spread ? "<i class='"+fnodeIconOpen+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" : "<i class='"+fnodeIconClose+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1565. }
  1566. if(fnodeIcon != "-1" && fleafIcon == "-1"){ // 加载node 隐藏leaf
  1567. return last ? "<i class='"+fleafIconLeaf+" "+ICON_HIDE+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" :
  1568. (spread ? "<i class='"+fnodeIconOpen+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" : "<i class='"+fnodeIconClose+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1569. }
  1570. if(fnodeIcon == "-1" && fleafIcon != "-1"){ // 隐藏node 加载leaf
  1571. return last ? "<i class='"+fleafIconLeaf+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" :
  1572. (spread ? "<i class='"+fnodeIconOpen+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" : "<i class='"+fnodeIconClose+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1573. }
  1574. if(fnodeIcon == "-1" && fleafIcon == "-1"){ // 都隐藏
  1575. return last ? "<i class='"+fleafIconLeaf+" "+ICON_HIDE+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' style='display:none;'></i>" :
  1576. (spread ? "<i class='"+fnodeIconOpen+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"'></i>" : "<i class='"+fnodeIconClose+" "+_this.style.dfont+" "+_this.style.ficon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1577. }
  1578. },
  1579. node: function() { // 二级图标样式
  1580. // 获取图标的变量
  1581. var nodeIcon = _this.nodeIcon,
  1582. leafIcon = _this.leafIcon;
  1583. var sleafIconLeaf = _this.usefontStyle.snode.leaf,
  1584. snodeIconOpen = _this.usefontStyle.snode.node.open,
  1585. snodeIconClose = _this.usefontStyle.snode.node.close;
  1586. if(iconClass){
  1587. var iconfont = _this.iconfont;
  1588. if(typeof iconfont === 'string') {
  1589. sleafIconLeaf = iconfont + " " + iconClass;
  1590. snodeIconOpen = iconfont + " " + iconClass;
  1591. snodeIconClose = iconfont + " " + iconClass;
  1592. } else {
  1593. sleafIconLeaf = iconfont[0] + " " + iconClass;
  1594. snodeIconOpen = iconfont[0] + " " + iconClass;
  1595. snodeIconClose = iconfont[0] + " " + iconClass;
  1596. }
  1597. }
  1598. if(nodeIcon != "-1" && leafIcon != "-1"){ // 都加载
  1599. return last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1600. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1601. }
  1602. if(nodeIcon != "-1" && leafIcon == "-1"){ // 加载node 隐藏leaf
  1603. return last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1604. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1605. }
  1606. if(nodeIcon == "-1" && leafIcon != "-1"){ // 隐藏node 加载leaf
  1607. return last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1608. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1609. }
  1610. if(nodeIcon == "-1" && leafIcon == "-1"){ // 都隐藏
  1611. return last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1612. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1613. }
  1614. },
  1615. checkbox: function() { // 复选框
  1616. var flag = false;
  1617. if(_this.checkbarLoad == "node"){if (checkbar) {flag = true;}} else {if (last) {if (checkbar) {flag = true;}}}
  1618. if(flag){
  1619. var result = "<div class='"+LI_DIV_CHECKBAR+"' data-id='"+treeId+"' dtree-id='"+rootId+"'>";
  1620. if(checkArr && checkArr.length > 0){
  1621. for (var i = 0; i < checkArr.length; i++) {
  1622. var checkData = checkArr[i];
  1623. var checked = checkData.checked;
  1624. var CHOOSE_CLASS = _this.usefontStyle.checkbox.out;
  1625. if (checked == "2") { //半选择
  1626. CHOOSE_CLASS = _this.usefontStyle.checkbox.noall + " " + _this.style.chs;
  1627. } else if (checked == "1") { //选择
  1628. CHOOSE_CLASS = _this.usefontStyle.checkbox.on + " " + _this.style.chs;
  1629. } else { //未选择或者无值
  1630. CHOOSE_CLASS = _this.usefontStyle.checkbox.out;
  1631. }
  1632. var disClass = "";
  1633. if(disabled){disClass = NAV_DIS;}
  1634. result += "<i class='"+CHOOSE_CLASS+" "+_this.style.dfont+" "+_this.style.cbox+" "+disClass+"' data-id='"+treeId+"' dtree-id='"+rootId+"' data-checked='"+checkData.checked+"' data-initchecked='"+checkData.checked+"' data-type='"+checkData.type+"' dtree-click='"+eventName.checkNodeClick+"' data-par='."+LI_CLICK_CHECKBAR+"' dtree-disabled='"+disabled+"'></i>";
  1635. }
  1636. }
  1637. result += "</div>";
  1638. return result;
  1639. }
  1640. return "";
  1641. },
  1642. text: function() { // 文字显示
  1643. var disClass = "";
  1644. if(disabled){disClass = NAV_DIS;}
  1645. return "<cite class='"+LI_DIV_TEXT_CLASS+" "+disClass+"' data-id='"+treeId+"' data-leaf='"+(last ? "leaf" : "node")+"' dtree-disabled='"+disabled+"' data-title='"+title+"' >"+fmtTitle+"</cite>";
  1646. },
  1647. ul: function() { //子节点ul
  1648. return last ? "<ul class='"+LI_NAV_CHILD+"' data-id='"+treeId+"' dtree-id='"+rootId+"'></ul>" :
  1649. (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>");
  1650. }
  1651. };
  1652. };
  1653. //替换节点的dom值,或指定值
  1654. DTree.prototype.replaceDom = function($div, treeId, last, spread, disabled, hide) {
  1655. var _this = this,
  1656. rootId = _this.obj[0].id,
  1657. toolbar = _this.toolbar,
  1658. checkbar = _this.checkbar;
  1659. return {
  1660. fnode: function(ficonClass) { // + - 图标
  1661. var fnode = "";
  1662. // 获取图标的变量
  1663. var fnodeIcon = _this.fnodeIcon,
  1664. fleafIcon = _this.fleafIcon;
  1665. var fleafIconLeaf = _this.usefontStyle.fnode.leaf,
  1666. fnodeIconOpen = _this.usefontStyle.fnode.node.open,
  1667. fnodeIconClose = _this.usefontStyle.fnode.node.close;
  1668. if(ficonClass){
  1669. var iconfont = _this.iconfont;
  1670. if(typeof iconfont === 'string') {
  1671. fleafIconLeaf = iconfont + " " + ficonClass;
  1672. fnodeIconOpen = iconfont + " " + ficonClass;
  1673. fnodeIconClose = iconfont + " " + ficonClass;
  1674. } else {
  1675. fleafIconLeaf = iconfont[0] + " " + ficonClass;
  1676. fnodeIconOpen = iconfont[0] + " " + ficonClass;
  1677. fnodeIconClose = iconfont[0] + " " + ficonClass;
  1678. }
  1679. }
  1680. if(fnodeIcon != "-1" && leafIcon != "-1"){ // 都加载
  1681. fnode = last ? "<i class='"+fleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1682. (spread ? "<i class='"+fnodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+fnodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1683. }else if(nodeIcon != "-1" && leafIcon == "-1"){ // 加载node 隐藏leaf
  1684. fnode = last ? "<i class='"+fleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1685. (spread ? "<i class='"+fnodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+fnodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1686. }else if(nodeIcon == "-1" && leafIcon != "-1"){ // 隐藏node 加载leaf
  1687. fnode = last ? "<i class='"+fleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1688. (spread ? "<i class='"+fnodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+fnodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1689. }else if(nodeIcon == "-1" && leafIcon == "-1"){ // 都隐藏
  1690. fnode = last ? "<i class='"+fleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1691. (spread ? "<i class='"+fnodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+fnodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+ficonClass+"'></i>");
  1692. }
  1693. if(fnode != ""){_this.getNodeDom($div).fnode().replaceWith($(fnode));}
  1694. },
  1695. node: function(iconClass) { // 二级图标样式
  1696. var snode = "";
  1697. // 获取图标的变量
  1698. var nodeIcon = _this.nodeIcon,
  1699. leafIcon = _this.leafIcon;
  1700. var sleafIconLeaf = _this.usefontStyle.snode.leaf,
  1701. snodeIconOpen = _this.usefontStyle.snode.node.open,
  1702. snodeIconClose = _this.usefontStyle.snode.node.close;
  1703. if(iconClass){
  1704. var iconfont = _this.iconfont;
  1705. if(typeof iconfont === 'string') {
  1706. sleafIconLeaf = iconfont + " " + iconClass;
  1707. snodeIconOpen = iconfont + " " + iconClass;
  1708. snodeIconClose = iconfont + " " + iconClass;
  1709. } else {
  1710. sleafIconLeaf = iconfont[0] + " " + iconClass;
  1711. snodeIconOpen = iconfont[0] + " " + iconClass;
  1712. snodeIconClose = iconfont[0] + " " + iconClass;
  1713. }
  1714. }
  1715. if(nodeIcon != "-1" && leafIcon != "-1"){ // 都加载
  1716. snode = last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1717. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1718. }else if(nodeIcon != "-1" && leafIcon == "-1"){ // 加载node 隐藏leaf
  1719. snode = last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1720. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1721. }else if(nodeIcon == "-1" && leafIcon != "-1"){ // 隐藏node 加载leaf
  1722. snode = last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1723. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1724. }else if(nodeIcon == "-1" && leafIcon == "-1"){ // 都隐藏
  1725. snode = last ? "<i class='"+sleafIconLeaf+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='last' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" :
  1726. (spread ? "<i class='"+snodeIconOpen+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='open' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>" : "<i class='"+snodeIconClose+" "+DTREEFONTSPECIAL+" "+_this.style.dfont+" "+_this.style.icon+"' data-spread='close' data-id='"+treeId+"' dtree-id='"+rootId+"' data-iconClass='"+iconClass+"'></i>");
  1727. }
  1728. if(snode != ""){_this.getNodeDom($div).snode().replaceWith($(snode));}
  1729. },
  1730. checkbox: function(checkArr) { // 复选框
  1731. var flag = false;
  1732. if(_this.checkbarLoad == "node"){if (checkbar) {flag = true;}} else {if (last) {if (checkbar) {flag = true;}}}
  1733. if(flag){
  1734. var result = "<div class='"+LI_DIV_CHECKBAR+"' data-id='"+treeId+"' dtree-id='"+rootId+"'>";
  1735. if(checkArr && checkArr.length > 0){
  1736. for (var i = 0; i < checkArr.length; i++) {
  1737. var checkData = checkArr[i];
  1738. var checked = checkData.checked;
  1739. var CHOOSE_CLASS = _this.usefontStyle.checkbox.out;
  1740. if (checked == "2") { //半选择
  1741. CHOOSE_CLASS = _this.usefontStyle.checkbox.noall + " " + _this.style.chs;
  1742. } else if (checked == "1") { //选择
  1743. CHOOSE_CLASS = _this.usefontStyle.checkbox.on + " " + _this.style.chs;
  1744. } else { //未选择或者无值
  1745. CHOOSE_CLASS = _this.usefontStyle.checkbox.out;
  1746. }
  1747. var disClass = "";
  1748. if(disabled){disClass = NAV_DIS;}
  1749. result += "<i class='"+CHOOSE_CLASS+" "+_this.style.dfont+" "+_this.style.cbox+" "+disClass+"' data-id='"+treeId+"' dtree-id='"+rootId+"' data-checked='"+checkData.checked+"' data-initchecked='"+checkData.checked+"' data-type='"+checkData.type+"' dtree-click='"+eventName.checkNodeClick+"' data-par='."+LI_CLICK_CHECKBAR+"' dtree-disabled='"+disabled+"'></i>";
  1750. }
  1751. }
  1752. result += "</div>";
  1753. _this.getNodeDom($div).snode().next("div").replaceWith($(result));
  1754. }
  1755. },
  1756. text: function(title) { // 文字显示
  1757. var disClass = "";
  1758. if(disabled){disClass = NAV_DIS;}
  1759. var cite = "<cite class='"+LI_DIV_TEXT_CLASS+" "+disClass+"' data-id='"+treeId+"' data-leaf='"+(last ? "leaf" : "node")+"' dtree-disabled='"+disabled+"' >"+title+"</cite>"
  1760. _this.getNodeDom($div).cite().replaceWith($(cite));
  1761. },
  1762. ul: function() { //子节点ul
  1763. var ul = last ? "<ul class='"+LI_NAV_CHILD+"' data-id='"+treeId+"' dtree-id='"+rootId+"'></ul>" :
  1764. (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>");
  1765. _this.getNodeDom($div).nextUl().replaceWith($(ul));
  1766. },
  1767. basicData: function(basicData){
  1768. basicData = (basicData == "{}") ? "" : basicData;
  1769. $div.attr("data-basic", basicData);
  1770. },
  1771. recordData: function(recordData){
  1772. recordData = (recordData == "{}") ? "" : recordData;
  1773. $div.attr("data-record", recordData);
  1774. },
  1775. p_li: function(){
  1776. $div.parent("li").attr("data-id", treeId);
  1777. }
  1778. };
  1779. };
  1780. // 获取拼接好的li
  1781. DTree.prototype.getLiItemDom = function(treeId, parentId, title, fmtTitle, last, ficonClass, iconClass, checkArr, level, spread, disabled, hide, basicData, recordData, flag) {
  1782. var _this = this,
  1783. rootId = _this.obj[0].id;
  1784. var dom = _this.getDom(treeId, parentId, title, fmtTitle, last, ficonClass, iconClass, checkArr, level, spread, disabled, hide);
  1785. basicData = (basicData == "{}") ? "" : basicData;
  1786. recordData = (recordData == "{}") ? "" : recordData;
  1787. 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+"' dtree-disabled='"+disabled+"' dtree-hide='"+hide+"' ";
  1788. if(_this.toolbar){
  1789. if(_this.toolbarWay == "contextmenu") {
  1790. if(_this.toolbarLoad == "node") { div += " d-contextmenu='true'>"; }
  1791. if(_this.toolbarLoad == "noleaf") { if(!last){ div += " d-contextmenu='true'>"; } else { div += " d-contextmenu='false'>";} }
  1792. if(_this.toolbarLoad == "leaf") { if(last){ div += " d-contextmenu='true'>"; } else { div += " d-contextmenu='false'>";} }
  1793. } else { div += " d-contextmenu='false'>"; }
  1794. } else { div += " d-contextmenu='false'>"; }
  1795. var hideClass = "";
  1796. var lineClass = "";
  1797. if(hide){hideClass = NAV_HIDE;}
  1798. /*if(_this.line){
  1799. if(flag == "root") {
  1800. lineClass = LI_NAV_FIRST_LINE;
  1801. } else if(last == true){
  1802. lineClass = LI_NAV_LAST_LINE;
  1803. } else {
  1804. lineClass = LI_NAV_LINE;
  1805. }
  1806. }*/
  1807. /*if(_this.line){
  1808. if(flag == "root") {
  1809. lineClass = LI_NAV_FIRST_LINE;
  1810. } else {
  1811. lineClass = LI_NAV_LINE;
  1812. }
  1813. }*/
  1814. var li = ["<li " + "class='"+LI_CLICK_CHECKBAR+" "+LI_NAV_ITEM+" "+hideClass+" "+lineClass+"'" + "data-id='"+treeId+"'" + "data-pid='"+(flag == "root" ? ((typeof parentId !== undefined && parentId != "") ? parentId : "-1") : parentId)+"'" + "dtree-id='"+rootId+"'" + "data-index='"+level+"'" + "dtree-hide='"+hide+"'" +">" +
  1815. div ,
  1816. dom.fnode(),
  1817. dom.node(),
  1818. dom.checkbox(),
  1819. dom.text(),
  1820. "</div>", dom.ul(), "</li>"].join("");
  1821. return li;
  1822. };
  1823. // 初始化节点,用于数据回显
  1824. DTree.prototype.dataInit = function(chooseId){
  1825. var _this = this;
  1826. var $div = _this.obj.find("div[data-id='"+chooseId+"']");
  1827. _this.getNodeDom($div).parentLi().find("."+NAV_THIS).removeClass(NAV_THIS);
  1828. _this.getNodeDom($div).parentLi().find("."+_this.style.itemThis).removeClass(_this.style.itemThis);
  1829. $div.addClass(NAV_THIS);
  1830. $div.addClass(_this.style.itemThis);
  1831. _this.setNodeParam($div);
  1832. // 将该节点的父节点全部展开
  1833. var $li_parents = $div.parents("."+LI_NAV_ITEM);
  1834. $li_parents.children("ul").addClass(NAV_SHOW);
  1835. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.fnode.node.close)).addClass(_this.usefontStyle.fnode.node.open);
  1836. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.fnode.node.close)).removeClass(_this.usefontStyle.fnode.node.close);
  1837. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.snode.node.close)).addClass(_this.usefontStyle.snode.node.open);
  1838. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.snode.node.close)).removeClass(_this.usefontStyle.snode.node.close);
  1839. return _this.getNowParam();
  1840. };
  1841. /******************** 基础事件区域 ********************/
  1842. // 数据格式化
  1843. DTree.prototype.escape = function(html){
  1844. return event.escape(html);
  1845. };
  1846. // 格式化数据转回正常数据
  1847. DTree.prototype.unescape = function(str){
  1848. return event.unescape(str);
  1849. };
  1850. // 取消选中div
  1851. DTree.prototype.cancelNavThis = function(){
  1852. var _this = this;
  1853. _this.obj.find("div[data-id]").parent().find("."+NAV_THIS).removeClass(NAV_THIS);
  1854. _this.obj.find("div[data-id]").parent().find("."+_this.style.itemThis).removeClass(_this.style.itemThis);
  1855. }
  1856. // 选中div
  1857. DTree.prototype.navThis = function(id){
  1858. var _this = this;
  1859. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  1860. if($div != null) {
  1861. _this.cancelNavThis();
  1862. $div.addClass(NAV_THIS);
  1863. $div.addClass(_this.style.itemThis);
  1864. }
  1865. }
  1866. // 手风琴模式操作其他节点
  1867. DTree.prototype.accordionUL = function($ul) {
  1868. var _this = this;
  1869. if(_this.accordion) {
  1870. $ul.closest("li[data-index]").siblings("li[data-index]").children("ul[data-id]").removeClass(NAV_SHOW);
  1871. var $divs = $ul.closest("li[data-index]").siblings("li[data-index]").children("ul[data-id]").prev("div");
  1872. if($divs.length && $divs.length > 0) {
  1873. for (var i=0; i<$divs.length; i++) {
  1874. var $div = $($divs[i]);
  1875. var $i_spread = _this.getNodeDom($div).fnode(),
  1876. $i_node = _this.getNodeDom($div).snode();
  1877. if($i_spread.attr("data-spread") != 'last'){
  1878. _this.operateIcon($i_spread, $i_node).close();
  1879. }
  1880. }
  1881. }
  1882. }
  1883. };
  1884. // 展开或隐藏节点 作用点: div
  1885. DTree.prototype.clickSpread = function($div) {
  1886. var _this = this;
  1887. var $i_spread = _this.getNodeDom($div).fnode(),
  1888. $i_node = _this.getNodeDom($div).snode(),
  1889. $cite = _this.getNodeDom($div).cite(),
  1890. spread = $i_spread.attr("data-spread"),
  1891. $ul = $div.next("ul");
  1892. if ($ul.length > 0) {
  1893. if (spread == "close") {
  1894. if (_this.type=="load") { //增加加载
  1895. if (_this.cache) { //开启缓存
  1896. if ($ul.html()) {
  1897. $ul.addClass(NAV_SHOW);
  1898. _this.accordionUL($ul);
  1899. } else { //加载节点
  1900. _this.getChild($div);
  1901. _this.accordionUL($ul);
  1902. }
  1903. }else { //每次取新的数据
  1904. $ul.html("");
  1905. _this.getChild($div);
  1906. _this.accordionUL($ul);
  1907. }
  1908. } else { // 全量加载
  1909. $ul.addClass(NAV_SHOW);
  1910. _this.accordionUL($ul);
  1911. }
  1912. _this.operateIcon($i_spread, $i_node).open();
  1913. } else if (spread == "open") {
  1914. $ul.removeClass(NAV_SHOW);
  1915. _this.operateIcon($i_spread, $i_node).close();
  1916. }
  1917. }
  1918. };
  1919. // 设置节点为disabled
  1920. DTree.prototype.setDisabledNodes = function(disabledIds){
  1921. var _this = this;
  1922. var disabledId = disabledIds.split(",");
  1923. for (var i=0; i<disabledId.length; i++) {
  1924. var $div = _this.getNodeDom(disabledId[i]).div();
  1925. var $i = $div.children("div."+LI_DIV_CHECKBAR).children("i[data-par]");
  1926. var $cite = $div.children("cite[data-leaf]");
  1927. if($div != null && $div.attr("dtree-disabled") != "true") {
  1928. $div.attr("dtree-disabled", "true");
  1929. $i.attr("dtree-disabled", "true");
  1930. $i.addClass(NAV_DIS);
  1931. $cite.attr("dtree-disabled", "true");
  1932. $cite.addClass(NAV_DIS);
  1933. }
  1934. }
  1935. };
  1936. // 将节点的disabled取消
  1937. DTree.prototype.cancelDisabledNodes = function(disabledIds){
  1938. var _this = this;
  1939. var disabledId = disabledIds.split(",");
  1940. for (var i=0; i<disabledId.length; i++) {
  1941. var $div = _this.getNodeDom(disabledId[i]).div();
  1942. var $i = $div.children("div."+LI_DIV_CHECKBAR).children("i[data-par]");
  1943. var $cite = $div.children("cite[data-leaf]");
  1944. if($div != null && $div.attr("dtree-disabled") == "true") {
  1945. $div.attr("dtree-disabled", "false");
  1946. $i.attr("dtree-disabled", "false");
  1947. $i.removeClass(NAV_DIS);
  1948. $cite.attr("dtree-disabled", "false");
  1949. $cite.removeClass(NAV_DIS);
  1950. }
  1951. }
  1952. };
  1953. // 获取指定disabled节点的值
  1954. DTree.prototype.getDisabledNodesParam = function(disabledIds){
  1955. var _this = this;
  1956. var disabledId = disabledIds.split(",");
  1957. var disabledNodes = [];
  1958. for (var i=0; i<disabledId.length; i++) {
  1959. var $div = _this.getNodeDom(disabledId[i]).div();
  1960. if($div != null && $div.attr("dtree-disabled") == "true") {
  1961. disabledNodes.push(_this.getRequestParam(_this.getTempNodeParam($div)));
  1962. }
  1963. }
  1964. return disabledNodes;
  1965. };
  1966. // 获取全部disabled节点的值
  1967. DTree.prototype.getAllDisabledNodesParam = function(){
  1968. var _this = this;
  1969. var disabledNodes = [];
  1970. _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][dtree-disabled='true']").each(function(){
  1971. var $div = $(this);
  1972. disabledNodes.push(_this.getRequestParam(_this.getTempNodeParam($div)));
  1973. });
  1974. return disabledNodes;
  1975. };
  1976. // 设置节点为hide
  1977. DTree.prototype.setHideNodes = function(hideIds){
  1978. var _this = this;
  1979. var hideId = hideIds.split(",");
  1980. for (var i=0; i<hideId.length; i++) {
  1981. var $div = _this.getNodeDom(hideId[i]).div();
  1982. var $li = $div.parent("li[dtree-hide]");
  1983. if($div != null && $div.attr("dtree-hide") != "true") {
  1984. $div.attr("dtree-hide", "true");
  1985. $li.attr("dtree-hide", "true");
  1986. $li.addClass(NAV_HIDE);
  1987. }
  1988. }
  1989. };
  1990. // 将节点的hide取消
  1991. DTree.prototype.cancelHideNodes = function(hideIds){
  1992. var _this = this;
  1993. var hideId = hideIds.split(",");
  1994. for (var i=0; i<hideId.length; i++) {
  1995. var $div = _this.getNodeDom(hideId[i]).div();
  1996. var $li = $div.parent("li[dtree-hide]");
  1997. if($div != null && $div.attr("dtree-hide") == "true") {
  1998. $div.attr("dtree-hide", "false");
  1999. $li.attr("dtree-hide", "false");
  2000. $li.removeClass(NAV_HIDE);
  2001. }
  2002. }
  2003. };
  2004. // 获取指定hide节点的值
  2005. DTree.prototype.getHideNodesParam = function(hideIds){
  2006. var _this = this;
  2007. var hideId = hideIds.split(",");
  2008. var hideNodes = [];
  2009. for (var i=0; i<hideId.length; i++) {
  2010. var $div = _this.getNodeDom(hideId[i]).div();
  2011. if($div != null && $div.attr("dtree-hide") == "true") {
  2012. hideNodes.push(_this.getRequestParam(_this.getTempNodeParam($div)));
  2013. }
  2014. }
  2015. return hideNodes;
  2016. };
  2017. // 获取全部hide节点的值
  2018. DTree.prototype.getAllHideNodesParam = function(){
  2019. var _this = this;
  2020. var hideNodes = [];
  2021. _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][dtree-hide='true']").each(function(){
  2022. var $div = $(this);
  2023. hideNodes.push(_this.getRequestParam(_this.getTempNodeParam($div)));
  2024. });
  2025. return hideNodes;
  2026. };
  2027. // 刷新树
  2028. DTree.prototype.refreshTree = function(){
  2029. var _this = this;
  2030. _this.obj.html(""); // 清空树结构
  2031. _this.initNodeParam(); // 清空参数
  2032. _this.init(); //执行初始化方法
  2033. }
  2034. // 局部刷新树--新增子节点时
  2035. DTree.prototype.partialRefreshAdd = function($div, data){
  2036. var _this = this;
  2037. $ul = $div.next("ul");
  2038. // 判断当前点击的节点是否是最后一级节点,如果是,则需要修改节点的样式
  2039. var $icon_i = $div.find("i[data-spread]");
  2040. if ($icon_i.eq(0).attr("data-spread") == "last") {
  2041. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).openWithLeaf();
  2042. } else { //如果不是,也要修改节点样式
  2043. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).open();
  2044. }
  2045. $ul.addClass(NAV_SHOW); //展开UL
  2046. _this.accordionUL($ul);
  2047. if(data) {
  2048. if(data.length && data.length > 0) {
  2049. _this.getChild($div, data);
  2050. } else {
  2051. var parseData = _this.parseData(data);
  2052. if(parseData.treeId()){
  2053. var level = parseInt($div.parent("li").attr("data-index"))+1;
  2054. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.fmtTitle(), parseData.last(0), parseData.ficonClass(), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(), parseData.disabled(), parseData.hide(), parseData.basicData(), parseData.recordData(), "item"));
  2055. // 建造完毕后,选中该DIV
  2056. $thisDiv = $ul.find("div[data-id='"+parseData.treeId()+"']");
  2057. _this.setNodeParam($thisDiv);
  2058. _this.showLine($ul.find("li"));
  2059. } else {
  2060. layer.msg("添加失败,节点ID为undefined!",{icon:5});
  2061. // 重新赋值
  2062. _this.setNodeParam($div);
  2063. }
  2064. }
  2065. } else {
  2066. _this.getChild($div);
  2067. }
  2068. }
  2069. // 局部刷新树--编辑当前节点选中节点时
  2070. DTree.prototype.partialRefreshEdit = function($div, data){
  2071. var _this = this;
  2072. $ul = $div.next("ul");
  2073. if(data) {
  2074. if(typeof data === 'object') {
  2075. var parseData = _this.parseData(data);
  2076. if(parseData.treeId()){
  2077. var replaceDom = _this.replaceDom($div, parseData.treeId(), parseData.last(0), parseData.spread(), parseData.disabled(), parseData.hide());
  2078. replaceDom.node(parseData.iconClass());
  2079. replaceDom.checkbox(parseData.checkArr());
  2080. replaceDom.text(parseData.title());
  2081. replaceDom.ul();
  2082. replaceDom.basicData(parseData.basicData());
  2083. replaceDom.recordData(parseData.recordData());
  2084. _this.setNodeParam($div);
  2085. } else {
  2086. layer.msg("编辑失败,节点ID为undefined!",{icon:5});
  2087. // 重新赋值
  2088. _this.setNodeParam($div);
  2089. }
  2090. } else {
  2091. _this.getNodeDom($div).cite().html(data);
  2092. }
  2093. }
  2094. }
  2095. // 局部刷新树--当前节点选中被删除时
  2096. DTree.prototype.partialRefreshDel = function($div){
  2097. var _this = this;
  2098. $p_li = $div.parent("li");
  2099. $p_ul = _this.getNodeDom($div).parentUl();
  2100. $p_div = _this.getNodeDom($div).parentDiv();
  2101. $p_li.remove();
  2102. _this.showLine($p_ul.find("li"));
  2103. // 判断父级ul中是否还存在li,如果不存在,则需要修改节点的样式
  2104. if($p_ul.children("li").length == 0){
  2105. var $icon_i = $p_div.find("i[data-spread]");
  2106. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).closeWithLeaf();
  2107. }
  2108. _this.initNodeParam();
  2109. }
  2110. /******************** 复选框区域 ********************/
  2111. // 初始化复选框的值
  2112. DTree.prototype.chooseDataInit = function(chooseIds){
  2113. var _this = this;
  2114. var chooseId = chooseIds.split(",");
  2115. for (var i=0; i<chooseId.length; i++) {
  2116. _this.obj.find("i[dtree-click='"+eventName.checkNodeClick+"']").each(function(){
  2117. if ($(this).attr("data-id") == chooseId[i]) {
  2118. _this.checkStatus($(this)).check();
  2119. }
  2120. });
  2121. }
  2122. // 展开选中节点的父节点
  2123. var $li_parents = _this.obj.find("i[dtree-click='"+eventName.checkNodeClick+"'][data-checked='1']").parents("."+LI_NAV_ITEM);
  2124. $li_parents.children("ul").addClass(NAV_SHOW);
  2125. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.fnode.node.close)).addClass(_this.usefontStyle.fnode.node.open);
  2126. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.fnode.node.close)).removeClass(_this.usefontStyle.fnode.node.close);
  2127. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.snode.node.close)).addClass(_this.usefontStyle.snode.node.open);
  2128. $li_parents.children("."+LI_DIV_ITEM).children("i[data-spread]."+event.trimToDot(_this.usefontStyle.snode.node.close)).removeClass(_this.usefontStyle.snode.node.close);
  2129. return _this.getCheckbarNodesParam();
  2130. };
  2131. //实现复选框点击,子集选中父级也选中
  2132. DTree.prototype.checkAllOrNot = function($i) {
  2133. var _this = this;
  2134. //$i 当前点击的checkbox
  2135. var dataPar = $i.attr("data-par"),
  2136. dataType = $i.attr("data-type"),
  2137. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2138. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2139. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2140. if ($i.attr("data-checked") == "1") {
  2141. // 处理当前节点的选中状态
  2142. _this.checkStatus($i).noCheck();
  2143. // 处理子级节点的选中状态
  2144. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2145. _this.checkStatus($child_li_i).noCheck();
  2146. // 处理父级节点的选中状态
  2147. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2148. var flag = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"'][data-checked='1']").length;
  2149. if (flag == 0) {
  2150. //把父级去掉选中
  2151. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2152. _this.checkStatus($item_i).noCheck();
  2153. }
  2154. }
  2155. } else {
  2156. // 处理当前节点的选中状态
  2157. _this.checkStatus($i).check();
  2158. // 处理子级节点的选中状态
  2159. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2160. _this.checkStatus($child_li_i).check();
  2161. // 处理父级节点的选中状态
  2162. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2163. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2164. //把父级选中
  2165. _this.checkStatus($item_i).check();
  2166. }
  2167. }
  2168. };
  2169. //实现复选框点击, no-all 子集选中父级半选中,子集全选父级选中
  2170. DTree.prototype.checkAllOrNoallOrNot = function($i) {
  2171. var _this = this;
  2172. //$i 当前点击的checkbox
  2173. var $div = $i.closest("."+LI_DIV_ITEM),
  2174. dataPar = $i.attr("data-par"),
  2175. dataType = $i.attr("data-type"),
  2176. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2177. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2178. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2179. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  2180. // 处理当前节点的选中状态
  2181. _this.checkStatus($i).noCheck();
  2182. // 处理子级节点的选中状态
  2183. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2184. _this.checkStatus($child_li_i).noCheck();
  2185. // 处理父级节点的选中状态
  2186. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2187. var flag = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"'][data-checked='1']").length;
  2188. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2189. if (flag == 0) {
  2190. //把父级去掉选中
  2191. _this.checkStatus($item_i).noCheck();
  2192. } else {
  2193. //把父级半选
  2194. _this.checkStatus($item_i).noallCheck();
  2195. }
  2196. }
  2197. } else { //当前复选框为未选中状态,点击后变为选中状态
  2198. // 处理当前节点的选中状态
  2199. _this.checkStatus($i).check();
  2200. // 处理子级节点的选中状态
  2201. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2202. _this.checkStatus($child_li_i).check();
  2203. // 处理父级节点的选中状态
  2204. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2205. var flag1 = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"'][data-checked='1']").length;
  2206. var flag2 = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']").length;
  2207. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2208. if (flag1 != flag2) {
  2209. // 父级复选框半选
  2210. _this.checkStatus($item_i).noallCheck();
  2211. } else {
  2212. // 父级复选框全选
  2213. _this.checkStatus($item_i).check();
  2214. }
  2215. }
  2216. }
  2217. };
  2218. //实现复选框点击,p-casc:父级选中子集全选,子集无法改变父级选中状态
  2219. DTree.prototype.checkAllOrPcascOrNot = function($i) {
  2220. var _this = this;
  2221. //$i 当前点击的checkbox
  2222. var $div = $i.closest("."+LI_DIV_ITEM),
  2223. dataPar = $i.attr("data-par"),
  2224. dataType = $i.attr("data-type"),
  2225. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2226. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2227. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2228. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  2229. // 处理当前节点的选中状态
  2230. _this.checkStatus($i).noCheck();
  2231. // 处理子级节点的选中状态
  2232. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2233. _this.checkStatus($child_li_i).noCheck();
  2234. } else { //当前复选框为未选中状态,点击后变为选中状态
  2235. // 处理当前节点的选中状态
  2236. _this.checkStatus($i).check();
  2237. // 处理子级节点的选中状态
  2238. var $child_li_i = $child_li.find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2239. _this.checkStatus($child_li_i).check();
  2240. }
  2241. };
  2242. //实现复选框点击,self:各自选中互不影响
  2243. DTree.prototype.checkOrNot = function($i) {
  2244. var _this = this;
  2245. //$i 当前点击的checkbox
  2246. var $div = $i.closest("."+LI_DIV_ITEM),
  2247. dataPar = $i.attr("data-par"),
  2248. dataType = $i.attr("data-type"),
  2249. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2250. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2251. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2252. if ($i.attr("data-checked") == "1") { //当前复选框为选中状态,点击后变为未选中状态
  2253. // 处理当前节点的选中状态
  2254. _this.checkStatus($i).noCheck();
  2255. } else { //当前复选框为未选中状态,点击后变为选中状态
  2256. // 处理当前节点的选中状态
  2257. _this.checkStatus($i).check();
  2258. }
  2259. };
  2260. //实现复选框点击,only:只能选中1个复选框
  2261. DTree.prototype.checkOnly = function($i) {
  2262. var _this = this;
  2263. //$i 当前点击的checkbox
  2264. var $div = $i.closest("."+LI_DIV_ITEM),
  2265. dataPar = $i.attr("data-par"),
  2266. dataType = $i.attr("data-type"),
  2267. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2268. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2269. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2270. var checked = $i.attr("data-checked");
  2271. // 将全部节点全部设为未选中状态
  2272. var $all_i = _this.obj.find("i[data-checked]");
  2273. _this.checkStatus($all_i).noCheck();
  2274. if (checked != "1") { //当前复选框为未选中状态,点击后变为选中状态
  2275. // 处理当前节点的选中状态
  2276. _this.checkStatus($i).check();
  2277. }
  2278. };
  2279. //实现复选框点击
  2280. DTree.prototype.changeCheck = function($i) {
  2281. var _this = this;
  2282. var temp = _this.temp;
  2283. if(typeof $i === 'undefined') {
  2284. $i = temp[0];
  2285. }
  2286. // 复选框选中事件
  2287. if (_this.checkbarType == "all") {
  2288. _this.checkAllOrNot($i);
  2289. } else if(_this.checkbarType == "no-all") {
  2290. _this.checkAllOrNoallOrNot($i);
  2291. } else if(_this.checkbarType == "p-casc") {
  2292. _this.checkAllOrPcascOrNot($i);
  2293. } else if(_this.checkbarType == "self") {
  2294. _this.checkOrNot($i);
  2295. } else if(_this.checkbarType == "only") {
  2296. _this.checkOnly($i);
  2297. } else {
  2298. _this.checkAllOrNot($i);
  2299. }
  2300. // 获取复选框选中节点的内容
  2301. var checkbarNodes = _this.setAndGetCheckbarNodesParam();
  2302. // 用户自定义想做的事情
  2303. _this.checkbarFun.chooseDone(checkbarNodes);
  2304. layui.event.call(this, MOD_NAME, "chooseDone("+$(_this.obj)[0].id+")", {"checkbarParams": checkbarNodes});
  2305. _this.temp = [];
  2306. };
  2307. //复选框半选状态初始化设置
  2308. DTree.prototype.initNoAllCheck = function(){
  2309. var _this = this;
  2310. //1.获取所有选中节点
  2311. var $is = _this.obj.find("i[data-checked='1']");
  2312. if($is.length > 0){
  2313. for ( var key = 0; key < $is.length; key++) {
  2314. var $i = $($is[key]),
  2315. dataPar = $i.attr("data-par"),
  2316. dataType = $i.attr("data-type"),
  2317. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2318. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2319. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2320. // 处理父级节点的选中状态
  2321. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2322. var flag1 = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"'][data-checked='1']").length;
  2323. var flag2 = item.eq(i).find(">."+LI_NAV_CHILD+" ."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']").length;
  2324. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2325. if (flag1 != flag2) {
  2326. // 父级复选框半选
  2327. _this.checkStatus($item_i).noallCheck();
  2328. } else {
  2329. // 父级复选框全选
  2330. _this.checkStatus($item_i).check();
  2331. }
  2332. }
  2333. }
  2334. }
  2335. };
  2336. //复选框选中状态初始化设置
  2337. DTree.prototype.initAllCheck = function(){
  2338. var _this = this;
  2339. //1.获取所有选中节点
  2340. var $is = _this.obj.find("i[data-checked='1']");
  2341. if($is.length > 0){
  2342. for ( var key = 0; key < $is.length; key++) {
  2343. var $i = $($is[key]),
  2344. dataPar = $i.attr("data-par"),
  2345. dataType = $i.attr("data-type"),
  2346. $li = $i.closest(dataPar), //当前checkbox的上级li节点
  2347. $parent_li = $i.parents(dataPar), //当前checkbox的所有父级li节点
  2348. $child_li = $li.find(dataPar); //当前checkbox的上级li节点下的所有子级li节点
  2349. // 处理父级节点的选中状态
  2350. for (var i = 1, item = $parent_li; i < item.length; i++) {
  2351. var $item_i = item.eq(i).find(">."+LI_DIV_ITEM+">."+LI_DIV_CHECKBAR+">i[data-type='"+dataType+"']");
  2352. // 父级复选框全选
  2353. _this.checkStatus($item_i).check();
  2354. }
  2355. }
  2356. }
  2357. };
  2358. // 设置复选框选中/未选中/半选 _this.checkStatus($i).check(); _this.checkStatus($i).noCheck(); _this.checkStatus($i).noallCheck();
  2359. DTree.prototype.checkStatus = function($i) {
  2360. var _this = this;
  2361. return {
  2362. check: function(){
  2363. $i.removeClass(_this.usefontStyle.checkbox.out);
  2364. $i.removeClass(_this.usefontStyle.checkbox.noall);
  2365. $i.addClass(_this.usefontStyle.checkbox.on);
  2366. $i.addClass(_this.style.chs);
  2367. $i.attr("data-checked","1");
  2368. },
  2369. noCheck: function(){
  2370. $i.removeClass(_this.usefontStyle.checkbox.noall);
  2371. $i.removeClass(_this.usefontStyle.checkbox.on);
  2372. $i.removeClass(_this.style.chs);
  2373. $i.addClass(_this.usefontStyle.checkbox.out);
  2374. $i.attr("data-checked","0");
  2375. },
  2376. noallCheck: function(){
  2377. $i.removeClass(_this.usefontStyle.checkbox.out);
  2378. $i.removeClass(_this.usefontStyle.checkbox.on);
  2379. $i.addClass(_this.usefontStyle.checkbox.noall);
  2380. $i.addClass(_this.style.chs);
  2381. $i.attr("data-checked","2");
  2382. }
  2383. }
  2384. };
  2385. // 设置树的复选框操作值的全部参数,并获取
  2386. DTree.prototype.setAndGetCheckbarNodesParam = function() {
  2387. var _this = this;
  2388. //操作前先清空
  2389. _this.checkbarNode = [];
  2390. // 选择所有复选框节点
  2391. if (_this.checkbarData == "change"){ //记录变更数据
  2392. _this.obj.find("i[data-par][dtree-disabled='false']").each(function(){
  2393. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2394. if ($i.attr("data-checked") != $i.attr("data-initchecked")) {
  2395. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2396. }
  2397. });
  2398. } else if (_this.checkbarData == "all"){ //记录全部数据
  2399. _this.obj.find("i[data-par][data-checked][dtree-disabled='false']").each(function(){
  2400. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2401. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2402. });
  2403. } else if (_this.checkbarData == "choose"){ //记录选中数据
  2404. _this.obj.find("i[data-par][data-checked='1'][dtree-disabled='false']").each(function(){
  2405. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2406. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2407. });
  2408. } else if (_this.checkbarData == "halfChoose"){ //记录选中和半选数据
  2409. _this.obj.find("i[data-par][data-checked='1'][dtree-disabled='false']").each(function(){
  2410. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2411. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2412. });
  2413. _this.obj.find("i[data-par][data-checked='2'][dtree-disabled='false']").each(function(){
  2414. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2415. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2416. });
  2417. }
  2418. return _this.checkbarNode;
  2419. };
  2420. // 获取树的复选框操作值的全部参数
  2421. DTree.prototype.getCheckbarNodesParam = function() {
  2422. var _this = this;
  2423. return _this.setAndGetCheckbarNodesParam();
  2424. };
  2425. // 获取树的一个复选框的参数
  2426. DTree.prototype.getCheckbarNodeParam = function($div, $i){
  2427. var _this = this;
  2428. var temp_node = {};
  2429. temp_node.nodeId = $div.attr("data-id");
  2430. temp_node.parentId = _this.getNodeDom($div).parentLi().attr("data-pid");
  2431. temp_node.context = (typeof _this.formatter.title === 'function') ? _this.getNodeDom($div).cite().attr("data-title") : _this.getNodeDom($div).cite().text();
  2432. temp_node.leaf = _this.getNodeDom($div).cite().attr("data-leaf") == "leaf" ? true : false;
  2433. temp_node.level = _this.getNodeDom($div).parentLi().attr("data-index");
  2434. temp_node.spread = _this.getNodeDom($div).fnode().attr("data-spread") == "open" ? true : false;
  2435. temp_node.basicData = $div.attr("data-basic")
  2436. temp_node.recordData = $div.attr("data-record");
  2437. temp_node.dataType = $i.attr("data-type");
  2438. temp_node.checked = $i.attr("data-checked");
  2439. temp_node.initchecked = $i.attr("data-initchecked");
  2440. return temp_node;
  2441. };
  2442. //判断复选框是否发生变更
  2443. DTree.prototype.changeCheckbarNodes = function(){
  2444. var flag = false;
  2445. var _this = this;
  2446. _this.obj.find("i[data-par]").each(function(){
  2447. var $i = $(this);
  2448. $div = $i.closest("."+LI_DIV_ITEM);
  2449. if ($i.attr("data-checked") != $i.attr("data-initchecked")) {
  2450. flag = true;
  2451. return true;
  2452. }
  2453. });
  2454. return flag;
  2455. };
  2456. /******************** 工具栏及菜单栏区域 ********************/
  2457. // 初始化菜单栏和工具栏的div
  2458. DTree.prototype.initTreePlus = function(){
  2459. var _this = this;
  2460. // 初始化菜单栏和工具栏的div
  2461. _this.obj.prevAll('div#dtree_menubar_'+_this.obj[0].id).remove();
  2462. _this.toolbarMenu = {};
  2463. 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>");
  2464. if(_this.toolbar){
  2465. if(_this.toolbarWay == "contextmenu") {
  2466. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).remove();
  2467. _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>");
  2468. }
  2469. }
  2470. };
  2471. // 开启工具栏和菜单栏
  2472. DTree.prototype.openTreePlus = function(){
  2473. var _this = this;
  2474. // 先对工具栏做处理,因为菜单栏可能会与工具栏产生关联。
  2475. var ggMenu = [];
  2476. if(_this.toolbar) _this.getToolbarDom();
  2477. if(_this.menubar) {
  2478. var menubarTips = _this.menubarTips,
  2479. mtbar = menubarTips.toolbar,
  2480. group = menubarTips.group,
  2481. freedom = menubarTips.freedom;
  2482. if(mtbar && mtbar.length > 0){
  2483. // 菜单栏吸附工具栏上
  2484. for(var i=0; i<mtbar.length; i++){
  2485. var mt = mtbar[i];
  2486. if(typeof mt === 'string'){
  2487. _this.getMenubarToolDom(mt);
  2488. }
  2489. if(typeof mt === 'object'){
  2490. _this.getExtMenubarToolDom(mt);
  2491. }
  2492. }
  2493. }
  2494. if(group && group.length > 0){
  2495. // 菜单栏吸附在上方的按钮组div中
  2496. for(var i=0; i<group.length; i++){
  2497. var gg = group[i];
  2498. if(typeof gg === 'string'){
  2499. ggMenu.push(_this.getMenubarDom(gg));
  2500. }
  2501. if(typeof gg === 'object'){
  2502. ggMenu.push(_this.getExtMenubarDom(gg));
  2503. }
  2504. }
  2505. _this.obj.prevAll('div#dtree_menubar_'+_this.obj[0].id).children('div.layui-btn-group').append(ggMenu.join(""));
  2506. }
  2507. }
  2508. };
  2509. /******************** 菜单栏区域 ********************/
  2510. // 获取菜单栏
  2511. DTree.prototype.getMenubarDom = function(menu){
  2512. var _this = this;
  2513. var rootId = _this.obj[0].id;
  2514. var gg = "";
  2515. switch (menu) {
  2516. case defaultMenu.moveDown:
  2517. gg = "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.moveDown+"' title='展开全部节点'><i class='"+_this.usefontStyle.menubar.movedown+"'></i></button>";
  2518. break;
  2519. case defaultMenu.moveUp:
  2520. gg = "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.moveUp+"' title='收缩全部节点'><i class='"+_this.usefontStyle.menubar.moveup+"'></i></button>";
  2521. break;
  2522. case defaultMenu.refresh:
  2523. gg = "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.refresh+"' title='刷新'><i class='"+_this.usefontStyle.menubar.refresh+"'></i></button>";
  2524. break;
  2525. case defaultMenu.checkAll:
  2526. gg = (_this.checkbar && _this.checkbarType != 'only') ? "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.checkAll+"' title='全选节点'><i class='"+_this.usefontStyle.menubar.checkAll+"'></i></button>" : "";
  2527. break;
  2528. case defaultMenu.unCheckAll:
  2529. gg = (_this.checkbar && _this.checkbarType != 'only') ? "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.unCheckAll+"' title='全不选节点'><i class='"+_this.usefontStyle.menubar.unCheckAll+"'></i></button>" : "";
  2530. break;
  2531. case defaultMenu.invertAll:
  2532. gg = (_this.checkbar && _this.checkbarType != 'only') ? "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.invertAll+"' title='反选节点'><i class='"+_this.usefontStyle.menubar.invertAll+"'></i></button>" : "";
  2533. break;
  2534. case defaultMenu.remove:
  2535. gg = (_this.checkbar) ? "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.remove+"' title='删除选中节点'><i class='"+_this.usefontStyle.menubar.remove+"'></i></button>" : "";
  2536. break;
  2537. case defaultMenu.searchNode:
  2538. gg = "<button type='button' class='layui-btn layui-btn-sm layui-btn-primary' dtree-id='"+rootId+"' d-menu='"+defaultMenu.searchNode+"' title='查询节点'><i class='"+_this.usefontStyle.menubar.search+"'></i></button>";
  2539. break;
  2540. }
  2541. return gg;
  2542. };
  2543. // 获取扩展菜单栏
  2544. DTree.prototype.getExtMenubarDom = function(menu){
  2545. var _this = this;
  2546. return "<button type='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='"+_this.usefontStyle.menubarExt+" "+menu.icon+"'></i></button>";
  2547. };
  2548. // 获取依附在工具栏的菜单栏
  2549. DTree.prototype.getMenubarToolDom = function(menu){
  2550. var _this = this;
  2551. var rootId = _this.obj[0].id;
  2552. switch (menu) {
  2553. case defaultMenu.moveDown:
  2554. _this.toolbarMenu[defaultMenu.moveDown] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.moveDown, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.movedown, "展开全部");
  2555. break;
  2556. case defaultMenu.moveUp:
  2557. _this.toolbarMenu[defaultMenu.moveUp] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.moveUp, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.moveup, "收缩全部");
  2558. break;
  2559. case defaultMenu.refresh:
  2560. _this.toolbarMenu[defaultMenu.refresh] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.refresh, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.refresh, "刷新");
  2561. break;
  2562. case defaultMenu.checkAll:
  2563. if(_this.checkbar && _this.checkbarType != 'only')
  2564. _this.toolbarMenu[defaultMenu.checkAll] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.checkAll, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.checkAll, "全选节点");
  2565. break;
  2566. case defaultMenu.unCheckAll:
  2567. if(_this.checkbar && _this.checkbarType != 'only')
  2568. _this.toolbarMenu[defaultMenu.unCheckAll] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.unCheckAll, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.unCheckAll, "全不选节点");
  2569. break;
  2570. case defaultMenu.invertAll:
  2571. if(_this.checkbar && _this.checkbarType != 'only')
  2572. _this.toolbarMenu[defaultMenu.invertAll] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.invertAll, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.invertAll, "反选节点");
  2573. break;
  2574. case defaultMenu.remove:
  2575. if(_this.checkbar)
  2576. _this.toolbarMenu[defaultMenu.remove] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.remove, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.remove, "删除选中");
  2577. break;
  2578. case defaultMenu.searchNode:
  2579. _this.toolbarMenu[defaultMenu.searchNode] = _this.setToolbarDom().setMenuToolbarOption(defaultMenu.searchNode, _this.toolbarStyle.title, _this.usefontStyle.toolbar.menubar.searchNode, "查询");
  2580. break;
  2581. }
  2582. };
  2583. // 获取依附在工具栏的扩展菜单栏
  2584. DTree.prototype.getExtMenubarToolDom = function(menu){
  2585. var _this = this;
  2586. _this.toolbarMenu[menu.menubarId] = _this.setToolbarDom().setMenuToolbarOption(menu.menubarId, menu.title, _this.usefontStyle.toolbar.menubarExt+" "+menu.icon, "");
  2587. };
  2588. // menubar内置方法
  2589. DTree.prototype.menubarMethod = function(){
  2590. var _this = this;
  2591. return {
  2592. openAllNode: function(obj){ // 展开所有节点
  2593. var $ulNode = obj || _this.obj.children("li").children("ul");
  2594. // 遍历所有ul子节点
  2595. for (var i = 0; i < $ulNode.length; i++) {
  2596. // 获取当前节点的信息
  2597. var $ul = $($ulNode[i]),
  2598. $div = $ul.prev("div"),
  2599. $i_spread = _this.getNodeDom($div).fnode(),
  2600. $i_node = _this.getNodeDom($div).snode(),
  2601. $cite = _this.getNodeDom($div).cite(),
  2602. spread = $i_spread.attr("data-spread"),
  2603. leaf = $cite.attr("data-leaf");
  2604. if (leaf == "leaf") { continue; } // 说明是叶子了,则继续循环下一个
  2605. if (spread == "open") {
  2606. // 说明该节点已经展开了,则进行子节点循环
  2607. } else {
  2608. if (_this.type=="load") { //是否全量加载
  2609. if (_this.cache) { //是否开启缓存
  2610. if ($ul.html()) {
  2611. $ul.addClass(NAV_SHOW);
  2612. } else { //加载节点
  2613. _this.getChild($div);
  2614. }
  2615. }else { //每次取新的数据
  2616. $ul.html("");
  2617. _this.getChild($div);
  2618. }
  2619. } else { // 全量加载
  2620. $ul.addClass(NAV_SHOW);
  2621. }
  2622. _this.operateIcon($i_spread, $i_node).open();
  2623. }
  2624. var $childUl = $ul.children("li").children("ul");
  2625. _this.menubarMethod().openAllNode($childUl);
  2626. }
  2627. },
  2628. closeAllNode: function(){ //收缩所有节点
  2629. _this.obj.find("."+LI_NAV_CHILD).each(function(){
  2630. // 获取当前节点的信息
  2631. var $ul = $(this),
  2632. $div = $ul.prev("div"),
  2633. $i_spread = _this.getNodeDom($div).fnode(),
  2634. $i_node = _this.getNodeDom($div).snode(),
  2635. $cite = _this.getNodeDom($div).cite(),
  2636. spread = $i_spread.attr("data-spread"),
  2637. leaf = $cite.attr("data-leaf");
  2638. $ul.removeClass(NAV_SHOW);
  2639. _this.operateIcon($i_spread, $i_node).close();
  2640. });
  2641. },
  2642. refreshTree: function(){// 刷新树
  2643. _this.refreshTree();
  2644. },
  2645. checkAll: function(){ // 全选节点
  2646. var $i = _this.obj.find("i[data-par][data-checked!='1']");
  2647. if($i.length > 0) { _this.checkStatus($i).check(); }
  2648. },
  2649. unCheckAll: function(){ // 全不选节点
  2650. var $i = _this.obj.find("i[data-par][data-checked!='0']");
  2651. if($i.length > 0) { _this.checkStatus($i).noCheck(); }
  2652. },
  2653. invertAll: function(){ // 反选节点
  2654. if(_this.obj.find("i[data-par]").length > 0) {
  2655. var b = false;
  2656. _this.obj.find("i[data-par]").each(function(){
  2657. var $i = $(this);
  2658. if($i.attr("data-checked") == '2'){
  2659. b = true;
  2660. }else if($i.attr("data-checked") == '0') {
  2661. _this.checkStatus($i).check();
  2662. }else if($i.attr("data-checked") == '1') {
  2663. _this.checkStatus($i).noCheck();
  2664. }
  2665. });
  2666. if(b) {
  2667. _this.initNoAllCheck();
  2668. } else {
  2669. _this.initAllCheck();
  2670. }
  2671. }
  2672. },
  2673. remove: function(){// 删除选中节点
  2674. var len = _this.obj.find("i[data-par][data-checked='1']").length;
  2675. if(len == 0){
  2676. layer.msg("请至少选中一个节点",{icon:2});
  2677. }else{
  2678. //操作前先清空
  2679. _this.checkbarNode = [];
  2680. // 选择所有复选框节点
  2681. var i_node = {};
  2682. _this.obj.find("i[data-par][data-checked='1']").each(function(){
  2683. var $i = $(this), $div = $i.closest("."+LI_DIV_ITEM);
  2684. _this.checkbarNode.push(_this.getRequestParam(_this.getCheckbarNodeParam($div, $i)));
  2685. });
  2686. layer.confirm('确定要删除选中节点?', {icon: 3, title:'删除选中节点'}, function(index1){
  2687. var flag = _this.menubarFun.remove(_this.checkbarNode);
  2688. if(flag){
  2689. _this.obj.find("i[data-par][data-checked='1']").closest("."+LI_DIV_ITEM).next("ul").remove();
  2690. _this.obj.find("i[data-par][data-checked='1']").closest("."+LI_DIV_ITEM).remove();
  2691. _this.checkbarNode=[];
  2692. }
  2693. layer.close(index1);
  2694. });
  2695. }
  2696. },
  2697. searchNode: function(){//模糊查询该值,展开该值节点
  2698. layer.prompt({
  2699. formType: 0,
  2700. value: "",
  2701. title: '查询节点'
  2702. }, function(value, index1, elem){
  2703. if (value) {
  2704. var flag = _this.searchNode(value);
  2705. if (!flag) {
  2706. layer.msg("该名称节点不存在!", {icon:5});
  2707. }
  2708. } else {
  2709. layer.msg("未指定查询节点名称", {icon:5});
  2710. }
  2711. layer.close(index1);
  2712. });
  2713. },
  2714. extMethod: function(menuId, $div, flag){
  2715. if(_this.menubar && _this.menubarTips.group && _this.menubarTips.group.length > 0 && flag == "group"){
  2716. for(var i=0; i<_this.menubarTips.group.length; i++){
  2717. var ext = _this.menubarTips.group[i];
  2718. if (menuId == ext.menubarId){
  2719. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  2720. break;
  2721. }
  2722. }
  2723. }
  2724. if(_this.menubar && _this.menubarTips.toolbar && _this.menubarTips.toolbar.length > 0 && flag == "toolbar"){
  2725. for(var i=0; i<_this.menubarTips.toolbar.length; i++){
  2726. var ext = _this.menubarTips.toolbar[i];
  2727. if (menuId == ext.menubarId){
  2728. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  2729. break;
  2730. }
  2731. }
  2732. }
  2733. if(_this.menubar && _this.menubarTips.freedom && _this.menubarTips.freedom.length > 0 && flag == "freedom"){
  2734. for(var i=0; i<_this.menubarTips.freedom.length; i++){
  2735. var ext = _this.menubarTips.freedom[i];
  2736. if (menuId == ext.menubarId){
  2737. ext.handler(_this.getRequestParam(_this.getNodeParam($div), $div));
  2738. break;
  2739. }
  2740. }
  2741. }
  2742. }
  2743. };
  2744. };
  2745. // menubar监听方法
  2746. DTree.prototype.menubarListener = function(menuId, flag){
  2747. var _this = this;
  2748. var $div = _this.getNodeDom().nowDiv();
  2749. switch (menuId) {
  2750. case defaultMenu.moveDown: // 展开全部节点
  2751. _this.menubarMethod().openAllNode();
  2752. break;
  2753. case defaultMenu.moveUp: // 收缩全部节点
  2754. _this.menubarMethod().closeAllNode();
  2755. break;
  2756. case defaultMenu.refresh:
  2757. _this.menubarMethod().refreshTree(); // 刷新树
  2758. break;
  2759. case defaultMenu.checkAll:
  2760. _this.menubarMethod().checkAll();
  2761. break;
  2762. case defaultMenu.unCheckAll:
  2763. _this.menubarMethod().unCheckAll();
  2764. break;
  2765. case defaultMenu.invertAll:
  2766. _this.menubarMethod().invertAll();
  2767. break;
  2768. case defaultMenu.remove:
  2769. _this.menubarMethod().remove();
  2770. break;
  2771. case defaultMenu.searchNode:
  2772. _this.menubarMethod().searchNode();
  2773. break;
  2774. default:
  2775. _this.menubarMethod().extMethod(menuId, $div, flag);
  2776. break;
  2777. }
  2778. };
  2779. //模糊查询该值,展开该值节点
  2780. DTree.prototype.searchNode = function(value){
  2781. var _this = this;
  2782. var b = false;
  2783. var $lis = [];
  2784. _this.obj.find("cite[data-leaf]").each(function(){
  2785. var $nthis = $(this);
  2786. var html = $nthis.html();
  2787. if(html.indexOf(value) > -1){
  2788. if($nthis.attr("data-leaf") == "leaf") {
  2789. // 叶子节点提供包含父节点的所有信息
  2790. var title = "";
  2791. $nthis.parents("li").each(function(){
  2792. title = "-" + $(this).find("cite[data-leaf]").html() + title;
  2793. });
  2794. title = title.substring(1, title.length);
  2795. $nthis.attr("title", title);
  2796. }
  2797. // 保存当前cite所在的li及父li中包含该值,则只保留父的
  2798. var i = 0;
  2799. $nthis.parents("li").each(function(){
  2800. var html2 = $(this).find("cite[data-leaf]").html();
  2801. if(html2.indexOf(value) > -1){
  2802. i++;
  2803. }
  2804. if(i >= 2){
  2805. return true;
  2806. }
  2807. });
  2808. if (i < 2){
  2809. $lis.push($nthis.closest("li").prop("outerHTML"));
  2810. }
  2811. }
  2812. });
  2813. if($lis.length > 0) {
  2814. b = true;
  2815. // 1.将树节点清空
  2816. _this.obj.html("");
  2817. // 2.遍历所有cite节点,展开当前cite节点
  2818. for(var i=0; i<$lis.length; i++){
  2819. _this.obj.append($lis[i]);
  2820. }
  2821. }
  2822. return b;
  2823. };
  2824. /******************** 工具栏区域 ********************/
  2825. // 获取工具栏
  2826. DTree.prototype.getToolbarDom = function(){
  2827. var _this = this;
  2828. var toolbarShow = _this.toolbarShow,
  2829. toolbarExt = _this.toolbarExt,
  2830. toolbarWay = _this.toolbarWay;
  2831. if(toolbarShow.length > 0){
  2832. for(var i=0; i<toolbarShow.length; i++){
  2833. var show = toolbarShow[i];
  2834. if(show == "pulldown"){
  2835. _this.toolbarMenu[defaultTool.pulldown] = _this.setToolbarDom().setToolbarOption(defaultTool.pulldown, _this.toolbarStyle.title, _this.usefontStyle.toolbar.pulldown, "展开");
  2836. }
  2837. if(show == "pullup"){
  2838. _this.toolbarMenu[defaultTool.pullup] = _this.setToolbarDom().setToolbarOption(defaultTool.pullup, _this.toolbarStyle.title, _this.usefontStyle.toolbar.pullup, "收缩");
  2839. }
  2840. if(show == "add"){
  2841. _this.toolbarMenu[defaultTool.addTool] = _this.setToolbarDom().setToolbarOption(defaultTool.addTool, _this.toolbarStyle.title, _this.usefontStyle.toolbar.add, "新增");
  2842. }
  2843. if(show == "edit"){
  2844. _this.toolbarMenu[defaultTool.editTool] = _this.setToolbarDom().setToolbarOption(defaultTool.editTool, _this.toolbarStyle.title, _this.usefontStyle.toolbar.edit, "编辑");
  2845. }
  2846. if(show == "delete"){
  2847. _this.toolbarMenu[defaultTool.delTool] = _this.setToolbarDom().setToolbarOption(defaultTool.delTool, _this.toolbarStyle.title, _this.usefontStyle.toolbar.del, "删除");
  2848. }
  2849. }
  2850. }
  2851. if(toolbarExt.length > 0){
  2852. for(var i=0; i<toolbarExt.length; i++){
  2853. var ext = toolbarExt[i];
  2854. _this.toolbarMenu[ext.toolbarId] = _this.setToolbarDom().setToolbarOption(ext.toolbarId, ext.title, _this.usefontStyle.toolbarExt+" "+ext.icon, "");
  2855. }
  2856. }
  2857. };
  2858. // 设置工具栏按钮
  2859. DTree.prototype.setToolbarDom = function(){
  2860. var _this = this;
  2861. var toolbarWay = _this.toolbarWay;
  2862. return {
  2863. setToolbarOption: function(toolbarId, title, classId, other){
  2864. if(toolbarWay == "contextmenu") {
  2865. return "<dd><a dtree-tool='"+toolbarId+"'><i class='"+classId+"'></i>&nbsp;"+other +title+"</a></dd>";
  2866. } else if(toolbarWay == "fixed" || toolbarWay == "follow") {
  2867. return "<a dtree-tool='"+toolbarId+"' title='"+other + title+"'><i class='"+classId+"'></i></a>";
  2868. }
  2869. },
  2870. setMenuToolbarOption: function(menubarId, title, classId, other){
  2871. var rootId = _this.obj[0].id;
  2872. if(toolbarWay == "contextmenu") {
  2873. return "<dd><a dtree-id='"+rootId+"' d-menu='"+menubarId+"'><i class='"+classId+"'></i>&nbsp;"+other +title+"</a></dd>";
  2874. } else if(toolbarWay == "fixed" || toolbarWay == "follow") {
  2875. return "<a dtree-id='"+rootId+"' d-menu='"+menubarId+"' title='"+other + title+"'><i class='"+classId+"'></i></a>";
  2876. }
  2877. },
  2878. setToolbarPlace: function(toolbarMenu){
  2879. if(toolbarWay == "contextmenu") {
  2880. if(toolbarMenu){
  2881. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).find('div.layui-nav-item>dl.layui-nav-child').html("");
  2882. for(var key in toolbarMenu){
  2883. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).find('div.layui-nav-item>dl.layui-nav-child').append(toolbarMenu[key]);
  2884. }
  2885. }
  2886. } else if(toolbarWay == "fixed" || toolbarWay == "follow") {
  2887. _this.obj.find("cite[data-leaf][dtree-disabled='false']").each(function(){
  2888. var $cite = $(this);
  2889. _this.dynamicToolbarDom($cite);
  2890. });
  2891. }
  2892. }
  2893. }
  2894. };
  2895. // 在节点后动态绑定fixed和follow条件的工具栏
  2896. DTree.prototype.dynamicToolbarDom = function($cite){
  2897. var _this = this;
  2898. var toolbarWay = _this.toolbarWay;
  2899. if($cite.next("em."+TOOLBAR_TOOL_EM).length == 0) {
  2900. var $div = $cite.parent("div");
  2901. var param = _this.getRequestParam(_this.getTempNodeParam($div));
  2902. var toolbarMenus = _this.toolbarFun.loadToolbarBefore(event.cloneObj(_this.toolbarMenu), param, $div);
  2903. var hideCls = (toolbarWay == "follow") ? NAV_HIDE : "";
  2904. var em = ["<em class='"+TOOLBAR_TOOL_EM+" "+hideCls+"'>"];
  2905. if(toolbarMenus){
  2906. for(var key in toolbarMenus){
  2907. em.push(toolbarMenus[key]);
  2908. }
  2909. }
  2910. em.push("</em>");
  2911. $cite.after(em.join(''));
  2912. }
  2913. }
  2914. // 隐藏toolbar
  2915. DTree.prototype.toolbarHide = function() {
  2916. var _this = this;
  2917. if(_this.toolbar && _this.toolbarWay == "contextmenu") {
  2918. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id);
  2919. $toolBarDiv.find(".layui-nav-child").removeClass('layui-anim-fadein layui-show');
  2920. }
  2921. }
  2922. // toolbar内置方法
  2923. DTree.prototype.toolbarMethod = function(){
  2924. var _this = this;
  2925. return {
  2926. pulldown: function(obj){ // 展开当前点击节点的下面全部节点
  2927. if(!obj) return;
  2928. var $ulNode = obj;
  2929. // 遍历所有ul子节点
  2930. for (var i = 0; i < $ulNode.length; i++) {
  2931. // 获取当前节点的信息
  2932. var $ul = $($ulNode[i]),
  2933. $div = $ul.prev("div"),
  2934. $i_spread = _this.getNodeDom($div).fnode(),
  2935. $i_node = _this.getNodeDom($div).snode(),
  2936. $cite = _this.getNodeDom($div).cite(),
  2937. spread = $i_spread.attr("data-spread"),
  2938. leaf = $cite.attr("data-leaf");
  2939. if (leaf == "leaf") { continue; } // 说明是叶子了,则继续循环下一个
  2940. if (spread == "open") {
  2941. // 说明该节点已经展开了,则进行子节点循环
  2942. } else {
  2943. if (_this.type=="load") { //是否全量加载
  2944. if (_this.cache) { //是否开启缓存
  2945. if ($ul.html()) {
  2946. $ul.addClass(NAV_SHOW);
  2947. } else { //加载节点
  2948. _this.getChild($div);
  2949. }
  2950. }else { //每次取新的数据
  2951. $ul.html("");
  2952. _this.getChild($div);
  2953. }
  2954. } else { // 全量加载
  2955. $ul.addClass(NAV_SHOW);
  2956. }
  2957. _this.operateIcon($i_spread, $i_node).open();
  2958. }
  2959. var $childUl = $ul.children("li").children("ul");
  2960. _this.toolbarMethod().pulldown($childUl);
  2961. }
  2962. },
  2963. pullup: function($li){ // 收缩当前点击节点的下面全部节点
  2964. $li.find("."+LI_NAV_CHILD).each(function(){
  2965. // 获取当前节点的信息
  2966. var $ul = $(this),
  2967. $div = $ul.prev("div"),
  2968. $i_spread = _this.getNodeDom($div).fnode(),
  2969. $i_node = _this.getNodeDom($div).snode(),
  2970. $cite = _this.getNodeDom($div).cite(),
  2971. spread = $i_spread.attr("data-spread"),
  2972. leaf = $cite.attr("data-leaf");
  2973. $ul.removeClass(NAV_SHOW);
  2974. _this.operateIcon($i_spread, $i_node).close();
  2975. });
  2976. }
  2977. }
  2978. };
  2979. // toolbar监听方法
  2980. DTree.prototype.toolbarListener = function(tool, $div) {
  2981. var _this = this;
  2982. var $cite = $div.children("cite[data-leaf]"),
  2983. $ul = $div.next("ul"),
  2984. $p_li = $div.parent("li[data-index]"), //当前选中节点的顶级li节点
  2985. $p_ul = $p_li.parent("ul"), //当前选中节点的顶级li节点的父级ul
  2986. $p_div = $p_ul.prev("div"), //当前选中节点的顶级li节点的父级ul的前一个div
  2987. title = $cite.html();
  2988. switch (tool) {
  2989. case defaultTool.pulldown:
  2990. _this.toolbarMethod().pulldown($ul);
  2991. break;
  2992. case defaultTool.pullup:
  2993. _this.toolbarMethod().pullup($p_li);
  2994. break;
  2995. case defaultTool.addTool:
  2996. var content = _this.loadToolBar(title, defaultTool.addTool);
  2997. layer.open({
  2998. title: "新增"+_this.toolbarStyle.title,
  2999. type: 1,
  3000. area: _this.toolbarStyle.area,
  3001. content: content,
  3002. success: function(layero, index){
  3003. form.render();
  3004. form.on("submit(dtree_addNode_form)",function(data){
  3005. var data = data.field;
  3006. var parentId = $div.attr("data-id"),
  3007. id = $div.attr("data-id")+"_node_"+$ul[0].childNodes.length,
  3008. leaf = true,
  3009. checked = "0",
  3010. level = parseInt($p_li.attr("data-index"))+1;
  3011. // 创建子节点的DOM,添加子节点
  3012. var checkArr = [];
  3013. if (_this.checkArrLen > 0) {
  3014. for (var i = 0; i < _this.checkArrLen; i++) {
  3015. checkArr.push({"type":i,"checked":"0"});
  3016. }
  3017. }
  3018. $ul.append(_this.getLiItemDom(id, parentId, data.addNodeName, data.addNodeName, true, "", "", checkArr, level, false, false, false, "", "", "item"));
  3019. // 先将li节点隐藏
  3020. $ul.find("li[data-id='"+id+"']").hide();
  3021. // 重新赋值
  3022. var $addDiv = $ul.find("div[data-id='"+id+"']");
  3023. node = _this.getNodeParam($addDiv);
  3024. //获取组装后的requestNode,组合参数
  3025. var requestNode = _this.getRequestParam(node);
  3026. requestNode = $.extend(requestNode, data);
  3027. _this.temp = [id, $ul, $div, level];
  3028. // 用户自定义想做的事情
  3029. _this.toolbarFun.addTreeNode(requestNode, $div);
  3030. layer.close(index);
  3031. return false;
  3032. });
  3033. }
  3034. });
  3035. break;
  3036. case defaultTool.editTool:
  3037. var content = _this.loadToolBar(title, defaultTool.editTool);
  3038. layer.open({
  3039. title: "编辑"+_this.toolbarStyle.title,
  3040. type: 1,
  3041. area: _this.toolbarStyle.area,
  3042. content: content,
  3043. success: function(layero, index){
  3044. _this.toolbarFun.editTreeLoad(_this.getRequestParam(_this.getNodeParam($div)));
  3045. form.render();
  3046. form.on("submit(dtree_editNode_form)",function(data){
  3047. var data = data.field;
  3048. $cite.html(data.editNodeName);
  3049. node = _this.getNodeParam($div);
  3050. var requestNode = _this.getRequestParam(node);
  3051. requestNode = $.extend(requestNode, data);
  3052. _this.temp = [$cite, $div, title, $p_div];
  3053. _this.toolbarFun.editTreeNode(requestNode, $div);
  3054. layer.close(index);
  3055. });
  3056. }
  3057. });
  3058. break;
  3059. case defaultTool.delTool:
  3060. layer.confirm('确定要删除该'+_this.toolbarStyle.title+'?', {icon: 3, title:'删除'+_this.toolbarStyle.title}, function(index){
  3061. var node = _this.getNodeParam($div);
  3062. _this.temp = [$p_li, $p_div];
  3063. _this.toolbarFun.delTreeNode(_this.getRequestParam(_this.getNodeParam($div)), $div);
  3064. layer.close(index);
  3065. });
  3066. break;
  3067. default:
  3068. if(_this.toolbarExt.length > 0){
  3069. for(var i=0; i<_this.toolbarExt.length; i++){
  3070. var ext = _this.toolbarExt[i];
  3071. if (tool == ext.toolbarId){
  3072. ext.handler(_this.getRequestParam(_this.getNodeParam($div)), $div);
  3073. break;
  3074. }
  3075. }
  3076. }
  3077. break;
  3078. }
  3079. }
  3080. // 加载toolBar中的内容
  3081. DTree.prototype.loadToolBar = function(title, name){
  3082. var _this = this;
  3083. var toolbarShow = _this.toolbarShow;
  3084. var nodeBarContents = _this.toolbarBtn;
  3085. var html = "";
  3086. switch (name) {
  3087. case defaultTool.addTool:
  3088. var addNodeBarDef = [{"label": "当前选中", "name": "nodeTitle", "type": "text", "value": title, "defElem": "nowChoose", "readonly": true},
  3089. {"label": "新增"+_this.toolbarStyle.title, "name": "addNodeName", "type": "text", "value": "", "defElem": "nowChange", "verify": "required"},
  3090. {"type": "submit", "value": "确认添加", "defElem": "btn", "filter": "dtree_addNode_form"}];
  3091. //2. 用户自定义的节点内容
  3092. var addNodeBar = ['<div class="'+TOOLBAR_TOOL+'"><form class="layui-form layui-form-pane" lay-filter="dtree_addNode_form">'];
  3093. if(nodeBarContents != null && nodeBarContents.length > 0){
  3094. if(nodeBarContents[0] != null && nodeBarContents[0] != undefined && nodeBarContents[0].length > 0){
  3095. var addNodeBarContents = nodeBarContents[0];
  3096. // 1. 检查是否包含了now、newly、btn这三个默认项,将其他元素依次排列,将特殊元素至于栈顶
  3097. for(var i=0; i<addNodeBarContents.length; i++){
  3098. var defElem = addNodeBarContents[i].defElem;
  3099. if(defElem == "nowChoose") {
  3100. $.extend(addNodeBarDef[0], addNodeBarContents[i]);
  3101. } else if(defElem == "nowChange") {
  3102. $.extend(addNodeBarDef[1], addNodeBarContents[i]);
  3103. } else if(defElem == "btn") {
  3104. $.extend(addNodeBarDef[2], addNodeBarContents[i]);
  3105. } else {
  3106. addNodeBarDef.push(addNodeBarContents[i]);
  3107. }
  3108. }
  3109. }
  3110. }
  3111. // 2. 遍历生成全部表单标签
  3112. for(var j=0; j<addNodeBarDef.length; j++){
  3113. var type = addNodeBarDef[j].type;
  3114. if(!type){type = "text";}
  3115. switch (type) {
  3116. case "text":
  3117. addNodeBar.push(_this.loadToolBarDetail(addNodeBarDef[j]).text());
  3118. break;
  3119. case "textarea":
  3120. addNodeBar.push(_this.loadToolBarDetail(addNodeBarDef[j]).textarea());
  3121. break;
  3122. case "select":
  3123. addNodeBar.push(_this.loadToolBarDetail(addNodeBarDef[j]).select());
  3124. break;
  3125. case "hidden":
  3126. addNodeBar.push(_this.loadToolBarDetail(addNodeBarDef[j]).hidden());
  3127. break;
  3128. }
  3129. }
  3130. var addBtn = ['<div class="layui-form-item">', '<div class="layui-input-block" style="margin-left:0px;text-align:center;">'];
  3131. // 3.遍历生成按钮
  3132. for(var j=0; j<addNodeBarDef.length; j++){
  3133. var type = addNodeBarDef[j].type;
  3134. if(!type){type = "text";}
  3135. switch (type) {
  3136. case "submit":
  3137. addBtn.push(_this.loadToolBarDetail(addNodeBarDef[j]).submit());
  3138. break;
  3139. case "button":
  3140. addBtn.push(_this.loadToolBarDetail(addNodeBarDef[j]).button());
  3141. break;
  3142. case "reset":
  3143. addBtn.push(_this.loadToolBarDetail(addNodeBarDef[j]).reset());
  3144. break;
  3145. }
  3146. }
  3147. addBtn.push('</div></div>');
  3148. addNodeBar.push(addBtn.join(''));
  3149. addNodeBar.push('</form></div>');
  3150. html = addNodeBar.join('');
  3151. break;
  3152. case defaultTool.editTool:
  3153. var editNodeBarDef = [{"label": "当前选中", "name": "nodeTitle", "type": "text", "value": title, "defElem": "nowChoose", "readonly": true},
  3154. {"label": "编辑"+_this.toolbarStyle.title, "name": "editNodeName", "type": "text", "value": "", "defElem": "nowChange", "verify": "required"},
  3155. {"type": "submit", "value": "确认编辑", "defElem": "btn", "filter": "dtree_editNode_form"}];
  3156. var editNodeBar = ['<div class="'+TOOLBAR_TOOL+'"><form class="layui-form layui-form-pane" lay-filter="dtree_editNode_form">'];
  3157. //2. 用户自定义的节点内容
  3158. if(nodeBarContents != null && nodeBarContents.length > 0){
  3159. if(nodeBarContents[1] != null && nodeBarContents[1] != undefined && nodeBarContents[1].length > 0){
  3160. var editNodeBarContents = nodeBarContents[1];
  3161. // 1. 检查是否包含了now、newly、btn这三个默认项,将其他元素依次排列,将特殊元素至于栈顶
  3162. for(var i=0; i<editNodeBarContents.length; i++){
  3163. var defElem = editNodeBarContents[i].defElem;
  3164. if(defElem == "nowChoose") {
  3165. $.extend(editNodeBarDef[0], editNodeBarContents[i]);
  3166. } else if(defElem == "nowChange") {
  3167. $.extend(editNodeBarDef[1], editNodeBarContents[i]);
  3168. } else if(defElem == "btn") {
  3169. $.extend(editNodeBarDef[2], editNodeBarContents[i]);
  3170. } else {
  3171. editNodeBarDef.push(editNodeBarContents[i]);
  3172. }
  3173. }
  3174. }
  3175. }
  3176. // 2. 遍历生成全部表单标签
  3177. for(var j=0; j<editNodeBarDef.length; j++){
  3178. var type = editNodeBarDef[j].type;
  3179. if(!type){type = "text";}
  3180. switch (type) {
  3181. case "text":
  3182. editNodeBar.push(_this.loadToolBarDetail(editNodeBarDef[j]).text());
  3183. break;
  3184. case "textarea":
  3185. editNodeBar.push(_this.loadToolBarDetail(editNodeBarDef[j]).textarea());
  3186. break;
  3187. case "select":
  3188. editNodeBar.push(_this.loadToolBarDetail(editNodeBarDef[j]).select());
  3189. break;
  3190. case "hidden":
  3191. editNodeBar.push(_this.loadToolBarDetail(editNodeBarDef[j]).hidden());
  3192. break;
  3193. }
  3194. }
  3195. var editBtn = ['<div class="layui-form-item">', '<div class="layui-input-block" style="margin-left:0px;text-align:center;">'];
  3196. // 3.遍历生成按钮
  3197. for(var j=0; j<editNodeBarDef.length; j++){
  3198. var type = editNodeBarDef[j].type;
  3199. if(!type){type = "text";}
  3200. switch (type) {
  3201. case "submit":
  3202. editBtn.push(_this.loadToolBarDetail(editNodeBarDef[j]).submit());
  3203. break;
  3204. case "button":
  3205. editBtn.push(_this.loadToolBarDetail(editNodeBarDef[j]).button());
  3206. break;
  3207. case "reset":
  3208. editBtn.push(_this.loadToolBarDetail(editNodeBarDef[j]).reset());
  3209. break;
  3210. }
  3211. }
  3212. editBtn.push('</div></div>');
  3213. editNodeBar.push(editBtn.join(''));
  3214. editNodeBar.push('</form></div>');
  3215. html = editNodeBar.join('');
  3216. break;
  3217. }
  3218. return html;
  3219. };
  3220. // 获取toolbar详细的标签信息
  3221. DTree.prototype.loadToolBarDetail = function(nodeBarContents){
  3222. var _this = this;
  3223. var readonly = (typeof (nodeBarContents.readonly) === "boolean") ? nodeBarContents.readonly : false;
  3224. var disabled = (typeof (nodeBarContents.disabled) === "boolean") ? nodeBarContents.disabled : false;
  3225. var id = nodeBarContents.id ? nodeBarContents.id : "";
  3226. var name = nodeBarContents.name ? nodeBarContents.name : "";
  3227. var val = nodeBarContents.value ? nodeBarContents.value : "";
  3228. var verify = nodeBarContents.verify ? nodeBarContents.verify : "";
  3229. var placeholder = nodeBarContents.placeholder ? nodeBarContents.placeholder : val;
  3230. return{
  3231. text: function(){
  3232. return ['<div class="layui-form-item">',
  3233. '<label class="layui-form-label" title="'+nodeBarContents.label+'">'+nodeBarContents.label+':</label>',
  3234. '<div class="layui-input-block f-input-par">',
  3235. '<input type="text" class="layui-input f-input" value="'+val+'" placeholder="'+placeholder+'" lay-verify="'+verify+'" ',
  3236. (id != "" ? 'id="'+id+'" ' : ''),
  3237. (name != "" ? 'name="'+name+'" ' : ''),
  3238. (readonly ? 'readonly ' : ''),
  3239. (disabled ? 'disabled ' : ''),
  3240. '/>',
  3241. '</div>',
  3242. '</div>'].join('');
  3243. },
  3244. textarea: function(){
  3245. return ['<div class="layui-form-item layui-form-text">',
  3246. '<label class="layui-form-label" title="'+nodeBarContents.label+'">'+nodeBarContents.label+':</label>',
  3247. '<div class="layui-input-block f-input-par">',
  3248. '<textarea class="layui-textarea f-input" value="'+val+'" placeholder="'+placeholder+'" lay-verify="'+verify+'" ',
  3249. (id != "" ? 'id="'+id+'" ' : ''),
  3250. (name != "" ? 'name="'+name+'" ' : ''),
  3251. (readonly ? 'readonly ' : ''),
  3252. (disabled ? 'disabled ' : ''),
  3253. '>'+val+'</textarea>',
  3254. '</div>',
  3255. '</div>'].join('');
  3256. },
  3257. hidden: function(){
  3258. return ['<input type="hidden" class="layui-input f-input" value="'+val+'" lay-verify="'+verify+'" ',
  3259. (id != "" ? 'id="'+id+'" ' : ''),
  3260. (name != "" ? 'name="'+name+'" ' : ''),
  3261. (readonly ? 'readonly ' : ''),
  3262. (disabled ? 'disabled ' : ''),
  3263. '/>'].join('');
  3264. },
  3265. select: function(){
  3266. var optionsData = (typeof nodeBarContents.optionsData === 'object') ? nodeBarContents.optionsData : nodeBarContents.optionsData();
  3267. var options = "";
  3268. for(var key in optionsData){
  3269. if(val == optionsData[key]){
  3270. options += "<option value='"+key+"' selected>"+optionsData[key]+"</option>";
  3271. } else {
  3272. options += "<option value='"+key+"'>"+optionsData[key]+"</option>";
  3273. }
  3274. }
  3275. return ['<div class="layui-form-item">',
  3276. '<label class="layui-form-label" title="'+nodeBarContents.label+'">'+nodeBarContents.label+':</label>',
  3277. '<div class="layui-input-block f-input-par">',
  3278. '<select lay-verify="'+verify+'" ',
  3279. (id != "" ? 'id="'+id+'" ' : ''),
  3280. (name != "" ? 'name="'+name+'" ' : ''),
  3281. (readonly ? 'readonly ' : ''),
  3282. (disabled ? 'disabled ' : ''),
  3283. '>',
  3284. options,
  3285. '</select>', '</div>', '</div>'].join('');
  3286. },
  3287. submit: function(){
  3288. var filter = nodeBarContents.filter;
  3289. return ['<button type="button" class="layui-btn layui-btn-normal btn-w100" lay-submit lay-filter="'+filter+'" ',
  3290. (id != "" ? 'id="'+id+'" ' : ''),
  3291. (name != "" ? 'name="'+name+'" ' : ''),
  3292. '>'+val+'</button>'].join('');
  3293. },
  3294. button: function(){
  3295. return ['<button type="button" class="layui-btn layui-btn-normal btn-w100" ',
  3296. (id != "" ? 'id="'+id+'" ' : ''),
  3297. (name != "" ? 'name="'+name+'" ' : ''),
  3298. ' >'+val+'</button>'].join('');
  3299. },
  3300. reset: function(){
  3301. return ['<button type="reset" class="layui-btn layui-btn-primary btn-w100" ',
  3302. (id != "" ? 'id="'+id+'" ' : ''),
  3303. (name != "" ? 'name="'+name+'" ' : ''),
  3304. '>'+val+'</button>'].join('');
  3305. }
  3306. }
  3307. };
  3308. // 新增节点后改变节点内容
  3309. DTree.prototype.changeTreeNodeAdd = function(returnID){
  3310. var _this = this;
  3311. var temp = _this.temp;
  3312. var id = temp[0], $ul = temp[1], $div = temp[2], level = temp[3];
  3313. var flag = false;
  3314. console.log(returnID);
  3315. if(returnID){
  3316. var $thisDiv = _this.obj.find("[data-id='"+id+"']");
  3317. if(typeof returnID === "object"){
  3318. // 如果是JSON格式数据,则将当前DIV删除,重新建造DIV
  3319. $thisDiv.remove();
  3320. var parseData = _this.parseData(returnID);
  3321. if(parseData.treeId()){
  3322. $ul.append(_this.getLiItemDom(parseData.treeId(), parseData.parentId(), parseData.title(), parseData.fmtTitle(), parseData.last(0), parseData.ficonClass(), parseData.iconClass(), parseData.checkArr(), level, parseData.spread(), parseData.disabled(), parseData.hide(), parseData.basicData(), parseData.recordData(), "item"));
  3323. // 建造完毕后,选中该DIV
  3324. $thisDiv = $ul.find("div[data-id='"+parseData.treeId()+"']");
  3325. _this.setNodeParam($thisDiv)
  3326. } else {
  3327. layer.msg("添加失败,节点ID为undefined!",{icon:5});
  3328. // 将li节点删除
  3329. $ul.find("li[data-id='"+id+"']").remove();
  3330. // 重新赋值
  3331. _this.setNodeParam($div);
  3332. // 临时变量制空
  3333. _this.temp = [];
  3334. return ;
  3335. }
  3336. }else if(returnID == 'refresh'){
  3337. // 如果是设置为refresh参数,则向后台发送请求,获取新增节点下的真实参数,局部刷新树。
  3338. flag = true;
  3339. } else if(typeof returnID === "string" || typeof returnID === 'number' || returnID == true){
  3340. $thisDiv.attr("data-id", returnID);
  3341. // 将li节点展示
  3342. $ul.find("li[data-id='"+returnID+"']").show();
  3343. _this.setNodeParam($thisDiv)
  3344. }
  3345. // 判断当前点击的节点是否是最后一级节点,如果是,则需要修改节点的样式
  3346. var $icon_i = $div.find("i[data-spread]");
  3347. if ($icon_i.eq(0).attr("data-spread") == "last") {
  3348. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).openWithLeaf();
  3349. } else { //如果不是,也要修改节点样式
  3350. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).open();
  3351. }
  3352. $ul.addClass(NAV_SHOW); //展开UL
  3353. _this.accordionUL($ul);
  3354. if(flag) {
  3355. _this.getChild($div);
  3356. } else {
  3357. // _this.showLine();
  3358. _this.showLine($ul.find("li"));
  3359. // 这种情况下需要在新增节点后对节点新增工具栏
  3360. if(_this.toolbar && _this.toolbarWay != 'contextmenu') {
  3361. _this.dynamicToolbarDom($thisDiv.find("cite[data-leaf]"));
  3362. }
  3363. }
  3364. } else {
  3365. // 将li节点删除
  3366. $ul.find("li[data-id='"+id+"']").remove();
  3367. // 重新赋值
  3368. _this.setNodeParam($div);
  3369. }
  3370. _this.temp = []; // 临时变量制空
  3371. };
  3372. // 编辑页打开后显示编辑页内容
  3373. DTree.prototype.changeTreeNodeDone = function(param){
  3374. var _this = this;
  3375. form.val('dtree_editNode_form', param);
  3376. form.render();
  3377. };
  3378. // 修改节点后改变节点内容
  3379. DTree.prototype.changeTreeNodeEdit = function(returnID){
  3380. var _this = this;
  3381. var temp = _this.temp;
  3382. var $cite = temp[0], $div = temp[1], title = temp[2], $p_div = temp[3];
  3383. var flag = false;
  3384. if(returnID){
  3385. if(typeof returnID === "object"){
  3386. var parseData = _this.parseData(data);
  3387. if(parseData.treeId()){
  3388. var replaceDom = _this.replaceDom($div, parseData.treeId(), parseData.last(0), parseData.spread(), parseData.disabled(), parseData.hide());
  3389. replaceDom.node(parseData.iconClass());
  3390. replaceDom.checkbox(parseData.checkArr());
  3391. replaceDom.text(parseData.title());
  3392. replaceDom.ul();
  3393. replaceDom.basicData(parseData.basicData());
  3394. replaceDom.recordData(parseData.recordData());
  3395. _this.setNodeParam($div);
  3396. } else {
  3397. layer.msg("编辑失败,节点ID为undefined!",{icon:5});
  3398. // 重新赋值
  3399. _this.setNodeParam($div);
  3400. }
  3401. }
  3402. } else {
  3403. $cite.html(title);
  3404. _this.getNodeParam($div);
  3405. }
  3406. _this.temp = []; // 临时变量制空
  3407. };
  3408. // 删除节点后改变节点内容
  3409. DTree.prototype.changeTreeNodeDel = function(flag){
  3410. var _this = this;
  3411. var temp = _this.temp;
  3412. var $p_li = temp[0],
  3413. $p_ul = $p_li.parent("ul"),
  3414. $p_div = temp[1];
  3415. if(flag){
  3416. $p_li.remove();
  3417. _this.showLine($p_ul.find("li"));
  3418. // 判断父级ul中是否还存在li,如果不存在,则需要修改节点的样式
  3419. if($p_ul.children("li").length == 0){
  3420. var $icon_i = $p_div.find("i[data-spread]");
  3421. _this.operateIcon($icon_i.eq(0), $icon_i.eq(1)).closeWithLeaf();
  3422. }
  3423. _this.initNodeParam();
  3424. }
  3425. _this.temp = []; // 临时变量制空
  3426. };
  3427. /******************** iframe区域 ********************/
  3428. // 加载iframe
  3429. DTree.prototype.loadIframe = function($div, iframeParam) {
  3430. var _this = this;
  3431. var $cite = _this.getNodeDom($div).cite();
  3432. if (!_this.useIframe) { // 启用iframe
  3433. return false;
  3434. }
  3435. var iframeElem = _this.iframe.iframeElem,
  3436. iframeUrl = _this.iframe.iframeUrl,
  3437. iframeLoad = _this.iframe.iframeLoad;
  3438. var flag = iframeLoad == "leaf" ? (($cite.attr("data-leaf") == "leaf") ? true : false) : true;
  3439. if (flag) {
  3440. if ($(iframeElem).length > 0) { //iframe存在
  3441. if (!iframeUrl) {
  3442. layer.msg("数据请求异常,iframeUrl参数未指定", {icon:5});
  3443. return false;
  3444. }
  3445. var param = AjaxHelper.serialize(iframeParam);
  3446. if(iframeUrl.indexOf("?")> -1){
  3447. param = "&"+param.substring(1, param.length);
  3448. }
  3449. var url = iframeUrl + param;
  3450. $(iframeElem).attr("src", url);
  3451. } else {
  3452. layer.msg("iframe绑定异常,请确认页面中是否有iframe页对应的容器", {icon:5});
  3453. return false;
  3454. }
  3455. }
  3456. return flag;
  3457. };
  3458. // 获取传递出去的参数,根据iframe.iframeDefaultRequest、iframe.iframeRequest和node拼出发出请求的参数
  3459. DTree.prototype.getIframeRequestParam = function(nodes){
  3460. var _this = this;
  3461. var request = _this.iframe.iframeRequest,
  3462. defaultRequestNames = _this.iframe.iframeDefaultRequest,
  3463. node = nodes || _this.node,
  3464. requestParam = {};
  3465. // 先拼用户自定义的,在拼树生成的,这样的话用户可以自定义当树未生成时的节点的初始值
  3466. for ( var key in request) {
  3467. requestParam[key] = request[key];
  3468. }
  3469. for ( var key in defaultRequestNames) {
  3470. var paramName = defaultRequestNames[key];
  3471. var paramValue = node[key];
  3472. if(typeof paramValue === "boolean"){
  3473. requestParam[paramName] = paramValue;
  3474. }else {
  3475. if(paramValue){
  3476. requestParam[paramName] = paramValue;
  3477. }
  3478. }
  3479. }
  3480. // 解决传递中文的乱码问题
  3481. var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/; //正则匹配中文
  3482. for(var key in requestParam){
  3483. if(reg.test(requestParam[key])) {
  3484. var str = requestParam[key];
  3485. requestParam[key] = encodeURI(encodeURI(str));
  3486. }
  3487. }
  3488. return requestParam;
  3489. };
  3490. /******************** 数据回调区域 ********************/
  3491. // 根据具体的id获取基于当前id的div以及对应的其他dom元素
  3492. DTree.prototype.getNodeDom = function(id){
  3493. var _this = this;
  3494. // 获取当前div,如果id就是一个dom,则就是这个,如果不是则进行选择。如果选不中则为null
  3495. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  3496. return {
  3497. div: function(){ // 获取当前div
  3498. return $div;
  3499. },
  3500. fnode: function(){ // 获取一级图标元素
  3501. return ($div == null) ? null : $div.find("i[data-spread]").eq(0);
  3502. },
  3503. snode: function(){ // 获取二级图标元素
  3504. return ($div == null) ? null : $div.find("i[data-spread]").eq(1);
  3505. },
  3506. checkbox: function(){ // 获取复选框元素
  3507. return ($div == null) ? null : $div.find("i[data-par]");
  3508. },
  3509. cite: function(){ // 获取cite元素
  3510. return ($div == null) ? null : $div.find("cite[data-leaf]");
  3511. },
  3512. nextUl: function(){ // 获取相邻的ul元素
  3513. return ($div == null) ? null : $div.next("ul");
  3514. },
  3515. parentLi: function(){ // 获取父级li元素
  3516. return ($div == null) ? null : $div.parent("li");
  3517. },
  3518. parentUl: function(){ // 获取基于当前$div的上级$ul
  3519. return ($div == null) ? null : $div.parent("li").parent("ul");
  3520. },
  3521. parentDiv: function(){ // 获取基于当前$div的上级$div
  3522. return ($div == null) ? null : $div.parent("li").parent("ul").prev("div");
  3523. },
  3524. nowDiv: function(){ // 获取当前选中节点,没有则返回null
  3525. return (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS).length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS);
  3526. },
  3527. nowOrRootDiv: function(){ // 获取当前选中节点,没有则返回根节点下的第一个div
  3528. return (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS).length == 0) ? _this.obj.children("li").eq(0).children("div").eq(0) : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS);
  3529. },
  3530. nowOrRootUl: function(){ // 获取当前选中节点下一个UL 或根节点。为了将新节点放入ul下
  3531. return (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS).length == 0) ? _this.obj : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id]").parent().find("."+NAV_THIS).next("ul");
  3532. }
  3533. }
  3534. };
  3535. // 获取当前选中节点下一个UL 或根节点。为了将新节点放入ul下
  3536. DTree.prototype.getNowNodeUl = function() {
  3537. var _this = this;
  3538. return _this.getNodeDom().nowOrRootUl();
  3539. };
  3540. // 获取当前选中节点 或第一个根节点。
  3541. DTree.prototype.getNowNode = function() {
  3542. var _this = this;
  3543. return _this.getNodeDom().nowOrRootDiv();
  3544. };
  3545. // 获取当前选中节点 无则返回null。
  3546. DTree.prototype.getNowNodeOrNull = function() {
  3547. var _this = this;
  3548. return _this.getNodeDom().nowDiv();
  3549. };
  3550. // 获取指定节点。
  3551. DTree.prototype.getNode = function(id) {
  3552. var _this = this;
  3553. return _this.getNodeDom(id).div();
  3554. };
  3555. // 设置当前选中节点的全部参数
  3556. DTree.prototype.setNodeParam = function($div) {
  3557. var _this = this;
  3558. _this.node.nodeId = $div.attr("data-id");
  3559. _this.node.parentId = _this.getNodeDom($div).parentLi().attr("data-pid");
  3560. _this.node.context = (typeof _this.formatter.title === 'function') ? _this.getNodeDom($div).cite().attr("data-title") : _this.getNodeDom($div).cite().text();
  3561. _this.node.leaf = _this.getNodeDom($div).cite().attr("data-leaf") == "leaf" ? true : false;
  3562. _this.node.level = _this.getNodeDom($div).parentLi().attr("data-index");
  3563. _this.node.spread = _this.getNodeDom($div).fnode().attr("data-spread") == "open" ? true : false;
  3564. _this.node.basicData = $div.attr("data-basic")
  3565. _this.node.recordData = $div.attr("data-record");
  3566. if (_this.getNodeDom($div).checkbox()) {
  3567. var dataTypes = "", checkeds = "", initcheckeds = "";
  3568. _this.getNodeDom($div).checkbox().each(function(){
  3569. dataTypes += $(this).attr("data-type") + ",";
  3570. checkeds += $(this).attr("data-checked") + ",";
  3571. initcheckeds += $(this).attr("data-initchecked") + ",";
  3572. });
  3573. dataTypes = dataTypes.substring(0, dataTypes.length-1);
  3574. checkeds = checkeds.substring(0, checkeds.length-1);
  3575. initcheckeds = initcheckeds.substring(0, initcheckeds.length-1);
  3576. _this.node.dataType = dataTypes;
  3577. _this.node.checked = checkeds;
  3578. _this.node.initchecked = initcheckeds;
  3579. }
  3580. };
  3581. // 获取当前选中节点的全部参数
  3582. DTree.prototype.getNodeParam = function($div) {
  3583. var _this = this;
  3584. if ($div) {
  3585. _this.setNodeParam($div);
  3586. } else {
  3587. if(_this.obj.find("div[data-id]").parent().find("."+NAV_THIS).length == 0){
  3588. _this.initNodeParam();
  3589. }
  3590. }
  3591. return this.node;
  3592. };
  3593. // 获取一个临时的node参数
  3594. DTree.prototype.getTempNodeParam = function($div) {
  3595. var _this = this;
  3596. var temp_node = {};
  3597. temp_node.nodeId = $div.attr("data-id");
  3598. temp_node.parentId = _this.getNodeDom($div).parentLi().attr("data-pid");
  3599. temp_node.context = (typeof _this.formatter.title === 'function') ? _this.getNodeDom($div).cite().attr("data-title") : _this.getNodeDom($div).cite().text();
  3600. temp_node.leaf = _this.getNodeDom($div).cite().attr("data-leaf") == "leaf" ? true : false;
  3601. temp_node.level = _this.getNodeDom($div).parentLi().attr("data-index");
  3602. temp_node.spread = _this.getNodeDom($div).fnode().attr("data-spread") == "open" ? true : false;
  3603. temp_node.basicData = $div.attr("data-basic")
  3604. temp_node.recordData = $div.attr("data-record");
  3605. if (_this.getNodeDom($div).checkbox()) {
  3606. var dataTypes = "", checkeds = "", initcheckeds = "";
  3607. _this.getNodeDom($div).checkbox().each(function(){
  3608. dataTypes += $(this).attr("data-type") + ",";
  3609. checkeds += $(this).attr("data-checked") + ",";
  3610. initcheckeds += $(this).attr("data-initchecked") + ",";
  3611. });
  3612. dataTypes = dataTypes.substring(0, dataTypes.length-1);
  3613. checkeds = checkeds.substring(0, checkeds.length-1);
  3614. initcheckeds = initcheckeds.substring(0, initcheckeds.length-1);
  3615. temp_node.dataType = dataTypes;
  3616. temp_node.checked = checkeds;
  3617. temp_node.initchecked = initcheckeds;
  3618. }
  3619. return temp_node;
  3620. };
  3621. // 重置参数
  3622. DTree.prototype.initNodeParam = function(){
  3623. var _this = this;
  3624. _this.node.nodeId = "";
  3625. _this.node.parentId = "";
  3626. _this.node.context = "";
  3627. _this.node.leaf = "";
  3628. _this.node.level = "";
  3629. _this.node.spread = "";
  3630. _this.node.dataType = "";
  3631. _this.node.checked = "";
  3632. _this.node.initchecked = "";
  3633. _this.node.basicData = "";
  3634. _this.node.recordData = "";
  3635. };
  3636. // 获取传递出去的参数,根据defaultRequest、request和node拼出发出请求的参数
  3637. DTree.prototype.getRequestParam = function(nodes){
  3638. var _this = this;
  3639. var request = _this.request,
  3640. defaultRequestNames = _this.defaultRequest,
  3641. node = nodes || _this.node,
  3642. requestParam = {};
  3643. // 先拼用户自定义的,在拼树生成的,这样的话用户可以自定义当树未生成时的节点的初始值
  3644. for ( var key in request) {
  3645. requestParam[key] = request[key];
  3646. }
  3647. for ( var key in defaultRequestNames) {
  3648. var paramName = defaultRequestNames[key];
  3649. var paramValue = node[key];
  3650. if(typeof paramValue === "boolean"){
  3651. requestParam[paramName] = paramValue;
  3652. }else {
  3653. if(paramValue){
  3654. requestParam[paramName] = paramValue;
  3655. }
  3656. }
  3657. }
  3658. return requestParam;
  3659. };
  3660. // 获取filterParam过滤后的requestParam
  3661. DTree.prototype.getFilterRequestParam = function(requestParam){
  3662. var _this = this;
  3663. var filterRequest = _this.filterRequest;
  3664. return event.cloneObj(requestParam, filterRequest);
  3665. };
  3666. // 获取当前选中值
  3667. DTree.prototype.getNowParam = function(){
  3668. var _this = this;
  3669. return _this.getRequestParam(_this.getNodeParam());
  3670. };
  3671. // 获取指定节点选中值
  3672. DTree.prototype.getParam = function(id){
  3673. var _this = this;
  3674. // 获取当前div,如果id就是一个dom,则就是这个,如果不是则进行选择。如果选不中则为null
  3675. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  3676. if($div != null){ return _this.callbackData().node(_this.getTempNodeParam($div)); } else { return {}; }
  3677. };
  3678. // 获取参数的上级节点
  3679. DTree.prototype.getParentParam = function(id){
  3680. var _this = this;
  3681. // 获取当前div,如果id就是一个dom,则就是这个,如果不是则进行选择。如果选不中则为null
  3682. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  3683. if($div != null){ return _this.callbackData().parentNode($div); } else { return {}; }
  3684. };
  3685. // 获取参数的全部上级节点
  3686. DTree.prototype.getAllParentParam = function(id){
  3687. var _this = this;
  3688. // 获取当前div,如果id就是一个dom,则就是这个,如果不是则进行选择。如果选不中则为null
  3689. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  3690. var arr = [];
  3691. if($div != null){
  3692. var level = _this.getTempNodeParam($div).level;
  3693. for(var i=1; i<level; i++){ // 从1开始遍历,如果level等于1说明是根节点
  3694. arr.unshift(_this.callbackData().parentNode($div));
  3695. $div = _this.getNodeDom($div).parentDiv();
  3696. }
  3697. }
  3698. return arr;
  3699. };
  3700. // 获取参数的下级节点
  3701. DTree.prototype.getChildParam = function(id){
  3702. var _this = this;
  3703. // 获取当前div,如果id就是一个dom,则就是这个,如果不是则进行选择。如果选不中则为null
  3704. var $div = (typeof id === 'object') ? id : (_this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']").length == 0) ? null : _this.obj.find("div[dtree-click='"+eventName.itemNodeClick+"'][data-id='"+id+"']");
  3705. if($div != null){ return _this.callbackData().childNode($div); } else { return []; }
  3706. };
  3707. // 获取回调数据
  3708. DTree.prototype.callbackData = function(){
  3709. var _this = this;
  3710. return {
  3711. dom: function($dom){ // 获取dom
  3712. return $dom;
  3713. },
  3714. node: function(node){ // 获取当前节点值
  3715. return _this.getRequestParam(node);
  3716. },
  3717. childNode: function($div){ // 获取下级节点值
  3718. var $childDivs = $div.next("ul").find("li."+LI_NAV_ITEM+" div."+LI_DIV_ITEM);
  3719. var childNode = [];
  3720. if($childDivs && $childDivs.length > 0){
  3721. $childDivs.each(function(){
  3722. var $cDiv = $(this);
  3723. childNode.push(_this.getRequestParam(_this.getTempNodeParam($cDiv)));
  3724. });
  3725. }
  3726. return childNode;
  3727. },
  3728. parentNode: function($div){ // 获取上级节点值
  3729. var pId = _this.getNodeDom($div).parentLi().attr("data-pid");
  3730. var $pdiv = _this.obj.find("div[data-id='"+pId+"']");
  3731. if($pdiv.length > 0) {return _this.getRequestParam(_this.getTempNodeParam($pdiv));} else {return {};}
  3732. }
  3733. }
  3734. };
  3735. /******************** 事件回调区域 ********************/
  3736. // 绑定浏览器事件
  3737. DTree.prototype.bindBrowserEvent = function(){
  3738. var _this = this;
  3739. // 绑定文件夹展开/收缩的图标的点击事件,点击时给当前节点的div添加选中class
  3740. _this.obj.on("click", "i[data-spread]", function(event) {
  3741. event.stopPropagation();
  3742. var $i = $(this),
  3743. $div = $i.parent("div"),
  3744. node = _this.getNodeParam($div);
  3745. _this.toolbarHide();
  3746. _this.navThis($div);
  3747. _this.clickSpread($div); // 展开或隐藏节点
  3748. // 树状态改变后,用户自定义想做的事情
  3749. layui.event.call(this, MOD_NAME, "changeTree("+$(_this.obj)[0].id+")", {
  3750. dom: _this.callbackData().dom($i),
  3751. param: _this.callbackData().node(node),
  3752. show: _this.callbackData().dom($i).attr("data-spread") == "open" ? true : false
  3753. });
  3754. });
  3755. // 绑定所有子节点div的单击事件,点击时触发加载iframe或用户自定义想做的事情
  3756. _this.obj.on("click", "div[dtree-click='"+eventName.itemNodeClick+"'][dtree-disabled='false']", function(event) {
  3757. event.stopPropagation();
  3758. var $div = $(this),
  3759. $cite = $div.find("cite"),
  3760. node = _this.getNodeParam($div);
  3761. _this.toolbarHide();
  3762. _this.navThis($div);
  3763. if (_this.useIframe) {
  3764. var iframeParam = _this.getFilterRequestParam(_this.getIframeRequestParam(node));
  3765. var flag = _this.loadIframe($div, iframeParam); // 加载iframe
  3766. if (flag) {
  3767. // iframe加载完毕后,用户自定义想做的事情
  3768. _this.iframeFun.iframeDone(iframeParam);
  3769. layui.event.call(this, MOD_NAME, "iframeDone("+$(_this.obj)[0].id+")", {
  3770. "iframeParam": iframeParam,
  3771. dom: _this.callbackData().dom($div)
  3772. });
  3773. }
  3774. } else {
  3775. // 单击事件执行完毕后,用户自定义想做的事情
  3776. layui.event.call(this, MOD_NAME, "node("+$(_this.obj)[0].id+")", {
  3777. param: _this.callbackData().node(node),
  3778. childParams: _this.callbackData().childNode($div),
  3779. parentParam: _this.callbackData().parentNode($div),
  3780. dom: _this.callbackData().dom($div)
  3781. });
  3782. }
  3783. });
  3784. // 绑定所有子节点div的双击事件,暴露on给用户自定义
  3785. _this.obj.on("dblclick", "div[dtree-click='"+eventName.itemNodeClick+"'][dtree-disabled='false']", function(event) {
  3786. event.stopPropagation();
  3787. var $div = $(this),
  3788. $cite = $div.find("cite"),
  3789. node = _this.getNodeParam($div);
  3790. _this.toolbarHide();
  3791. _this.navThis($div);
  3792. // 双击事件执行完毕后,用户自定义想做的事情
  3793. layui.event.call(this, MOD_NAME, "nodedblclick("+$(_this.obj)[0].id+")", {
  3794. param: _this.callbackData().node(node),
  3795. childParams: _this.callbackData().childNode($div),
  3796. parentParam: _this.callbackData().parentNode($div),
  3797. dom: _this.callbackData().dom($div)
  3798. });
  3799. });
  3800. if(_this.checkbar) {
  3801. // 绑定cheboxbar的节点复选框
  3802. _this.obj.on("click", "i[dtree-click='"+eventName.checkNodeClick+"'][dtree-disabled='false']", function(event) {
  3803. _this.toolbarHide();
  3804. var $i = $(this),
  3805. $div = $i.closest("div[dtree-click='"+eventName.itemNodeClick+"']"),
  3806. node = _this.getNodeParam($div);
  3807. // 复选框选中前的回调
  3808. var flag = _this.checkbarFun.chooseBefore($i, _this.getRequestParam(node));
  3809. _this.temp = [$i];
  3810. if(flag){_this.changeCheck();}
  3811. event.stopPropagation();
  3812. });
  3813. }
  3814. if(_this.menubar) {
  3815. // 绑定menubar的点击事件
  3816. _this.obj.prevAll('div#dtree_menubar_'+_this.obj[0].id).on("click", "button[d-menu]", function(event) {
  3817. event.stopPropagation();
  3818. _this.toolbarHide();
  3819. _this.menubarListener($(this).attr("d-menu"), "group");
  3820. });
  3821. // 绑定menubar的点击事件
  3822. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).on("click", "a[d-menu]", function(event) {
  3823. event.stopPropagation();
  3824. _this.toolbarHide();
  3825. _this.menubarListener($(this).attr("d-menu"), "toolbar");
  3826. });
  3827. // 绑定menubar的点击按钮事件
  3828. _this.obj.closest('body').find("*[dtree-id='"+_this.obj[0].id+"'][dtree-menu]").on("click", function(event) {
  3829. event.stopPropagation();
  3830. _this.toolbarHide();
  3831. _this.menubarListener($(this).attr("dtree-menu"), "freedom");
  3832. });
  3833. }
  3834. if(_this.toolbar) {
  3835. if(_this.toolbarWay == "contextmenu") {
  3836. //绑定所有子节点div的右键点击事件,用于显示toolbar
  3837. _this.obj.on("contextmenu", "div[dtree-click='"+eventName.itemNodeClick+"'][d-contextmenu='true'][dtree-disabled='false']", function(e){
  3838. var $div = $(this),
  3839. node = _this.getNodeParam($div);
  3840. _this.toolbarHide();
  3841. // toolbar加载前执行的方法,执行完毕之后创建按钮
  3842. _this.setToolbarDom().setToolbarPlace(_this.toolbarFun.loadToolbarBefore(event.cloneObj(_this.toolbarMenu), _this.getRequestParam(node), $div));
  3843. var e = e || window.event,
  3844. mx = e.pageX - $div.offset().left +45 ,
  3845. my = $div.offset().top - _this.obj.closest(_this.toolbarScroll).offset().top +15;
  3846. _this.navThis($div);
  3847. var $toolBarDiv = _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id);
  3848. $toolBarDiv.find(".layui-nav-child").addClass('layui-anim-fadein layui-show');
  3849. $toolBarDiv.css({'left':mx+'px','top':my+'px'});
  3850. e.stopPropagation();
  3851. return false;
  3852. });
  3853. // 绑定装载树的上层出现滚动条的容器,让toolbar隐藏
  3854. _this.obj.closest(_this.toolbarScroll).scroll(function() {
  3855. _this.toolbarHide();
  3856. });
  3857. // 绑定toolbar的点击事件
  3858. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).on("click", "a[dtree-tool]", function(event) {
  3859. event.stopPropagation();
  3860. var $div = _this.getNodeDom().nowOrRootDiv(),
  3861. node = _this.getNodeParam($div);
  3862. _this.toolbarHide();
  3863. var tool = $(this).attr("dtree-tool");
  3864. _this.toolbarListener(tool, $div);
  3865. });
  3866. } else if(_this.toolbarWay == "fixed") {
  3867. // 绑定toolbar的点击事件
  3868. _this.obj.on("click", "a[dtree-tool]", function(event) {
  3869. event.stopPropagation();
  3870. var $a = $(this),
  3871. $cite = $a.parent("em."+TOOLBAR_TOOL_EM).prev("cite"), //当前选中节点的text
  3872. $div = $cite.parent("div"),
  3873. node = _this.getNodeParam($div);
  3874. var tool = $a.attr("dtree-tool");
  3875. _this.toolbarHide();
  3876. _this.navThis($div);
  3877. _this.toolbarListener(tool, $div);
  3878. });
  3879. } else if(_this.toolbarWay == "follow") {
  3880. //绑定所有子节点div的mouseover mouseout事件,用于显示或隐藏toolbar
  3881. _this.obj.on("mouseover mouseout", "div[dtree-click='"+eventName.itemNodeClick+"'][dtree-disabled='false']", function(event){
  3882. var $div = $(this),
  3883. $toolBarEm = $div.children("em."+TOOLBAR_TOOL_EM);
  3884. if(event.type == "mouseover"){
  3885. $toolBarEm.removeClass(NAV_HIDE);
  3886. event.stopPropagation();
  3887. } else if(event.type == "mouseout"){
  3888. $toolBarEm.addClass(NAV_HIDE);
  3889. event.stopPropagation();
  3890. }
  3891. });
  3892. // 绑定toolbar的点击事件
  3893. _this.obj.on("click", "a[dtree-tool]", function(event) {
  3894. event.stopPropagation();
  3895. var $a = $(this),
  3896. $cite = $a.parent("em."+TOOLBAR_TOOL_EM).prev("cite"), //当前选中节点的text
  3897. $div = $cite.parent("div"),
  3898. node = _this.getNodeParam($div);
  3899. var tool = $a.attr("dtree-tool");
  3900. _this.toolbarHide();
  3901. _this.navThis($div);
  3902. _this.toolbarListener(tool, $div);
  3903. });
  3904. }
  3905. }
  3906. // 开启拖拽
  3907. /*if(_this.drawable) {
  3908. //绑定所有子节点div的拖拽事件
  3909. _this.obj.on("mousedown", "div[dtree-click='" + eventName.itemNodeClick + "'][dtree-disabled='false']", function (e) {
  3910. var moveCount = 0;
  3911. var $div = $(this);
  3912. // 克隆点击节点
  3913. var $tempDiv = $div.clone();
  3914. // 获取当前被点击节点的坐标
  3915. var e = e || window.event;
  3916. var x = $div.offset().left;
  3917. var y = $div.offset().top;
  3918. var left = e.pageX - x;
  3919. var top = e.pageY - y;
  3920. _this.obj.append($tempDiv);
  3921. // 克隆节点标记
  3922. $tempDiv.css({
  3923. "position": "absolute",
  3924. "background-color": "#FF8C69",
  3925. 'left' : x,
  3926. 'top' : top,
  3927. });
  3928. // 添加临时节点
  3929. $div.css('cursor', 'move');
  3930. $(document).on("mousemove", function (e1) {
  3931. // moveCount为了区别click事件
  3932. moveCount++;
  3933. if (moveCount > 2) {
  3934. // 计算坐标
  3935. var e1 = e1 || window.event;
  3936. var x1 = e1.pageX - left;
  3937. var y1 = e1.pageY - top;
  3938. var maxL = $(document).width() - $div.outerWidth();
  3939. var maxT = $(document).height() - $div.outerHeight();
  3940. //不允许超出浏览器范围
  3941. x1 = x1 < 0 ? 0: x1;
  3942. x1 = x1 > maxL ? maxL: x1;
  3943. y1 = y1 < 0 ? 0: y;
  3944. y1 = y1 > maxT ? maxT: y1;
  3945. //3.修改克隆节点的坐标
  3946. $tempDiv.css({
  3947. 'left' : x1,
  3948. 'top' : y1,
  3949. });
  3950. }
  3951. }).on("mouseup", function (e2) {
  3952. $(document).off("mousemove").off("mouseup");
  3953. });
  3954. });
  3955. }*/
  3956. };
  3957. // 绑定body的单击,让本页面所有的toolbar隐藏
  3958. $BODY.on("click", function(event){
  3959. $("div."+LI_DIV_TOOLBAR).find(".layui-show").removeClass('layui-anim-fadein layui-show');
  3960. });
  3961. // 解绑浏览器事件
  3962. DTree.prototype.unbindBrowserEvent = function(){
  3963. var _this = this;
  3964. // 本身事件解绑
  3965. _this.obj.unbind();
  3966. // 菜单栏解绑
  3967. if(_this.menubar){
  3968. _this.obj.prevAll('div#dtree_menubar_'+_this.obj[0].id).unbind();
  3969. if(_this.obj.closest('body').find("*[dtree-id='"+_this.obj[0].id+"'][dtree-menu]").length > 0){
  3970. _this.obj.closest('body').find("*[dtree-id='"+_this.obj[0].id+"'][dtree-menu]").unbind();
  3971. }
  3972. }
  3973. // 工具栏解绑
  3974. if(_this.toolbar){
  3975. if(_this.toolbarWay == "contextmenu") {
  3976. _this.obj.prevAll('div#dtree_toolbar_'+_this.obj[0].id).unbind();
  3977. if(_this.obj.closest(_this.toolbarScroll).length > 0){
  3978. _this.obj.closest(_this.toolbarScroll).unbind();
  3979. }
  3980. }
  3981. }
  3982. };
  3983. /** 外部访问 **/
  3984. var dtree = {
  3985. render: function(options){ // 初始化树
  3986. var dTree = null;
  3987. var id = event.getElemId(options);
  3988. if(id == "") {
  3989. layer.msg("页面中未找到绑定id", {icon:5});
  3990. } else {
  3991. dTree = DTrees[id];
  3992. if(typeof dTree === 'object'){
  3993. dTree.reloadSetting(options);
  3994. dTree.initTreePlus();
  3995. dTree.openTreePlus();
  3996. dTree.initNodeParam();
  3997. dTree.init();
  3998. dTree.unbindBrowserEvent();
  3999. dTree.bindBrowserEvent();
  4000. } else {
  4001. // 创建树
  4002. dTree = new DTree(options);
  4003. // 添加到树数组中去
  4004. DTrees[id] = dTree;
  4005. dTree.initTreePlus();
  4006. dTree.openTreePlus();
  4007. dTree.init();
  4008. dTree.bindBrowserEvent();
  4009. }
  4010. }
  4011. return dTree;
  4012. },
  4013. reload: function(dTree, options){ // 重新加载树
  4014. if(typeof dTree === "string"){
  4015. dTree = DTrees[dTree];
  4016. }
  4017. if(typeof dTree === "undefined"){
  4018. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4019. return ;
  4020. }
  4021. dTree.reloadSetting(options);
  4022. dTree.initTreePlus();
  4023. dTree.openTreePlus();
  4024. dTree.initNodeParam();
  4025. dTree.init();
  4026. dTree.unbindBrowserEvent();
  4027. dTree.bindBrowserEvent();
  4028. },
  4029. on: function(events, callback) { // 绑定事件
  4030. if(events.indexOf("'") > 0){
  4031. events = events.replace(/'/g,"");
  4032. }
  4033. if(events.indexOf('"') > 0) {
  4034. events = events.replace(/"/g,"");
  4035. }
  4036. return layui.onevent.call(this, MOD_NAME, events, callback);
  4037. },
  4038. click: function(dTree, id) { // 模拟单击事件
  4039. if(typeof dTree === "string"){
  4040. dTree = DTrees[dTree];
  4041. }
  4042. if(typeof dTree === "undefined"){
  4043. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4044. return ;
  4045. }
  4046. $("div[dtree-click='"+eventName.itemNodeClick+"'][dtree-id='"+dTree.obj[0].id+"'][data-id='"+id+"']").click();
  4047. },
  4048. getNowParam: function(dTree){ // 获取当前选中值
  4049. if(typeof dTree === "string"){
  4050. dTree = DTrees[dTree];
  4051. }
  4052. if(typeof dTree === "undefined"){
  4053. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4054. return ;
  4055. }
  4056. return dTree.getNowParam(); // 获取当前选中值
  4057. },
  4058. getParam: function(dTree, id){ // 获取指定节点值
  4059. if(typeof dTree === "string"){
  4060. dTree = DTrees[dTree];
  4061. }
  4062. if(typeof dTree === "undefined"){
  4063. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4064. return ;
  4065. }
  4066. return dTree.getParam(id); // 获取指定节点值
  4067. },
  4068. getParentParam: function(dTree, id){ // 获取参数的上级节点
  4069. if(typeof dTree === "string"){
  4070. dTree = DTrees[dTree];
  4071. }
  4072. if(typeof dTree === "undefined"){
  4073. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4074. return ;
  4075. }
  4076. return dTree.getParentParam(id);
  4077. },
  4078. getAllParentParam: function(dTree, id){ // 获取参数的全部上级节点
  4079. if(typeof dTree === "string"){
  4080. dTree = DTrees[dTree];
  4081. }
  4082. if(typeof dTree === "undefined"){
  4083. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4084. return ;
  4085. }
  4086. return dTree.getAllParentParam(id);
  4087. },
  4088. getChildParam: function(dTree, id){ // 获取参数的全部下级节点
  4089. if(typeof dTree === "string"){
  4090. dTree = DTrees[dTree];
  4091. }
  4092. if(typeof dTree === "undefined"){
  4093. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4094. return ;
  4095. }
  4096. return dTree.getChildParam(id);
  4097. },
  4098. getCheckbarNodesParam: function(dTree){ // 获取复选框选中值
  4099. if(typeof dTree === "string"){
  4100. dTree = DTrees[dTree];
  4101. }
  4102. if(typeof dTree === "undefined"){
  4103. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4104. return {};
  4105. }
  4106. return dTree.getCheckbarNodesParam(); // 获取复选框选中值
  4107. },
  4108. dataInit: function(dTree, chooseId){ // 初始化选中树,针对数据反选
  4109. if(typeof dTree === "string"){
  4110. dTree = DTrees[dTree];
  4111. }
  4112. if(typeof dTree === "undefined"){
  4113. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4114. return ;
  4115. }
  4116. if(chooseId){
  4117. return dTree.dataInit(chooseId);
  4118. }
  4119. },
  4120. chooseDataInit: function(dTree, chooseIds){ // 初始化复选框选中,针对数据反选
  4121. if(typeof dTree === "string"){
  4122. dTree = DTrees[dTree];
  4123. }
  4124. if(typeof dTree === "undefined"){
  4125. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4126. return ;
  4127. }
  4128. if(chooseIds){
  4129. return dTree.chooseDataInit(chooseIds);
  4130. }
  4131. },
  4132. changeCheckbarNodes: function(dTree){ //判断复选框是否发生变更
  4133. if(typeof dTree === "string"){
  4134. dTree = DTrees[dTree];
  4135. }
  4136. if(typeof dTree === "undefined"){
  4137. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4138. return ;
  4139. }
  4140. return dTree.changeCheckbarNodes();
  4141. },
  4142. initNoAllCheck: function(dTree) { //复选框半选状态初始化设置
  4143. if(typeof dTree === "string"){
  4144. dTree = DTrees[dTree];
  4145. }
  4146. if(typeof dTree === "undefined"){
  4147. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4148. return ;
  4149. }
  4150. return dTree.initNoAllCheck();
  4151. },
  4152. initAllCheck: function(dTree){ // 复选框选中状态初始化设置
  4153. if(typeof dTree === "string"){
  4154. dTree = DTrees[dTree];
  4155. }
  4156. if(typeof dTree === "undefined"){
  4157. layer.msg("方法获取失败,请检查ID或对象传递是否正确",{icon:2});
  4158. return ;
  4159. }
  4160. return dTree.initAllCheck();
  4161. },
  4162. escape: function(html){ // 字符串格式化
  4163. return event.escape(html);
  4164. },
  4165. unescape: function(str){ // 字符串反格式化
  4166. return event.unescape(str);
  4167. },
  4168. version: function(){ //获取版本号
  4169. return VERSION;
  4170. }
  4171. };
  4172. exports('dtree', dtree);
  4173. });