cs_tools.js 42 KB

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