cs_tools.js 42 KB

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