cs_tools.js 43 KB

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