MForm.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. 外部方法
  3. getResult(verify, separator, isAlert, onlyModified, keepAllData )
  4. 返回:如果不校验或者通过了校验返回表单上的数据,如果不能通过校验返回null
  5. 参数:
  6. verify 是否校验,
  7. separator 多值域的分割符,
  8. isAlert 如果verify为true则是否提醒用户校验未通过,
  9. onlyModified只获取修改过的结果,
  10. keepAllData获取所有MDOMItem对象的值并保留未生成对象的data
  11. getItem( name )
  12. 根据name返回MDOMItem对象
  13. verify(isAlert)
  14. 校验用户是否已经填写了必填对象
  15. */
  16. MWF.xDesktop.requireApp("Template", "MDomItem", null, false);
  17. var MForm = new Class({
  18. Extends: MWF.widget.Common,
  19. Implements: [Options, Events],
  20. options : {
  21. style : "default",
  22. isNew : false,
  23. isEdited : false,
  24. emptyItemContainer : true,
  25. showNotEmptyFlag : false,
  26. //batch,所有item的错误都校验,每个item所有错误一起校验,错误显示在Item的字段后或指定区域;
  27. //batchSingle, 所有item的错误都校验,每个item只校验一个错误,错误显示在Item的字段后或指定区域;
  28. //alert,验证时遇到一个不成功的Item用app.notice()弹出消息后中断,
  29. //single,验证时遇到一个不成功的Item在字段后面或指定区域显示错误信息后中断
  30. verifyType : "alert",
  31. itemTemplateUrl : "",
  32. containerHtml : null,
  33. itemTemplate : null,
  34. hasColon : false
  35. },
  36. initialize: function (container, data, options , app, css ) {
  37. this.setOptions(options);
  38. this.path = "../x_component_Template/$MForm/";
  39. if( this.options.style ) {
  40. this.cssPath = "../x_component_Template/$MForm/" + this.options.style + "/css.wcss";
  41. this._loadCss();
  42. }else{
  43. this.css = {};
  44. }
  45. if( css ){
  46. this.css = Object.merge( this.css, css )
  47. }
  48. this.app = app;
  49. this.container = $(container);
  50. this.data = typeOf( data ) === "object" ? [data] : data;
  51. this.isSourceDataEmpty = false;
  52. if( !this.data || this.data == "" ){
  53. this.isSourceDataEmpty = true;
  54. this.data = [{}];
  55. }
  56. this.itemTemplateUrl = this.options.itemTemplateUrl;
  57. this.itemTemplate = this.options.itemTemplate;
  58. this.items = null;
  59. this.labelContainers = null;
  60. this.itemContainers = null;
  61. this.valSeparator = /,|;|\^\^|\|/g; //如果是多值对象,作为用户选择的多个值的分隔符
  62. },
  63. load: function () {
  64. this.fireEvent("queryLoad");
  65. this.loadTemplate( function(){
  66. //如果itemTemplate没有name赋值Key
  67. for( var it in this.itemTemplate ){
  68. if( !this.itemTemplate[it]["name"] ){
  69. this.itemTemplate[it]["name"] = it;
  70. }
  71. this.itemTemplate[it]["key"] = it;
  72. }
  73. //如果itemTemplate没有name和Key不一致,那么根据name赋值itemTemplate
  74. var json = {};
  75. for( var it in this.itemTemplate ){
  76. if( it != this.itemTemplate[it]["name"] ){
  77. json[ this.itemTemplate[it]["name"] ] = this.itemTemplate[it];
  78. }
  79. }
  80. for( var it in json ){
  81. this.itemTemplate[it] = json[it];
  82. }
  83. this.items = {};
  84. this.itemsByKey = {};
  85. if( this.options.containerHtml ){
  86. this.container.set("html",this.options.containerHtml);
  87. }
  88. this.labelContainers = this.container.getElements("[lable]");
  89. this.itemContainers = this.container.getElements("[item]");
  90. this.formatStyles();
  91. (this.options.isEdited || this.options.isNew) ? this.loadEdit() : this.loadRead();
  92. this.fireEvent("postLoad");
  93. }.bind(this) )
  94. },
  95. formatStyles: function(){
  96. this.container.getElements("[styles]").each(function(el){
  97. var styles = el.get("styles");
  98. if( styles && this.css[styles] ){
  99. el.setStyles( this.css[styles] )
  100. }
  101. }.bind(this));
  102. this.container.getElements("[class]").each(function(el){
  103. var className = el.get("class");
  104. if( className && this.css[className] ){
  105. el.setStyles( this.css[className] )
  106. }
  107. }.bind(this))
  108. },
  109. loadTemplate : function( callback ){
  110. if ( !this.itemTemplate && this.itemTemplateUrl) {
  111. MWF.getJSON(this.itemTemplateUrl, function(json){
  112. this.itemTemplate = json;
  113. if( callback )callback();
  114. }.bind(this));
  115. }else{
  116. if( callback )callback();
  117. }
  118. },
  119. changeMode: function(keepData){
  120. debugger;
  121. // this.itemContainers.each(function(node){
  122. // node.empty();
  123. // });
  124. // this.items = {};
  125. // this.itemsByKey = {};
  126. //
  127. // this.isEdited ? this.loadRead() : this.loadEdit();
  128. for(var key in this.items){
  129. this.isEdited ? this.items[key].readMode(keepData) : this.items[key].editMode(keepData)
  130. }
  131. this.isEdited = !this.isEdited;
  132. },
  133. loadEdit : function() {
  134. if (this.options.isNew) {
  135. this.formatEdit(this.itemTemplate, true);
  136. } else if (!this.isSourceDataEmpty) {
  137. if (typeOf(this.data) != "array") {
  138. this.data = [this.data]
  139. }
  140. for (var i = 0; i < this.data.length; i++) {
  141. var d = this.data[i];
  142. var items = this.itemTemplate;
  143. for (var it in d ) {
  144. if (items[it]) {
  145. items[it].value = d[it];
  146. }
  147. }
  148. this.formatEdit(items, false);
  149. }
  150. for (var it in this.itemTemplate ) {
  151. this.itemTemplate[it].value = "";
  152. }
  153. } else {
  154. this.formatEdit(this.itemTemplate, true);
  155. }
  156. },
  157. loadRead : function() {
  158. if (this.options.isNew) {
  159. this.formatRead(this.itemTemplate);
  160. } else if (!this.isSourceDataEmpty) {
  161. if ( typeOf(this.data) != "array") {
  162. this.data = [this.data]
  163. }
  164. for (var i = 0; i < this.data.length; i++) {
  165. var d = this.data[i];
  166. var items = this.itemTemplate;
  167. for (var it in d ) {
  168. if (items[it]) {
  169. items[it].value = d[it];
  170. }
  171. }
  172. this.formatRead(items);
  173. }
  174. for (var it in this.itemTemplate ) {
  175. this.itemTemplate[it].value = "";
  176. }
  177. }
  178. },
  179. //formatRead : function(itemData) {
  180. // var self = this;
  181. //
  182. // this.labelContainers.each(function( el ) {
  183. // var obj = itemData[el.get("lable")];
  184. // if (!obj)
  185. // return;
  186. // el.set("text",obj.text);
  187. // });
  188. //
  189. // this.itemContainers.each(function( el ) {
  190. // var obj = itemData[el.get("item")];
  191. // if (!obj)
  192. // return;
  193. //
  194. // if( obj.style ){
  195. // el.setStyles( obj.style )
  196. // }
  197. //
  198. // if( typeOf( obj.value ) == "function" ) {
  199. // var value = obj.value();
  200. // }else if( typeOf( obj.value ) == "boolean" ){
  201. // var value = obj.value.toString();
  202. // }else{
  203. // var value = (obj.value ? obj.value : "").toString();
  204. // }
  205. // if( obj.type == "hidden" ) {
  206. // el.hide();
  207. // el.set("html",value.replace( this.valSeparator, ",").replace(/\n/g,"<br/>") )
  208. // }else if( obj.type == "radio" || obj.type == "select" || obj.type == "checkbox" || obj.type == "multiselect" ) {
  209. // var values = value.split( this.valSeparator );
  210. // if( obj.selectText && obj.selectValue ){
  211. // var selectValues = this._getSelectOpt( obj.selectValue );
  212. // var selectTexts = this._getSelectOpt( obj.selectText );
  213. // var result = [];
  214. // for( i=0;i<selectValues.length;i++){
  215. // if( values.contains( selectValues[i] ) ){
  216. // result.push( selectTexts[i] )
  217. // }
  218. // }
  219. // el.set("html",result.join(","));
  220. // }else{
  221. // el.set("html",value.replace( this.valSeparator, ",").replace(/\n/g,"<br/>") )
  222. // }
  223. // }else if( obj.type == "rtf"){
  224. // el.set("html",value )
  225. // } else {
  226. // el.set("html",value.replace( this.valSeparator, ",").replace(/\n/g,"<br/>") )
  227. // }
  228. // }.bind(this))
  229. //},
  230. formatRead : function(itemData) {
  231. var self = this;
  232. this.isEdited = false;
  233. this.labelContainers.each(function( el ) {
  234. var obj = itemData[el.get("lable")];
  235. if (!obj)
  236. return;
  237. el.set("text",obj.text + (self.options.hasColon ? ":" : "") );
  238. });
  239. this.itemContainers.each(function( el ) {
  240. var obj = itemData[el.get("item")];
  241. if (!obj)
  242. return;
  243. if (self.options.emptyItemContainer) {
  244. el.set("html","");
  245. }
  246. obj.isEdited = false;
  247. self.loadItem(obj, el);
  248. })
  249. },
  250. _getSelectOpt: function( option ){
  251. var opt = option;
  252. if( typeOf( opt ) == "function" ){
  253. opt = opt.call();
  254. }
  255. return typeOf( opt ) == "array" ? opt : opt.split( this.valSeparator );
  256. },
  257. formatEdit : function(itemData, isNew, unid) {
  258. var self = this;
  259. this.isEdited = true;
  260. this.labelContainers.each(function( el ) {
  261. var obj = itemData[el.get("lable")];
  262. if (!obj)
  263. return;
  264. var text = obj.text + (self.options.hasColon ? ":" : "");
  265. if (self.options.showNotEmptyFlag && obj.notEmpty) {
  266. el.set("html", text + "<span style='color:red;'>*</span>")
  267. } else {
  268. el.set("text",text);
  269. }
  270. });
  271. this.itemContainers.each(function( el ) {
  272. var obj = itemData[el.get("item")];
  273. if (!obj)
  274. return;
  275. if (self.options.emptyItemContainer) {
  276. el.set("html","");
  277. }
  278. self.loadItem(obj, el);
  279. })
  280. },
  281. loadItem : function(template, container) {
  282. //if( template.disable )return;
  283. template.objectId = template.name;
  284. var item = new MDomItem(container, template, this, this.app, this.css );
  285. if( this.options.verifyType == "batchSingle" ){
  286. item.options.warningType = "single";
  287. }else{
  288. item.options.warningType = this.options.verifyType;
  289. }
  290. //item.parent = this;
  291. item.load();
  292. this.items[template.objectId] = item;
  293. this.itemsByKey[template.key] = item;
  294. },
  295. enableItem: function( itemName ){
  296. if( itemName && this.items[itemName] ){
  297. var item = this.items[itemName];
  298. if( item.options.disable ){
  299. item.enable();
  300. }
  301. }
  302. },
  303. disableItem: function( itemName ){
  304. if( itemName && this.items[itemName] ){
  305. var item = this.items[itemName];
  306. if( !item.options.disable ){
  307. item.disable();
  308. }
  309. }
  310. },
  311. verify : function(isShowWarming) {
  312. var flag = true;
  313. for (var it in this.items ) {
  314. if (!this.items[it].verify(isShowWarming)) {
  315. if (this.options.verifyType == "batch" || this.options.verifyType == "batchSingle") {
  316. flag = false;
  317. } else {
  318. return false;
  319. }
  320. }
  321. }
  322. return flag;
  323. },
  324. getItemsKeyValue : function(separator , onlyModified ) {
  325. //separator 多值合并分隔符
  326. var key_value = {};
  327. for (var it in this.items ) {
  328. var item = this.items[it];
  329. var value = onlyModified ? item.getModifiedValue() : item.getValue();
  330. if( value != null ){
  331. if (typeOf(value) === "array") {
  332. key_value[item.options.objectId] = ( typeOf(separator) == "string" ? value.join(separator) : value );
  333. } else {
  334. key_value[item.options.objectId] = value;
  335. }
  336. }
  337. }
  338. return key_value;
  339. },
  340. getResult : function(verify, separator, isShowWarming, onlyModified, keepAllData ) {
  341. if ( !verify || this.verify(isShowWarming)) {
  342. if( keepAllData ){
  343. var result = this.data[0];
  344. var keyValue = this.getItemsKeyValue(separator, onlyModified);
  345. for( var key in keyValue ){
  346. result[ key ] = keyValue[ key ];
  347. }
  348. return result;
  349. }else{
  350. return this.getItemsKeyValue(separator, onlyModified);
  351. }
  352. } else {
  353. return false;
  354. }
  355. },
  356. getItem : function( name ){
  357. return this.items[name] || this.itemsByKey[name];
  358. },
  359. clearWarning: function( type ){
  360. for (var it in this.items ) {
  361. var item = this.items[it];
  362. if( !type ){
  363. item.clearWarning( "empty" );
  364. item.clearWarning( "invalid" );
  365. }else{
  366. item.clearWarning( type )
  367. }
  368. }
  369. },
  370. reset: function(){
  371. for (var it in this.items ) {
  372. var item = this.items[it];
  373. item.reset();
  374. }
  375. },
  376. destroy : function(){
  377. Object.each(this.items, function(item){
  378. item.destroy();
  379. }.bind(this));
  380. this.container.empty();
  381. MWF.release(this);
  382. }
  383. });