MGrid.js 34 KB

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