Statement.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. MWF.xApplication.query = MWF.xApplication.query || {};
  2. MWF.xApplication.query.Query = MWF.xApplication.query.Query || {};
  3. MWF.xDesktop.requireApp("query.Query", "Viewer", null, false);
  4. MWF.xApplication.query.Query.Statement = MWF.QStatement = new Class({
  5. Extends: MWF.QViewer,
  6. options: {
  7. },
  8. initialize: function(container, json, options, app, parentMacro){
  9. //本类有三种事件,
  10. //一种是通过 options 传进来的事件,包括 loadView、openDocument、select
  11. //一种是用户配置的 事件, 在this.options.moduleEvents 中定义的作为类事件
  12. //还有一种也是用户配置的事件,不在this.options.moduleEvents 中定义的作为 this.node 的DOM事件
  13. this.setOptions(options);
  14. this.path = "../x_component_query_Query/$Viewer/";
  15. this.cssPath = "../x_component_query_Query/$Viewer/"+this.options.style+"/css.wcss";
  16. this._loadCss();
  17. this.lp = MWF.xApplication.query.Query.LP;
  18. this.app = app;
  19. this.container = $(container);
  20. this.json = json;
  21. this.parentMacro = parentMacro;
  22. this.originalJson = Object.clone(json);
  23. this.viewJson = null;
  24. this.filterItems = [];
  25. this.searchStatus = "none"; //none, custom, default
  26. this.items = [];
  27. this.selectedItems = [];
  28. this.hideColumns = [];
  29. this.openColumns = [];
  30. this.gridJson = null;
  31. if (this.options.isload){
  32. this.init(function(){
  33. this.load();
  34. }.bind(this));
  35. }
  36. },
  37. init: function(callback){
  38. if (this.json.view){
  39. this.viewJson = JSON.decode(this.json.view);
  40. if (callback) callback();
  41. }else{
  42. this.getView(callback);
  43. }
  44. },
  45. loadMacro: function (callback) {
  46. MWF.require("MWF.xScript.Macro", function () {
  47. this.Macro = new MWF.Macro.ViewContext(this);
  48. if (callback) callback();
  49. }.bind(this));
  50. },
  51. createActionbarNode : function(){
  52. this.actionbarAreaNode.empty();
  53. if( typeOf(this.json.showActionbar) === "boolean" && this.json.showActionbar !== true )return;
  54. if( typeOf( this.viewJson.actionbarHidden ) === "boolean" ){
  55. if( this.viewJson.actionbarHidden === true || !this.viewJson.actionbarList || !this.viewJson.actionbarList.length )return;
  56. this.actionbar = new MWF.xApplication.query.Query.Statement.Actionbar(this.actionbarAreaNode, this.viewJson.actionbarList[0], this, {});
  57. this.actionbar.load();
  58. }
  59. },
  60. _loadPageNode : function(){
  61. this.viewPageAreaNode.empty();
  62. if( !this.paging ){
  63. var json;
  64. if( !this.viewJson.pagingList || !this.viewJson.pagingList.length ){
  65. json = {
  66. "firstPageText": this.lp.firstPage,
  67. "lastPageText": this.lp.lastPage
  68. };
  69. }else{
  70. json = this.viewJson.pagingList[0];
  71. }
  72. this.paging = new MWF.xApplication.query.Query.Statement.Paging(this.viewPageAreaNode, json, this, {});
  73. this.paging.load();
  74. }else{
  75. this.paging.reload();
  76. }
  77. },
  78. _initPage: function(){
  79. this.count = this.bundleItems.length;
  80. var i = this.count/this.json.pageSize;
  81. this.pages = (i.toInt()<i) ? i.toInt()+1 : i;
  82. this.currentPage = this.options.defaultPage || 1;
  83. this.options.defaultPage = null;
  84. },
  85. lookup: function(data, callback){
  86. if( this.lookuping )return;
  87. this.lookuping = true;
  88. this.getLookupAction(function(){
  89. if (this.json.application){
  90. var d = data || {};
  91. d.count = this.json.count;
  92. this.lookupAction.bundleView(this.json.id, d, function(json){
  93. this.bundleItems = json.data.valueList;
  94. this._initPage();
  95. if (this.bundleItems.length){
  96. if( this.noDataTextNode )this.noDataTextNode.destroy();
  97. this.loadCurrentPageData( function () {
  98. this.fireEvent("postLoad"); //用户配置的事件
  99. this.lookuping = false;
  100. if(callback)callback(this);
  101. }.bind(this));
  102. }else{
  103. //this._loadPageNode();
  104. this.viewPageAreaNode.empty();
  105. if( this.viewJson.noDataText ){
  106. var noDataTextNodeStyle = this.css.noDataTextNode;
  107. if( this.viewJson.viewStyles && this.viewJson.viewStyles["noDataTextNode"] ){
  108. noDataTextNodeStyle = this.viewJson.viewStyles["noDataTextNode"];
  109. }
  110. this.noDataTextNode = new Element( "div", {
  111. "styles": noDataTextNodeStyle,
  112. "text" : this.viewJson.noDataText
  113. }).inject( this.contentAreaNode );
  114. }
  115. if (this.loadingAreaNode){
  116. this.loadingAreaNode.destroy();
  117. this.loadingAreaNode = null;
  118. }
  119. this.fireEvent("postLoad"); //用户配置的事件
  120. this.lookuping = false;
  121. if(callback)callback(this);
  122. }
  123. }.bind(this));
  124. }
  125. }.bind(this));
  126. },
  127. loadCurrentPageData: function( callback, async ){
  128. //是否需要在翻页的时候清空之前的items ?
  129. if( this.pageloading )return;
  130. this.pageloading = true;
  131. this.items = [];
  132. var p = this.currentPage;
  133. var d = {};
  134. var valueList = this.bundleItems.slice((p-1)*this.json.pageSize,this.json.pageSize*p);
  135. d.bundleList = valueList;
  136. while (this.viewTable.rows.length>1){
  137. this.viewTable.deleteRow(-1);
  138. }
  139. //this.createLoadding();
  140. this.loadViewRes = this.lookupAction.loadView(this.json.name, this.json.application, d, function(json){
  141. this.viewData = json.data;
  142. this.fireEvent("postLoadPageData");
  143. if (this.viewJson.group.column){
  144. this.gridJson = json.data.groupGrid;
  145. // this.loadGroupData();
  146. }else{
  147. this.gridJson = json.data.grid;
  148. this.loadData();
  149. }
  150. if (this.gridJson.length) this._loadPageNode();
  151. if (this.loadingAreaNode){
  152. this.loadingAreaNode.destroy();
  153. this.loadingAreaNode = null;
  154. }
  155. this.pageloading = false;
  156. this.fireEvent("loadView"); //options 传入的事件
  157. this.fireEvent("postLoadPage");
  158. if(callback)callback();
  159. }.bind(this), null, async === false ? false : true );
  160. },
  161. loadData: function(){
  162. if (this.gridJson.length){
  163. // if( !this.options.paging ){
  164. this.gridJson.each(function(line, i){
  165. this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, line, null, i));
  166. }.bind(this));
  167. // }else{
  168. // this.loadPaging();
  169. // }
  170. }else{
  171. if (this.viewPageAreaNode) this.viewPageAreaNode.empty();
  172. }
  173. },
  174. loadDataByPaging : function(){
  175. if( this.isItemsLoading )return;
  176. if( !this.isItemsLoaded ){
  177. var from = Math.min( this.pageNumber * this.options.perPageCount , this.gridJson.length);
  178. var to = Math.min( ( this.pageNumber + 1 ) * this.options.perPageCount + 1 , this.gridJson.length);
  179. this.isItemsLoading = true;
  180. for( var i = from; i<to; i++ ){
  181. this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, this.gridJson[i], null, i));
  182. }
  183. this.isItemsLoading = false;
  184. this.pageNumber ++;
  185. if( to == this.gridJson.length )this.isItemsLoaded = true;
  186. }
  187. },
  188. getView: function(callback){
  189. this.getLookupAction(function(){
  190. if (this.json.application){
  191. this.getViewRes = this.lookupAction.getStatement(this.json.statementName, this.json.application, function(json){
  192. this.viewJson = JSON.decode(json.data.data);
  193. this.json = Object.merge(this.json, json.data);
  194. if (callback) callback();
  195. }.bind(this));
  196. // this.lookupAction.invoke({"name": "getView","async": true, "parameter": {"view": this.json.statementName, "application": this.json.application},"success": function(json){
  197. // this.viewJson = JSON.decode(json.data.data);
  198. // this.json = Object.merge(this.json, json.data);
  199. // //var viewData = JSON.decode(json.data.data);
  200. // if (callback) callback();
  201. // }.bind(this)});
  202. }else{
  203. this.getViewRes = this.lookupAction.getViewById(this.json.viewId, function(json){
  204. this.viewJson = JSON.decode(json.data.data);
  205. this.json.application = json.data.query;
  206. this.json = Object.merge(this.json, json.data);
  207. if (callback) callback();
  208. }.bind(this));
  209. }
  210. }.bind(this));
  211. },
  212. getFilter: function(){
  213. var filterData = [];
  214. if (this.searchStatus==="custom"){
  215. if (this.filterItems.length){
  216. this.filterItems.each(function(filter){
  217. filterData.push(filter.data);
  218. }.bind(this));
  219. }
  220. }
  221. if (this.searchStatus==="default"){
  222. var key = this.viewSearchInputNode.get("value");
  223. if (key && key!==this.lp.searchKeywork){
  224. this.viewJson.customFilterList.each(function(entry){
  225. if (entry.formatType==="textValue"){
  226. var d = {
  227. "path": entry.path,
  228. "value": key,
  229. "formatType": entry.formatType,
  230. "logic": "or",
  231. "comparison": "like"
  232. };
  233. filterData.push(d);
  234. }
  235. if (entry.formatType==="numberValue"){
  236. var v = key.toFloat();
  237. if (!isNaN(v)){
  238. var d = {
  239. "path": entry.path,
  240. "value": v,
  241. "formatType": entry.formatType,
  242. "logic": "or",
  243. "comparison": "like"
  244. };
  245. filterData.push(d);
  246. }
  247. }
  248. }.bind(this));
  249. }
  250. }
  251. return (filterData.length) ? filterData : null;
  252. },
  253. viewSearchCustomAddToFilter: function(){
  254. var pathIdx = this.viewSearchCustomPathListNode.selectedIndex;
  255. var comparisonIdx = this.viewSearchCustomComparisonListNode.selectedIndex;
  256. if (pathIdx===-1){
  257. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorTitle, this.viewSearchCustomPathListNode, {"x": 0, "y": 85});
  258. return false;
  259. }
  260. if (comparisonIdx===-1){
  261. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorComparison, this.viewSearchCustomComparisonListNode, {"x": 0, "y": 85});
  262. return false;
  263. }
  264. var pathOption = this.viewSearchCustomPathListNode.options[pathIdx];
  265. var entry = pathOption.retrieve("entry");
  266. if (entry){
  267. var pathTitle = entry.title;
  268. var path = entry.path;
  269. var comparison = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("value");
  270. var comparisonTitle = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("text");
  271. var value = "";
  272. if( entry.valueType === "script" && entry.valueScript && entry.valueScript.code ){
  273. var idx = this.viewSearchCustomValueNode.selectedIndex;
  274. if (idx!==-1){
  275. var v = this.viewSearchCustomValueNode.options[idx].get("value");
  276. value = entry.formatType === "booleanValue" ? (v==="true") : v;
  277. }
  278. }else{
  279. switch (entry.formatType){
  280. case "numberValue":
  281. value = this.viewSearchCustomValueNode.get("value");
  282. break;
  283. case "dateTimeValue":
  284. value = this.viewSearchCustomValueNode.get("value");
  285. break;
  286. case "booleanValue":
  287. var idx = this.viewSearchCustomValueNode.selectedIndex;
  288. if (idx!==-1){
  289. var v = this.viewSearchCustomValueNode.options[idx].get("value");
  290. value = (v==="true");
  291. }
  292. break;
  293. default:
  294. value = this.viewSearchCustomValueNode.get("value");
  295. }
  296. }
  297. if (value===""){
  298. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorValue, this.viewSearchCustomValueContentNode, {"x": 0, "y": 85});
  299. return false;
  300. }
  301. this.filterItems.push(new MWF.xApplication.query.Query.Statement.Filter(this, {
  302. "logic": "and",
  303. "path": path,
  304. "title": pathTitle,
  305. "comparison": comparison,
  306. "comparisonTitle": comparisonTitle,
  307. "value": value,
  308. "formatType": (entry.formatType=="datetimeValue") ? "dateTimeValue": entry.formatType
  309. }, this.viewSearchCustomFilterContentNode));
  310. this.searchCustomView();
  311. }
  312. },
  313. //搜索相关结束
  314. getStatementInfor : function () {
  315. return this.json;
  316. },
  317. switchStatement : function (json) {
  318. this.switchView(json);
  319. }
  320. });
  321. MWF.xApplication.query.Query.Statement.Item = new Class({
  322. Extends : MWF.xApplication.query.Query.Viewer.Item,
  323. initialize: function(view, data, prev, i){
  324. this.view = view;
  325. this.data = data;
  326. this.css = this.view.css;
  327. this.isSelected = false;
  328. this.prev = prev;
  329. this.idx = i;
  330. this.clazzType = "item";
  331. this.load();
  332. },
  333. load: function(){
  334. this.view.fireEvent("queryLoadItemRow", [null, this]);
  335. var viewStyles = this.view.viewJson.viewStyles;
  336. var viewContentTdNode = ( viewStyles && viewStyles["contentTd"] ) ? viewStyles["contentTd"] : this.css.viewContentTdNode;
  337. this.node = new Element("tr", {
  338. "styles": ( viewStyles && viewStyles["contentTr"] ) ? viewStyles["contentTr"] : this.css.viewContentTrNode
  339. });
  340. if (this.prev){
  341. this.node.inject(this.prev.node, "after");
  342. }else{
  343. this.node.inject(this.view.viewTable);
  344. }
  345. //if (this.view.json.select==="single" || this.view.json.select==="multi"){
  346. this.selectTd = new Element("td", { "styles": viewContentTdNode }).inject(this.node);
  347. this.selectTd.setStyles({"cursor": "pointer"});
  348. if (this.view.json.itemStyles) this.selectTd.setStyles(this.view.json.itemStyles);
  349. if( this.view.isSelectTdHidden() ){
  350. this.selectTd.hide();
  351. }
  352. //}
  353. //序号
  354. if (this.view.viewJson.isSequence==="yes"){
  355. this.sequenceTd = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
  356. this.sequenceTd.setStyle("width", "10px");
  357. var s= 1+this.view.json.pageSize*(this.view.currentPage-1)+this.idx;
  358. this.sequenceTd.set("text", s);
  359. }
  360. debugger;
  361. Object.each(this.view.entries, function(c, k){
  362. var cell = this.data.data[k];
  363. if (cell === undefined) cell = "";
  364. //if (cell){
  365. if (this.view.hideColumns.indexOf(k)===-1){
  366. var td = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
  367. if (k!== this.view.viewJson.group.column){
  368. //var v = (this.view.entries[k].code) ? MWF.Macro.exec(this.view.entries[k].code, {"value": cell, "gridData": this.view.gridJson, "data": this.view.viewData, "entry": this.data}) : cell;
  369. var v = cell;
  370. if (c.isHtml){
  371. td.set("html", v);
  372. }else{
  373. td.set("text", v);
  374. }
  375. if( typeOf(c.contentProperties) === "object" )td.setProperties(c.contentProperties);
  376. if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
  377. if( typeOf(c.contentStyles) === "object" )td.setStyles(c.contentStyles);
  378. }else{
  379. if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
  380. }
  381. if (this.view.openColumns.indexOf(k)!==-1){
  382. this.setOpenWork(td, c)
  383. }
  384. if (k!== this.view.viewJson.group.column){
  385. Object.each( c.events || {}, function (e , key) {
  386. if(e.code){
  387. if( key === "loadContent" ){
  388. this.view.Macro.fire( e.code,
  389. {"node" : td, "json" : c, "data" : v, "view": this.view, "row" : this});
  390. }else if( key !== "loadTitle" ){
  391. td.addEvent(key, function(event){
  392. return this.view.Macro.fire(
  393. e.code,
  394. {"node" : td, "json" : c, "data" : v, "view": this.view, "row" : this},
  395. event
  396. );
  397. }.bind(this));
  398. }
  399. }
  400. }.bind(this));
  401. }
  402. }
  403. //}
  404. }.bind(this));
  405. //默认选中
  406. var defaultSelectedScript = this.view.json.defaultSelectedScript || this.view.viewJson.defaultSelectedScript;
  407. if( !this.isSelected && defaultSelectedScript ){
  408. // var flag = this.view.json.select || this.view.viewJson.select || "none";
  409. // if ( flag ==="single" || flag==="multi"){
  410. //
  411. // }
  412. var flag = this.view.Macro.exec( defaultSelectedScript,
  413. {"node" : this.node, "data" : this.data, "view": this.view, "row" : this});
  414. if( flag ){
  415. if( flag === "multi" || flag === "single" ){
  416. this.select( flag );
  417. }else if( flag.toString() === "true" ){
  418. var f = this.view.json.select || this.view.viewJson.select || "none";
  419. if ( f ==="single" || f==="multi"){
  420. this.select();
  421. }
  422. }
  423. }
  424. }
  425. this.setEvent();
  426. this.view.fireEvent("postLoadItemRow", [null, this]);
  427. },
  428. setOpenWork: function(td, column){
  429. td.setStyle("cursor", "pointer");
  430. if( column.clickCode ){
  431. if( !this.view.Macro ){
  432. MWF.require("MWF.xScript.Macro", function () {
  433. this.view.businessData = {};
  434. this.view.Macro = new MWF.Macro.PageContext(this.view);
  435. }.bind(this), false);
  436. }
  437. td.addEvent("click", function( ev ){
  438. var result = this.view.Macro.fire(column.clickCode, this, ev);
  439. ev.stopPropagation();
  440. return result;
  441. }.bind(this));
  442. }else{
  443. // if (this.view.json.type==="cms"){
  444. // td.addEvent("click", function(ev){
  445. // this.openCms(ev)
  446. // ev.stopPropagation();
  447. // }.bind(this));
  448. // }else{
  449. td.addEvent("click", function(ev){
  450. this.openWorkAndCompleted(ev)
  451. ev.stopPropagation();
  452. }.bind(this));
  453. // }
  454. }
  455. }
  456. });
  457. MWF.xApplication.query.Query.Statement.Filter = new Class({
  458. Extends : MWF.xApplication.query.Query.Viewer.Filter
  459. });
  460. MWF.xApplication.query.Query.Statement.Actionbar = new Class({
  461. Extends : MWF.xApplication.query.Query.Viewer.Actionbar
  462. });
  463. MWF.xApplication.query.Query.Statement.Paging = new Class({
  464. Extends : MWF.xApplication.query.Query.Viewer.Paging
  465. });