DatatablePC.js 143 KB

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