DatatablePC.js 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587
  1. /**
  2. * 数据表格数据结构.
  3. * @typedef {Array} DatatableData
  4. * @example
  5. { //数据表格数据条目
  6. "data": [
  7. {
  8. "org": [{
  9. "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
  10. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  11. "name": "张三",
  12. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  13. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  14. "unitLevelName": "兰德纵横/市场营销部",
  15. "unitName": "市场营销部"
  16. }],
  17. "org_1": [{
  18. "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
  19. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  20. "name": "张三",
  21. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  22. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  23. "unitLevelName": "兰德纵横/市场营销部",
  24. "unitName": "市场营销部"
  25. }, {
  26. "distinguishedName": "李四@bf007525-99a3-4178-a474-32865bdddec8@I",
  27. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  28. "name": "李四",
  29. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  30. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  31. "unitLevelName": "兰德纵横/市场营销部",
  32. "unitName": "市场营销部"
  33. }],
  34. "number": "111",
  35. "textfield": "杭州",
  36. "attachment": [
  37. {
  38. "activityName": "拟稿",
  39. "extension": "jpg",
  40. "id": "9514758e-9e28-4bfe-87d7-824f2811f173",
  41. "businessId": "1234758e-9e28-4bfe-87d7-824f2811f173",
  42. "lastUpdateTime": "2020-12-09 21:48:03",
  43. "length": 452863.0,
  44. "name": "111.jpg",
  45. "person": "李四@lisi@P"
  46. }
  47. ]
  48. }
  49. ...
  50. ],
  51. "total": {
  52. "number": 222, //总计采用字段id
  53. "textfield": 2
  54. }
  55. }
  56. */
  57. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  58. /** @class DatatablePC 数据表格组件。表格形式的多行数据编辑组件。
  59. * @o2cn 数据表格PC端
  60. * @example
  61. * //可以在脚本中获取该组件
  62. * //方法1:
  63. * var datatable = this.form.get("name"); //获取组件
  64. * //方法2
  65. * var datatable = this.target; //在组件事件脚本中获取
  66. * @extends MWF.xApplication.process.Xform.$Module
  67. * @o2category FormComponents
  68. * @since v6.2
  69. * @o2range {Process|CMS|Portal}
  70. * @hideconstructor
  71. */
  72. MWF.xApplication.process.Xform.DatatablePC = new Class(
  73. /** @lends MWF.xApplication.process.Xform.DatatablePC# */
  74. {
  75. Implements: [Events],
  76. Extends: MWF.APP$Module,
  77. isEdit: false,
  78. options: {
  79. /**
  80. * 所有内容加载后执行(包括异步加载)。
  81. * @event MWF.xApplication.process.Xform.DatatablePC#afterLoad
  82. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  83. */
  84. /**
  85. * 每初始化一个条目,但未加载的时候触发,通过this.event可以获取条目对象。
  86. * @event MWF.xApplication.process.Xform.DatatablePC#beforeLoadLine
  87. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  88. */
  89. /**
  90. * 每一个条目加载后时候触发,通过this.event可以获取条目对象。
  91. * @event MWF.xApplication.process.Xform.DatatablePC#afterLoadLine
  92. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  93. */
  94. /**
  95. * 数据表格改变时触发。通过this.event.lines可以获取修改的条目数组,this.event.type可以获得修改的类型。<br/>
  96. * <table>
  97. * <tr><th><b>this.event.type</b></th><th><b>触发类型</b></th><th><b>this.event.lines</b></th></tr>
  98. * <tr><td>addline</td><td>添加一行</td><td>添加的行数组</td></tr>
  99. * <tr><td>deleteline</td><td>删除一行</td><td>删除的行数组</td></tr>
  100. * <tr><td>editcomplete</td><td>某行完成编辑(点击当前编辑行前面的√执行。同时编辑多行忽略)</td><td>编辑的行数组</td></tr>
  101. * <tr><td>editmodule</td><td>字段值改变时(同时编辑多行触发此事件,每次编辑单行忽略)</td><td>this.event.lines为编辑的行数组<br/>this.event.module为修改的字段</td></tr>
  102. * <tr><td>move</td><td>通过向上箭头调整行顺序</td><td>数据表格所有行</td></tr>
  103. * <tr><td>import</td><td>导入数据后</td><td>数据表格所有行</td></tr>
  104. * </table>
  105. * @event MWF.xApplication.process.Xform.DatatablePC#change
  106. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  107. */
  108. /**
  109. * 添加条目时触发。通过this.event.line可以获取对应的条目对象,this.event.ev可以获得事件触发的Event。
  110. * @event MWF.xApplication.process.Xform.DatatablePC#addLine
  111. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  112. */
  113. /**
  114. * 编辑条目时触发(同时编辑多行不触发此事件)。通过this.event可以获取对应的条目对象。
  115. * @event MWF.xApplication.process.Xform.DatatablePC#editLine
  116. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  117. */
  118. /**
  119. * 完成编辑条目时触发(点击当前编辑行前面的√执行。同时编辑多行不触发此事件)。通过this.event可以获取对应的条目对象。
  120. * @event MWF.xApplication.process.Xform.DatatablePC#completeLineEdit
  121. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  122. */
  123. /**
  124. * 取消编辑条目时触发(点击当前编辑行前面的 — 执行。同时编辑多行不触发此事件)。通过this.event可以获取对应的条目对象。
  125. * @event MWF.xApplication.process.Xform.DatatablePC#cancelLineEdit
  126. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  127. */
  128. /**
  129. * 删除条目前触发。通过this.event可以获取对应的条目对象。
  130. * @event MWF.xApplication.process.Xform.DatatablePC#deleteLine
  131. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  132. */
  133. /**
  134. * 删除条目后触发。
  135. * @event MWF.xApplication.process.Xform.DatatablePC#afterDeleteLine
  136. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  137. */
  138. /**
  139. * 导出前触发。
  140. * @event MWF.xApplication.process.Xform.DatatablePC#beforeExport
  141. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  142. */
  143. /**
  144. * 导出excel的时候触发,this.event指向导出的数据,您可以通过修改this.event来修改数据。
  145. * @event MWF.xApplication.process.Xform.DatatablePC#export
  146. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  147. * @example
  148. * <caption>this.event数据格式如下:</caption>
  149. * {
  150. * data : [
  151. * ["姓名","性别","学历","专业","出生日期","毕业日期"], //标题
  152. * [ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
  153. * [ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
  154. * ], //导出的数据
  155. * colWidthArray : [100, 50, 100, 200, 150, 150], //每列宽度
  156. * title : "xxxx" //导出的excel文件标题
  157. * }
  158. */
  159. /**
  160. * 在导入excel,进行数据校验后触发,this.event指向导入的数据。
  161. * @event MWF.xApplication.process.Xform.DatatablePC#validImport
  162. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  163. * @example
  164. * <caption>this.event数据格式如下:</caption>
  165. * {
  166. * data : [
  167. * {
  168. * "姓名" : "张三",
  169. * "性别" : "男",
  170. * "学历" : "大学本科",
  171. * "专业" : "计算机",
  172. * "出生日期" : "aa01-1-2",
  173. * "毕业日期" : "2019-9-2",
  174. * "errorTextList" : [
  175. * "第5列:aa01-1-2不是正确的日期格式。"
  176. * ] //校验出的错误信息,如果该行数据正确,则无该字段
  177. * }
  178. * ...
  179. * ], //导入的数据
  180. * "validted" : true //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
  181. * }
  182. */
  183. /**
  184. * 导入前触发。
  185. * @event MWF.xApplication.process.Xform.DatatablePC#beforeImport
  186. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  187. */
  188. /**
  189. * 在导入excel,数据校验成功将要设置回数据表格的时候触发,this.event指向整理过的导入数据,格式见{@link DatatableData}。
  190. * @event MWF.xApplication.process.Xform.DatatablePC#import
  191. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  192. */
  193. /**
  194. * 在导入excel,数据设置回数据表格以后触发,this.event指向整理过的导入数据,格式见{@link DatatableData}。
  195. * @event MWF.xApplication.process.Xform.DatatablePC#afterImport
  196. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  197. */
  198. "moduleEvents": ["queryLoad","postLoad","load", "afterLoad",
  199. "beforeLoadLine", "afterLoadLine", "change", "addLine", "deleteLine", "afterDeleteLine", "editLine",
  200. "completeLineEdit", "cancelLineEdit", "beforeExport", "export", "beforeImport", "import", "validImport", "afterImport"]
  201. },
  202. initialize: function(node, json, form, options){
  203. this.node = $(node);
  204. this.node.store("module", this);
  205. this.json = json;
  206. this.form = form;
  207. this.field = true;
  208. this.fieldModuleLoaded = false;
  209. },
  210. load: function(){
  211. this._loadModuleEvents();
  212. if (this.fireEvent("queryLoad")){
  213. this._queryLoaded();
  214. // if( this.isSectionMergeEdit() ){ //区段合并,删除区段值合并数据后编辑
  215. // if( this.json.mergeTypeEdit === "script" ){
  216. // this._loadMergeEditNodeByScript();
  217. // }else{
  218. // this._loadMergeEditNodeByDefault();
  219. // }
  220. // }else{
  221. // this._loadUserInterface();
  222. // }
  223. this._loadUserInterface();
  224. this._loadStyles();
  225. this._loadDomEvents();
  226. //this._loadEvents();
  227. this._afterLoaded();
  228. this.fireEvent("afterLoad");
  229. // this.fireEvent("load");
  230. }
  231. },
  232. _loadMergeEditNodeByScript: function(){
  233. if (this.json.sectionMergeEditScript && this.json.sectionMergeEditScript.code) {
  234. var data = this.form.Macro.exec(this.json.sectionMergeEditScript.code, this);
  235. this._setBusinessData( data );
  236. //this._loadUserInterface();
  237. }
  238. },
  239. _loadMergeEditNodeByDefault: function(){
  240. var data = this.getSortedSectionData();
  241. var businessData = [];
  242. data.each(function(d){
  243. d.data = d.data || {};
  244. businessData = businessData.concat( d.data.data || [] );
  245. });
  246. this._setBusinessData({
  247. data: businessData
  248. });
  249. //this._loadUserInterface();
  250. },
  251. _loadUserInterface: function(){
  252. //区段合并后编辑
  253. if( this.isSectionMergeEdit() ){ //区段合并,删除区段值合并数据后编辑
  254. if( this.json.mergeTypeEdit === "script" ){
  255. this._loadMergeEditNodeByScript();
  256. }else{
  257. this._loadMergeEditNodeByDefault();
  258. }
  259. }
  260. //区段合并展现
  261. this.isMergeRead = this.isSectionMergeRead();
  262. //启用区段且显示所有区段
  263. this.sectionBy = this._getSectionBy();
  264. this.isShowAllSection = this.isAllSectionShow();
  265. // this.editModules = [];
  266. // if( !layout.mobile ){
  267. // this.node.setStyle("overflow-x", "auto");
  268. // this.node.setStyle("overflow-y", "hidden");
  269. // }
  270. this.table = this.node.getElement("table");
  271. this.tBody = this.table.getElement("tbody");
  272. this.editable = !(this.readonly || (this.json.isReadonly === true) || (this.form.json.isReadonly === true));
  273. if( this.isMergeRead )this.editable = false;
  274. if (this.editable && this.json.editableScript && this.json.editableScript.code){
  275. this.editable = this.form.Macro.exec(((this.json.editableScript) ? this.json.editableScript.code : ""), this);
  276. }
  277. this.deleteable = this.json.deleteable !== "no";
  278. this.addable = this.json.addable !== "no";
  279. this.sortable = this.json.sortable !== "no";
  280. //允许导入
  281. this.importenable = this.editable && (this.json.impexpType === "impexp" || this.json.impexpType === "imp");
  282. //允许导出
  283. this.exportenable = this.json.impexpType === "impexp" || this.json.impexpType === "exp";
  284. //是否多行同时编辑
  285. this.multiEditMode = this.json.editMode === "multi";
  286. //是否有总计列
  287. this.totalFlag = false;
  288. this.totalColumns = [];
  289. this.totalNumberModuleIds = [];
  290. // this.hiddenColIndexList = [];
  291. if( this.isShowAllSection ){
  292. this.data = this._getAllSectionData()
  293. }else if( this.isMergeRead ){
  294. this.data = this.getSectionMergeReadData()
  295. }else{
  296. this.data = this.getValue();
  297. if( !this._getBusinessData() ){
  298. this.isNew = true;
  299. this._setValue(this.data);
  300. }
  301. }
  302. this.lineList = [];
  303. this.sectionlineList = [];
  304. this.loadDatatable();
  305. },
  306. /*
  307. * @summary 重新加载数据表格。
  308. * @example
  309. * this.form.get("fieldId").reload(); //重新加载
  310. */
  311. reload: function(){
  312. this.reloading = true;
  313. this._removeEl();
  314. // this.editModules = [];
  315. //是否有总计列
  316. this.totalFlag = false;
  317. this.totalColumns = [];
  318. this.totalNumberModuleIds = [];
  319. this.checkMerge( this.getValue() );
  320. this.clearSubModules();
  321. this.lineList = [];
  322. this.sectionlineList = [];
  323. this.loadDatatable();
  324. this.reloading = false;
  325. },
  326. loadDatatable: function(){
  327. this.loading = true;
  328. this._loadStyles();
  329. this._loadTitleTr();
  330. this._loadTemplate();
  331. this._loadTotalTr();
  332. this.fireEvent("load");
  333. this._loadDatatable(function(){
  334. this._loadImportExportAction();
  335. this.fieldModuleLoaded = true;
  336. this.loading = false;
  337. this.fireEvent("postLoad");
  338. }.bind(this));
  339. },
  340. _removeEl: function(){
  341. var node;
  342. if( this.titleTr ){
  343. node = this.titleTr.getElement("th.mwf_addlineaction");
  344. if( node )node.destroy();
  345. node = this.titleTr.getElement("th.mwf_moveaction");
  346. if( node )node.destroy();
  347. }
  348. if( this.templateTr ){
  349. node = this.templateTr.getElement("td.mwf_editaction");
  350. if( node )node.destroy();
  351. node = this.templateTr.getElement("td.mwf_moveaction");
  352. if( node )node.destroy();
  353. }
  354. if( this.totalTr ){
  355. this.totalTr.destroy();
  356. this.totalTr = null;
  357. }
  358. if( this.exportActionNode ){
  359. this.exportActionNode.destroy();
  360. this.totalTr = null;
  361. }
  362. if( this.importActionNode ){
  363. this.importActionNode.destroy();
  364. this.totalTr = null;
  365. }
  366. },
  367. _loadTitleTr: function(){
  368. this.titleTr = this.table.getElement("tr");
  369. var ths = this.titleTr.getElements("th");
  370. if (this.json.border){
  371. ths.setStyles({
  372. "border-bottom": this.json.border,
  373. "border-right": this.json.border
  374. });
  375. }
  376. if (this.json.titleStyles)ths.setStyles(this.json.titleStyles);
  377. //datatable$Title Module
  378. ths.each(function(th, index){
  379. var json = this.form._getDomjson(th);
  380. th.store("dataTable", this);
  381. th.addClass("mwf_origional");
  382. if (json){
  383. var module = this.form._loadModule(json, th);
  384. this.form.modules.push(module);
  385. if( json.isShow === false ){
  386. th.hide(); //隐藏列
  387. }else if( this.reloading && json.isShow === true){
  388. th.setStyle("display", "");
  389. }
  390. if((json.total === "number") || (json.total === "count"))this.totalFlag = true;
  391. }
  392. }.bind(this));
  393. if(this.editable){
  394. var actionTh = new Element("th.mwf_addlineaction", {"styles": {"width": "46px"}}).inject(this.titleTr, "top"); //操作列
  395. if(this.addable){
  396. var addLineAction = new Element("div.addLineAction.ooicon-create", {
  397. "styles": this.form.css.addLineAction,
  398. "events": {
  399. "click": function(e){
  400. if( this.json.addTo === "first" ){
  401. this._insertLineByIndex(e.target, 0);
  402. }else{
  403. this._addLine(e.target, true);
  404. }
  405. }.bind(this)
  406. }
  407. }).inject(actionTh);
  408. }
  409. var moveTh;
  410. if( this.sortable ){
  411. moveTh = new Element("th.mwf_moveaction", {"styles": this.form.css.gridMoveActionCell || {}}).inject(this.titleTr, "bottom"); //总计列
  412. }
  413. if (this.json.border){
  414. Array.each([actionTh,moveTh], function(th){
  415. if(th)th.setStyles({
  416. "border-bottom": this.json.border,
  417. "border-right": this.json.border
  418. })
  419. }.bind(this));
  420. }
  421. if (this.json.titleStyles){
  422. Object.each(this.json.titleStyles, function( value, key ){
  423. if( key && key.toLowerCase() !== "width" ){
  424. actionTh.setStyle(key, value);
  425. if(moveTh)moveTh.setStyle(key, value);
  426. }
  427. });
  428. }
  429. }
  430. },
  431. _loadTemplate: function(){
  432. // this.templateJson = {};
  433. var trs = this.table.getElements("tr");
  434. this.templateTr = trs[trs.length-1];
  435. this.templateNode = this.templateTr;
  436. var tds = this.templateNode.getElements("td");
  437. this.columnCount = tds.length;
  438. if (this.json.border) {
  439. tds.setStyles({
  440. "border-bottom": this.json.border,
  441. "border-right": this.json.border,
  442. "background": "transparent"
  443. });
  444. }
  445. if (this.json.contentStyles)tds.setStyles(this.json.contentStyles);
  446. //datatable$Data Module
  447. tds.each(function(td, index){
  448. var json = this.form._getDomjson(td);
  449. td.store("dataTable", this);
  450. td.addClass("mwf_origional");
  451. if (json){
  452. // var module = this.form._loadModule(json, td);
  453. // this.form.modules.push(module);
  454. if( json.cellType === "sequence" )td.addClass("mwf_sequence"); //序号列
  455. if( json.isShow === false ){
  456. td.hide(); //隐藏列
  457. }else if( this.reloading && json.isShow === true){
  458. td.setStyle("display", "");
  459. }
  460. }
  461. }.bind(this));
  462. if(this.editable){
  463. var eTd = new Element("td.mwf_editaction",{"styles": this.json.actionStyles || {}}).inject(this.templateNode, "top"); //操作列
  464. this.columnCount = this.columnCount+1;
  465. var mTd;
  466. if( this.sortable ){
  467. mTd= new Element("td.mwf_moveaction", {"styles": this.form.css.gridMoveActionCell || {}}).inject(this.templateNode, "bottom");
  468. this.columnCount = this.columnCount+1;
  469. } //排序列
  470. if (this.json.border){
  471. Array.each([eTd,mTd], function(td){
  472. if(td)td.setStyles({
  473. "border-bottom": this.json.border,
  474. "border-right": this.json.border,
  475. "background": "transparent"
  476. })
  477. }.bind(this));
  478. }
  479. if (this.json.contentStyles){
  480. Object.each(this.json.contentStyles, function( value, key ){
  481. if( key && key.toLowerCase() !== "width" ){
  482. eTd.setStyle(key, value);
  483. if(mTd)mTd.setStyle(key, value);
  484. }
  485. });
  486. // eTd.setStyles(this.json.contentStyles);
  487. // if(mTd)mTd.setStyles(this.json.contentStyles);
  488. }
  489. }
  490. this.templateHtml = this.templateNode.get("html");
  491. // var moduleNodes = this.form._getModuleNodes(this.templateNode);
  492. // moduleNodes.each(function (node) {
  493. // if (node.get("MWFtype") !== "form") {
  494. // var json = this.form._getDomjson(node);
  495. // this.templateJson[json.id] = json ;
  496. // }
  497. // }.bind(this));
  498. this.templateNode.hide();
  499. },
  500. _loadTotalTr: function(){
  501. if( !this.totalFlag )return;
  502. this.totalTr = new Element("tr.mwf_totaltr", {"styles": this.form.css.datagridTotalTr}).inject(this.tBody||this.table);
  503. if( this.isShowSectionKey() && !(this.json.totalRowBySection || [] ).contains("module")){
  504. this.totalTr.hide()
  505. }
  506. if( this.isShowSectionBy() && !(this.json.totalRowBy || [] ).contains("module") ){
  507. this.totalTr.hide()
  508. }
  509. var ths = this.titleTr.getElements("th");
  510. //datatable$Title Module
  511. ths.each(function(th, index){
  512. var td = new Element("td", {"text": "", "styles": this.form.css.datagridTotalTd}).inject(this.totalTr);
  513. if (this.json.amountStyles) td.setStyles(this.json.amountStyles);
  514. var json = this.form._getDomjson(th);
  515. if (json){
  516. if( json.isShow === false ){
  517. td.hide(); //隐藏列
  518. }else if( this.reloading && json.isShow === true){
  519. td.setStyle("display", "");
  520. }
  521. if ((json.total === "number") || (json.total === "count")){
  522. this.totalColumns.push({
  523. "th" : th,
  524. "td" : td,
  525. "index": index,
  526. "type": json.total
  527. })
  528. }
  529. }
  530. }.bind(this));
  531. var tds = this.templateTr.getElements("td");
  532. //datatable$Data Module
  533. tds.each(function(td, index){
  534. var json = this.form._getDomjson(td);
  535. if (json){
  536. //总计列
  537. var tColumn = this.totalColumns.find(function(a){ return a.index === index });
  538. if(tColumn){
  539. var moduleNodes = this.form._getModuleNodes(td); //获取总计列内的填写组件
  540. if( moduleNodes.length > 0 ){
  541. tColumn.moduleJson = this.form._getDomjson(moduleNodes[0]);
  542. if(tColumn.type === "number")this.totalNumberModuleIds.push( tColumn.moduleJson.id );
  543. }
  544. }
  545. }
  546. }.bind(this));
  547. },
  548. _getTotalTr: function(){
  549. return this.totalTr;
  550. },
  551. _loadTotal: function(){
  552. var totalData = {};
  553. if (!this.totalFlag)return totalData;
  554. if (!this._getTotalTr())this._loadTotalTr();
  555. var data;
  556. if( this.isShowAllSection ){
  557. data = { data : [] };
  558. Object.each( this.getBusinessDataById(), function (d, key) {
  559. if( !["data","total"].contains(key) ){
  560. data.data = data.data.concat( d.data )
  561. }
  562. })
  563. }else if( this.isMergeRead ){
  564. data = this.data;
  565. }else{
  566. data = this.getValue();
  567. }
  568. this.totalColumns.each(function(column, index){
  569. var json = column.moduleJson;
  570. if(!json)return;
  571. var total = this._loadColumnTotal( column, data );
  572. if( typeOf(total) !== "null" )totalData[json.id] = total;
  573. }.bind(this));
  574. data.total = totalData;
  575. return totalData;
  576. },
  577. _loadColumnTotal: function(column, data){
  578. var json = column.moduleJson;
  579. if(!json)return;
  580. var pointLength = 0; //小数点后的最大数位
  581. var tmpV;
  582. if (column.type === "count"){
  583. tmpV = data.data.length;
  584. }else if(column.type === "number"){
  585. tmpV = new Decimal(0);
  586. for (var i=0; i<data.data.length; i++){
  587. var d = data.data[i];
  588. if(d[json.id]){
  589. var v = this.formatDecimals( json, d[json.id].toFloat() );
  590. tmpV = tmpV.plus(v ? v.toFloat() : 0);
  591. if( v.indexOf(".") > -1 ){
  592. pointLength = Math.max(pointLength, v.split(".")[1].length);
  593. }
  594. }
  595. }
  596. }
  597. if( isNaN( tmpV ) ) {
  598. column.td.set("text", "");
  599. return;
  600. }
  601. var s = tmpV.toString(), total;
  602. if( json.decimals && (json.decimals!=="*")){
  603. total = this.formatDecimals( json, s.toFloat());
  604. }else if( pointLength <= 0 || s === "0" ){
  605. total = s;
  606. }else if( s.indexOf(".") > -1 ){
  607. var length = s.split(".")[1].length;
  608. total = length < pointLength ? (s + "0".repeat(pointLength-length)) : s
  609. }else{
  610. total = s +"."+ "0".repeat(pointLength);
  611. }
  612. column.td.set("text", this.formatSeparate( json, total ) );
  613. if( json.currencySymbol ){
  614. new Element("span", {"text": json.currencySymbol, "style":"padding-right:5px"}).inject( column.td, "top" );
  615. }
  616. return total;
  617. },
  618. formatDecimals: function( json, v ){
  619. var str;
  620. if (json.decimals && (json.decimals!=="*")) { //小数点数位
  621. var decimals = json.decimals.toInt();
  622. var p = Math.pow(10, decimals);
  623. var f_x = Math.round(v * p) / p;
  624. str = f_x.toString();
  625. if (decimals > 0) {
  626. var pos_decimal = str.indexOf('.');
  627. if (pos_decimal < 0) {
  628. pos_decimal = str.length;
  629. str += '.';
  630. }
  631. var decimalStr = (str).substr(pos_decimal + 1, (str).length);
  632. while (decimalStr.length < decimals) {
  633. str += '0';
  634. decimalStr += 0;
  635. }
  636. }
  637. }
  638. return str || v.toString();
  639. },
  640. formatSeparate: function(json, str){
  641. if( typeOf( str ) === "number" )str = str.toString();
  642. if( json.digitsToSeparate && parseInt(json.digitsToSeparate) > 1 ){
  643. var digits = parseInt(json.digitsToSeparate);
  644. var reg = new RegExp( "(\\d{"+digits+"}\\B)" ,"g");
  645. var arr = str.split(".");
  646. var i = arr[0].split("").reverse().join("")
  647. .replace(reg, "$1,")
  648. .split("").reverse().join("");
  649. str = arr.length > 1 ? ( i + "." + arr[1] ) : i ;
  650. }
  651. return str;
  652. },
  653. isTotalNumberModule: function( id ){
  654. return this.totalNumberModuleIds.contains(id)
  655. },
  656. isShowSectionKey: function(){
  657. return this.json.showSectionKey && this.isMergeRead ;
  658. },
  659. isShowSectionBy: function(){
  660. return this.json.showSectionBy && this.isShowAllSection ;
  661. },
  662. isSectionData: function(){ //数据是否经过区段处理
  663. var data = this.getBusinessDataById();
  664. if( o2.typeOf( data ) === "object" ){
  665. var keys = Object.keys(data);
  666. for( var i=0; i<keys.length; i++ ){
  667. var key = keys[i];
  668. if( key !== "data" && key !== "total" ){
  669. if( o2.typeOf(data[key]) === "object" && o2.typeOf(data[key].data) === "array" ){
  670. return true;
  671. }
  672. }
  673. }
  674. }
  675. return false;
  676. },
  677. /**
  678. * @summary 当数据表格设置为区段合并展现、区段合并编辑时,可以使用本方法获取所有区段数据。
  679. * @return {Object} 对象.
  680. * @example
  681. * var data = this.form.get("fieldId").getAllSectionData();
  682. * //data格式如下:
  683. * {
  684. * "3455b82a-399c-4ee4-b9b9-e70ae40fbaf1": { //区段1的key和data
  685. * "data": [
  686. * {
  687. * "good": "yf",
  688. * "number_2": 11,
  689. * "prize": 1
  690. * }
  691. * ]
  692. * },
  693. * "83de86fc-60bc-4b4c-955c-1085915865a4": { //区段2的key和data
  694. * "data": [
  695. * {
  696. * "good": "yf",
  697. * "number_2": 11,
  698. * "prize": 10
  699. * }
  700. * ]
  701. * }
  702. * }
  703. */
  704. getAllSectionData: function(){
  705. return this.getBusinessDataById();
  706. },
  707. _getAllSectionData: function(){
  708. var bData = this.getBusinessDataById();
  709. var flag = false;
  710. if( !bData ){
  711. flag = true;
  712. bData = {};
  713. }
  714. if( !bData[this.sectionBy] ){
  715. flag = true;
  716. this.isNew = true;
  717. bData[this.sectionBy] = this.getValue();
  718. }
  719. if( flag )this.setBusinessDataById( bData );
  720. this.dataWithSectionBy = this.getAllSortedSectionData();
  721. return flag ? this.getBusinessDataById() : bData;
  722. },
  723. getAllSortedSectionData: function(){ //获取合并排序后的数据
  724. var data = this.getBusinessDataById();
  725. var array = [];
  726. for( var key in data ){
  727. if( !["data","total"].contains(key) )array.push({
  728. sectionKey: key,
  729. key: key,
  730. data: data[key]
  731. })
  732. }
  733. if( this.json.sectionDisplaySortScript && this.json.sectionDisplaySortScript.code){
  734. array.sort( function(a, b){
  735. this.form.Macro.environment.event = {
  736. "a": a,
  737. "b": b
  738. };
  739. var flag = this.form.Macro.exec(this.json.sectionDisplaySortScript.code, this);
  740. this.form.Macro.environment.event = null;
  741. return flag;
  742. }.bind(this))
  743. }
  744. return array;
  745. },
  746. getSortedSectionData: function(){ //获取合并排序后的数据
  747. var data = this.getBusinessDataById();
  748. var array = [];
  749. for( var key in data ){
  750. if( !["data","total"].contains(key) )array.push({
  751. sectionKey: key,
  752. key: key,
  753. data: data[key]
  754. });
  755. }
  756. if( this.json.sectionMergeSortScript && this.json.sectionMergeSortScript.code){
  757. array.sort( function(a, b){
  758. this.form.Macro.environment.event = {
  759. "a": a,
  760. "b": b
  761. };
  762. var flag = this.form.Macro.exec(this.json.sectionMergeSortScript.code, this);
  763. this.form.Macro.environment.event = null;
  764. return flag;
  765. }.bind(this))
  766. }
  767. return array;
  768. },
  769. getSectionMergeReadData: function(){
  770. switch (this.json.mergeTypeRead) {
  771. case "dataScript":
  772. if (this.json.sectionMergeReadDataScript && this.json.sectionMergeReadDataScript.code) {
  773. return this.form.Macro.exec(this.json.sectionMergeReadDataScript.code, this);
  774. }else{
  775. return {"data":[]};
  776. }
  777. default:
  778. var sortedData = this.getSortedSectionData();
  779. if( this.json.showSectionKey ){
  780. this.dataWithSectionKey = sortedData;
  781. }
  782. var data = [];
  783. //把区段值放在每行的数据里
  784. sortedData.each(function(d){
  785. ( d.data.data || [] ).each(function( obj ){
  786. if( o2.typeOf(obj) === "object" ){
  787. // obj.sectionKey = d.sectionKey;
  788. data.push( obj )
  789. }
  790. });
  791. // data = data.concat( d.data.data );
  792. });
  793. return { "data": data };
  794. }
  795. },
  796. _createLineNode: function( beforeNode ){
  797. var tr;
  798. if( beforeNode ) {
  799. tr = new Element("tr").inject(beforeNode, "after");
  800. }else if( this.totalTr ){
  801. tr = new Element("tr").inject(this.totalTr, "before");
  802. }else{
  803. tr = new Element("tr").inject(this.tBody || this.table);
  804. }
  805. return tr;
  806. },
  807. _checkAddAction: function(){
  808. },
  809. _loadStyles: function(){
  810. if (this.json.border) {
  811. this.table.setStyles({
  812. "border-top": this.json.border,
  813. "border-left": this.json.border
  814. });
  815. }
  816. if( this.json.recoveryStyles ){
  817. this.node.setStyles(this.json.recoveryStyles);
  818. }
  819. this.node.setStyles(this.json.styles);
  820. this.table.setStyles(this.json.tableStyles);
  821. this.table.set(this.json.properties);
  822. },
  823. getDefaultValue: function(){
  824. var value;
  825. if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
  826. if (value && !value.then) if (o2.typeOf(value)==="array") value = {"data": value || [], "total":{}};
  827. if(!value && this.multiEditMode){
  828. value = {"data": [], "total":{}};
  829. var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
  830. for( var i=0; i<count; i++ ){
  831. value.data.push({})
  832. }
  833. }
  834. return value;
  835. },
  836. _getValue: function(){
  837. if (this.moduleValueAG) return this.moduleValueAG;
  838. var value = this._getBusinessData();
  839. if (!value){
  840. value = this.getDefaultValue();
  841. }
  842. return value || {"data": [], "total":{}};
  843. },
  844. getValue: function(){
  845. return this._getValue();
  846. },
  847. _setValue: function(value){
  848. if (!!value && o2.typeOf(value.then)=="function"){
  849. var p = o2.promiseAll(value).then(function(v){
  850. this.__setValue(v);
  851. }.bind(this), function(){});
  852. this.moduleValueAG = p;
  853. p.then(function(){
  854. this.moduleValueAG = null;
  855. }.bind(this), function(){
  856. this.moduleValueAG = null;
  857. }.bind(this));
  858. }else{
  859. this.moduleValueAG = null;
  860. this.__setValue(value);
  861. }
  862. },
  863. __setValue: function(value){
  864. this._setBusinessData(value);
  865. this.moduleValueAG = null;
  866. return value;
  867. },
  868. _loadDatatable: function(callback, operation){
  869. //operation 可能为setLineData, addLine, insertLine, deleteLines, deleteLine, moveUpList
  870. var p = o2.promiseAll(this.data).then(function(v){
  871. this.data = v;
  872. if( this.isShowAllSection ){
  873. this._loadSectionLineList_EditSection(function(){
  874. this._checkAddAction();
  875. this._loadTotal();
  876. if(callback)callback();
  877. }.bind(this), operation)
  878. }else if( this.isShowSectionKey() ){
  879. this._loadSectionLineList(function(){
  880. this._checkAddAction();
  881. this._loadTotal();
  882. if(callback)callback();
  883. }.bind(this), operation)
  884. }else{
  885. this._loadLineList(function(){
  886. this._checkAddAction();
  887. this._loadTotal();
  888. if(callback)callback();
  889. }.bind(this), operation);
  890. }
  891. this.moduleValueAG = null;
  892. return v;
  893. }.bind(this), function(){
  894. this.moduleValueAG = null;
  895. }.bind(this));
  896. this.moduleValueAG = p;
  897. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  898. this.moduleValueAG = null;
  899. }.bind(this), function(){
  900. this.moduleValueAG = null;
  901. }.bind(this));
  902. },
  903. _loadSectionLineList_EditSection: function(callback, operation){
  904. var map = this.unchangedSectionLineMap || {};
  905. // Object.each(map, function (sline, idx) {
  906. // sline.setIndex( idx.toInt(), (idx.toInt()+1).toString() );
  907. // });
  908. this.dataWithSectionBy.each(function(data, idx){
  909. var isEdited = false;
  910. if( this.isSectionLineEditable( data ) && this.editable ){
  911. isEdited = true;
  912. }
  913. var isNew = false;
  914. var sectionLine, beforeNode = idx > 0 ? this.sectionlineList[idx-1].getLastTr() : this.templateNode;
  915. if( map[data.sectionKey] ) {
  916. sectionLine = map[data.sectionKey];
  917. sectionLine.setIndex( beforeNode, data, idx, isEdited, isNew, operation );
  918. }else{
  919. var node = this._createLineNode( beforeNode );
  920. sectionLine = this._loadSectionLine_EditSection(node, data, idx, isEdited, isNew);
  921. }
  922. if( this.sectionBy && this.sectionBy === data.sectionKey ){
  923. this.sectionLineEdited = sectionLine;
  924. }
  925. this.sectionlineList.push(sectionLine);
  926. }.bind(this))
  927. if (callback) callback();
  928. },
  929. _loadSectionLine_EditSection: function(container, data, index, isEdited, isNew){
  930. var sectionLine = new MWF.xApplication.process.Xform.DatatablePC.SectionLine(container, this, data, {
  931. index : index,
  932. indexText : (index+1).toString(),
  933. isNew: isNew,
  934. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.editable,
  935. isEditable: this.editable && this.isSectionLineEditable(data),
  936. isDeleteable: this.deleteable && this.isSectionLineEditable(data),
  937. isAddable: this.addable && this.isSectionLineEditable(data)
  938. });
  939. // this.fireEvent("beforeLoadLine", [line]);
  940. sectionLine.load();
  941. // this.fireEvent("afterLoadLine", [line]);
  942. return sectionLine;
  943. },
  944. isSectionLineEditable: function(data){
  945. return this.isShowAllSection && this.sectionBy && this.sectionBy === data.sectionKey;
  946. },
  947. _loadSectionLineList: function(callback, operation){
  948. var map = this.unchangedSectionLineMap || {};
  949. this.dataWithSectionKey.each(function(data, idx){
  950. var sectionLine;
  951. var isEdited = false;
  952. var isNew = false;
  953. var beforeNode = idx > 0 ? this.sectionlineList[idx-1].getLastTr() : this.templateNode;
  954. if( map[data.sectionKey] ) {
  955. sectionLine = map[data.sectionKey];
  956. sectionLine.setIndex( beforeNode, data, idx, isEdited, isNew, operation );
  957. }else {
  958. var node = this._createLineNode( beforeNode );
  959. sectionLine = this._loadSectionLine(node, data, idx, isEdited, isNew);
  960. }
  961. this.sectionlineList.push(sectionLine);
  962. }.bind(this))
  963. if (callback) callback();
  964. },
  965. _loadSectionLine: function(container, data, index, isEdited, isNew){
  966. var sectionLine = new MWF.xApplication.process.Xform.DatatablePC.SectionLine(container, this, data, {
  967. index : index,
  968. indexText : (index+1).toString(),
  969. isNew: isNew,
  970. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.editable,
  971. isEditable: this.editable,
  972. isDeleteable: this.deleteable,
  973. isAddable: this.addable,
  974. isMergeRead: this.isMergeRead
  975. });
  976. // this.fireEvent("beforeLoadLine", [line]);
  977. sectionLine.load();
  978. // this.fireEvent("afterLoadLine", [line]);
  979. return sectionLine;
  980. },
  981. _loadLineList: function(callback, operation){
  982. var map = this.unchangedLineMap || {};
  983. Object.each(map, function (line, idx) {
  984. line.setIndex( idx.toInt() );
  985. });
  986. this.data.data.each(function(data, idx){
  987. if( !data )return;
  988. var idxStr = idx.toString();
  989. var beforeNode = idx > 0 ? this.lineList[idx - 1].node : this.templateNode;
  990. if( map[idxStr] ){
  991. if( !operation || operation === "moveUpList" ){
  992. map[idxStr].node.inject( beforeNode, "after" );
  993. }
  994. this.lineList.push( map[idxStr] );
  995. }else{
  996. var isNew = this.isNew || (o2.typeOf(this.newLineIndex) === "number" ? idx === this.newLineIndex : false);
  997. var isEdited = (!this.multiEditMode && o2.typeOf(this.newLineIndex) === "number") ? idx === this.newLineIndex : this.multiEditMode;
  998. var node = this._createLineNode( beforeNode );
  999. var line = this._loadLine(node, data, idx, isEdited, isNew );
  1000. this.lineList.push(line);
  1001. }
  1002. }.bind(this));
  1003. this.isNew = false;
  1004. this.newLineIndex = null;
  1005. if (callback) callback();
  1006. },
  1007. _loadLine: function(container, data, index, isEdited, isNew){
  1008. var line = new MWF.xApplication.process.Xform.DatatablePC.Line(container, this, data, {
  1009. index : index,
  1010. indexText : (index+1).toString(),
  1011. isNew: isNew,
  1012. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.editable,
  1013. isEditable: this.editable,
  1014. isDeleteable: this.deleteable,
  1015. isAddable: this.addable,
  1016. isMergeRead: this.isMergeRead
  1017. });
  1018. this.fireEvent("beforeLoadLine", [line]);
  1019. line.load();
  1020. this.fireEvent("afterLoadLine", [line]);
  1021. return line;
  1022. },
  1023. isMax : function(){
  1024. var maxCount = this.json.maxCount ? this.json.maxCount.toInt() : 0;
  1025. if( this.editable && maxCount > 0 ) {
  1026. if( this.isShowAllSection ){
  1027. if( this.sectionLineEdited && this.sectionLineEdited.lineList.length >= maxCount )return true;
  1028. }else{
  1029. if( this.lineList.length >= maxCount )return true;
  1030. }
  1031. }
  1032. return false;
  1033. },
  1034. isMin : function(){
  1035. var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
  1036. if( this.editable && minCount > 0 ) {
  1037. if( this.isShowAllSection ){
  1038. if( this.sectionLineEdited && this.sectionLineEdited.lineList.length <= minCount )return true;
  1039. }else {
  1040. if (this.lineList.length <= minCount) return true;
  1041. }
  1042. }
  1043. return false;
  1044. },
  1045. _setLineData: function(line, d){
  1046. if( line.sectionLine ){
  1047. var data = this.getBusinessDataById();
  1048. var sdata = data[ line.sectionLine.sectionKey ];
  1049. if( sdata && sdata.data ){
  1050. sdata.data[line.options.indexInSectionLine] = d;
  1051. this.setAllSectionData( data, false, "setLineData" );
  1052. }
  1053. }else{
  1054. var index = line.options.index;
  1055. var data = this.getInputData();
  1056. data.data[index] = d;
  1057. this.setData( data, false, "setLineData" );
  1058. }
  1059. },
  1060. _addLine: function(ev, edited, d){
  1061. if( !this._completeLineEdit(ev, true) )return;
  1062. if( this.isMax() ){
  1063. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  1064. this.form.notice(text,"info");
  1065. return false;
  1066. }
  1067. var data, index, line;
  1068. if( this.isShowAllSection ){
  1069. data = this.getBusinessDataById();
  1070. var sdata = data[ this.sectionBy ];
  1071. if( !sdata ){
  1072. sdata = data[ this.sectionBy ] = { data: [] };
  1073. }
  1074. index = sdata.data.length;
  1075. // if( d && !d.sectionKey )d.sectionKey = this.sectionBy;
  1076. // sectionKey: this.sectionBy
  1077. sdata.data.push(d||{});
  1078. this.newLineIndex = index;
  1079. this.setAllSectionData( data, false, "addLine");
  1080. line = this.sectionLineEdited.lineList[index];
  1081. line.isNewAdd = true;
  1082. }else{
  1083. index = this.lineList.length;
  1084. data = this.getInputData();
  1085. data.data.push(d||{});
  1086. this.newLineIndex = index;
  1087. this.setData( data , false, "addLine");
  1088. line = this.getLine(index);
  1089. line.isNewAdd = true;
  1090. }
  1091. this.validationMode();
  1092. this.fireEvent("addLine", [{"line":line, "ev":ev}]);
  1093. this.fireEvent("change", [{"lines":[line], "type":"addline"}]);
  1094. return line;
  1095. },
  1096. _insertLine: function(ev, beforeLine){
  1097. if( !this._completeLineEdit(ev, true) )return;
  1098. if( this.isMax() ){
  1099. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  1100. this.form.notice(text,"info");
  1101. return false;
  1102. }
  1103. //使用数据驱动
  1104. var data, index, line;
  1105. if( this.isShowAllSection ){
  1106. index = beforeLine.options.indexInSectionLine + 1;
  1107. data = this.getBusinessDataById();
  1108. var sdata = data[ this.sectionBy ];
  1109. if( !sdata ){
  1110. sdata = data[ this.sectionBy ] = { data: [] };
  1111. }
  1112. sdata.data.splice(index, 0, {});
  1113. this.newLineIndex = index;
  1114. this.setAllSectionData( data, false, "insertLine");
  1115. line = this.sectionLineEdited.lineList[index];
  1116. line.isNewAdd = true;
  1117. }else {
  1118. index = beforeLine.options.index + 1;
  1119. data = this.getInputData();
  1120. data.data.splice(index, 0, {});
  1121. this.newLineIndex = index;
  1122. this.setData(data, false, "insertLine");
  1123. line = this.getLine(index);
  1124. line.isNewAdd = true;
  1125. }
  1126. this.validationMode();
  1127. this.fireEvent("addLine",[{"line":line, "ev":ev}]);
  1128. this.fireEvent("change", [{"lines":[line], "type":"addline"}]);
  1129. return line;
  1130. },
  1131. _insertLineByIndex: function(ev, index, d){
  1132. if( !this._completeLineEdit(ev, true) )return;
  1133. if( this.isMax() ){
  1134. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  1135. this.form.notice(text,"info");
  1136. return false;
  1137. }
  1138. var data, line;
  1139. if( this.isShowAllSection ){
  1140. data = this.getBusinessDataById();
  1141. var sdata = data[ this.sectionBy ];
  1142. if( !sdata ){
  1143. sdata = data[ this.sectionBy ] = { data: [] };
  1144. }
  1145. if (sdata.data.length < index) return null;
  1146. sdata.data.splice(index, 0, d || {});
  1147. this.newLineIndex = index;
  1148. this.setAllSectionData( data , false, "insertLine" );
  1149. line = this.sectionLineEdited.lineList[index];
  1150. line.isNewAdd = true;
  1151. }else {
  1152. //使用数据驱动
  1153. data = this.getInputData();
  1154. if (data.data.length < index) return null;
  1155. data.data.splice(index, 0, d || {});
  1156. this.newLineIndex = index;
  1157. this.setData(data, false, "insertLine");
  1158. line = this.getLine(index);
  1159. line.isNewAdd = true;
  1160. }
  1161. this.validationMode();
  1162. this.fireEvent("addLine",[{"line":line, "ev":ev}]);
  1163. this.fireEvent("change", [{"lines":[line], "type":"addline"}]);
  1164. return line;
  1165. },
  1166. _deleteSelectedLine: function(ev){
  1167. var selectedLine = this.lineList.filter(function (line) { return line.selected; });
  1168. if( selectedLine.length === 0 ){
  1169. this.form.notice( MWF.xApplication.process.Xform.LP.selectItemNotice,"info");
  1170. return false;
  1171. }
  1172. var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
  1173. if( minCount > 0 ){
  1174. if( this.lineList.length - selectedLine.length < minCount ){
  1175. var text = MWF.xApplication.process.Xform.LP.minItemNotice.replace("{n}", minCount );
  1176. this.form.notice(text,"info");
  1177. return false;
  1178. }
  1179. }
  1180. var _self = this;
  1181. this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteSelectedItemNotice, 300, 120, function(){
  1182. _self._delLines( selectedLine );
  1183. this.close();
  1184. }, function(){
  1185. this.close();
  1186. }, null, null, this.form.json.confirmStyle);
  1187. },
  1188. _delLines: function(lines){
  1189. var _self = this;
  1190. var saveFlag = false;
  1191. var data;
  1192. if( this.isShowAllSection ){
  1193. data = this.getBusinessDataById();
  1194. }else{
  1195. data = _self.getInputData();
  1196. }
  1197. lines.reverse().each(function(line){
  1198. _self.fireEvent("deleteLine", [line]);
  1199. if(line.deleteAttachment())saveFlag = true;
  1200. if( line.sectionLine ){
  1201. var d = data[ line.sectionLine.sectionKey ];
  1202. if( d && d.data ){
  1203. d.data.splice(line.options.indexInSectionLine, 1);
  1204. if(this.currentEditedLine === line)this.currentEditedLine = null;
  1205. }
  1206. }else {
  1207. data.data.splice(line.options.index, 1);
  1208. if (this.currentEditedLine === line) this.currentEditedLine = null;
  1209. }
  1210. _self.fireEvent("afterDeleteLine");
  1211. });
  1212. if( this.isShowAllSection ){
  1213. _self.setAllSectionData(data, false, "deleteLines");
  1214. }else{
  1215. _self.setData( data , false, "deleteLines");
  1216. }
  1217. this.validationMode();
  1218. _self.fireEvent("change", [{"lines":lines, "type":"deletelines"}]);
  1219. if(saveFlag)this.saveFormData();
  1220. },
  1221. _deleteLine: function(ev, line){
  1222. if( !this._completeLineEdit(ev, true) )return;
  1223. if( this.isMin() ){
  1224. var text = MWF.xApplication.process.Xform.LP.minItemCountNotice.replace("{n}", this.json.minCount );
  1225. this.form.notice(text,"info");
  1226. return false;
  1227. }
  1228. var _self = this;
  1229. this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteDatagridLine, 300, 120, function(){
  1230. _self._delLine(line);
  1231. this.close();
  1232. }, function(){
  1233. this.close();
  1234. }, null, null, this.form.json.confirmStyle);
  1235. },
  1236. _delLine: function(line){
  1237. this.fireEvent("deleteLine", [line]);
  1238. var saveFlag = line.deleteAttachment();
  1239. //使用数据驱动
  1240. var data;
  1241. if( line.sectionLine ){
  1242. var data = this.getBusinessDataById();
  1243. var d = data[ line.sectionLine.sectionKey ];
  1244. if( d && d.data ){
  1245. d.data.splice(line.options.indexInSectionLine, 1);
  1246. }
  1247. if(this.currentEditedLine === line)this.currentEditedLine = null;
  1248. this.setAllSectionData( data, false, "deleteLine" );
  1249. }else{
  1250. data = this.getInputData();
  1251. data.data.splice(line.options.index, 1);
  1252. if(this.currentEditedLine === line)this.currentEditedLine = null;
  1253. this.setData( data , false, "deleteLine");
  1254. }
  1255. this.validationMode();
  1256. this.fireEvent("afterDeleteLine");
  1257. this.fireEvent("change", [{"lines":[line], "type":"deleteline"}]);
  1258. if(saveFlag)this.saveFormData();
  1259. },
  1260. _cancelLineEdit: function(){
  1261. var line = this.currentEditedLine;
  1262. if( !line )return true;
  1263. line.validationMode();
  1264. if( line.isNewAdd ){
  1265. // var saveFlag = line.deleteAttachment();
  1266. this._delLine( line );
  1267. this.currentEditedLine = null;
  1268. // if(saveFlag)this.form.saveFormData();
  1269. }else{
  1270. // line.data = Object.clone(line.getOriginalDataWithCheckAttachment());
  1271. line.resetDataWithOriginalData();
  1272. line.changeEditMode(false);
  1273. this._loadTotal();
  1274. if( line.sectionLine )line.sectionLine._loadTotal();
  1275. if(line.attachmentChangeFlag){
  1276. this.saveFormData();
  1277. line.attachmentChangeFlag = false;
  1278. }
  1279. this.currentEditedLine = null;
  1280. this.fireEvent("cancelLineEdit", [line]);
  1281. }
  1282. return true;
  1283. },
  1284. _completeLineEdit: function( ev, fireChange, ignoerSave ){
  1285. var line = this.currentEditedLine;
  1286. if( !line )return true;
  1287. if( !line.validation() )return false;
  1288. var originalDataStr, dataStr;
  1289. if( fireChange ){
  1290. if( line.originalData && o2.typeOf(line.originalData) === "object"){
  1291. originalDataStr = JSON.stringify(line.originalData)
  1292. }
  1293. dataStr = JSON.stringify(line.data);
  1294. }
  1295. line.isNewAdd = false;
  1296. // line.data = line.getData();
  1297. line.computeModuleData("save");
  1298. line.originalData = Object.clone(line.data);
  1299. line.changeEditMode(false);
  1300. this._loadTotal();
  1301. if( line.sectionLine )line.sectionLine._loadTotal();
  1302. if(line.attachmentChangeFlag && !ignoerSave){
  1303. this.saveFormData();
  1304. line.attachmentChangeFlag = false;
  1305. }
  1306. this.currentEditedLine = null;
  1307. this.validationMode();
  1308. this.fireEvent("completeLineEdit", [line]);
  1309. if( fireChange && originalDataStr !== dataStr ){
  1310. this.fireEvent("change", [{"lines":[line], "type":"editcomplete"}]);
  1311. }
  1312. return true;
  1313. },
  1314. _moveUpLine: function(ev, line){
  1315. if( this.currentEditedLine && !this._completeLineEdit(null, true) )return false;
  1316. var data, upData, curData;
  1317. if( this.isShowAllSection ){
  1318. if (line.options.indexInSectionLine === 0) return;
  1319. data = this.getBusinessDataById();
  1320. var sdata = data[ this.sectionBy ];
  1321. if( !sdata )return;
  1322. upData = sdata.data[line.options.indexInSectionLine - 1];
  1323. curData = sdata.data[line.options.indexInSectionLine];
  1324. sdata.data[line.options.indexInSectionLine] = upData;
  1325. sdata.data[line.options.indexInSectionLine - 1] = curData;
  1326. this.setAllSectionData( data, false, "moveUpList" );
  1327. }else {
  1328. if (line.options.index === 0) return;
  1329. data = this.getInputData();
  1330. upData = data.data[line.options.index - 1];
  1331. curData = data.data[line.options.index];
  1332. data.data[line.options.index] = upData;
  1333. data.data[line.options.index - 1] = curData;
  1334. this.setData(data, false, "moveUpList");
  1335. }
  1336. this.fireEvent("change", [{lines: this.lineList, "type":"move"}]);
  1337. },
  1338. _changeEditedLine: function(line){
  1339. if( this.currentEditedLine ){
  1340. if( line === this.currentEditedLine )return;
  1341. if( !this._completeLineEdit( null,true ) )return;
  1342. }
  1343. line.changeEditMode(true);
  1344. /**
  1345. * 数据表格当前正在编辑的条目,当数据表格为“同时编辑多行”时无此属性。
  1346. * @member {MWF.xApplication.process.Xform.DatatablePC.Line | MWF.xApplication.process.Xform.DatatableMobile.Line | Null}
  1347. * @example
  1348. * //获取数据表格“dt1”的正在编辑的条目。
  1349. * var line = this.form.get("dt1").currentEditedLine;
  1350. * //获取数据
  1351. * var data = line.getData();
  1352. * //设置数据
  1353. * line.setData({"subject":"111"});
  1354. * //获取subject字段的值
  1355. * var data = line.get("subject").getData();
  1356. * //设置subject字段的值
  1357. * line.get("subject").setData("test1");
  1358. */
  1359. this.currentEditedLine = line;
  1360. },
  1361. // editValidation: function(){
  1362. // var flag = true;
  1363. // this.editModules.each(function(field, key){
  1364. // if (field.json.type!=="sequence" && field.validationMode ){
  1365. // field.validationMode();
  1366. // if (!field.validation()) flag = false;
  1367. // }
  1368. // }.bind(this));
  1369. // return flag;
  1370. // },
  1371. _afterLoaded: function(){
  1372. },
  1373. // /**
  1374. // * @summary 重置数据表格的值为默认值或置空。
  1375. // * @example
  1376. // * this.form.get('fieldId').resetData();
  1377. // */
  1378. resetData: function(){
  1379. //var value = this.getDefaultValue() || {"data": [], "total":{}};
  1380. var value = this.getValue();
  1381. this.setData( value , false );
  1382. },
  1383. /**当参数为Promise的时候,请查看文档: {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
  1384. * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
  1385. * @summary 为数据表格赋值,如果需要设置所有区段数据请使用setAllSectionData方法。
  1386. * @param data{DatatableData|Promise} 必选,数组或Promise.
  1387. * @param fireChange{boolean} 可选,是否触发change事件,默认false.
  1388. * @example
  1389. * this.form.get("fieldId").setData({data:[]}); //赋空值
  1390. * @example
  1391. * //如果无法确定表单上是否有组件,需要判断
  1392. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  1393. * this.form.get('fieldId').setData( data );
  1394. * }else{
  1395. * this.data['fieldId'] = data;
  1396. * }
  1397. *@example
  1398. * //使用Promise
  1399. * var field = this.form.get("fieldId");
  1400. * var promise = new Promise(function(resolve, reject){ //发起异步请求
  1401. * var oReq = new XMLHttpRequest();
  1402. * oReq.addEventListener("load", function(){ //绑定load事件
  1403. * resolve(oReq.responseText);
  1404. * });
  1405. * oReq.open("GET", "/data.json"); //假设数据存放在data.json
  1406. * oReq.send();
  1407. * });
  1408. * promise.then( function(){
  1409. * var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获得data.json的值
  1410. * })
  1411. * field.setData( promise );
  1412. */
  1413. setData: function(data, fireChange, operation){
  1414. if (!data){
  1415. data = this._getValue();
  1416. }else{
  1417. //todo 计算total
  1418. }
  1419. this._setData(data, fireChange, operation);
  1420. },
  1421. _setData: function(data, fireChange, operation){
  1422. var p = o2.promiseAll(this.data).then(function(v){
  1423. this.data = v;
  1424. // if (o2.typeOf(data)==="object") data = [data];
  1425. this.__setData(data, fireChange, operation);
  1426. this.moduleValueAG = null;
  1427. return v;
  1428. }.bind(this), function(){
  1429. this.moduleValueAG = null;
  1430. }.bind(this));
  1431. this.moduleValueAG = p;
  1432. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  1433. this.moduleValueAG = null;
  1434. }.bind(this), function(){
  1435. this.moduleValueAG = null;
  1436. }.bind(this));
  1437. },
  1438. __setData: function(data, fireChange, operation){
  1439. // if( typeOf( data ) === "object" && typeOf(data.data) === "array" ){
  1440. if( this.isShowAllSection ){
  1441. //兼容外部对编辑当前区段的setData,内部的setData不走这里,直接走setAllSectionData
  1442. this._setEditedSectionData(data, fireChange, operation);
  1443. return;
  1444. }else if( this.isMergeRead ){
  1445. //合并且只读,不允许setData
  1446. throw new Error("The data table is merged and read-only, you can use the 'setAllSectionData' method to set all section data");
  1447. }
  1448. var old;
  1449. if(fireChange)old = Object.clone(this._getBusinessData() || {});
  1450. this._setUnchangedLineMap(data, operation);
  1451. this._setBusinessData(data);
  1452. this.data = data;
  1453. if (this.data){
  1454. this.clearSubModules();
  1455. }
  1456. this.lineList = [];
  1457. this.sectionlineList = [];
  1458. if( this.currentEditedLine )this.currentEditedLine = null;
  1459. this._loadDatatable( function () {
  1460. if (fireChange && JSON.stringify(old) !== JSON.stringify(data)) this.fireEvent("change");
  1461. this.unchangedLineMap = null;
  1462. }.bind(this), operation);
  1463. },
  1464. _setEditedSectionData: function(data, fireChange, operation){
  1465. if( this.isShowAllSection ){
  1466. var d = this.getBusinessDataById();
  1467. d[ this.sectionBy ] = data || { data: [] };
  1468. this.setAllSectionData( d, fireChange , operation);
  1469. }
  1470. },
  1471. deleteAttachment: function( attId ){
  1472. this.form.workAction.deleteAttachment(attId, this.form.businessData.work.id);
  1473. },
  1474. saveFormData: function(){
  1475. this.form.saveFormData();
  1476. },
  1477. /**
  1478. * @summary 当数据表格设置为区段合并展现、区段合并编辑时,可以使用本方法设置所有区段数据。
  1479. * @param data{Object} 必选,对象.
  1480. * @param fireChange{boolean} 可选,是否触发change事件,默认false.
  1481. * @example
  1482. * this.form.get("fieldId").setAllSectionData({}); //赋空值
  1483. * @example
  1484. * this.data['fieldId'].setAllSectionData({
  1485. * "3455b82a-399c-4ee4-b9b9-e70ae40fbaf1": { //区段1的key和data
  1486. * "data": [
  1487. * {
  1488. * "good": "yf",
  1489. * "number_2": 11,
  1490. * "prize": 1
  1491. * }
  1492. * ]
  1493. * },
  1494. * "83de86fc-60bc-4b4c-955c-1085915865a4": { //区段2的key和data
  1495. * "data": [
  1496. * {
  1497. * "good": "yf",
  1498. * "number_2": 11,
  1499. * "prize": 10
  1500. * }
  1501. * ]
  1502. * }
  1503. * });
  1504. */
  1505. setAllSectionData: function(data, fireChange, operation){
  1506. // if( this.isSectionMergeEdit() ){
  1507. // //合并且编辑,不允许setAllSectionData
  1508. // throw new Error("The data table is in merge editing state, you can use the 'setData' method to set the data");
  1509. // }
  1510. var old;
  1511. if(fireChange)old = Object.clone(this.getBusinessDataById() || {});
  1512. //删除并没有用,因为会对比数据提交,如果要清空可以给data.data = []; data.total = {}
  1513. // if( data && data.data )delete data.data;
  1514. // if( data && data.total )delete data.total;
  1515. this._setUnchangedSectionLineMap(data);
  1516. this.setBusinessDataById(data);
  1517. if( operation ){
  1518. this.data = this.isShowAllSection ? this._getAllSectionData() : data;
  1519. }else{
  1520. this.checkMerge(data);
  1521. }
  1522. if (this.data){
  1523. this.clearSubModules();
  1524. }
  1525. if (fireChange && JSON.stringify(old) !== JSON.stringify(data)) this.fireEvent("change");
  1526. this.lineList = [];
  1527. this.sectionlineList = [];
  1528. if( this.currentEditedLine )this.currentEditedLine = null;
  1529. this._loadDatatable(function () {
  1530. Object.each(this.unchangedSectionLineMap, function(sline, key){
  1531. sline.isUnchangedAll = null;
  1532. sline.unchangedLineMap = null;
  1533. });
  1534. this.unchangedSectionLineMap = null;
  1535. }.bind(this), operation);
  1536. },
  1537. checkMerge: function(data){
  1538. //区段合并后编辑
  1539. var isMergeEidt = this.isSectionMergeEdit();
  1540. if( isMergeEidt ){ //区段合并,删除区段值合并数据后编辑
  1541. if( this.json.mergeTypeEdit === "script" ){
  1542. this._loadMergeEditNodeByScript();
  1543. }else{
  1544. this._loadMergeEditNodeByDefault();
  1545. }
  1546. }
  1547. //区段合并展现
  1548. this.isMergeRead = this.isSectionMergeRead();
  1549. //启用区段且显示所有区段
  1550. this.sectionBy = this._getSectionBy();
  1551. this.isShowAllSection = this.isAllSectionShow();
  1552. if( this.isShowAllSection ){
  1553. this.data = this._getAllSectionData();
  1554. }else if( this.isMergeRead ) {
  1555. this.data = this.getSectionMergeReadData();
  1556. }else if( isMergeEidt ){
  1557. this.data = this._getBusinessData()
  1558. }else{
  1559. this.data = data;
  1560. }
  1561. },
  1562. _setUnchangedSectionLineMap: function( data, operation ){
  1563. if( !data ){
  1564. return;
  1565. }
  1566. var map = {};
  1567. if( this.sectionlineList && this.sectionlineList.length ){
  1568. this.sectionlineList.each(function (sline, i) {
  1569. for( var key in data ){
  1570. if( key === sline.sectionKey ){
  1571. var m = sline._setUnchangedLineMap(data[key], operation);
  1572. if( m )map[key] = m;
  1573. break;
  1574. }
  1575. }
  1576. }.bind(this));
  1577. }
  1578. this.unchangedSectionLineMap = map;
  1579. },
  1580. _setUnchangedLineMap: function(data, operation){
  1581. if( !data ){
  1582. this.unchangedLineMap = {};
  1583. return;
  1584. }
  1585. var fromOutside = !operation;
  1586. var lineDataList = this.lineList.map(function (line) {
  1587. if( line.options.isEditable !== this.editable )return "";
  1588. if( line.options.isDeleteable !== this.deleteable )return "";
  1589. if( line.options.isAddable !== this.addable )return "";
  1590. return fromOutside ? JSON.stringify(line.data) : line.data;
  1591. }.bind(this));
  1592. var dStr, map = {};
  1593. data.data.each(function (d, idx) {
  1594. if(fromOutside)dStr = JSON.stringify(d);
  1595. for (var i = 0; i < this.lineList.length; i++) {
  1596. var isEqual= fromOutside ? (dStr === lineDataList[i]) : (d === lineDataList[i] );
  1597. if ( isEqual ) {
  1598. map[idx] = this.lineList[i];
  1599. lineDataList[i] = "";
  1600. break;
  1601. }
  1602. }
  1603. }.bind(this));
  1604. this.unchangedLineMap = map;
  1605. },
  1606. clearSubModules: function(){
  1607. if( this.sectionlineList && this.sectionlineList.length ){
  1608. this.sectionlineList.each(function (sline, i) {
  1609. sline.clearSubModules();
  1610. }.bind(this));
  1611. }else{
  1612. var map = this.unchangedLineMap || {};
  1613. var lines = [];
  1614. Object.values(map).each(function (d) {
  1615. lines = lines.concat(d);
  1616. });
  1617. for (var i=0; i<this.lineList.length; i++){
  1618. var l = this.lineList[i];
  1619. if(!lines.contains(l))l.clearSubModules();
  1620. }
  1621. }
  1622. },
  1623. resetId: function(){
  1624. if( this.sectionlineList && this.sectionlineList.length ){
  1625. this.sectionlineList.each(function (sline, i) {
  1626. sline.resetId();
  1627. }.bind(this));
  1628. }else{
  1629. for (var i=0; i<this.lineList.length; i++){
  1630. this.lineList[i].resetId();
  1631. }
  1632. }
  1633. },
  1634. /**
  1635. * @summary 判断数据表格是否为空.
  1636. * @example
  1637. * if( this.form.get('fieldId').isEmpty() ){
  1638. * this.form.notice('至少需要添加一条数据', 'warn');
  1639. * }
  1640. * @return {Boolean} 是否为空
  1641. */
  1642. isEmpty: function(){
  1643. var data = this.getInputData();
  1644. if( !data || !data.data )return true;
  1645. if( o2.typeOf( data.data ) === "array" ){
  1646. return data.data.length === 0;
  1647. }
  1648. //????
  1649. // if( o2.typeOf( data ) === "object" ){
  1650. // return Object.keys(data).length === 0;
  1651. // }
  1652. return false;
  1653. },
  1654. //api 相关开始
  1655. /**
  1656. * 获取对应的条目。
  1657. * @param {Number} index 条目序号,从零开始
  1658. * @return {MWF.xApplication.process.Xform.DatatablePC.Line | Null} 对应的数据表格条目
  1659. * @example
  1660. * //获取数据表格“dt1”的第一个条目。
  1661. * var line = this.form.get("dt1").getLine(0);
  1662. * //获取第一行的数据
  1663. * var data = line.getData();
  1664. * //设置第一行的数据
  1665. * line.setData({"subject":"111"});
  1666. * //获取第一个条目subject字段的值
  1667. * var data = line.get("subject").getData();
  1668. * //设置subject字段的值
  1669. * line.get("subject").setData("test1");
  1670. */
  1671. getLine: function(index){
  1672. var line = this.lineList[index];
  1673. return line || null;
  1674. },
  1675. /**
  1676. * 在数据表格末尾添加条目。
  1677. * @param {Object} [data] 添加条目的数据。
  1678. * @return {MWF.xApplication.process.Xform.DatatablePC.Line} 添加的数据表格条目
  1679. * @example
  1680. * var line = this.form.get("dt1").addLine();
  1681. */
  1682. addLine: function( data ){
  1683. return this._addLine( null, null, data );
  1684. },
  1685. /**
  1686. * 在数据表格指定位置添加条目。
  1687. * @param {Number} index 条目序号,从零开始,如果下标超过当前数据表格条目数,插入失败并返回null。
  1688. * @param {Object} [data] 添加条目的数据。
  1689. * @return {MWF.xApplication.process.Xform.DatatablePC.Line | Null} 插入的数据表格条目
  1690. * @example
  1691. * var line = this.form.get("dt1").insertLine(0);
  1692. */
  1693. insertLine: function(index, data){
  1694. return this._insertLineByIndex(null, index, data);
  1695. },
  1696. /**
  1697. * 删除指定位置的条目。
  1698. * @param {Number} index 条目序号,从零开始,如果下标超过当前数据表格条目数,删除失败。
  1699. * @example
  1700. * //直接删除第一个条目
  1701. * this.form.get("dt1").deleteLine(0);
  1702. */
  1703. deleteLine: function(index, ev){
  1704. var line = this.lineList[index];
  1705. if( !line )return null;
  1706. // if( ev ){
  1707. // this._deleteLine(ev, line);
  1708. // }else{
  1709. this._delLine(line);
  1710. // }
  1711. },
  1712. /**
  1713. * 获取对应表单组件,作用等同于get。
  1714. * @param {Number} index 条目序号,从零开始
  1715. * @param {String} id 组件标识
  1716. * @return {FormComponent} 对应表单组件
  1717. * @example
  1718. * //获取数据表格“dt1”的第一个条目的subject字段。
  1719. * var module = this.form.get("dt1").getModule(0, "subject");
  1720. * //获取subject字段的值
  1721. * var data = module.getData();
  1722. * //设置subject字段的值
  1723. * module.setData("test1");
  1724. */
  1725. getModule: function(index, id){
  1726. var line = this.lineList[index];
  1727. if( !line )return null;
  1728. return line.getModule(id);
  1729. },
  1730. /**
  1731. * 获取对应表单组件,作用等同于getModule。
  1732. * @param {Number} index 条目序号,从零开始
  1733. * @param {String} id 组件标识
  1734. * @return {FormComponent} 对应表单组件
  1735. * @example
  1736. * //获取数据表格“dt1”的第一个条目的subject字段。
  1737. * var module = this.form.get("dt1").get(0, "subject");
  1738. * //获取subject字段的值
  1739. * var data = module.getData();
  1740. * //设置subject字段的值
  1741. * module.setData("test1");
  1742. */
  1743. get: function(index, id){
  1744. return this.getModule(index, id);
  1745. },
  1746. //api 相关
  1747. /**
  1748. * 在脚本中使用 this.data[fieldId] 也可以获取组件值。
  1749. * 区别如下:<br/>
  1750. * 1、当使用Promise的时候<br/>
  1751. * 使用异步函数生成器(Promise)为组件赋值的时候,用getData方法立即获取数据,可能返回修改前的值,当Promise执行完成以后,会返回修改后的值。<br/>
  1752. * this.data[fieldId] 立即获取数据,可能获取到异步函数生成器,当Promise执行完成以后,会返回修改后的值。<br/>
  1753. * {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0#EggIl|具体差异请查看链接}<br/>
  1754. * 2、当表单上没有对应组件的时候,可以使用this.data[fieldId]获取值,但是this.form.get('fieldId')无法获取到组件。
  1755. * @summary 获取数据表格数据.
  1756. * @example
  1757. * var data = this.form.get('fieldId').getData();
  1758. *@example
  1759. * //如果无法确定表单上是否有组件,需要判断
  1760. * var data;
  1761. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  1762. * data = this.form.get('fieldId').getData();
  1763. * }else{
  1764. * data = this.data['fieldId']; //直接从数据中获取字段值
  1765. * }
  1766. * @example
  1767. * //使用Promise
  1768. * var field = this.form.get("fieldId");
  1769. * var promise = new Promise(function(resolve, reject){ //发起异步请求
  1770. * var oReq = new XMLHttpRequest();
  1771. * oReq.addEventListener("load", function(){ //绑定load事件
  1772. * resolve(oReq.responseText);
  1773. * });
  1774. * oReq.open("GET", "/data.json"); //假设数据存放在data.json
  1775. * oReq.send();
  1776. * });
  1777. * promise.then( function(){
  1778. * var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获得data.json的值
  1779. * })
  1780. * field.setData( promise );
  1781. * @return {DatatableData}
  1782. */
  1783. getData: function(){
  1784. if( this.importer ){
  1785. this.importer.destroySimulateModule();
  1786. }
  1787. if (this.editable!==false){
  1788. // this.lineList.each(function(line, index){
  1789. // if( !this.multiEditMode && line.options.isEdited ){
  1790. // line.data = line.getData();
  1791. // }else{
  1792. // line.data = line.getData();
  1793. // }
  1794. // });
  1795. if( this.multiEditMode){
  1796. this.lineList.each(function (line) {
  1797. line.computeModuleData("save");
  1798. })
  1799. }
  1800. return this._getBusinessData();
  1801. }else{
  1802. return this._getBusinessData();
  1803. }
  1804. },
  1805. getInputData: function(){
  1806. if( this.importer ){
  1807. this.importer.destroySimulateModule();
  1808. }
  1809. if (this.editable!==false){
  1810. return this._getBusinessData();
  1811. }else{
  1812. return this._getBusinessData();
  1813. }
  1814. },
  1815. //区段依据
  1816. _getSectionBy: function(){
  1817. if (this.json.section!=="yes"){
  1818. return "";
  1819. }else {
  1820. switch (this.json.sectionBy){
  1821. case "person":
  1822. return layout.desktop.session.user.id;
  1823. case "unit":
  1824. return (this.form.businessData.task) ? this.form.businessData.task.unit : "";
  1825. case "activity":
  1826. return (this.form.businessData.work) ? this.form.businessData.work.activity : "";
  1827. case "splitValue":
  1828. return (this.form.businessData.work) ? this.form.businessData.work.splitValue : "";
  1829. case "script":
  1830. if( this.json.sectionByScript && this.json.sectionByScript.code){
  1831. return this.form.Macro.exec(this.json.sectionByScript.code, this) || "";
  1832. }else{
  1833. return "";
  1834. }
  1835. default:
  1836. return "";
  1837. }
  1838. }
  1839. },
  1840. createErrorNode: function(text){
  1841. var node = new Element("div");
  1842. var iconNode = new Element("div", {
  1843. "styles": {
  1844. "width": "20px",
  1845. "height": "20px",
  1846. "float": "left",
  1847. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  1848. }
  1849. }).inject(node);
  1850. var textNode = new Element("div", {
  1851. "styles": {
  1852. "line-height": "20px",
  1853. "margin-left": "20px",
  1854. "color": "red",
  1855. "word-break": "keep-all"
  1856. },
  1857. "text": text
  1858. }).inject(node);
  1859. return node;
  1860. },
  1861. notValidationMode: function(text){
  1862. if (!this.isNotValidationMode){
  1863. this.isNotValidationMode = true;
  1864. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  1865. this.node.setStyle("border", "1px solid red");
  1866. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  1867. this.showNotValidationMode(this.node);
  1868. var parentNode = this.errNode;
  1869. while( parentNode && parentNode.offsetParent === null ){
  1870. parentNode = parentNode.getParent();
  1871. }
  1872. if (parentNode && !parentNode.isIntoView()) parentNode.scrollIntoView(false);
  1873. }
  1874. },
  1875. showNotValidationMode: function(node){
  1876. var p = node.getParent("div");
  1877. if (p){
  1878. if (p.get("MWFtype") == "tab$Content"){
  1879. if (p.getParent("div").getStyle("display")=="none"){
  1880. var contentAreaNode = p.getParent("div").getParent("div");
  1881. var tabAreaNode = contentAreaNode.getPrevious("div");
  1882. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  1883. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  1884. tabNode.click();
  1885. p = tabAreaNode.getParent("div");
  1886. }
  1887. }
  1888. this.showNotValidationMode(p);
  1889. }
  1890. },
  1891. validationMode: function(){
  1892. if (this.isNotValidationMode){
  1893. this.isNotValidationMode = false;
  1894. this.node.setStyles(this.node.retrieve("borderStyle"));
  1895. if (this.errNode){
  1896. this.errNode.destroy();
  1897. this.errNode = null;
  1898. }
  1899. }
  1900. this.lineList.each(function(line){
  1901. line.validationMode();
  1902. })
  1903. },
  1904. validationConfigItem: function(routeName, data){
  1905. var flag = (data.status=="all") ? true: (routeName == data.decision);
  1906. if (flag){
  1907. //???
  1908. var n = this.getInputData();
  1909. if( o2.typeOf(n)==="object"){
  1910. var arr = [];
  1911. Object.each( n, function (d, key) {
  1912. if(o2.typeOf(d) === "array")arr = arr.concat(d);
  1913. });
  1914. n = arr;
  1915. }
  1916. var v = (data.valueType=="value") ? n : n.length;
  1917. switch (data.operateor){
  1918. case "isnull":
  1919. if (!v){
  1920. this.notValidationMode(data.prompt);
  1921. return false;
  1922. }
  1923. break;
  1924. case "notnull":
  1925. if (v){
  1926. this.notValidationMode(data.prompt);
  1927. return false;
  1928. }
  1929. break;
  1930. case "gt":
  1931. if (v>data.value){
  1932. this.notValidationMode(data.prompt);
  1933. return false;
  1934. }
  1935. break;
  1936. case "lt":
  1937. if (v<data.value){
  1938. this.notValidationMode(data.prompt);
  1939. return false;
  1940. }
  1941. break;
  1942. case "equal":
  1943. if (v==data.value){
  1944. this.notValidationMode(data.prompt);
  1945. return false;
  1946. }
  1947. break;
  1948. case "neq":
  1949. if (v!=data.value){
  1950. this.notValidationMode(data.prompt);
  1951. return false;
  1952. }
  1953. break;
  1954. case "contain":
  1955. if (v.indexOf(data.value)!=-1){
  1956. this.notValidationMode(data.prompt);
  1957. return false;
  1958. }
  1959. break;
  1960. case "notcontain":
  1961. if (v.indexOf(data.value)==-1){
  1962. this.notValidationMode(data.prompt);
  1963. return false;
  1964. }
  1965. break;
  1966. }
  1967. }
  1968. return true;
  1969. },
  1970. validationConfig: function(routeName, opinion){
  1971. if (this.json.validationConfig){
  1972. if (this.json.validationConfig.length){
  1973. for (var i=0; i<this.json.validationConfig.length; i++) {
  1974. var data = this.json.validationConfig[i];
  1975. if (!this.validationConfigItem(routeName, data)) return false;
  1976. }
  1977. }
  1978. return true;
  1979. }
  1980. return true;
  1981. },
  1982. saveValidation: function () {
  1983. return this.validationCurrentEditedLine();
  1984. },
  1985. validationCurrentEditedLine: function () {
  1986. var line = this.currentEditedLine;
  1987. if( !line )return true;
  1988. if( !line.validation() )return false;
  1989. return true;
  1990. },
  1991. validation: function(routeName, opinion){
  1992. // if (this.isEdit){
  1993. // if (!this.editValidation()){
  1994. // return false;
  1995. // }
  1996. // }
  1997. if (!this.validationConfig(routeName, opinion)) return false;
  1998. if( !this.validationCurrentEditedLine() )return false;
  1999. if (!this.json.validation) return true;
  2000. if (!this.json.validation.code) return true;
  2001. this.currentRouteName = routeName;
  2002. var flag = this.form.Macro.exec(this.json.validation.code, this);
  2003. this.currentRouteName = "";
  2004. if (!flag) flag = MWF.xApplication.process.Xform.LP.lineNotValidation;
  2005. if (flag.toString()!=="true"){
  2006. this.notValidationMode(flag);
  2007. return false;
  2008. }
  2009. return true;
  2010. },
  2011. getAttachmentRandomSite: function(){
  2012. var i = (new Date()).getTime();
  2013. return this.json.id+i;
  2014. },
  2015. _loadImportExportAction: function(){
  2016. this.impexpNode = this.node.getElement("div.impexpNode");
  2017. if( this.impexpNode )this.impexpNode.destroy();
  2018. this.impexpNode = null;
  2019. if( !this.exportenable && !this.importenable )return;
  2020. var position = ["leftTop","centerTop","rightTop"].contains( this.json.impexpPosition || "" ) ? "top" : "bottom";
  2021. var container = new Element("div", {style: "overflow:hidden;"}).inject(this.node, position);
  2022. this.importExportAreaNode = new Element("div").inject( container );
  2023. if( ["leftTop","leftBottom"].contains( this.json.impexpPosition || "" ) ){
  2024. this.importExportAreaNode.setStyles({ "float" : "left" })
  2025. }else if( ["rightTop","rightBottom"].contains( this.json.impexpPosition || "" ) ){
  2026. this.importExportAreaNode.setStyles({ "float" : "right" })
  2027. }else{
  2028. this.importExportAreaNode.setStyles({ "margin" : "0px auto" })
  2029. }
  2030. var styles;
  2031. if( this.exportenable ){
  2032. this.exportActionNode = new Element("div", {
  2033. text : this.json.exportActionText || MWF.xApplication.process.Xform.LP.datagridExport
  2034. }).inject(this.importExportAreaNode);
  2035. if( this.json.exportActionStyles ){
  2036. styles = this.json.exportActionStyles
  2037. }else{
  2038. styles = this.form.css.gridExportActionStyles;
  2039. }
  2040. this.exportActionNode.setStyles(styles);
  2041. this.exportActionNode.addEvent("click", function () {
  2042. this.exportToExcel();
  2043. }.bind(this))
  2044. }
  2045. if( this.importenable ){
  2046. this.importActionNode = new Element("div", {
  2047. text : this.json.importActionText || MWF.xApplication.process.Xform.LP.datagridImport
  2048. }).inject(this.importExportAreaNode);
  2049. if( this.json.importActionStyles ){
  2050. styles = this.json.importActionStyles;
  2051. }else{
  2052. styles = this.form.css.gridImportActionStyles;
  2053. }
  2054. this.importActionNode.setStyles(styles);
  2055. this.importActionNode.addEvent("click", function () {
  2056. this.importFromExcel();
  2057. }.bind(this))
  2058. }
  2059. if( ["centerTop","centerBottom"].contains( this.json.impexpPosition ) ){
  2060. var width = 2;
  2061. if( this.exportActionNode ){
  2062. width = width + this.exportActionNode.getSize().x +
  2063. this.exportActionNode.getStyle("padding-left").toFloat() +
  2064. + this.exportActionNode.getStyle("padding-right").toFloat() +
  2065. + this.exportActionNode.getStyle("margin-left").toFloat() +
  2066. + this.exportActionNode.getStyle("margin-right").toFloat()
  2067. }
  2068. if( this.importActionNode ){
  2069. width = width + this.importActionNode.getSize().x +
  2070. this.importActionNode.getStyle("padding-left").toFloat() +
  2071. + this.importActionNode.getStyle("padding-right").toFloat() +
  2072. + this.importActionNode.getStyle("margin-left").toFloat() +
  2073. + this.importActionNode.getStyle("margin-right").toFloat()
  2074. }
  2075. this.importExportAreaNode.setStyle( "width", width+"px" );
  2076. }
  2077. },
  2078. exportToExcel: function(){
  2079. this.exporter = new MWF.xApplication.process.Xform.DatatablePC.Exporter(this);
  2080. this.exporter.exportToExcel();
  2081. },
  2082. importFromExcel: function(){
  2083. this.importer = new MWF.xApplication.process.Xform.DatatablePC.Importer(this);
  2084. this.importer.importFromExcel();
  2085. }
  2086. });
  2087. MWF.xApplication.process.Xform.DatatablePC$Title = new Class({
  2088. Extends: MWF.APP$Module,
  2089. _loadUserInterface: function(){
  2090. if(this.json.recoveryStyles){
  2091. this.node.setStyles(this.json.recoveryStyles);
  2092. }
  2093. if (this.json.prefixIcon || this.json.suffixIcon){
  2094. var text = this.node.get("text");
  2095. this.node.empty();
  2096. var lineheight = this.node.getStyle("line-height") || "28px";
  2097. this.wrapNode = new Element("div", {
  2098. "styles": {
  2099. "display": "flex",
  2100. "align-items": "center",
  2101. "justify-content": "center"
  2102. }
  2103. }).inject(this.node);
  2104. if (this.json.prefixIcon){
  2105. this.prefixNode = new Element("div", {"styles": {
  2106. "width": "20px",
  2107. "min-width": "20px",
  2108. "height": lineheight,
  2109. "background": "url("+this.json.prefixIcon+") center center no-repeat"
  2110. }}).inject(this.wrapNode);
  2111. }
  2112. this.textNode = new Element("div", {"styles": {
  2113. "line-height": lineheight,
  2114. "vertical-align": "top",
  2115. "padding": "1px"
  2116. }, "text": text}).inject(this.wrapNode);
  2117. if (this.json.suffixIcon){
  2118. this.suffixNode = new Element("div", {"styles": {
  2119. "width": "20px",
  2120. "min-width": "20px",
  2121. "height": lineheight,
  2122. "background": "url("+this.json.suffixIcon+") center center no-repeat"
  2123. }}).inject(this.wrapNode);
  2124. }
  2125. }
  2126. },
  2127. getOffsetY: function(){
  2128. return (this.node.getStyle("padding-top") || 0).toInt()
  2129. + (this.node.getStyle("padding-bottom") || 0).toInt()
  2130. + (this.node.getStyle("border-top") || 0).toInt()
  2131. + (this.node.getStyle("border-bottom") || 0).toInt()
  2132. }
  2133. });
  2134. MWF.xApplication.process.Xform.DatatablePC$Data = new Class({
  2135. Extends: MWF.APP$Module,
  2136. _loadUserInterface: function(){
  2137. if(this.json.recoveryStyles){
  2138. this.node.setStyles(this.json.recoveryStyles);
  2139. }
  2140. }
  2141. });
  2142. MWF.xApplication.process.Xform.DatatablePC.SectionLine = new Class({
  2143. Implements: [Options, Events],
  2144. options: {
  2145. isNew: false,
  2146. isEdited: true, //是否正在编辑
  2147. isEditable: true, //能否被编辑
  2148. isDeleteable: true, //能否被删除
  2149. isAddable: true, //能否添加
  2150. isMergeRead: false, //合并阅读
  2151. index: 0,
  2152. indexText: "0"
  2153. },
  2154. initialize: function (node, datatable, data, options) {
  2155. this.setOptions(options);
  2156. this.sectionKeyNode = node;
  2157. this.datatable = datatable;
  2158. this.data = data;
  2159. this.form = this.datatable.form;
  2160. this.lineList = [];
  2161. this.totalColumns = [];
  2162. this.totalNumberModuleIds = [];
  2163. this.sectionKey = this.data.sectionKey;
  2164. },
  2165. load: function () {
  2166. if( this.datatable.isShowSectionKey() || this.datatable.isShowSectionBy() ){
  2167. this.loadSectionKeyNode();
  2168. }else{
  2169. this.sectionKeyNode.hide();
  2170. }
  2171. this._loadTotalTr();
  2172. if( this.data.data && this.data.data.data ){
  2173. ( this.data.data.data || [] ).each(function(d, idx){
  2174. if( !d )return;
  2175. var node = this._createLineNode();
  2176. var isEdited = false, isNew = false;
  2177. if( this.options.isEdited ){
  2178. var dt = this.datatable;
  2179. isNew = dt.isNew || (o2.typeOf(dt.newLineIndex) === "number" ? idx === dt.newLineIndex : false);
  2180. isEdited = (!dt.multiEditMode && o2.typeOf(dt.newLineIndex) === "number") ? idx === dt.newLineIndex : dt.multiEditMode;
  2181. dt.isNew = false;
  2182. dt.newLineIndex = null;
  2183. }
  2184. var line = this._loadLine( node, d, idx, isEdited, isNew );
  2185. this.lineList.push(line);
  2186. this.datatable.lineList.push(line);
  2187. }.bind(this));
  2188. this._loadTotal();
  2189. }
  2190. },
  2191. resetId: function(){
  2192. this.lineList.each(function (line) {
  2193. line.resetId();
  2194. }.bind(this))
  2195. },
  2196. setIndex: function( preNode, data, index, isEdited, isNew, operation ){
  2197. if( this.isUnchangedAll && index === this.options.index )return;
  2198. this.data = data;
  2199. this.options.index = index;
  2200. this.options.indexText = (index+1).toString();
  2201. this.sectionKeyNode.inject(preNode, "after");
  2202. this.lineList = [];
  2203. var map = this.unchangedLineMap || {};
  2204. Object.each(map, function (line, idx) {
  2205. line.setIndex( this.datatable.lineList.length + idx.toInt(), idx.toInt() );
  2206. }.bind(this));
  2207. if( this.data.data && this.data.data.data ){
  2208. this.data.data.data.each(function (d, idx) {
  2209. if( !d )return;
  2210. var idxStr = idx.toString();
  2211. var beforeNode = idx > 0 ? this.lineList[idx - 1].node : this.sectionKeyNode;
  2212. if( map[idxStr] ){
  2213. if( !operation || operation === "moveUpList" ){
  2214. map[idxStr].node.inject( beforeNode, "after" );
  2215. }
  2216. this.lineList.push( map[idxStr] );
  2217. this.datatable.lineList.push(map[idxStr]);
  2218. }else{
  2219. var node = this._createLineNode( beforeNode );
  2220. var isEdited = false, isNew = false;
  2221. if( this.options.isEdited ){
  2222. var dt = this.datatable;
  2223. isNew = dt.isNew || (o2.typeOf(dt.newLineIndex) === "number" ? idx === dt.newLineIndex : false);
  2224. isEdited = (!dt.multiEditMode && o2.typeOf(dt.newLineIndex) === "number") ? idx === dt.newLineIndex : dt.multiEditMode;
  2225. dt.isNew = false;
  2226. dt.newLineIndex = null;
  2227. }
  2228. var line = this._loadLine( node, d, idx, isEdited, isNew );
  2229. this.lineList.push(line);
  2230. this.datatable.lineList.push(line);
  2231. }
  2232. }.bind(this))
  2233. }
  2234. if( this.totalTr )this.totalTr.inject( this.lineList.getLast().node, "after" );
  2235. this._loadTotal();
  2236. },
  2237. _loadLine: function(container, data, index, isEdited, isNew){
  2238. var line = new MWF.xApplication.process.Xform.DatatablePC.Line(container, this.datatable, data, {
  2239. indexInSectionLine : index,
  2240. indexInSectionLineText : (index+1).toString(),
  2241. index: this.datatable.lineList.length,
  2242. indexText : (this.datatable.lineList.length + 1).toString(),
  2243. isNew: isNew,
  2244. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.options.isEdited,
  2245. isEditable: this.options.isEditable,
  2246. isDeleteable: this.options.isDeleteable,
  2247. isAddable: this.options.isAddable,
  2248. isMergeRead: this.options.isMergeRead,
  2249. sectionKey: this.sectionKey
  2250. }, this);
  2251. this.datatable.fireEvent("beforeLoadLine", [line]);
  2252. line.load();
  2253. this.datatable.fireEvent("afterLoadLine", [line]);
  2254. return line;
  2255. },
  2256. _createLineNode: function( beforeNode ){
  2257. var tr;
  2258. if( beforeNode ){
  2259. tr = new Element("tr").inject(beforeNode, "after");
  2260. }else if( this.totalTr ){
  2261. tr = new Element("tr").inject(this.totalTr, "before");
  2262. }else{
  2263. tr = this.datatable._createLineNode();
  2264. // tr = new Element("tr").inject(this.tBody || this.table);
  2265. }
  2266. return tr;
  2267. },
  2268. loadSectionKeyNode: function () {
  2269. var styleName = this.datatable.isShowSectionKey() ? "sectionKeyStyles" : "sectionByStyles";
  2270. var sectionKeyStyles = this.datatable._parseStyles( this.datatable.json[styleName] || {} );
  2271. var keyNode = new Element("td.mwf_sectionkey", {
  2272. colspan: this.datatable.columnCount,
  2273. styles : sectionKeyStyles
  2274. }).inject( this.sectionKeyNode );
  2275. this.keyNode = keyNode;
  2276. var separator;
  2277. if( this.datatable.isShowSectionKey() ){
  2278. separator = this.datatable.json.keyContentSeparator;
  2279. }else{
  2280. separator = this.datatable.json.keyContentSeparatorSectionBy;
  2281. }
  2282. this.datatable.getSectionKeyWithMerge( this.data, function (key) {
  2283. if( o2.typeOf(key) === "string" ){
  2284. keyNode.set("text", key + (separator || ""));
  2285. }else{
  2286. Promise.resolve(key).then(function (k) {
  2287. keyNode.set("text", k + (separator || ""));
  2288. }.bind(this))
  2289. }
  2290. }.bind(this));
  2291. },
  2292. _setUnchangedLineMap: function(data, operation){
  2293. var fromOutside = !operation;
  2294. var dt = this.datatable;
  2295. var editalbe;
  2296. if( dt.isShowAllSection ){
  2297. editalbe = dt.isShowAllSection && dt.sectionBy && dt.sectionBy === this.sectionKey;
  2298. }
  2299. var lineDataList = this.lineList.map(function (line) {
  2300. if( dt.isShowAllSection ){
  2301. if( line.options.isEditable !== (dt.editable && editalbe) )return "";
  2302. if( line.options.isDeleteable !== (dt.deleteable && editalbe) )return "";
  2303. if( line.options.isAddable !== (dt.addable && editalbe) )return "";
  2304. }else{
  2305. if( line.options.isEditable !== dt.editable )return "";
  2306. if( line.options.isDeleteable !== dt.deleteable )return "";
  2307. if( line.options.isAddable !== dt.addable )return "";
  2308. }
  2309. return fromOutside ? JSON.stringify(line.data) : line.data;
  2310. }.bind(this));
  2311. var dStr, map = {};
  2312. data.data.each(function (d, idx) {
  2313. if(fromOutside)dStr = JSON.stringify(d);
  2314. for (var i = 0; i < this.lineList.length; i++) {
  2315. var isEqual = fromOutside ? (dStr === lineDataList[i]) : (d === lineDataList[i] );
  2316. if ( isEqual ) {
  2317. map[idx] = this.lineList[i];
  2318. lineDataList[i] = "";
  2319. break;
  2320. }
  2321. }
  2322. }.bind(this));
  2323. this.isUnchangedAll = data.data.length === this.lineList.length;
  2324. if( this.isUnchangedAll ){
  2325. for( var i=0; i<data.data.length; i++ ){
  2326. var line = map[i.toString()];
  2327. if( !line || line.options.indexInSectionLine !== i ){
  2328. this.isUnchangedAll = false;
  2329. break;
  2330. }
  2331. }
  2332. }
  2333. this.unchangedLineMap = map;
  2334. return Object.keys(map).length ? this : null;
  2335. },
  2336. clearSubModules: function(){
  2337. if( this.isUnchangedAll )return;
  2338. var map = this.unchangedLineMap || {};
  2339. var hasUnchangedLine = Object.keys(map).length > 0;
  2340. if( !hasUnchangedLine ){
  2341. if( this.sectionKeyNode ){
  2342. this.sectionKeyNode.destroy();
  2343. this.sectionKeyNode = null;
  2344. }
  2345. }
  2346. var lines = [];
  2347. Object.values(map).each(function (d) {
  2348. lines = lines.concat(d);
  2349. });
  2350. for (var i=0; i<this.lineList.length; i++){
  2351. var l = this.lineList[i];
  2352. if(!lines.contains(l)){
  2353. l.clearSubModules();
  2354. }
  2355. }
  2356. // for (var i=0; i<this.lineList.length; i++){
  2357. // this.lineList[i].clearSubModules();
  2358. // }
  2359. if( !hasUnchangedLine ) {
  2360. if (this.totalTr) {
  2361. this.totalTr.destroy();
  2362. this.totalTr = null;
  2363. }
  2364. }
  2365. },
  2366. isTotalTrShow: function(){
  2367. var dt = this.datatable;
  2368. if( dt.isShowSectionKey() && ( dt.json.totalRowBySection || [] ).contains("section") )return true;
  2369. if( dt.isShowSectionBy() && ( dt.json.totalRowBy || [] ).contains("section") )return true;
  2370. return false;
  2371. },
  2372. _loadTotalTr: function(){
  2373. if( !this.datatable.totalFlag )return false;
  2374. this.totalTr = new Element("tr.mwf_totaltr", {"styles": this.form.css.datagridTotalTr_section}).inject(this.sectionKeyNode, "after");
  2375. if( !this.isTotalTrShow() )this.totalTr.hide();
  2376. var ths = this.datatable.titleTr.getElements("th");
  2377. //datatable$Title Module
  2378. ths.each(function(th, index){
  2379. var td = new Element("td", {"text": "", "styles": this.form.css.datagridTotalTd}).inject(this.totalTr);
  2380. if (this.datatable.json.sectionAmountStyles) td.setStyles(this.datatable.json.sectionAmountStyles);
  2381. var json = this.form._getDomjson(th);
  2382. if (json){
  2383. if( json.isShow === false ){
  2384. td.hide(); //隐藏列
  2385. }else if( this.reloading && json.isShow === true){
  2386. td.setStyle("display", "");
  2387. }
  2388. if ((json.total === "number") || (json.total === "count")){
  2389. this.totalColumns.push({
  2390. "th" : th,
  2391. "td" : td,
  2392. "index": index,
  2393. "type": json.total
  2394. })
  2395. }
  2396. }
  2397. }.bind(this));
  2398. var tds = this.datatable.templateTr.getElements("td");
  2399. //datatable$Data Module
  2400. tds.each(function(td, index){
  2401. var json = this.form._getDomjson(td);
  2402. if (json){
  2403. //总计列
  2404. var tColumn = this.totalColumns.find(function(a){ return a.index === index });
  2405. if(tColumn){
  2406. var moduleNodes = this.form._getModuleNodes(td); //获取总计列内的填写组件
  2407. if( moduleNodes.length > 0 ){
  2408. tColumn.moduleJson = this.form._getDomjson(moduleNodes[0]);
  2409. if(tColumn.type === "number")this.totalNumberModuleIds.push( tColumn.moduleJson.id );
  2410. }
  2411. }
  2412. }
  2413. }.bind(this));
  2414. },
  2415. _getTotalTr: function(){
  2416. return this.totalTr;
  2417. },
  2418. _loadTotal: function(){
  2419. var totalData = {};
  2420. if( !this.datatable.totalFlag )return totalData;
  2421. if (!this._getTotalTr())this._loadTotalTr();
  2422. var data;
  2423. if( this.datatable.isShowAllSection ){
  2424. Object.each( this.datatable.getBusinessDataById(), function (d, k) {
  2425. if( this.sectionKey === k )data = d
  2426. }.bind(this))
  2427. }else{
  2428. data = this.data.data;
  2429. }
  2430. this.totalColumns.each(function(column, index){
  2431. var json = column.moduleJson;
  2432. if(!json)return;
  2433. var total = this.datatable._loadColumnTotal( column, data );
  2434. if( typeOf(total) !== "null" )totalData[json.id] = total;
  2435. }.bind(this));
  2436. data.total = totalData;
  2437. return totalData;
  2438. },
  2439. isTotalNumberModule: function( id ){
  2440. return this.totalNumberModuleIds.contains(id)
  2441. },
  2442. getLastTr: function () {
  2443. if( this.totalTr )return this.totalTr;
  2444. if( this.lineList.length )return this.lineList.getLast().node;
  2445. return this.sectionKeyNode;
  2446. }
  2447. });
  2448. MWF.xApplication.process.Xform.DatatablePC.Line = new Class({
  2449. Implements: [Options, Events],
  2450. options: {
  2451. isNew: false,
  2452. isEdited : true, //是否正在编辑
  2453. isEditable : true, //能否被编辑
  2454. isDeleteable: true, //能否被删除
  2455. isAddable: true, //能否添加
  2456. isMergeRead: false, //合并阅读
  2457. index : 0,
  2458. indexText : "0",
  2459. indexInSectionLine: 0,
  2460. indexInSectionLineText : "0",
  2461. sectionKey: ""
  2462. },
  2463. initialize: function (node, datatable, data, options, sectionLine) {
  2464. this.setOptions(options);
  2465. this.node = node;
  2466. this.datatable = datatable;
  2467. this.data = data;
  2468. this.form = this.datatable.form;
  2469. this.sectionLine = sectionLine;
  2470. // if( !this.datatable.multiEditMode && !this.options.isNew){
  2471. // this.originalData = Object.clone(data);
  2472. // }
  2473. this.init()
  2474. },
  2475. init: function(){
  2476. this.modules = [];
  2477. this.all = {};
  2478. this.all_templateId = {};
  2479. this.fields = [];
  2480. this.allField = {};
  2481. this.allField_templateId = {};
  2482. this.changedAttachmentMap = {};
  2483. },
  2484. load: function(){
  2485. if( !this.datatable.multiEditMode && this.options.isEdited )this.datatable.currentEditedLine = this;
  2486. this.loadModules();
  2487. this.loadSequence();
  2488. this.createActions();
  2489. this.loadZebraStyle();
  2490. this.loadEditedStyle();
  2491. this.addNodeEvent();
  2492. if( !this.datatable.multiEditMode )this.originalData = Object.clone(this.data);
  2493. // if(this.options.isNew && this.options.isEdited){
  2494. // this.data = this.getData();
  2495. // if( !this.datatable.multiEditMode )this.originalData = Object.clone(this.data);
  2496. // this.options.isNew = false;
  2497. // }
  2498. },
  2499. resetId: function(){
  2500. this.setIndex();
  2501. },
  2502. setIndex: function(index, indexInSectionLine){
  2503. var hasIndexArg = typeOf(index) !== "null";
  2504. var hasIndexInSectionLineArg = typeOf(indexInSectionLine) !== "null";
  2505. if( hasIndexArg && hasIndexInSectionLineArg){
  2506. if( this.options.index === index && this.options.indexInSectionLine === indexInSectionLine )return;
  2507. }else if( hasIndexArg && !hasIndexInSectionLineArg){
  2508. if( this.options.index === index )return;
  2509. }else if(!hasIndexArg && hasIndexInSectionLineArg){
  2510. if( this.options.indexInSectionLine === indexInSectionLine )return;
  2511. }
  2512. if( hasIndexArg ){
  2513. this.options.index = index;
  2514. this.options.indexText = (index.toInt()+1).toString();
  2515. }
  2516. if( hasIndexInSectionLineArg ){
  2517. this.options.indexInSectionLine = indexInSectionLine;
  2518. this.options.indexInSectionLineText = (indexInSectionLine.toInt()+1).toString();
  2519. }
  2520. //合并状态或拆分状态
  2521. var sectionKey = this.options.sectionKey || this.datatable.sectionBy;
  2522. this.modules.each(function (module) {
  2523. var json = module.json;
  2524. var id, oldId = json.id, templateJsonId = json.originialId;
  2525. if( this.datatable.isShowAllSection ){
  2526. id = this.datatable.json.id + ".." + sectionKey + "..data.." + this.options.indexInSectionLine + ".." + json.originialId;
  2527. }else if( sectionKey ){
  2528. id = this.datatable.json.id + ".." + sectionKey + "..data.." + this.options.index + ".." + json.originialId;
  2529. }else{
  2530. id = this.datatable.json.id + "..data.." + this.options.index + ".." + json.originialId;
  2531. }
  2532. json.id = id;
  2533. switch (module.json.type) {
  2534. case "Select":
  2535. (module.areaNode || module.node).set("id", id);
  2536. break;
  2537. default:
  2538. module.node.set("id", id);
  2539. break;
  2540. }
  2541. if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  2542. json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
  2543. }
  2544. delete this.all[oldId];
  2545. this.all[id] = module;
  2546. delete this.allField[oldId];
  2547. this.allField[id] = module;
  2548. if(this.form.all[oldId] && this.form.all[oldId] === module){
  2549. delete this.form.all[oldId];
  2550. }
  2551. this.form.all[id] = module;
  2552. if(this.form.forms[oldId] && this.form.forms[oldId] === module){
  2553. delete this.form.forms[oldId];
  2554. }
  2555. if( module.field )this.form.forms[id] = module;
  2556. if( hasIndexArg || hasIndexInSectionLineArg ){
  2557. this.loadSequence();
  2558. this.loadZebraStyle();
  2559. }
  2560. }.bind(this));
  2561. },
  2562. loadModules: function(){
  2563. this.node.set("html", this.datatable.templateHtml);
  2564. var moduleNodes = this.form._getModuleNodes(this.node, true);
  2565. //合并状态或拆分状态
  2566. var sectionKey = this.options.sectionKey || this.datatable.sectionBy;
  2567. moduleNodes.each(function (node) {
  2568. var mwfType = node.get("MWFtype");
  2569. if (mwfType === "form")return;
  2570. var _self = this;
  2571. var tJson = this.form._getDomjson(node);
  2572. if( tJson ){
  2573. var json = Object.clone(tJson);
  2574. if( !this.options.isEdited || !this.options.isEditable ){
  2575. if( json.hasOwnProperty('showMode') ){
  2576. json.showMode = 'read';
  2577. }else{
  2578. json.isReadonly = true;
  2579. }
  2580. }
  2581. var templateJsonId = json.id;
  2582. var index = this.options.index;
  2583. var id;
  2584. if( this.datatable.isShowAllSection ){
  2585. id = this.datatable.json.id + ".." + sectionKey + "..data.." + this.options.indexInSectionLine + ".." + json.id;
  2586. }else if( sectionKey ){
  2587. id = this.datatable.json.id + ".." + sectionKey + "..data.." + index + ".." + json.id;
  2588. }else{
  2589. id = this.datatable.json.id + "..data.." + index + ".." + json.id;
  2590. }
  2591. json.originialId = templateJsonId;
  2592. json.id = id;
  2593. node.set("id", id);
  2594. if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  2595. json.type = "AttachmentDg";
  2596. json.ignoreSite = true;
  2597. json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
  2598. }
  2599. if (this.form.all[id]) this.form.all[id] = null;
  2600. if (this.form.forms[id])this.form.forms[id] = null;
  2601. var hasData = this.data.hasOwnProperty(templateJsonId);
  2602. var module = this.form._loadModule(json, node, function () {
  2603. if( _self.options.isMergeRead ){
  2604. this.field = false; //不希望保存数据
  2605. this._getBusinessData = function(){
  2606. return _self.data[templateJsonId];
  2607. };
  2608. this._setBusinessData = function () {};
  2609. }
  2610. if( _self.widget )this.widget = _self.widget;
  2611. this.parentLine = _self;
  2612. this.parentDatatable = _self.datatable;
  2613. //只读方法值在页面加载的时候或者new的时候计算一下
  2614. if( this.json.compute === "show" ){
  2615. var needComputeShow = false;
  2616. if( _self.datatable.loading ) {
  2617. needComputeShow = true;
  2618. }else if( _self.options.isNew && !_self.reloading ){
  2619. needComputeShow = true;
  2620. }
  2621. if( !needComputeShow ){
  2622. this.json.compute = "create"; //
  2623. if( this.options.moduleEvents && this.options.moduleEvents.length ){ //恢复compute
  2624. var eventName = ( this.options.moduleEvents || [] ).contains("afterLoad") ? "afterLoad" : "load";
  2625. var resetCompute = function () {
  2626. this.json.compute = "show";
  2627. this.removeEvent( eventName, resetCompute );
  2628. }.bind(this)
  2629. this.addEvent(eventName, resetCompute);
  2630. }
  2631. }
  2632. }
  2633. });
  2634. if(!module.parentLine)module.parentLine = this;
  2635. if(!module.parentDatatable)module.parentDatatable = this.datatable;
  2636. if((json.type==="Attachment" || json.type==="AttachmentDg")){
  2637. module.addEvent("change", function(){
  2638. if( this.datatable.multiEditMode ){
  2639. _self.datatable.saveFormData();
  2640. }else{
  2641. _self.attachmentChangeFlag = true;
  2642. }
  2643. }.bind(this))
  2644. }
  2645. this.form.modules.push(module);
  2646. this.modules.push(module);
  2647. this.all[id] = module;
  2648. this.all_templateId[templateJsonId] = module;
  2649. if (module.field) {
  2650. // if(hasData){
  2651. // module.setData(this.data[templateJsonId]);
  2652. // }else if(this.options.isEdited){
  2653. // this.data[templateJsonId] = module.getData();
  2654. // }
  2655. if(this.options.isEdited ) {
  2656. if (json.type !== "Attachment" && json.type !== "AttachmentDg"){
  2657. if( module.json.compute === "save" && module.getInputData ){
  2658. this.data[templateJsonId] = module.getInputData();
  2659. }else{
  2660. this.data[templateJsonId] = module.getData();
  2661. }
  2662. }
  2663. }else if(!hasData && module.getValue ){
  2664. this.data[templateJsonId] = module.getValue();
  2665. }
  2666. this.allField[id] = module;
  2667. this.allField_templateId[templateJsonId] = module;
  2668. this.fields.push( module );
  2669. if( this.options.isEdited && this.datatable.multiEditMode ){
  2670. module.addEvent("change", function(){
  2671. this.datatable.fireEvent("change", [{lines: [this], type: "editmodule", module: module}]);
  2672. }.bind(this))
  2673. }
  2674. //该字段是合集数值字段
  2675. if(this.datatable.multiEditMode && this.isTotalNumberModule(templateJsonId)){
  2676. //module
  2677. module.addEvent("change", function(){
  2678. this.datatable._loadTotal();
  2679. if( this.sectionLine )this.sectionLine._loadTotal();
  2680. }.bind(this))
  2681. }
  2682. }
  2683. }
  2684. }.bind(this));
  2685. },
  2686. isTotalNumberModule: function(id){
  2687. if( this.sectionLine ){
  2688. return this.sectionLine.isTotalNumberModule(id)
  2689. }else{
  2690. return this.datatable.isTotalNumberModule(id)
  2691. }
  2692. },
  2693. getIndex: function(){
  2694. return this.options.index;
  2695. },
  2696. getModule: function(templateJsonId){
  2697. var _subform = this.datatable.json._subform;
  2698. if( _subform ){
  2699. var module = this.all_templateId[_subform+"_"+templateJsonId];
  2700. if( module )return module;
  2701. }
  2702. return this.all_templateId[templateJsonId];
  2703. },
  2704. get: function(templateJsonId){
  2705. return this.getModule( templateJsonId );
  2706. },
  2707. getAttachmentSite: function(json, templateJsonId, sectionKey){
  2708. //确保site最长为64,否则后台会报错
  2709. var index = this.options.index;
  2710. var baseSite;
  2711. baseSite = "." + index + "." + (json.site || templateJsonId);
  2712. var maxLength;
  2713. var sectionId = "";
  2714. if( sectionKey ){
  2715. maxLength = Math.floor((63 - baseSite.length)/2 );
  2716. sectionId = (sectionKey.length > maxLength) ? sectionKey.substr(sectionKey.length-maxLength, maxLength) : sectionKey;
  2717. sectionId = "." + sectionId;
  2718. }else{
  2719. maxLength = 64 - baseSite.length;
  2720. }
  2721. var templateId = this.datatable.json.id;
  2722. templateId = (templateId.length > maxLength) ? templateId.substr(templateId.length-maxLength, maxLength) : templateId;
  2723. return templateId + sectionId + baseSite;
  2724. },
  2725. deleteAttachment: function(){
  2726. var saveFlag = false;
  2727. for( var key in this.allField){
  2728. var module = this.allField[key];
  2729. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  2730. var array = module._getBusinessData();
  2731. (array || []).each(function(d){
  2732. saveFlag = true;
  2733. this.datatable.deleteAttachment(d.id);
  2734. for( var i=0; i<this.form.businessData.attachmentList.length; i++ ){
  2735. var attData = this.form.businessData.attachmentList[i];
  2736. if( (attData.businessId && attData.businessId === d.businessId) || attData.id === d.id ){
  2737. this.form.businessData.attachmentList.erase(attData);
  2738. break;
  2739. }
  2740. }
  2741. }.bind(this))
  2742. }
  2743. }
  2744. return saveFlag;
  2745. },
  2746. loadSequence: function(){
  2747. var sequenceTd = this.node.getElements(".mwf_sequence");
  2748. if(sequenceTd){
  2749. if( this.datatable.isShowSectionKey() ) {
  2750. if (this.datatable.json.sequenceBySection === "section") {
  2751. sequenceTd.set("text", this.options.indexInSectionLineText);
  2752. } else {
  2753. sequenceTd.set("text", this.options.indexText)
  2754. }
  2755. }else if( this.datatable.isShowSectionBy() ){
  2756. if( this.datatable.json.sequenceBy === "section" ){
  2757. sequenceTd.set("text", this.options.indexInSectionLineText );
  2758. }else {
  2759. sequenceTd.set("text", this.options.indexText)
  2760. }
  2761. }else{
  2762. sequenceTd.set("text", this.options.indexText)
  2763. }
  2764. }
  2765. },
  2766. loadZebraStyle: function(){
  2767. if ((this.options.index%2)===0 && this.datatable.json.zebraColor){
  2768. this.node.setStyle("background-color", this.datatable.json.zebraColor);
  2769. }else if(this.datatable.json.backgroundColor){
  2770. this.node.setStyle("background-color", this.datatable.json.backgroundColor);
  2771. }
  2772. },
  2773. loadEditedStyle: function(){
  2774. if (!this.datatable.multiEditMode && this.options.isEdited && this.datatable.json.editStyles){
  2775. var tds = this.node.getElements("td");
  2776. tds.setStyles(this.datatable.json.editStyles);
  2777. }
  2778. },
  2779. addNodeEvent: function(){
  2780. if(!this.datatable.multiEditMode && this.options.isEditable){
  2781. this.editFun = function(){
  2782. if( !this.options.isEdited ){
  2783. this.datatable._changeEditedLine(this)
  2784. }
  2785. }.bind(this);
  2786. this.node.addEvent("click", this.editFun)
  2787. }
  2788. },
  2789. createActions: function () {
  2790. //不允许编辑,直接返回
  2791. if(!this.options.isEditable)return;
  2792. var editActionTd = this.node.getElement(".mwf_editaction");
  2793. var moveActionTd = this.node.getElement(".mwf_moveaction");
  2794. if(this.datatable.multiEditMode){ //多行编辑模式
  2795. if(this.options.isAddable)this.createAddAction(editActionTd);
  2796. if(this.options.isDeleteable)this.createDelAction(editActionTd);
  2797. }else{ //单行编辑模式
  2798. if(this.options.isAddable)this.createAddAction(editActionTd);
  2799. if(this.options.isDeleteable)this.createDelAction(editActionTd);
  2800. this.createCompleteAction(editActionTd);
  2801. this.createCancelEditAction(editActionTd);
  2802. this.checkActionDisplay();
  2803. }
  2804. if( moveActionTd )this.createMoveAction(moveActionTd);
  2805. },
  2806. checkActionDisplay: function(){
  2807. if( this.options.isEdited ){
  2808. if( this.addLineAction )this.addLineAction.hide();
  2809. if( this.delLineAction )this.delLineAction.hide();
  2810. if( this.completeLineAction )this.completeLineAction.show();
  2811. if( this.cancelLineEditAction )this.cancelLineEditAction.show();
  2812. }else{
  2813. if( this.addLineAction )this.addLineAction.show();
  2814. if( this.delLineAction )this.delLineAction.show();
  2815. if( this.completeLineAction )this.completeLineAction.hide();
  2816. if( this.cancelLineEditAction )this.cancelLineEditAction.hide();
  2817. }
  2818. },
  2819. createAddAction: function(td){
  2820. this.addLineAction = new Element("div.addLineAction.ooicon-create", {
  2821. "styles": this.form.css.addLineAction,
  2822. "events": {
  2823. "click": function(ev){
  2824. this.datatable._insertLine( ev, this );
  2825. ev.stopPropagation();
  2826. }.bind(this)
  2827. }
  2828. }).inject(td);
  2829. },
  2830. createCompleteAction: function(td){
  2831. this.completeLineAction = new Element("div.completeLineAction.ooicon-checkmark", {
  2832. "styles": this.form.css.completeLineAction,
  2833. "events": {
  2834. "click": function(ev){
  2835. this.datatable._completeLineEdit(ev, true);
  2836. ev.stopPropagation();
  2837. }.bind(this)
  2838. }
  2839. }).inject(td);
  2840. },
  2841. createCancelEditAction: function(td){
  2842. this.cancelLineEditAction = new Element("div.cancelLineEditAction.ooicon-process-cancel", {
  2843. "styles": this.form.css.delLineAction,
  2844. "events": {
  2845. "click": function(ev){
  2846. this.datatable._cancelLineEdit(ev, this);
  2847. ev.stopPropagation();
  2848. }.bind(this)
  2849. }
  2850. }).inject(td);
  2851. },
  2852. createDelAction: function(td){
  2853. this.delLineAction = new Element("div.delLineAction.ooicon-delete", {
  2854. "styles": this.form.css.delLineAction,
  2855. "events": {
  2856. "click": function(ev){
  2857. this.datatable._deleteLine( ev, this );
  2858. // if( this.datatable.currentEditedLine === this )this.datatable.currentEditedLine = null;
  2859. ev.stopPropagation();
  2860. }.bind(this)
  2861. }
  2862. }).inject(td);
  2863. },
  2864. createMoveAction: function(td){
  2865. this.moveAction = new Element("div", {
  2866. "styles": this.form.css.datatableMoveLineAction,
  2867. "text": "↑",
  2868. "events": {
  2869. "click": function(ev){
  2870. this.datatable._moveUpLine( ev, this );
  2871. // if( this.datatable.currentEditedLine === this )this.datatable.currentEditedLine = null;
  2872. ev.stopPropagation();
  2873. }.bind(this)
  2874. }
  2875. }).inject(td);
  2876. },
  2877. changeEditMode: function( isEdited ){
  2878. if( isEdited === this.options.isEdited )return;
  2879. if( !this.options.isEditable )return;
  2880. this.options.isEdited = isEdited;
  2881. this.reload();
  2882. if( this.options.isEdited ){
  2883. this.datatable.fireEvent("editLine",[this]);
  2884. }
  2885. },
  2886. reload: function(){
  2887. this.reloading = true;
  2888. for(var key in this.all){
  2889. var module = this.all[key];
  2890. this.form.modules.erase(module);
  2891. if (this.form.all[key]) delete this.form.all[key];
  2892. if (this.form.forms[key])delete this.form.forms[key];
  2893. }
  2894. this.node.empty();
  2895. if(this.editFun){
  2896. this.node.removeEvent("click", this.editFun);
  2897. this.editFun = null;
  2898. }
  2899. this.init();
  2900. this.load();
  2901. this.reloading = false;
  2902. },
  2903. clearSubModules: function () { //把module清除掉
  2904. for(var key in this.all){
  2905. var module = this.all[key];
  2906. //如果嵌套数据模板或者数据表格,还要清除掉下级
  2907. if(module.clearSubModules)module.clearSubModules();
  2908. this.form.modules.erase(module);
  2909. if (this.form.all[key]) delete this.form.all[key];
  2910. if (this.form.forms[key])delete this.form.forms[key];
  2911. }
  2912. this.node.destroy();
  2913. this.init();
  2914. },
  2915. computeModuleData: function( when ){
  2916. for( var key in this.allField_templateId){
  2917. var module = this.allField_templateId[key];
  2918. if( module.json.compute === when ){
  2919. this.data[key] = module.getData();
  2920. }
  2921. }
  2922. },
  2923. getData: function () {
  2924. var data = this.data;
  2925. for( var key in this.allField_templateId){
  2926. var module = this.allField_templateId[key];
  2927. // var id = key.split("..").getLast();
  2928. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  2929. data[key] = module._getBusinessData();
  2930. }else{
  2931. data[key] = module.getData();
  2932. }
  2933. }
  2934. return data;
  2935. },
  2936. setData: function (data) {
  2937. this.datatable._setLineData(this, data);
  2938. },
  2939. validation: function(){
  2940. if( !this.options.isEdited || !this.options.isEditable )return true;
  2941. if( !this.validationFields())return false;
  2942. if( !this.validationCompleteLine())return false;
  2943. return true;
  2944. },
  2945. validationFields: function(){
  2946. if( !this.options.isEdited || !this.options.isEditable )return true;
  2947. var flag = true;
  2948. this.fields.each(function(field, key){
  2949. if (field.json.type!="sequence" && field.validationMode ){
  2950. field.validationMode();
  2951. if (!field.validation()) flag = false;
  2952. }
  2953. }.bind(this));
  2954. return flag;
  2955. },
  2956. validationCompleteLine: function(){
  2957. if( !this.options.isEdited || !this.options.isEditable )return true;
  2958. var flag = true;
  2959. if( !this.datatable.multiEditMode ){
  2960. if (this.datatable.json.validationCompleteLine && this.datatable.json.validationCompleteLine.code){
  2961. flag = this.form.Macro.exec(this.datatable.json.validationCompleteLine.code, this);
  2962. if (!flag) flag = MWF.xApplication.process.Xform.LP.lineNotValidation;
  2963. }
  2964. }
  2965. if (flag.toString()!=="true"){
  2966. var isTr = !layout.mobile;
  2967. this.notValidationMode(flag, isTr);
  2968. return false;
  2969. }
  2970. return true;
  2971. },
  2972. createErrorNode: function(text, isTr){
  2973. var node, tr, td;
  2974. if( this.form.json.errorStyle ){
  2975. if( this.form.json.errorStyle.type === "notice" ){
  2976. if( !this.form.errorNoticing ){ //如果是弹出
  2977. this.form.errorNoticing = true;
  2978. this.form.notice(text, "error", this.node, null, null, {
  2979. onClose : function () {
  2980. this.form.errorNoticing = false;
  2981. }.bind(this)
  2982. });
  2983. }
  2984. }else{
  2985. if( isTr ){
  2986. tr = new Element("tr");
  2987. td = new Element("td", {"colspan": this.datatable.columnCount}).inject(tr);
  2988. }
  2989. node = new Element("div",{
  2990. "styles" : this.form.json.errorStyle.node,
  2991. "text": text
  2992. });
  2993. if( td )node.inject(td);
  2994. if( this.form.json.errorStyle.close ){
  2995. var closeNode = new Element("div",{
  2996. "styles" : this.form.json.errorStyle.close ,
  2997. "events": {
  2998. "click" : function(){
  2999. // (tr || node).destroy();
  3000. this.validationMode()
  3001. }.bind(this)
  3002. }
  3003. }).inject(node);
  3004. }
  3005. }
  3006. }else{
  3007. if( isTr ){
  3008. tr = new Element("tr");
  3009. td = new Element("td", {"colspan": this.datatable.columnCount}).inject(tr);
  3010. }
  3011. node = new Element("div");
  3012. if( td )node.inject(td);
  3013. var iconNode = new Element("div", {
  3014. "styles": {
  3015. "width": "20px",
  3016. "height": "20px",
  3017. "float": "left",
  3018. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  3019. }
  3020. }).inject(node);
  3021. var textNode = new Element("div", {
  3022. "styles": {
  3023. "height": "auto",
  3024. "line-height": "20px",
  3025. "margin-left": "20px",
  3026. "color": "red",
  3027. "word-break": "keep-all"
  3028. },
  3029. "text": text
  3030. }).inject(node);
  3031. }
  3032. return tr || node;
  3033. },
  3034. notValidationMode: function(text, isTr){
  3035. if (!this.isNotValidationMode){
  3036. this.isNotValidationMode = true;
  3037. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  3038. this.node.setStyle("border-color", "red");
  3039. this.errNode = this.createErrorNode(text, isTr);
  3040. //if (this.iconNode){
  3041. // this.errNode.inject(this.iconNode, "after");
  3042. //}else{
  3043. this.errNode.inject(this.node, "after");
  3044. //}
  3045. this.showNotValidationMode(this.node);
  3046. var parentNode = this.errNode;
  3047. while( parentNode && parentNode && parentNode.offsetParent === null ){
  3048. parentNode = parentNode.getParent();
  3049. }
  3050. if (!parentNode.isIntoView()) parentNode.scrollIntoView(false);
  3051. }
  3052. },
  3053. showNotValidationMode: function(node){
  3054. var p = node.getParent("div");
  3055. if (p){
  3056. var mwftype = p.get("MWFtype") || p.get("mwftype");
  3057. if (mwftype == "tab$Content"){
  3058. if (p.getParent("div").getStyle("display")=="none"){
  3059. var contentAreaNode = p.getParent("div").getParent("div");
  3060. var tabAreaNode = contentAreaNode.getPrevious("div");
  3061. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  3062. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  3063. tabNode.click();
  3064. p = tabAreaNode.getParent("div");
  3065. }
  3066. }
  3067. this.showNotValidationMode(p);
  3068. }
  3069. },
  3070. validationMode: function(){
  3071. if (this.isNotValidationMode){
  3072. this.isNotValidationMode = false;
  3073. this.node.setStyles(this.node.retrieve("borderStyle"));
  3074. if (this.errNode){
  3075. this.errNode.destroy();
  3076. this.errNode = null;
  3077. }
  3078. }
  3079. },
  3080. resetDataWithOriginalData: function () {
  3081. var data = this.originalData;
  3082. var attachmentList = this.form.businessData.attachmentList;
  3083. for( var key in this.allField_templateId){
  3084. var module = this.allField_templateId[key];
  3085. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  3086. data[key] = (data[key] || []).filter(function(att, index){
  3087. for( var i=0; i<attachmentList.length; i++){
  3088. if( (attachmentList[i].businessId && attachmentList[i].businessId === att.businessId) || attachmentList[i].id === att.id )return true;
  3089. }
  3090. return false;
  3091. }.bind(this))
  3092. }
  3093. this.data[key] = data[key];
  3094. }
  3095. return data;
  3096. }
  3097. });
  3098. MWF.xApplication.process.Xform.DatatablePC.ImporterDatabale = new Class({
  3099. Extends: MWF.xApplication.process.Xform.DatatablePC,
  3100. initialize: function(mainDatatable){
  3101. this.mainDatatable = mainDatatable;
  3102. this.json = Object.clone( mainDatatable.json );
  3103. this.json.id = this.json.id + "_o2simula";
  3104. this.node = new Element("div", {
  3105. id: this.json.id
  3106. });
  3107. this.node.store("module", this);
  3108. this.form = mainDatatable.form;
  3109. this.field = false;
  3110. this.fieldModuleLoaded = false;
  3111. },
  3112. load: function(){
  3113. this.deleteFormData = function(data){
  3114. delete data[this.id];
  3115. this._self.form.removeEvent("getData", this._self.deleteFormData);
  3116. }.bind({_self: this, id: this.json.id});
  3117. this.form.addEvent("getData", this.deleteFormData);
  3118. this._loadModuleEvents();
  3119. if (this.fireEvent("queryLoad")){
  3120. this._queryLoaded();
  3121. this._loadUserInterface();
  3122. this._afterLoaded();
  3123. this.fireEvent("afterLoad");
  3124. // this.fireEvent("load");
  3125. }
  3126. },
  3127. _loadUserInterface: function(){
  3128. // this.fireEvent("queryLoad");
  3129. // this.editModules = [];
  3130. this.table = new Element("table").inject(this.node);
  3131. this.tBody = new Element("tbody").inject(this.table);
  3132. this.editable = true;
  3133. //是否多行同时编辑
  3134. this.multiEditMode = true;
  3135. this.data = {"data": [], "total":{}};
  3136. this._setValue(this.data);
  3137. this.lineList = [];
  3138. this.loadDatatable();
  3139. },
  3140. loadDatatable: function(){
  3141. this.loading = true;
  3142. //this._loadTitleTr();
  3143. //this._loadTemplate();
  3144. //this._loadTotalTr();
  3145. // this.templateNode = this.mainDatatable.templateNode;
  3146. // this.templateTr = this.mainDatatable.templateTr;
  3147. this.columnCount = this.mainDatatable.columnCount;
  3148. this.templateHtml = this.mainDatatable.templateHtml;
  3149. this.fireEvent("load");
  3150. this._loadDatatable(function(){
  3151. this.fieldModuleLoaded = true;
  3152. this.loading = false;
  3153. this.fireEvent("postLoad");
  3154. }.bind(this));
  3155. },
  3156. _loadDatatable: function(callback){
  3157. this._loadLineList(function(){
  3158. //this._loadTotal();
  3159. if(callback)callback();
  3160. }.bind(this));
  3161. },
  3162. _loadLine: function(container, data, index, isEdited, isNew){
  3163. var line = new MWF.xApplication.process.Xform.DatatablePC.ImporterLine(container, this, data, {
  3164. index : index,
  3165. indexText : (index+1).toString()
  3166. });
  3167. this.fireEvent("beforeLoadLine", [line]);
  3168. line.load();
  3169. this.fireEvent("afterLoadLine", [line]);
  3170. return line;
  3171. },
  3172. _addLine: function(ev, edited, d){
  3173. var data, index, line;
  3174. index = this.lineList.length;
  3175. data = this.data;
  3176. data.data.push(d||{});
  3177. this.newLineIndex = index;
  3178. this.setData( data );
  3179. line = this.getLine(index);
  3180. line.isNewAdd = true;
  3181. this.fireEvent("addLine", [{"line":line, "ev":ev}]);
  3182. this.fireEvent("change", [{"lines":[line], "type":"addline"}]);
  3183. return line;
  3184. },
  3185. // _delLine: function(line){
  3186. // this.fireEvent("deleteLine", [line]);
  3187. //
  3188. // //使用数据驱动
  3189. // var data = this.data;
  3190. // data.data.splice(line.options.index, 1);
  3191. //
  3192. // if(this.currentEditedLine === line)this.currentEditedLine = null;
  3193. // this.setData( data );
  3194. //
  3195. // // this.validationMode();
  3196. // this.fireEvent("afterDeleteLine");
  3197. //
  3198. // this.fireEvent("change", [{"lines":[line], "type":"deleteline"}]);
  3199. // },
  3200. destroy: function(){
  3201. this.clearSubModules();
  3202. var id = this.json.id;
  3203. if (this.form.all[id]) delete this.form.all[id];
  3204. if (this.form.forms[id])delete this.form.forms[id];
  3205. // if( this.form.businessData.data.hasOwnProperty(id) )delete this.form.businessData.data[id];
  3206. // if( this.form.Macro.environment.data.hasOwnProperty(id) )delete this.form.Macro.environment.data[id];
  3207. this.node.destroy();
  3208. }
  3209. });
  3210. //Excel导入时候创建的组件
  3211. MWF.xApplication.process.Xform.DatatablePC.ImporterLine = new Class({
  3212. Implements: [Options, Events],
  3213. options: {
  3214. isNew: true,
  3215. isEdited : true, //是否正在编辑
  3216. isEditable : true, //能否被编辑
  3217. isDeleteable: true, //能否被删除
  3218. },
  3219. initialize: function (node, datatable, data, options) {
  3220. this.setOptions(options);
  3221. this.node = node;
  3222. this.datatable = datatable;
  3223. this.data = data;
  3224. this.form = this.datatable.form;
  3225. this.init()
  3226. },
  3227. init: function(){
  3228. this.modules = [];
  3229. this.all = {};
  3230. this.all_templateId = {};
  3231. this.fields = [];
  3232. this.allField = {};
  3233. this.allField_templateId = {};
  3234. },
  3235. load: function(){
  3236. // if( !this.datatable.multiEditMode && this.options.isEdited )this.datatable.currentEditedLine = this;
  3237. this.loadModules();
  3238. // if( !this.datatable.multiEditMode )this.originalData = Object.clone(this.data);
  3239. },
  3240. loadModules: function(){
  3241. this.node.set("html", this.datatable.templateHtml);
  3242. var moduleNodes = this.form._getModuleNodes(this.node, true);
  3243. //合并状态或拆分状态
  3244. // var sectionKey = this.options.sectionKey || this.datatable.sectionBy;
  3245. moduleNodes.each(function (node) {
  3246. var mwfType = node.get("MWFtype");
  3247. if (mwfType === "form")return;
  3248. var _self = this;
  3249. var tJson = this.form._getDomjson(node);
  3250. if( tJson ){
  3251. var json = Object.clone(tJson);
  3252. if( !this.options.isEdited || !this.options.isEditable )json.isReadonly = true;
  3253. var templateJsonId = json.id;
  3254. var index = "0"; //this.options.index;
  3255. var id;
  3256. // if( this.datatable.isShowAllSection ){
  3257. // id = this.datatable.json.id + ".." + sectionKey + "..data.." + this.options.indexInSectionLine + ".." + json.id;
  3258. // }else if( sectionKey ){
  3259. // id = this.datatable.json.id + ".." + sectionKey + "..data.." + index + ".." + json.id;
  3260. // }else{
  3261. id = this.datatable.json.id + "..data.." + index + ".." + json.id;
  3262. // }
  3263. json.id = id;
  3264. node.set("id", id);
  3265. // if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  3266. // json.type = "AttachmentDg";
  3267. // json.ignoreSite = true;
  3268. // json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
  3269. // }
  3270. if (this.form.all[id]) this.form.all[id] = null;
  3271. if (this.form.forms[id])this.form.forms[id] = null;
  3272. var hasData = this.data.hasOwnProperty(templateJsonId);
  3273. var module = this.form._loadModule(json, node, function () {
  3274. // this.field = false; //不希望保存数据
  3275. // this._getBusinessData = function(){
  3276. // return _self.data[templateJsonId];
  3277. // };
  3278. // this._setBusinessData = function () {};
  3279. if( _self.widget )this.widget = _self.widget;
  3280. this.parentLine = _self;
  3281. this.parentDatatable = _self.datatable;
  3282. //只读方法值在页面加载的时候或者new的时候计算一下
  3283. if( this.json.compute === "show" ){
  3284. var needComputeShow = false;
  3285. if( _self.datatable.loading ) {
  3286. needComputeShow = true;
  3287. }else if( _self.options.isNew && !_self.reloading ){
  3288. needComputeShow = true;
  3289. }
  3290. if( !needComputeShow ){
  3291. this.json.compute = "create"; //
  3292. if( this.options.moduleEvents && this.options.moduleEvents.length ){ //恢复compute
  3293. var eventName = ( this.options.moduleEvents || [] ).contains("afterLoad") ? "afterLoad" : "load";
  3294. var resetCompute = function () {
  3295. this.json.compute = "show";
  3296. this.removeEvent( eventName, resetCompute );
  3297. }.bind(this)
  3298. this.addEvent(eventName, resetCompute);
  3299. }
  3300. }
  3301. }
  3302. }, true);
  3303. if(!module.parentLine)module.parentLine = this;
  3304. if(!module.parentDatatable)module.parentDatatable = this.datatable;
  3305. this.form.modules.push(module);
  3306. this.modules.push(module);
  3307. this.all[id] = module;
  3308. this.all_templateId[templateJsonId] = module;
  3309. if (module.field) {
  3310. if(this.options.isEdited ) {
  3311. if (json.type !== "Attachment" && json.type !== "AttachmentDg"){
  3312. if( module.json.compute === "save" && module.getInputData ){
  3313. this.data[templateJsonId] = module.getInputData();
  3314. }else{
  3315. this.data[templateJsonId] = module.getData();
  3316. }
  3317. }
  3318. }else if(!hasData && module.getValue ){
  3319. this.data[templateJsonId] = module.getValue();
  3320. }
  3321. this.allField[id] = module;
  3322. this.allField_templateId[templateJsonId] = module;
  3323. this.fields.push( module );
  3324. }
  3325. }
  3326. }.bind(this));
  3327. },
  3328. clearSubModules: function () { //把module清除掉
  3329. for(var key in this.all){
  3330. var module = this.all[key];
  3331. //如果嵌套数据模板或者数据表格,还要清除掉下级
  3332. if(module.clearSubModules)module.clearSubModules();
  3333. this.form.modules.erase(module);
  3334. // if( this.form.businessData.data.hasOwnProperty(key) )delete this.form.businessData.data[key];
  3335. // if( this.form.Macro.environment.data.hasOwnProperty(key) )delete this.form.Macro.environment.data[key];
  3336. if (this.form.all[key]) delete this.form.all[key];
  3337. if (this.form.forms[key])delete this.form.forms[key];
  3338. }
  3339. this.node.destroy();
  3340. this.init();
  3341. },
  3342. getModule: function(templateJsonId){
  3343. return this.all_templateId[templateJsonId];
  3344. },
  3345. get: function(templateJsonId){
  3346. return this.all_templateId[templateJsonId];
  3347. },
  3348. // computeModuleData: function( when ){
  3349. // for( var key in this.allField_templateId){
  3350. // var module = this.allField_templateId[key];
  3351. // if( module.json.compute === when ){
  3352. // this.data[key] = module.getData();
  3353. // }
  3354. // }
  3355. // },
  3356. getData: function () {
  3357. var data = this.data;
  3358. for( var key in this.allField_templateId){
  3359. var module = this.allField_templateId[key];
  3360. // var id = key.split("..").getLast();
  3361. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  3362. data[key] = module._getBusinessData();
  3363. }else{
  3364. data[key] = module.getData();
  3365. }
  3366. }
  3367. return data;
  3368. },
  3369. // validation: function(){
  3370. // if( !this.validationFields())return false;
  3371. // if( !this.validationCompleteLine())return false;
  3372. // return true;
  3373. // },
  3374. // validationFields: function(){
  3375. // var flag = true;
  3376. // this.fields.each(function(field, key){
  3377. // if (field.json.type!="sequence" && field.validationMode ){
  3378. // field.validationMode();
  3379. // if (!field.validation()) flag = false;
  3380. // }
  3381. // }.bind(this));
  3382. // return flag;
  3383. // },
  3384. validationCompleteLine: function(){
  3385. var flag = true;
  3386. if( !this.datatable.multiEditMode ){
  3387. if (this.datatable.json.validationCompleteLine && this.datatable.json.validationCompleteLine.code){
  3388. flag = this.form.Macro.exec(this.datatable.json.validationCompleteLine.code, this);
  3389. if (!flag) flag = MWF.xApplication.process.Xform.LP.lineNotValidation;
  3390. }
  3391. }
  3392. return flag;
  3393. }
  3394. });
  3395. MWF.xApplication.process.Xform.DatatablePC.Exporter = new Class({
  3396. Implements: [Options, Events],
  3397. options: {
  3398. },
  3399. initialize: function (datatable, options) {
  3400. this.setOptions(options);
  3401. this.datatable = datatable;
  3402. this.form = this.datatable.form;
  3403. this.columnJsonList = [];
  3404. },
  3405. exportToExcel : function () {
  3406. MWF.require("MWF.widget.Mask", null, false);
  3407. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  3408. // 适配移动端
  3409. if (layout.mobile) {
  3410. this.mask.load();
  3411. } else {
  3412. this.mask.loadNode(this.form.app.content);
  3413. }
  3414. this.datatable.fireEvent("beforeExport");
  3415. this.getColumnList();
  3416. var resultArr = [];
  3417. var titleArr = this.getTitleArray();
  3418. resultArr.push( titleArr );
  3419. var lineList = [];
  3420. if( this.datatable.isShowAllSection ){
  3421. lineList = this.datatable.sectionLineEdited ? this.datatable.sectionLineEdited.lineList : this.datatable.lineList;
  3422. }else if( this.datatable.isMergeRead ) {
  3423. lineList = this.datatable.lineList;
  3424. }else{
  3425. lineList = this.datatable.lineList;
  3426. }
  3427. lineList.each(function (line, index) {
  3428. var lineData = this.getLineExportData(line, index);
  3429. var p = Promise.all(lineData).then(function (arr) {
  3430. return arr;
  3431. });
  3432. resultArr.push( p );
  3433. }.bind(this));
  3434. Promise.all(resultArr).then(function ( rstArr ) {
  3435. var colWidthArr = this.getColWidthArray();
  3436. var excelName = this.getExcelName();
  3437. var arg = {
  3438. data : rstArr,
  3439. colWidthArray : colWidthArr,
  3440. title : excelName
  3441. };
  3442. this.datatable.fireEvent("export", [arg]);
  3443. new MWF.xApplication.process.Xform.DatatablePC.ExcelUtils( this.datatable ).exportToExcel(
  3444. arg.data || rstArr,
  3445. arg.title || excelName,
  3446. arg.colWidthArray || colWidthArr,
  3447. this.getDateIndexArray(), //日期格式列下标
  3448. null,
  3449. function () {
  3450. if (this.mask) {
  3451. this.mask.hide();
  3452. this.mask = null;
  3453. }
  3454. }.bind(this)
  3455. );
  3456. }.bind(this))
  3457. },
  3458. getColumnList: function(){
  3459. this.columnJsonList = [];
  3460. var ths = this.datatable.titleTr.getElements("th.mwf_origional");
  3461. var tds = this.datatable.templateTr.getElements("td.mwf_origional");
  3462. ths.each(function(th, index){
  3463. var thJson = this.form._getDomjson( th );
  3464. var mJson;
  3465. if(tds[index]){
  3466. var mNodes = this.form._getModuleNodes(tds[index]); //获取总计列内的填写组件
  3467. if( mNodes.length > 0 )mJson = this.form._getDomjson(mNodes[0]);
  3468. }
  3469. if(thJson && mJson && this.isAvaliableColumn(thJson, mJson)){
  3470. this.columnJsonList.push({
  3471. "thJson": thJson,
  3472. "title": th.get("text"),
  3473. "mJson" : mJson,
  3474. "available": true
  3475. })
  3476. }
  3477. }.bind(this));
  3478. },
  3479. getLineExportData: function(line, index ){
  3480. var exportData = [];
  3481. this.columnJsonList.each(function (column) {
  3482. var module;
  3483. if( column.mJson && column.available ){
  3484. module = line.all_templateId[column.mJson.id];
  3485. }
  3486. if ( !module ) {
  3487. exportData.push("");
  3488. }else{
  3489. var text;
  3490. if ( column.mJson.type === "Label" && module.node) {
  3491. text = module.node.get("text");
  3492. }else{
  3493. text = module.getExcelData();
  3494. }
  3495. if( !text && o2.typeOf(text) !== "number" ){
  3496. text = "";
  3497. }
  3498. exportData.push( text );
  3499. }
  3500. }.bind(this));
  3501. return exportData;
  3502. },
  3503. // getLineExportData: function(line, index ){
  3504. // var exportData = [];
  3505. // this.columnJsonList.each(function (column) {
  3506. //
  3507. // var module;
  3508. // if( column.mJson && column.available ){
  3509. // module = line.all_templateId[column.mJson.id];
  3510. // }
  3511. // if ( !module ) {
  3512. // exportData.push("");
  3513. // }else{
  3514. // var value = module.getData();
  3515. // var text = "";
  3516. //
  3517. // if( value ){
  3518. // switch (column.mJson.type) {
  3519. // case "Org":
  3520. // case "Reader":
  3521. // case "Author":
  3522. // case "Personfield":
  3523. // case "Orgfield":
  3524. // if (o2.typeOf(value) === "array") {
  3525. // var textArray = [];
  3526. // value.each(function (item) {
  3527. // if (o2.typeOf(item) === "object") {
  3528. // textArray.push(item.distinguishedName);
  3529. // } else {
  3530. // textArray.push(item);
  3531. // }
  3532. // }.bind(this));
  3533. // text = textArray.join(", \n");
  3534. // } else if (o2.typeOf(value) === "object") {
  3535. // text = value.distinguishedName;
  3536. // } else {
  3537. // text = value;
  3538. // }
  3539. // break;
  3540. // case "Combox":
  3541. // case "Address":
  3542. // text = o2.typeOf(value) === "array" ? value.join(", ") : value;
  3543. // break;
  3544. // case "Checkbox":
  3545. // var options = module.getOptionsObj();
  3546. // var value = o2.typeOf(value) === "array" ? value : [value];
  3547. // var arr = [];
  3548. // value.each( function( a, i ){
  3549. // var idx = options.valueList.indexOf( a );
  3550. // arr.push( idx > -1 ? options.textList[ idx ] : "") ;
  3551. // });
  3552. // text = arr.join(", ");
  3553. // break;
  3554. // case "Radio":
  3555. // case "Select":
  3556. // var options = module.getOptionsObj();
  3557. // var idx = options.textList.indexOf( value );
  3558. // text = idx > -1 ? options.valueList[ idx ] : "";
  3559. // break;
  3560. // case "Textarea":
  3561. // text = value;
  3562. // break;
  3563. // case "Calendar":
  3564. // text = value;
  3565. // break;
  3566. // case "Elautocomplete":
  3567. // text = value;
  3568. // break;
  3569. // case "Elcascader":
  3570. // if( typeOf(value) === "text" )
  3571. // break;
  3572. // default:
  3573. // text = value;
  3574. // break;
  3575. // }
  3576. // } else if ( column.mJson.type === "Label" && module.node) {
  3577. // text = module.node.get("text");
  3578. // }
  3579. //
  3580. // if( !text && o2.typeOf(text) !== "number" ){
  3581. // text = "";
  3582. // }
  3583. //
  3584. // exportData.push( text );
  3585. // }
  3586. // }.bind(this));
  3587. // return exportData;
  3588. // },
  3589. isAvaliableColumn : function(thJson, mJson){
  3590. if (thJson && ( thJson.isShow === false || thJson.isImpExp === false ))return false; //隐藏列,不允许导入导出
  3591. if (mJson && (mJson.type == "sequence" || mJson.cellType == "sequence") )return false; //序号列
  3592. if (mJson && ["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label",
  3593. "Elbutton","Elcarousel","Eldropdown","Elicon","Eltree"].contains(mJson.type) )return false; //图片,附件,Label列不导入导出
  3594. // if (type==="import" && module && ["Label"].contains(module.json.type))return false; //Label 不导入
  3595. return true;
  3596. },
  3597. getColWidthArray : function(){
  3598. var colWidthArr = [];
  3599. this.columnJsonList.each(function(c, index){
  3600. if ( c.available ) {
  3601. if (c.mJson && ["Org","Reader","Author","Personfield","Orgfield"].contains(c.mJson.type)) {
  3602. colWidthArr.push(340);
  3603. } else if (c.mJson && c.mJson.type === "Address") {
  3604. colWidthArr.push(170);
  3605. } else if (c.mJson && c.mJson.type === "Textarea") {
  3606. colWidthArr.push(260);
  3607. } else if (c.mJson && c.mJson.type === "Htmleditor") {
  3608. colWidthArr.push(500);
  3609. } else if (c.mJson && c.mJson.type === "TinyMCEEditor") {
  3610. colWidthArr.push(500);
  3611. } else if (c.mJson && c.mJson.type === "Calendar") {
  3612. colWidthArr.push(150);
  3613. } else {
  3614. colWidthArr.push(150);
  3615. }
  3616. }
  3617. }.bind(this));
  3618. return colWidthArr;
  3619. },
  3620. getDateIndexArray : function(){
  3621. var dateIndexArr = []; //日期格式列下标
  3622. var idx=0;
  3623. this.columnJsonList.each(function(c){
  3624. if ( c.available && c.mJson ) {
  3625. if(c.mJson.type === "Calendar")dateIndexArr.push(idx);
  3626. idx++;
  3627. }
  3628. }.bind(this));
  3629. return dateIndexArr;
  3630. },
  3631. getTitleArray : function(){
  3632. var arr = [];
  3633. this.columnJsonList.each(function(c, index){
  3634. if ( c.available && c.mJson )arr.push(c.title);
  3635. }.bind(this));
  3636. return arr;
  3637. },
  3638. getExcelName: function(){
  3639. var title;
  3640. if( this.datatable.json.excelName && this.datatable.json.excelName.code ){
  3641. title = this.form.Macro.exec(this.datatable.json.excelName.code, this);
  3642. }else{
  3643. title = MWF.xApplication.process.Xform.LP.datatableExportDefaultName;
  3644. }
  3645. var titleA = title.split(".");
  3646. if( ["xls","xlst"].contains( titleA[titleA.length-1].toLowerCase() ) ){
  3647. titleA.splice( titleA.length-1 );
  3648. }
  3649. title = titleA.join(".");
  3650. return title;
  3651. },
  3652. exportWithImportDataToExcel : function ( importedData ) {
  3653. MWF.require("MWF.widget.Mask", null, false);
  3654. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  3655. // 适配移动端
  3656. if (layout.mobile) {
  3657. this.mask.load();
  3658. } else {
  3659. this.mask.loadNode(this.form.app.content);
  3660. }
  3661. this.datatable.fireEvent("beforeExport");
  3662. this.getColumnList();
  3663. var resultArr = [];
  3664. var titleArr = this.getTitleArray("import");
  3665. titleArr.push( MWF.xApplication.process.Xform.LP.validationInfor );
  3666. resultArr.push( titleArr );
  3667. importedData.each( function( lineData, lineIndex ){
  3668. var array = [];
  3669. this.columnJsonList.each( function (obj, i) {
  3670. array.push( ( lineData[ obj.title ] || '' ).replace(/&#10;/g, "\n") );
  3671. });
  3672. array.push( lineData.errorTextListExcel ? lineData.errorTextListExcel.join("\n") : "" );
  3673. resultArr.push( array );
  3674. }.bind(this));
  3675. var colWidthArr = this.getColWidthArray();
  3676. colWidthArr.push( 300 ); //提示信息
  3677. var excelName = this.getExcelName();
  3678. var arg = {
  3679. data : resultArr,
  3680. colWidthArray : colWidthArr,
  3681. title : excelName,
  3682. withError : true
  3683. };
  3684. this.datatable.fireEvent("export", [arg]);
  3685. new MWF.xApplication.process.Xform.DatatablePC.ExcelUtils( this.datatable ).exportToExcel(
  3686. arg.data || resultArr,
  3687. arg.title || excelName,
  3688. arg.colWidthArray || colWidthArr,
  3689. this.getDateIndexArray(), //日期格式列下标
  3690. null,
  3691. function () {
  3692. if (this.mask) {
  3693. this.mask.hide();
  3694. this.mask = null;
  3695. }
  3696. }.bind(this)
  3697. );
  3698. }
  3699. });
  3700. MWF.xApplication.process.Xform.DatatablePC.Importer = new Class({
  3701. Implements: [Options, Events],
  3702. options: {
  3703. },
  3704. initialize: function (datatable, options) {
  3705. this.setOptions(options);
  3706. this.datatable = datatable;
  3707. this.form = this.datatable.form;
  3708. this.lp = MWF.xApplication.process.Xform.LP;
  3709. this.columnText = this.lp.importValidationColumnText;
  3710. this.columnTextExcel = this.lp.importValidationColumnTextExcel;
  3711. this.excelUtil = new MWF.xApplication.process.Xform.DatatablePC.ExcelUtils( this.datatable );
  3712. this.columnJsonList = [];
  3713. },
  3714. isAvaliableColumn : function(thJson, mJson){
  3715. if (thJson && ( thJson.isShow === false || thJson.isImpExp === false ))return false; //隐藏列,不允许导入导出
  3716. if (mJson && (mJson.type == "sequence" || mJson.cellType == "sequence") )return false; //序号列
  3717. if (mJson && ["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label",
  3718. "Elbutton","Elbutton","Elcarousel","Eldropdown","Elicon","Eltree"].contains(mJson.type) )return false; //图片,附件,Label列不导入导出
  3719. // if (type==="import" && module && ["Label"].contains(module.json.type))return false; //Label 不导入
  3720. return true;
  3721. },
  3722. importFromExcel : function () {
  3723. this.datatable.fireEvent("beforeImport");
  3724. this.getColumnList();
  3725. var dateColArray = this.getDateIndexArray(); //日期列
  3726. var orgTitleArray = this.getOrgTitleArray();
  3727. this.excelUtil.upload( dateColArray, function (data) {
  3728. MWF.require("MWF.widget.Mask", null, false);
  3729. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  3730. // 适配移动端
  3731. if (layout.mobile) {
  3732. this.mask.load();
  3733. } else {
  3734. this.mask.loadNode(this.form.app.content);
  3735. }
  3736. this.importedData = data;
  3737. if( !this.checkCount() )return;
  3738. this.loadSimulateModule();
  3739. this.columnJsonList.each(function (c) {
  3740. c.module = this.importerLine.getModule(c.mJson.id)
  3741. }.bind(this));
  3742. if( orgTitleArray.length > 0 ){
  3743. this.listAllOrgData( orgTitleArray, function () {
  3744. this.checkAndImport();
  3745. }.bind(this));
  3746. }else{
  3747. this.checkAndImport();
  3748. }
  3749. }.bind(this));
  3750. },
  3751. checkAndImport: function () {
  3752. this.checkData( function (flag) {
  3753. if( !flag ){
  3754. this.openErrorDlg();
  3755. }else{
  3756. this.importData()
  3757. }
  3758. this.destroySimulateModule();
  3759. }.bind(this));
  3760. },
  3761. destroySimulateModule: function(){
  3762. if( !this.importerDatatable ){
  3763. this.form.disallowSaving = false;
  3764. return;
  3765. }
  3766. this.importerDatatable.destroy();
  3767. this.importerDatatable = null;
  3768. this.form.disallowSaving = false;
  3769. },
  3770. loadSimulateModule: function(){
  3771. this.form.disallowSaving = true;
  3772. this.importerDatatable = new MWF.xApplication.process.Xform.DatatablePC.ImporterDatabale( this.datatable );
  3773. this.importerDatatable.load();
  3774. this.importerLine = this.importerDatatable.addLine({});
  3775. },
  3776. getColumnList: function(){
  3777. this.columnJsonList = [];
  3778. var ths = this.datatable.titleTr.getElements("th.mwf_origional");
  3779. var tds = this.datatable.templateTr.getElements("td.mwf_origional");
  3780. var idx = 0;
  3781. ths.each(function(th, index){
  3782. var thJson = this.form._getDomjson( th );
  3783. var mJson;
  3784. if(tds[index]){
  3785. var mNodes = this.form._getModuleNodes(tds[index]); //获取总计列内的填写组件
  3786. if( mNodes.length > 0 )mJson = this.form._getDomjson(mNodes[0]);
  3787. }
  3788. if(thJson && mJson && this.isAvaliableColumn(thJson, mJson)){
  3789. this.columnJsonList.push({
  3790. "thJson": thJson,
  3791. "title": th.get("text"),
  3792. "mJson" : mJson,
  3793. "field": mJson.id,
  3794. "index": idx,
  3795. // "module": this.importerLine.getModule(mJson.id)
  3796. // "module": this.simelateModuleMap[mJson.id]
  3797. });
  3798. idx++;
  3799. }
  3800. }.bind(this));
  3801. return this.columnJsonList;
  3802. },
  3803. getDateIndexArray : function(){
  3804. var dateIndexArr = []; //日期格式列下标
  3805. var idx=0;
  3806. this.columnJsonList.each(function(c){
  3807. if ( c.mJson && c.mJson.type === "Calendar" && (c.mJson.format === "%Y-%m-%d" || c.mJson.format === "%Y-%m-%d %H:%M:%S")) {
  3808. dateIndexArr.push(idx);
  3809. }
  3810. idx++;
  3811. }.bind(this));
  3812. return dateIndexArr;
  3813. },
  3814. getOrgTitleArray : function(){
  3815. var orgTitleArr = [];
  3816. this.columnJsonList.each(function(c){
  3817. if ( c.mJson && ["Org","Reader","Author","Personfield","Orgfield"].contains(c.mJson.type) ) {
  3818. orgTitleArr.push(c.title);
  3819. }
  3820. }.bind(this));
  3821. return orgTitleArr;
  3822. },
  3823. parseImportedData: function(){
  3824. var data = [];
  3825. this.importedData.each( function( ilineData ){
  3826. var lineData = {};
  3827. this.columnJsonList.each( function (obj, i) {
  3828. var index = obj.index;
  3829. var module = obj.module;
  3830. var json = obj.mJson;
  3831. var text = obj.title;
  3832. var d = ilineData[text] || "";
  3833. var value;
  3834. if( d === "" || d === undefined || d === null ){
  3835. value = "";
  3836. }else{
  3837. switch (json.type) {
  3838. case "Org":
  3839. case "Reader":
  3840. case "Author":
  3841. case "Personfield":
  3842. case "Orgfield":
  3843. var arr = this.stringToArray(d);
  3844. if( arr.length === 0 ){
  3845. value = this.getOrgData( d );
  3846. }else{
  3847. value = [];
  3848. arr.each( function(d, idx){
  3849. var obj = this.getOrgData( d );
  3850. value.push( obj );
  3851. }.bind(this));
  3852. }
  3853. break;
  3854. default:
  3855. value = d; //换行符&#10;
  3856. break;
  3857. }
  3858. }
  3859. lineData[ json.id ] = value;
  3860. }.bind(this));
  3861. data.push( lineData );
  3862. }.bind(this));
  3863. return data;
  3864. },
  3865. stringToArray: function(string){
  3866. return string.replace(/&#10;/g,",").split(/\s*,\s*/g ).filter(function(s){
  3867. return !!s;
  3868. });
  3869. },
  3870. importData: function(){
  3871. var data = this.parsedData; //this.parseImportedData(idata);
  3872. this.datatable.fireEvent("import", [data] );
  3873. this.datatable.setData( { "data" : data } );
  3874. this.datatable.fireEvent("afterImport", [data] );
  3875. this.datatable.fireEvent("change", [{lines: this.datatable.lineList, type : "import"}]);
  3876. if (this.mask) {
  3877. this.mask.hide();
  3878. this.mask = null;
  3879. }
  3880. this.form.notice( MWF.xApplication.process.Xform.LP.importSuccess );
  3881. },
  3882. openErrorDlg : function(){
  3883. var eData = this.importedData;
  3884. var _self = this;
  3885. var objectToString = function (obj, type) {
  3886. if(!obj)return "";
  3887. var arr = [];
  3888. Object.each(obj, function (value, key) {
  3889. if( type === "style" ){
  3890. arr.push( key + ":"+ value +";" )
  3891. }else{
  3892. arr.push( key + "='"+ value +"'" )
  3893. }
  3894. })
  3895. return arr.join(" ")
  3896. }
  3897. var htmlArray = ["<table "+ objectToString( this.datatable.json.impExpTableProperties ) +" style='"+objectToString( this.datatable.json.impExpTableStyles, "style" )+"'>"];
  3898. var titleStyle = objectToString(this.datatable.json.impExpTableTitleStyles, "style");
  3899. htmlArray.push("<tr>");
  3900. this.columnJsonList.each(function (obj, i) {
  3901. htmlArray.push( "<th style='"+titleStyle+"'>"+obj.title+"</th>" );
  3902. });
  3903. htmlArray.push("<th style='"+titleStyle+"'> "+MWF.xApplication.process.Xform.LP.validationInfor +"</th>");
  3904. htmlArray.push("</tr>" );
  3905. var contentStyles = Object.clone( this.datatable.json.impExpTableContentStyles );
  3906. if( !contentStyles[ "border-bottom" ] && !contentStyles[ "border" ] )contentStyles[ "border-bottom" ] = "1px solid #eee";
  3907. var contentStyle = objectToString( Object.merge( contentStyles, {"text-align":"left"}) , "style" );
  3908. eData.each( function( lineData, lineIndex ){
  3909. htmlArray.push( "<tr>" );
  3910. this.columnJsonList.each( function (obj, i) {
  3911. htmlArray.push( "<td style='"+contentStyle+"'>"+ ( lineData[ obj.title ] || '' ).replace(/&#10;/g,"<br/>") +"</td>" ); //换行符&#10;
  3912. });
  3913. htmlArray.push( "<td style='"+contentStyle+"'>"+( lineData.errorTextList ? lineData.errorTextList.join("<br/>") : "" )+"</td>" );
  3914. htmlArray.push( "</tr>" );
  3915. }.bind(this));
  3916. htmlArray.push( "</table>" );
  3917. var width = this.datatable.json.impExpDlgWidth || 1000;
  3918. var height = this.datatable.json.impExpDlgHeight || 700;
  3919. width = width.toInt();
  3920. height = height.toInt();
  3921. var div = new Element("div", { style : "padding:10px;", html : htmlArray.join("") });
  3922. var dlg = o2.DL.open({
  3923. "style" : this.form.json.dialogStyle || "user",
  3924. "title": MWF.xApplication.process.Xform.LP.importFail,
  3925. "content": div,
  3926. "offset": {"y": 0},
  3927. "isMax": true,
  3928. "width": width,
  3929. "height": height,
  3930. "buttonList": [
  3931. {
  3932. "type": "exportWithError",
  3933. "text": MWF.xApplication.process.Xform.LP.datagridExport,
  3934. "action": function () { _self.exportWithImportDataToExcel(eData); }
  3935. },
  3936. {
  3937. "type": "cancel",
  3938. "text": MWF.LP.process.button.cancel,
  3939. "action": function () { dlg.close(); }
  3940. }
  3941. ],
  3942. "onPostClose": function(){
  3943. dlg = null;
  3944. }.bind(this)
  3945. });
  3946. if (this.mask) {
  3947. this.mask.hide();
  3948. this.mask = null;
  3949. }
  3950. },
  3951. checkCount: function(){
  3952. var idata = this.importedData;
  3953. var lp = MWF.xApplication.process.Xform.LP;
  3954. var exceeded = false;
  3955. var maxCount = this.datatable.json.maxCount ? this.datatable.json.maxCount.toInt() : 0;
  3956. if( maxCount > 0 && idata.length > maxCount )exceeded = true;
  3957. var less = false;
  3958. var minCount = this.datatable.json.minCount ? this.datatable.json.minCount.toInt() : 0;
  3959. if( minCount > 0 && idata.length < minCount) less = true;
  3960. if( exceeded ) {
  3961. var text = lp.importTooManyNotice.replace("{n1}", idata.length).replace("{n2}", this.datatable.json.maxCount);
  3962. this.form.notice(text, "error");
  3963. return false;
  3964. }
  3965. if( less ){
  3966. var text = lp.importTooFewNotice.replace("{n1}", idata.length).replace("{n2}", this.datatable.json.minCount );
  3967. this.form.notice(text,"error");
  3968. return false;
  3969. }
  3970. return true;
  3971. },
  3972. checkData : function( callback){
  3973. this.parsedData = this.parseImportedData();
  3974. this.isImportSuccess = true;
  3975. this.checkLineData(0, function () {
  3976. var arg = {
  3977. validted : this.isImportSuccess,
  3978. data : this.importedData
  3979. };
  3980. this.datatable.fireEvent( "validImport", [arg] );
  3981. callback( arg.validted )
  3982. }.bind(this));
  3983. },
  3984. checkLineData: function(lineIndex, callback){
  3985. if( lineIndex < this.importedData.length ){
  3986. this._checkLineData(this.importedData[lineIndex], lineIndex, function (flag) {
  3987. lineIndex++;
  3988. if( !flag )this.isImportSuccess = false;
  3989. this.checkLineData(lineIndex, callback);
  3990. }.bind(this));
  3991. }else{
  3992. if(callback)callback();
  3993. }
  3994. },
  3995. _checkLineData: function(lineData, lineIndex, callback){
  3996. lineData.errorTextList = lineData.errorTextList || [];
  3997. lineData.errorTextListExcel = lineData.errorTextListExcel || [];
  3998. var parsedLineData = (this.parsedData && this.parsedData[lineIndex]) ? this.parsedData[lineIndex] : [];
  3999. this.checkModuleData(0, lineData, parsedLineData, function () {
  4000. var flag = !lineData.errorTextList.length;
  4001. callback(flag);
  4002. });
  4003. },
  4004. checkModuleData: function(index, lineData, parsedLineData, callback){
  4005. if( index < this.columnJsonList.length ){
  4006. var result = this._checkModuleData(this.columnJsonList[index], lineData, parsedLineData);
  4007. Promise.resolve(result).then(function (flag) {
  4008. index++;
  4009. this.checkModuleData(index, lineData, parsedLineData, callback);
  4010. }.bind(this))
  4011. }else{
  4012. if(callback)callback();
  4013. }
  4014. },
  4015. _checkModuleData: function (columnJson, lineData, parsedLineData) {
  4016. var index = columnJson.index;
  4017. var json = columnJson.mJson;
  4018. var module = columnJson.module;
  4019. var text = columnJson.title;
  4020. var colInfor = this.columnText.replace( "{n}", index+1 );
  4021. var colInforExcel = this.columnTextExcel.replace( "{n}", this.excelUtil.index2ColName( index ) );
  4022. var d = lineData[text] || "";
  4023. var parsedD = parsedLineData[json.id] || "";
  4024. var lp = this.lp;
  4025. var flag = true;
  4026. if(d){
  4027. switch (json && json.type) {
  4028. case "Org":
  4029. case "Reader":
  4030. case "Author":
  4031. case "Personfield":
  4032. case "Orgfield":
  4033. var arr = this.stringToArray(d);
  4034. arr.each( function(d, idx){
  4035. var obj = this.getOrgData( d );
  4036. if( obj.errorText ){
  4037. lineData.errorTextList.push( colInfor + obj.errorText + lp.fullstop );
  4038. lineData.errorTextListExcel.push( colInforExcel + obj.errorText + lp.fullstop );
  4039. flag = false;
  4040. }
  4041. }.bind(this));
  4042. break;
  4043. case "Number":
  4044. case "Currency":
  4045. case "Elnumber":
  4046. if (isNaN(d)){
  4047. lineData.errorTextList.push( colInfor + d + lp.notValidNumber + lp.fullstop );
  4048. lineData.errorTextListExcel.push( colInforExcel + d + lp.notValidNumber + lp.fullstop );
  4049. flag = false;
  4050. }
  4051. break;
  4052. case "Calendar":
  4053. case "Eldate":
  4054. case "Eldatetime":
  4055. if( json.format === "%Y-%m-%d" || json.format === "%Y-%m-%d %H:%M:%S" ){
  4056. if( !( isNaN(d) && !isNaN(Date.parse(d) ))){
  4057. lineData.errorTextList.push(colInfor + d + lp.notValidDate + lp.fullstop );
  4058. lineData.errorTextListExcel.push( colInforExcel + d + lp.notValidDate + lp.fullstop );
  4059. flag = false;
  4060. }
  4061. }
  4062. break;
  4063. default:
  4064. break;
  4065. }
  4066. }
  4067. if (module && module.setData && json.type !== "Address"){
  4068. var hasError = false;
  4069. if(["Org","Reader","Author","Personfield","Orgfield"].contains(json.type)){
  4070. if(o2.typeOf(parsedD)==="array" && parsedD.length){
  4071. hasError = parsedD.some(function (item) { return item.errorText; });
  4072. flag = false;
  4073. }
  4074. }
  4075. if(!hasError){
  4076. module.setExcelData(parsedD);
  4077. var ps = [];
  4078. if( module.moduleExcelAG )ps.push( module.moduleExcelAG );
  4079. if( module.moduleValueAG && !ps.contains(module.moduleValueAG) )ps.push( module.moduleValueAG );
  4080. if( module.moduleSelectAG && !ps.contains(module.moduleSelectAG) )ps.push( module.moduleSelectAG );
  4081. return Promise.all( ps ).then(function () {
  4082. var result = module.validationExcel();
  4083. if ( result && result.length ){
  4084. lineData.errorTextList.push(colInfor + result.join("\n") );
  4085. lineData.errorTextListExcel.push( colInforExcel + result.join("\n"));
  4086. flag = false;
  4087. }
  4088. parsedLineData[json.id] = module.getData();
  4089. return flag;
  4090. })
  4091. }
  4092. }
  4093. return flag
  4094. },
  4095. exportWithImportDataToExcel: function(eData){
  4096. var exporter = new MWF.xApplication.process.Xform.DatatablePC.Exporter(this.datatable);
  4097. exporter.exportWithImportDataToExcel(eData)
  4098. },
  4099. getOrgData : function( str ){
  4100. str = str.trim();
  4101. var flag = str.substr(str.length-2, 2);
  4102. switch (flag.toLowerCase()){
  4103. case "@i":
  4104. return this.identityMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  4105. case "@p":
  4106. return this.personMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  4107. case "@u":
  4108. return this.unitMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  4109. case "@g":
  4110. return this.groupMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  4111. default:
  4112. return this.identityMap[str] ||
  4113. this.personMap[str] ||
  4114. this.unitMap[str] ||
  4115. this.groupMap[str] ||
  4116. {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  4117. }
  4118. },
  4119. listAllOrgData : function (orgTitleList, callback) {
  4120. var identityList = [], personList = [], unitList = [], groupList = [];
  4121. if( orgTitleList.length > 0 ){
  4122. this.importedData.each( function( lineData, lineIndex ){
  4123. // if( lineIndex === 0 )return;
  4124. orgTitleList.each( function (title, index) {
  4125. if( !lineData[title] )return;
  4126. var arr = this.stringToArray(lineData[title]);
  4127. arr.each( function( a ){
  4128. a = a.trim();
  4129. var flag = a.substr(a.length-2, 2);
  4130. switch (flag.toLowerCase()){
  4131. case "@i":
  4132. identityList.push( a ); break;
  4133. case "@p":
  4134. personList.push( a ); break;
  4135. case "@u":
  4136. unitList.push( a ); break;
  4137. case "@g":
  4138. groupList.push( a ); break;
  4139. default:
  4140. identityList.push( a );
  4141. personList.push( a );
  4142. unitList.push( a );
  4143. groupList.push( a );
  4144. break;
  4145. }
  4146. })
  4147. }.bind(this))
  4148. }.bind(this));
  4149. var identityLoaded, personLoaded, unitLoaded, groupLoaded;
  4150. var check = function () {
  4151. if( identityLoaded && personLoaded && unitLoaded && groupLoaded ){
  4152. if(callback)callback();
  4153. }
  4154. };
  4155. this.identityMap = {};
  4156. if( identityList.length ){
  4157. identityList = identityList.unique();
  4158. o2.Actions.load("x_organization_assemble_express").IdentityAction.listObject({ identityList : identityList }, function (json) {
  4159. json.data.each( function (d) { this.identityMap[ d.matchKey ] = d; }.bind(this));
  4160. identityLoaded = true;
  4161. check();
  4162. }.bind(this))
  4163. }else{
  4164. identityLoaded = true;
  4165. check();
  4166. }
  4167. this.personMap = {};
  4168. if( personList.length ){
  4169. personList = personList.unique();
  4170. o2.Actions.load("x_organization_assemble_express").PersonAction.listObject({ personList : personList }, function (json) {
  4171. json.data.each( function (d) { this.personMap[ d.matchKey ] = d; }.bind(this));
  4172. personLoaded = true;
  4173. check();
  4174. }.bind(this))
  4175. }else{
  4176. personLoaded = true;
  4177. check();
  4178. }
  4179. this.unitMap = {};
  4180. if( unitList.length ){
  4181. unitList = unitList.unique();
  4182. o2.Actions.load("x_organization_assemble_express").UnitAction.listObject({ unitList : unitList }, function (json) {
  4183. json.data.each( function (d) { this.unitMap[ d.matchKey ] = d; }.bind(this));
  4184. unitLoaded = true;
  4185. check();
  4186. }.bind(this))
  4187. }else{
  4188. unitLoaded = true;
  4189. check();
  4190. }
  4191. this.groupMap = {};
  4192. if( groupList.length ){
  4193. groupList = groupList.unique();
  4194. o2.Actions.load("x_organization_assemble_express").GroupAction.listObject({ groupList : groupList }, function (json) {
  4195. json.data.each( function (d) { this.groupMap[ d.matchKey ] = d; }.bind(this));
  4196. groupLoaded = true;
  4197. check();
  4198. }.bind(this))
  4199. }else{
  4200. groupLoaded = true;
  4201. check();
  4202. }
  4203. }
  4204. }
  4205. });
  4206. MWF.xDesktop.requireApp("Template", "utils.ExcelUtils", null, false);
  4207. MWF.xApplication.process.Xform.DatatablePC.ExcelUtils = new Class({
  4208. Extends: MWF.xApplication.Template.utils.ExcelUtils,
  4209. initialize: function(){
  4210. this.sheet2JsonOptions = {};
  4211. this.pollyfill();
  4212. }
  4213. });