Subform.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. this.fireSubFormEvent("load");
  190. },
  191. fireSubFormEvent : function( name ){
  192. var events = this.subformData.json.events;
  193. if( events && events[name] && events[name]["code"] ){
  194. this.form.Macro.exec(events[name]["code"], this);
  195. }
  196. },
  197. loadSubform: function(){
  198. if (this.subformData){
  199. if( !this.checkSubformUnique( this.subformData.json.id ) ){ //如果提交表单已经嵌入到表单中,那么把这个表单弹出来
  200. // this.form.notice(MWF.xApplication.process.Xform.LP.subformUniqueError, "error");
  201. }else if( !this.checkSubformNested( this.subformData.json.id ) ){
  202. this.form.notice(MWF.xApplication.process.Xform.LP.subformNestedError, "error");
  203. }else{
  204. //this.form.addEvent("postLoad", function(){
  205. this.fireSubFormEvent("queryLoad");
  206. this.loadCss();
  207. this.node.set("html", this.subformData.html);
  208. Object.each(this.subformData.json.moduleList, function (module, key) {
  209. var formKey = key;
  210. if (this.form.json.moduleList[key]) {
  211. formKey = this.json.id + "_" + key;
  212. var moduleNode = this.node.getElement("#" + key);
  213. if (moduleNode) moduleNode.set("id", formKey);
  214. module.id = formKey;
  215. }
  216. this.form.json.moduleList[formKey] = module;
  217. }.bind(this));
  218. var moduleNodes = this.form._getModuleNodes(this.node);
  219. moduleNodes.each(function (node) {
  220. if (node.get("MWFtype") !== "form") {
  221. var _self = this;
  222. var json = this.form._getDomjson(node);
  223. //if( json.type === "Subform" || json.moduleName === "subform" )this.form.subformCount++;
  224. var module = this.form._loadModule(json, node, function(){
  225. this.parentformIdList = _self.getParentformIdList();
  226. });
  227. this.form.modules.push(module);
  228. }
  229. }.bind(this));
  230. this.form.subformLoaded.push( this.subformData.json.id );
  231. this.fireSubFormEvent("postLoad");
  232. this.fireSubFormEvent("load");
  233. this.fireSubFormEvent("afterLoad");
  234. }
  235. }
  236. // if( this.form.subformLoadedCount ){
  237. // this.form.subformLoadedCount++;
  238. // }else{
  239. // this.form.subformLoadedCount = 1
  240. // }
  241. // this.form.checkSubformLoaded();
  242. },
  243. getSubform: function(callback){
  244. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getForm": "getFormMobile";
  245. if (this.json.submitFormType==="script"){
  246. if (this.json.submitFormScript.code){
  247. var data = this.form.Macro.exec(this.json.submitFormScript.code, this);
  248. if (data){
  249. var formName, app;
  250. if( typeOf( data ) === "string" ){
  251. formName = data;
  252. }else{
  253. if( data.application )app = data.application;
  254. if( data.subform )formName = data.subform;
  255. }
  256. if( formName ){
  257. if( !app )app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  258. MWF.Actions.get("x_processplatform_assemble_surface")[method](formName, app, function(json){
  259. this.getSubformData(json.data);
  260. if (callback) callback();
  261. }.bind(this));
  262. }else{
  263. if (callback) callback();
  264. }
  265. }else{
  266. if (callback) callback();
  267. }
  268. }
  269. }else{
  270. if (this.json.submitFormSelected && this.json.submitFormSelected!=="none"){
  271. var app;
  272. if( this.json.submitFormAppSelected ){
  273. app = this.json.submitFormAppSelected;
  274. }else{
  275. app = (this.form.businessData.work || this.form.businessData.workCompleted).application;
  276. }
  277. MWF.Actions.get("x_processplatform_assemble_surface")[method](this.json.submitFormSelected, app, function(json){
  278. this.getSubformData(json.data);
  279. if (callback) callback();
  280. }.bind(this));
  281. }else{
  282. if (callback) callback();
  283. }
  284. }
  285. }
  286. });