std_ration_lib.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /**
  2. * Standard Ration Lib
  3. * Created by Mai on 2017/5/16.
  4. */
  5. /*var rationChapterSpread, sectionRationsSpread;*/
  6. var rationLibObj = {
  7. doAfterGetRationTree: null, //获取章节树回调
  8. doAfterLoadGetRations: null, //获取章节树下定额后回调
  9. rationChapterSpread: null,
  10. sectionRationsSpread: null,
  11. rationChapterTreeController: null,
  12. refreshSettingForHint: function () {
  13. TREE_SHEET_HELPER.initSetting($('#stdSectionRations')[0], rationLibObj.sectionRationsSetting);
  14. },
  15. checkSpread: function () {
  16. if (!this.rationChapterSpread) {
  17. this.rationChapterSpread = SheetDataHelper.createNewSpread($('#stdRationChapter')[0]);
  18. sheetCommonObj.spreadDefaultStyle(this.rationChapterSpread);
  19. this.rationChapterSpread.getSheet(0).name('stdRationLib_chapter');
  20. this.rationChapterSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onChapterSpreadCellDoubleClick);
  21. }
  22. if (!this.sectionRationsSpread) {
  23. this.sectionRationsSpread = SheetDataHelper.createNewSpread($('#stdSectionRations')[0]);
  24. sheetCommonObj.spreadDefaultStyle(this.sectionRationsSpread);
  25. this.sectionRationsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onRationSpreadCellDoubleClick);
  26. this.refreshSettingForHint();
  27. }
  28. },
  29. refreshSpread: function () {
  30. if (this.rationChapterSpread) {
  31. this.rationChapterSpread.refresh();
  32. }
  33. if (this.sectionRationsSpread) {
  34. this.sectionRationsSpread.refresh();
  35. }
  36. },
  37. loadStdRationLibs: function () {
  38. let select = $('#stdRationLibSelect');
  39. select.empty();
  40. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  41. let selectedRationLib = sessionStorage.getItem('stdRationLib');
  42. ration_lib.forEach(function (data) {
  43. let option = $('<option>').val(data.id).text(data.name);
  44. //select.append($('<option>').val(data.id).text(data.name));
  45. if(selectedRationLib && data.id == selectedRationLib){
  46. option.attr('selected', 'selected');
  47. }
  48. select.append(option);
  49. });
  50. if (select[0].options.length !== 0) {
  51. rationLibObj.loadStdRation(select.val());
  52. }
  53. },
  54. loadStdRation: function (rationLibID) {
  55. $.bootstrapLoading.start();
  56. var that = this;
  57. var showRationChapterTree = function (datas) {
  58. var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  59. console.log(that);
  60. that.tree = rationChapterTree;
  61. var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, that.rationChapterSpread.getActiveSheet(), that.rationChapterTreeSetting);
  62. rationChapterTree.loadDatas(datas);
  63. //读取展开收起状态
  64. let currentExpState = sessionStorage.getItem('stdRationLibExpState');
  65. if(currentExpState){
  66. that.tree.setExpandedByState(that.tree.items, currentExpState);
  67. }
  68. else {
  69. //展开至第二层
  70. for(let root of that.tree.roots){
  71. root.setExpanded(true);
  72. that.tree.setRootExpanded(root.children, false);
  73. }
  74. }
  75. rationChapterTreeController.showTreeData();
  76. rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  77. rationLibObj.loadSectionRations(node.getID());
  78. });
  79. if (rationChapterTree.firstNode()) {
  80. rationLibObj.loadSectionRations(rationChapterTree.firstNode().getID());
  81. } else {
  82. rationLibObj.loadSectionRations();
  83. };
  84. };
  85. CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID}, function (datas) {
  86. showRationChapterTree(datas);
  87. if(that.doAfterGetRationTree){
  88. that.doAfterGetRationTree();
  89. }
  90. $.bootstrapLoading.end();
  91. }, function () {
  92. showRationChapterTree([]);
  93. $.bootstrapLoading.end();
  94. });
  95. },
  96. //双击隐藏显示
  97. onChapterSpreadCellDoubleClick: function (sender, args) {
  98. let me = rationLibObj;
  99. let node = me.tree.items[args.row];
  100. if (!node || node.children.length === 0)
  101. return;
  102. node.setExpanded(!node.expanded);
  103. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  104. TREE_SHEET_HELPER.massOperationSheet(args.sheet, function () {
  105. let iCount = node.posterityCount(), i, child;
  106. for (i = 0; i < iCount; i++) {
  107. child = me.tree.items[args.row + i + 1];
  108. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  109. }
  110. args.sheet.invalidateLayout();
  111. });
  112. args.sheet.repaint();
  113. },
  114. setTagForHint: function (datas) {
  115. let sheet = this.sectionRationsSpread.getActiveSheet();
  116. sheet.suspendPaint();
  117. sheet.suspendEvent();
  118. for(let i = 0, len = sheet.getRowCount(); i < len; i++){
  119. sheet.setTag(i, 0, '');
  120. }
  121. for(let i = 0, len = datas.length; i < len; i++){
  122. sheet.setTag(i, 0, datas[i].hint ? datas[i].hint : '');
  123. }
  124. sheet.resumePaint();
  125. sheet.resumeEvent();
  126. },
  127. loadSectionRations: function (sectionID) {
  128. let me = this;
  129. var showDatas = function (datas, setting) {
  130. let rationSheet = rationLibObj.sectionRationsSpread.getActiveSheet();
  131. TREE_SHEET_HELPER.massOperationSheet(rationSheet, function () {
  132. rationSheet.setColumnWidth(0, 25, GC.Spread.Sheets.SheetArea.rowHeader);
  133. });
  134. SheetDataHelper.loadSheetHeader(setting, rationLibObj.sectionRationsSpread.getActiveSheet());
  135. SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
  136. rationLibObj.setTagForHint(datas);
  137. };
  138. if (sectionID) {
  139. CommonAjax.post('/complementaryRation/api/getRationGljItemsBySection', {user_Id: userID, sectionId: sectionID}, function (datas) {
  140. showDatas(datas, rationLibObj.sectionRationsSetting);
  141. if(me.doAfterLoadGetRations){
  142. me.doAfterLoadGetRations(datas);
  143. me.doAfterLoadGetRations = null;
  144. }
  145. }, function () {
  146. showDatas([], rationLibObj.sectionRationsSetting);
  147. });
  148. } else {
  149. showDatas([], rationLibObj.sectionRationsSetting);
  150. }
  151. },
  152. onRationSpreadCellDoubleClick: function (sender, args) {
  153. var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
  154. if (rationCode !== '') {
  155. projectObj.project.Ration.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  156. projectObj.setActiveCell('quantity', true);
  157. });
  158. }
  159. },
  160. loadStdRationContextMenu: function () {
  161. let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(), rationModel = projectObj.project.Ration;;
  162. $.contextMenu({
  163. selector: '#stdSectionRations',
  164. build: function ($trigger, e) {
  165. let target = SheetDataHelper.safeRightClickSelection($trigger, e, rationSpread);
  166. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  167. },
  168. items: {
  169. "insertStdRation": {
  170. name: "插入定额",
  171. icon: 'fa-sign-in',
  172. callback: function (key, opt) {
  173. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  174. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  175. if (rationCode !== '') {
  176. rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  177. });
  178. }
  179. }
  180. },
  181. "replaceStdRation": {
  182. name: "替换定额",
  183. icon: 'fa-sign-in',
  184. callback: function (key, opt) {
  185. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  186. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  187. let mainTreeSelected = projectObj.project.mainTree.selected;
  188. if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
  189. rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
  190. }
  191. }
  192. },
  193. }
  194. });
  195. },
  196. expandSearchNodes: function(nodes){
  197. let that = rationLibObj;
  198. TREE_SHEET_HELPER.massOperationSheet(that.rationChapterSpread.getActiveSheet(), function () {
  199. function expParentNode(node){
  200. if(node.parent){
  201. expParentNode(node.parent);
  202. if(!node.parent.expanded){
  203. node.parent.setExpanded(true);
  204. }
  205. }
  206. }
  207. for(let node of nodes){
  208. expParentNode(node);
  209. }
  210. TREE_SHEET_HELPER.refreshTreeNodeData(that.rationChapterTreeSetting, that.rationChapterSpread.getActiveSheet(), that.tree.roots, true);
  211. TREE_SHEET_HELPER.refreshNodesVisible(that.tree.roots, that.rationChapterSpread.getActiveSheet(), true);
  212. });
  213. },
  214. initSel: function (row) {
  215. let me = this;
  216. let sheet = me.rationChapterSpread.getActiveSheet();
  217. sheet.setActiveCell(row, 0);
  218. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  219. let sectionNode = me.tree.items[row] || null;
  220. me.loadSectionRations(sectionNode ? sectionNode.data.ID : null);
  221. },
  222. locateAtRation: function(code){
  223. let me = rationLibObj;
  224. //查找定额,以确定定额所在章节节点
  225. let firstLibID = projectInfoObj.projectInfo.engineeringInfo.ration_lib.length > 0 ?
  226. projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id : null;
  227. let locateRow = 0,
  228. locateSubRow = 0;
  229. CommonAjax.post('/complementaryRation/api/getRationItem', {rationRepIds: [firstLibID], code: code}, function (ration) {
  230. if(ration && ration.sectionId){
  231. let sectionNode = me.tree.findNode(ration.sectionId);
  232. if(sectionNode){
  233. me.expandSearchNodes([sectionNode]);
  234. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  235. }
  236. locateRow = sectionNode.serialNo();
  237. me.doAfterLoadGetRations = function (rationItems) {
  238. let rationSheet = me.sectionRationsSpread.getActiveSheet();
  239. locateSubRow = _.findIndex(rationItems, {ID: ration.ID});
  240. rationSheet.setActiveCell(locateSubRow, 0);
  241. rationSheet.showRow(locateSubRow, GC.Spread.Sheets.VerticalPosition.center);
  242. };
  243. me.initSel(locateRow);
  244. }
  245. else {
  246. me.initSel(locateRow);
  247. }
  248. }, function () {
  249. me.initSel(locateRow);
  250. });
  251. },
  252. rationChapterTreeSetting: {
  253. "emptyRowHeader": true,
  254. "rowHeaderWidth": 15,
  255. "emptyRows":0,
  256. "headRows":1,
  257. "headRowHeight":[30],
  258. "defaultRowHeight": 21,
  259. "treeCol": 0,
  260. "cols":[{
  261. "width":400,
  262. "readOnly": true,
  263. "head":{
  264. "titleNames":["名称"],
  265. "spanCols":[1],
  266. "spanRows":[1],
  267. "vAlign":[1],
  268. "hAlign":[1],
  269. "font":["Arial"]
  270. },
  271. "data":{
  272. "field":"name",
  273. "vAlign":1,
  274. "hAlign":0,
  275. "font":"Arial"
  276. }
  277. }]
  278. },
  279. sectionRationsSetting: {
  280. "emptyRows":3,
  281. "headRows":1,
  282. "headRowHeight":[20],
  283. "defaultRowHeight": 21,
  284. "cols":[{
  285. "width":60,
  286. "readOnly": true,
  287. "showHint": true,
  288. "head":{
  289. "titleNames":["编码"],
  290. "spanCols":[1],
  291. "spanRows":[1],
  292. "vAlign":[1],
  293. "hAlign":[1],
  294. "font":["Arial"]
  295. },
  296. "data":{
  297. "field":"code",
  298. "vAlign":1,
  299. "hAlign":0,
  300. "font":"Arial"
  301. }
  302. }, {
  303. "width":220,
  304. "readOnly": true,
  305. "head":{
  306. "titleNames":["名称"],
  307. "spanCols":[1],
  308. "spanRows":[1],
  309. "vAlign":[1],
  310. "hAlign":[1],
  311. "font":["Arial"]
  312. },
  313. "data":{
  314. "field":"name",
  315. "vAlign":1,
  316. "hAlign":0,
  317. "font":"Arial"
  318. }
  319. }, {
  320. "width":55,
  321. "readOnly":true,
  322. "head":{
  323. "titleNames":["单位"],
  324. "spanCols":[1],
  325. "spanRows":[1],
  326. "vAlign":[1],
  327. "hAlign":[1],
  328. "font":["Arial"]
  329. },
  330. "data":{
  331. "field":"unit",
  332. "vAlign":1,
  333. "hAlign":1,
  334. "font":"Arial"
  335. }
  336. }, {
  337. "width":60,
  338. "readOnly":true,
  339. "head":{
  340. "titleNames":["基价"],
  341. "spanCols":[1],
  342. "spanRows":[1],
  343. "vAlign":[1],
  344. "hAlign":[1],
  345. "font":["Arial"]
  346. },
  347. "data":{
  348. "field":"basePrice",
  349. "vAlign":1,
  350. "hAlign":2,
  351. "font":"Arial"
  352. }
  353. }]
  354. },
  355. getStdRationLibIDs: function () {
  356. let ids = [];
  357. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  358. alert('当前项目无定额库,请添加定额库。');
  359. return null;
  360. }
  361. for(let rationLib of projectInfoObj.projectInfo.engineeringInfo.ration_lib){
  362. ids.push(rationLib.id);
  363. }
  364. return ids;
  365. },
  366. getCurrentStdRationLibID:function () {
  367. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  368. alert('当前项目无定额库,请添加定额库。');
  369. return null;
  370. }
  371. if($('#stdRationLibSelect').val()){
  372. return parseInt($('#stdRationLibSelect').val());
  373. }else {
  374. return projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id;
  375. }
  376. },
  377. getFirstStdRationLibID: function () {
  378. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  379. alert('当前项目无定额库,请添加定额库。');
  380. return null;
  381. }
  382. return parseInt(projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id);
  383. }
  384. };
  385. addEventOnResize(rationLibObj.refreshSettingForHint);
  386. //赋初始高度
  387. if($('#stdRationChapter').height() === 0 || $('#stdSectionRations').height() === 0){
  388. $('#stdRationChapter').height($(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-q").height()-312);
  389. $('#stdSectionRations').height(270);
  390. }
  391. $('#stdRationTab').bind('click', function () {
  392. var select = $('#stdRationLibSelect');
  393. rationLibObj.checkSpread();
  394. if (select[0].options.length === 0) {
  395. rationLibObj.loadStdRationLibs();
  396. rationLibObj.loadStdRationContextMenu();
  397. };
  398. });
  399. $('#stdRationLibSelect').change(function () {
  400. var select = $(this);
  401. if (this.children.length !== 0) {
  402. let rationLibId = select.val();
  403. sessionStorage.setItem('stdRationLib', rationLibId);
  404. sessionStorage.removeItem('stdRationLibExpState');
  405. rationLibObj.loadStdRation(rationLibId);
  406. }
  407. });
  408. //回车键搜索
  409. $('#rationSearchKeyword').bind('keypress', function (event) {
  410. if(event.keyCode === 13){
  411. $(this).blur();
  412. $('#rationSearch').click();
  413. }
  414. });
  415. $('#rationSearch').click(function () {
  416. var keyword = $('#rationSearchKeyword').val(), rationLibID = $('#stdRationLibSelect').val();
  417. var getResultHtml = function (result) {
  418. var html = [], i, serialNo;
  419. html.push('<div class="d-flex justify-content-between">');
  420. html.push('<span>搜索结果:');
  421. html.push(result.length.toString());
  422. html.push('</span>');
  423. html.push('<a title="关闭搜索" class="btn btn-link btn-sm" href="javascript:void(0)"><i class="fa fa-remove" aria-hidden="true"></i></a>');
  424. html.push('</div>');
  425. html.push('<div class="w-100 main-data-side-search">');
  426. html.push('</div>');
  427. return html.join('');
  428. };
  429. var showResult = function (result) {
  430. var resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
  431. SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
  432. SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet(), result);
  433. resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
  434. };
  435. CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibID, 'keyword': keyword}, function (result) {
  436. //sort
  437. result.sort(function (a, b) {
  438. let rst = 0;
  439. if(a.code > b.code) rst = 1;
  440. else if(a.code < b.code) rst = -1;
  441. return rst;
  442. });
  443. var resultObj = $('#rationSearchResult'), resultSpread = null;
  444. resultObj.empty();
  445. resultObj.append(getResultHtml(result));
  446. $('a', resultObj).click(function () {
  447. resultObj.hide();
  448. $(".main-data-side-search", resultObj).height(0);
  449. autoFlashHeight();
  450. //$(".main-data-side-d").height($(window).height() - $(".header").height() - $(".toolsbar").height() - $(".tools-bar-height-d").height() - 202);
  451. rationLibObj.refreshSpread();
  452. });
  453. resultObj.show();
  454. $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
  455. showResult(result);
  456. });
  457. });