Subform.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Subform = MWF.APPSubform = new Class({
  3. Extends: MWF.APP$Module,
  4. _loadUserInterface: function(){
  5. this.node.empty();
  6. this.getSubform(function(){
  7. this.loadSubform();
  8. }.bind(this));
  9. },
  10. reload: function(){
  11. this.node.empty();
  12. this.getSubform(function(){
  13. this.loadSubform();
  14. }.bind(this));
  15. },
  16. loadCss: function(){
  17. if (this.subformData.json.css && this.subformData.json.css.code){
  18. var cssText = this.form.parseCSS(this.subformData.json.css.code);
  19. var rex = new RegExp("(.+)(?=\\{)", "g");
  20. var match;
  21. var id = this.form.json.id.replace(/\-/g, "");
  22. while ((match = rex.exec(cssText)) !== null) {
  23. var prefix = ".css" + id + " ";
  24. var rule = prefix + match[0];
  25. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  26. rex.lastIndex = rex.lastIndex + prefix.length;
  27. }
  28. var styleNode = $("style"+this.form.json.id);
  29. if (!styleNode){
  30. var styleNode = document.createElement("style");
  31. styleNode.setAttribute("type", "text/css");
  32. styleNode.id="style"+this.form.json.id;
  33. styleNode.inject(this.form.container, "before");
  34. }
  35. if(styleNode.styleSheet){
  36. var setFunc = function(){
  37. styleNode.styleSheet.cssText += cssText;
  38. };
  39. if(styleNode.styleSheet.disabled){
  40. setTimeout(setFunc, 10);
  41. }else{
  42. setFunc();
  43. }
  44. }else{
  45. var cssTextNode = document.createTextNode(cssText);
  46. styleNode.appendChild(cssTextNode);
  47. }
  48. }
  49. },
  50. checkSubformNested : function( id ){
  51. if( this.parentformIdList ){
  52. return !this.parentformIdList.contains( id );
  53. }else{
  54. return ![ this.form.json.id ].contains( id );
  55. }
  56. },
  57. checkSubformUnique : function( id ){
  58. if( !this.form.subformLoaded )return true;
  59. return !this.form.subformLoaded.contains( id );
  60. },
  61. getParentformIdList : function(){
  62. var parentformIdList;
  63. if( this.parentformIdList ){
  64. parentformIdList = Array.clone( this.parentformIdList );
  65. parentformIdList.push( this.subformData.json.id )
  66. }else{
  67. parentformIdList = [ this.form.json.id, this.subformData.json.id ];
  68. }
  69. return parentformIdList;
  70. },
  71. loadSubform: function(){
  72. if (this.subformData){
  73. if( !this.checkSubformNested( this.subformData.json.id ) ){
  74. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  75. }else if( !this.checkSubformUnique( this.subformData.json.id ) ){
  76. this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  77. }else{
  78. //this.form.addEvent("postLoad", function(){
  79. this.loadCss();
  80. this.node.set("html", this.subformData.html);
  81. Object.each(this.subformData.json.moduleList, function (module, key) {
  82. var formKey = key;
  83. if (this.form.json.moduleList[key]) {
  84. formKey = this.json.id + "_" + key;
  85. var moduleNode = this.node.getElement("#" + key);
  86. if (moduleNode) moduleNode.set("id", formKey);
  87. module.id = formKey;
  88. }
  89. this.form.json.moduleList[formKey] = module;
  90. }.bind(this));
  91. var moduleNodes = this.form._getModuleNodes(this.node);
  92. moduleNodes.each(function (node) {
  93. if (node.get("MWFtype") !== "form") {
  94. var _self = this;
  95. var json = this.form._getDomjson(node);
  96. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  97. var module = this.form._loadModule(json, node, function(){
  98. this.parentformIdList = _self.getParentformIdList();
  99. });
  100. this.form.modules.push(module);
  101. }
  102. }.bind(this));
  103. this.form.subformLoaded.push( this.subformData.json.id );
  104. //}.bind(this));
  105. }
  106. }
  107. if( this.form.subformLoadedCount ){
  108. this.form.subformLoadedCount++;
  109. }else{
  110. this.form.subformLoadedCount = 1
  111. }
  112. //console.log( "add subformLoadedCount , this.form.subformLoadedCount = "+ this.form.subformLoadedCount)
  113. this.form.checkSubformLoaded();
  114. },
  115. getSubform: function(callback){
  116. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm": "getFormMobile";
  117. if (this.json.subformType==="script"){
  118. if (this.json.subformScript.code){
  119. var data = this.form.Macro.exec(this.json.subformScript.code, this);
  120. if (data){
  121. var formName, app;
  122. if( typeOf( data ) === "string" ){
  123. formName = data;
  124. }else{
  125. if( data.application )app = data.application;
  126. if( data.subform )formName = data.subform;
  127. }
  128. if( formName ){
  129. if( !app )app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  130. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function(json){
  131. this.getSubformData(json.data);
  132. if (callback) callback();
  133. }.bind(this));
  134. }else{
  135. if (callback) callback();
  136. }
  137. }else{
  138. if (callback) callback();
  139. }
  140. }
  141. }else{
  142. if (this.json.subformSelected && this.json.subformSelected!=="none"){
  143. var app;
  144. if( this.json.subformAppSelected ){
  145. app = this.json.subformAppSelected;
  146. }else{
  147. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  148. }
  149. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.subformSelected, app, function(json){
  150. this.getSubformData(json.data);
  151. if (callback) callback();
  152. }.bind(this));
  153. }else{
  154. if (callback) callback();
  155. }
  156. }
  157. },
  158. getSubformData: function(data){
  159. if( !data || typeOf(data)!=="object" )return;
  160. var subformDataStr = null;
  161. // if ( this.form.json.mode !== "Mobile" && !layout.mobile){
  162. // subformDataStr = data.data;
  163. // }else{
  164. // subformDataStr = data.mobileData;
  165. // }
  166. subformDataStr = data.data;
  167. this.subformData = null;
  168. if (subformDataStr){
  169. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  170. this.subformData.updateTime = data.updateTime;
  171. }
  172. }
  173. });
  174. MWF.xApplication.process.Xform.SubmitForm = MWF.APPSubmitform = new Class({
  175. Extends: MWF.APPSubform,
  176. _loadUserInterface: function(){
  177. // this.node.empty();
  178. this.getSubform(function(){
  179. this.loadSubform();
  180. }.bind(this));
  181. },
  182. reload: function(){
  183. // this.node.empty();
  184. this.getSubform(function(){
  185. this.loadSubform();
  186. }.bind(this));
  187. },
  188. show : function(){
  189. if (this.json.submitScript.code) {
  190. this.form.Macro.exec(this.json.submitScript.code, this);
  191. }
  192. // this.fireSubFormEvent("load");
  193. },
  194. // fireSubFormEvent : function( name ){
  195. // var events = this.subformData.json.events;
  196. // if( events && events[name] && events[name]["code"] ){
  197. // this.form.Macro.exec(events[name]["code"], this);
  198. // }
  199. // },
  200. loadSubform: function(){
  201. if (this.subformData){
  202. if( !this.checkSubformUnique( this.subformData.json.id ) ){ //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  203. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  204. this.isEmbedded = true;
  205. this.fireEvent("afterModulesLoad");
  206. }else if( !this.checkSubformNested( this.subformData.json.id ) ){
  207. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  208. }else{
  209. //this.form.addEvent("postLoad", function(){
  210. // this.fireSubFormEvent("queryLoad");
  211. this.loadCss();
  212. this.node.set("html", this.subformData.html);
  213. Object.each(this.subformData.json.moduleList, function (module, key) {
  214. var formKey = key;
  215. if (this.form.json.moduleList[key]) {
  216. formKey = this.json.id + "_" + key;
  217. var moduleNode = this.node.getElement("#" + key);
  218. if (moduleNode) moduleNode.set("id", formKey);
  219. module.id = formKey;
  220. }
  221. this.form.json.moduleList[formKey] = module;
  222. }.bind(this));
  223. var moduleNodes = this.form._getModuleNodes(this.node);
  224. moduleNodes.each(function (node) {
  225. if (node.get("MWFtype") !== "form") {
  226. var _self = this;
  227. var json = this.form._getDomjson(node);
  228. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  229. var module = this.form._loadModule(json, node, function(){
  230. this.parentformIdList = _self.getParentformIdList();
  231. });
  232. this.form.modules.push(module);
  233. }
  234. }.bind(this));
  235. this.form.subformLoaded.push( this.subformData.json.id );
  236. this.fireEvent("afterModulesLoad");
  237. // this.fireSubFormEvent("postLoad");
  238. // this.fireSubFormEvent("load");
  239. // this.fireSubFormEvent("afterLoad");
  240. }
  241. }
  242. // if( this.form.subformLoadedCount ){
  243. // this.form.subformLoadedCount++;
  244. // }else{
  245. // this.form.subformLoadedCount = 1
  246. // }
  247. // this.form.checkSubformLoaded();
  248. },
  249. getSubform: function(callback){
  250. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm": "getFormMobile";
  251. if (this.json.submitFormType==="script"){
  252. if (this.json.submitFormScript.code){
  253. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  254. if (data){
  255. var formName, app;
  256. if( typeOf( data ) === "string" ){
  257. formName = data;
  258. }else{
  259. if( data.application )app = data.application;
  260. if( data.subform )formName = data.subform;
  261. }
  262. if( formName ){
  263. if( !app )app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  264. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function(json){
  265. this.getSubformData(json.data);
  266. if (callback) callback();
  267. }.bind(this));
  268. }else{
  269. if (callback) callback();
  270. }
  271. }else{
  272. if (callback) callback();
  273. }
  274. }
  275. }else{
  276. if (this.json.submitFormSelected && this.json.submitFormSelected!=="none"){
  277. var app;
  278. if( this.json.submitFormAppSelected ){
  279. app = this.json.submitFormAppSelected;
  280. }else{
  281. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  282. }
  283. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function(json){
  284. this.getSubformData(json.data);
  285. if (callback) callback();
  286. }.bind(this));
  287. }else{
  288. if (callback) callback();
  289. }
  290. }
  291. }
  292. });