std_ration_lib.js 31 KB

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