cs_tools.js 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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: '@', hAlign: 1},
  68. {
  69. title: '错误类型', field: 'errorType', width: 85, formatter: '@',
  70. getValue: function (x) {
  71. switch (x.errorType) {
  72. case 'gather': return '汇总错误';
  73. case 'qty': return '数量';
  74. case 'tp': return '金额';
  75. case 'over': return '超计';
  76. case 'sibling': return '项目节清单同层';
  77. case 'same_code': return '重复项目节编号';
  78. case 's2b_over_gxby': return '违规计量(工序报验)';
  79. case 's2b_over_dagl': return '违规计量(档案管理)';
  80. case 's2b_lost_gxby': return '遗漏计量(工序报验)';
  81. case 's2b_lost_dagl': return '遗漏计量(档案管理)';
  82. case 'minus_cb': return '负变更清单漏计';
  83. case 'change_over': return '变更令超计';
  84. case 'settle': return '结算清单';
  85. default: return '';
  86. }
  87. }
  88. },
  89. {title: '清单编号', field: 'b_code', width: 85, formatter: '@'},
  90. {title: '清单名称', field: 'name', width: 165, formatter: '@'},
  91. {title: '备注', field: 'memo', width: 100, formatter: '@'},
  92. ],
  93. emptyRows: 0,
  94. headRows: 1,
  95. headRowHeight: [32],
  96. defaultRowHeight: 21,
  97. headerFont: '12px 微软雅黑',
  98. font: '12px 微软雅黑',
  99. selectedBackColor: '#fffacd',
  100. readOnly: true,
  101. };
  102. }
  103. const clearErrorData = function () {
  104. if (setting.storeKey) removeLocalCache(setting.storeKey);
  105. };
  106. const autoShowHistory = function (show) {
  107. if (setting.storeKey) {
  108. setLocalCache(setting.storeKey + '-showHis', show.toString());
  109. }
  110. };
  111. if (setting.selector && setting.relaSpread) {
  112. const resultId = setting.id + '-spread';
  113. const obj = $(setting.selector);
  114. obj.html(
  115. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  116. ' </div>'
  117. );
  118. autoFlashHeight();
  119. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  120. const sheet = spread.getActiveSheet();
  121. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  122. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  123. const sheet = info.sheet;
  124. const data = sheet.zh_data;
  125. if (!data) { return }
  126. const curBills = data[info.row];
  127. if (!curBills) { return }
  128. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  129. if (setting.afterLocated) {
  130. setting.afterLocated();
  131. }
  132. });
  133. const loadErrorData = function (data, his = false) {
  134. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  135. if (!sourceTree) return;
  136. for (const d of data) {
  137. if (d.ledger_id) {
  138. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  139. } else if (d.lid) {
  140. const nodeIndex = sourceTree.nodes.findIndex(x => { return x.id === d.lid });
  141. if (nodeIndex >= 0) {
  142. d.serialNo = nodeIndex + 1;
  143. d.ledger_id = sourceTree.nodes[nodeIndex].ledger_id;
  144. }
  145. }
  146. }
  147. data.sort(function (a, b) {
  148. return a.serialNo ? (b.serialNo ? a.serialNo - b.serialNo : 1) : 1;
  149. });
  150. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  151. if (!his && setting.storeKey) {
  152. setLocalCache(setting.storeKey, JSON.stringify(data));
  153. }
  154. $(setting.tabSelector).show();
  155. };
  156. const showErrorList = function () {
  157. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  158. $('a', '.side-menu').removeClass('active');
  159. tab.addClass('active');
  160. $('.tab-content .tab-pane').removeClass('active');
  161. tabPanel.addClass('active');
  162. showSideTools(true);
  163. spread.refresh();
  164. if (setting.afterShow) setting.afterShow();
  165. };
  166. const loadHisErrorData = function () {
  167. if (setting.storeKey) {
  168. const storeStr = getLocalCache(setting.storeKey);
  169. const storeData = storeStr ? JSON.parse(storeStr) : [];
  170. if (storeData.length > 0) {
  171. loadErrorData(storeData, true);
  172. const showHis = getLocalCache(setting.storeKey + '-showHis');
  173. if (showHis === 'true') {
  174. showErrorList();
  175. removeLocalCache(setting.storeKey + '-showHis');
  176. }
  177. }
  178. }
  179. };
  180. return {
  181. spread: spread,
  182. loadErrorData: loadErrorData,
  183. clearErrorData: clearErrorData,
  184. loadHisErrorData: loadHisErrorData,
  185. show: showErrorList,
  186. autoShowHistory: autoShowHistory,
  187. };
  188. } else {
  189. const loadErrorData = function (data) {
  190. if (setting.storeKey) {
  191. setLocalCache(setting.storeKey, JSON.stringify(data));
  192. }
  193. };
  194. return {
  195. loadErrorData: loadErrorData,
  196. clearErrorData: clearErrorData,
  197. autoShowHistory: autoShowHistory,
  198. };
  199. }
  200. };
  201. $.ledger_checkList = function (setting) {
  202. const checkTypeText = [];
  203. for (const ct in setting.checkType) {
  204. checkTypeText[setting.checkType[ct].value] = setting.checkType[ct].text;
  205. }
  206. if (!setting.spreadSetting) {
  207. setting.spreadSetting = {
  208. cols: [
  209. {
  210. title: '类型', field: 'type', width: 150, formatter: '@',
  211. getValue: function (data){
  212. if (setting.checkType) {
  213. return checkTypeText[data.type] || '';
  214. } else {
  215. return '';
  216. }
  217. }
  218. },
  219. {title: '行号', field: 'serialNo', hAlign: 1, width: 40, formatter: '@'},
  220. {title: '项目节编号\n(变更令号)', field: 'code', width: 80, formatter: '@'},
  221. {title: '清单编号', field: 'b_code', width: 80, formatter: '@'},
  222. {title: '名称', field: 'name', width: 150, formatter: '@'},
  223. ],
  224. emptyRows: 0,
  225. headRows: 1,
  226. headRowHeight: [32],
  227. defaultRowHeight: 21,
  228. headerFont: '12px 微软雅黑',
  229. font: '12px 微软雅黑',
  230. selectedBackColor: '#fffacd',
  231. readOnly: true,
  232. };
  233. }
  234. const clearCheckData = function () {
  235. if (setting.storeKey) removeLocalCache(setting.storeKey);
  236. };
  237. const autoShowHistory = function (show) {
  238. if (setting.storeKey) {
  239. setLocalCache(setting.storeKey + '-showHis', show.toString());
  240. }
  241. };
  242. if (setting.selector && setting.relaSpread) {
  243. const resultId = setting.id + '-spread';
  244. const obj = $(setting.selector);
  245. const dropdown = [];
  246. if (setting.checkType) {
  247. dropdown.push('<div class="dropdown">');
  248. 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>');
  249. dropdown.push('<div class="dropdown-menu" aria-labelledby="'+ setting.id + 'drop">');
  250. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="all">所有类型</a>');
  251. for (const ct in setting.checkType) {
  252. dropdown.push('<a class="dropdown-item" href="javascript: void(0);" check-type="' + setting.checkType[ct].value +'">' + setting.checkType[ct].text + '</a>');
  253. }
  254. dropdown.push('</div>');
  255. dropdown.push('</div>');
  256. }
  257. obj.html(
  258. '<div class="sjs-bar">\n' +
  259. ' <div class="pb-1 d-flex">\n' + dropdown.join('') +
  260. ' <span class="ml-auto pr-2" id="' + setting.id + '-time">检查时间:2020-08-01 13:20:25</span>\n' +
  261. ' </div>\n' +
  262. '</div>' +
  263. '<div id="' + resultId + '" class="sjs-sh">\n' +
  264. '</div>'
  265. );
  266. autoFlashHeight();
  267. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  268. const sheet = spread.getActiveSheet();
  269. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  270. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  271. const sheet = info.sheet;
  272. const data = sheet.zh_data;
  273. if (!data) { return }
  274. const curBills = data[info.row];
  275. if (!curBills) { return }
  276. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  277. if (setting.afterLocated) {
  278. setting.afterLocated();
  279. }
  280. });
  281. const filterCheckData = function () {
  282. const filter = $(this).attr('check-type');
  283. $('#' + setting.id + 'drop').html(this.innerHTML);
  284. for (const d of sheet.zh_data) {
  285. if (filter === 'all') {
  286. d.visible = true;
  287. } else {
  288. d.visible = d.type == filter;
  289. }
  290. }
  291. SpreadJsObj.refreshTreeRowVisible(sheet);
  292. };
  293. $('a[check-type]').bind('click', filterCheckData);
  294. const hideCheckData = function () {
  295. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  296. if (tab.hasClass('active')) {
  297. $('a', '.side-menu').removeClass('active');
  298. tab.addClass('active');
  299. $('.tab-content .tab-pane').removeClass('active');
  300. tabPanel.addClass('active');
  301. showSideTools(false);
  302. if (spread) spread.refresh();
  303. if (setting.afterShow) setting.afterShow();
  304. tab.hide();
  305. }
  306. };
  307. const loadCheckData = function (data, his = false) {
  308. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  309. if (!sourceTree) return;
  310. for (const d of data.warning_data) {
  311. if (d.ledger_id) {
  312. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  313. } else if (d.lid) {
  314. const nodeIndex = sourceTree.nodes.findIndex(x => { return x.id === d.lid });
  315. if (nodeIndex >= 0) {
  316. d.serialNo = nodeIndex + 1;
  317. d.ledger_id = sourceTree.nodes[nodeIndex].ledger_id;
  318. }
  319. }
  320. }
  321. $('#' + setting.id + '-time').html('检查时间:' + moment(data.check_time).format('YYYY-MM-DD HH:mm:ss'));
  322. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data.warning_data);
  323. if (!his && setting.storeKey) {
  324. setLocalCache(setting.storeKey, JSON.stringify(data));
  325. }
  326. $(setting.tabSelector).show();
  327. };
  328. const showCheckList = function () {
  329. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  330. $('a', '.side-menu').removeClass('active');
  331. tab.addClass('active');
  332. $('.tab-content .tab-pane').removeClass('active');
  333. tabPanel.addClass('active');
  334. showSideTools(true);
  335. spread.refresh();
  336. if (setting.afterShow) setting.afterShow();
  337. };
  338. const loadHisCheckData = function () {
  339. if (setting.storeKey) {
  340. const storeStr = getLocalCache(setting.storeKey);
  341. const storeData = storeStr ? JSON.parse(storeStr) : null;
  342. if (storeData) {
  343. loadCheckData(storeData, true);
  344. const showHis = getLocalCache(setting.storeKey + '-showHis');
  345. if (showHis === 'true') {
  346. showCheckList();
  347. removeLocalCache(setting.storeKey + '-showHis');
  348. }
  349. }
  350. }
  351. };
  352. return {
  353. spread: spread,
  354. loadCheckData: loadCheckData,
  355. clearCheckData: clearCheckData,
  356. loadHisCheckData: loadHisCheckData,
  357. show: showCheckList,
  358. hide: hideCheckData,
  359. autoShowHistory: autoShowHistory,
  360. };
  361. } else {
  362. const loadCheckData = function (data) {
  363. if (setting.storeKey) {
  364. setLocalCache(setting.storeKey, JSON.stringify(data));
  365. }
  366. };
  367. return {
  368. loadCheckData: loadCheckData,
  369. clearCheckData: clearCheckData,
  370. autoShowHistory: autoShowHistory,
  371. };
  372. }
  373. };
  374. $.posSearch = function (setting) {
  375. if (!setting.selector || !setting.searchSpread) return;
  376. const searchHtml =
  377. ' <div class="ml-2">\n' +
  378. ' <div class="input-group input-group-sm">\n' +
  379. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  380. ' <div class="input-group-append">\n' +
  381. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  382. ' </div>\n' +
  383. ' <div class="input-group-append" >\n' +
  384. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  385. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  386. ' </div>\n' +
  387. ' </div>\n' +
  388. ' </div>\n';
  389. $(setting.selector).html(searchHtml);
  390. const sheet = setting.searchSpread.getActiveSheet();
  391. const searchObj = (function () {
  392. let resultArr = [];
  393. const search = function (keyword) {
  394. if (keyword && keyword !== '') {
  395. resultArr = [];
  396. const sortData = sheet.zh_data;
  397. if (sortData) {
  398. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  399. const sd = sortData[i];
  400. if (sd.name && sd.name.indexOf(keyword) > -1) {
  401. resultArr.push({index: i, data: sd});
  402. }
  403. }
  404. }
  405. } else {
  406. resultArr = [];
  407. }
  408. $('#pos-search-hint').html('结果:' + resultArr.length);
  409. };
  410. const searchAndLocate = function (keyword) {
  411. search(keyword);
  412. if (resultArr.length > 0) {
  413. const sel = sheet.getSelections()[0];
  414. const curRow = sel ? sel.row : 0;
  415. const pos = resultArr[0];
  416. if (pos.index !== curRow) {
  417. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  418. sheet.getParent().focus();
  419. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  420. SpreadJsObj.reloadRowsBackColor(sheet, [pos.index, curRow]);
  421. }
  422. }
  423. };
  424. const locateNext = function () {
  425. if (resultArr.length > 0) {
  426. const sel = sheet.getSelections()[0];
  427. const curRow = sel ? sel.row : 0;
  428. let next = _.find(resultArr, function (d) {
  429. return d.index > curRow;
  430. });
  431. if (!next) next = resultArr[0];
  432. if (next.index !== curRow) {
  433. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  434. sheet.getParent().focus();
  435. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  436. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  437. }
  438. }
  439. };
  440. const locatePre = function () {
  441. if (resultArr.length > 0) {
  442. const sel = sheet.getSelections()[0];
  443. const curRow = sel ? sel.row : 0;
  444. let next = _.findLast(resultArr, function (d) {
  445. return d.index < curRow;
  446. });
  447. if (!next) next = resultArr[resultArr.length - 1];
  448. if (next.index !== curRow) {
  449. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  450. sheet.getParent().focus();
  451. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  452. SpreadJsObj.reloadRowsBackColor(sheet, [next.index, curRow]);
  453. }
  454. }
  455. };
  456. return {search, searchAndLocate, locateNext, locatePre};
  457. })();
  458. // $('#pos-keyword').bind('input propertychange', function () {
  459. // posSearch.search(this.value);
  460. // });
  461. $('#pos-keyword').bind('keydown', function(e){
  462. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  463. });
  464. $('#search-pre-pos').click(function () {
  465. searchObj.locatePre();
  466. });
  467. $('#search-next-pos').click(function () {
  468. searchObj.locateNext();
  469. });
  470. return searchObj;
  471. };
  472. $.billsSearch = function (setting) {
  473. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  474. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称/台账数量';
  475. if (!setting.keyId) setting.keyId = 'ledger_id';
  476. const resultId = setting.id + '-search-result';
  477. const obj = $(setting.selector);
  478. let filter = [];
  479. if (setting.searchOver || setting.searchEmpty) {
  480. filter.push('<select class="input-group-text" id="search-filter">');
  481. filter.push('<option value="">台账</option>');
  482. if (setting.customSearch) {
  483. for (const cs of setting.customSearch) {
  484. if (cs.valid) filter.push('<option value="' + cs.key + '">' + cs.title + '</option>');
  485. }
  486. }
  487. filter.push('</select>');
  488. }
  489. obj.html(
  490. ' <div class="sjs-bar">\n' +
  491. ' <div class="input-group input-group-sm pb-1">\n' +
  492. ' <div class="input-group-prepend">\n' +
  493. filter.join('') +
  494. ' </div>' +
  495. ' <input id="searchKeyword" type="text" class="form-control" autocomplete="off" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  496. ' <div class="input-group-append">\n' +
  497. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  498. ' </div>\n' +
  499. ' </div>\n' +
  500. ' </div>\n' +
  501. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  502. ' </div>'
  503. );
  504. autoFlashHeight();
  505. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  506. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  507. const searchSheet = setting.searchSpread.getActiveSheet();
  508. let searchResult = [];
  509. const search = function () {
  510. const filter = $('#search-filter').val();
  511. if (filter) {
  512. searchCustom(filter);
  513. } else {
  514. searchBills();
  515. }
  516. };
  517. const get18Bw = function(tree, data) {
  518. let parent = tree.getParent(data);
  519. while (parent && (!parent.code || /^[a-zA-Z]/.test(parent.code || ''))) {
  520. parent = tree.getParent(parent);
  521. }
  522. return parent ? parent.name : '';
  523. };
  524. const get08Bw = function(tree, data) {
  525. let parent = tree.getParent(data);
  526. let lastXmj = '', level4Xmj = '';
  527. while (parent) {
  528. if (parent.code && !lastXmj) lastXmj = parent.name;
  529. if (parent.code && parent.level === 4) level4Xmj = parent.name;
  530. parent = tree.getParent(parent);
  531. }
  532. return level4Xmj || lastXmj;
  533. };
  534. const getBw = function (data) {
  535. if (!data.b_code) return '';
  536. if (searchSheet.zh_dataType !== SpreadJsObj.DataType.Tree) return '';
  537. const sortTree = searchSheet.zh_tree;
  538. if (!sortTree.checkCodeType) return '';
  539. if (sortTree.checkCodeType() === '18') {
  540. return get18Bw(sortTree, data)
  541. } else {
  542. return get08Bw(sortTree, data);
  543. }
  544. };
  545. const searchBills = function () {
  546. const keyword = $('#searchKeyword', obj).val();
  547. const keyNum = _.toNumber(keyword);
  548. searchResult = [];
  549. const sortData = SpreadJsObj.getSortData(searchSheet);
  550. for (const node of sortData) {
  551. if ((node.code && node.code.indexOf(keyword) > -1) ||
  552. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  553. (node.name && node.name.indexOf(keyword) > -1) ||
  554. (!_.isNaN(keyNum) && checkZero(ZhCalc.sub(keyNum, node.quantity)))
  555. ) {
  556. const data = JSON.parse(JSON.stringify(node));
  557. data.visible = true;
  558. data.bw = getBw(data);
  559. searchResult.push(data);
  560. }
  561. }
  562. calculateCompletePercent(searchResult);
  563. calculateSum();
  564. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  565. };
  566. const getCheckFun = function (key) {
  567. const cs = setting.customSearch.find(function (x) {return x.key === key});
  568. return cs ? cs.check : null;
  569. };
  570. const getParentFun = function (key) {
  571. const cs = setting.customSearch.find(function (x) {return x.key === key});
  572. return cs && cs.parent !== undefined ? cs.parent : false;
  573. };
  574. const searchCustom = function (key) {
  575. const keyword = $('#searchKeyword', obj).val();
  576. const keyNum = _.toNumber(keyword);
  577. const checkFun = getCheckFun(key);
  578. searchResult = [];
  579. const sortData = SpreadJsObj.getSortData(searchSheet);
  580. const parentFun = getParentFun(key);
  581. for (const node of sortData) {
  582. if (node.children && node.children.length > 0 && !parentFun) continue;
  583. if (checkFun && checkFun(node)) {
  584. if (!keyword ||
  585. (node.code && node.code.indexOf(keyword) > -1) ||
  586. (node.b_code && node.b_code.indexOf(keyword) > -1) ||
  587. (node.name && node.name.indexOf(keyword) > -1) ||
  588. (!_.isNaN(keyNum) && checkZero(ZhCalc.sub(keyNum, node.quantity)))
  589. ) {
  590. const data = JSON.parse(JSON.stringify(node));
  591. data.visible = true;
  592. searchResult.push(data);
  593. }
  594. }
  595. }
  596. calculateCompletePercent(searchResult);
  597. calculateSum();
  598. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  599. };
  600. const calculateSum = function () {
  601. if (!searchResult || searchResult.length === 0 || !setting.calcSum) return;
  602. const sum = setting.calcSum(searchResult);
  603. if (sum) searchResult.unshift(sum);
  604. };
  605. const calculateCompletePercent = function (searchResult) {
  606. if (!searchResult) return;
  607. for (const sr of searchResult) {
  608. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  609. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  610. }
  611. };
  612. $('input', obj).bind('keydown', function (e) {
  613. if (e.keyCode == 13) search();
  614. });
  615. $('button', obj).bind('click', () => {search()});
  616. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  617. const sheet = info.sheet;
  618. const data = sheet.zh_data;
  619. if (!data) { return }
  620. const curBills = data[info.row];
  621. if (!curBills || curBills[setting.keyId] === undefined) { return }
  622. SpreadJsObj.locateTreeNode(searchSheet, curBills[setting.keyId], true);
  623. if (setting.afterLocated) {
  624. setting.afterLocated();
  625. }
  626. });
  627. return {spread: resultSpread};
  628. };
  629. $.listSearch = function (setting) {
  630. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  631. if (!setting.searchRangeStr) setting.searchRangeStr = '清单编号/名称';
  632. const resultId = setting.id + '-search-result';
  633. const obj = $(setting.selector);
  634. let filter = [];
  635. if (setting.searchOver || setting.searchEmpty) {
  636. filter.push('<select class="input-group-text" id="search-filter">');
  637. filter.push('<option value="">台账</option>');
  638. if (setting.customSearch) {
  639. for (const cs of setting.customSearch) {
  640. if (cs.valid) filter.push('<option value="' + cs.key + '">' + cs.title + '</option>');
  641. }
  642. }
  643. filter.push('</select>');
  644. }
  645. obj.html(
  646. ' <div class="sjs-bar">\n' +
  647. ' <div class="input-group input-group-sm pb-1">\n' +
  648. ' <div class="input-group-prepend">\n' +
  649. filter.join('') +
  650. ' </div>' +
  651. ' <input id="searchKeyword" type="text" class="form-control" autocomplete="off" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  652. ' <div class="input-group-append">\n' +
  653. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  654. ' </div>\n' +
  655. ' </div>\n' +
  656. ' </div>\n' +
  657. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  658. ' </div>'
  659. );
  660. autoFlashHeight();
  661. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  662. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  663. const searchSheet = setting.searchSpread.getActiveSheet();
  664. let searchResult = [];
  665. const search = function () {
  666. const filter = $('#search-filter').val();
  667. if (filter) {
  668. searchCustom(filter);
  669. } else {
  670. searchList();
  671. }
  672. };
  673. const calulateSum = function () {
  674. if (searchResult.length === 0 || !setting.calcSum) return;
  675. searchResult.unshift(setting.calcSum(searchResult));
  676. };
  677. const searchList = function () {
  678. const keyword = $('#searchKeyword', obj).val();
  679. searchResult = [];
  680. const sortData = SpreadJsObj.getSortData(searchSheet);
  681. for (const [i, node] of sortData.entries()) {
  682. if (setting.check(node, keyword)) {
  683. const data = JSON.parse(JSON.stringify(node));
  684. data.searchIndex = i;
  685. data.visible = true;
  686. searchResult.push(data);
  687. }
  688. }
  689. calulateSum();
  690. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  691. };
  692. const getCheckFun = function (key) {
  693. const cs = setting.customSearch.find(function (x) {return x.key === key});
  694. return cs ? cs.check : null;
  695. };
  696. const searchCustom = function (key) {
  697. const keyword = $('#searchKeyword', obj).val();
  698. const checkFun = getCheckFun(key);
  699. searchResult = [];
  700. const sortData = SpreadJsObj.getSortData(searchSheet);
  701. for (const [i, node] of sortData.entries()) {
  702. if (checkFun && checkFun(node)) {
  703. if (setting.check(node, keyword)) {
  704. const data = JSON.parse(JSON.stringify(node));
  705. data.searchIndex = i;
  706. data.visible = true;
  707. searchResult.push(data);
  708. }
  709. }
  710. }
  711. calulateSum();
  712. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  713. };
  714. $('input', obj).bind('keydown', function (e) {
  715. if (e.keyCode == 13) search();
  716. });
  717. $('button', obj).bind('click', () => {search()});
  718. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  719. const cur = SpreadJsObj.getSelectObject(info.sheet);
  720. if (!cur || cur.searchIndex < 0) return;
  721. SpreadJsObj.locateRow(searchSheet, cur.searchIndex);
  722. if (setting.afterLocated) {
  723. setting.afterLocated();
  724. }
  725. });
  726. return {spread: resultSpread};
  727. };
  728. $.billsTag = function (setting) {
  729. if (!setting.selector || !setting.relaSpread) return;
  730. if (!setting.tagType) setting.tagType = [
  731. {tagClass: 'text-primary', color: '#007bff'},
  732. {tagClass: 'text-success', color: '#28a745'},
  733. {tagClass: 'text-danger', color: '#dc3545'},
  734. {tagClass: 'text-warning', color: '#da9500'},
  735. {tagClass: 'text-info', color: '#17a2b8'},
  736. ];
  737. if (!setting.key) setting.key = 'id';
  738. if (!setting.treeId) setting.treeId = 'ledger_id';
  739. const obj = $(setting.selector);
  740. const relaTree = setting.relaTree;
  741. const html = [], pageLength = 15;
  742. let billsTags = [], classIndexes = [], billsIndexes = {}, curShow = [];
  743. html.push('<div class="sjs-bar d-flex justify-content-between">');
  744. // 下拉过滤
  745. html.push('<div class="dropdown mr-2">');
  746. 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>');
  747. 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">');
  748. html.push('<a class="dropdown-item" href="javascript: void(0);" tagType="all" ><i class="fa fa-list-ol"></i></a>');
  749. for (const t of setting.tagType) {
  750. html.push(`<a class="dropdown-item ${t.tagClass}" href="javascript: void(0);" tagType="${t.tagClass}" ><i class="fa fa-tag"></i></a>`);
  751. t.tags = [];
  752. classIndexes.push(t);
  753. }
  754. html.push('</div>', '</div>');
  755. // 搜索框
  756. html.push('<div class="input-group input-group-sm">');
  757. html.push('<input type="text" class="form-control" placeholder="可查找 项目节编号 / 清单编号 /名称" id="bills-tag-keyword" autocomplete="off">');
  758. html.push('<div class="input-group-append">', '<div class="input-group-cancel">',
  759. '<a href="javascript: void(0);" id="bills-tag-clear" class="text-danger"><i class="fa fa-times-circle" title="移除搜索结果"></i></a>', '</div>',
  760. '<button class="btn btn-outline-secondary" type="button" id="bills-tag-search">搜索</button>', '</div>');
  761. html.push('</div>');
  762. html.push('</div>');
  763. // 书签列表
  764. html.push('<div class="sjs-sh" style="overflow: auto;" id="bills-tag-list"></div>');
  765. obj.html(html.join(''));
  766. const clearViewTags = function () {
  767. const viewTags = $('.tag-item', obj);
  768. if (viewTags && viewTags.length > 0) viewTags.remove();
  769. billsTags.forEach(x => {x.display = false});
  770. };
  771. const getTagEditHtml = function(tag) {
  772. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  773. const tagHtml = [];
  774. tagHtml.push('<div name="tag-edit">');
  775. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  776. 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>`);
  777. // 下拉选择颜色
  778. tagHtml.push('<div class="dropdown-menu" aria-labelledby="tag-change-color" style="min-width:60px">',
  779. '<a class="dropdown-item text-primary" href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  780. '<a class="dropdown-item text-success " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  781. '<a class="dropdown-item text-danger " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  782. '<a class="dropdown-item text-warning " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  783. '<a class="dropdown-item text-info " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>', '</div>');
  784. tagHtml.push('</div>');
  785. if (tag.node) {
  786. const posHint = tag.pos ? ' - ' + tag.pos.name : '';
  787. tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '', posHint);
  788. }
  789. tagHtml.push('</div>');
  790. tagHtml.push('<div class="card-body p-2">');
  791. tagHtml.push('<p class="card-text">', '<textarea class="form-control form-control-sm p-1" id="tag-comment">', tag.comment, '</textarea>', '</p>');
  792. tagHtml.push('<div class="d-flex justify-content-between">');
  793. // 参与人可见
  794. tagHtml.push('<div class="custom-control custom-switch mr-2">');
  795. tagHtml.push('<input type="checkbox" class="custom-control-input custom-control-warning-input" id="tag-share"', tag.share ? 'checked' : '', '>');
  796. 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>');
  797. tagHtml.push('</div>');
  798. // 编辑按钮
  799. 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>',
  800. '<button type="button" class="btn btn-sm btn-outline-success mr-1" id="tag-edit-ok"><i class="fa fa-check"></i> 确定</button>',
  801. '<button type="button" class="btn btn-sm btn-outline-secondary" id="tag-edit-cancel">取消</button>', '</div>');
  802. tagHtml.push('</div>');
  803. tagHtml.push('</div>');
  804. tagHtml.push('</div>');
  805. return tagHtml.join('');
  806. };
  807. const getTagDisplayHtml = function (tag) {
  808. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  809. const tagHtml = [];
  810. tagHtml.push('<div name="tag-view">');
  811. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  812. tagHtml.push(`<div class="pull-left mr-2"><i class="fa fa-tag ${tagClass.tagClass}"></i></div>`);
  813. tagHtml.push('</div>');
  814. if (tag.node) {
  815. const posHint = tag.pos ? ' - ' + tag.pos.name : '';
  816. tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '', posHint);
  817. }
  818. if (tag.share) {
  819. tagHtml.push(`<div class="pull-right"><i class="fa fa-users text-warning" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="所有参与台账审批管理的用户都可以看到这条书签"></i> <span>${tag.u_name}</span></div>`);
  820. }
  821. tagHtml.push('<div class="pull-right edit-tag-btn">');
  822. const lid = tag.node ? tag.node[setting.treeId] : -1;
  823. tagHtml.push(`<a class="mr-1" name="bills-tag-locate" href="javascript: void(0);" lid="${lid}" pos_id="${tag.pos_id}"><i class="fa fa-crosshairs"></i> 定位</a>`);
  824. if (tag.uid === userID && !setting.readOnly) tagHtml.push(`<a href="javascript: void(0);" name="bills-tag-edit" tag-id="${tag.id}"><i class="fa fa-edit"></i> 编辑</a>`);
  825. tagHtml.push('</div>');
  826. if (tag.node && relaTree) {
  827. const parents = relaTree.getAllParents(tag.node);
  828. const parentName = [];
  829. parents.forEach(p => { if (p.code && p.level > 1) parentName.push(p.name); });
  830. if(parentName.length > 0) tagHtml.push(`<div>所属部位:${parentName.join('/')}</div>`);
  831. }
  832. tagHtml.push('<div class="card-body p-2">', '<p class="card-text">', tag.comment, '</p>', '</div>');
  833. tagHtml.push('</div>');
  834. return tagHtml.join('');
  835. };
  836. const searchTagsAndShow = function () {
  837. const keyword = $('#bills-tag-keyword').val();
  838. const filterClass = $('#bills-tag-filter')[0].classList;
  839. const tagClass = filterClass.length > 2 ? filterClass[2] : null;
  840. const ci = tagClass ? classIndexes.find(x => {return x.tagClass === tagClass}) : null;
  841. curShow = billsTags.filter(x => {
  842. if (ci && ci.color !== x.color) return false;
  843. if (!keyword) return true;
  844. if (x.node.code && x.node.code.indexOf(keyword) >= 0) return true;
  845. if (x.node.b_code && x.node.b_code.indexOf(keyword) >= 0) return true;
  846. if (x.node.name && x.node.name.indexOf(keyword) >= 0) return true;
  847. return false;
  848. });
  849. reloadViewTags();
  850. };
  851. const refreshTagView = function (tag) {
  852. const obj = $('#bills-tag-' + tag.id);
  853. if (obj && obj.length > 0) {
  854. obj.html(getTagDisplayHtml(tag));
  855. }
  856. };
  857. const refreshBillsTagView = function (bills) {
  858. const bi = billsIndexes[bills.id] || [];
  859. for (const tag of bi) {
  860. refreshTagView(tag);
  861. }
  862. };
  863. const refreshPosTagView = function(pos) {
  864. const posRange = pos instanceof Array ? pos : [pos];
  865. for (const p of posRange) {
  866. const bi = billsIndexes[p.lid] || [];
  867. const pi = bi.filter(x => { return x.pos_id === p.id; });
  868. for (const tag of pi) {
  869. refreshTagView(tag);
  870. }
  871. }
  872. };
  873. const reviewTag = function (tag, isTop = false) {
  874. const obj = $('#bills-tag-' + tag.id);
  875. if (obj && obj.length > 0) {
  876. obj.html(getTagDisplayHtml(tag));
  877. } else {
  878. const objHtml = [];
  879. objHtml.push(`<div class="card border-primary my-2 tag-item" id="bills-tag-${tag.id}" tag-id="${tag.id}">`);
  880. objHtml.push(getTagDisplayHtml(tag));
  881. objHtml.push('</div>');
  882. if (isTop) {
  883. $('#bills-tag-list').prepend(objHtml.join(''));
  884. } else {
  885. $('#bills-tag-list').append(objHtml.join(''));
  886. }
  887. }
  888. tag.display = true;
  889. };
  890. const loadViewTags = function () {
  891. let showCount = 0;
  892. for (const t of curShow) {
  893. if (showCount >= pageLength) continue;
  894. if (t.display) continue;
  895. reviewTag(t);
  896. showCount++;
  897. }
  898. };
  899. const reloadViewTags = function () {
  900. clearViewTags();
  901. $("#bills-tag-lis").scrollTop(0);
  902. loadViewTags();
  903. };
  904. const _addToBillsIndex = function(data, isTop = false) {
  905. let bi = billsIndexes[data.lid];
  906. if (!bi) {
  907. bi = [];
  908. billsIndexes[data.lid] = bi;
  909. }
  910. isTop ? bi.unshift(data) : bi.push(data);
  911. };
  912. const loadDatas = function (datas) {
  913. billsTags = [];
  914. billsIndexes = {};
  915. for (const d of datas) {
  916. if (!d.node) continue;
  917. billsTags.push(d);
  918. _addToBillsIndex(d);
  919. }
  920. curShow = billsTags;
  921. reloadViewTags();
  922. };
  923. const updateDatas = function (data) {
  924. const refresh = {};
  925. if (data.add) {
  926. billsTags.push(data.add);
  927. _addToBillsIndex(data.add, true);
  928. refresh.add = data.add;
  929. }
  930. if (data.del) {
  931. const delTag = billsTags.find(x => {return x.id === data.del});
  932. billsTags.splice(billsTags.indexOf(delTag), 1);
  933. if (delTag.node) {
  934. const bi = billsIndexes[delTag.node.id];
  935. bi.splice(bi.indexOf(delTag), 1);
  936. }
  937. refresh.del = delTag;
  938. }
  939. if (data.update) {
  940. const updateTag = billsTags.find(x => {return x.id === data.update.id});
  941. for (const prop in data.update) {
  942. updateTag[prop] = data.update[prop];
  943. }
  944. refresh.update = updateTag;
  945. }
  946. return refresh;
  947. };
  948. const updateDatasAndShow = function (data) {
  949. const relaBills = [];
  950. const refresh = updateDatas(data);
  951. if (refresh.add) {
  952. reviewTag(refresh.add, true);
  953. relaBills.push(refresh.add.node);
  954. }
  955. if (refresh.del) {
  956. $('#bills-tag-' + refresh.del.id).remove();
  957. relaBills.push(refresh.del.node);
  958. }
  959. if (refresh.update) {
  960. refreshTagView(refresh.update);
  961. relaBills.push(refresh.update.node);
  962. }
  963. return relaBills;
  964. };
  965. const show = function () {
  966. showSelectTab(setting.selector, null, setting.afterShow);
  967. };
  968. const getBillsTagsColor = function (id) {
  969. const billsTags = billsIndexes[id] || [];
  970. return billsTags.length > 0 ? billsTags.map(x => {return x.color}) : undefined;
  971. };
  972. const getBillsTagsInfo = function (id) {
  973. const billsTags = billsIndexes[id] || [];
  974. return billsTags.length > 0 ? billsTags.map(x => {
  975. const tagClass = classIndexes.find(tc => {return tc.color === x.color}) || {};
  976. return {color: x.color, comment: x.comment, tagClass: tagClass.tagClass};
  977. }) : undefined;
  978. };
  979. const getPosTagsColor = function (lid, pos_id) {
  980. const billsTags = billsIndexes[lid] || [];
  981. const posTags = billsTags.filter(x => { return x.pos_id === pos_id; });
  982. return posTags.length > 0 ? posTags.map(x => {return x.color}) : undefined;
  983. };
  984. const getPosTagsInfo = function(lid, pos_id) {
  985. const billsTags = billsIndexes[lid] || [];
  986. const posTags = billsTags.filter(x => { return x.pos_id === pos_id; });
  987. return posTags.length > 0 ? posTags.map(x => {
  988. const tagClass = classIndexes.find(tc => {return tc.color === x.color}) || {};
  989. return {color: x.color, comment: x.comment, tagClass: tagClass.tagClass};
  990. }) : undefined;
  991. };
  992. const afterDeleteBills = function (nodes) {
  993. for (const node of nodes) {
  994. const bi = billsIndexes[node.id];
  995. if (!bi) continue;
  996. delete billsIndexes[node.id];
  997. for (const biTag of bi) {
  998. const delTag = billsTags.find(x => {return x.id === biTag.id});
  999. billsTags.splice(billsTags.indexOf(delTag), 1);
  1000. $('#bills-tag-' + delTag.id).remove();
  1001. }
  1002. }
  1003. };
  1004. const afterDeletePos = function (pos) {
  1005. for (const p of pos) {
  1006. const bi = billsIndexes[p.lid];
  1007. if (!bi) continue;
  1008. const pi = bi.filter(x => { return x.pos_id === p.id; });
  1009. for (const piTag of pi) {
  1010. const delTag = billsTags.find(x => {return x.id === piTag.id});
  1011. billsTags.splice(billsTags.indexOf(delTag), 1);
  1012. $('#bills-tag-' + delTag.id).remove();
  1013. bi.splice(pi.indexOf(piTag), 1);
  1014. }
  1015. }
  1016. };
  1017. $('body').on('click', '[name=bills-tag-locate]', function () {
  1018. const lid = parseInt(this.getAttribute('lid'));
  1019. const pos_id = this.getAttribute('pos_id');
  1020. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), lid, true);
  1021. setting.afterLocated && setting.afterLocated(lid, pos_id);
  1022. });
  1023. $('body').on('click', '[name=bills-tag-edit]', function () {
  1024. const tagId = this.getAttribute('tag-id');
  1025. const tag = billsTags.find(x => {return x.id == tagId});
  1026. if (tag) {
  1027. const obj = $('#bills-tag-' + tag.id);
  1028. $('[name=tag-view]', obj).hide();
  1029. obj.append(getTagEditHtml(tag));
  1030. }
  1031. });
  1032. $('body').on('click', '#tag-edit-cancel', function () {
  1033. const obj = $('[name=tag-edit]').parent();
  1034. $('[name=tag-edit]').remove();
  1035. $('[name=tag-view]', obj).show();
  1036. });
  1037. $('body').on('click', '#tag-del', function () {
  1038. const obj = $('[name=tag-edit]').parent();
  1039. postData(setting.updateUrl, {del: parseInt(obj.attr('tag-id'))}, function (result) {
  1040. if (!result.del) return;
  1041. const bills = updateDatasAndShow(result);
  1042. setting.afterModify && setting.afterModify(bills);
  1043. });
  1044. });
  1045. $('body').on('click', '#tag-edit-ok', function () {
  1046. const obj = $('[name=tag-edit]').parent();
  1047. const data = {
  1048. id: parseInt(obj.attr('tag-id')),
  1049. share: $('#tag-share')[0].checked,
  1050. comment: $('#tag-comment').val(),
  1051. color: $('#tag-change-color').attr('tag-color'),
  1052. };
  1053. postData(setting.updateUrl, {update: data}, function (result) {
  1054. if (!result.update) return;
  1055. const bills = updateDatasAndShow(result);
  1056. setting.afterModify && setting.afterModify(bills);
  1057. });
  1058. });
  1059. $('body').on('click', '[name=tag-color]', function () {
  1060. const tagClass = this.classList[1];
  1061. const ci = classIndexes.find(tc => {return tc.tagClass === tagClass});
  1062. const tcc = $('#tag-change-color');
  1063. tcc.attr('tag-color', ci.color);
  1064. tcc.find('i').attr('class', 'fa fa-tag ' + tagClass);
  1065. });
  1066. $('body').on('click', '[tagType]', function () {
  1067. const tagClass = this.getAttribute('tagType');
  1068. if (tagClass === 'all') {
  1069. $('#bills-tag-filter').attr('class', 'fa fa-list-ol');
  1070. } else {
  1071. $('#bills-tag-filter').attr('class', 'fa fa-tag ' + tagClass);
  1072. }
  1073. searchTagsAndShow();
  1074. });
  1075. // 防抖
  1076. function debounce(fun, delay) {
  1077. let timer = null;
  1078. return function () {
  1079. if (timer) {
  1080. clearTimeout(timer);
  1081. }
  1082. timer = setTimeout(fun, delay);
  1083. }
  1084. }
  1085. $('#bills-tag-list').bind('scroll', debounce(function (e) {
  1086. const obj = $('#bills-tag-list');
  1087. var sum = obj[0].scrollHeight;
  1088. if (sum <= obj.scrollTop() + obj.height()) {
  1089. loadViewTags();
  1090. }
  1091. }, 300));
  1092. $('#bills-tag-clear').bind('click', () => {
  1093. if (!$('#bills-tag-keyword').val()) return;
  1094. $('#bills-tag-keyword').val('');
  1095. searchTagsAndShow();
  1096. });
  1097. $('#bills-tag-search').bind('click', () => {searchTagsAndShow();});
  1098. $('#bills-tag-keyword').bind('keydown', e => {if (e.keyCode === 13) searchTagsAndShow();});
  1099. return { loadDatas, updateDatasAndShow, show,
  1100. getBillsTagsColor, getBillsTagsInfo, getPosTagsColor, getPosTagsInfo,
  1101. refreshBillsTagView, refreshPosTagView,
  1102. afterDeleteBills, afterDeletePos }
  1103. };
  1104. $.sumLoadMiss = function (setting) {
  1105. if (!setting.spreadSetting) {
  1106. setting.spreadSetting = {
  1107. cols: [
  1108. { title: '清单编号', field: 'b_code', width: 80, formatter: '@' },
  1109. { title: '清单名称', field: 'name', width: 120, formatter: '@' },
  1110. { title: '单位', field: 'unit', width: 50, formatter: '@' },
  1111. { title: setting.type === 'stage' ? '合同数量' : '数量', field: 'qty', width: 60 },
  1112. { title: '变更数量', field: 'qc_qty', width: 60, visible: setting.type === 'stage' },
  1113. {
  1114. title: '类型', field: 'type', width: 100, getValue: function (x) {
  1115. switch (x.type) {
  1116. case 'less': return '数量变少';
  1117. case 'miss': return '找不到清单';
  1118. case 'qc-conflict': return '变更冲突(已调用变更令)';
  1119. default: return '';
  1120. }
  1121. }
  1122. }
  1123. ],
  1124. emptyRows: 0,
  1125. headRows: 1,
  1126. headRowHeight: [32],
  1127. defaultRowHeight: 21,
  1128. headerFont: '12px 微软雅黑',
  1129. font: '12px 微软雅黑',
  1130. selectedBackColor: '#fffacd',
  1131. readOnly: true,
  1132. };
  1133. }
  1134. const clearMissData = function () {
  1135. if (setting.storeKey) removeLocalCache(setting.storeKey);
  1136. };
  1137. const autoShowHistory = function (show) {
  1138. if (setting.storeKey) {
  1139. setLocalCache(setting.storeKey + '-showHis', show.toString());
  1140. }
  1141. };
  1142. if (setting.selector && setting.relaSpread) {
  1143. const resultId = setting.id + '-spread';
  1144. const obj = $(setting.selector);
  1145. obj.html(
  1146. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  1147. ' </div>'
  1148. );
  1149. autoFlashHeight();
  1150. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  1151. const sheet = spread.getActiveSheet();
  1152. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  1153. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1154. if (!setting.relaSpread) return;
  1155. const sheet = info.sheet;
  1156. const data = sheet.zh_data;
  1157. if (!data) { return }
  1158. const curBills = data[info.row];
  1159. if (!curBills) { return }
  1160. if (curBills.ledger_id) {
  1161. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  1162. if (setting.afterLocated) {
  1163. setting.afterLocated();
  1164. }
  1165. } else {
  1166. toastr.warning('找不到该清单');
  1167. }
  1168. });
  1169. const loadMissData = function (data, his = false) {
  1170. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  1171. if (!sourceTree) return;
  1172. for (const d of data) {
  1173. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  1174. }
  1175. data.sort(function (a, b) {
  1176. return a.serialNo - b.serialNo;
  1177. });
  1178. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  1179. if (!his && setting.storeKey) {
  1180. setLocalCache(setting.storeKey, JSON.stringify(data));
  1181. }
  1182. $(setting.tabSelector).show();
  1183. };
  1184. const showMissList = function () {
  1185. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  1186. $('a', '.side-menu').removeClass('active');
  1187. tab.addClass('active');
  1188. $('.tab-content .tab-pane').removeClass('active');
  1189. tabPanel.addClass('active');
  1190. showSideTools(true);
  1191. spread.refresh();
  1192. if (setting.afterShow) setting.afterShow();
  1193. };
  1194. const loadHisMissData = function () {
  1195. if (setting.storeKey) {
  1196. const storeStr = getLocalCache(setting.storeKey);
  1197. const storeData = storeStr ? JSON.parse(storeStr) : [];
  1198. if (storeData.length > 0) {
  1199. loadMissData(storeData, true);
  1200. const showHis = getLocalCache(setting.storeKey + '-showHis');
  1201. if (showHis === 'true') {
  1202. showMissList();
  1203. removeLocalCache(setting.storeKey + '-showHis');
  1204. }
  1205. }
  1206. }
  1207. };
  1208. return {
  1209. spread: spread,
  1210. loadMissData: loadMissData,
  1211. clearMissData: clearMissData,
  1212. loadHisMissData: loadHisMissData,
  1213. show: showMissList,
  1214. autoShowHistory: autoShowHistory,
  1215. };
  1216. } else {
  1217. const loadMissData = function (data) {
  1218. if (setting.storeKey) {
  1219. setLocalCache(setting.storeKey, JSON.stringify(data));
  1220. }
  1221. };
  1222. return {
  1223. loadErrorData: loadMissData,
  1224. clearErrorData: clearMissData,
  1225. autoShowHistory: autoShowHistory,
  1226. };
  1227. }
  1228. };
  1229. $.stdLib = function (setting) {
  1230. if (!setting.selector) return;
  1231. const obj = $(setting.selector);
  1232. const stdLibHtml = setting.libs.map(l => {
  1233. return `<option value="${l.id}" >${l.name}</option>`;
  1234. });
  1235. const relaSelect = {
  1236. showLevel: `std-${setting.stdType}-sl`,
  1237. searchText: `std-${setting.stdType}-st`,
  1238. searchResult: `std-${setting.stdType}-sr`,
  1239. searchPre: `std-${setting.stdType}-sp`,
  1240. searchNext: `std-${setting.stdType}-sn`,
  1241. searchClose: `std-${setting.stdType}-sc`,
  1242. addParent: `std-${setting.stdType}-wp`
  1243. };
  1244. const hiddenOption = setting.showAddType ? '' : 'style="display: none;"';
  1245. obj.html(
  1246. '<div class="sjs-bar d-flex">\n' +
  1247. ' <div class="dropdown mr-1">\n' +
  1248. ' <button class="btn btn-sm btn-light dropdown-toggle text-primary" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1249. ' <i class="fa fa-list-ol"></i> 显示层级\n' +
  1250. ' </button>\n' +
  1251. ' <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">\n' +
  1252. ` <a class="dropdown-item" href="javascript: void(0);" tag="1" name="${relaSelect.showLevel}">第一层</a>\n` +
  1253. ` <a class="dropdown-item" href="javascript: void(0);" tag="2" name="${relaSelect.showLevel}">第二层</a>\n` +
  1254. ` <a class="dropdown-item" href="javascript: void(0);" tag="3" name="${relaSelect.showLevel}">第三层</a>\n` +
  1255. ` <a class="dropdown-item" href="javascript: void(0);" tag="4" name="${relaSelect.showLevel}">第四层</a>\n` +
  1256. ` <a class="dropdown-item" href="javascript: void(0);" tag="5" name="${relaSelect.showLevel}">第五层</a>\n` +
  1257. ` <a class="dropdown-item" href="javascript: void(0);" tag="last" name="${relaSelect.showLevel}">最底层</a>\n` +
  1258. ' </div>\n' +
  1259. ' </div>' +
  1260. ` <div class="input-group input-group-sm pr-1"><select class="form-control form-control-sm col-auto">${stdLibHtml.join('')}</select></div>\n` +
  1261. ' <div class="ml-1">\n' +
  1262. ' <div class="dropdown">\n' +
  1263. ' <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1264. ' <i class="fa fa-search"></i>\n' +
  1265. ' </button>\n' +
  1266. ' <div class="dropdown-menu dropdown-menu-right">\n' +
  1267. ' <div class="px-2 border-primary border-1 d-flex" style="width: 300px">\n' +
  1268. ' <div class="d-inline-block">\n' +
  1269. ' <div class="input-group input-group-sm mr-1">' +
  1270. ` <input type="text" class="form-control" placeholder="输入编号/名称查找" id="${relaSelect.searchText}">\n` +
  1271. ` <div class="input-group-append" ><span class="input-group-text" id="${relaSelect.searchResult}">结果:0</span></div>\n` +
  1272. ' <div class="input-group-append" >\n' +
  1273. ` <button class="btn btn-outline-secondary" type="button" title="上一个" id="${relaSelect.searchPre}"><i class="fa fa-angle-double-left"></i></button>\n` +
  1274. ` <button class="btn btn-outline-secondary" type="button" title="下一个" id="${relaSelect.searchNext}"><i class="fa fa-angle-double-right"></i></button>\n` +
  1275. ' </div>\n' +
  1276. ' </div>\n' +
  1277. ' </div>\n' +
  1278. ` <div class="d-inline-block"><button class="btn btn-light text-danger btn-sm ml-1" type="button" id="${relaSelect.searchClose}"><i class="fa fa-remove"></i></button></div>\n` +
  1279. ' </div>\n' +
  1280. ' </div>\n' +
  1281. ' </div>\n' +
  1282. ' </div>' +
  1283. ` <div class="ml-1" ${hiddenOption}>\n` +
  1284. ' <div class="dropdown">\n' +
  1285. ' <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1286. ' <i class="fa fa-cog"></i>\n' +
  1287. ' </button>\n' +
  1288. ' <div class="dropdown-menu dropdown-menu-right">\n' +
  1289. ' <div class="px-2 border-primary border-1 d-flex" style="width: 100px">\n' +
  1290. ' <div class="form-check form-check-inline">\n' +
  1291. ` <input class="form-check-input pt-1" type="checkbox" id="${relaSelect.addParent}">\n` +
  1292. ` <label class="form-check-label" for="${relaSelect.addParent}">添加父项</label>\n` +
  1293. ' </div>' +
  1294. ' </div>\n' +
  1295. ' </div>\n' +
  1296. ' </div>\n' +
  1297. ' </div>' +
  1298. '</div>\n' +
  1299. `<div id="std-${setting.stdType}-spread" class="sjs-sh"></div>\n`
  1300. );
  1301. autoFlashHeight();
  1302. const pathTree = createNewPathTree('base', setting.treeSetting);
  1303. const spreadSetting = {
  1304. cols: [
  1305. {title: '名称', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  1306. {title: '单位', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  1307. ],
  1308. treeCol: 0,
  1309. emptyRows: 0,
  1310. headRows: 1,
  1311. headRowHeight: [32],
  1312. defaultRowHeight: 21,
  1313. headerFont: '12px 微软雅黑',
  1314. font: '12px 微软雅黑',
  1315. headColWidth: [30],
  1316. selectedBackColor: '#fffacd',
  1317. readOnly: true,
  1318. stdType: setting.stdType,
  1319. };
  1320. spreadSetting.cols.unshift(setting.stdType === 'xmj'
  1321. ? {title: '项目节编号', field: 'code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'}
  1322. : {title: '清单编号', field: 'b_code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'});
  1323. const spread = SpreadJsObj.createNewSpread($(`#std-${setting.stdType}-spread`)[0]);
  1324. const sheet = spread.getActiveSheet();
  1325. if (setting.spreadSetting) setting.spreadSetting.stdType = setting.stdType;
  1326. SpreadJsObj.initSheet(sheet, setting.spreadSetting || spreadSetting);
  1327. if (setting.cellDoubleClick) sheet.bind(spreadNS.Events.CellDoubleClick, function(e, info) {
  1328. const stdSheet = info.sheet;
  1329. if (!stdSheet.zh_setting || !stdSheet.zh_tree) return;
  1330. const stdTree = stdSheet.zh_tree;
  1331. const stdNode = stdTree.nodes[info.row];
  1332. if (!stdNode) { return; }
  1333. const updateData = { postType: 'add-std', postData: { stdType: stdSheet.zh_setting.stdType, withParent: $(`#${relaSelect.addParent}`)[0].checked } };
  1334. const fullPath = stdTree.getAllParents(stdNode);
  1335. fullPath.push(stdNode);
  1336. fullPath.sort((x, y) => { return x.level - y.level; });
  1337. updateData.postData.stdData = fullPath.map(x => {
  1338. return { code: x.code || '', b_code: x.b_code || '', name: x.name, unit: x.unit, source: x.source, node_type: x.node_type }
  1339. });
  1340. updateData.postData.stdNode = stdNode;
  1341. setting.cellDoubleClick(updateData, stdNode, stdTree);
  1342. });
  1343. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  1344. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  1345. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1346. }
  1347. });
  1348. sheet.bind(spreadNS.Events.TopRowChanged, function (e, info) {
  1349. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1350. });
  1351. const cacheLib = [];
  1352. const cacheKey = {
  1353. lib: setting.page + '-' + setting.tid + '-' + setting.stdType,
  1354. };
  1355. $('select', setting.selector).change(function () {
  1356. loadLib(parseInt(this.value), true);
  1357. });
  1358. (function (select, sheet) {
  1359. $(select).click(function () {
  1360. if (!sheet.zh_tree) return;
  1361. const tag = $(this).attr('tag');
  1362. const tree = sheet.zh_tree;
  1363. setTimeout(() => {
  1364. switch (tag) {
  1365. case "1":
  1366. case "2":
  1367. case "3":
  1368. case "4":
  1369. case "5":
  1370. tree.expandByLevel(parseInt(tag));
  1371. SpreadJsObj.refreshTreeRowVisible(sheet);
  1372. break;
  1373. case "last":
  1374. tree.expandByCustom(() => { return true; });
  1375. SpreadJsObj.refreshTreeRowVisible(sheet);
  1376. break;
  1377. }
  1378. }, 100);
  1379. });
  1380. })(`a[name=${relaSelect.showLevel}]`, sheet);
  1381. const loadLib = function(listId, reload = false) {
  1382. cacheKey.node = cacheLib.lib + '-' + listId;
  1383. const locateMemory = function () {
  1384. if (!reload) {
  1385. SpreadJsObj.loadTopAndSelect(sheet, cacheKey.node);
  1386. } else {
  1387. removeLocalCache(cacheKey.node);
  1388. }
  1389. };
  1390. const cacheData = cacheLib.find(function (lib) {
  1391. return lib.id === listId;
  1392. });
  1393. if (cacheData) {
  1394. pathTree.loadDatas(cacheData.data);
  1395. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1396. locateMemory();
  1397. setLocalCache(cacheKey.lib, listId);
  1398. } else {
  1399. postData(`/std-lib/get-data`, {stdType: setting.stdType, list_id: listId}, function (data) {
  1400. cacheLib.push({id: listId, data: data});
  1401. pathTree.loadDatas(data);
  1402. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1403. locateMemory();
  1404. setLocalCache(cacheKey.lib, listId);
  1405. });
  1406. }
  1407. };
  1408. const _loadCacheLib = function() {
  1409. let libId = getLocalCache(cacheKey.lib);
  1410. if (libId) {
  1411. $('select', setting.selector).val(libId);
  1412. loadLib(parseInt(libId));
  1413. } else {
  1414. loadLib(parseInt($('select', setting.selector).val()));
  1415. }
  1416. };
  1417. _loadCacheLib();
  1418. const searchObj = {
  1419. result: [],
  1420. cur: 0,
  1421. searchStdNode: function() {
  1422. const keyword = $(`#${relaSelect.searchText}`).val();
  1423. searchObj.result = keyword ? pathTree.nodes.filter(x => {
  1424. return x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0;
  1425. }) : [];
  1426. // searchObj.result = [];
  1427. // for (const x of pathTree.nodes) {
  1428. // if (x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0) {
  1429. // searchObj.result.push(x);
  1430. // }
  1431. // }
  1432. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${searchObj.result.length}`;
  1433. searchObj.cur = 0;
  1434. if (searchObj.result.length > 0) {
  1435. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]));
  1436. }
  1437. },
  1438. searchPre: function () {
  1439. if (searchObj.result.length === 0) return;
  1440. searchObj.cur = searchObj.cur === 0 ? searchObj.result.length - 1 : this.cur - 1;
  1441. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1442. },
  1443. searchNext: function () {
  1444. if (searchObj.result.length === 0) return;
  1445. searchObj.cur = searchObj.cur === searchObj.result.length - 1 ? 0 : searchObj.cur + 1;
  1446. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1447. },
  1448. clear: function () {
  1449. $(`#${relaSelect.searchText}`).val('');
  1450. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${0}`;
  1451. searchObj.result = [];
  1452. searchObj.cur = 0;
  1453. }
  1454. };
  1455. $(`#${relaSelect.searchText}`).change(searchObj.searchStdNode);
  1456. $(`#${relaSelect.searchPre}`).click(function (e) {
  1457. searchObj.searchPre();
  1458. e.stopPropagation();
  1459. });
  1460. $(`#${relaSelect.searchNext}`).click(function (e) {
  1461. searchObj.searchNext();
  1462. e.stopPropagation();
  1463. });
  1464. $(`#${relaSelect.searchClose}`).click(function (e) {
  1465. searchObj.clear();
  1466. e.stopPropagation();
  1467. });
  1468. return { spread }
  1469. };
  1470. $.gljLib = function (setting) {
  1471. if (!setting.selector) return;
  1472. const obj = $(setting.selector);
  1473. const stdLibHtml = setting.libs.map(l => {
  1474. return `<option value="${l.id}" >${l.name}</option>`;
  1475. });
  1476. const relaSelect = {
  1477. showLevel: `std-${setting.stdType}-sl`,
  1478. searchText: `std-${setting.stdType}-st`,
  1479. searchResult: `std-${setting.stdType}-sr`,
  1480. searchPre: `std-${setting.stdType}-sp`,
  1481. searchNext: `std-${setting.stdType}-sn`,
  1482. searchClose: `std-${setting.stdType}-sc`,
  1483. };
  1484. obj.html(
  1485. '<div class="sjs-bar d-flex">\n' +
  1486. ` <div class="input-group input-group-sm pr-1"><select class="form-control form-control-sm col-auto">${stdLibHtml.join('')}</select></div>\n` +
  1487. ' <div class="ml-1">\n' +
  1488. ' <div class="dropdown">\n' +
  1489. ' <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1490. ' <i class="fa fa-search"></i>\n' +
  1491. ' </button>\n' +
  1492. ' <div class="dropdown-menu dropdown-menu-right">\n' +
  1493. ' <div class="px-2 border-primary border-1 d-flex" style="width: 300px">\n' +
  1494. ' <div class="d-inline-block">\n' +
  1495. ' <div class="input-group input-group-sm mr-1">' +
  1496. ` <input type="text" class="form-control" placeholder="输入编号/名称查找" id="${relaSelect.searchText}">\n` +
  1497. ` <div class="input-group-append" ><span class="input-group-text" id="${relaSelect.searchResult}">结果:0</span></div>\n` +
  1498. ' <div class="input-group-append" >\n' +
  1499. ` <button class="btn btn-outline-secondary" type="button" title="上一个" id="${relaSelect.searchPre}"><i class="fa fa-angle-double-left"></i></button>\n` +
  1500. ` <button class="btn btn-outline-secondary" type="button" title="下一个" id="${relaSelect.searchNext}"><i class="fa fa-angle-double-right"></i></button>\n` +
  1501. ' </div>\n' +
  1502. ' </div>\n' +
  1503. ' </div>\n' +
  1504. ` <div class="d-inline-block"><button class="btn btn-light text-danger btn-sm ml-1" type="button" id="${relaSelect.searchClose}"><i class="fa fa-remove"></i></button></div>\n` +
  1505. ' </div>\n' +
  1506. ' </div>\n' +
  1507. ' </div>\n' +
  1508. ' </div>' +
  1509. '</div>\n' +
  1510. `<div id="std-${setting.stdType}-spread" class="${setting.spreadClassCss ? setting.spreadClassCss : 'sjs-sh'}"></div>\n`
  1511. );
  1512. autoFlashHeight();
  1513. const pathTree = createNewPathTree('base', setting.treeSetting);
  1514. const spreadSetting = {
  1515. cols: [
  1516. {title: '名称', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  1517. {title: '单位', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  1518. ],
  1519. treeCol: 0,
  1520. emptyRows: 0,
  1521. headRows: 1,
  1522. headRowHeight: [32],
  1523. defaultRowHeight: 21,
  1524. headerFont: '12px 微软雅黑',
  1525. font: '12px 微软雅黑',
  1526. headColWidth: [30],
  1527. selectedBackColor: '#fffacd',
  1528. readOnly: true,
  1529. stdType: setting.stdType,
  1530. };
  1531. spreadSetting.cols.unshift(setting.stdType === 'xmj'
  1532. ? {title: '项目节编号', field: 'code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'}
  1533. : {title: '清单编号', field: 'b_code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'});
  1534. const spread = SpreadJsObj.createNewSpread($(`#std-${setting.stdType}-spread`)[0]);
  1535. const sheet = spread.getActiveSheet();
  1536. SpreadJsObj.initSheet(sheet, setting.spreadSetting ? setting.spreadSetting : spreadSetting);
  1537. if (setting.cellDoubleClick) sheet.bind(spreadNS.Events.CellDoubleClick, setting.cellDoubleClick);
  1538. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  1539. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  1540. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1541. }
  1542. });
  1543. sheet.bind(spreadNS.Events.TopRowChanged, function (e, info) {
  1544. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1545. });
  1546. const cacheLib = [];
  1547. const cacheKey = {
  1548. lib: setting.page + '-' + setting.tid + '-' + setting.stdType,
  1549. };
  1550. $('select', setting.selector).change(function () {
  1551. loadLib(parseInt(this.value), true);
  1552. });
  1553. const loadLib = function(listId, reload = false) {
  1554. cacheKey.node = cacheLib.lib + '-' + listId;
  1555. const locateMemory = function () {
  1556. if (!reload) {
  1557. SpreadJsObj.loadTopAndSelect(sheet, cacheKey.node);
  1558. } else {
  1559. removeLocalCache(cacheKey.node);
  1560. }
  1561. };
  1562. const cacheData = cacheLib.find(function (lib) {
  1563. return lib.id === listId;
  1564. });
  1565. if (cacheData) {
  1566. pathTree.loadDatas(cacheData.data);
  1567. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1568. locateMemory();
  1569. setLocalCache(cacheKey.lib, listId);
  1570. } else {
  1571. postData(`/std-lib/get-data`, {stdType: setting.stdType, list_id: listId}, function (data) {
  1572. cacheLib.push({id: listId, data: data});
  1573. pathTree.loadDatas(data);
  1574. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1575. locateMemory();
  1576. setLocalCache(cacheKey.lib, listId);
  1577. });
  1578. }
  1579. };
  1580. const _loadCacheLib = function() {
  1581. let libId = getLocalCache(cacheKey.lib);
  1582. if (libId) {
  1583. $('select', setting.selector).val(libId);
  1584. loadLib(parseInt(libId));
  1585. } else {
  1586. loadLib(parseInt($('select', setting.selector).val()));
  1587. }
  1588. };
  1589. _loadCacheLib();
  1590. const searchObj = {
  1591. result: [],
  1592. cur: 0,
  1593. searchStdNode: function() {
  1594. const keyword = $(`#${relaSelect.searchText}`).val();
  1595. searchObj.result = keyword ? pathTree.nodes.filter(x => {
  1596. return x.code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0;
  1597. }) : [];
  1598. // searchObj.result = [];
  1599. // for (const x of pathTree.nodes) {
  1600. // if (x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0) {
  1601. // searchObj.result.push(x);
  1602. // }
  1603. // }
  1604. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${searchObj.result.length}`;
  1605. searchObj.cur = 0;
  1606. if (searchObj.result.length > 0) {
  1607. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]));
  1608. }
  1609. },
  1610. searchPre: function () {
  1611. if (searchObj.result.length === 0) return;
  1612. searchObj.cur = searchObj.cur === 0 ? searchObj.result.length - 1 : this.cur - 1;
  1613. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1614. },
  1615. searchNext: function () {
  1616. if (searchObj.result.length === 0) return;
  1617. searchObj.cur = searchObj.cur === searchObj.result.length - 1 ? 0 : searchObj.cur + 1;
  1618. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1619. },
  1620. clear: function () {
  1621. $(`#${relaSelect.searchText}`).val('');
  1622. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${0}`;
  1623. searchObj.result = [];
  1624. searchObj.cur = 0;
  1625. }
  1626. };
  1627. $(`#${relaSelect.searchText}`).change(searchObj.searchStdNode);
  1628. $(`#${relaSelect.searchPre}`).click(function (e) {
  1629. searchObj.searchPre();
  1630. e.stopPropagation();
  1631. });
  1632. $(`#${relaSelect.searchNext}`).click(function (e) {
  1633. searchObj.searchNext();
  1634. e.stopPropagation();
  1635. });
  1636. $(`#${relaSelect.searchClose}`).click(function (e) {
  1637. searchObj.clear();
  1638. e.stopPropagation();
  1639. });
  1640. return { spread }
  1641. };
  1642. })(jQuery);