Statement.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. this.currentPage = this.options.defaultPage || 1;
  96. this.options.defaultPage = null;
  97. if( this.noDataTextNode )this.noDataTextNode.destroy();
  98. this.loadCurrentPageData( function (json) {
  99. if( typeOf(json.count) === "number" )this.totalCount = json.count;
  100. if(this.totalCount){
  101. this.fireEvent("postLoad"); //用户配置的事件
  102. this.lookuping = false;
  103. if(callback)callback(this);
  104. }else{
  105. this.viewPageAreaNode.empty();
  106. if( this.viewJson.noDataText ){
  107. var noDataTextNodeStyle = this.css.noDataTextNode;
  108. if( this.viewJson.viewStyles && this.viewJson.viewStyles["noDataTextNode"] ){
  109. noDataTextNodeStyle = this.viewJson.viewStyles["noDataTextNode"];
  110. }
  111. this.noDataTextNode = new Element( "div", {
  112. "styles": noDataTextNodeStyle,
  113. "text" : this.viewJson.noDataText
  114. }).inject( this.contentAreaNode );
  115. }
  116. // if (this.loadingAreaNode){
  117. // this.loadingAreaNode.destroy();
  118. // this.loadingAreaNode = null;
  119. // }
  120. this.fireEvent("postLoad"); //用户配置的事件
  121. this.lookuping = false;
  122. if(callback)callback(this);
  123. }
  124. }.bind(this), true,"all");
  125. // }.bind(this));
  126. // }
  127. // }.bind(this));
  128. },
  129. loadCurrentPageData: function( callback, async, type ){
  130. //是否需要在翻页的时候清空之前的items ?
  131. if( this.pageloading )return;
  132. this.pageloading = true;
  133. this.items = [];
  134. var p = this.currentPage;
  135. var d = {
  136. "filterList" : [],
  137. "parameter" : {
  138. }
  139. };
  140. while (this.viewTable.rows.length>1){
  141. this.viewTable.deleteRow(-1);
  142. }
  143. //this.createLoadding();
  144. this.loadViewRes = o2.Actions.load("x_query_assemble_surface").StatementAction.executeV2(
  145. this.json.statementId || this.json.statementName,
  146. type || "data", p, this.json.pageSize, d, function(json){
  147. this.gridJson = json.data;
  148. this.fireEvent("postLoadPageData");
  149. // if (this.viewJson.group.column){
  150. // this.gridJson = json.data.groupGrid;
  151. // }else{
  152. // this.gridJson = json.data.grid;
  153. this.loadData();
  154. // }
  155. if (this.gridJson.length) this._loadPageNode();
  156. if (this.loadingAreaNode){
  157. this.loadingAreaNode.destroy();
  158. this.loadingAreaNode = null;
  159. }
  160. this.pageloading = false;
  161. this.fireEvent("loadView"); //options 传入的事件
  162. this.fireEvent("postLoadPage");
  163. if(callback)callback( json );
  164. }.bind(this), null, async === false ? false : true );
  165. },
  166. getView: function(callback){
  167. this.getViewRes = o2.Actions.load("x_query_assemble_surface").StatementAction.get(this.json.statementId || this.json.statementName, function(json){
  168. debugger;
  169. this.viewJson = JSON.decode(json.data.view);
  170. this.json.application = json.data.query;
  171. //this.json = Object.merge(this.json, json.data);
  172. this.statementJson = json.data;
  173. if (callback) callback();
  174. }.bind(this));
  175. },
  176. loadData: function(){
  177. if (this.gridJson.length){
  178. // if( !this.options.paging ){
  179. this.gridJson.each(function(line, i){
  180. this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, line, null, i));
  181. }.bind(this));
  182. // }else{
  183. // this.loadPaging();
  184. // }
  185. }else{
  186. if (this.viewPageAreaNode) this.viewPageAreaNode.empty();
  187. }
  188. },
  189. loadDataByPaging : function(){
  190. if( this.isItemsLoading )return;
  191. if( !this.isItemsLoaded ){
  192. var from = Math.min( this.pageNumber * this.options.perPageCount , this.gridJson.length);
  193. var to = Math.min( ( this.pageNumber + 1 ) * this.options.perPageCount + 1 , this.gridJson.length);
  194. this.isItemsLoading = true;
  195. for( var i = from; i<to; i++ ){
  196. this.items.push(new MWF.xApplication.query.Query.Statement.Item(this, this.gridJson[i], null, i));
  197. }
  198. this.isItemsLoading = false;
  199. this.pageNumber ++;
  200. if( to == this.gridJson.length )this.isItemsLoaded = true;
  201. }
  202. },
  203. getFilter: function(){
  204. var filterData = [];
  205. if (this.searchStatus==="custom"){
  206. if (this.filterItems.length){
  207. this.filterItems.each(function(filter){
  208. filterData.push(filter.data);
  209. }.bind(this));
  210. }
  211. }
  212. if (this.searchStatus==="default"){
  213. var key = this.viewSearchInputNode.get("value");
  214. if (key && key!==this.lp.searchKeywork){
  215. this.viewJson.customFilterList.each(function(entry){
  216. if (entry.formatType==="textValue"){
  217. var d = {
  218. "path": entry.path,
  219. "value": key,
  220. "formatType": entry.formatType,
  221. "logic": "and",
  222. "comparison": "like"
  223. };
  224. filterData.push(d);
  225. }
  226. if (entry.formatType==="numberValue"){
  227. var v = key.toFloat();
  228. if (!isNaN(v)){
  229. var d = {
  230. "path": entry.path,
  231. "value": v,
  232. "formatType": entry.formatType,
  233. "logic": "and",
  234. "comparison": "like"
  235. };
  236. filterData.push(d);
  237. }
  238. }
  239. }.bind(this));
  240. }
  241. }
  242. return (filterData.length) ? filterData : null;
  243. },
  244. viewSearchCustomAddToFilter: function(){
  245. var pathIdx = this.viewSearchCustomPathListNode.selectedIndex;
  246. var comparisonIdx = this.viewSearchCustomComparisonListNode.selectedIndex;
  247. if (pathIdx===-1){
  248. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorTitle, this.viewSearchCustomPathListNode, {"x": 0, "y": 85});
  249. return false;
  250. }
  251. if (comparisonIdx===-1){
  252. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorComparison, this.viewSearchCustomComparisonListNode, {"x": 0, "y": 85});
  253. return false;
  254. }
  255. var pathOption = this.viewSearchCustomPathListNode.options[pathIdx];
  256. var entry = pathOption.retrieve("entry");
  257. if (entry){
  258. var pathTitle = entry.title;
  259. var path = entry.path;
  260. var comparison = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("value");
  261. var comparisonTitle = this.viewSearchCustomComparisonListNode.options[comparisonIdx].get("text");
  262. var value = "";
  263. if( entry.valueType === "script" && entry.valueScript && entry.valueScript.code ){
  264. var idx = this.viewSearchCustomValueNode.selectedIndex;
  265. if (idx!==-1){
  266. var v = this.viewSearchCustomValueNode.options[idx].get("value");
  267. value = entry.formatType === "booleanValue" ? (v==="true") : v;
  268. }
  269. }else{
  270. switch (entry.formatType){
  271. case "numberValue":
  272. value = this.viewSearchCustomValueNode.get("value");
  273. break;
  274. case "dateTimeValue":
  275. value = this.viewSearchCustomValueNode.get("value");
  276. break;
  277. case "booleanValue":
  278. var idx = this.viewSearchCustomValueNode.selectedIndex;
  279. if (idx!==-1){
  280. var v = this.viewSearchCustomValueNode.options[idx].get("value");
  281. value = (v==="true");
  282. }
  283. break;
  284. default:
  285. value = this.viewSearchCustomValueNode.get("value");
  286. }
  287. }
  288. if (value===""){
  289. MWF.xDesktop.notice("error", {"x": "left", "y": "top"}, this.lp.filterErrorValue, this.viewSearchCustomValueContentNode, {"x": 0, "y": 85});
  290. return false;
  291. }
  292. this.filterItems.push(new MWF.xApplication.query.Query.Statement.Filter(this, {
  293. "logic": "and",
  294. "path": path,
  295. "title": pathTitle,
  296. "comparison": comparison,
  297. "comparisonTitle": comparisonTitle,
  298. "value": value,
  299. "formatType": (entry.formatType=="datetimeValue") ? "dateTimeValue": entry.formatType
  300. }, this.viewSearchCustomFilterContentNode));
  301. this.searchCustomView();
  302. }
  303. },
  304. //搜索相关结束
  305. getStatementInfor : function () {
  306. return this.json;
  307. },
  308. switchStatement : function (json) {
  309. this.switchView(json);
  310. }
  311. });
  312. MWF.xApplication.query.Query.Statement.Item = new Class({
  313. Extends : MWF.xApplication.query.Query.Viewer.Item,
  314. initialize: function(view, data, prev, i){
  315. this.view = view;
  316. this.data = data;
  317. this.css = this.view.css;
  318. this.isSelected = false;
  319. this.prev = prev;
  320. this.idx = i;
  321. this.clazzType = "item";
  322. this.load();
  323. },
  324. load: function(){
  325. this.view.fireEvent("queryLoadItemRow", [null, this]);
  326. var viewStyles = this.view.viewJson.viewStyles;
  327. var viewContentTdNode = ( viewStyles && viewStyles["contentTd"] ) ? viewStyles["contentTd"] : this.css.viewContentTdNode;
  328. this.node = new Element("tr", {
  329. "styles": ( viewStyles && viewStyles["contentTr"] ) ? viewStyles["contentTr"] : this.css.viewContentTrNode
  330. });
  331. if (this.prev){
  332. this.node.inject(this.prev.node, "after");
  333. }else{
  334. this.node.inject(this.view.viewTable);
  335. }
  336. //if (this.view.json.select==="single" || this.view.json.select==="multi"){
  337. this.selectTd = new Element("td", { "styles": viewContentTdNode }).inject(this.node);
  338. this.selectTd.setStyles({"cursor": "pointer"});
  339. if (this.view.json.itemStyles) this.selectTd.setStyles(this.view.json.itemStyles);
  340. if( this.view.isSelectTdHidden() ){
  341. this.selectTd.hide();
  342. }
  343. //}
  344. //序号
  345. if (this.view.viewJson.isSequence==="yes"){
  346. this.sequenceTd = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
  347. this.sequenceTd.setStyle("width", "10px");
  348. var s= 1+this.view.json.pageSize*(this.view.currentPage-1)+this.idx;
  349. this.sequenceTd.set("text", s);
  350. }
  351. debugger;
  352. Object.each(this.view.entries, function(c, k){
  353. var cell = this.data.data[k];
  354. if (cell === undefined) cell = "";
  355. //if (cell){
  356. if (this.view.hideColumns.indexOf(k)===-1){
  357. var td = new Element("td", {"styles": viewContentTdNode}).inject(this.node);
  358. if (k!== this.view.viewJson.group.column){
  359. //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;
  360. var v = cell;
  361. if (c.isHtml){
  362. td.set("html", v);
  363. }else{
  364. td.set("text", v);
  365. }
  366. if( typeOf(c.contentProperties) === "object" )td.setProperties(c.contentProperties);
  367. if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
  368. if( typeOf(c.contentStyles) === "object" )td.setStyles(c.contentStyles);
  369. }else{
  370. if (this.view.json.itemStyles) td.setStyles(this.view.json.itemStyles);
  371. }
  372. if (this.view.openColumns.indexOf(k)!==-1){
  373. this.setOpenWork(td, c)
  374. }
  375. if (k!== this.view.viewJson.group.column){
  376. Object.each( c.events || {}, function (e , key) {
  377. if(e.code){
  378. if( key === "loadContent" ){
  379. this.view.Macro.fire( e.code,
  380. {"node" : td, "json" : c, "data" : v, "view": this.view, "row" : this});
  381. }else if( key !== "loadTitle" ){
  382. td.addEvent(key, function(event){
  383. return this.view.Macro.fire(
  384. e.code,
  385. {"node" : td, "json" : c, "data" : v, "view": this.view, "row" : this},
  386. event
  387. );
  388. }.bind(this));
  389. }
  390. }
  391. }.bind(this));
  392. }
  393. }
  394. //}
  395. }.bind(this));
  396. //默认选中
  397. var defaultSelectedScript = this.view.json.defaultSelectedScript || this.view.viewJson.defaultSelectedScript;
  398. if( !this.isSelected && defaultSelectedScript ){
  399. // var flag = this.view.json.select || this.view.viewJson.select || "none";
  400. // if ( flag ==="single" || flag==="multi"){
  401. //
  402. // }
  403. var flag = this.view.Macro.exec( defaultSelectedScript,
  404. {"node" : this.node, "data" : this.data, "view": this.view, "row" : this});
  405. if( flag ){
  406. if( flag === "multi" || flag === "single" ){
  407. this.select( flag );
  408. }else if( flag.toString() === "true" ){
  409. var f = this.view.json.select || this.view.viewJson.select || "none";
  410. if ( f ==="single" || f==="multi"){
  411. this.select();
  412. }
  413. }
  414. }
  415. }
  416. this.setEvent();
  417. this.view.fireEvent("postLoadItemRow", [null, this]);
  418. },
  419. setOpenWork: function(td, column){
  420. td.setStyle("cursor", "pointer");
  421. if( column.clickCode ){
  422. if( !this.view.Macro ){
  423. MWF.require("MWF.xScript.Macro", function () {
  424. this.view.businessData = {};
  425. this.view.Macro = new MWF.Macro.PageContext(this.view);
  426. }.bind(this), false);
  427. }
  428. td.addEvent("click", function( ev ){
  429. var result = this.view.Macro.fire(column.clickCode, this, ev);
  430. ev.stopPropagation();
  431. return result;
  432. }.bind(this));
  433. }else{
  434. // if (this.view.json.type==="cms"){
  435. // td.addEvent("click", function(ev){
  436. // this.openCms(ev)
  437. // ev.stopPropagation();
  438. // }.bind(this));
  439. // }else{
  440. td.addEvent("click", function(ev){
  441. this.openWorkAndCompleted(ev)
  442. ev.stopPropagation();
  443. }.bind(this));
  444. // }
  445. }
  446. }
  447. });
  448. MWF.xApplication.query.Query.Statement.Filter = new Class({
  449. Extends : MWF.xApplication.query.Query.Viewer.Filter
  450. });
  451. MWF.xApplication.query.Query.Statement.Actionbar = new Class({
  452. Extends : MWF.xApplication.query.Query.Viewer.Actionbar
  453. });
  454. MWF.xApplication.query.Query.Statement.Paging = new Class({
  455. Extends : MWF.xApplication.query.Query.Viewer.Paging
  456. });