std_ration_lib.js 31 KB

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