Datatemplate.js 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527
  1. /**
  2. * 数据模板数据结构.
  3. * @typedef {Array} DatatemplateData
  4. * @example
  5. [ //数据模板数据条目
  6. {
  7. "org": [{
  8. "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
  9. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  10. "name": "张三",
  11. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  12. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  13. "unitLevelName": "兰德纵横/市场营销部",
  14. "unitName": "市场营销部"
  15. }],
  16. "org_1": [{
  17. "distinguishedName": "张三@bf007525-99a3-4178-a474-32865bdddec8@I",
  18. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  19. "name": "张三",
  20. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  21. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  22. "unitLevelName": "兰德纵横/市场营销部",
  23. "unitName": "市场营销部"
  24. }, {
  25. "distinguishedName": "李四@bf007525-99a3-4178-a474-32865bdddec8@I",
  26. "id": "bf007525-99a3-4178-a474-32865bdddec8",
  27. "name": "李四",
  28. "person": "0c828550-d8ab-479e-9880-09a59332f1ed",
  29. "unit": "9e6ce205-86f6-4d84-96e1-83147567aa8d",
  30. "unitLevelName": "兰德纵横/市场营销部",
  31. "unitName": "市场营销部"
  32. }],
  33. "number": "111",
  34. "textfield": "杭州",
  35. "attachment": [
  36. {
  37. "activityName": "拟稿",
  38. "extension": "jpg",
  39. "id": "9514758e-9e28-4bfe-87d7-824f2811f173",
  40. "lastUpdateTime": "2020-12-09 21:48:03",
  41. "length": 452863.0,
  42. "name": "111.jpg",
  43. "person": "李四@lisi@P"
  44. }
  45. ]
  46. },
  47. ...
  48. ]
  49. */
  50. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  51. /** @class Datatemplate 数据模板组件。自定义结构和样式的多行数据编辑组件。
  52. * @o2cn 数据模板
  53. * @example
  54. * //可以在脚本中获取该组件
  55. * //方法1:
  56. * var datatemplate = this.form.get("name"); //获取组件
  57. * //方法2
  58. * var datatemplate = this.target; //在组件事件脚本中获取
  59. * @extends MWF.xApplication.process.Xform.$Module
  60. * @o2category FormComponents
  61. * @since v6.2
  62. * @o2range {Process|CMS|Protal}
  63. * @hideconstructor
  64. */
  65. MWF.xApplication.process.Xform.Datatemplate = MWF.APPDatatemplate = new Class(
  66. /** @lends MWF.xApplication.process.Xform.Datatemplate# */
  67. {
  68. Implements: [Events],
  69. Extends: MWF.APP$Module,
  70. isEdit: false,
  71. options: {
  72. /**
  73. * 所有内容加载后执行(包括异步加载)。
  74. * @event MWF.xApplication.process.Xform.Datatemplate#afterLoad
  75. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  76. */
  77. /**
  78. * 每初始化一个条目,但未加载的时候触发,通过this.event可以获取条目对象。
  79. * @event MWF.xApplication.process.Xform.Datatemplate#beforeLoadLine
  80. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  81. */
  82. /**
  83. * 每一个条目加载后时候触发,通过this.event可以获取条目对象。
  84. * @event MWF.xApplication.process.Xform.Datatemplate#afterLoadLine
  85. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  86. */
  87. /**
  88. * 数据模板改变时触发。通过this.event.lines可以获取修改的条目数组,this.event.type可以获得修改的类型。<br/>
  89. * <table>
  90. * <tr><th><b>this.event.type</b></th><th><b>触发类型</b></th><th><b>this.event.lines</b></th></tr>
  91. * <tr><td>addline</td><td>添加一行</td><td>添加的行数组</td></tr>
  92. * <tr><td>deleteline</td><td>删除一行</td><td>删除的行数组</td></tr>
  93. * <tr><td>deletelines</td><td>删除多行</td><td>删除的行数组</td></tr>
  94. * <tr><td>editmodule</td><td>字段值改变时</td><td>this.event.lines为编辑的行数组<br/>this.event.module为修改的字段</td></tr>
  95. * <tr><td>import</td><td>导入数据后</td><td>数据模板所有行</td></tr>
  96. * </table>
  97. * @event MWF.xApplication.process.Xform.Datatemplate#change
  98. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  99. */
  100. /**
  101. * 添加条目时触发。通过this.event.line可以获取对应的条目对象,this.event.ev可以获得事件触发的Event。
  102. * @event MWF.xApplication.process.Xform.Datatemplate#addLine
  103. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  104. */
  105. /**
  106. * 删除条目前触发。通过this.event可以获取对应的条目对象。
  107. * @event MWF.xApplication.process.Xform.Datatemplate#deleteLine
  108. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  109. */
  110. /**
  111. * 删除条目后触发。
  112. * @event MWF.xApplication.process.Xform.Datatemplate#afterDeleteLine
  113. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  114. */
  115. /**
  116. * 导出前触发。
  117. * @event MWF.xApplication.process.Xform.DatatablePC#beforeExport
  118. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  119. */
  120. /**
  121. * 导出excel的时候触发,this.event指向导出的数据,您可以通过修改this.event来修改数据。
  122. * @event MWF.xApplication.process.Xform.Datatemplate#export
  123. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  124. * @example
  125. * <caption>this.event数据格式如下:</caption>
  126. * {
  127. * data : [
  128. * ["姓名","性别","学历","专业","出生日期","毕业日期"], //标题
  129. * [ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
  130. * [ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
  131. * ], //导出的数据
  132. * colWidthArray : [100, 50, 100, 200, 150, 150], //每列宽度
  133. * title : "xxxx" //导出的excel文件标题
  134. * }
  135. */
  136. /**
  137. * 导入前触发。
  138. * @event MWF.xApplication.process.Xform.DatatablePC#beforeImport
  139. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  140. */
  141. /**
  142. * 在导入excel,进行数据校验后触发,this.event指向导入的数据。
  143. * @event MWF.xApplication.process.Xform.Datatemplate#validImport
  144. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  145. * @example
  146. * <caption>this.event数据格式如下:</caption>
  147. * {
  148. * data : [
  149. * {
  150. * "姓名" : "张三",
  151. * "性别" : "男",
  152. * "学历" : "大学本科",
  153. * "专业" : "计算机",
  154. * "出生日期" : "aa01-1-2",
  155. * "毕业日期" : "2019-9-2",
  156. * "errorTextList" : [
  157. * "第5列:aa01-1-2不是正确的日期格式。"
  158. * ] //校验出的错误信息,如果该行数据正确,则无该字段
  159. * }
  160. * ...
  161. * ], //导入的数据
  162. * "validted" : true //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
  163. * }
  164. */
  165. /**
  166. * 在导入excel,数据校验成功将要设置回数据模板的时候触发,this.event指向整理过的导入数据,格式见{@link DatatemplateData}。
  167. * @event MWF.xApplication.process.Xform.Datatemplate#import
  168. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  169. */
  170. /**
  171. * 在导入excel,数据设置回数据模板以后触发,this.event指向整理过的导入数据,格式见{@link DatatemplateData}。
  172. * @event MWF.xApplication.process.Xform.Datatemplate#afterImport
  173. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  174. */
  175. "moduleEvents": ["queryLoad","postLoad","load", "afterLoad",
  176. "beforeLoadLine", "afterLoadLine", "change", "addLine", "deleteLine", "afterDeleteLine", "beforeExport",
  177. "beforeImport", "export", "import", "validImport", "afterImport"]
  178. },
  179. initialize: function(node, json, form, options){
  180. this.node = $(node);
  181. this.node.store("module", this);
  182. this.json = json;
  183. this.form = form;
  184. this.field = true;
  185. this.fieldModuleLoaded = false;
  186. },
  187. load: function(){
  188. this._loadModuleEvents();
  189. if (this.fireEvent("queryLoad")){
  190. this._queryLoaded();
  191. this._loadUserInterface();
  192. this._loadStyles();
  193. this._loadDomEvents();
  194. //this._loadEvents();
  195. this._afterLoaded();
  196. this.fireEvent("afterLoad");
  197. // this.fireEvent("load");
  198. }
  199. },
  200. _loadMergeEditNodeByScript: function(){
  201. if (this.json.sectionMergeEditScript && this.json.sectionMergeEditScript.code) {
  202. var data = this.form.Macro.exec(this.json.sectionMergeEditScript.code, this);
  203. this._setBusinessData( data );
  204. //this._loadUserInterface();
  205. }
  206. },
  207. _loadMergeEditNodeByDefault: function(){
  208. var data = this.getSortedSectionData();
  209. var businessData = [];
  210. data.each(function(d){
  211. d.data = d.data || [];
  212. businessData = businessData.concat( d.data );
  213. });
  214. this._setBusinessData(businessData);
  215. //this._loadUserInterface();
  216. },
  217. _loadUserInterface: function(){
  218. // this.fireEvent("queryLoad");
  219. debugger;
  220. this.loading = true;
  221. if( this.isSectionMergeEdit() ){ //区段合并,删除区段值合并数据后编辑
  222. if( this.json.mergeTypeEdit === "script" ){
  223. this._loadMergeEditNodeByScript();
  224. }else{
  225. this._loadMergeEditNodeByDefault();
  226. }
  227. }
  228. //区段合并展现
  229. this.isMergeRead = this.isSectionMergeRead();
  230. //启用区段且显示所有区段
  231. this.sectionBy = this._getSectionBy();
  232. this.isShowAllSection = this.isAllSectionShow();
  233. var iconNode = this.node.getElement("div[o2icon='datatemplate']");
  234. if(iconNode)iconNode.destroy();
  235. // this.editModules = [];
  236. this.node.setStyle("overflow-x", "auto");
  237. this.node.setStyle("overflow-y", "hidden");
  238. this.editable = !(this.readonly || (this.json.isReadonly === true) || (this.form.json.isReadonly === true));
  239. if( this.isMergeRead )this.editable = false;
  240. if (this.editable && this.json.editableScript && this.json.editableScript.code){
  241. this.editable = this.form.Macro.exec(((this.json.editableScript) ? this.json.editableScript.code : ""), this);
  242. }
  243. this.getRelativeId();
  244. //允许导入
  245. this.importenable = this.editable && (this.importActionIdList.length > 0) &&
  246. (this.json.impexpType === "impexp" || this.json.impexpType === "imp");
  247. //允许导出
  248. this.exportenable = (this.exportActionIdList.length > 0) && (this.json.impexpType === "impexp" || this.json.impexpType === "exp");
  249. if( this.isShowAllSection ){
  250. this.data = this._getAllSectionData();
  251. }else if( this.isMergeRead ){
  252. this.data = this.getSectionMergeReadData();
  253. }else{
  254. this.data = this._getValue();
  255. if( !this._getBusinessData() ){
  256. this.isNew = true;
  257. this._setValue(this.data);
  258. }
  259. }
  260. this.lineList = [];
  261. this.sectionlineList = [];
  262. //this.data为object的时候才有值
  263. // this.lineMap = {};
  264. // this.totalModules = [];
  265. this._loadStyles();
  266. //获取html模板和json模板
  267. this.getTemplate();
  268. if( !this.form.isLoaded ){ //如果表单还没加载完成
  269. //通过表单的afterModulesLoad事件设置节点外的操作:添加、删除、导入、导出
  270. this.setOuterActionEvents();
  271. }
  272. //隐藏节点
  273. this.node.getChildren().hide();
  274. this.fireEvent("load");
  275. this._loadDataTemplate(function(){
  276. // this._loadImportExportAction();
  277. this.fieldModuleLoaded = true;
  278. this.loading = false;
  279. this.fireEvent("postLoad");
  280. }.bind(this));
  281. },
  282. /*
  283. * @summary 重新加载数据表格。会触发load和postLoad事件。
  284. * @example
  285. * this.form.get("fieldId").reload(); //重新加载
  286. */
  287. reload: function( isReloadTemplate ){
  288. this.reloading = true;
  289. // this.editModules = [];
  290. this.getRelativeId();
  291. this.checkMerge( this.getValue() );
  292. this.clearSubModules();
  293. this._loadStyles();
  294. if( isReloadTemplate ){ //重新获取html模板和json模板
  295. this.node.getChildren().setStyle("display", "");
  296. this.getTemplate();
  297. this.node.getChildren().hide();
  298. }
  299. this.lineList = [];
  300. this.sectionlineList = [];
  301. this.fireEvent("load");
  302. this._loadDataTemplate(function(){
  303. // this._loadImportExportAction();
  304. this.reloading = false;
  305. this.fireEvent("postLoad");
  306. }.bind(this));
  307. },
  308. getRelativeId: function(){
  309. this.outerAddActionIdList = (this.json.outerAddActionId || "").split(",");
  310. this.outerDeleteActionIdList = (this.json.outerDeleteActionId || "").split(",");
  311. this.outerSelectAllIdList = (this.json.outerSelectAllId || "").split(",");
  312. this.addActionIdList = (this.json.addActionId || "").split(",");
  313. this.deleteActionIdList = (this.json.deleteActionId || "").split(",");
  314. this.sequenceIdList = (this.json.sequenceId || "").split(",");
  315. this.selectorId = this.json.selectorId;
  316. this.importActionIdList = (this.json.importActionId || "").split(",");
  317. this.exportActionIdList = (this.json.exportActionId || "").split(",");
  318. },
  319. getTemplate: function(){
  320. this.templateJson = {};
  321. this.templateHtml = this.node.get("html");
  322. var moduleNodes = this.form._getModuleNodes(this.node);
  323. moduleNodes.each(function (node) {
  324. if (node.get("MWFtype") !== "form") {
  325. var json = this.form._getDomjson(node);
  326. this.templateJson[json.id] = json ;
  327. }
  328. }.bind(this));
  329. },
  330. _loadStyles: function(){
  331. this.node.setStyles(this.json.styles);
  332. this.node.set(this.json.properties);
  333. },
  334. _getOuterActionModules: function( idList ){ //判断不在数据模板中,但是在表单内的Id
  335. var list = [];
  336. idList.each( function (id) {
  337. var module = this._getModuleByPath(id);
  338. var tId = id.split("..").getLast();
  339. if( !this.templateJson.hasOwnProperty(tId) && module ){
  340. list.push( module );
  341. }
  342. }.bind(this));
  343. return list;
  344. },
  345. _setOuterActionEvents: function(){
  346. this.addActionList = this._getOuterActionModules( [].concat(this.addActionIdList, this.outerAddActionIdList) );
  347. this.addActionList.each( function (module) {
  348. var addEvent = function (){
  349. module.node.addEvents({"click": function(e){
  350. this._addLine(e);
  351. }.bind(this)});
  352. if( !this.editable )module.node.hide();
  353. }.bind(this);
  354. if( module.json.type.substr(0, 2) === "El" ){
  355. module.vm ? addEvent() : module.addEvent("load", addEvent);
  356. }else{
  357. addEvent();
  358. }
  359. }.bind(this));
  360. this.deleteActionList = this._getOuterActionModules( [].concat( this.outerDeleteActionIdList ) );
  361. this.deleteActionList.each( function (module) {
  362. var addEvent = function (){
  363. module.node.addEvents({"click": function(e){
  364. this._deleteSelectedLine(e);
  365. }.bind(this)});
  366. if( !this.editable )module.node.hide();
  367. }.bind(this);
  368. if( module.json.type.substr(0, 2) === "El" ){
  369. module.vm ? addEvent() : module.addEvent("load", addEvent);
  370. }else{
  371. addEvent();
  372. }
  373. }.bind(this));
  374. this.selectAllList = this._getOuterActionModules( this.outerSelectAllIdList );
  375. this.selectAllList.each( function (module) {
  376. // module.setData(""); //默认不选中
  377. var addEvent = function (){
  378. module.node.addEvents({"click": function(e){
  379. this._checkSelectAll(e);
  380. }.bind(this)});
  381. if( !this.editable )module.node.hide();
  382. }.bind(this);
  383. if( module.json.type.substr(0, 2) === "El" ){
  384. module.vm ? addEvent() : module.addEvent("load", addEvent);
  385. }else{
  386. addEvent();
  387. }
  388. }.bind(this));
  389. this.selectAllSelector = this.selectAllList[0];
  390. if(this.selectAllSelector){
  391. this.unselectAll();
  392. }
  393. this.importActionList = this._getOuterActionModules( this.importActionIdList );
  394. this.importActionList.each( function (module) {
  395. var addEvent = function (){
  396. module.node.addEvents({"click": function(e){
  397. this.importFromExcel();
  398. }.bind(this)});
  399. if( !this.editable )module.node.hide();
  400. }.bind(this);
  401. if( module.json.type.substr(0, 2) === "El" ){
  402. module.vm ? addEvent() : module.addEvent("load", addEvent);
  403. }else{
  404. addEvent();
  405. }
  406. }.bind(this));
  407. this.exportActionList = this._getOuterActionModules( this.exportActionIdList );
  408. this.exportActionList.each( function (module) {
  409. var addEvent = function (){
  410. module.node.addEvents({"click": function(e){
  411. this.exportToExcel();
  412. }.bind(this)})
  413. }.bind(this);
  414. if( module.json.type.substr(0, 2) === "El" ){
  415. module.vm ? addEvent() : module.addEvent("load", addEvent);
  416. }else{
  417. addEvent();
  418. }
  419. }.bind(this));
  420. },
  421. setOuterActionEvents: function(){
  422. this.bindEvent = function () {
  423. this._setOuterActionEvents();
  424. this.fireEvent("afterLoad");
  425. //加载完成以后,删除事件
  426. this.form.removeEvent("afterModulesLoad", this.bindEvent );
  427. }.bind(this);
  428. //去要表单的所有组件加载完成以后再去获取外部组件
  429. this.form.addEvent("afterModulesLoad", this.bindEvent );
  430. },
  431. isShowSectionKey: function(){
  432. return this.json.showSectionKey && this.isMergeRead ;
  433. },
  434. isShowSectionBy: function(){
  435. return this.json.showSectionBy && this.isShowAllSection ;
  436. },
  437. isSectionData: function(){ //数据是否经过区段处理
  438. var data = this.getBusinessDataById();
  439. if( o2.typeOf( data ) === "object" ){
  440. var keys = Object.keys(data);
  441. // if( o2.typeOf(data[keys[0]]) === "array" ){
  442. // return true;
  443. // }
  444. for( var i=0; i<keys.length; i++ ){
  445. var key = keys[i];
  446. if( key !== "data" ){
  447. if( o2.typeOf(data[key]) === "array" ){
  448. return true;
  449. }
  450. }
  451. }
  452. }
  453. return false;
  454. },
  455. /**
  456. * @summary 当数据模板设置为区段合并展现、区段合并编辑时,可以使用本方法获取所有区段数据。
  457. * @return {Object} 对象.
  458. * @example
  459. * var data = this.form.get("fieldId").getAllSectionData();
  460. * //data格式如下:
  461. * {
  462. * "3455b82a-399c-4ee4-b9b9-e70ae40fbaf1": [ //区段1的key和data
  463. * {
  464. * "good": "yf",
  465. * "number_2": 11,
  466. * "prize": 1
  467. * }
  468. * ],
  469. * "83de86fc-60bc-4b4c-955c-1085915865a4": [ //区段2的key和data
  470. * {
  471. * "good": "yf",
  472. * "number_2": 11,
  473. * "prize": 10
  474. * }
  475. * ]
  476. * }
  477. */
  478. getAllSectionData: function(){
  479. return this.getBusinessDataById();
  480. },
  481. _getAllSectionData: function(){
  482. var bData = this.getBusinessDataById();
  483. var flag = false;
  484. if( !bData ){
  485. flag = true;
  486. bData = {};
  487. }
  488. if( !bData[this.sectionBy] ){
  489. flag = true;
  490. this.isNew = true;
  491. bData[this.sectionBy] = this.getValue();
  492. }
  493. if( flag )this.setBusinessDataById( bData );
  494. this.dataWithSectionBy = this.getAllSortedSectionData();
  495. return flag ? this.getBusinessDataById() : bData;
  496. },
  497. getAllSortedSectionData: function(){ //获取合并排序后的数据
  498. var data = this.getBusinessDataById();
  499. var array = [];
  500. for( var key in data ){
  501. array.push({
  502. sectionKey: key,
  503. key: key,
  504. data: data[key]
  505. })
  506. }
  507. if( this.json.sectionDisplaySortScript && this.json.sectionDisplaySortScript.code){
  508. array.sort( function(a, b){
  509. this.form.Macro.environment.event = {
  510. "a": a,
  511. "b": b
  512. };
  513. var flag = this.form.Macro.exec(this.json.sectionDisplaySortScript.code, this);
  514. this.form.Macro.environment.event = null;
  515. return flag;
  516. }.bind(this))
  517. }
  518. return array;
  519. },
  520. getSectionMergeReadData: function(){
  521. switch (this.json.mergeTypeRead) {
  522. case "dataScript":
  523. if (this.json.sectionMergeReadDataScript && this.json.sectionMergeReadDataScript.code) {
  524. return this.form.Macro.exec(this.json.sectionMergeReadDataScript.code, this);
  525. }else{
  526. return [];
  527. }
  528. default:
  529. var sortedData = this.getSortedSectionData();
  530. if( this.json.showSectionKey ){
  531. this.dataWithSectionKey = sortedData;
  532. }
  533. var data = [];
  534. //把区段值放在每行的数据里
  535. sortedData.each(function(d){
  536. ( d.data || [] ).each(function( obj ){
  537. if( o2.typeOf(obj) === "object" ){
  538. // obj.sectionKey = d.sectionKey;
  539. data.push( obj )
  540. }
  541. });
  542. // data = data.concat( d.data.data );
  543. });
  544. return data;
  545. }
  546. },
  547. getDefaultValue: function(){
  548. var value;
  549. if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
  550. if (value && !value.then) if (o2.typeOf(value)==="object") value = [value];
  551. if(!value){
  552. value = [];
  553. var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
  554. for( var i=0; i<count; i++ )value.push({})
  555. }
  556. return value;
  557. },
  558. _getValue: function(){
  559. if (this.moduleValueAG) return this.moduleValueAG;
  560. var value = this._getBusinessData();
  561. if( !value ){
  562. value = this.getDefaultValue();
  563. }
  564. // if (!value){
  565. // if (this.json.defaultData && this.json.defaultData.code) value = this.form.Macro.exec(this.json.defaultData.code, this);
  566. // if (value && !value.then) if (o2.typeOf(value)==="object") value = [value];
  567. // }
  568. // if(!value){
  569. // value = [];
  570. // var count = this.json.defaultCount ? this.json.defaultCount.toInt() : 0;
  571. // for( var i=0; i<count; i++ )value.push({})
  572. // }
  573. return value;
  574. },
  575. getValue: function(){
  576. return this._getValue();
  577. },
  578. _setValue: function(value){
  579. if (!!value && o2.typeOf(value.then)=="function"){
  580. var p = o2.promiseAll(value).then(function(v){
  581. this.__setValue(v);
  582. }.bind(this), function(){});
  583. this.moduleValueAG = p;
  584. p.then(function(){
  585. this.moduleValueAG = null;
  586. }.bind(this), function(){
  587. this.moduleValueAG = null;
  588. }.bind(this));
  589. }else{
  590. this.moduleValueAG = null;
  591. this.__setValue(value);
  592. }
  593. },
  594. __setValue: function(value){
  595. this._setBusinessData(value);
  596. this.moduleValueAG = null;
  597. return value;
  598. },
  599. _loadDataTemplate: function(callback, operation){
  600. var p = o2.promiseAll(this.data).then(function(v){
  601. this.data = v;
  602. // if (o2.typeOf(this.data)=="object") this.data = [this.data];
  603. if( this.isShowAllSection ){
  604. this._loadSectionLineList_EditSection(callback, operation)
  605. }else if( this.isShowSectionKey() ){
  606. this._loadSectionLineList(callback, operation)
  607. }else{
  608. this._loadLineList(callback, operation);
  609. }
  610. this.moduleValueAG = null;
  611. return v;
  612. }.bind(this), function(){
  613. this.moduleValueAG = null;
  614. }.bind(this));
  615. this.moduleValueAG = p;
  616. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  617. this.moduleValueAG = null;
  618. }.bind(this), function(){
  619. this.moduleValueAG = null;
  620. }.bind(this));
  621. },
  622. _loadSectionLineList_EditSection: function(callback, operation){
  623. var map = this.unchangedSectionLineMap || {};
  624. //this._getAllSectionData();
  625. this.dataWithSectionBy.each(function(data, idx){
  626. var isEdited = false;
  627. if( this.isSectionLineEditable( data ) && this.editable ){
  628. isEdited = true;
  629. }
  630. var isNew = false;
  631. var sectionLine;
  632. var beforeNode = idx > 0 ? this.sectionlineList[idx-1].node : null;
  633. if( map[data.sectionKey] ) {
  634. sectionLine = map[data.sectionKey];
  635. if( !operation || operation === "moveUpList"){
  636. this._injectLineNode( sectionLine.node, beforeNode );
  637. }
  638. sectionLine.setIndex( data, idx, isEdited, isNew, operation );
  639. //console.log("setIndex", sectionLine);
  640. }else{
  641. var div = this._injectLineNode( new Element("div"), beforeNode );
  642. sectionLine = this._loadSectionLine_EditSection(div, data, idx, isEdited, isNew );
  643. //console.log("_loadSectionLine_EditSection", sectionLine);
  644. }
  645. if( this.sectionBy && this.sectionBy === data.sectionKey ){
  646. this.sectionLineEdited = sectionLine;
  647. }
  648. this.sectionlineList.push(sectionLine);
  649. }.bind(this))
  650. if (callback) callback();
  651. },
  652. _loadSectionLine_EditSection: function(container, data, index, isEdited, isNew){
  653. var sectionLine = new MWF.xApplication.process.Xform.Datatemplate.SectionLine(container, this, data, {
  654. index : index,
  655. indexText : (index+1).toString(),
  656. isNew: isNew,
  657. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.editable,
  658. isDeleteable: this.editable && this.isSectionLineEditable(data),
  659. isAddable: this.editable && this.isSectionLineEditable(data)
  660. });
  661. // this.fireEvent("beforeLoadLine", [line]);
  662. sectionLine.load();
  663. // this.fireEvent("afterLoadLine", [line]);
  664. return sectionLine;
  665. },
  666. isSectionLineEditable: function(data){
  667. return this.isShowAllSection && this.sectionBy && this.sectionBy === data.sectionKey;
  668. },
  669. _loadSectionLineList: function(callback, operation){
  670. var map = this.unchangedSectionLineMap || {};
  671. this.dataWithSectionKey.each(function(data, idx){
  672. var sectionLine;
  673. var isEdited = false;
  674. var isNew = false;
  675. var beforeNode = idx > 0 ? this.sectionlineList[idx-1].node : null;
  676. if( map[data.sectionKey] ) {
  677. sectionLine = map[data.sectionKey];
  678. if( !operation || operation === "moveUpList"){
  679. this._injectLineNode( sectionLine.node, beforeNode );
  680. }
  681. //console.log("setIndex", sectionLine);
  682. sectionLine.setIndex( data, idx, isEdited, isNew, operation );
  683. }else {
  684. //var div = new Element("div").inject(this.node);
  685. var div = this._injectLineNode( new Element("div"), beforeNode );
  686. sectionLine = this._loadSectionLine(div, data, idx, isEdited, isNew );
  687. //console.log("_loadSectionLine", sectionLine);
  688. }
  689. this.sectionlineList.push(sectionLine);
  690. }.bind(this))
  691. if (callback) callback();
  692. },
  693. _loadSectionLine: function(container, data, index, isEdited, isNew){
  694. var sectionLine = new MWF.xApplication.process.Xform.Datatemplate.SectionLine(container, this, data, {
  695. index : index,
  696. indexText : (index+1).toString(),
  697. isNew: isNew,
  698. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.editable,
  699. isDeleteable: this.editable,
  700. isAddable: this.editable,
  701. isMergeRead: this.isMergeRead
  702. });
  703. // this.fireEvent("beforeLoadLine", [line]);
  704. sectionLine.load();
  705. // this.fireEvent("afterLoadLine", [line]);
  706. return sectionLine;
  707. },
  708. _loadLineList: function(callback, operation){
  709. var map = this.unchangedLineMap || {};
  710. Object.each(map, function (line, idx) {
  711. line.setIndex( idx.toInt() );
  712. });
  713. this.data.each(function(data, idx){
  714. var idxStr = idx.toString();
  715. var beforeNode = idx > 0 ? this.lineList[idx - 1].node : null;
  716. if( map[idxStr] ){
  717. if( !operation || operation === "moveUpList" ){
  718. this._injectLineNode( map[idxStr].node, beforeNode );
  719. }
  720. //console.log("inject", map[idxStr]);
  721. this.lineList.push( map[idxStr] );
  722. }else{
  723. var isNew = this.isNew || (o2.typeOf(this.newLineIndex) === "number" ? idx === this.newLineIndex : false);
  724. var div = this._injectLineNode( new Element("div"), beforeNode );
  725. var line = this._loadLine(div, data, idx, isNew);
  726. //console.log("_loadLine", line);
  727. this.lineList.push(line);
  728. }
  729. }.bind(this));
  730. this.newLineIndex = null;
  731. this.isNew = false;
  732. if (callback) callback();
  733. },
  734. _loadLine: function(container, data, index, isNew){
  735. var line = new MWF.xApplication.process.Xform.Datatemplate.Line(container, this, data, {
  736. index : index,
  737. indexText : (index+1).toString(),
  738. isEdited : this.editable,
  739. isNew : isNew,
  740. isMergeRead: this.isMergeRead
  741. });
  742. this.fireEvent("beforeLoadLine", [line]);
  743. line.load();
  744. this.fireEvent("afterLoadLine", [line]);
  745. return line;
  746. },
  747. _injectLineNode: function( lineNode, beforeNode ){
  748. if( beforeNode ){
  749. lineNode.inject( beforeNode, "after");
  750. }else{
  751. lineNode.inject( this.node );
  752. }
  753. return lineNode;
  754. },
  755. isMax : function(){
  756. var maxCount = this.json.maxCount ? this.json.maxCount.toInt() : 0;
  757. if( this.editable && maxCount > 0 ) {
  758. if( this.isShowAllSection ){
  759. if( this.sectionLineEdited && this.sectionLineEdited.lineList.length >= maxCount )return true;
  760. }else{
  761. if( this.lineList.length >= maxCount )return true;
  762. }
  763. }
  764. return false;
  765. },
  766. isMin : function(){
  767. var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
  768. if( this.editable && minCount > 0 ) {
  769. if( this.isShowAllSection ){
  770. if( this.sectionLineEdited && this.sectionLineEdited.lineList.length <= minCount )return true;
  771. }else {
  772. if( this.lineList.length <= minCount )return true;
  773. }
  774. }
  775. return false;
  776. },
  777. _setLineData: function(line, d){
  778. if( line.sectionLine ){
  779. var data = this.getBusinessDataById();
  780. var sdata = data[ line.sectionLine.sectionKey ];
  781. if( sdata ){
  782. sdata[line.options.indexInSectionLine] = d;
  783. this.setAllSectionData( data, false, "setLineData" );
  784. }
  785. }else{
  786. var index = line.options.index;
  787. var data = this.getInputData();
  788. data[index] = d;
  789. this.setData( data, false, "setLineData" );
  790. }
  791. },
  792. _addLine: function(ev, d){
  793. if( this.isMax() ){
  794. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  795. this.form.notice(text,"info");
  796. return false;
  797. }
  798. var data, index, newLine;
  799. if( this.isShowAllSection ){
  800. data = this.getBusinessDataById();
  801. var sdata = data[ this.sectionBy ];
  802. if( !sdata ){
  803. sdata = data[ this.sectionBy ] = [];
  804. }
  805. sdata.push(d||{});
  806. index = sdata.length - 1;
  807. this.newLineIndex = index;
  808. this.setAllSectionData( data , false, "addLine");
  809. newLine = this.sectionLineEdited.lineList[index];
  810. }else{
  811. data = this.getInputData();
  812. data.push(d || {});
  813. index = data.length-1;
  814. this.newLineIndex = index;
  815. this.setData( data, false, "addLine" );
  816. newLine = this.getLine(index);
  817. }
  818. this.validationMode();
  819. this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
  820. this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
  821. return newLine;
  822. },
  823. _insertLine: function(ev, beforeLine){
  824. if( this.isMax() ){
  825. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  826. this.form.notice(text,"info");
  827. return false;
  828. }
  829. //使用数据驱动
  830. var data, index, newLine;
  831. if( this.isShowAllSection ){
  832. index = beforeLine.options.indexInSectionLine + 1;
  833. data = this.getBusinessDataById();
  834. var sdata = data[ this.sectionBy ];
  835. if( !sdata ){
  836. sdata = data[ this.sectionBy ] = [];
  837. }
  838. sdata.splice(index, 0, {});
  839. this.newLineIndex = index;
  840. this.setAllSectionData( data , false, "insertLine");
  841. newLine = this.sectionLineEdited.lineList[index];
  842. }else {
  843. index = beforeLine.options.index+1;
  844. data = this.getInputData();
  845. data.splice(index, 0, {});
  846. this.newLineIndex = index;
  847. this.setData( data, false, "insertLine" );
  848. newLine = this.getLine( index );
  849. }
  850. this.validationMode();
  851. this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
  852. this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
  853. return newLine;
  854. },
  855. _insertLineByIndex: function(ev, index, d){
  856. if( this.isMax() ){
  857. var text = MWF.xApplication.process.Xform.LP.maxItemCountNotice.replace("{n}",this.json.maxCount);
  858. this.form.notice(text,"info");
  859. return false;
  860. }
  861. //使用数据驱动
  862. var data, newLine;
  863. if( this.isShowAllSection ){
  864. data = this.getBusinessDataById();
  865. var sdata = data[ this.sectionBy ];
  866. if( !sdata ){
  867. sdata = data[ this.sectionBy ] = [];
  868. }
  869. if (sdata.length < index) return null;
  870. sdata.splice(index, 0, d || {});
  871. this.newLineIndex = index;
  872. this.setAllSectionData( data , false, "insertLine");
  873. line = this.sectionLineEdited.lineList[index];
  874. }else {
  875. data = this.getInputData();
  876. if(data.length < index )return null;
  877. data.splice(index, 0, d||{});
  878. this.newLineIndex = index;
  879. this.setData( data , false, "insertLine");
  880. newLine = this.getLine( index );
  881. }
  882. this.validationMode();
  883. this.fireEvent("addLine",[{"line":newLine, "ev":ev}]);
  884. this.fireEvent("change", [{"lines":[newLine], "type":"addline"}]);
  885. return newLine;
  886. },
  887. _deleteSelectedLine: function(ev){
  888. var selectedLine = this.lineList.filter(function (line) { return line.selected; });
  889. if( selectedLine.length === 0 ){
  890. this.form.notice( MWF.xApplication.process.Xform.LP.selectItemNotice,"info");
  891. return false;
  892. }
  893. var minCount = this.json.minCount ? this.json.minCount.toInt() : 0;
  894. if( minCount > 0 ){
  895. var length;
  896. if( this.isShowAllSection && this.sectionLineEdited ){
  897. length = this.sectionLineEdited.lineList.length;
  898. }else{
  899. length = this.lineList.length;
  900. }
  901. if( length - selectedLine.length < minCount ){
  902. var text = MWF.xApplication.process.Xform.LP.minItemNotice.replace("{n}", minCount );
  903. this.form.notice(text,"info");
  904. return false;
  905. }
  906. }
  907. var _self = this;
  908. this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteSelectedItemNotice, 300, 120, function(){
  909. _self._delLines( selectedLine );
  910. this.close();
  911. }, function(){
  912. this.close();
  913. }, null, null, this.form.json.confirmStyle);
  914. },
  915. _delLines: function(lines){
  916. var _self = this;
  917. var saveFlag = false;
  918. var data;
  919. if( this.isShowAllSection ){
  920. data = this.getBusinessDataById();
  921. }else{
  922. data = _self.getInputData();
  923. }
  924. lines.reverse().each(function(line){
  925. _self.fireEvent("deleteLine", [line]);
  926. if(line.deleteAttachment())saveFlag = true;
  927. if( line.sectionLine ){
  928. var d = data[ line.sectionLine.sectionKey ];
  929. if( d ){
  930. d.splice(line.options.indexInSectionLine, 1);
  931. }
  932. }else {
  933. data.splice(line.options.index, 1);
  934. }
  935. _self.fireEvent("afterDeleteLine");
  936. });
  937. if( this.isShowAllSection ){
  938. _self.setAllSectionData(data, false, "deleteLines");
  939. }else{
  940. _self.setData( data , false, "deleteLines");
  941. }
  942. this.validationMode();
  943. _self.fireEvent("change", [{"lines":lines, "type":"deletelines"}]);
  944. if(saveFlag)this.form.saveFormData();
  945. },
  946. _deleteLine: function(ev, line){
  947. if( this.isMin() ){
  948. var text = MWF.xApplication.process.Xform.LP.minItemCountNotice.replace("{n}", this.json.minCount );
  949. this.form.notice(text,"info");
  950. return false;
  951. }
  952. var _self = this;
  953. this.form.confirm("warn", ev, MWF.xApplication.process.Xform.LP.deleteDatagridLineTitle, MWF.xApplication.process.Xform.LP.deleteDatagridLine, 300, 120, function(){
  954. _self._delLine(line);
  955. this.close();
  956. }, function(){
  957. this.close();
  958. }, null, null, this.form.json.confirmStyle);
  959. },
  960. _delLine: function(line){
  961. this.fireEvent("deleteLine", [line]);
  962. var saveFlag = line.deleteAttachment();
  963. //使用数据驱动
  964. var data;
  965. if( line.sectionLine ){
  966. var data = this.getBusinessDataById();
  967. var d = data[ line.sectionLine.sectionKey ];
  968. if( d ){
  969. d.splice(line.options.indexInSectionLine, 1);
  970. }
  971. this.setAllSectionData( data, false, "deleteLine" );
  972. }else{
  973. data = this.getInputData();
  974. data.splice(line.options.index, 1);
  975. this.setData( data , false, "deleteLine");
  976. }
  977. this.validationMode();
  978. this.fireEvent("afterDeleteLine");
  979. this.fireEvent("change", [{"lines":[line], "type":"deleteline"}]);
  980. if(saveFlag)this.form.saveFormData();
  981. },
  982. _checkSelectAll: function () {
  983. var selectData = this.selectAllSelector.getData();
  984. var selected;
  985. if(o2.typeOf(selectData)==="array"){
  986. selected = selectData.contains(this.json.outerSelectAllSelectedValue);
  987. }else{
  988. selected = selectData === this.json.outerSelectAllSelectedValue;
  989. }
  990. this.selected = selected;
  991. if( this.isShowAllSection && this.sectionLineEdited){
  992. this.sectionLineEdited.lineList.each(function (line) {
  993. this.selected ? line.select() : line.unselect();
  994. }.bind(this))
  995. }else{
  996. this.lineList.each(function (line) {
  997. this.selected ? line.select() : line.unselect();
  998. }.bind(this))
  999. }
  1000. },
  1001. selectAll: function(){
  1002. this.selected = true;
  1003. if(this.selectAllSelector)this.selectAllSelector.setData(this.json.outerSelectAllSelectedValue);
  1004. },
  1005. unselectAll: function(){
  1006. this.selected = false;
  1007. if( this.selectAllSelector.getOptionsObj ){
  1008. var options = this.selectAllSelector.getOptionsObj();
  1009. var value = "";
  1010. var arr = options.valueList || [];
  1011. for( var i=0; i<arr.length; i++ ){
  1012. var v = arr[i];
  1013. if( v !== this.json.outerSelectAllSelectedValue ){
  1014. value = v;
  1015. break;
  1016. }
  1017. }
  1018. this.selectAllSelector.setData(value);
  1019. }else{
  1020. this.selectAllSelector.setData("")
  1021. }
  1022. },
  1023. // editValidation: function(){
  1024. // var flag = true;
  1025. // this.editModules.each(function(field, key){
  1026. // if (field.json.type!=="sequence" && field.validationMode ){
  1027. // field.validationMode();
  1028. // if (!field.validation()) flag = false;
  1029. // }
  1030. // }.bind(this));
  1031. // return flag;
  1032. // },
  1033. exportToExcel: function(){
  1034. this.exporter = new MWF.xApplication.process.Xform.Datatemplate.Exporter(this);
  1035. this.exporter.exportToExcel();
  1036. },
  1037. importFromExcel: function(){
  1038. this.importer = new MWF.xApplication.process.Xform.Datatemplate.Importer(this);
  1039. this.importer.importFromExcel();
  1040. },
  1041. _afterLoaded: function(){
  1042. },
  1043. // /**
  1044. // * @summary 重置数据模板的值为默认值或置空。
  1045. // * @example
  1046. // * this.form.get('fieldId').resetData();
  1047. // */
  1048. resetData: function(){
  1049. // var value = this.getDefaultValue() || [];
  1050. var value = this.getValue() || [];
  1051. this.setData(value);
  1052. },
  1053. /**当参数为Promise的时候,请查看文档: {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0|使用Promise处理表单异步}<br/>
  1054. * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
  1055. * @summary 为数据模板赋值。
  1056. * @param data{DatatemplateData|Promise|Array} 必选,数组或Promise.
  1057. * @example
  1058. * this.form.get("fieldId").setData([]); //赋空值
  1059. * @example
  1060. * //如果无法确定表单上是否有组件,需要判断
  1061. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  1062. * this.form.get('fieldId').setData( data );
  1063. * }else{
  1064. * this.data['fieldId'] = data;
  1065. * }
  1066. *@example
  1067. * //使用Promise
  1068. * var field = this.form.get("fieldId");
  1069. * var promise = new Promise(function(resolve, reject){ //发起异步请求
  1070. * var oReq = new XMLHttpRequest();
  1071. * oReq.addEventListener("load", function(){ //绑定load事件
  1072. * resolve(oReq.responseText);
  1073. * });
  1074. * oReq.open("GET", "/data.json"); //假设数据存放在data.json
  1075. * oReq.send();
  1076. * });
  1077. * promise.then( function(){
  1078. * var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获得data.json的值
  1079. * })
  1080. * field.setData( promise );
  1081. */
  1082. setData: function(data, fireChange, operation){
  1083. if (!data){
  1084. data = this._getValue();
  1085. }
  1086. this._setData(data, fireChange, operation);
  1087. },
  1088. _setData: function(data, fireChange, operation){
  1089. var p = o2.promiseAll(this.data).then(function(v){
  1090. this.data = v;
  1091. // if (o2.typeOf(data)==="object") data = [data];
  1092. this.__setData(data, fireChange, operation);
  1093. this.moduleValueAG = null;
  1094. return v;
  1095. }.bind(this), function(){
  1096. this.moduleValueAG = null;
  1097. }.bind(this));
  1098. this.moduleValueAG = p;
  1099. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  1100. this.moduleValueAG = null;
  1101. }.bind(this), function(){
  1102. this.moduleValueAG = null;
  1103. }.bind(this));
  1104. },
  1105. __setData: function(data, fireChange, operation){
  1106. if( this.isShowAllSection ){
  1107. //兼容外部对编辑当前区段的setData,内部的setData不走这里,直接走setAllSectionData
  1108. this._setEditedSectionData(data, fireChange, operation);
  1109. return;
  1110. }else if( this.isMergeRead ){
  1111. //合并且只读,不允许setData
  1112. throw new Error("The data template is merged and read-only, you can use the 'setAllSectionData' method to set all section data");
  1113. }
  1114. var old;
  1115. if(fireChange)old = Object.clone(this._getBusinessData() || {});
  1116. this._setUnchangedLineMap(data, operation);
  1117. this._setBusinessData(data);
  1118. this.data = data;
  1119. if (this.data){
  1120. this.clearSubModules();
  1121. }
  1122. this.lineList = [];
  1123. this.sectionlineList = [];
  1124. this._loadDataTemplate(function(){
  1125. this._setSubDatatemplateOuterEvents();
  1126. if (fireChange && JSON.stringify(old) !== JSON.stringify(data)) this.fireEvent("change");
  1127. this.unchangedLineMap = null;
  1128. }.bind(this), operation);
  1129. },
  1130. _setSubDatatemplateOuterEvents: function(){
  1131. //告诉下层的数据模板绑定外部事件
  1132. for (var i=0; i<this.lineList.length; i++){
  1133. this.lineList[i].setSubDatatemplateOuterActionEvents();
  1134. }
  1135. },
  1136. _setEditedSectionData: function(data, fireChange, operation){
  1137. if( this.isShowAllSection ){
  1138. var d = this.getBusinessDataById();
  1139. d[ this.sectionBy ] = data || { data: [] };
  1140. this.setAllSectionData( d, fireChange , operation);
  1141. }
  1142. },
  1143. /**
  1144. * @summary 当数据模板设置为区段合并展现、区段合并编辑时,可以使用本方法设置所有区段数据。
  1145. * @param data{Object} 必选,对象.
  1146. * @param fireChange{boolean} 可选,是否触发change事件,默认false.
  1147. * @example
  1148. * this.form.get("fieldId").setAllSectionData({}); //赋空值
  1149. * @example
  1150. * this.data['fieldId'].setAllSectionData({
  1151. * "3455b82a-399c-4ee4-b9b9-e70ae40fbaf1": [ //区段1的key和data
  1152. * {
  1153. * "good": "yf",
  1154. * "number_2": 11,
  1155. * "prize": 1
  1156. * }
  1157. * ],
  1158. * "83de86fc-60bc-4b4c-955c-1085915865a4": [ //区段2的key和data
  1159. * {
  1160. * "good": "yf",
  1161. * "number_2": 11,
  1162. * "prize": 10
  1163. * }
  1164. * ]
  1165. * });
  1166. */
  1167. setAllSectionData: function(data, fireChange, operation){
  1168. var old;
  1169. if(fireChange)old = Object.clone(this.getBusinessDataById() || {});
  1170. this._setUnchangedSectionLineMap(data);
  1171. this.setBusinessDataById(data);
  1172. if( operation ){
  1173. this.data = this.isShowAllSection ? this._getAllSectionData() : data;
  1174. }else{
  1175. this.checkMerge(data);
  1176. }
  1177. if (this.data){
  1178. this.clearSubModules();
  1179. }
  1180. if (fireChange && JSON.stringify(old) !== JSON.stringify(data)) this.fireEvent("change");
  1181. this.lineList = [];
  1182. this.sectionlineList = [];
  1183. this._loadDataTemplate(function () {
  1184. Object.each(this.unchangedSectionLineMap, function(sline, key){
  1185. sline.isUnchangedAll = null;
  1186. sline.unchangedLineMap = null;
  1187. });
  1188. this.unchangedSectionLineMap = null;
  1189. }.bind(this), operation);
  1190. },
  1191. checkMerge: function(data){
  1192. //区段合并后编辑
  1193. var isMergeEidt = this.isSectionMergeEdit();
  1194. if( isMergeEidt ){ //区段合并,删除区段值合并数据后编辑
  1195. if( this.json.mergeTypeEdit === "script" ){
  1196. this._loadMergeEditNodeByScript();
  1197. }else{
  1198. this._loadMergeEditNodeByDefault();
  1199. }
  1200. }
  1201. //区段合并展现
  1202. this.isMergeRead = this.isSectionMergeRead();
  1203. //启用区段且显示所有区段
  1204. this.sectionBy = this._getSectionBy();
  1205. this.isShowAllSection = this.isAllSectionShow();
  1206. if( this.isShowAllSection ){
  1207. this.data = this._getAllSectionData();
  1208. }else if( this.isMergeRead ) {
  1209. this.data = this.getSectionMergeReadData();
  1210. }else if( isMergeEidt ){
  1211. this.data = this._getBusinessData()
  1212. }else{
  1213. this.data = data;
  1214. }
  1215. },
  1216. _setUnchangedSectionLineMap: function( data, operation ){
  1217. if( !data ){
  1218. return;
  1219. }
  1220. var map = {};
  1221. if( this.sectionlineList && this.sectionlineList.length ){
  1222. this.sectionlineList.each(function (sline, i) {
  1223. for( var key in data ){
  1224. if( key === sline.sectionKey ){
  1225. var m = sline._setUnchangedLineMap(data[key], operation);
  1226. if( m )map[key] = m;
  1227. break;
  1228. }
  1229. }
  1230. }.bind(this));
  1231. }
  1232. this.unchangedSectionLineMap = map;
  1233. },
  1234. _setUnchangedLineMap: function(data, operation){
  1235. if( !data ){
  1236. this.unchangedLineMap = {};
  1237. return;
  1238. }
  1239. var fromOutside = !operation;
  1240. var lineDataList = this.lineList.map(function (line) {
  1241. if( line.options.isEdited !== this.editable )return "";
  1242. if( line.options.isDeleteable !== this.editable )return "";
  1243. if( line.options.isAddable !== this.editable )return "";
  1244. return fromOutside ? JSON.stringify(line.data) : line.data;
  1245. }.bind(this));
  1246. var dStr, map = {};
  1247. data.each(function (d, idx) {
  1248. if(fromOutside)dStr = JSON.stringify(d);
  1249. for (var i = 0; i < this.lineList.length; i++) {
  1250. var isEqual= fromOutside ? (dStr === lineDataList[i]) : (d === lineDataList[i] );
  1251. if ( isEqual ) {
  1252. map[idx] = this.lineList[i];
  1253. lineDataList[i] = "";
  1254. break;
  1255. }
  1256. }
  1257. }.bind(this));
  1258. this.unchangedLineMap = map;
  1259. },
  1260. clearSubModules: function(){
  1261. if( this.sectionlineList && this.sectionlineList.length ){
  1262. for( var i=0; i<this.sectionlineList.length; i++ ){
  1263. this.sectionlineList[i].clearSubModules();
  1264. }
  1265. }else{
  1266. var map = this.unchangedLineMap || {};
  1267. var lines = [];
  1268. Object.values(map).each(function (d) {
  1269. lines = lines.concat(d);
  1270. });
  1271. for (var i=0; i<this.lineList.length; i++){
  1272. //this.lineList[i].clearSubModules();
  1273. var l = this.lineList[i];
  1274. if(!lines.contains(l))l.clearSubModules();
  1275. }
  1276. }
  1277. },
  1278. resetId: function(){
  1279. if( this.sectionlineList && this.sectionlineList.length ){
  1280. this.sectionlineList.each(function (sline, i) {
  1281. sline.resetId();
  1282. }.bind(this));
  1283. }else{
  1284. for (var i=0; i<this.lineList.length; i++){
  1285. this.lineList[i].resetId();
  1286. }
  1287. }
  1288. },
  1289. /**
  1290. * @summary 判断数据模板是否为空.
  1291. * @example
  1292. * if( this.form.get('fieldId').isEmpty() ){
  1293. * this.form.notice('至少需要添加一条数据', 'warn');
  1294. * }
  1295. * @return {Boolean} 是否为空
  1296. */
  1297. isEmpty: function(){
  1298. var data = this.getInputData();
  1299. if( !data )return true;
  1300. if( o2.typeOf( data ) === "array" ){
  1301. return data.length === 0;
  1302. }
  1303. if( o2.typeOf( data ) === "object" ){
  1304. return Object.keys(data).length === 0;
  1305. }
  1306. return false;
  1307. },
  1308. //api 相关开始
  1309. /**
  1310. * 获取对应的条目。
  1311. * @param {Number} index 条目序号,从零开始
  1312. * @return {MWF.xApplication.process.Xform.Datatemplate.Line | Null} 对应的数据模板条目
  1313. * @example
  1314. * //获取数据模板“dt1”的第一个条目。
  1315. * var line = this.form.get("dt1").getLine(0);
  1316. * //获取第一个条目subject字段的值
  1317. * var data = line.get("subject").getData();
  1318. * //设置subject字段的值
  1319. * line.get("subject").setData("test1");
  1320. */
  1321. getLine: function(index){
  1322. var line = this.lineList[index];
  1323. return line || null;
  1324. },
  1325. /**
  1326. * 在数据模板末尾添加条目。
  1327. * @param {Object} [data] 添加条目的数据。
  1328. * @return {MWF.xApplication.process.Xform.Datatemplate.Line} 添加的数据模板条目
  1329. * @example
  1330. * var line = this.form.get("dt1").addLine();
  1331. */
  1332. addLine: function( data ){
  1333. return this._addLine( null, data );
  1334. },
  1335. /**
  1336. * 在数据模板指定位置添加条目。
  1337. * @param {Number} index 条目序号,从零开始,如果下标超过当前数据模板条目数,插入失败并返回null。
  1338. * @param {Object} [data] 添加条目的数据。
  1339. * @return {MWF.xApplication.process.Xform.Datatemplate.Line | Null} 插入的数据模板条目
  1340. * @example
  1341. * var line = this.form.get("dt1").insertLine(0);
  1342. */
  1343. insertLine: function(index, data){
  1344. return this._insertLineByIndex(null, index, data);
  1345. },
  1346. /**
  1347. * 删除指定位置的条目。
  1348. * @param {Number} index 条目序号,从零开始,如果下标超过当前数据模板条目数,删除失败。
  1349. * @example
  1350. * //直接删除第一个条目
  1351. * this.form.get("dt1").deleteLine(0);
  1352. */
  1353. deleteLine: function(index, ev){
  1354. var line = this.lineList[index];
  1355. if( !line )return null;
  1356. // if( ev ){
  1357. // this._deleteLine(ev, line);
  1358. // }else{
  1359. this._delLine(line);
  1360. // }
  1361. },
  1362. /**
  1363. * 获取对应表单组件,作用等同于get。
  1364. * @param {Number} index 条目序号,从零开始
  1365. * @param {String} id 组件标识
  1366. * @return {FormComponent} 对应表单组件
  1367. * @example
  1368. * //获取数据模板“dt1”的第一个条目的subject字段。
  1369. * var module = this.form.get("dt1").getModule(0, "subject");
  1370. * //获取subject字段的值
  1371. * var data = module.getData();
  1372. * //设置subject字段的值
  1373. * module.setData("test1");
  1374. */
  1375. getModule: function(index, id){
  1376. var line = this.lineList[index];
  1377. if( !line )return null;
  1378. return line.getModule(id);
  1379. },
  1380. /**
  1381. * 获取对应表单组件,作用等同于getModule。
  1382. * @param {Number} index 条目序号,从零开始
  1383. * @param {String} id 组件标识
  1384. * @return {FormComponent} 对应表单组件
  1385. * @example
  1386. * //获取数据模板“dt1”的第一个条目的subject字段。
  1387. * var module = this.form.get("dt1").get(0, "subject");
  1388. * //获取subject字段的值
  1389. * var data = module.getData();
  1390. * //设置subject字段的值
  1391. * module.setData("test1");
  1392. */
  1393. get: function(index, id){
  1394. return this.getModule(index, id);
  1395. },
  1396. //api 相关
  1397. /**
  1398. * 在脚本中使用 this.data[fieldId] 也可以获取组件值。
  1399. * 区别如下:<br/>
  1400. * 1、当使用Promise的时候<br/>
  1401. * 使用异步函数生成器(Promise)为组件赋值的时候,用getData方法立即获取数据,可能返回修改前的值,当Promise执行完成以后,会返回修改后的值。<br/>
  1402. * this.data[fieldId] 立即获取数据,可能获取到异步函数生成器,当Promise执行完成以后,会返回修改后的值。<br/>
  1403. * {@link https://www.yuque.com/o2oa/ixsnyt/ws07m0#EggIl|具体差异请查看链接}<br/>
  1404. * 2、当表单上没有对应组件的时候,可以使用this.data[fieldId]获取值,但是this.form.get('fieldId')无法获取到组件。
  1405. * @summary 获取数据模板数据.
  1406. * @example
  1407. * var data = this.form.get('fieldId').getData();
  1408. *@example
  1409. * //如果无法确定表单上是否有组件,需要判断
  1410. * var data;
  1411. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  1412. * data = this.form.get('fieldId').getData();
  1413. * }else{
  1414. * data = this.data['fieldId']; //直接从数据中获取字段值
  1415. * }
  1416. * @example
  1417. * //使用Promise
  1418. * var field = this.form.get("fieldId");
  1419. * var promise = new Promise(function(resolve, reject){ //发起异步请求
  1420. * var oReq = new XMLHttpRequest();
  1421. * oReq.addEventListener("load", function(){ //绑定load事件
  1422. * resolve(oReq.responseText);
  1423. * });
  1424. * oReq.open("GET", "/data.json"); //假设数据存放在data.json
  1425. * oReq.send();
  1426. * });
  1427. * promise.then( function(){
  1428. * var data = field.getData(); //此时由于异步请求已经执行完毕,getData方法获得data.json的值
  1429. * })
  1430. * field.setData( promise );
  1431. * @return {DatatemplateData}
  1432. */
  1433. getData: function(){
  1434. if( this.importer ){
  1435. this.importer.destroySimulateModule();
  1436. }
  1437. if (this.editable!==false){
  1438. // var data = [];
  1439. // this.lineList.each(function(line, index){
  1440. // data.push(line.getData())
  1441. // });
  1442. //
  1443. // this.data = data;
  1444. //
  1445. // this._setBusinessData(this.data);
  1446. //
  1447. // return (this.data.length) ? this.data : [];
  1448. this.lineList.each(function (line) {
  1449. line.computeModuleData("save");
  1450. })
  1451. return this._getBusinessData();
  1452. }else{
  1453. return this._getBusinessData();
  1454. }
  1455. },
  1456. getInputData: function(){
  1457. if( this.importer ){
  1458. this.importer.destroySimulateModule();
  1459. }
  1460. if (this.editable!==false){
  1461. return this._getBusinessData();
  1462. }else{
  1463. return this._getBusinessData();
  1464. }
  1465. },
  1466. _getSectionBy: function(){
  1467. if (this.json.section!=="yes"){
  1468. return "";
  1469. }else {
  1470. switch (this.json.sectionBy){
  1471. case "person":
  1472. return layout.desktop.session.user.id;
  1473. case "unit":
  1474. return (this.form.businessData.task) ? this.form.businessData.task.unit : "";
  1475. case "activity":
  1476. return (this.form.businessData.work) ? this.form.businessData.work.activity : "";
  1477. case "splitValue":
  1478. return (this.form.businessData.work) ? this.form.businessData.work.splitValue : "";
  1479. case "script":
  1480. if( this.json.sectionByScript && this.json.sectionByScript.code){
  1481. return this.form.Macro.exec(this.json.sectionByScript.code, this) || "";
  1482. }else{
  1483. return "";
  1484. }
  1485. default:
  1486. return "";
  1487. }
  1488. }
  1489. },
  1490. createErrorNode: function(text){
  1491. var node = new Element("div");
  1492. var iconNode = new Element("div", {
  1493. "styles": {
  1494. "width": "20px",
  1495. "height": "20px",
  1496. "float": "left",
  1497. "background": "url("+"../x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  1498. }
  1499. }).inject(node);
  1500. var textNode = new Element("div", {
  1501. "styles": {
  1502. "line-height": "20px",
  1503. "margin-left": "20px",
  1504. "color": "red",
  1505. "word-break": "keep-all"
  1506. },
  1507. "text": text
  1508. }).inject(node);
  1509. return node;
  1510. },
  1511. notValidationMode: function(text){
  1512. if (!this.isNotValidationMode){
  1513. this.isNotValidationMode = true;
  1514. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  1515. this.node.setStyle("border", "1px solid red");
  1516. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  1517. this.showNotValidationMode(this.node);
  1518. var parentNode = this.errNode;
  1519. while( parentNode && parentNode.offsetParent === null ){
  1520. parentNode = parentNode.getParent();
  1521. }
  1522. if (parentNode && !parentNode.isIntoView()) parentNode.scrollIntoView(false);
  1523. }
  1524. },
  1525. showNotValidationMode: function(node){
  1526. var p = node.getParent("div");
  1527. if (p){
  1528. if (p.get("MWFtype") == "tab$Content"){
  1529. if (p.getParent("div").getStyle("display")=="none"){
  1530. var contentAreaNode = p.getParent("div").getParent("div");
  1531. var tabAreaNode = contentAreaNode.getPrevious("div");
  1532. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  1533. var tabNode = tabAreaNode.getLast().getFirst().getChildren()[idx];
  1534. tabNode.click();
  1535. p = tabAreaNode.getParent("div");
  1536. }
  1537. }
  1538. this.showNotValidationMode(p);
  1539. }
  1540. },
  1541. validationMode: function(){
  1542. if (this.isNotValidationMode){
  1543. this.isNotValidationMode = false;
  1544. this.node.setStyles(this.node.retrieve("borderStyle"));
  1545. if (this.errNode){
  1546. this.errNode.destroy();
  1547. this.errNode = null;
  1548. }
  1549. }
  1550. },
  1551. validationConfigItem: function(routeName, data){
  1552. var flag = (data.status=="all") ? true: (routeName == data.decision);
  1553. if (flag){
  1554. var n = this.getData();
  1555. if( o2.typeOf(n)==="object"){
  1556. var arr = [];
  1557. Object.each( n, function (d, key) {
  1558. if(o2.typeOf(d) === "array")arr = arr.concat(d);
  1559. });
  1560. n = arr;
  1561. }
  1562. var v = (data.valueType=="value") ? n : n.length;
  1563. switch (data.operateor){
  1564. case "isnull":
  1565. if (!v){
  1566. this.notValidationMode(data.prompt);
  1567. return false;
  1568. }
  1569. break;
  1570. case "notnull":
  1571. if (v){
  1572. this.notValidationMode(data.prompt);
  1573. return false;
  1574. }
  1575. break;
  1576. case "gt":
  1577. if (v>data.value){
  1578. this.notValidationMode(data.prompt);
  1579. return false;
  1580. }
  1581. break;
  1582. case "lt":
  1583. if (v<data.value){
  1584. this.notValidationMode(data.prompt);
  1585. return false;
  1586. }
  1587. break;
  1588. case "equal":
  1589. if (v==data.value){
  1590. this.notValidationMode(data.prompt);
  1591. return false;
  1592. }
  1593. break;
  1594. case "neq":
  1595. if (v!=data.value){
  1596. this.notValidationMode(data.prompt);
  1597. return false;
  1598. }
  1599. break;
  1600. case "contain":
  1601. if (v.indexOf(data.value)!=-1){
  1602. this.notValidationMode(data.prompt);
  1603. return false;
  1604. }
  1605. break;
  1606. case "notcontain":
  1607. if (v.indexOf(data.value)==-1){
  1608. this.notValidationMode(data.prompt);
  1609. return false;
  1610. }
  1611. break;
  1612. }
  1613. }
  1614. return true;
  1615. },
  1616. validationConfig: function(routeName, opinion){
  1617. if (this.json.validationConfig){
  1618. if (this.json.validationConfig.length){
  1619. for (var i=0; i<this.json.validationConfig.length; i++) {
  1620. var data = this.json.validationConfig[i];
  1621. if (!this.validationConfigItem(routeName, data)) return false;
  1622. }
  1623. }
  1624. return true;
  1625. }
  1626. return true;
  1627. },
  1628. saveValidation: function () {
  1629. var flag = true;
  1630. for (var i=0; i<this.lineList.length; i++){
  1631. if( this.lineList[i] && !this.lineList[i].saveValidation())flag = false;
  1632. }
  1633. return flag;
  1634. },
  1635. validation: function(routeName, opinion){
  1636. // if (this.isEdit){
  1637. // if (!this.editValidation()){
  1638. // return false;
  1639. // }
  1640. // }
  1641. if (!this.validationConfig(routeName, opinion)) return false;
  1642. if (!this.json.validation) return true;
  1643. if (!this.json.validation.code) return true;
  1644. this.currentRouteName = routeName;
  1645. var flag = this.form.Macro.exec(this.json.validation.code, this);
  1646. this.currentRouteName = "";
  1647. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  1648. if (flag.toString()!=="true"){
  1649. this.notValidationMode(flag);
  1650. return false;
  1651. }
  1652. return true;
  1653. },
  1654. getAttachmentRandomSite: function(){
  1655. var i = (new Date()).getTime();
  1656. return this.json.id+i;
  1657. }
  1658. });
  1659. MWF.xApplication.process.Xform.Datatemplate.SectionLine = new Class({
  1660. Implements: [Options, Events],
  1661. options: {
  1662. isNew: false,
  1663. isEdited: true, //是否正在编辑
  1664. isDeleteable: true, //能否被删除
  1665. isAddable: true, //能否添加
  1666. isMergeRead: false, //合并阅读
  1667. index: 0,
  1668. indexText: "0"
  1669. },
  1670. initialize: function (node, template, data, options) {
  1671. this.setOptions(options);
  1672. this.node = node;
  1673. this.template = template;
  1674. this.data = data;
  1675. this.form = this.template.form;
  1676. this.lineList = [];
  1677. this.sectionKey = this.data.sectionKey;
  1678. },
  1679. load: function () {
  1680. if( this.template.isShowSectionKey() || this.template.isShowSectionBy() ){
  1681. this.loadSectionKeyNode();
  1682. }
  1683. if( this.data.data ){
  1684. ( this.data.data || [] ).each(function(d, idx){
  1685. if( !d )return;
  1686. var div = new Element("div").inject(this.node);
  1687. var isEdited = false, isNew = false;
  1688. if( this.options.isEdited ){
  1689. var dt = this.template;
  1690. isNew = dt.isNew || (o2.typeOf(dt.newLineIndex) === "number" ? idx === dt.newLineIndex : false);
  1691. isEdited = true;
  1692. dt.isNew = false;
  1693. dt.newLineIndex = null;
  1694. }
  1695. var line = this._loadLine( div, d, idx, isEdited, isNew );
  1696. this.lineList.push(line);
  1697. this.template.lineList.push(line);
  1698. }.bind(this));
  1699. }
  1700. },
  1701. resetId: function(){
  1702. this.lineList.each(function (line) {
  1703. line.resetId();
  1704. }.bind(this))
  1705. },
  1706. setIndex: function( data, index, isEdited, isNew, operation ){
  1707. if( this.isUnchangedAll && index === this.options.index )return;
  1708. this.data = data;
  1709. this.options.index = index;
  1710. this.options.indexText = (index+1).toString();
  1711. //this.node.inject(preNode, "after");
  1712. this.lineList = [];
  1713. var map = this.unchangedLineMap || {};
  1714. Object.each(map, function (line, idx) {
  1715. line.setIndex( this.template.lineList.length + idx.toInt(), idx.toInt() );
  1716. }.bind(this));
  1717. if( this.data && this.data.data ){
  1718. this.data.data.each(function (d, idx) {
  1719. if( !d )return;
  1720. var idxStr = idx.toString();
  1721. var beforeNode = idx > 0 ? this.lineList[idx - 1].node : null;
  1722. if( map[idxStr] ){
  1723. if( !operation || operation === "moveUpList" ){
  1724. this._injectLineNode( map[idxStr].node, beforeNode )
  1725. }
  1726. //console.log("inject", map[idxStr]);
  1727. this.lineList.push( map[idxStr] );
  1728. this.template.lineList.push(map[idxStr]);
  1729. }else{
  1730. var node = this._injectLineNode( new Element("div"), beforeNode );
  1731. var isEdited = false, isNew = false;
  1732. if( this.options.isEdited ){
  1733. var dt = this.template;
  1734. isNew = dt.isNew || (o2.typeOf(dt.newLineIndex) === "number" ? idx === dt.newLineIndex : false);
  1735. isEdited = true;
  1736. dt.isNew = false;
  1737. dt.newLineIndex = null;
  1738. }
  1739. var line = this._loadLine( node, d, idx, isEdited, isNew );
  1740. //console.log("_loadLine", line);
  1741. this.lineList.push(line);
  1742. this.template.lineList.push(line);
  1743. }
  1744. }.bind(this))
  1745. }
  1746. },
  1747. _loadLine: function(container, data, index, isEdited, isNew){
  1748. var line = new MWF.xApplication.process.Xform.Datatemplate.Line(container, this.template, data, {
  1749. indexInSectionLine : index,
  1750. indexInSectionLineText : (index+1).toString(),
  1751. index: this.template.lineList.length,
  1752. indexText : (this.template.lineList.length + 1).toString(),
  1753. isNew: isNew,
  1754. isEdited: typeOf(isEdited) === "boolean" ? isEdited : this.options.isEdited,
  1755. isDeleteable: this.options.isDeleteable,
  1756. isAddable: this.options.isAddable,
  1757. isMergeRead: this.options.isMergeRead,
  1758. sectionKey: this.sectionKey
  1759. }, this);
  1760. this.template.fireEvent("beforeLoadLine", [line]);
  1761. line.load();
  1762. this.template.fireEvent("afterLoadLine", [line]);
  1763. return line;
  1764. },
  1765. _injectLineNode: function( lineNode, beforeNode ){
  1766. if( beforeNode ){
  1767. lineNode.inject( beforeNode, "after");
  1768. }else{
  1769. lineNode.inject( this.node );
  1770. }
  1771. return lineNode;
  1772. },
  1773. loadSectionKeyNode: function () {
  1774. var sectionKeyStyles = this.template._parseStyles(this.template.json.sectionKeyStyles);
  1775. var keyNode = new Element("div.mwf_sectionkey", {
  1776. styles : sectionKeyStyles
  1777. }).inject( this.node );
  1778. this.keyNode = keyNode;
  1779. var separator;
  1780. if( this.template.isShowSectionKey() ){
  1781. separator = this.template.json.keyContentSeparator;
  1782. }else{
  1783. separator = this.template.json.keyContentSeparatorSectionBy;
  1784. }
  1785. this.template.getSectionKeyWithMerge( this.data, function (key) {
  1786. if( o2.typeOf(key) === "string" ){
  1787. keyNode.set("text", key + (separator || ""));
  1788. }else{
  1789. Promise.resolve(key).then(function (k) {
  1790. keyNode.set("text", k + (separator || ""));
  1791. }.bind(this))
  1792. }
  1793. }.bind(this));
  1794. },
  1795. _setUnchangedLineMap: function(data, operation){
  1796. var fromOutside = !operation;
  1797. var dt = this.template;
  1798. var editalbe;
  1799. if( dt.isShowAllSection ){
  1800. editalbe = dt.isShowAllSection && dt.sectionBy && dt.sectionBy === this.sectionKey;
  1801. }
  1802. var lineDataList = this.lineList.map(function (line) {
  1803. if( dt.isShowAllSection ){
  1804. if( line.options.isEdited !== (dt.editable && editalbe) )return "";
  1805. if( line.options.isDeleteable !== (dt.editable && editalbe) )return "";
  1806. if( line.options.isAddable !== (dt.editable && editalbe) )return "";
  1807. }else{
  1808. if( line.options.isEdited !== dt.editable )return "";
  1809. if( line.options.isDeleteable !== dt.editable )return "";
  1810. if( line.options.isAddable !== dt.editable )return "";
  1811. }
  1812. return fromOutside ? JSON.stringify(line.data) : line.data;
  1813. }.bind(this));
  1814. var dStr, map = {};
  1815. data.each(function (d, idx) {
  1816. if(fromOutside)dStr = JSON.stringify(d);
  1817. for (var i = 0; i < this.lineList.length; i++) {
  1818. var isEqual = fromOutside ? (dStr === lineDataList[i]) : (d === lineDataList[i] );
  1819. if ( isEqual ) {
  1820. map[idx] = this.lineList[i];
  1821. lineDataList[i] = "";
  1822. break;
  1823. }
  1824. }
  1825. }.bind(this));
  1826. this.isUnchangedAll = data.length === this.lineList.length;
  1827. if( this.isUnchangedAll ){
  1828. for( var i=0; i<data.length; i++ ){
  1829. var line = map[i.toString()];
  1830. if( !line || line.options.indexInSectionLine !== i ){
  1831. this.isUnchangedAll = false;
  1832. break;
  1833. }
  1834. }
  1835. }
  1836. this.unchangedLineMap = map;
  1837. return Object.keys(map).length ? this : null;
  1838. },
  1839. clearSubModules: function(){
  1840. if( this.isUnchangedAll )return;
  1841. var map = this.unchangedLineMap || {};
  1842. var hasUnchangedLine = Object.keys(map).length > 0;
  1843. if( !hasUnchangedLine ){
  1844. if( this.keyNode ){
  1845. this.keyNode.destroy();
  1846. this.keyNode = null;
  1847. }
  1848. }
  1849. var lines = [];
  1850. Object.values(map).each(function (d) {
  1851. lines = lines.concat(d);
  1852. });
  1853. for (var i=0; i<this.lineList.length; i++){
  1854. var l = this.lineList[i];
  1855. if(!lines.contains(l)){
  1856. l.clearSubModules();
  1857. }
  1858. }
  1859. }
  1860. });
  1861. MWF.xApplication.process.Xform.Datatemplate.Line = new Class({
  1862. Implements: [Options, Events],
  1863. options: {
  1864. isNew: false,
  1865. isEdited : true,
  1866. isAddable: true,
  1867. isDeleteable: true,
  1868. index : 0,
  1869. indexText : "0",
  1870. indexInSectionLine: 0,
  1871. indexInSectionLineText : "0",
  1872. sectionKey: ""
  1873. },
  1874. initialize: function (node, template, data, options, sectionLine) {
  1875. this.setOptions(options);
  1876. this.node = node;
  1877. this.template = template;
  1878. this.data = data;
  1879. this.form = this.template.form;
  1880. this.sectionLine = sectionLine;
  1881. this.modules = [];
  1882. this.all = {};
  1883. this.all_templateId = {};
  1884. this.fields = [];
  1885. this.allField = {};
  1886. this.allField_templateId = {};
  1887. this.addActionList = [];
  1888. this.deleteActionList = [];
  1889. this.sequenceNodeList = [];
  1890. this.selector = null;
  1891. this.importActionList = [];
  1892. this.exportActionList = [];
  1893. this.subDatatemplateModuleList = [];
  1894. },
  1895. resetId: function(){
  1896. this.setIndex();
  1897. },
  1898. setIndex: function(index, indexInSectionLine){
  1899. var hasIndexArg = typeOf(index) !== "null";
  1900. var hasIndexInSectionLineArg = typeOf(indexInSectionLine) !== "null";
  1901. if( hasIndexArg && hasIndexInSectionLineArg){
  1902. if( this.options.index === index && this.options.indexInSectionLine === indexInSectionLine )return;
  1903. }else if( hasIndexArg && !hasIndexInSectionLineArg){
  1904. if( this.options.index === index )return;
  1905. }else if(!hasIndexArg && hasIndexInSectionLineArg){
  1906. if( this.options.indexInSectionLine === indexInSectionLine )return;
  1907. }
  1908. if( hasIndexArg ){
  1909. this.options.index = index;
  1910. this.options.indexText = (index.toInt()+1).toString();
  1911. }
  1912. if( hasIndexInSectionLineArg ){
  1913. this.options.indexInSectionLine = indexInSectionLine;
  1914. this.options.indexInSectionLineText = (indexInSectionLine.toInt()+1).toString();
  1915. }
  1916. //合并状态或拆分状态
  1917. var sectionKey = this.options.sectionKey || this.template.sectionBy;
  1918. this.modules.each(function (module) {
  1919. var json = module.json;
  1920. var id, oldId = json.id, templateJsonId = json.originialId;
  1921. if( this.template.isShowAllSection ){
  1922. id = this.template.json.id + ".." + sectionKey + ".." + this.options.indexInSectionLine + ".." + json.originialId;
  1923. }else if( sectionKey ){
  1924. id = this.template.json.id + ".." + sectionKey + ".." + this.options.index + ".." + json.originialId;
  1925. }else{
  1926. id = this.template.json.id + ".." + this.options.index + ".." + json.originialId;
  1927. }
  1928. json.id = id;
  1929. switch (json.type) {
  1930. case "Select":
  1931. (module.areaNode || module.node).set("id", id);
  1932. break;
  1933. case "Datatable":
  1934. case "Datatemplate":
  1935. module.resetId();
  1936. break;
  1937. default:
  1938. module.node.set("id", id);
  1939. break;
  1940. }
  1941. if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  1942. json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
  1943. }
  1944. delete this.all[oldId];
  1945. this.all[id] = module;
  1946. delete this.allField[oldId];
  1947. this.allField[id] = module;
  1948. if(this.form.all[oldId]){
  1949. delete this.form.all[oldId];
  1950. this.form.all[id] = module;
  1951. }
  1952. if(this.form.forms[oldId]){
  1953. delete this.form.forms[oldId];
  1954. this.form.forms[id] = module;
  1955. }
  1956. this.checkSequence(module, templateJsonId);
  1957. }.bind(this));
  1958. },
  1959. load: function(){
  1960. this.node.set("html", this.template.templateHtml);
  1961. var moduleNodes = this.form._getModuleNodes(this.node);
  1962. //拆分状态
  1963. var sectionKey = this.options.sectionKey || this.template.sectionBy;
  1964. moduleNodes.each(function (node) {
  1965. if (node.get("MWFtype") !== "form") {
  1966. var _self = this;
  1967. var tJson = this.form._getDomjson(node);
  1968. if( tJson ){
  1969. var json = Object.clone(tJson);
  1970. if( !this.options.isEdited )json.isReadonly = true;
  1971. var templateJsonId = json.id;
  1972. var index = this.options.index;
  1973. var id;
  1974. if( this.template.isShowAllSection ){
  1975. id = this.template.json.id + ".." + sectionKey + ".." + this.options.indexInSectionLine + ".." + json.id;
  1976. }else if( sectionKey ){
  1977. id = this.template.json.id + ".." + sectionKey + ".."+ index + ".." + json.id;
  1978. }else{
  1979. id = this.template.json.id + ".." + index + ".." + json.id;
  1980. }
  1981. json.originialId = templateJsonId;
  1982. json.id = id;
  1983. node.set("id", id);
  1984. if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  1985. json.type = "AttachmentDg";
  1986. json.ignoreSite = true;
  1987. json.site = this.getAttachmentSite(json, templateJsonId, sectionKey);
  1988. }
  1989. if (this.form.all[id]) this.form.all[id] = null;
  1990. if (this.form.forms[id])this.form.forms[id] = null;
  1991. var module = this.form._loadModule(json, node, function () {
  1992. if( _self.options.isMergeRead ){
  1993. this.field = false; //不希望保存数据
  1994. this._getBusinessData = function(){
  1995. return _self.data[templateJsonId];
  1996. };
  1997. this._setBusinessData = function () {};
  1998. }
  1999. if( _self.widget )this.widget = _self.widget;
  2000. this.parentLine = _self;
  2001. this.parentDatatemplate = _self.template;
  2002. //只读方法值在页面加载的时候或者new的时候计算一下
  2003. if( this.json.compute === "show" ){
  2004. var needComputeShow = false;
  2005. if( _self.template.loading ) {
  2006. needComputeShow = true;
  2007. }else if( _self.options.isNew && !_self.reloading ){
  2008. needComputeShow = true;
  2009. }
  2010. if( !needComputeShow ){
  2011. this.json.compute = "create"; //
  2012. if( this.options.moduleEvents && this.options.moduleEvents.length ){ //恢复compute
  2013. var eventName = ( this.options.moduleEvents || [] ).contains("afterLoad") ? "afterLoad" : "load";
  2014. var resetCompute = function () {
  2015. this.json.compute = "show";
  2016. this.removeEvent( eventName, resetCompute );
  2017. }.bind(this)
  2018. this.addEvent(eventName, resetCompute);
  2019. }
  2020. }
  2021. }
  2022. });
  2023. if(!module.parentLine)module.parentLine = this;
  2024. if(!module.parentDatatemplate)module.parentDatatemplate = this.template;
  2025. if( json.type==="Attachment" || json.type==="AttachmentDg" ){
  2026. module.addEvent("change", function(){
  2027. _self.form.saveFormData();
  2028. }.bind(this))
  2029. }else if( json.type==="Datatemplate" ){
  2030. this.subDatatemplateModuleList.push(module);
  2031. }
  2032. this.form.modules.push(module);
  2033. this.modules.push(module);
  2034. this.all[id] = module;
  2035. this.all_templateId[templateJsonId] = module;
  2036. if (module.field) {
  2037. // if(this.data.hasOwnProperty(templateJsonId)){
  2038. // module.setData(this.data[templateJsonId]);
  2039. // }
  2040. this.allField[id] = module;
  2041. this.allField_templateId[templateJsonId] = module;
  2042. this.fields.push( module );
  2043. module.addEvent("change", function(){
  2044. this.template.fireEvent("change", [{lines: [this], type: "editmodule", module: module}]);
  2045. }.bind(this))
  2046. }
  2047. this.setEvents(module, templateJsonId);
  2048. this.checkSequence(module, templateJsonId);
  2049. }
  2050. }
  2051. }.bind(this));
  2052. if(this.options.isNew){
  2053. this.data = this.getData();
  2054. this.options.isNew = false;
  2055. }
  2056. },
  2057. setSubDatatemplateOuterActionEvents: function(){
  2058. this.subDatatemplateModuleList.each(function(module){
  2059. //绑定下级模板事件
  2060. module._setOuterActionEvents();
  2061. //让下级数据模板再去绑定下级模板外部事件
  2062. module._setSubDatatemplateOuterEvents();
  2063. })
  2064. },
  2065. getIndex: function(){
  2066. return this.options.index;
  2067. },
  2068. getModule: function(templateJsonId){
  2069. return this.all_templateId[templateJsonId];
  2070. },
  2071. get: function(templateJsonId){
  2072. return this.all_templateId[templateJsonId];
  2073. },
  2074. getAttachmentSite: function(json, templateJsonId, sectionKey){
  2075. //确保site最长为64,否则后台会报错
  2076. var index = this.options.index;
  2077. var baseSite;
  2078. baseSite = "." + index + "." + (json.site || templateJsonId);
  2079. var maxLength;
  2080. var sectionId = "";
  2081. if( sectionKey ){
  2082. maxLength = Math.floor((63 - baseSite.length)/2 );
  2083. sectionId = (sectionKey.length > maxLength) ? sectionKey.substr(sectionKey.length-maxLength, maxLength) : sectionKey;
  2084. sectionId = "." + sectionId;
  2085. }else{
  2086. maxLength = 64 - baseSite.length;
  2087. }
  2088. var templateId = this.template.json.id;
  2089. templateId = (templateId.length > maxLength) ? templateId.substr(templateId.length-maxLength, maxLength) : templateId;
  2090. return templateId + sectionId + baseSite;
  2091. },
  2092. setEvents: function (module, id) {
  2093. if( this.template.addActionIdList.contains( id )){
  2094. this.addActionList.push( module );
  2095. module.node.addEvent("click", function (ev) {
  2096. this.template._insertLine( ev, this )
  2097. }.bind(this))
  2098. if( !this.template.editable )module.node.hide();
  2099. if( !this.options.isAddable )module.node.hide();
  2100. }
  2101. if( this.template.deleteActionIdList.contains(id)){
  2102. this.deleteActionList.push( module );
  2103. module.node.addEvent("click", function (ev) {
  2104. this.template._deleteLine( ev, this )
  2105. }.bind(this))
  2106. if( !this.template.editable )module.node.hide();
  2107. if( !this.options.isDeleteable )module.node.hide();
  2108. }
  2109. if( this.template.selectorId === id){
  2110. this.selector = module;
  2111. // module.setData(""); //默认不选择
  2112. module.node.addEvent("click", function (ev) {
  2113. this.checkSelect();
  2114. }.bind(this))
  2115. if( !this.template.editable )module.node.hide();
  2116. if( !this.options.isDeleteable )module.node.hide();
  2117. this.unselect();
  2118. }
  2119. //???
  2120. // if( this.template.importActionIdList.contains(id))this.importActionList.push( module );
  2121. // if( this.template.exportActionIdList.contains(id))this.exportActionList.push( module );
  2122. },
  2123. checkSequence: function(module, id){
  2124. if( this.template.sequenceIdList.contains(id)){
  2125. this.sequenceNodeList.push( module );
  2126. var indexText;
  2127. if(
  2128. ( this.template.isShowSectionKey() && this.template.json.sequenceBySection === "section" ) ||
  2129. ( this.template.isShowSectionBy() && this.template.json.sequenceBy === "section" )
  2130. ){
  2131. indexText = this.options.indexInSectionLineText;
  2132. }else{
  2133. indexText = this.options.indexText;
  2134. }
  2135. if(this.form.getModuleType(module) === "label"){
  2136. if (module.json.valueType === "script" && module.json.script && module.json.script.code){
  2137. var value = module.form.Macro.exec(module.json.script.code, module);
  2138. module._setNodeText(value);
  2139. }else{
  2140. module.node.set("text", indexText );
  2141. }
  2142. }else if(module.setData){
  2143. module.setData( indexText );
  2144. }
  2145. }
  2146. },
  2147. checkSelect: function () {
  2148. var selectData = this.selector.getData();
  2149. var selected;
  2150. if(o2.typeOf(selectData)==="array"){
  2151. selected = selectData.contains(this.template.json.selectorSelectedValue);
  2152. }else{
  2153. selected = selectData === this.template.json.selectorSelectedValue;
  2154. }
  2155. this.selected = selected;
  2156. },
  2157. select: function(){
  2158. this.selected = true;
  2159. if(this.selector)this.selector.setData(this.template.json.selectorSelectedValue);
  2160. },
  2161. unselect: function(){
  2162. this.selected = false;
  2163. if( this.selector.getOptionsObj ){
  2164. var options = this.selector.getOptionsObj();
  2165. var value = "";
  2166. var arr = options.valueList || [];
  2167. for( var i=0; i<arr.length; i++ ){
  2168. var v = arr[i];
  2169. if( v !== this.template.json.selectorSelectedValue ){
  2170. value = v;
  2171. break;
  2172. }
  2173. }
  2174. this.selector.setData(value);
  2175. }else{
  2176. this.selector.setData("")
  2177. }
  2178. },
  2179. reload: function(){
  2180. this.reloading = true;
  2181. for(var key in this.all){
  2182. var module = this.all[key];
  2183. this.form.modules.erase(module);
  2184. if (this.form.all[key]) delete this.form.all[key];
  2185. if (this.form.forms[key])delete this.form.forms[key];
  2186. }
  2187. this.node.empty();
  2188. this.load();
  2189. this.reloading = false;
  2190. },
  2191. clearSubModules: function () { //把module清除掉
  2192. for(var key in this.all){
  2193. var module = this.all[key];
  2194. //如果嵌套数据模板或者数据表格,还要清除掉下级
  2195. if(module.clearSubModules)module.clearSubModules();
  2196. if( module.json && (module.json.type==="TinyMCEEditor" || module.json.type==="Htmleditor"))module.destroy();
  2197. this.form.modules.erase(module);
  2198. if (this.form.all[key]) delete this.form.all[key];
  2199. if (this.form.forms[key])delete this.form.forms[key];
  2200. }
  2201. this.node.destroy();
  2202. },
  2203. computeModuleData: function( when ){
  2204. for( var key in this.allField){
  2205. var module = this.allField[key];
  2206. var id = key.split("..").getLast();
  2207. if( module.json.compute === when ){
  2208. this.data[id] = module.getData();
  2209. }
  2210. }
  2211. },
  2212. getData: function () {
  2213. var data = this.data;
  2214. for( var key in this.allField){
  2215. var module = this.allField[key];
  2216. var id = key.split("..").getLast();
  2217. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ) {
  2218. data[id] = module._getBusinessData();
  2219. }else if( module.json.compute === "save" && module.getInputData ){
  2220. data[id] = module.getInputData();
  2221. }else{
  2222. data[id] = module.getData();
  2223. }
  2224. }
  2225. return data;
  2226. },
  2227. setData: function (data) {
  2228. this.template._setLineData(this, data);
  2229. },
  2230. deleteAttachment: function(){
  2231. var saveFlag = false;
  2232. for( var key in this.allField){
  2233. var module = this.allField[key];
  2234. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  2235. var array = module._getBusinessData();
  2236. (array || []).each(function(d){
  2237. saveFlag = true;
  2238. this.form.workAction.deleteAttachment(d.id, this.form.businessData.work.id);
  2239. }.bind(this))
  2240. }
  2241. }
  2242. return saveFlag;
  2243. },
  2244. saveValidation: function(){
  2245. if( !this.options.isEdited )return true;
  2246. var flag = true;
  2247. this.fields.each(function(field, key){
  2248. if (field.json.type!="sequence" && field.validationMode ){
  2249. field.validationMode();
  2250. if (!field.saveValidation()) flag = false;
  2251. }
  2252. }.bind(this));
  2253. return flag;
  2254. }
  2255. });
  2256. MWF.xApplication.process.Xform.Datatemplate.ImporterDatatemplate = new Class({
  2257. Extends: MWF.xApplication.process.Xform.Datatemplate,
  2258. initialize: function(mainDatatable){
  2259. this.mainDatatable = mainDatatable;
  2260. this.json = Object.clone( mainDatatable.json );
  2261. this.json.id = this.json.id + "_o2simula";
  2262. this.node = new Element("div", {
  2263. id: this.json.id
  2264. });
  2265. this.node.store("module", this);
  2266. this.form = mainDatatable.form;
  2267. this.field = false;
  2268. this.fieldModuleLoaded = false;
  2269. },
  2270. load: function(){
  2271. this.deleteFormData = function(data){
  2272. delete data[this.id];
  2273. this._self.form.removeEvent("getData", this._self.deleteFormData);
  2274. }.bind({_self: this, id: this.json.id});
  2275. this.form.addEvent("getData", this.deleteFormData);
  2276. this._loadModuleEvents();
  2277. if (this.fireEvent("queryLoad")){
  2278. this._queryLoaded();
  2279. this._loadUserInterface();
  2280. this._afterLoaded();
  2281. this.fireEvent("afterLoad");
  2282. }
  2283. },
  2284. _loadUserInterface: function(){
  2285. this.editable = true;
  2286. //是否多行同时编辑
  2287. this.multiEditMode = true;
  2288. this.data = [];
  2289. this._setValue(this.data);
  2290. this.lineList = [];
  2291. this.loadDatatemplate();
  2292. },
  2293. loadDatatemplate: function(){
  2294. this.loading = true;
  2295. this.templateJson = this.mainDatatable.templateJson;
  2296. this.templateHtml = this.mainDatatable.templateHtml;
  2297. this.fireEvent("load");
  2298. this._loadDataTemplate(function(){
  2299. this.fieldModuleLoaded = true;
  2300. this.loading = false;
  2301. this.fireEvent("postLoad");
  2302. }.bind(this));
  2303. },
  2304. _loadDataTemplate: function(callback){
  2305. this._loadLineList(function(){
  2306. if(callback)callback();
  2307. }.bind(this));
  2308. },
  2309. _loadLine: function(container, data, index, isNew){
  2310. var line = new MWF.xApplication.process.Xform.Datatemplate.ImporterLine(container, this, data, {
  2311. index : index,
  2312. indexText : (index+1).toString()
  2313. });
  2314. this.fireEvent("beforeLoadLine", [line]);
  2315. line.load();
  2316. this.fireEvent("afterLoadLine", [line]);
  2317. return line;
  2318. },
  2319. _addLine: function(ev, d){
  2320. var data, index, line;
  2321. index = this.lineList.length;
  2322. data = this.getInputData();
  2323. data.push(d||{});
  2324. this.newLineIndex = index;
  2325. this.setData( data );
  2326. line = this.getLine(index);
  2327. line.isNewAdd = true;
  2328. this.fireEvent("addLine", [{"line":line, "ev":ev}]);
  2329. this.fireEvent("change", [{"lines":[line], "type":"addline"}]);
  2330. return line;
  2331. },
  2332. _setSubDatatemplateOuterEvents: function(){
  2333. },
  2334. destroy: function(){
  2335. this.clearSubModules();
  2336. var id = this.json.id;
  2337. if (this.form.all[id]) delete this.form.all[id];
  2338. if (this.form.forms[id])delete this.form.forms[id];
  2339. // if( this.form.businessData.data.hasOwnProperty(id) )delete this.form.businessData.data[id];
  2340. // if( this.form.Macro.environment.data.hasOwnProperty(id) )delete this.form.Macro.environment.data[id];
  2341. this.node.destroy();
  2342. }
  2343. });
  2344. //Excel导入时候创建的组件
  2345. MWF.xApplication.process.Xform.Datatemplate.ImporterLine = new Class({
  2346. Implements: [Options, Events],
  2347. options: {
  2348. isNew: true,
  2349. isEdited : true, //是否正在编辑
  2350. isEditable : true, //能否被编辑
  2351. isDeleteable: true, //能否被删除
  2352. },
  2353. initialize: function (node, template, data, options) {
  2354. this.setOptions(options);
  2355. this.node = node;
  2356. this.template = template;
  2357. this.data = data;
  2358. this.form = this.template.form;
  2359. this.init()
  2360. },
  2361. init: function(){
  2362. this.modules = [];
  2363. this.all = {};
  2364. this.all_templateId = {};
  2365. this.fields = [];
  2366. this.allField = {};
  2367. this.allField_templateId = {};
  2368. },
  2369. load: function(){
  2370. this.loadModules();
  2371. },
  2372. loadModules: function(){
  2373. this.node.set("html", this.template.templateHtml);
  2374. var moduleNodes = this.form._getModuleNodes(this.node);
  2375. //合并状态或拆分状态
  2376. // var sectionKey = this.options.sectionKey || this.datatable.sectionBy;
  2377. moduleNodes.each(function (node) {
  2378. var mwfType = node.get("MWFtype");
  2379. if (mwfType === "form")return;
  2380. var _self = this;
  2381. var tJson = this.form._getDomjson(node);
  2382. if( tJson ){
  2383. var json = Object.clone(tJson);
  2384. if( !this.options.isEdited )json.isReadonly = true;
  2385. var templateJsonId = json.id;
  2386. var index = "0"; //this.options.index;
  2387. var id = this.template.json.id + "..data.." + index + ".." + json.id;
  2388. json.id = id;
  2389. node.set("id", id);
  2390. if (this.form.all[id]) this.form.all[id] = null;
  2391. if (this.form.forms[id])this.form.forms[id] = null;
  2392. var module = this.form._loadModule(json, node, function () {
  2393. if( _self.widget )this.widget = _self.widget;
  2394. this.parentLine = _self;
  2395. this.parentDatatemplate = _self.template;
  2396. //只读方法值在页面加载的时候或者new的时候计算一下
  2397. if( this.json.compute === "show" ){
  2398. var needComputeShow = false;
  2399. if( _self.template.loading ) {
  2400. needComputeShow = true;
  2401. }else if( _self.options.isNew && !_self.reloading ){
  2402. needComputeShow = true;
  2403. }
  2404. if( !needComputeShow ){
  2405. this.json.compute = "create"; //
  2406. if( this.options.moduleEvents && this.options.moduleEvents.length ){ //恢复compute
  2407. var eventName = ( this.options.moduleEvents || [] ).contains("afterLoad") ? "afterLoad" : "load";
  2408. var resetCompute = function () {
  2409. this.json.compute = "show";
  2410. this.removeEvent( eventName, resetCompute );
  2411. }.bind(this)
  2412. this.addEvent(eventName, resetCompute);
  2413. }
  2414. }
  2415. }
  2416. });
  2417. if(!module.parentLine)module.parentLine = this;
  2418. if(!module.parentDatatemplate)module.parentDatatemplate = this.template;
  2419. this.form.modules.push(module);
  2420. this.modules.push(module);
  2421. this.all[id] = module;
  2422. this.all_templateId[templateJsonId] = module;
  2423. if( module.field && this.options.isEdited ) {
  2424. this.allField[id] = module;
  2425. this.allField_templateId[templateJsonId] = module;
  2426. this.fields.push( module );
  2427. }
  2428. }
  2429. }.bind(this));
  2430. },
  2431. clearSubModules: function () { //把module清除掉
  2432. for(var key in this.all){
  2433. var module = this.all[key];
  2434. //如果嵌套数据模板或者数据表格,还要清除掉下级
  2435. if(module.clearSubModules)module.clearSubModules();
  2436. if( module.json && (module.json.type==="TinyMCEEditor" || module.json.type==="Htmleditor"))module.destroy();
  2437. this.form.modules.erase(module);
  2438. // if( this.form.businessData.data.hasOwnProperty(key) )delete this.form.businessData.data[key];
  2439. // if( this.form.Macro.environment.data.hasOwnProperty(key) )delete this.form.Macro.environment.data[key];
  2440. if (this.form.all[key]) delete this.form.all[key];
  2441. if (this.form.forms[key])delete this.form.forms[key];
  2442. }
  2443. this.node.destroy();
  2444. this.init();
  2445. },
  2446. getModule: function(templateJsonId){
  2447. return this.all_templateId[templateJsonId];
  2448. },
  2449. get: function(templateJsonId){
  2450. return this.all_templateId[templateJsonId];
  2451. },
  2452. getData: function () {
  2453. var data = this.data;
  2454. for( var key in this.allField_templateId){
  2455. var module = this.allField_templateId[key];
  2456. // var id = key.split("..").getLast();
  2457. if( module.json.type==="Attachment" || module.json.type==="AttachmentDg" ){
  2458. data[key] = module._getBusinessData();
  2459. }else{
  2460. data[key] = module.getData();
  2461. }
  2462. }
  2463. return data;
  2464. }
  2465. });
  2466. MWF.xApplication.process.Xform.Datatemplate.Exporter = new Class({
  2467. Implements: [Options, Events],
  2468. options: {
  2469. },
  2470. initialize: function (template, options) {
  2471. this.setOptions(options);
  2472. this.template = template;
  2473. this.form = this.template.form;
  2474. },
  2475. exportToExcel : function () {
  2476. MWF.require("MWF.widget.Mask", null, false);
  2477. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  2478. // 适配移动端
  2479. if (layout.mobile) {
  2480. this.mask.load();
  2481. } else {
  2482. this.mask.loadNode(this.form.app.content);
  2483. }
  2484. this.template.fireEvent("beforeExport");
  2485. var resultArr = [];
  2486. var titleArr = this.template.json.excelFieldConfig.map(function(config){
  2487. return config.title;
  2488. });
  2489. resultArr.push( titleArr );
  2490. var lineList = [];
  2491. if( this.template.isShowAllSection ){
  2492. lineList = this.template.sectionLineEdited ? this.template.sectionLineEdited.lineList : this.template.lineList;
  2493. }else if( this.template.isMergeRead ) {
  2494. lineList = this.template.lineList;
  2495. }else{
  2496. lineList = this.template.lineList;
  2497. }
  2498. lineList.each(function (line, index) {
  2499. var lineData = this.getLineExportData(line, index);
  2500. var p = Promise.all(lineData).then(function (arr) {
  2501. return arr;
  2502. });
  2503. resultArr.push( p );
  2504. }.bind(this));
  2505. Promise.all(resultArr).then(function ( rstArr ) {
  2506. var colWidthArr = this.getColWidthArray();
  2507. var excelName = this.getExcelName();
  2508. var arg = {
  2509. data : rstArr,
  2510. colWidthArray : colWidthArr,
  2511. title : excelName
  2512. };
  2513. this.template.fireEvent("export", [arg]);
  2514. new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template ).exportToExcel(
  2515. arg.data || rstArr,
  2516. arg.title || excelName,
  2517. arg.colWidthArray || colWidthArr,
  2518. this.getDateIndexArray(), //日期格式列下标
  2519. null,
  2520. function () {
  2521. if (this.mask) {
  2522. this.mask.hide();
  2523. this.mask = null;
  2524. }
  2525. }.bind(this)
  2526. );
  2527. }.bind(this))
  2528. },
  2529. getLineExportData: function(line, index ){
  2530. var exportData = [];
  2531. this.template.json.excelFieldConfig.each(function (config) {
  2532. var module = line.all_templateId[config.field];
  2533. var json = module ? module.json : "";
  2534. if ( !module || !json || !this.isAvaliableField( json ) ) {
  2535. exportData.push("");
  2536. }else{
  2537. var text;
  2538. if ( json.type === "Label" && module.node) {
  2539. text = module.node.get("text");
  2540. }else{
  2541. text = module.getExcelData();
  2542. }
  2543. if( !text && o2.typeOf(text) !== "number" ){
  2544. text = "";
  2545. }
  2546. exportData.push( text );
  2547. }
  2548. }.bind(this));
  2549. return exportData;
  2550. },
  2551. // getLineExportData: function(line, index ){
  2552. // var exportData = [];
  2553. // this.template.json.excelFieldConfig.each(function (config) {
  2554. //
  2555. // var module = line.all_templateId[config.field];
  2556. // var json = module ? module.json : "";
  2557. //
  2558. // if ( !module || !json || !this.isAvaliableField( json ) ) {
  2559. // exportData.push("");
  2560. // }else{
  2561. // var value = module.getData();
  2562. // var text = "";
  2563. //
  2564. //
  2565. // if( value ){
  2566. // switch (module.json.type) {
  2567. // case "Org":
  2568. // case "Reader":
  2569. // case "Author":
  2570. // case "Personfield":
  2571. // case "Orgfield":
  2572. // if (o2.typeOf(value) === "array") {
  2573. // var textArray = [];
  2574. // value.each(function (item) {
  2575. // if (o2.typeOf(item) === "object") {
  2576. // textArray.push(item.distinguishedName);
  2577. // } else {
  2578. // textArray.push(item);
  2579. // }
  2580. // }.bind(this));
  2581. // text = textArray.join(", \n");
  2582. // } else if (o2.typeOf(value) === "object") {
  2583. // text = value.distinguishedName;
  2584. // } else {
  2585. // text = value;
  2586. // }
  2587. // break;
  2588. // case "Combox":
  2589. // case "Address":
  2590. // text = o2.typeOf(value) === "array" ? value.join(", ") : value;
  2591. // break;
  2592. // case "Checkbox":
  2593. // var options = module.getOptionsObj();
  2594. // var value = o2.typeOf(value) === "array" ? value : [value];
  2595. // var arr = [];
  2596. // value.each( function( a, i ){
  2597. // var idx = options.valueList.indexOf( a );
  2598. // arr.push( idx > -1 ? options.textList[ idx ] : "") ;
  2599. // });
  2600. // text = arr.join(", ");
  2601. // break;
  2602. // case "Radio":
  2603. // case "Select":
  2604. // var options = module.getOptionsObj();
  2605. // var idx = options.textList.indexOf( value );
  2606. // text = idx > -1 ? options.valueList[ idx ] : "";
  2607. // break;
  2608. // case "Textarea":
  2609. // text = value;
  2610. // break;
  2611. // case "Calendar":
  2612. // text = value;
  2613. // break;
  2614. // default:
  2615. // text = value;
  2616. // break;
  2617. // }
  2618. // } else if ( json.type === "Label" && module.node) {
  2619. // text = module.node.get("text");
  2620. // }
  2621. //
  2622. // if( !text && o2.typeOf(text) !== "number" ){
  2623. // text = "";
  2624. // }
  2625. //
  2626. // exportData.push( text );
  2627. // }
  2628. // }.bind(this));
  2629. // return exportData;
  2630. // },
  2631. isAvaliableField : function(json){
  2632. if (["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label",
  2633. "Elbutton","Elbutton","Elcarousel","Eldropdown","Elicon","Eltree"].contains( json.type) )return false; //图片,附件,Label不导入导出
  2634. return true;
  2635. },
  2636. getExcelName: function(){
  2637. var title;
  2638. if( this.template.json.excelName && this.template.json.excelName.code ){
  2639. title = this.form.Macro.exec(this.template.json.excelName.code, this);
  2640. }else{
  2641. title = MWF.xApplication.process.Xform.LP.datatemplateExportDefaultName;
  2642. }
  2643. var titleA = title.split(".");
  2644. if( ["xls","xlst"].contains( titleA[titleA.length-1].toLowerCase() ) ){
  2645. titleA.splice( titleA.length-1 );
  2646. }
  2647. title = titleA.join(".");
  2648. return title;
  2649. },
  2650. getColWidthArray : function(){
  2651. var colWidthArr = [];
  2652. this.template.json.excelFieldConfig.each(function (config) {
  2653. var json = this.form.json.moduleList[config.field];
  2654. if ( !json ){
  2655. colWidthArr.push(150);
  2656. }else if ( ["Org","Reader","Author","Personfield","Orgfield"].contains(json.type)) {
  2657. colWidthArr.push(340);
  2658. } else if (json.type === "Address") {
  2659. colWidthArr.push(170);
  2660. } else if (json.type === "Textarea") {
  2661. colWidthArr.push(260);
  2662. } else if (json.type === "Htmleditor") {
  2663. colWidthArr.push(500);
  2664. } else if (json.type === "TinyMCEEditor") {
  2665. colWidthArr.push(500);
  2666. } else if (json.type === "Calendar") {
  2667. colWidthArr.push(150);
  2668. } else {
  2669. colWidthArr.push(150);
  2670. }
  2671. }.bind(this));
  2672. return colWidthArr;
  2673. },
  2674. getDateIndexArray : function(){
  2675. var dateIndexArr = []; //日期格式列下标
  2676. this.template.json.excelFieldConfig.each(function (config, i) {
  2677. var json = this.form.json.moduleList[config.field];
  2678. if (json && json.type === "Calendar") {
  2679. dateIndexArr.push(i);
  2680. }
  2681. }.bind(this));
  2682. return dateIndexArr;
  2683. },
  2684. exportWithImportDataToExcel : function ( columnList, importedData ) {
  2685. MWF.require("MWF.widget.Mask", null, false);
  2686. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  2687. // 适配移动端
  2688. if (layout.mobile) {
  2689. this.mask.load();
  2690. } else {
  2691. this.mask.loadNode(this.form.app.content);
  2692. }
  2693. this.template.fireEvent("beforeExport");
  2694. var resultArr = [];
  2695. var titleArr = this.template.json.excelFieldConfig.map(function(config){
  2696. return config.title;
  2697. });
  2698. titleArr.push( MWF.xApplication.process.Xform.LP.validationInfor );
  2699. resultArr.push( titleArr );
  2700. // this.template.lineList.each(function (line, index) {
  2701. // resultArr.push( this.getLineExportData(line, index) );
  2702. // }.bind(this));
  2703. importedData.each( function( lineData, lineIndex ){
  2704. var array = [];
  2705. columnList.each( function (obj, i) {
  2706. array.push( ( lineData[ obj.text ] || '' ).replace(/&#10;/g, "\n") );
  2707. });
  2708. array.push( lineData.errorTextListExcel ? lineData.errorTextListExcel.join("\n") : "" );
  2709. resultArr.push( array );
  2710. }.bind(this));
  2711. var colWidthArr = this.getColWidthArray();
  2712. colWidthArr.push(300); //提示信息
  2713. var excelName = this.getExcelName();
  2714. var arg = {
  2715. data : resultArr,
  2716. colWidthArray : colWidthArr,
  2717. title : excelName,
  2718. withError: true
  2719. };
  2720. this.template.fireEvent("export", [arg]);
  2721. new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template ).exportToExcel(
  2722. arg.data || resultArr,
  2723. arg.title || excelName,
  2724. arg.colWidthArray || colWidthArr,
  2725. this.getDateIndexArray(), //日期格式列下标
  2726. null,
  2727. function () {
  2728. if (this.mask) {
  2729. this.mask.hide();
  2730. this.mask = null;
  2731. }
  2732. }.bind(this)
  2733. );
  2734. }
  2735. });
  2736. MWF.xApplication.process.Xform.Datatemplate.Importer = new Class({
  2737. Implements: [Options, Events],
  2738. options: {
  2739. },
  2740. initialize: function (template, options) {
  2741. this.setOptions(options);
  2742. this.template = template;
  2743. this.form = this.template.form;
  2744. this.lp = MWF.xApplication.process.Xform.LP;
  2745. this.columnText = this.lp.importValidationColumnText;
  2746. this.columnTextExcel = this.lp.importValidationColumnTextExcel;
  2747. this.excelUtil = new MWF.xApplication.process.Xform.Datatemplate.ExcelUtils( this.template );
  2748. },
  2749. isAvaliableField : function(json, module, type){
  2750. if (["Image","Button","ImageClipper","WritingBoard","Attachment","AttachmentDg","Label",
  2751. "Elbutton","Elcarousel","Eldropdown","Elicon","Eltree"].contains( json.type) )return false; //图片,附件,Label不导入导出
  2752. return true;
  2753. },
  2754. importFromExcel : function () {
  2755. this.template.fireEvent("beforeImport");
  2756. this.getFieldArray();
  2757. var dateColArray = this.getDateIndexArray(); //日期列
  2758. var orgTitleArray = this.getOrgTitleArray();
  2759. this.excelUtil.upload( dateColArray, function (data) {
  2760. MWF.require("MWF.widget.Mask", null, false);
  2761. this.mask = new MWF.widget.Mask({ "style": "desktop", "zIndex": 50000 });
  2762. // 适配移动端
  2763. if (layout.mobile) {
  2764. this.mask.load();
  2765. } else {
  2766. this.mask.loadNode(this.form.app.content);
  2767. }
  2768. this.importedData = data;
  2769. if( !this.checkCount() )return;
  2770. this.loadSimulateModule();
  2771. this.fieldArray.each(function (c) {
  2772. c.module = this.importerLine.getModule(c.field)
  2773. }.bind(this));
  2774. if( orgTitleArray.length > 0 ){
  2775. this.listAllOrgData( orgTitleArray, function () {
  2776. this.checkAndImport();
  2777. }.bind(this));
  2778. }else{
  2779. this.checkAndImport();
  2780. }
  2781. }.bind(this));
  2782. },
  2783. checkAndImport: function(){
  2784. this.checkData( function (flag) {
  2785. if( !flag ){
  2786. this.openErrorDlg();
  2787. }else{
  2788. this.importData()
  2789. }
  2790. this.destroySimulateModule();
  2791. }.bind(this));
  2792. },
  2793. destroySimulateModule: function(){
  2794. if( !this.importerDatatemplate ){
  2795. this.form.disallowSaving = false;
  2796. return;
  2797. }
  2798. this.importerDatatemplate.destroy();
  2799. this.importerDatatemplate = null;
  2800. this.form.disallowSaving = false;
  2801. },
  2802. loadSimulateModule: function(){
  2803. this.form.disallowSaving = true;
  2804. this.importerDatatemplate = new MWF.xApplication.process.Xform.Datatemplate.ImporterDatatemplate( this.template );
  2805. this.importerDatatemplate.load();
  2806. this.importerLine = this.importerDatatemplate.addLine({});
  2807. },
  2808. getFieldArray: function(){
  2809. //this.loadSimulateModule();
  2810. this.fieldArray = []; //日期格式列下标
  2811. this.template.json.excelFieldConfig.each(function (config, i) {
  2812. this.fieldArray.push({
  2813. "text": config.title,
  2814. "field": config.field,
  2815. "index": i,
  2816. // "module": this.simelateModuleMap[config.field],
  2817. "json": this.form.json.moduleList[config.field]
  2818. })
  2819. }.bind(this));
  2820. },
  2821. getDateIndexArray : function(){
  2822. var dateIndexArr = []; //日期格式列下标
  2823. this.template.json.excelFieldConfig.each(function (config, i) {
  2824. var json = this.form.json.moduleList[config.field];
  2825. if (json && json.type === "Calendar" && (c.mJson.format === "%Y-%m-%d" || c.mJson.format === "%Y-%m-%d %H:%M:%S")) {
  2826. dateIndexArr.push(i);
  2827. }
  2828. }.bind(this));
  2829. return dateIndexArr;
  2830. },
  2831. getOrgTitleArray : function(){
  2832. var orgTitleArr = []; //日期格式列下标
  2833. this.template.json.excelFieldConfig.each(function (config, i) {
  2834. var json = this.form.json.moduleList[config.field];
  2835. if (json && ["Org","Reader","Author","Personfield","Orgfield"].contains(json.type) ) {
  2836. orgTitleArr.push(config.title);
  2837. }
  2838. }.bind(this));
  2839. return orgTitleArr;
  2840. },
  2841. parseImportedData: function(){
  2842. var data = [];
  2843. this.importedData.each( function( ilineData ){
  2844. var lineData = {};
  2845. this.fieldArray.each( function (obj, i) {
  2846. var index = obj.index;
  2847. var module = obj.module;
  2848. var json = obj.json;
  2849. var text = obj.text;
  2850. var d = ilineData[text] || "";
  2851. var value;
  2852. if( d === "" || d === undefined || d === null ){
  2853. value = "";
  2854. }else{
  2855. switch (json.type) {
  2856. case "Org":
  2857. case "Reader":
  2858. case "Author":
  2859. case "Personfield":
  2860. case "Orgfield":
  2861. var arr = this.stringToArray(d); //空格,空格
  2862. if( arr.length === 0 ){
  2863. value = this.getOrgData( d );
  2864. }else{
  2865. value = [];
  2866. arr.each( function(d, idx){
  2867. var obj = this.getOrgData( d );
  2868. value.push( obj );
  2869. }.bind(this));
  2870. }
  2871. break;
  2872. default:
  2873. value = d; //d.replace(/&#10;/g,""); //换行符&#10;
  2874. break;
  2875. }
  2876. }
  2877. lineData[ json.id ] = value;
  2878. }.bind(this));
  2879. data.push( lineData );
  2880. }.bind(this));
  2881. return data;
  2882. },
  2883. stringToArray: function(string){
  2884. return string.replace(/&#10;/g,",").split(/\s*,\s*/g ).filter(function(s){
  2885. return !!s;
  2886. });
  2887. },
  2888. importData: function(){
  2889. var data = this.parsedData;
  2890. this.template.fireEvent("import", [data] );
  2891. this.template.setData( data );
  2892. this.template.fireEvent("afterImport", [data] );
  2893. this.template.fireEvent("change", [{lines: this.template.lineList, type : "import"}]);
  2894. if (this.mask) {
  2895. this.mask.hide();
  2896. this.mask = null;
  2897. }
  2898. this.form.notice( MWF.xApplication.process.Xform.LP.importSuccess );
  2899. },
  2900. openErrorDlg : function(){
  2901. var eData = this.importedData;
  2902. var _self = this;
  2903. var objectToString = function (obj, type) {
  2904. if(!obj)return "";
  2905. var arr = [];
  2906. Object.each(obj, function (value, key) {
  2907. if( type === "style" ){
  2908. arr.push( key + ":"+ value +";" )
  2909. }else{
  2910. arr.push( key + "='"+ value +"'" )
  2911. }
  2912. });
  2913. return arr.join(" ")
  2914. }
  2915. var htmlArray = ["<table "+ objectToString( this.template.json.impExpTableProperties ) +" style='"+objectToString( this.template.json.impExpTableStyles, "style" )+"'>"];
  2916. var titleStyle = objectToString(this.template.json.impExpTableTitleStyles, "style");
  2917. htmlArray.push("<tr>");
  2918. this.fieldArray.each(function (obj, i) {
  2919. htmlArray.push( "<th style='"+titleStyle+"'>"+obj.text+"</th>" );
  2920. });
  2921. htmlArray.push("<th style='"+titleStyle+"'> "+MWF.xApplication.process.Xform.LP.validationInfor +"</th>");
  2922. htmlArray.push("</tr>" );
  2923. var contentStyles = Object.clone( this.template.json.impExpTableContentStyles );
  2924. if( !contentStyles[ "border-bottom" ] && !contentStyles[ "border" ] )contentStyles[ "border-bottom" ] = "1px solid #eee";
  2925. var contentStyle = objectToString( Object.merge( contentStyles, {"text-align":"left"}) , "style" );
  2926. eData.each( function( lineData, lineIndex ){
  2927. htmlArray.push( "<tr>" );
  2928. this.fieldArray.each( function (obj, i) {
  2929. htmlArray.push( "<td style='"+contentStyle+"'>"+ ( lineData[ obj.text ] || '' ).replace(/&#10;/g,"<br/>") +"</td>" ); //换行符&#10;
  2930. });
  2931. htmlArray.push( "<td style='"+contentStyle+"'>"+( lineData.errorTextList ? lineData.errorTextList.join("<br/>") : "" )+"</td>" );
  2932. htmlArray.push( "</tr>" );
  2933. }.bind(this));
  2934. htmlArray.push( "</table>" );
  2935. var width = this.template.json.impExpDlgWidth || 1000;
  2936. var height = this.template.json.impExpDlgHeight || 700;
  2937. width = width.toInt();
  2938. height = height.toInt();
  2939. var div = new Element("div", { style : "padding:10px;", html : htmlArray.join("") });
  2940. var dlg = o2.DL.open({
  2941. "style" : this.form.json.dialogStyle || "user",
  2942. "title": MWF.xApplication.process.Xform.LP.importFail,
  2943. "content": div,
  2944. "offset": {"y": 0},
  2945. "isMax": true,
  2946. "width": width,
  2947. "height": height,
  2948. "buttonList": [
  2949. {
  2950. "type": "exportWithError",
  2951. "text": MWF.xApplication.process.Xform.LP.datagridExport,
  2952. "action": function () { _self.exportWithImportDataToExcel(eData); }
  2953. },
  2954. {
  2955. "type": "cancel",
  2956. "text": MWF.LP.process.button.cancel,
  2957. "action": function () { dlg.close(); }
  2958. }
  2959. ],
  2960. "onPostClose": function(){
  2961. dlg = null;
  2962. }.bind(this)
  2963. });
  2964. if (this.mask) {
  2965. this.mask.hide();
  2966. this.mask = null;
  2967. }
  2968. },
  2969. checkCount: function(){
  2970. var idata = this.importedData;
  2971. var lp = MWF.xApplication.process.Xform.LP;
  2972. var exceeded = false;
  2973. var maxCount = this.template.json.maxCount ? this.template.json.maxCount.toInt() : 0;
  2974. if( maxCount > 0 && idata.length > maxCount )exceeded = true;
  2975. var less = false;
  2976. var minCount = this.template.json.minCount ? this.template.json.minCount.toInt() : 0;
  2977. if( minCount > 0 && idata.length < minCount) less = true;
  2978. if( exceeded ) {
  2979. var text = lp.importTooManyNotice.replace("{n1}", idata.length).replace("{n2}", this.template.json.maxCount);
  2980. this.form.notice(text, "error");
  2981. return false;
  2982. }
  2983. if( less ){
  2984. var text = lp.importTooFewNotice.replace("{n1}", idata.length).replace("{n2}", this.template.json.minCount );
  2985. this.form.notice(text,"error");
  2986. return false;
  2987. }
  2988. return true;
  2989. },
  2990. checkData : function( callback){
  2991. this.parsedData = this.parseImportedData();
  2992. this.isImportSuccess = true;
  2993. this.checkLineData(0, function () {
  2994. var arg = {
  2995. validted : this.isImportSuccess,
  2996. data : this.importedData
  2997. };
  2998. this.template.fireEvent( "validImport", [arg] );
  2999. callback( arg.validted )
  3000. }.bind(this));
  3001. },
  3002. checkLineData: function(lineIndex, callback){
  3003. if( lineIndex < this.importedData.length ){
  3004. this._checkLineData(this.importedData[lineIndex], lineIndex, function (flag) {
  3005. lineIndex++;
  3006. if( !flag )this.isImportSuccess = false;
  3007. this.checkLineData(lineIndex, callback);
  3008. }.bind(this));
  3009. }else{
  3010. if(callback)callback();
  3011. }
  3012. },
  3013. _checkLineData: function(lineData, lineIndex, callback){
  3014. lineData.errorTextList = lineData.errorTextList || [];
  3015. lineData.errorTextListExcel = lineData.errorTextListExcel || [];
  3016. var parsedLineData = (this.parsedData && this.parsedData[lineIndex]) ? this.parsedData[lineIndex] : [];
  3017. this.checkModuleData(0, lineData, parsedLineData, function () {
  3018. var flag = !lineData.errorTextList.length;
  3019. callback(flag);
  3020. });
  3021. },
  3022. checkModuleData: function(index, lineData, parsedLineData, callback){
  3023. if( index < this.fieldArray.length ){
  3024. var result = this._checkModuleData(this.fieldArray[index], lineData, parsedLineData);
  3025. Promise.resolve(result).then(function (flag) {
  3026. index++;
  3027. this.checkModuleData(index, lineData, parsedLineData, callback);
  3028. }.bind(this))
  3029. }else{
  3030. if(callback)callback();
  3031. }
  3032. },
  3033. _checkModuleData: function (field, lineData, parsedLineData) {
  3034. var index = field.index;
  3035. var json = field.json;
  3036. var module = field.module;
  3037. var text = field.text;
  3038. var colInfor = this.columnText.replace( "{n}", index+1 );
  3039. var colInforExcel = this.columnTextExcel.replace( "{n}", this.excelUtil.index2ColName( index ) );
  3040. var d = lineData[text] || "";
  3041. var parsedD = parsedLineData[json.id] || "";
  3042. var lp = this.lp;
  3043. var flag = true;
  3044. if(d){
  3045. switch (json && json.type) {
  3046. case "Org":
  3047. case "Reader":
  3048. case "Author":
  3049. case "Personfield":
  3050. case "Orgfield":
  3051. var arr = this.stringToArray(d);
  3052. arr.each( function(d, idx){
  3053. var obj = this.getOrgData( d );
  3054. if( obj.errorText ){
  3055. lineData.errorTextList.push( colInfor + obj.errorText + lp.fullstop );
  3056. lineData.errorTextListExcel.push( colInforExcel + obj.errorText + lp.fullstop );
  3057. flag = false;
  3058. }
  3059. }.bind(this));
  3060. break;
  3061. case "Number":
  3062. case "Currency":
  3063. case "Elnumber":
  3064. if (isNaN(d)){
  3065. lineData.errorTextList.push( colInfor + d + lp.notValidNumber + lp.fullstop );
  3066. lineData.errorTextListExcel.push( colInforExcel + d + lp.notValidNumber + lp.fullstop );
  3067. flag = false;
  3068. }
  3069. break;
  3070. case "Calendar":
  3071. case "Eldate":
  3072. case "Eldatetime":
  3073. if( json.format === "%Y-%m-%d" || json.format === "%Y-%m-%d %H:%M:%S" ) {
  3074. if (!(isNaN(d) && !isNaN(Date.parse(d)))) {
  3075. lineData.errorTextList.push(colInfor + d + lp.notValidDate + lp.fullstop);
  3076. lineData.errorTextListExcel.push(colInforExcel + d + lp.notValidDate + lp.fullstop);
  3077. flag = false;
  3078. }
  3079. }
  3080. break;
  3081. default:
  3082. break;
  3083. }
  3084. }
  3085. if (module && module.setData && json.type !== "Address"){
  3086. var hasError = false;
  3087. if(["Org","Reader","Author","Personfield","Orgfield"].contains(json.type)){
  3088. if(o2.typeOf(parsedD)==="array" && parsedD.length){
  3089. hasError = parsedD.some(function (item) { return item.errorText; });
  3090. flag = false;
  3091. }
  3092. }
  3093. if(!hasError){
  3094. module.setExcelData(parsedD);
  3095. var ps = [];
  3096. if( module.moduleExcelAG )ps.push( module.moduleExcelAG );
  3097. if( module.moduleValueAG && !ps.contains(module.moduleValueAG) )ps.push( module.moduleValueAG );
  3098. if( module.moduleSelectAG && !ps.contains(module.moduleSelectAG) )ps.push( module.moduleSelectAG );
  3099. return Promise.all( ps ).then(function () {
  3100. var result = module.validationExcel();
  3101. if ( result && result.length ){
  3102. lineData.errorTextList.push(colInfor + result.join("\n") );
  3103. lineData.errorTextListExcel.push( colInforExcel + result.join("\n"));
  3104. flag = false;
  3105. }
  3106. parsedLineData[json.id] = module.getData();
  3107. return flag;
  3108. })
  3109. }
  3110. }
  3111. return flag
  3112. },
  3113. exportWithImportDataToExcel: function(eData){
  3114. var exporter = new MWF.xApplication.process.Xform.Datatemplate.Exporter(this.template);
  3115. exporter.exportWithImportDataToExcel(this.fieldArray, eData)
  3116. },
  3117. getOrgData : function( str ){
  3118. str = str.trim();
  3119. var flag = str.substr(str.length-2, 2);
  3120. switch (flag.toLowerCase()){
  3121. case "@i":
  3122. return this.identityMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  3123. case "@p":
  3124. return this.personMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  3125. case "@u":
  3126. return this.unitMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  3127. case "@g":
  3128. return this.groupMap[str] || {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  3129. default:
  3130. return this.identityMap[str] ||
  3131. this.personMap[str] ||
  3132. this.unitMap[str] ||
  3133. this.groupMap[str] ||
  3134. {"errorText": str + MWF.xApplication.process.Xform.LP.notExistInSystem };
  3135. }
  3136. },
  3137. listAllOrgData : function (orgTitleList, callback) {
  3138. var identityList = [], personList = [], unitList = [], groupList = [];
  3139. if( orgTitleList.length > 0 ){
  3140. this.importedData.each( function( lineData, lineIndex ){
  3141. // if( lineIndex === 0 )return;
  3142. orgTitleList.each( function (title, index) {
  3143. if( !lineData[title] )return;
  3144. var arr = this.stringToArray(lineData[title]);
  3145. arr.each( function( a ){
  3146. a = a.trim();
  3147. var flag = a.substr(a.length-2, 2);
  3148. switch (flag.toLowerCase()){
  3149. case "@i":
  3150. identityList.push( a ); break;
  3151. case "@p":
  3152. personList.push( a ); break;
  3153. case "@u":
  3154. unitList.push( a ); break;
  3155. case "@g":
  3156. groupList.push( a ); break;
  3157. default:
  3158. identityList.push( a );
  3159. personList.push( a );
  3160. unitList.push( a );
  3161. groupList.push( a );
  3162. break;
  3163. }
  3164. })
  3165. }.bind(this))
  3166. }.bind(this));
  3167. var identityLoaded, personLoaded, unitLoaded, groupLoaded;
  3168. var check = function () {
  3169. if( identityLoaded && personLoaded && unitLoaded && groupLoaded ){
  3170. if(callback)callback();
  3171. }
  3172. };
  3173. this.identityMap = {};
  3174. if( identityList.length ){
  3175. identityList = identityList.unique();
  3176. o2.Actions.load("x_organization_assemble_express").IdentityAction.listObject({ identityList : identityList }, function (json) {
  3177. json.data.each( function (d) { this.identityMap[ d.matchKey ] = d; }.bind(this));
  3178. identityLoaded = true;
  3179. check();
  3180. }.bind(this))
  3181. }else{
  3182. identityLoaded = true;
  3183. check();
  3184. }
  3185. this.personMap = {};
  3186. if( personList.length ){
  3187. personList = personList.unique();
  3188. o2.Actions.load("x_organization_assemble_express").PersonAction.listObject({ personList : personList }, function (json) {
  3189. json.data.each( function (d) { this.personMap[ d.matchKey ] = d; }.bind(this));
  3190. personLoaded = true;
  3191. check();
  3192. }.bind(this))
  3193. }else{
  3194. personLoaded = true;
  3195. check();
  3196. }
  3197. this.unitMap = {};
  3198. if( unitList.length ){
  3199. unitList = unitList.unique();
  3200. o2.Actions.load("x_organization_assemble_express").UnitAction.listObject({ unitList : unitList }, function (json) {
  3201. json.data.each( function (d) { this.unitMap[ d.matchKey ] = d; }.bind(this));
  3202. unitLoaded = true;
  3203. check();
  3204. }.bind(this))
  3205. }else{
  3206. unitLoaded = true;
  3207. check();
  3208. }
  3209. this.groupMap = {};
  3210. if( groupList.length ){
  3211. groupList = groupList.unique();
  3212. o2.Actions.load("x_organization_assemble_express").GroupAction.listObject({ groupList : groupList }, function (json) {
  3213. json.data.each( function (d) { this.groupMap[ d.matchKey ] = d; }.bind(this));
  3214. groupLoaded = true;
  3215. check();
  3216. }.bind(this))
  3217. }else{
  3218. groupLoaded = true;
  3219. check();
  3220. }
  3221. }
  3222. }
  3223. });
  3224. MWF.xDesktop.requireApp("Template", "utils.ExcelUtils", null, false);
  3225. MWF.xApplication.process.Xform.Datatemplate.ExcelUtils = new Class({
  3226. Extends: MWF.xApplication.Template.utils.ExcelUtils,
  3227. initialize: function(){
  3228. this.sheet2JsonOptions = {};
  3229. this.pollyfill();
  3230. }
  3231. });