MGrid.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. MWF.xDesktop.requireApp("Template", "MDomItem", null, false);
  2. //MWF.xDesktop.requireApp("Template", "lp." + MWF.language, null, false);
  3. var MGrid = new Class({
  4. Extends: MWF.widget.Common,
  5. Implements: [Options, Events],
  6. options: {
  7. style: "default",
  8. isNew: false,
  9. isEdited: false,
  10. showNotEmptyFlag : false,
  11. verifyType : "batch", //batch一起校验,或alert弹出
  12. //batch,所有item的错误都校验,每个item所有错误一起校验,错误显示在Item的字段后或指定区域;
  13. //batchSingle, 所有item的错误都校验,每个item只校验一个错误,错误显示在Item的字段后或指定区域;
  14. //alert,验证时遇到一个不成功的Item用app.notice()弹出消息后中断,
  15. //single,验证时遇到一个不成功的Item在字段后面或指定区域显示错误信息后中断
  16. itemTemplate : null,
  17. objectId : "",
  18. hasSequence : true,
  19. hasOperation : false,
  20. isCreateTh : true,
  21. containerIsTable : false,
  22. isCreateTrOnNull : true,
  23. minTrCount : 0,
  24. maxTrCount : 0,
  25. tableAttributes : null,
  26. thAttributes : null,
  27. tdAttributes : null,
  28. textOnly : false,
  29. tableClass : "formTable",
  30. thClass : "formTableTitle",
  31. tdClass : "formTableValue",
  32. thAlign : "center",
  33. tdAlign : "left",
  34. sequenceClass : "formTableSequence",
  35. addActionTdClass : "formTableAddTd",
  36. removeActionTdClass : "formTableRemoveTd",
  37. lp : {
  38. remove : "",
  39. add : ""
  40. }
  41. },
  42. initialize: function (container, data, options, app, css) {
  43. this.setOptions(options);
  44. this.path = "../x_component_Template/$MGrid/";
  45. this.cssPath = "../x_component_Template/$MGrid/" + this.options.style + "/css.wcss";
  46. this._loadCss();
  47. if (css) {
  48. this.css = Object.merge(this.css, css)
  49. }
  50. this.app = app;
  51. this.container = $(container);
  52. this.data = data;
  53. this.isSourceDataEmpty = false;
  54. if (!this.data || this.data == "") {
  55. this.isSourceDataEmpty = true;
  56. this.data = [{}];
  57. }
  58. this.itemTemplate = this.options.itemTemplate;
  59. this.items = null;
  60. this.th = null;
  61. this.trIndex = 0;
  62. this.trList = [];
  63. this.trObjs = null;
  64. this.trObjs_removed = null;
  65. this.trObjs_new = null;
  66. this.thTemplate = null; //属性 lable button_add styles class
  67. this.trTemplate = null; //属性 item sequence button_remove lable styles class
  68. this.valSeparator = /,|;|\^\^|\|/g; //如果是多值对象,作为用户选择的多个值的分隔符
  69. },
  70. load: function () {
  71. this.fireEvent("queryLoad");
  72. //如果itemTemplate没有name赋值Key
  73. for (var it in this.itemTemplate) {
  74. if (!this.itemTemplate[it]["name"]) {
  75. this.itemTemplate[it]["name"] = it;
  76. }
  77. this.itemTemplate[it]["key"] = it;
  78. }
  79. //如果itemTemplate没有name和Key不一致,那么根据name赋值itemTemplate
  80. var json = {};
  81. for (var it in this.itemTemplate) {
  82. if (it != this.itemTemplate[it]["name"]) {
  83. json[this.itemTemplate[it]["name"]] = this.itemTemplate[it]
  84. }
  85. }
  86. for (var it in json) {
  87. this.itemTemplate[it] = json[it];
  88. }
  89. this.createTable( this.itemTemplate );
  90. this.createHead( this.itemTemplate );
  91. this.trObjs = {};
  92. this.trObjs_removed = {};
  93. this.trObjs_new = {};
  94. this.trList = [];
  95. if( this.options.textOnly ){
  96. this.loadTextOnly();
  97. }else{
  98. (this.options.isEdited || this.options.isNew) ? this.loadEdit() : this.loadRead();
  99. }
  100. this.fireEvent("postLoad", [this]);
  101. },
  102. setTrTemplate: function( template ){
  103. if( typeOf( template ) == "string"){
  104. this.trTemplate = $(this.string2DOM( template )[0]);
  105. }else{
  106. this.trTemplate = $(template);
  107. }
  108. this.formatStyles( this.trTemplate );
  109. },
  110. setThTemplate : function( template ){
  111. if( typeOf( template ) == "string"){
  112. this.thTemplate = $(this.string2DOM( template )[0])
  113. }else{
  114. this.thTemplate = $(template);
  115. }
  116. this.formatStyles( this.thTemplate );
  117. },
  118. loadEdit:function(){
  119. if( !this.isSourceDataEmpty ){
  120. if( typeOf( this.data ) != "array" ){
  121. this.data = [ this.data ]
  122. }
  123. for( var i=0; i<this.data.length; i++ ){
  124. var d = this.data[i];
  125. var items = Object.clone(this.itemTemplate);
  126. for (var it in d ){
  127. if ( items[ it ] ){
  128. items[ it ].value = d[it];
  129. }
  130. }
  131. this.createTr( items, false, null, d );
  132. }
  133. //for( var it in this.itemTemplate ){
  134. // this.itemTemplate[it].value = "";
  135. //}
  136. }else if( this.options.isCreateTrOnNull ){
  137. this.createTr( this.itemTemplate, true );
  138. }
  139. },
  140. loadRead:function(){
  141. if( !this.isSourceDataEmpty ){
  142. if( typeOf( this.data ) != "array" ){
  143. this.data = [ this.data ]
  144. }
  145. for( var i=0; i<this.data.length; i++ ){
  146. var d = this.data[i];
  147. var items = Object.clone(this.itemTemplate);
  148. for (var it in d ){
  149. if ( items[ it ] ){
  150. items[ it ].value = d[it];
  151. }
  152. }
  153. this.createTr( items, false, null, d );
  154. }
  155. //for( var it in this.itemTemplate ){
  156. // this.itemTemplate[it].value = "";
  157. //}
  158. }else if( this.options.isCreateTrOnNull ){
  159. this.createTr( this.itemTemplate, true );
  160. }
  161. },
  162. loadTextOnly : function(){
  163. if( !this.isSourceDataEmpty ){
  164. if( typeOf( this.data ) != "array" ){
  165. this.data = [ this.data ]
  166. }
  167. for( var i=0; i<this.data.length; i++ ){
  168. var d = this.data[i];
  169. var items = Object.clone(this.itemTemplate);
  170. for (var it in d ){
  171. if ( items[ it ] ){
  172. items[ it ].value = d[it];
  173. }
  174. }
  175. //this.createTr( items, false );
  176. this.createTr_textOnly( items );
  177. }
  178. }else if( this.options.isCreateTrOnNull ){
  179. this.createTr_textOnly( this.itemTemplate );
  180. }
  181. },
  182. createTable : function( itemData ){
  183. if( this.options.containerIsTable ){
  184. this.table = this.container;
  185. var labelContainers = this.table.getElements("[lable]");
  186. labelContainers.each(function( el ) {
  187. var obj = itemData[el.get("lable")];
  188. if (!obj)return;
  189. if(obj.text)el.set("text",obj.text);
  190. });
  191. this.fireEvent( "postCreateTable", [this] );
  192. }else{
  193. var styles = {};
  194. if( this.options.tableClass && this.css[this.options.tableClass] )styles = this.css[this.options.tableClass];
  195. var tableAttr = this.options.tableAttributes || {};
  196. this.table = new Element( "table", {
  197. styles : styles
  198. }).inject(this.container);
  199. this.table.set( tableAttr );
  200. this.fireEvent( "postCreateTable", [this] );
  201. }
  202. return this.table;
  203. },
  204. createHead : function( itemData ){
  205. if( !this.options.isCreateTh )return;
  206. if( this.thTemplate ){
  207. return this.createHead_byTemplate( itemData );
  208. }else{
  209. return this.createHead_noTemplate( itemData );
  210. }
  211. },
  212. createHead_byTemplate : function( itemData ){
  213. var showNotEmptyFlag = this.options.showNotEmptyFlag;
  214. var isEdited = this.options.isEdited;
  215. var th = this.tableHead = this.thTemplate;
  216. var labelContainers = th.getElements("[lable]");
  217. labelContainers.each(function(el) {
  218. var lable = el.get("lable");
  219. var obj = itemData[lable];
  220. if (!obj)return;
  221. if(obj.text)el.set("text", obj.text);
  222. if( showNotEmptyFlag && itemData[lable].notEmpty && isEdited ){
  223. new Element( "span" , { styles : { color : "red" }, text : "*" }).inject( el )
  224. }
  225. });
  226. if( this.options.hasOperation && isEdited ){
  227. var add_button = th.getElement("[button_add]");
  228. if( add_button )this.createAddButton( add_button );
  229. }
  230. th.inject( this.table );
  231. return th;
  232. },
  233. createHead_noTemplate : function( itemData ){
  234. var tr = this.tableHead = new Element("tr");
  235. var align = this.options.thAlign == "" ? {} : { align : this.options.thAlign };
  236. var styles = (this.options.thClass && this.css[this.options.thClass]) ? this.css[this.options.thClass] : {};
  237. if( this.options.hasSequence ){
  238. var th = new Element("th", { text : MWF.xApplication.Template.LP.MGrid.sequence }).inject( tr );
  239. th.set( align );
  240. th.setStyles( styles );
  241. }
  242. var idx = 1;
  243. for (var it in this.itemTemplate){
  244. var thAttr = {};
  245. if(this.options.thAttributes && this.options.thAttributes["_"+idx] ){
  246. thAttr = this.options.thAttributes["_"+idx];
  247. }
  248. var th = new Element("th").inject(tr);
  249. if( this.options.showNotEmptyFlag && this.itemTemplate[it].notEmpty && this.options.isEdited ){
  250. new Element( "span" , { styles : { color : "red", text : "*" } }).inject( th )
  251. }
  252. th.set( align );
  253. th.setStyles( styles );
  254. if(this.itemTemplate[it].text)th.set("text", this.itemTemplate[it].text );
  255. idx++;
  256. }
  257. if( this.options.hasOperation && this.options.isEdited ){
  258. var th = new Element("th", { align : "center", styles : { width : "24px", styles : {"text-align" : "center" }} } ).inject(tr);
  259. if( this.options.addActionTdClass && this.css[this.options.addActionTdClass] )th.setStyles(this.css[this.options.addActionTdClass]);
  260. this.createAddButton( th );
  261. }
  262. tr.inject( this.table );
  263. this.fireEvent("postCreateHead", [tr] );
  264. return tr;
  265. },
  266. createAddButton : function( container ){
  267. var button = new Element("div", { title : MWF.xApplication.Template.LP.MGrid.add }).inject( container );
  268. if( this.options.lp.add )button.set("text",this.options.lp.add );
  269. if( this.css.actionAdd )button.setStyles( this.css.actionAdd );
  270. button.addEvent("click", function( e ){
  271. this.fireEvent("queryAddTr");
  272. this.createTr( this.itemTemplate, true );
  273. this.fireEvent("postAddTr");
  274. }.bind(this));
  275. if( this.css.actionAdd && this.css.actionAdd_over){
  276. button.addEvents({
  277. "mouseover" : function( e ){ this.node.setStyles( this.obj.css.actionAdd_over ) }.bind({ node : button, obj : this }),
  278. "mouseout" : function( e ){ this.node.setStyles( this.obj.css.actionAdd ) }.bind({ node : button, obj : this })
  279. })
  280. }
  281. return button;
  282. },
  283. addTrs : function( count ){
  284. if( 100 < count )count = 100;
  285. var trObj, trObjList = [];
  286. for( var i=0 ; i<count; i++ ){
  287. trObj = this.createTr( this.itemTemplate, true );
  288. trObjList.push( trObj );
  289. }
  290. return trObjList;
  291. },
  292. appendTr : function( d, isNew, unid, sourceData ){
  293. var items = Object.clone(this.itemTemplate);
  294. for (var it in d ){
  295. if ( items[ it ] ){
  296. items[ it ].value = d[it];
  297. }
  298. }
  299. var trObj = this.createTr( items, isNew, unid, sourceData );
  300. //for( var it in this.itemTemplate ){
  301. // this.itemTemplate[it].value = "";
  302. //}
  303. return trObj;
  304. },
  305. getTrCounts : function(){
  306. return this.trList.length;
  307. },
  308. createTr : function( itemData, isNew, unid, sourceData ){
  309. if( this.options.maxTrCount ){
  310. if( this.getTrCounts() < this.options.maxTrCount ){
  311. return this._createTr( itemData, isNew, unid, sourceData )
  312. }else{
  313. if( this.app && this.app.notice ){
  314. var text = MWF.xApplication.Template.LP.MGrid.addMaxLimitText.replace("{count}", this.options.maxTrCount);
  315. this.app.notice(text,"error");
  316. }
  317. }
  318. }else{
  319. return this._createTr( itemData, isNew, unid, sourceData )
  320. }
  321. },
  322. _createTr : function( itemData, isNew, unid, sourceData ){
  323. this.fireEvent("queryCreateTr", [this]);
  324. var d;
  325. if( isNew ){
  326. this.fireEvent("newData", [this, function( data ){
  327. d = data;
  328. }.bind(this) ]);
  329. if( d ){
  330. //itemData, isNew, unid, sourceData
  331. itemData = Object.clone( this.itemTemplate );
  332. for (var it in d ){
  333. if ( itemData[ it ] ){
  334. itemData[ it ].value = d[it];
  335. }
  336. }
  337. isNew = false;
  338. sourceData = d;
  339. }
  340. }
  341. this.trIndex ++;
  342. var trOptions = {
  343. objectId : unid ? unid : "_"+this.trIndex,
  344. isEdited : this.options.isEdited,
  345. id : "_"+this.trIndex,
  346. index : this.trIndex,
  347. indexText : (this.maxIndexText ? this.maxIndexText++ : this.trIndex),
  348. hasSequence : this.options.hasSequence,
  349. hasOperation : this.options.hasOperation,
  350. align : this.options.tdAlign,
  351. isNew : isNew,
  352. className : this.options.tdClass,
  353. tdAttributes : this.options.tdAttributes
  354. };
  355. var template = null;
  356. if( this.trTemplate ){
  357. template = this.trTemplate.clone();
  358. }
  359. var trObj = new MGridTr(this.table, trOptions, itemData, this, template, sourceData );
  360. trObj.load();
  361. this.trObjs[ trOptions.objectId ] = trObj;
  362. this.trList.push( trObj );
  363. if( isNew ){
  364. this.trObjs_new[ trOptions.objectId ] = trObj;
  365. }
  366. this.fireEvent("postCreateTr",[this, trObj]);
  367. return trObj;
  368. },
  369. replaceTr : function( oldTrObjOr_Index, data, isNew, unid, sourceData ){
  370. var oldTrObj;
  371. if( typeof oldTrObjOr_Index == "string" ){ //如果传入的是 _index
  372. oldTrObj = this.trObjs[ oldTrObjOr_Index ];
  373. }else{
  374. oldTrObj = oldTrObjOr_Index;
  375. }
  376. var itemData = Object.clone(this.itemTemplate);
  377. for (var it in data ){
  378. if ( itemData[ it ] ){
  379. itemData[ it ].value = data[it];
  380. }
  381. }
  382. var trIndex = oldTrObj.options.index;
  383. var trOptions = {
  384. objectId : unid ? unid : "_"+trIndex,
  385. isEdited : this.options.isEdited,
  386. id : "_"+trIndex,
  387. index : trIndex,
  388. indexText : trIndex,
  389. hasSequence : this.options.hasSequence,
  390. hasOperation : this.options.hasOperation,
  391. align : this.options.tdAlign,
  392. isNew : isNew,
  393. className : this.options.tdClass,
  394. tdAttributes : this.options.tdAttributes
  395. };
  396. var template = null;
  397. if( this.trTemplate ){
  398. template = this.trTemplate.clone();
  399. }
  400. var trObj = new MGridTr(this.table, trOptions, itemData, this, template, sourceData );
  401. trObj.load();
  402. //oldTrObj.mElement.replaceWith( trObj.mElement );
  403. trObj.mElement.inject( oldTrObj.mElement, "before" );
  404. var idx = this.trList.indexOf( oldTrObj );
  405. this.trList[idx] = trObj;
  406. this.trObjs[ trOptions.objectId ] = trObj;
  407. if( oldTrObj.options.isNew ){
  408. this.trObjs_new[ oldTrObj.options.objectId ] = null;
  409. }else{
  410. this.trObjs_removed[ oldTrObj.options.objectId ] = oldTrObj;
  411. }
  412. if( isNew ){
  413. this.trObjs_new[ trOptions.objectId ] = trObj;
  414. }
  415. oldTrObj.mElement.destroy();
  416. //for( var it in this.itemTemplate ){
  417. // this.itemTemplate[it].value = "";
  418. //}
  419. return trObj;
  420. },
  421. createRemoveButton : function( trObj, container ){
  422. var button = new Element("div", { title : MWF.xApplication.Template.LP.MGrid.delete }).inject( container );
  423. if( this.options.lp.remove )button.set("text",this.options.lp.remove );
  424. if( this.css.actionRemove )button.setStyles( this.css.actionRemove );
  425. button.addEvents( {
  426. "click": function( e ){ this.removeTr( e, e.target, trObj ); }.bind(this)
  427. });
  428. if( this.css.actionRemove && this.css.actionRemove_over){
  429. button.addEvents({
  430. "mouseover" : function( e ){ this.node.setStyles( this.obj.css.actionRemove_over ) }.bind({ node : button, obj : this }),
  431. "mouseout" : function( e ){ this.node.setStyles( this.obj.css.actionRemove ) }.bind({ node : button, obj : this })
  432. })
  433. }
  434. return button
  435. },
  436. removeTr : function(e, el, trObj ){
  437. this.fireEvent("queryRemoveTr", [e, el, trObj]);
  438. var s = trObj.options;
  439. var id = s.objectId;
  440. if( ! s.isNew ){
  441. this.trObjs_removed[ id ] = this.trObjs[ id ];
  442. }
  443. this.trList.erase( trObj );
  444. this.trObjs[ id ] = null;
  445. //delete this.trObjs[ id ];
  446. if( this.trObjs_new[ id ] ){
  447. this.trObjs_new[ id ]= null;
  448. //delete this.trObjs_new[ id ];
  449. }
  450. //this.fireEvent("queryRemoveTr", [e, el.getParent("tr")]);
  451. trObj.destroy();
  452. this.adjustSequenceText();
  453. this.fireEvent("postRemoveTr", [e, el, trObj ]);
  454. },
  455. replaceText : function( value, selectValue, selectText ){
  456. var vals = typeOf( value ) == "array" ? value : value.split( this.valSeparator );
  457. var selectValues = typeOf( selectValue ) == "array" ? selectValue : selectValue.split( this.valSeparator );
  458. var selectTexts = typeOf( selectText ) == "array" ? selectText : selectText.split(this.valSeparator);
  459. for( var i=0 ;i<vals.length; i++ ){
  460. for( var j= 0; j<selectValues.length; j++){
  461. if( vals[i] == selectValues[j] ){
  462. vals[i] = selectTexts[j]
  463. }
  464. }
  465. }
  466. return vals;
  467. },
  468. createTr_textOnly : function( itemData ){
  469. var self = this;
  470. this.trIndex ++;
  471. if( this.trTemplate ){
  472. return this.createTr_textOnly_byTemplate( itemData );
  473. }else{
  474. return this.createTr_textOnly_noTemplate( itemData );
  475. }
  476. },
  477. createTr_textOnly_byTemplate : function( itemData ){
  478. var tr = this.trTemplate.clone();
  479. tr.set( "data-id", "_"+ this.trIndex );
  480. var labelContainers = tr.getElements("[lable]");
  481. var itemContainers = tr.getElements("[item]");
  482. var sequenceContainers = tr.getElements( "[sequence]" );
  483. labelContainers.each(function( el ) {
  484. var obj = itemData[el.get("lable")];
  485. if (!obj)return;
  486. if(obj.text)el.set("text", obj.text);
  487. });
  488. itemContainers.each(function( el ) {
  489. var obj = itemData[el.get("item")];
  490. if (!obj)return;
  491. var val = obj.value ? obj.value : "";
  492. var valtype = typeOf( val );
  493. if( valtype === "string" )val = val.replace(/\n/g,"<br/>");
  494. if( obj.selectValue && obj.selectText ){
  495. var vals = this.replaceText( val, obj.selectValue, obj.selectText );
  496. val = vals.join(",");
  497. }else{
  498. if( valtype === "string" )val = val.replace( this.valSeparator,"," );
  499. }
  500. el.set("html", val );
  501. }.bind(this));
  502. sequenceContainers.set("text", this.trIndex );
  503. tr.inject( this.table );
  504. return tr;
  505. },
  506. createTr_textOnly_noTemplate : function( itemData ){
  507. var tr = new Element("tr" , { "data-id" : "_"+this.trIndex });
  508. if( this.options.hasSequence ){
  509. var td = new Element("td", { align : "center", text : this.trIndex }).inject( tr );
  510. if( this.options.sequenceClass && this.css[this.options.sequenceClass] )td.setStyles(this.css[this.options.sequenceClass]);
  511. }
  512. var attr = {};
  513. if( this.options.tdAlign )attr.align = this.options.tdAlign;
  514. var idx = 1;
  515. for (var it in itemData ){
  516. var tdAttributes = {};
  517. if(this.options.tdAttributes && this.options.tdAttributes["_"+idx] ){
  518. tdAttributes = this.options.tdAttributes["_"+idx];
  519. }
  520. var obj = itemData[it];
  521. var val = obj.value || "";
  522. var valtype = typeOf( val );
  523. if( valtype === "string" )val = val.replace(/\n/g,"<br/>");
  524. if( obj.selectValue && obj.selectText ){
  525. var vals = this.replaceText( val, obj.selectValue, obj.selectText );
  526. val = vals.join(",");
  527. }else{
  528. if( valtype === "string" )val = val.replace( this.valSeparator,"," )
  529. }
  530. var td = new Element("td", tdAttributes).inject( tr );
  531. td.set( "text", val );
  532. if( this.options.tdAlign )td.set( "align" , this.options.tdAlign);
  533. if( this.options.tdClass && this.css[this.options.tdClass] )td.setStyles(this.css[this.options.tdClass]);
  534. idx ++;
  535. }
  536. tr.inject( this.table );
  537. return tr;
  538. },
  539. getResult : function( verify, separator, isAlert, onlyModified, keepAllData ){
  540. var result = [];
  541. var trObjs = this.trObjs;
  542. var flag = true;
  543. for( var tr in trObjs ){
  544. if( tr && trObjs[tr] ){
  545. var data = trObjs[tr].getResult( verify, separator, isAlert, onlyModified , keepAllData );
  546. if( !data ){
  547. if( this.options.verifyType == "batch" ){
  548. flag= false;
  549. }else{
  550. return false;
  551. }
  552. }else{
  553. result.push( data )
  554. }
  555. }
  556. }
  557. if( flag ){
  558. return result;
  559. }else{
  560. return false ;
  561. }
  562. },
  563. enableItem : function( itemName ){
  564. for( var tr in this.trObjs ){
  565. if( tr && this.trObjs[tr] ){
  566. this.trObjs[tr].enableItem( itemName );
  567. }
  568. }
  569. },
  570. disableItem : function( itemName ){
  571. for( var tr in this.trObjs ){
  572. if( tr && this.trObjs[tr] ){
  573. this.trObjs[tr].disableItem( itemName );
  574. }
  575. }
  576. },
  577. adjustSequenceText : function(){
  578. var array = [];
  579. for( var tr in this.trObjs ){
  580. if( tr && this.trObjs[tr] ){
  581. array.push( this.trObjs[tr] )
  582. }
  583. }
  584. array.sort( function( a, b ){
  585. return a.options.index - b.options.index
  586. });
  587. array.each( function( o, index ){
  588. o.setSequenceText(index + 1 );
  589. this.maxIndexText = index + 2;
  590. }.bind(this))
  591. },
  592. string2DOM: function( str, container, callback ){
  593. var wrapper = str.test('^<the|^<tf|^<tb|^<colg|^<ca') && ['<table>', '</table>', 1] ||
  594. str.test('^<col') && ['<table><colgroup>', '</colgroup><tbody></tbody></table>',2] ||
  595. str.test('^<tr') && ['<table><tbody>', '</tbody></table>', 2] ||
  596. str.test('^<th|^<td') && ['<table><tbody><tr>', '</tr></tbody></table>', 3] ||
  597. str.test('^<li') && ['<ul>', '</ul>', 1] ||
  598. str.test('^<dt|^<dd') && ['<dl>', '</dl>', 1] ||
  599. str.test('^<le') && ['<fieldset>', '</fieldset>', 1] ||
  600. str.test('^<opt') && ['<select multiple="multiple">', '</select>', 1] ||
  601. ['', '', 0];
  602. if( container ){
  603. var el = new Element('div', {html: wrapper[0] + str + wrapper[1]}).getChildren();
  604. while(wrapper[2]--) el = el[0].getChildren();
  605. el.inject( container );
  606. if( callback )callback( container );
  607. return el;
  608. }else{
  609. var div = new Element('div', {html: wrapper[0] + str + wrapper[1]});
  610. div.setStyle("display","none").inject( $(document.body) );
  611. if( callback )callback( div );
  612. var el = div.getChildren();
  613. while(wrapper[2]--) el = el[0].getChildren();
  614. div.dispose();
  615. return el;
  616. }
  617. },
  618. formatStyles: function( container ){
  619. container.getElements("[styles]").each(function(el){
  620. var styles = el.get("styles");
  621. if( styles && this.css[styles] ){
  622. el.setStyles( this.css[styles] )
  623. }
  624. }.bind(this));
  625. container.getElements("[class]").each(function(el){
  626. var className = el.get("class");
  627. if( className && this.css[className] ){
  628. el.setStyles( this.css[className] )
  629. }
  630. }.bind(this))
  631. },
  632. });
  633. var MGridTr = new Class({
  634. Implements: [Options, Events],
  635. options: {
  636. objectId : "",
  637. isEdited : true,
  638. id : "",
  639. index : 0,
  640. indexText : "0",
  641. hasSequence : true,
  642. hasOperation : true,
  643. align : "left",
  644. isNew : true,
  645. className : "",
  646. tdAttributes : null
  647. },
  648. initialize: function (container,options,itemData, parent, template, sourceData) {
  649. this.setOptions(options);
  650. this.container = container;
  651. this.mElement = null;
  652. this.items = {};
  653. this.itemData = itemData;
  654. this.parent = parent;
  655. this.template = template;
  656. this.sourceData = sourceData;
  657. this.css = this.parent.css || {};
  658. this.app = this.parent.app;
  659. },
  660. load:function(){
  661. //if( this.options.isEdited ){
  662. // this.create_Edit();
  663. //}else{
  664. // this.create_Read();
  665. //}
  666. this.create();
  667. },
  668. reload : function( data ){
  669. if( !data )data = this.getResult(false, null, false, false, true );
  670. this.parent.replaceTr( this, data, this.options.isNew, this.options.objectId, this.sourceData )
  671. },
  672. //create_Read : function(){
  673. // var tr = this.mElement = new Element("tr", { "data-id" : this.options.id }).inject(this.container);
  674. //
  675. // var attr = {};
  676. // if( this.options.align )attr.align = this.options.align;
  677. //
  678. // var styles = {};
  679. // if( this.options.className && this.css[this.options.className] )styles = this.css[this.options.className];
  680. //
  681. // if( this.options.hasSequence ){
  682. // var td = this.sequenceTd = new Element("td", { align : "center", text : ( this.options.indexText || this.options.index) }).inject( tr );
  683. // if( this.parent.options.sequenceClass && this.css[this.parent.options.sequenceClass] )td.setStyles(this.css[this.parent.options.sequenceClass]);
  684. // }
  685. //
  686. // var idx = 1;
  687. // for (var it in this.itemData ){
  688. // var tdAttr = this.options.tdAttributes && this.options.tdAttributes["_"+idx] ? this.options.tdAttributes["_"+idx] : {};
  689. // var td = new Element("td", { "text" : this.itemData[it].value }).inject( tr );
  690. // td.set( attr );
  691. // td.set( tdAttr );
  692. // td.setStyle( styles );
  693. // idx++;
  694. // }
  695. //},
  696. create : function(e, el){
  697. if( this.template ){
  698. this.create_byTemplate( e, el );
  699. }else{
  700. this.create_noTemplate( e, el );
  701. }
  702. },
  703. setSequenceText : function( text ){
  704. if(this.sequenceTd)this.sequenceTd.set("text",text);
  705. },
  706. create_byTemplate : function(){
  707. this.mElement = this.template;
  708. this.mElement.set("data-id", this.options.id );
  709. if( this.options.hasSequence ){
  710. this.sequenceTd = this.mElement.getElement( "[sequence]" );
  711. if(this.sequenceTd)this.sequenceTd.set( "text" , ( this.options.indexText || this.options.index) );
  712. }
  713. this.mElement.getElements("[lable]").each(function( el ) {
  714. var itData = this.itemData[el.get("lable")];
  715. if (!itData)return;
  716. if(itData.text)el.set("text", itData.text);
  717. }.bind(this));
  718. this.mElement.getElements("[item]").each(function( el ) {
  719. var itData = this.itemData[el.get("item")];
  720. if (!itData)return;
  721. this.createItem( el, itData );
  722. }.bind(this));
  723. if( this.options.hasOperation && this.options.isEdited ){
  724. if( this.parent.options.minTrCount && this.options.index > this.parent.options.minTrCount ){
  725. var removeContainer = this.mElement.getElement("[button_remove]");
  726. this.parent.createRemoveButton( this, removeContainer );
  727. }
  728. }
  729. this.mElement.setStyle("display","");
  730. this.mElement.inject( this.container );
  731. },
  732. create_noTemplate : function(){
  733. this.mElement = new Element("tr", { "data-id" : this.options.id });
  734. var attr = {};
  735. if( this.options.align )attr.align = this.options.align;
  736. var styles = {};
  737. if( this.options.className && this.css[this.options.className] )styles = this.css[this.options.className];
  738. if( this.options.hasSequence ){
  739. var td = this.sequenceTd = new Element("td", { align : "center", text : ( this.options.indexText || this.options.index) }).inject( this.mElement );
  740. if( this.parent.options.sequenceClass && this.css[this.parent.options.sequenceClass] )td.setStyles(this.css[this.parent.options.sequenceClass]);
  741. }
  742. var idx = 1;
  743. for (var it in this.itemData){
  744. var tdAttr = this.options.tdAttributes && this.options.tdAttributes["_"+idx] ? this.options.tdAttributes["_"+idx] : {};
  745. var td = new Element("td").inject( this.mElement );
  746. td.set(attr);
  747. td.set( tdAttr );
  748. td.setStyles( styles );
  749. var itData = this.itemData[it];
  750. this.createItem( td, itData );
  751. idx++;
  752. }
  753. if( this.options.hasOperation && this.options.isEdited ){
  754. if( this.parent.options.minTrCount && this.options.index > this.parent.options.minTrCount ) {
  755. var t = new Element("td", {align: "center", style: {width: "30px"}}).inject(this.mElement);
  756. var className = this.parent.options.removeActionTdClass;
  757. if( className && this.css[className] )t.setStyles(this.css[className]);
  758. this.parent.createRemoveButton(this, t);
  759. }else{
  760. var t = new Element("td").inject(this.mElement);
  761. var className = this.parent.options.removeActionTdClass;
  762. if( className && this.css[className] )t.setStyles(this.css[className]);
  763. }
  764. }
  765. this.mElement.inject( this.container );
  766. },
  767. createItem : function( container, itData ) {
  768. //if( itData.disable )return;
  769. itData.isEdited = this.options.isEdited;
  770. itData.objectId = itData.name;
  771. var item = new MDomItem(container, itData, this, this.app, this.css);
  772. if (this.options.isEdited){
  773. if (this.parent.options.verifyType == "batchSingle") {
  774. item.options.warningType = "single";
  775. } else {
  776. item.options.warningType = this.parent.options.verifyType;
  777. }
  778. }
  779. item.options.name = itData.name + "_" + this.options.index;
  780. item.index = this.options.index;
  781. item.parent = this;
  782. item.load();
  783. this.items[itData.objectId] = item;
  784. },
  785. //remove : function(){
  786. // this.mElement.destroy();
  787. //},
  788. destroy : function(){
  789. Object.each(this.items, function(item){
  790. item.destroy();
  791. }.bind(this));
  792. this.mElement.destroy();
  793. MWF.release(this);
  794. },
  795. enableItem: function( itemName ){
  796. if( itemName && this.items[itemName] ){
  797. var item = this.items[itemName];
  798. if( item.options.disable ){
  799. item.enable();
  800. }
  801. }
  802. },
  803. disableItem: function( itemName ){
  804. if( itemName && this.items[itemName] ){
  805. var item = this.items[itemName];
  806. if( !item.options.disable ){
  807. item.disable();
  808. }
  809. }
  810. },
  811. getPrevSilbing : function(){
  812. var idx = this.parent.trList.indexOf( this );
  813. if( idx > 0 ){
  814. return this.parent.trList[ idx -1 ];
  815. }else{
  816. return null;
  817. }
  818. },
  819. getNextSilbing : function(){
  820. var idx = this.parent.trList.indexOf( this );
  821. if( idx!=-1 && idx < this.parent.trList.length - 1 ){
  822. return this.parent.trList[ idx + 1 ];
  823. }else{
  824. return null;
  825. }
  826. },
  827. verify : function(isShowWarming) {
  828. var flag = true;
  829. for (var it in this.items ) {
  830. if (!this.items[it].verify(isShowWarming)) {
  831. if (this.parent.options.verifyType == "batch" || this.parent.options.verifyType == "batchSingle") {
  832. flag = false;
  833. } else {
  834. return false;
  835. }
  836. }
  837. }
  838. return flag;
  839. },
  840. getItemsKeyValue : function(separator , onlyModified ) {
  841. //separator 多值合并分隔符
  842. var key_value = {};
  843. for (var it in this.items ) {
  844. var item = this.items[it];
  845. var value = onlyModified ? item.getModifiedValue() : item.getValue();
  846. if( value != null ){
  847. if (typeOf(value) === "array") {
  848. key_value[item.options.objectId] = (typeOf(separator) == "string" ? value.join(separator) : value );
  849. } else {
  850. key_value[item.options.objectId] = value;
  851. }
  852. }
  853. }
  854. return key_value;
  855. },
  856. getResult : function(verify, separator, isShowWarming, onlyModified, keepAllData ) {
  857. if ( !verify || this.verify(isShowWarming)) {
  858. if( keepAllData && this.sourceData ){
  859. var result = this.sourceData;
  860. var map = this.getItemsKeyValue(separator, onlyModified);
  861. for( var key in map ){
  862. result[key] = map[key];
  863. }
  864. return result;
  865. }else{
  866. return this.getItemsKeyValue(separator, onlyModified);
  867. }
  868. } else {
  869. return false;
  870. }
  871. }
  872. });