cs_tools.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. 'use strict';
  2. /**
  3. * cs_errorList:错误列表
  4. * 使用范围:
  5. * 台账分解(原报)、台账修订(原报)、计量台账(所有角色)
  6. *
  7. * posSearch & billsSearch:台账搜索相关
  8. * 使用范围:
  9. * 0号台账:台账分解、台账审批、台账修订、部位台账;
  10. * 期计量:计量台账、部位台账
  11. *
  12. * 所有工具均基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  13. *
  14. * @author Mai
  15. * @date
  16. * @version
  17. */
  18. const showSideTools = function (show) {
  19. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  20. if (show) {
  21. right.show();
  22. autoFlashHeight();
  23. /**
  24. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  25. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  26. * 故需要通过最终的parent.width再计算一次left.width
  27. *
  28. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  29. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  30. *
  31. */
  32. //left.css('width', parent.width() - right.outerWidth());
  33. //left.css('width', parent.width() - right.outerWidth());
  34. const percent = 100 - right.outerWidth() /parent.width() * 100;
  35. left.css('width', percent + '%');
  36. } else {
  37. left.width(parent.width());
  38. right.hide();
  39. }
  40. };
  41. const showSelectTab = function(select, spread, afterShow) {
  42. const tab = $(select), tabPanel = $(tab.attr('content'));
  43. $('a', '#side-menu').removeClass('active');
  44. tab.addClass('active');
  45. $('.tab-content .tab-pane').removeClass('active');
  46. tabPanel.addClass('active');
  47. showSideTools(true);
  48. spread && spread.refresh();
  49. if (afterShow) afterShow();
  50. };
  51. (function($){
  52. /**
  53. * 错误列表
  54. * @param setting
  55. * {
  56. * tabSelector: 'a[content=#error-list]',
  57. * selector: '#error-list',
  58. * relaSpread: ledgerSpread,
  59. * storeKey: 'ledger-error-' + tenderId,
  60. * }
  61. * @returns {{spread: *}}
  62. */
  63. $.cs_errorList = function (setting) {
  64. if (!setting.spreadSetting) {
  65. setting.spreadSetting = {
  66. cols: [
  67. {title: '行号', field: 'serialNo', width: 50, formatter: '@'},
  68. {
  69. title: '错误类型', field: 'errorType', width: 60, formatter: '@',
  70. getValue: function (x) {
  71. switch (x.errorType) {
  72. case 'qty': return '数量';
  73. case 'tp': return '金额';
  74. case 'over': return '超计';
  75. case 'sibling': return '项目节清单同层';
  76. case 'same_code': return '重复项目节编号';
  77. case 's2b_over_gxby': return '违规计量(工序报验)';
  78. case 's2b_over_dagl': return '违规计量(档案管理)';
  79. case 's2b_lost_gxby': return '遗漏计量(工序报验)';
  80. case 's2b_lost_dagl': return '遗漏计量(档案管理)';
  81. default: return '';
  82. }
  83. }
  84. },
  85. {title: '清单编号', field: 'b_code', width: 135, formatter: '@'},
  86. {title: '清单名称', field: 'name', width: 215, formatter: '@'},
  87. ],
  88. emptyRows: 0,
  89. headRows: 1,
  90. headRowHeight: [32],
  91. defaultRowHeight: 21,
  92. headerFont: '12px 微软雅黑',
  93. font: '12px 微软雅黑',
  94. selectedBackColor: '#fffacd',
  95. readOnly: true,
  96. };
  97. }
  98. const clearErrorData = function () {
  99. if (setting.storeKey) removeLocalCache(setting.storeKey);
  100. };
  101. const autoShowHistory = function (show) {
  102. if (setting.storeKey) {
  103. setLocalCache(setting.storeKey + '-showHis', show.toString());
  104. }
  105. };
  106. if (setting.selector && setting.relaSpread) {
  107. const resultId = setting.id + '-spread';
  108. const obj = $(setting.selector);
  109. obj.html(
  110. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  111. ' </div>'
  112. );
  113. autoFlashHeight();
  114. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  115. const sheet = spread.getActiveSheet();
  116. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  117. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  118. const sheet = info.sheet;
  119. const data = sheet.zh_data;
  120. if (!data) { return }
  121. const curBills = data[info.row];
  122. if (!curBills) { return }
  123. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  124. if (setting.afterLocated) {
  125. setting.afterLocated();
  126. }
  127. });
  128. const loadErrorData = function (data, his = false) {
  129. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  130. if (!sourceTree) return;
  131. for (const d of data) {
  132. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  133. }
  134. data.sort(function (a, b) {
  135. return a.serialNo - b.serialNo;
  136. });
  137. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  138. if (!his && setting.storeKey) {
  139. setLocalCache(setting.storeKey, JSON.stringify(data));
  140. }
  141. $(setting.tabSelector).show();
  142. };
  143. const showErrorList = function () {
  144. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  145. $('a', '#side-menu').removeClass('active');
  146. tab.addClass('active');
  147. $('.tab-content .tab-pane').removeClass('active');
  148. tabPanel.addClass('active');
  149. showSideTools(true);
  150. spread.refresh();
  151. if (setting.afterShow) setting.afterShow();
  152. };
  153. const loadHisErrorData = function () {
  154. if (setting.storeKey) {
  155. const storeStr = getLocalCache(setting.storeKey);
  156. const storeData = storeStr ? JSON.parse(storeStr) : [];
  157. if (storeData.length > 0) {
  158. loadErrorData(storeData, true);
  159. const showHis = getLocalCache(setting.storeKey + '-showHis');
  160. if (showHis === 'true') {
  161. showErrorList();
  162. removeLocalCache(setting.storeKey + '-showHis');
  163. }
  164. }
  165. }
  166. };
  167. return {
  168. spread: spread,
  169. loadErrorData: loadErrorData,
  170. clearErrorData: clearErrorData,
  171. loadHisErrorData: loadHisErrorData,
  172. show: showErrorList,
  173. autoShowHistory: autoShowHistory,
  174. };
  175. } else {
  176. const loadErrorData = function (data) {
  177. if (setting.storeKey) {
  178. setLocalCache(setting.storeKey, JSON.stringify(data));
  179. }
  180. };
  181. return {
  182. loadErrorData: loadErrorData,
  183. clearErrorData: clearErrorData,
  184. autoShowHistory: autoShowHistory,
  185. };
  186. }
  187. };
  188. $.ledger_checkList = function (setting) {
  189. const checkTypeText = [];
  190. for (const ct in setting.checkType) {
  191. checkTypeText[setting.checkType[ct].value] = setting.checkType[ct].text;
  192. }
  193. if (!setting.spreadSetting) {
  194. setting.spreadSetting = {
  195. cols: [
  196. {
  197. title: '类型', field: 'type', width: 150, formatter: '@',
  198. getValue: function (data){
  199. if (setting.checkType) {
  200. return checkTypeText[data.type] || '';
  201. } else {
  202. return '';
  203. }
  204. }
  205. },
  206. {title: '行号', field: 'serialNo', hAlign: 1, width: 40, formatter: '@'},
  207. {title: '项目节编号', field: 'code', width: 80, formatter: '@'},
  208. {title: '清单编号', field: 'b_code', width: 80, formatter: '@'},
  209. {title: '名称', field: 'name', width: 150, formatter: '@'},
  210. ],
  211. emptyRows: 0,
  212. headRows: 1,
  213. headRowHeight: [32],
  214. defaultRowHeight: 21,
  215. headerFont: '12px 微软雅黑',
  216. font: '12px 微软雅黑',
  217. selectedBackColor: '#fffacd',
  218. readOnly: true,
  219. };
  220. }
  221. const clearCheckData = function () {
  222. if (setting.storeKey) removeLocalCache(setting.storeKey);
  223. };
  224. const autoShowHistory = function (show) {
  225. if (setting.storeKey) {
  226. setLocalCache(setting.storeKey + '-showHis', show.toString());
  227. }
  228. };
  229. if (setting.selector && setting.relaSpread) {
  230. const resultId = setting.id + '-spread';
  231. const obj = $(setting.selector);
  232. const dropdown = [];
  233. if (setting.checkType) {
  234. dropdown.push('<div class="dropdown">');
  235. dropdown.push('<button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" id="'+ setting.id + 'drop" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">所有类型</button>');
  236. dropdown.push('<div class="dropdown-menu" aria-labelledby="'+ setting.id + 'drop">');
  237. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="all">所有类型</a>');
  238. for (const ct in setting.checkType) {
  239. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="' + setting.checkType[ct].value +'">' + setting.checkType[ct].text + '</a>');
  240. }
  241. dropdown.push('</div>');
  242. dropdown.push('</div>');
  243. }
  244. obj.html(
  245. '<div class="sjs-bar">\n' +
  246. ' <div class="pb-1 d-flex">\n' + dropdown.join('') +
  247. ' <span class="ml-auto pr-2" id="' + setting.id + '-time">检查时间:2020-08-01 13:20:25</span>\n' +
  248. ' </div>\n' +
  249. '</div>' +
  250. '<div id="' + resultId + '" class="sjs-sh">\n' +
  251. '</div>'
  252. );
  253. autoFlashHeight();
  254. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  255. const sheet = spread.getActiveSheet();
  256. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  257. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  258. const sheet = info.sheet;
  259. const data = sheet.zh_data;
  260. if (!data) { return }
  261. const curBills = data[info.row];
  262. if (!curBills) { return }
  263. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  264. if (setting.afterLocated) {
  265. setting.afterLocated();
  266. }
  267. });
  268. const filterCheckData = function () {
  269. const filter = $(this).attr('check-type');
  270. $('#' + setting.id + 'drop').html(this.innerHTML);
  271. for (const d of sheet.zh_data) {
  272. if (filter === 'all') {
  273. d.visible = true;
  274. } else {
  275. d.visible = d.type == filter;
  276. }
  277. }
  278. SpreadJsObj.refreshTreeRowVisible(sheet);
  279. };
  280. $('a[check-type]').bind('click', filterCheckData);
  281. const hideCheckData = function () {
  282. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  283. if (tab.hasClass('active')) {
  284. $('a', '#side-menu').removeClass('active');
  285. tab.addClass('active');
  286. $('.tab-content .tab-pane').removeClass('active');
  287. tabPanel.addClass('active');
  288. showSideTools(false);
  289. if (spread) spread.refresh();
  290. if (setting.afterShow) setting.afterShow();
  291. tab.hide();
  292. }
  293. };
  294. const loadCheckData = function (data, his = false) {
  295. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  296. if (!sourceTree) return;
  297. for (const d of data.warning_data) {
  298. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  299. }
  300. $('#' + setting.id + '-time').html('检查时间:' + moment(data.check_time).format('YYYY-MM-DD HH:mm:ss'));
  301. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data.warning_data);
  302. if (!his && setting.storeKey) {
  303. setLocalCache(setting.storeKey, JSON.stringify(data));
  304. }
  305. $(setting.tabSelector).show();
  306. };
  307. const showCheckList = function () {
  308. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  309. $('a', '#side-menu').removeClass('active');
  310. tab.addClass('active');
  311. $('.tab-content .tab-pane').removeClass('active');
  312. tabPanel.addClass('active');
  313. showSideTools(true);
  314. spread.refresh();
  315. if (setting.afterShow) setting.afterShow();
  316. };
  317. const loadHisCheckData = function () {
  318. if (setting.storeKey) {
  319. const storeStr = getLocalCache(setting.storeKey);
  320. const storeData = storeStr ? JSON.parse(storeStr) : null;
  321. if (storeData) {
  322. loadCheckData(storeData, true);
  323. const showHis = getLocalCache(setting.storeKey + '-showHis');
  324. if (showHis === 'true') {
  325. showCheckList();
  326. removeLocalCache(setting.storeKey + '-showHis');
  327. }
  328. }
  329. }
  330. };
  331. return {
  332. spread: spread,
  333. loadCheckData: loadCheckData,
  334. clearCheckData: clearCheckData,
  335. loadHisCheckData: loadHisCheckData,
  336. show: showCheckList,
  337. hide: hideCheckData,
  338. autoShowHistory: autoShowHistory,
  339. };
  340. } else {
  341. const loadCheckData = function (data) {
  342. if (setting.storeKey) {
  343. setLocalCache(setting.storeKey, JSON.stringify(data));
  344. }
  345. };
  346. return {
  347. loadCheckData: loadCheckData,
  348. clearCheckData: clearCheckData,
  349. autoShowHistory: autoShowHistory,
  350. };
  351. }
  352. };
  353. $.posSearch = function (setting) {
  354. if (!setting.selector || !setting.searchSpread) return;
  355. const searchHtml =
  356. ' <div class="ml-2">\n' +
  357. ' <div class="input-group input-group-sm">\n' +
  358. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  359. ' <div class="input-group-append">\n' +
  360. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  361. ' </div>\n' +
  362. ' <div class="input-group-append" >\n' +
  363. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  364. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  365. ' </div>\n' +
  366. ' </div>\n' +
  367. ' </div>\n';
  368. $(setting.selector).html(searchHtml);
  369. const sheet = setting.searchSpread.getActiveSheet();
  370. const searchObj = (function () {
  371. let resultArr = [];
  372. const search = function (keyword) {
  373. if (keyword && keyword !== '') {
  374. resultArr = [];
  375. const sortData = sheet.zh_data;
  376. if (sortData) {
  377. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  378. const sd = sortData[i];
  379. if (sd.name && sd.name.indexOf(keyword) > -1) {
  380. resultArr.push({index: i, data: sd});
  381. }
  382. }
  383. }
  384. } else {
  385. resultArr = [];
  386. }
  387. $('#pos-search-hint').html('结果:' + resultArr.length);
  388. };
  389. const searchAndLocate = function (keyword) {
  390. search(keyword);
  391. if (resultArr.length > 0) {
  392. const sel = sheet.getSelections()[0];
  393. const curRow = sel ? sel.row : 0;
  394. const pos = resultArr[0];
  395. if (pos.index !== curRow) {
  396. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  397. sheet.getParent().focus();
  398. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  399. SpreadJsObj.reloadRowsBackColor(sheet, [pos.index, curRow]);
  400. }
  401. }
  402. };
  403. const locateNext = function () {
  404. if (resultArr.length > 0) {
  405. const sel = sheet.getSelections()[0];
  406. const curRow = sel ? sel.row : 0;
  407. let next = _.find(resultArr, function (d) {
  408. return d.index > curRow;
  409. });
  410. if (!next) next = resultArr[0];
  411. if (next.index !== curRow) {
  412. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  413. sheet.getParent().focus();
  414. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  415. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  416. }
  417. }
  418. };
  419. const locatePre = function () {
  420. if (resultArr.length > 0) {
  421. const sel = sheet.getSelections()[0];
  422. const curRow = sel ? sel.row : 0;
  423. let next = _.findLast(resultArr, function (d) {
  424. return d.index < curRow;
  425. });
  426. if (!next) next = resultArr[resultArr.length - 1];
  427. if (next.index !== curRow) {
  428. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  429. sheet.getParent().focus();
  430. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  431. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  432. }
  433. }
  434. };
  435. return {search, searchAndLocate, locateNext, locatePre};
  436. })();
  437. // $('#pos-keyword').bind('input propertychange', function () {
  438. // posSearch.search(this.value);
  439. // });
  440. $('#pos-keyword').bind('keydown', function(e){
  441. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  442. });
  443. $('#search-pre-pos').click(function () {
  444. searchObj.locatePre();
  445. });
  446. $('#search-next-pos').click(function () {
  447. searchObj.locateNext();
  448. });
  449. return searchObj;
  450. };
  451. $.billsSearch = function (setting) {
  452. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  453. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  454. const resultId = setting.id + '-search-result';
  455. const obj = $(setting.selector);
  456. let filter = [];
  457. if (setting.searchOver || setting.searchEmpty) {
  458. filter.push('<select class="form-control form-control-sm" id="search-filter">');
  459. filter.push('<option value="">台账</option>');
  460. if (setting.customSearch) {
  461. for (const cs of setting.customSearch) {
  462. if (cs.valid) filter.push('<option value="' + cs.key + '">' + cs.title + '</option>');
  463. }
  464. }
  465. filter.push('</select>');
  466. // filter.push('<div class="input-group-prepend">');
  467. // filter.push('<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">台账</button>');
  468. // filter.push('<div class="dropdown-menu">');
  469. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-bills">台账</a>');
  470. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-over">超计</a>');
  471. // filter.push('<a class="dropdown-item" href="javascripty: void(0)" id="search-less">漏计</a>');
  472. // filter.push('</div>');
  473. // filter.push('</div>');
  474. }
  475. obj.html(
  476. ' <div class="sjs-bar">\n' +
  477. ' <div class="input-group input-group-sm pb-1">\n' +
  478. ' <div class="input-group-prepend">\n' +
  479. filter.join('') +
  480. ' </div>' +
  481. ' <input id="searchKeyword" type="text" class="form-control" autocomplete="off" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  482. ' <div class="input-group-append">\n' +
  483. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  484. ' </div>\n' +
  485. ' </div>\n' +
  486. ' </div>\n' +
  487. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  488. ' </div>'
  489. );
  490. autoFlashHeight();
  491. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  492. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  493. const searchSheet = setting.searchSpread.getActiveSheet();
  494. let searchResult = [];
  495. const search = function () {
  496. const filter = $('#search-filter').val();
  497. if (filter) {
  498. searchCustom(filter);
  499. } else {
  500. searchBills();
  501. }
  502. };
  503. const searchBills = function () {
  504. const keyword = $('#searchKeyword', obj).val();
  505. searchResult = [];
  506. const sortData = SpreadJsObj.getSortData(searchSheet);
  507. for (const node of sortData) {
  508. if ((node.code && node.code.indexOf(keyword) > -1) ||
  509. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  510. node.name && node.name.indexOf(keyword) > -1) {
  511. const data = JSON.parse(JSON.stringify(node));
  512. data.visible = true;
  513. searchResult.push(data);
  514. }
  515. }
  516. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  517. };
  518. const getCheckFun = function (key) {
  519. const cs = setting.customSearch.find(function (x) {return x.key === key});
  520. return cs ? cs.check : null;
  521. };
  522. const searchCustom = function (key) {
  523. const keyword = $('#searchKeyword', obj).val();
  524. const checkFun = getCheckFun(key);
  525. searchResult = [];
  526. const sortData = SpreadJsObj.getSortData(searchSheet);
  527. for (const node of sortData) {
  528. if (node.children && node.children.length > 0) continue;
  529. if (checkFun && checkFun(node)) {
  530. if (!keyword ||
  531. (node.code && node.code.indexOf(keyword) > -1) ||
  532. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  533. (node.name && node.name.indexOf(keyword) > -1)) {
  534. const data = JSON.parse(JSON.stringify(node));
  535. data.visible = true;
  536. searchResult.push(data);
  537. }
  538. }
  539. }
  540. calculateCompletePercent(searchResult);
  541. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  542. };
  543. const calculateCompletePercent = function (searchResult) {
  544. if (!searchResult) return;
  545. for (const sr of searchResult) {
  546. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  547. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  548. }
  549. };
  550. $('input', obj).bind('keydown', function (e) {
  551. if (e.keyCode == 13) search();
  552. });
  553. $('button', obj).bind('click', () => {search()});
  554. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  555. const sheet = info.sheet;
  556. const data = sheet.zh_data;
  557. if (!data) { return }
  558. const curBills = data[info.row];
  559. if (!curBills) { return }
  560. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  561. if (setting.afterLocated) {
  562. setting.afterLocated();
  563. }
  564. });
  565. return {spread: resultSpread};
  566. };
  567. $.billsTag = function (setting) {
  568. if (!setting.selector || !setting.relaSpread) return;
  569. if (!setting.tagType) setting.tagType = [
  570. {tagClass: 'text-primary', color: '#007bff'},
  571. {tagClass: 'text-success', color: '#28a745'},
  572. {tagClass: 'text-danger', color: '#dc3545'},
  573. {tagClass: 'text-warning', color: '#da9500'},
  574. {tagClass: 'text-info', color: '#17a2b8'},
  575. ];
  576. const obj = $(setting.selector);
  577. const html = [], pageLength = 15;
  578. let billsTags = [], classIndexes = [], billsIndexes = {}, curShow = [];
  579. html.push('<div class="sjs-bar d-flex justify-content-between">');
  580. // 下拉过滤
  581. html.push('<div class="dropdown mr-2">');
  582. html.push('<a class="btn btn-sm btn-outline-secondary" id="dmb-bills-tag" data-toggle="dropdown" title="优先显示" aria-expanded="false"><i class="fa fa-list-ol" id="bills-tag-filter"></i></a>');
  583. html.push('<div class="dropdown-menu" aria-labelledby="dmb-bills-tag" style="min-width: 60px; position: absolute; transform: translate3d(0px, 22px, 0px); top: 0px; left: 0px; will-change: transform;" x-placement="bottom-start">');
  584. html.push('<a class="dropdown-item" href="javascript: void(0);" tagType="all" ><i class="fa fa-list-ol"></i></a>');
  585. for (const t of setting.tagType) {
  586. html.push(`<a class="dropdown-item ${t.tagClass}" href="javascript: void(0);" tagType="${t.tagClass}" ><i class="fa fa-tag"></i></a>`);
  587. t.tags = [];
  588. classIndexes.push(t);
  589. }
  590. html.push('</div>', '</div>');
  591. // 搜索框
  592. html.push('<div class="input-group input-group-sm">');
  593. html.push('<input type="text" class="form-control" placeholder="可查找 项目节编号 / 清单编号 /名称" id="bills-tag-keyword" autocomplete="off">');
  594. html.push('<div class="input-group-append">', '<div class="input-group-cancel">',
  595. '<a href="javascript: void(0);" id="bills-tag-clear" class="text-danger"><i class="fa fa-times-circle" title="移除搜索结果"></i></a>', '</div>',
  596. '<button class="btn btn-outline-secondary" type="button" id="bills-tag-search">搜索</button>', '</div>');
  597. html.push('</div>');
  598. html.push('</div>');
  599. // 书签列表
  600. html.push('<div class="sjs-sh" style="overflow: auto;" id="bills-tag-list"></div>');
  601. obj.html(html.join(''));
  602. const clearViewTags = function () {
  603. const viewTags = $('.tag-item', obj);
  604. if (viewTags && viewTags.length > 0) viewTags.remove();
  605. billsTags.forEach(x => {x.display = false});
  606. };
  607. const getTagEditHtml = function(tag) {
  608. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  609. const tagHtml = [];
  610. tagHtml.push('<div name="tag-edit">');
  611. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  612. tagHtml.push(`<a class="pull-left mr-2" href="javascript: void(0);" id="tag-change-color" tag-color="${tag.color}" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-tag ${tagClass.tagClass}" title="修改书签颜色"></i></a>`);
  613. // 下拉选择颜色
  614. tagHtml.push('<div class="dropdown-menu" aria-labelledby="tag-change-color" style="min-width:60px">',
  615. '<a class="dropdown-item text-primary" href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  616. '<a class="dropdown-item text-success " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  617. '<a class="dropdown-item text-danger " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  618. '<a class="dropdown-item text-warning " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  619. '<a class="dropdown-item text-info " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>', '</div>');
  620. tagHtml.push('</div>');
  621. tag.node && tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '');
  622. tagHtml.push('</div>');
  623. tagHtml.push('<div class="card-body p-2">');
  624. tagHtml.push('<p class="card-text">', '<textarea class="form-control form-control-sm p-1" id="tag-comment">', tag.comment, '</textarea>', '</p>');
  625. tagHtml.push('<div class="d-flex justify-content-between">');
  626. // 参与人可见
  627. tagHtml.push('<div class="custom-control custom-switch mr-2">');
  628. tagHtml.push('<input type="checkbox" class="custom-control-input custom-control-warning-input" id="tag-share"', tag.share ? 'checked' : '', '>');
  629. tagHtml.push('<label class="custom-control-label custom-control-warning-label" for="tag-share" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="所有参与台帐审批管理的用户都可以看到这条书签"><i class="fa fa-users"></i> 参与者可见</label>');
  630. tagHtml.push('</div>');
  631. // 编辑按钮
  632. tagHtml.push('<div>', '<button type="button" class="btn btn-sm btn-outline-danger mr-3" id="tag-del"><i class="fa fa-close"></i> 删除书签</button>',
  633. '<button type="button" class="btn btn-sm btn-outline-success mr-1" id="tag-edit-ok"><i class="fa fa-check"></i> 确定</button>',
  634. '<button type="button" class="btn btn-sm btn-outline-secondary" id="tag-edit-cancel">取消</button>', '</div>');
  635. tagHtml.push('</div>');
  636. tagHtml.push('</div>');
  637. tagHtml.push('</div>');
  638. return tagHtml.join('');
  639. };
  640. const getTagDisplayHtml = function (tag) {
  641. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  642. const tagHtml = [];
  643. tagHtml.push('<div name="tag-view">');
  644. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  645. tagHtml.push(`<div class="pull-left mr-2"><i class="fa fa-tag ${tagClass.tagClass}"></i></div>`);
  646. tagHtml.push('</div>');
  647. tag.node && tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '');
  648. if (tag.share) {
  649. tagHtml.push('<i class="fa fa-users pull-right text-warning" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="所有参与台帐审批管理的用户都可以看到这条书签"></i>')
  650. }
  651. tagHtml.push('<div class="pull-right edit-tag-btn">');
  652. const lid = tag.node ? tag.node.ledger_id : -1;
  653. tagHtml.push(`<a class="mr-1" name="bills-tag-locate" href="javascript: void(0);" lid="${lid}"><i class="fa fa-crosshairs"></i> 定位</a>`);
  654. if (tag.uid === userID) tagHtml.push(`<a href="javascript: void(0);" name="bills-tag-edit" tag-id="${tag.id}"><i class="fa fa-edit"></i> 编辑</a>`);
  655. tagHtml.push('</div>');
  656. tagHtml.push('<div class="card-body p-2">', '<p class="card-text">', tag.comment, '</p>', '</div>');
  657. tagHtml.push('</div>');
  658. return tagHtml.join('');
  659. };
  660. const searchTagsAndShow = function () {
  661. const keyword = $('#bills-tag-keyword').val();
  662. const filterClass = $('#bills-tag-filter')[0].classList;
  663. const tagClass = filterClass.length > 2 ? filterClass[2] : null;
  664. const ci = tagClass ? classIndexes.find(x => {return x.tagClass === tagClass}) : null;
  665. curShow = billsTags.filter(x => {
  666. if (ci && ci.color !== x.color) return false;
  667. if (!keyword) return true;
  668. if (x.node.code && x.node.code.indexOf(keyword) >= 0) return true;
  669. if (x.node.b_code && x.node.b_code.indexOf(keyword) >= 0) return true;
  670. if (x.node.name && x.node.name.indexOf(keyword) >= 0) return true;
  671. return false;
  672. });
  673. reloadViewTags();
  674. };
  675. const refreshTagView = function (tag) {
  676. const obj = $('#bills-tag-' + tag.id);
  677. if (obj && obj.length > 0) {
  678. obj.html(getTagDisplayHtml(tag));
  679. }
  680. };
  681. const refreshBillsTagView = function (bills) {
  682. const bi = billsIndexes[bills.id] || [];
  683. for (const tag of bi) {
  684. refreshTagView(tag);
  685. }
  686. };
  687. const reviewTag = function (tag, isTop = false) {
  688. const obj = $('#bills-tag-' + tag.id);
  689. if (obj && obj.length > 0) {
  690. obj.html(getTagDisplayHtml(tag));
  691. } else {
  692. const objHtml = [];
  693. objHtml.push(`<div class="card border-primary my-2 tag-item" id="bills-tag-${tag.id}" tag-id="${tag.id}">`);
  694. objHtml.push(getTagDisplayHtml(tag));
  695. objHtml.push('</div>');
  696. if (isTop) {
  697. $('#bills-tag-list').prepend(objHtml.join(''));
  698. } else {
  699. $('#bills-tag-list').append(objHtml.join(''));
  700. }
  701. }
  702. tag.display = true;
  703. };
  704. const loadViewTags = function () {
  705. let showCount = 0;
  706. for (const t of curShow) {
  707. if (showCount >= pageLength) continue;
  708. if (t.display) continue;
  709. reviewTag(t);
  710. showCount++;
  711. }
  712. };
  713. const reloadViewTags = function () {
  714. clearViewTags();
  715. $("#bills-tag-lis").scrollTop(0);
  716. loadViewTags();
  717. };
  718. const _addToBillsIndex = function(data, isTop = false) {
  719. let bi = billsIndexes[data.lid];
  720. if (!bi) {
  721. bi = [];
  722. billsIndexes[data.lid] = bi;
  723. }
  724. isTop ? bi.unshift(data) : bi.push(data);
  725. };
  726. const loadDatas = function (datas) {
  727. billsTags = [];
  728. billsIndexes = {};
  729. for (const d of datas) {
  730. billsTags.push(d);
  731. _addToBillsIndex(d);
  732. }
  733. curShow = billsTags;
  734. reloadViewTags();
  735. };
  736. const updateDatas = function (data) {
  737. const refresh = {};
  738. if (data.add) {
  739. billsTags.push(data.add);
  740. _addToBillsIndex(data.add, true);
  741. refresh.add = data.add;
  742. }
  743. if (data.del) {
  744. const delTag = billsTags.find(x => {return x.id === data.del});
  745. billsTags.splice(billsTags.indexOf(delTag), 1);
  746. if (delTag.node) {
  747. const bi = billsIndexes[delTag.node.id];
  748. bi.splice(bi.indexOf(delTag), 1);
  749. }
  750. refresh.del = delTag;
  751. }
  752. if (data.update) {
  753. const updateTag = billsTags.find(x => {return x.id === data.update.id});
  754. for (const prop in data.update) {
  755. updateTag[prop] = data.update[prop];
  756. }
  757. refresh.update = updateTag;
  758. }
  759. return refresh;
  760. };
  761. const updateDatasAndShow = function (data) {
  762. const relaBills = [];
  763. const refresh = updateDatas(data);
  764. if (refresh.add) {
  765. reviewTag(refresh.add, true);
  766. relaBills.push(refresh.add.node);
  767. }
  768. if (refresh.del) {
  769. $('#bills-tag-' + refresh.del.id).remove();
  770. relaBills.push(refresh.del.node);
  771. }
  772. if (refresh.update) {
  773. refreshTagView(refresh.update);
  774. relaBills.push(refresh.update.node);
  775. }
  776. return relaBills;
  777. };
  778. const show = function () {
  779. showSelectTab(setting.selector, null, setting.afterShow);
  780. };
  781. const getBillsTagsColor = function (id) {
  782. const billsTags = billsIndexes[id] || [];
  783. return billsTags.length > 0 ? billsTags.map(x => {return x.color}) : undefined;
  784. };
  785. const getBillsTagsInfo = function (id) {
  786. const billsTags = billsIndexes[id] || [];
  787. return billsTags.length > 0 ? billsTags.map(x => {
  788. const tagClass = classIndexes.find(tc => {return tc.color === x.color}) || {};
  789. return {color: x.color, comment: x.comment, tagClass: tagClass.tagClass};
  790. }) : undefined;
  791. };
  792. $('body').on('click', '[name=bills-tag-locate]', function () {
  793. const lid = parseInt(this.getAttribute('lid'));
  794. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), lid);
  795. setting.afterLocated && setting.afterLocated();
  796. });
  797. $('body').on('click', '[name=bills-tag-edit]', function () {
  798. const tagId = this.getAttribute('tag-id');
  799. const tag = billsTags.find(x => {return x.id == tagId});
  800. if (tag) {
  801. const obj = $('#bills-tag-' + tag.id);
  802. $('[name=tag-view]', obj).hide();
  803. obj.append(getTagEditHtml(tag));
  804. }
  805. });
  806. $('body').on('click', '#tag-edit-cancel', function () {
  807. const obj = $('[name=tag-edit]').parent();
  808. $('[name=tag-edit]').remove();
  809. $('[name=tag-view]', obj).show();
  810. });
  811. $('body').on('click', '#tag-del', function () {
  812. const obj = $('[name=tag-edit]').parent();
  813. postData(setting.updateUrl, {del: parseInt(obj.attr('tag-id'))}, function (result) {
  814. if (!result.del) return;
  815. const bills = updateDatasAndShow(result);
  816. setting.afterModify && setting.afterModify(bills);
  817. });
  818. });
  819. $('body').on('click', '#tag-edit-ok', function () {
  820. const obj = $('[name=tag-edit]').parent();
  821. const data = {
  822. id: parseInt(obj.attr('tag-id')),
  823. share: $('#tag-share')[0].checked,
  824. comment: $('#tag-comment').val(),
  825. color: $('#tag-change-color').attr('tag-color'),
  826. };
  827. postData(setting.updateUrl, {update: data}, function (result) {
  828. if (!result.update) return;
  829. const bills = updateDatasAndShow(result);
  830. setting.afterModify && setting.afterModify(bills);
  831. });
  832. });
  833. $('body').on('click', '[name=tag-color]', function () {
  834. const tagClass = this.classList[1];
  835. const ci = classIndexes.find(tc => {return tc.tagClass === tagClass});
  836. const tcc = $('#tag-change-color');
  837. tcc.attr('tag-color', ci.color);
  838. tcc.find('i').attr('class', 'fa fa-tag ' + tagClass);
  839. });
  840. $('body').on('click', '[tagType]', function () {
  841. const tagClass = this.getAttribute('tagType');
  842. if (tagClass === 'all') {
  843. $('#bills-tag-filter').attr('class', 'fa fa-list-ol');
  844. } else {
  845. $('#bills-tag-filter').attr('class', 'fa fa-tag ' + tagClass);
  846. }
  847. searchTagsAndShow();
  848. });
  849. // 防抖
  850. function debounce(fun, delay) {
  851. let timer = null;
  852. return function () {
  853. if (timer) {
  854. clearTimeout(timer);
  855. }
  856. timer = setTimeout(fun, delay);
  857. }
  858. }
  859. $('#bills-tag-list').bind('scroll', debounce(function (e) {
  860. const obj = $('#bills-tag-list');
  861. var sum = obj[0].scrollHeight;
  862. if (sum <= obj.scrollTop() + obj.height()) {
  863. loadViewTags();
  864. }
  865. }, 300));
  866. $('#bills-tag-clear').bind('click', () => {
  867. if (!$('#bills-tag-keyword').val()) return;
  868. $('#bills-tag-keyword').val('');
  869. searchTagsAndShow();
  870. });
  871. $('#bills-tag-search').bind('click', () => {searchTagsAndShow();});
  872. $('#bills-tag-keyword').bind('keydown', e => {if (e.keyCode === 13) searchTagsAndShow();});
  873. return { loadDatas, updateDatasAndShow, show, getBillsTagsColor, getBillsTagsInfo, refreshBillsTagView, }
  874. }
  875. })(jQuery);