std_ration_lib.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /**
  2. * Standard Ration Lib
  3. * Created by Mai on 2017/5/16.
  4. */
  5. /*var rationChapterSpread, sectionRationsSpread;*/
  6. var rationLibObj = {
  7. searchLimit: 50,
  8. libType: {complementary: 0, std: 1},
  9. compleRationLibId: 'compleRationLib',
  10. doAfterGetRationTree: null, //获取章节树回调
  11. doAfterLoadGetRations: null, //获取章节树下定额后回调
  12. rationChapterSpread: null,
  13. sectionRationsSpread: null,
  14. resultSpread: null,
  15. rationChapterTreeController: null,
  16. refreshSettingForHint: function () {
  17. TREE_SHEET_HELPER.initSetting($('#stdSectionRations')[0], rationLibObj.sectionRationsSetting);
  18. },
  19. checkSpread: function () {
  20. if (!this.rationChapterSpread) {
  21. this.rationChapterSpread = SheetDataHelper.createNewSpread($('#stdRationChapter')[0]);
  22. sheetCommonObj.spreadDefaultStyle(this.rationChapterSpread);
  23. this.rationChapterSpread.getSheet(0).name('stdRationLib_chapter');
  24. this.rationChapterSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onChapterSpreadCellDoubleClick);
  25. }
  26. if (!this.sectionRationsSpread) {
  27. this.sectionRationsSpread = SheetDataHelper.createNewSpread($('#stdSectionRations')[0]);
  28. sheetCommonObj.spreadDefaultStyle(this.sectionRationsSpread);
  29. this.sectionRationsSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onRationSpreadCellDoubleClick);
  30. this.refreshSettingForHint();
  31. }
  32. },
  33. refreshSpread: function () {
  34. if (this.rationChapterSpread) {
  35. this.rationChapterSpread.refresh();
  36. }
  37. if (this.sectionRationsSpread) {
  38. this.sectionRationsSpread.refresh();
  39. }
  40. if(this.resultSpread){
  41. this.resultSpread.refresh();
  42. }
  43. },
  44. loadStdRationLibs: function () {
  45. let select = $('#stdRationLibSelect');
  46. select.empty();
  47. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  48. ration_lib.push({
  49. isDefault: false,
  50. id: rationLibObj.compleRationLibId,
  51. name: '我的补充定额'
  52. });
  53. let selectedRationLib = sessionStorage.getItem('stdRationLib');
  54. ration_lib.forEach(function (data) {
  55. let option = $('<option>').val(data.id).text(data.name);
  56. if(selectedRationLib){
  57. if(data.id == selectedRationLib){
  58. option.attr('selected', 'selected');
  59. }
  60. }else if(data.isDefault == true){
  61. option.attr('selected', 'selected');
  62. }
  63. select.append(option);
  64. });
  65. //我的补充定额库
  66. /* let $opt = $('<option>').val(rationLibObj.compleRationLibId).text('我的补充定额');
  67. select.append($opt);*/
  68. if (select[0].options.length !== 0) {
  69. rationLibObj.loadStdRation(select.val());
  70. }
  71. },
  72. initQuestionModal: function(row) {
  73. let node = rationLibObj.tree.items[row];
  74. while (node && !node.data.explanation){
  75. node = node.parent;
  76. }
  77. let explanation = node && node.data.explanation ? node.data.explanation : '无内容';
  78. node = rationLibObj.tree.items[row];
  79. $('#questionContent1').html(explanation);
  80. while (node && !node.data.ruleText){
  81. node = node.parent;
  82. }
  83. let ruleText = node && node.data.ruleText ? node.data.ruleText : '无内容';
  84. $('#questionTab1').text('说明');
  85. $('#questionTab2').text('工程量计算规则');
  86. $('#questionContent2').html(ruleText);
  87. $('#questionModal').modal('show');
  88. },
  89. hasExplanationRuleText: function(row) {
  90. let node = rationLibObj.tree.items[row];
  91. if (!node) {
  92. return false;
  93. }
  94. while (node) {
  95. if (node.data.explanation || node.data.ruleText) {
  96. return true;
  97. }
  98. node = node.parent;
  99. }
  100. return false;
  101. },
  102. loadStdRation: function (rationLibID) {
  103. $.bootstrapLoading.start();
  104. rationLibObj.curLibType = rationLibID === rationLibObj.compleRationLibId ? rationLibObj.libType.complementary : rationLibObj.libType.std;
  105. var that = this;
  106. var showRationChapterTree = function (datas) {
  107. var rationChapterTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  108. that.tree = rationChapterTree;
  109. var rationChapterTreeController = TREE_SHEET_CONTROLLER.createNew(rationChapterTree, that.rationChapterSpread.getActiveSheet(), that.rationChapterTreeSetting);
  110. sheetCommonObj.setColumnWidthByRate($('#stdRationChapter').width() - 40, that.rationChapterSpread, that.rationChapterTreeSetting.cols);
  111. rationChapterTree.loadDatas(datas);
  112. //读取展开收起状态
  113. let currentExpState = sessionStorage.getItem('stdRationLibExpState');
  114. if(currentExpState){
  115. that.tree.setExpandedByState(that.tree.items, currentExpState);
  116. }
  117. else {
  118. //展开至第二层
  119. for(let root of that.tree.roots){
  120. root.setExpanded(true);
  121. that.tree.setRootExpanded(root.children, false);
  122. }
  123. }
  124. rationChapterTreeController.showTreeData();
  125. rationChapterTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  126. rationLibObj.loadSectionRations(node && node.children.length === 0 ? node.getID() : null);
  127. });
  128. if (rationChapterTree.firstNode() && rationChapterTree.firstNode().length === 0) {
  129. rationLibObj.loadSectionRations(rationChapterTree.firstNode().getID());
  130. } else {
  131. rationLibObj.loadSectionRations();
  132. };
  133. };
  134. //type: 0-补充库 1-标准库
  135. CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID, type: rationLibObj.curLibType}, function (datas) {
  136. showRationChapterTree(datas);
  137. if(that.doAfterGetRationTree){
  138. that.doAfterGetRationTree();
  139. }
  140. $.bootstrapLoading.end();
  141. }, function () {
  142. showRationChapterTree([]);
  143. $.bootstrapLoading.end();
  144. });
  145. },
  146. //双击隐藏显示
  147. onChapterSpreadCellDoubleClick: function (sender, args) {
  148. let me = rationLibObj;
  149. let node = me.tree.items[args.row];
  150. if (!node || node.children.length === 0)
  151. return;
  152. node.setExpanded(!node.expanded);
  153. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  154. TREE_SHEET_HELPER.massOperationSheet(args.sheet, function () {
  155. let iCount = node.posterityCount(), i, child;
  156. for (i = 0; i < iCount; i++) {
  157. child = me.tree.items[args.row + i + 1];
  158. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  159. }
  160. args.sheet.invalidateLayout();
  161. });
  162. args.sheet.repaint();
  163. },
  164. setTagForHint: function (sheet, datas) {
  165. sheet.suspendPaint();
  166. sheet.suspendEvent();
  167. for(let i = 0, len = sheet.getRowCount(); i < len; i++){
  168. sheet.setTag(i, 0, '');
  169. }
  170. for(let i = 0, len = datas.length; i < len; i++){
  171. sheet.setTag(i, 0, datas[i].hint ? datas[i].hint : '');
  172. }
  173. sheet.resumePaint();
  174. sheet.resumeEvent();
  175. },
  176. loadSectionRations: function (sectionID) {
  177. let me = this;
  178. var showDatas = function (datas, setting) {
  179. let rationSheet = rationLibObj.sectionRationsSpread.getActiveSheet();
  180. TREE_SHEET_HELPER.massOperationSheet(rationSheet, function () {
  181. rationSheet.setColumnWidth(0, 25, GC.Spread.Sheets.SheetArea.rowHeader);
  182. });
  183. SheetDataHelper.loadSheetHeader(setting, rationLibObj.sectionRationsSpread.getActiveSheet());
  184. SheetDataHelper.loadSheetData(setting, rationLibObj.sectionRationsSpread.getActiveSheet(), datas);
  185. rationLibObj.setTagForHint(rationSheet, datas);
  186. };
  187. //定额名称的处理:
  188. /*
  189. * 1、从定额库提取的名称,是否含有空格:
  190. * 1.1、无,则不处理。
  191. * 1.2、有,则取第一个空格前的文本,与定额所属节点名称(去掉前面和后面的编号、括号、空格,保留中间的中文及符号)比较是否相同:
  192. * 1.2.1、不同,则不处理。
  193. * 1.2.2、相同,则将定额名称显示为去除第一个空格及空格之前的文本。
  194. */
  195. //@param {String}sectionName(章节名称) {Array}datas(定额数据)
  196. function simplifyName(sectionName, datas){
  197. if (!sectionName || !datas || datas.length === 0) {
  198. return;
  199. }
  200. //提取需要匹配的章节名称
  201. //去掉前缀
  202. let toMatchArr = sectionName.split(' '),
  203. toMatchStr = toMatchArr[toMatchArr.length - 1];
  204. //去掉后缀
  205. let sectionReg = /\(\w{9,}\)/g,
  206. regMatch = toMatchStr.match(sectionReg);
  207. if (regMatch) {
  208. toMatchStr = toMatchStr.replace(regMatch[regMatch.length - 1], '');
  209. }
  210. //简化匹配到的定额名称
  211. for (let data of datas) {
  212. //第一个空格前的字符串去进行匹配,没有则不匹配
  213. let nameArr = data.name.split(' ');
  214. if (nameArr.length <= 1) {
  215. continue;
  216. }
  217. let matchName = nameArr[0];
  218. if (matchName === toMatchStr) {
  219. nameArr.shift();
  220. data.name = nameArr.join(' ');
  221. }
  222. }
  223. }
  224. if (sectionID) {
  225. CommonAjax.post('/complementaryRation/api/getRationGljItemsBySection', {user_Id: userID, sectionId: sectionID, type: me.curLibType}, function (datas) {
  226. let chapterSheet = me.rationChapterSpread.getActiveSheet();
  227. let sectionName = chapterSheet.getText(chapterSheet.getActiveRowIndex(), chapterSheet.getActiveColumnIndex());
  228. simplifyName(sectionName, datas);
  229. showDatas(datas, rationLibObj.sectionRationsSetting);
  230. if(me.doAfterLoadGetRations){
  231. me.doAfterLoadGetRations(datas);
  232. me.doAfterLoadGetRations = null;
  233. }
  234. }, function () {
  235. showDatas([], rationLibObj.sectionRationsSetting);
  236. });
  237. } else {
  238. showDatas([], rationLibObj.sectionRationsSetting);
  239. }
  240. },
  241. onRationSpreadCellDoubleClick: function (sender, args) {
  242. var select = $('#stdRationLibSelect'), rationCode = args.sheet.getText(args.row, 0);
  243. if (rationCode !== '') {
  244. let query = {userID: userID, rationRepId: select.val(), code: rationCode};
  245. //搜索结果全部定额中双击添加定额、有可能同名不同库,更新查询的库ID
  246. if (rationLibObj.resultCache && rationLibObj.resultCache[args.row]) {
  247. query.rationRepId = rationLibObj.resultCache[args.row].type === 'std' ? rationLibObj.resultCache[args.row].rationRepId : rationLibObj.compleRationLibId;
  248. }
  249. projectObj.project.Ration.addNewRation(query,rationType.ration, function () {
  250. projectObj.setActiveCell('quantity', true);
  251. });
  252. }
  253. },
  254. //滚动条到底部加载
  255. onRationSpreadTopRowChanged: function (sender, args) {
  256. let me = rationLibObj;
  257. if(me.searching) {
  258. return;
  259. }
  260. let bottomRow = args.sheet.getViewportBottomRow(1),
  261. rowCount = args.sheet.getRowCount();
  262. //滚到了底部
  263. if (bottomRow + 1 - me.sectionRationsSetting.emptyRows === rowCount - me.sectionRationsSetting.emptyRows) {
  264. seachRation();
  265. }
  266. },
  267. loadStdRationContextMenu: function () {
  268. let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(), rationModel = projectObj.project.Ration;;
  269. $.contextMenu({
  270. selector: '#stdSectionRations',
  271. build: function ($trigger, e) {
  272. let target = SheetDataHelper.safeRightClickSelection($trigger, e, rationSpread);
  273. return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  274. },
  275. items: {
  276. "insertStdRation": {
  277. name: "插入定额",
  278. icon: 'fa-sign-in',
  279. callback: function (key, opt) {
  280. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  281. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  282. if (rationCode !== '') {
  283. rationModel.addNewRation({userID: userID, rationRepId: select.val(), code: rationCode},rationType.ration, function () {
  284. });
  285. }
  286. }
  287. },
  288. "replaceStdRation": {
  289. name: "替换定额",
  290. icon: 'fa-sign-in',
  291. callback: function (key, opt) {
  292. let select = $('#stdRationLibSelect'), rationSelect = rationSheet.getSelections();
  293. let rationCode = rationSelect.length > 0 ? rationSheet.getText(rationSelect[0].row, 0) : '';
  294. let mainTreeSelected = projectObj.project.mainTree.selected;
  295. if (rationCode !== ''&&mainTreeSelected&&mainTreeSelected.sourceType == rationModel.getSourceType()) {
  296. rationModel.updateRationCodes([{'node':mainTreeSelected,value:rationCode}]);
  297. }
  298. }
  299. },
  300. }
  301. });
  302. },
  303. expandSearchNodes: function(nodes){
  304. let that = rationLibObj;
  305. TREE_SHEET_HELPER.massOperationSheet(that.rationChapterSpread.getActiveSheet(), function () {
  306. function expParentNode(node){
  307. if(node.parent){
  308. expParentNode(node.parent);
  309. if(!node.parent.expanded){
  310. node.parent.setExpanded(true);
  311. }
  312. }
  313. }
  314. for(let node of nodes){
  315. expParentNode(node);
  316. }
  317. TREE_SHEET_HELPER.refreshTreeNodeData(that.rationChapterTreeSetting, that.rationChapterSpread.getActiveSheet(), that.tree.roots, true);
  318. TREE_SHEET_HELPER.refreshNodesVisible(that.tree.roots, that.rationChapterSpread.getActiveSheet(), true);
  319. });
  320. },
  321. initSel: function (row) {
  322. let me = this;
  323. let sheet = me.rationChapterSpread.getActiveSheet();
  324. sheet.setActiveCell(row, 0);
  325. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  326. let sectionNode = me.tree.items[row] || null;
  327. me.loadSectionRations(sectionNode && sectionNode.children.length === 0 ? sectionNode.data.ID : null);
  328. },
  329. locateAtRation: function(libID, code){
  330. let me = rationLibObj;
  331. //查找定额,以确定定额所在章节节点
  332. let firstLibID = projectInfoObj.projectInfo.engineeringInfo.ration_lib.length > 0 ?
  333. projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id : null;
  334. let locateRow = 0,
  335. locateSubRow = 0;
  336. CommonAjax.post('/complementaryRation/api/getRationItem', {rationRepIds: [libID], code: code}, function (ration) {
  337. if(ration && ration.sectionId){
  338. let sectionNode = me.tree.findNode(ration.sectionId);
  339. if(sectionNode){
  340. me.expandSearchNodes([sectionNode]);
  341. sessionStorage.setItem('stdRationLibExpState', me.tree.getExpState(me.tree.items));
  342. }
  343. locateRow = sectionNode.serialNo();
  344. me.doAfterLoadGetRations = function (rationItems) {
  345. let rationSheet = me.sectionRationsSpread.getActiveSheet();
  346. locateSubRow = _.findIndex(rationItems, {ID: ration.ID});
  347. rationSheet.setActiveCell(locateSubRow, 0);
  348. rationSheet.showRow(locateSubRow, GC.Spread.Sheets.VerticalPosition.center);
  349. };
  350. me.initSel(locateRow);
  351. }
  352. else {
  353. me.initSel(locateRow);
  354. }
  355. }, function () {
  356. me.initSel(locateRow);
  357. });
  358. },
  359. rationChapterTreeSetting: {
  360. "emptyRowHeader": true,
  361. "rowHeaderWidth": 15,
  362. "emptyRows":0,
  363. "headRows":1,
  364. "headRowHeight":[30],
  365. "defaultRowHeight": 21,
  366. "treeCol": 0,
  367. "cols":[{
  368. "rateWidth": 1,
  369. "width":400,
  370. "readOnly": true,
  371. "head":{
  372. "titleNames":["名称"],
  373. "spanCols":[1],
  374. "spanRows":[1],
  375. "vAlign":[1],
  376. "hAlign":[1],
  377. "font":["Arial"]
  378. },
  379. "data":{
  380. "field":"name",
  381. "vAlign":1,
  382. "hAlign":0,
  383. "font":"Arial"
  384. }
  385. }]
  386. },
  387. sectionRationsSetting: {
  388. "emptyRows":3,
  389. "headRows":1,
  390. "headRowHeight":[20],
  391. "defaultRowHeight": 21,
  392. "cols":[{
  393. "width":60,
  394. "readOnly": true,
  395. "showHint": true,
  396. "head":{
  397. "titleNames":["编码"],
  398. "spanCols":[1],
  399. "spanRows":[1],
  400. "vAlign":[1],
  401. "hAlign":[1],
  402. "font":["Arial"]
  403. },
  404. "data":{
  405. "field":"code",
  406. "vAlign":1,
  407. "hAlign":0,
  408. "font":"Arial"
  409. }
  410. }, {
  411. "width":220,
  412. "readOnly": true,
  413. "showHint": true,
  414. "head":{
  415. "titleNames":["名称"],
  416. "spanCols":[1],
  417. "spanRows":[1],
  418. "vAlign":[1],
  419. "hAlign":[1],
  420. "font":["Arial"]
  421. },
  422. "data":{
  423. "field":"name",
  424. "vAlign":1,
  425. "hAlign":0,
  426. "font":"Arial"
  427. }
  428. }, {
  429. "width":55,
  430. "readOnly":true,
  431. "head":{
  432. "titleNames":["单位"],
  433. "spanCols":[1],
  434. "spanRows":[1],
  435. "vAlign":[1],
  436. "hAlign":[1],
  437. "font":["Arial"]
  438. },
  439. "data":{
  440. "field":"unit",
  441. "vAlign":1,
  442. "hAlign":1,
  443. "font":"Arial"
  444. }
  445. }, {
  446. "width":60,
  447. "readOnly":true,
  448. "head":{
  449. "titleNames":["基价"],
  450. "spanCols":[1],
  451. "spanRows":[1],
  452. "vAlign":[1],
  453. "hAlign":[1],
  454. "font":["Arial"]
  455. },
  456. "data":{
  457. "field":"basePrice",
  458. "vAlign":1,
  459. "hAlign":2,
  460. "font":"Arial"
  461. }
  462. }]
  463. },
  464. getStdRationLibIDs: function () {
  465. let ids = [];
  466. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  467. alert('当前项目无定额库,请添加定额库。');
  468. return null;
  469. }
  470. for(let rationLib of projectInfoObj.projectInfo.engineeringInfo.ration_lib){
  471. ids.push(rationLib.id);
  472. }
  473. return ids;
  474. },
  475. getCurrentStdRationLibID:function () {
  476. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  477. alert('当前项目无定额库,请添加定额库。');
  478. return null;
  479. }
  480. if($('#stdRationLibSelect').val()){
  481. return parseInt($('#stdRationLibSelect').val());
  482. }else {
  483. return projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id;
  484. }
  485. },
  486. getFirstStdRationLibID: function () {
  487. if(projectInfoObj.projectInfo.engineeringInfo.ration_lib.length === 0){
  488. alert('当前项目无定额库,请添加定额库。');
  489. return null;
  490. }
  491. return parseInt(projectInfoObj.projectInfo.engineeringInfo.ration_lib[0].id);
  492. },
  493. getDefaultStdRationLibID:function(){
  494. let ration_lib = projectInfoObj.projectInfo.engineeringInfo.ration_lib;
  495. if(ration_lib.length === 0){
  496. alert('当前项目无定额库,请添加定额库。');
  497. return null;
  498. }
  499. let defaultLib = _.find(ration_lib,{'isDefault':true});
  500. let libID = defaultLib?defaultLib.id:ration_lib[0].id;
  501. return parseInt(libID);
  502. },
  503. //@param {Array}datas(resultCache) @return {Object}
  504. //搜索skip信息,不能被每页搜索数整除,则说明上次搜索已经搜索完整
  505. getSearchSkip: function (datas) {
  506. if (datas.length % this.searchLimit !== 0) {
  507. return null;
  508. }
  509. let skip = {std: 0, comple: 0};
  510. if (!datas || !Array.isArray(datas) || datas.length === 0) {
  511. return skip;
  512. }
  513. for (let data of datas) {
  514. if (data.type === 'std') {
  515. skip.std++;
  516. } else {
  517. skip.comple++;
  518. }
  519. }
  520. return skip;
  521. }
  522. };
  523. addEventOnResize(rationLibObj.refreshSettingForHint);
  524. //赋初始高度
  525. if($('#stdRationChapter').height() === 0 || $('#stdSectionRations').height() === 0){
  526. $('#stdRationChapter').height($(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-q").height()-312);
  527. $('#stdSectionRations').height(270);
  528. }
  529. $('#stdRationTab').bind('click', function () {
  530. if(!projectReadOnly){
  531. var select = $('#stdRationLibSelect');
  532. rationLibObj.checkSpread();
  533. if (select[0].options.length === 0) {
  534. rationLibObj.loadStdRationLibs();
  535. rationLibObj.loadStdRationContextMenu();
  536. };
  537. }
  538. });
  539. $('#stdRationLibSelect').change(function () {
  540. var select = $(this);
  541. if (this.children.length !== 0) {
  542. let rationLibId = select.val();
  543. sessionStorage.setItem('stdRationLib', rationLibId);
  544. sessionStorage.removeItem('stdRationLibExpState');
  545. rationLibObj.loadStdRation(rationLibId);
  546. }
  547. });
  548. //回车键搜索
  549. $('#rationSearchKeyword').bind('keypress', function (event) {
  550. if(event.keyCode === 13){
  551. $(this).blur();
  552. $('#rationSearch').click();
  553. }
  554. });
  555. $('#rationSearchKeyword').keyup(function () {
  556. let keyword = $('#rationSearchKeyword').val();
  557. if(keyword === ''){
  558. if($('#rationSearchResult').is(':visible')){
  559. rationLibObj.resultCache = [];
  560. $('#rationSearchResult').hide();
  561. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  562. switchRationSearchMode(0);
  563. autoFlashHeight();
  564. rationLibObj.refreshSpread();
  565. }
  566. }
  567. });
  568. //变换搜索本定额、全部定额状态
  569. function switchRationSearchMode(mode) {
  570. rationLibObj.resultCache = [];
  571. //搜索本定额
  572. if(mode === 0){
  573. $('#curRationLib').removeClass('btn-light');
  574. $('#curRationLib').addClass('btn-secondary');
  575. $('#allRationLibs').removeClass('btn-secondary');
  576. $('#allRationLibs').addClass('btn-light');
  577. } else {//搜索全部定额
  578. $('#allRationLibs').removeClass('btn-light');
  579. $('#allRationLibs').addClass('btn-secondary');
  580. $('#curRationLib').removeClass('btn-secondary');
  581. $('#curRationLib').addClass('btn-light');
  582. }
  583. }
  584. //搜索本定额
  585. $('#curRationLib').click(function () {
  586. if($(this).hasClass('btn-secondary')){
  587. return;
  588. }
  589. switchRationSearchMode(0);
  590. $('#rationSearch').click();
  591. });
  592. //搜索全部定额
  593. $('#allRationLibs').click(function () {
  594. if($(this).hasClass('btn-secondary')){
  595. return;
  596. }
  597. switchRationSearchMode(1);
  598. $('#rationSearch').click();
  599. });
  600. //搜索
  601. function seachRation(){
  602. let skip = rationLibObj.getSearchSkip(rationLibObj.resultCache);
  603. if (!skip) {
  604. return;
  605. }
  606. rationLibObj.searching = true;
  607. var keyword = $('#rationSearchKeyword').val();
  608. if(keyword === ''){
  609. if($('#rationSearchResult').is(':visible')){
  610. rationLibObj.resultCache = [];
  611. $('#rationSearchResult').hide();
  612. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  613. autoFlashHeight();
  614. rationLibObj.refreshSpread();
  615. }
  616. return;
  617. }
  618. //获取搜索定额的库:本库/所有库
  619. let rationLibIDs = [];
  620. if($('#curRationLib').hasClass('btn-secondary')){
  621. rationLibIDs.push($('#stdRationLibSelect').val());
  622. } else {
  623. for(let lib of projectInfoObj.projectInfo.engineeringInfo.ration_lib){
  624. rationLibIDs.push(lib.id);
  625. }
  626. }
  627. let bindContextmenuOpr = function (sheet) {
  628. $.contextMenu({
  629. selector: '#rationSearchResult',
  630. build: function($triggerElement, e){
  631. //控制允许右键菜单在哪个位置出现
  632. let offset = $('.main-data-side-search').offset(),
  633. x = e.pageX - offset.left,
  634. y = e.pageY - offset.top;
  635. let target = sheet.hitTest(x, y);
  636. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  637. sheet.setActiveCell(target.row, target.col);
  638. return {
  639. callback: function(){},
  640. items: {
  641. "locate": {
  642. name: "定位至章节",
  643. disabled: function () {
  644. return target.row >= rationLibObj.resultCache.length;
  645. },
  646. icon: "fa-arrow-left",
  647. callback: function (key, opt) {
  648. let data = rationLibObj.resultCache[target.row],
  649. libId = data.rationRepId ? data.rationRepId : rationLibObj.compleRationLibId;
  650. $('#rationSearchResult').hide();
  651. $(".main-data-side-search", $('#rationSearchResult')).height(0);
  652. autoFlashHeight();
  653. rationLibObj.refreshSpread();
  654. switchRationSearchMode(0);
  655. if($('#stdRationLibSelect').select().val() != libId){
  656. let libOpts = $('#stdRationLibSelect').find('option');
  657. for(let libOpt of libOpts){
  658. if($(libOpt).val() == libId){
  659. $(libOpt).prop('selected', 'selected');
  660. break;
  661. }
  662. }
  663. $('#stdRationLibSelect').change();
  664. rationLibObj.doAfterGetRationTree = function () {
  665. this.locateAtRation(libId, data.code);
  666. this.doAfterGetRationTree = null;
  667. };
  668. } else {
  669. rationLibObj.locateAtRation(libId, data.code);
  670. }
  671. }}
  672. }
  673. };
  674. }
  675. else{
  676. return false;
  677. }
  678. }
  679. });
  680. };
  681. var showResult = function (result) {
  682. if(!rationLibObj.resultSpread){
  683. let resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
  684. rationLibObj.resultSpread = resultSpread;
  685. bindContextmenuOpr(resultSpread.getActiveSheet());
  686. SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
  687. resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
  688. resultSpread.bind(GC.Spread.Sheets.Events.TopRowChanged, rationLibObj.onRationSpreadTopRowChanged);
  689. }else {
  690. rationLibObj.resultSpread.refresh();
  691. }
  692. SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, rationLibObj.resultSpread.getActiveSheet(), result);
  693. rationLibObj.setTagForHint(rationLibObj.resultSpread.getActiveSheet(), result);
  694. rationLibObj.resultCache = result;
  695. };
  696. $.bootstrapLoading.start();
  697. CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibIDs, 'keyword': keyword, skip: skip}, function (result) {
  698. var resultObj = $('#rationSearchResult');
  699. if (result.count !== null) {
  700. $('#rationSearchCount').text(`搜索结果:${result.count}`);
  701. }
  702. $('a', resultObj).unbind('click');
  703. $('a', resultObj).bind('click', function () {
  704. rationLibObj.resultCache = [];
  705. switchRationSearchMode(0);
  706. resultObj.hide();
  707. $(".main-data-side-search", resultObj).height(0);
  708. autoFlashHeight();
  709. rationLibObj.refreshSpread();
  710. });
  711. resultObj.show();
  712. $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
  713. showResult(rationLibObj.resultCache.concat(result.data));
  714. rationLibObj.searching = false;
  715. //以防一开始就需要加载后面的分页数据
  716. if (result.data.length > 0) {
  717. rationLibObj.onRationSpreadTopRowChanged({}, {sheet: rationLibObj.resultSpread.getActiveSheet()});
  718. }
  719. $.bootstrapLoading.end();
  720. }, function () {
  721. rationLibObj.searching = false;
  722. $.bootstrapLoading.end();
  723. });
  724. }
  725. $('#rationSearch').click(function () {
  726. rationLibObj.resultCache = [];
  727. seachRation();
  728. });