StatementSelector.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xDesktop.requireApp("process.Xform", "ViewSelector", null, false);
  3. /** @class StatementSelector 查询视图选择组件。
  4. * @o2cn 查询视图选择
  5. * @example
  6. * //可以在脚本中获取该组件
  7. * //方法1:
  8. * var statementSelector = this.form.get("fieldId"); //获取组件
  9. * //方法2
  10. * var statementSelector = this.target; //在组件本身的脚本中获取
  11. * @extends MWF.xApplication.process.Xform.ViewSelector
  12. * @o2category FormComponents
  13. * @o2range {Process|CMS}
  14. * @hideconstructor
  15. */
  16. MWF.xApplication.process.Xform.StatementSelector = MWF.APPStatementSelector = new Class({
  17. Implements: [Events],
  18. Extends: MWF.xApplication.process.Xform.ViewSelector,
  19. options: {
  20. /**
  21. * 视图参数(options)已经准备好,还未加载视图时执行。可以通过this.event得到视图参数,并可修改this.event修改视图的加载。
  22. * @since V8.2
  23. * @event MWF.xApplication.process.Xform.StatementSelector#beforeLoadView
  24. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  25. */
  26. /**
  27. * 视图设计已经获取,容器也已经准备好。
  28. * @event MWF.xApplication.process.Xform.StatementSelector#loadViewLayout
  29. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  30. */
  31. /**
  32. * 异步加载查询视图后执行。
  33. * @since V8.2
  34. * @event MWF.xApplication.process.Xform.StatementSelector#loadView
  35. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  36. */
  37. /**
  38. * 选中查询视图中的一条记录后执行。
  39. * @since V8.2
  40. * @event MWF.xApplication.process.Xform.StatementSelector#select
  41. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  42. */
  43. /**
  44. * 取消选中查询视图中的一条记录后执行。
  45. * @since V8.2
  46. * @event MWF.xApplication.process.Xform.StatementSelector#unselect
  47. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  48. */
  49. /**
  50. * 打开查询视图中的一条记录后执行。
  51. * @event MWF.xApplication.process.Xform.StatementSelector#openDocument,可以通过this.event得到打开的文档参数
  52. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  53. */
  54. "moduleEvents": ["load", "beforeLoadView", "loadViewLayout", "loadView", "queryLoad", "postLoad", "select", "unselect", "openDocument"]
  55. },
  56. doResult: function(data){
  57. if (this.json.result === "script"){
  58. this.selectedData = data;
  59. return (this.json.selectedScript.code) ? this.form.Macro.exec(this.json.selectedScript.code, this) : "";
  60. }else{
  61. Object.each(this.json.selectedSetValues, function(v, k){
  62. var value = "";
  63. data.each(function(d, idx){
  64. // Object.each(d, function(dv, dk){
  65. // if (dk===v) value = (value) ? (value+", "+dv) : dv;
  66. // }.bind(this));
  67. var pathList = v.split(".");
  68. for( var i=0; i<pathList.length; i++ ){
  69. var p = pathList[i];
  70. if( (/(^[1-9]\d*$)/.test(p)) )p = p.toInt();
  71. if( d[ p ] ){
  72. d = d[ p ];
  73. }else{
  74. d = "";
  75. break;
  76. }
  77. }
  78. if( typeOf(d) === "array" || typeOf(d) === "object" ) {
  79. d = JSON.stringify(d);
  80. }
  81. value = (value) ? (value+", "+d) : d;
  82. }.bind(this));
  83. var field = this.form.all[k];
  84. if (field){
  85. field.setData(value);
  86. if (value){
  87. if (field.descriptionNode) field.descriptionNode.setStyle("display", "none");
  88. }else{
  89. if (field.descriptionNode) field.descriptionNode.setStyle("display", "block");
  90. }
  91. }
  92. }.bind(this));
  93. }
  94. },
  95. selectView: function(callback){
  96. var viewData = this.json.queryStatement;
  97. if (viewData){
  98. var filter = null;
  99. if (this.json.filterList && this.json.filterList.length){
  100. filter = [];
  101. this.json.filterList.each(function(entry){
  102. entry.value = this.form.Macro.exec(entry.code.code, this);
  103. //delete entry.code;
  104. filter.push(entry);
  105. }.bind(this));
  106. }
  107. var parameter = null;
  108. if( this.json.parameterList && this.json.parameterList.length ){
  109. parameter = {};
  110. this.json.parameterList.each(function(entry){
  111. parameter[entry.parameter] = this.parseParameter(entry);
  112. }.bind(this));
  113. }
  114. var viewJson = {
  115. "application": viewData.appName,
  116. "statementName": viewData.name,
  117. "statementId": viewData.id,
  118. "isTitle": this.json.isTitle || "yes",
  119. "select": this.json.select || "single",
  120. "titleStyles": this.json.titleStyles,
  121. "itemStyles": this.json.itemStyles,
  122. "isExpand": this.json.isExpand || "no",
  123. "showActionbar" : this.json.actionbar === "show",
  124. "filter": filter,
  125. "parameter": parameter,
  126. "defaultSelectedScript" : this.json.defaultSelectedScript ? this.json.defaultSelectedScript.code : null,
  127. "selectedAbleScript" : this.json.selectedAbleScript ? this.json.selectedAbleScript.code : null
  128. };
  129. this.fireEvent("beforeLoadView", [viewJson]);
  130. var options = {};
  131. // var width = options.width || "850";
  132. // var height = options.height || "700";
  133. var width = this.json.DialogWidth || "850";
  134. var height = this.json.DialogHeight || "700";
  135. if (layout.mobile){
  136. var size = document.body.getSize();
  137. width = size.x;
  138. height = size.y;
  139. options.style = "viewmobile";
  140. }
  141. width = width.toInt();
  142. height = height.toInt();
  143. var size = this.form.app.content.getSize();
  144. var x = (size.x-width)/2;
  145. var y = (size.y-height)/2;
  146. if (x<0) x = 0;
  147. if (y<0) y = 0;
  148. if (layout.mobile){
  149. x = 20;
  150. y = 0;
  151. }
  152. var _self = this;
  153. MWF.require("MWF.xDesktop.Dialog", function(){
  154. var dlg = new MWF.xDesktop.Dialog({
  155. "title": this.json.title || "select view",
  156. "style": options.style || "view",
  157. "top": y,
  158. "left": x-20,
  159. "fromTop":y,
  160. "fromLeft": x-20,
  161. "width": width,
  162. "height": height,
  163. "html": "",
  164. "maskNode": layout.mobile?$(document.body) : this.form.app.content,
  165. "container": layout.mobile?$(document.body) : this.form.app.content,
  166. "buttonList": [
  167. {
  168. "text": MWF.LP.process.button.ok,
  169. "action": function(){
  170. //if (callback) callback(_self.view.selectedItems);
  171. if (callback) callback(_self.view.getData());
  172. this.close();
  173. }
  174. },
  175. {
  176. "text": MWF.LP.process.button.cancel,
  177. "action": function(){this.close();}
  178. }
  179. ],
  180. "onPostShow": function(){
  181. if(layout.mobile){
  182. dlg.node.setStyle("z-index",200);
  183. }
  184. MWF.xDesktop.requireApp("query.Query", "Statement", function(){
  185. this.view = new MWF.xApplication.query.Query.Statement(dlg.content, viewJson, {
  186. "style": "select",
  187. "onLoadLayout": function () {
  188. this.fireEvent("loadViewLayout");
  189. }.bind(this),
  190. "onLoadView": function(){
  191. this.fireEvent("loadView");
  192. }.bind(this),
  193. "onSelect": function(item){
  194. this.fireEvent("select", [item]);
  195. }.bind(this),
  196. "onUnselect": function(item){
  197. this.fireEvent("unselect", [item]);
  198. }.bind(this),
  199. "onOpenDocument": function(options, item){
  200. this.openOptions = {
  201. "options": options,
  202. "item": item
  203. };
  204. this.fireEvent("openDocument", [this.openOptions]);
  205. this.openOptions = null;
  206. }.bind(this)
  207. }, this.form.app, this.form.Macro );
  208. }.bind(this));
  209. }.bind(this)
  210. });
  211. dlg.show();
  212. if (layout.mobile){
  213. var backAction = dlg.node.getElement(".MWF_dialod_Action_back");
  214. var okAction = dlg.node.getElement(".MWF_dialod_Action_ok");
  215. if (backAction) backAction.addEvent("click", function(e){
  216. dlg.close();
  217. }.bind(this));
  218. if (okAction) okAction.addEvent("click", function(e){
  219. //if (callback) callback(this.view.selectedItems);
  220. if (callback) callback(this.view.getData());
  221. dlg.close();
  222. }.bind(this));
  223. }
  224. // MWF.xDesktop.requireApp("process.Xform", "widget.View", function(){
  225. // this.view = new MWF.xApplication.process.Xform.widget.View(dlg.content.getFirst(), viewJson, {"style": "select"});
  226. // }.bind(this));
  227. // MWF.xDesktop.requireApp("query.Query", "Viewer", function(){
  228. // this.view = new MWF.xApplication.query.Query.Viewer(dlg.content, viewJson, {"style": "select"});
  229. // }.bind(this));
  230. }.bind(this));
  231. }
  232. },
  233. parseParameter: function (f) {
  234. var value = f.value;
  235. if( f.valueType === "script" ){
  236. value = this.form.Macro.exec(f.valueScript ? f.valueScript.code : "", this);
  237. }
  238. if (typeOf(value) === "date") {
  239. value = value.format("db");
  240. }
  241. var user = layout.user;
  242. switch (value) {
  243. case "@person":
  244. value = user.distinguishedName;
  245. break;
  246. case "@identityList":
  247. value = user.identityList.map(function (d) {
  248. return d.distinguishedName;
  249. });
  250. break;
  251. case "@unitList":
  252. o2.Actions.load("x_organization_assemble_express").UnitAction.listWithPerson({"personList": [user.distinguishedName]}, function (json) {
  253. value = json.unitList;
  254. }, null, false);
  255. break;
  256. case "@unitAllList":
  257. o2.Actions.load("x_organization_assemble_express").UnitAction.listWithIdentitySupNested({"personList": [user.distinguishedName]}, function (json) {
  258. value = json.unitList;
  259. }, null, false);
  260. break;
  261. case "@year":
  262. value = (new Date().getFullYear()).toString();
  263. break;
  264. case "@season":
  265. var m = new Date().format("%m");
  266. if (["01", "02", "03"].contains(m)) {
  267. value = "1"
  268. } else if (["04", "05", "06"].contains(m)) {
  269. value = "2"
  270. } else if (["07", "08", "09"].contains(m)) {
  271. value = "3"
  272. } else {
  273. value = "4"
  274. }
  275. break;
  276. case "@month":
  277. value = new Date().format("%Y-%m");
  278. break;
  279. case "@time":
  280. value = new Date().format("db");
  281. break;
  282. case "@date":
  283. value = new Date().format("%Y-%m-%d");
  284. break;
  285. default:
  286. }
  287. if (f.formatType === "dateTimeValue" || f.formatType === "datetimeValue") {
  288. value = "{ts '" + value + "'}"
  289. } else if (f.formatType === "dateValue") {
  290. value = "{d '" + value + "'}"
  291. } else if (f.formatType === "timeValue") {
  292. value = "{t '" + value + "'}"
  293. }
  294. return value;
  295. }
  296. });