cs_tools.js 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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 html = [], pageLength = 15;
  741. let billsTags = [], classIndexes = [], billsIndexes = {}, curShow = [];
  742. html.push('<div class="sjs-bar d-flex justify-content-between">');
  743. // 下拉过滤
  744. html.push('<div class="dropdown mr-2">');
  745. 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>');
  746. 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">');
  747. html.push('<a class="dropdown-item" href="javascript: void(0);" tagType="all" ><i class="fa fa-list-ol"></i></a>');
  748. for (const t of setting.tagType) {
  749. html.push(`<a class="dropdown-item ${t.tagClass}" href="javascript: void(0);" tagType="${t.tagClass}" ><i class="fa fa-tag"></i></a>`);
  750. t.tags = [];
  751. classIndexes.push(t);
  752. }
  753. html.push('</div>', '</div>');
  754. // 搜索框
  755. html.push('<div class="input-group input-group-sm">');
  756. html.push('<input type="text" class="form-control" placeholder="可查找 项目节编号 / 清单编号 /名称" id="bills-tag-keyword" autocomplete="off">');
  757. html.push('<div class="input-group-append">', '<div class="input-group-cancel">',
  758. '<a href="javascript: void(0);" id="bills-tag-clear" class="text-danger"><i class="fa fa-times-circle" title="移除搜索结果"></i></a>', '</div>',
  759. '<button class="btn btn-outline-secondary" type="button" id="bills-tag-search">搜索</button>', '</div>');
  760. html.push('</div>');
  761. html.push('</div>');
  762. // 书签列表
  763. html.push('<div class="sjs-sh" style="overflow: auto;" id="bills-tag-list"></div>');
  764. obj.html(html.join(''));
  765. const clearViewTags = function () {
  766. const viewTags = $('.tag-item', obj);
  767. if (viewTags && viewTags.length > 0) viewTags.remove();
  768. billsTags.forEach(x => {x.display = false});
  769. };
  770. const getTagEditHtml = function(tag) {
  771. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  772. const tagHtml = [];
  773. tagHtml.push('<div name="tag-edit">');
  774. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  775. 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>`);
  776. // 下拉选择颜色
  777. tagHtml.push('<div class="dropdown-menu" aria-labelledby="tag-change-color" style="min-width:60px">',
  778. '<a class="dropdown-item text-primary" href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  779. '<a class="dropdown-item text-success " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  780. '<a class="dropdown-item text-danger " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  781. '<a class="dropdown-item text-warning " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>',
  782. '<a class="dropdown-item text-info " href="javascript: void(0);" name="tag-color"><i class="fa fa-tint"></i></a>', '</div>');
  783. tagHtml.push('</div>');
  784. tag.node && tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '');
  785. tagHtml.push('</div>');
  786. tagHtml.push('<div class="card-body p-2">');
  787. tagHtml.push('<p class="card-text">', '<textarea class="form-control form-control-sm p-1" id="tag-comment">', tag.comment, '</textarea>', '</p>');
  788. tagHtml.push('<div class="d-flex justify-content-between">');
  789. // 参与人可见
  790. tagHtml.push('<div class="custom-control custom-switch mr-2">');
  791. tagHtml.push('<input type="checkbox" class="custom-control-input custom-control-warning-input" id="tag-share"', tag.share ? 'checked' : '', '>');
  792. 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>');
  793. tagHtml.push('</div>');
  794. // 编辑按钮
  795. 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>',
  796. '<button type="button" class="btn btn-sm btn-outline-success mr-1" id="tag-edit-ok"><i class="fa fa-check"></i> 确定</button>',
  797. '<button type="button" class="btn btn-sm btn-outline-secondary" id="tag-edit-cancel">取消</button>', '</div>');
  798. tagHtml.push('</div>');
  799. tagHtml.push('</div>');
  800. tagHtml.push('</div>');
  801. return tagHtml.join('');
  802. };
  803. const getTagDisplayHtml = function (tag) {
  804. const tagClass = classIndexes.find(x => {return x.color === tag.color}) || {};
  805. const tagHtml = [];
  806. tagHtml.push('<div name="tag-view">');
  807. tagHtml.push('<div class="card-header p-2"><div class="dropdown">');
  808. tagHtml.push(`<div class="pull-left mr-2"><i class="fa fa-tag ${tagClass.tagClass}"></i></div>`);
  809. tagHtml.push('</div>');
  810. tag.node && tagHtml.push((tag.node.code || '') + (tag.node.b_code || ''), ' / ', tag.node.name || '');
  811. if (tag.share) {
  812. 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>`);
  813. }
  814. tagHtml.push('<div class="pull-right edit-tag-btn">');
  815. const lid = tag.node ? tag.node[setting.treeId] : -1;
  816. tagHtml.push(`<a class="mr-1" name="bills-tag-locate" href="javascript: void(0);" lid="${lid}"><i class="fa fa-crosshairs"></i> 定位</a>`);
  817. 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>`);
  818. tagHtml.push('</div>');
  819. tagHtml.push('<div class="card-body p-2">', '<p class="card-text">', tag.comment, '</p>', '</div>');
  820. tagHtml.push('</div>');
  821. return tagHtml.join('');
  822. };
  823. const searchTagsAndShow = function () {
  824. const keyword = $('#bills-tag-keyword').val();
  825. const filterClass = $('#bills-tag-filter')[0].classList;
  826. const tagClass = filterClass.length > 2 ? filterClass[2] : null;
  827. const ci = tagClass ? classIndexes.find(x => {return x.tagClass === tagClass}) : null;
  828. curShow = billsTags.filter(x => {
  829. if (ci && ci.color !== x.color) return false;
  830. if (!keyword) return true;
  831. if (x.node.code && x.node.code.indexOf(keyword) >= 0) return true;
  832. if (x.node.b_code && x.node.b_code.indexOf(keyword) >= 0) return true;
  833. if (x.node.name && x.node.name.indexOf(keyword) >= 0) return true;
  834. return false;
  835. });
  836. reloadViewTags();
  837. };
  838. const refreshTagView = function (tag) {
  839. const obj = $('#bills-tag-' + tag.id);
  840. if (obj && obj.length > 0) {
  841. obj.html(getTagDisplayHtml(tag));
  842. }
  843. };
  844. const refreshBillsTagView = function (bills) {
  845. const bi = billsIndexes[bills.id] || [];
  846. for (const tag of bi) {
  847. refreshTagView(tag);
  848. }
  849. };
  850. const reviewTag = function (tag, isTop = false) {
  851. const obj = $('#bills-tag-' + tag.id);
  852. if (obj && obj.length > 0) {
  853. obj.html(getTagDisplayHtml(tag));
  854. } else {
  855. const objHtml = [];
  856. objHtml.push(`<div class="card border-primary my-2 tag-item" id="bills-tag-${tag.id}" tag-id="${tag.id}">`);
  857. objHtml.push(getTagDisplayHtml(tag));
  858. objHtml.push('</div>');
  859. if (isTop) {
  860. $('#bills-tag-list').prepend(objHtml.join(''));
  861. } else {
  862. $('#bills-tag-list').append(objHtml.join(''));
  863. }
  864. }
  865. tag.display = true;
  866. };
  867. const loadViewTags = function () {
  868. let showCount = 0;
  869. for (const t of curShow) {
  870. if (showCount >= pageLength) continue;
  871. if (t.display) continue;
  872. reviewTag(t);
  873. showCount++;
  874. }
  875. };
  876. const reloadViewTags = function () {
  877. clearViewTags();
  878. $("#bills-tag-lis").scrollTop(0);
  879. loadViewTags();
  880. };
  881. const _addToBillsIndex = function(data, isTop = false) {
  882. let bi = billsIndexes[data.lid];
  883. if (!bi) {
  884. bi = [];
  885. billsIndexes[data.lid] = bi;
  886. }
  887. isTop ? bi.unshift(data) : bi.push(data);
  888. };
  889. const loadDatas = function (datas) {
  890. billsTags = [];
  891. billsIndexes = {};
  892. for (const d of datas) {
  893. if (!d.node) continue;
  894. billsTags.push(d);
  895. _addToBillsIndex(d);
  896. }
  897. curShow = billsTags;
  898. reloadViewTags();
  899. };
  900. const updateDatas = function (data) {
  901. const refresh = {};
  902. if (data.add) {
  903. billsTags.push(data.add);
  904. _addToBillsIndex(data.add, true);
  905. refresh.add = data.add;
  906. }
  907. if (data.del) {
  908. const delTag = billsTags.find(x => {return x.id === data.del});
  909. billsTags.splice(billsTags.indexOf(delTag), 1);
  910. if (delTag.node) {
  911. const bi = billsIndexes[delTag.node.id];
  912. bi.splice(bi.indexOf(delTag), 1);
  913. }
  914. refresh.del = delTag;
  915. }
  916. if (data.update) {
  917. const updateTag = billsTags.find(x => {return x.id === data.update.id});
  918. for (const prop in data.update) {
  919. updateTag[prop] = data.update[prop];
  920. }
  921. refresh.update = updateTag;
  922. }
  923. return refresh;
  924. };
  925. const updateDatasAndShow = function (data) {
  926. const relaBills = [];
  927. const refresh = updateDatas(data);
  928. if (refresh.add) {
  929. reviewTag(refresh.add, true);
  930. relaBills.push(refresh.add.node);
  931. }
  932. if (refresh.del) {
  933. $('#bills-tag-' + refresh.del.id).remove();
  934. relaBills.push(refresh.del.node);
  935. }
  936. if (refresh.update) {
  937. refreshTagView(refresh.update);
  938. relaBills.push(refresh.update.node);
  939. }
  940. return relaBills;
  941. };
  942. const show = function () {
  943. showSelectTab(setting.selector, null, setting.afterShow);
  944. };
  945. const getBillsTagsColor = function (id) {
  946. const billsTags = billsIndexes[id] || [];
  947. return billsTags.length > 0 ? billsTags.map(x => {return x.color}) : undefined;
  948. };
  949. const getBillsTagsInfo = function (id) {
  950. const billsTags = billsIndexes[id] || [];
  951. return billsTags.length > 0 ? billsTags.map(x => {
  952. const tagClass = classIndexes.find(tc => {return tc.color === x.color}) || {};
  953. return {color: x.color, comment: x.comment, tagClass: tagClass.tagClass};
  954. }) : undefined;
  955. };
  956. const afterDeleteBills = function (nodes) {
  957. for (const node of nodes) {
  958. const bi = billsIndexes[node.id];
  959. if (!bi) continue;
  960. delete billsIndexes[node.id];
  961. for (const biTag of bi) {
  962. const delTag = billsTags.find(x => {return x.id === biTag.id});
  963. billsTags.splice(billsTags.indexOf(delTag), 1);
  964. $('#bills-tag-' + delTag.id).remove();
  965. }
  966. }
  967. };
  968. $('body').on('click', '[name=bills-tag-locate]', function () {
  969. const lid = parseInt(this.getAttribute('lid'));
  970. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), lid, true);
  971. setting.afterLocated && setting.afterLocated();
  972. });
  973. $('body').on('click', '[name=bills-tag-edit]', function () {
  974. const tagId = this.getAttribute('tag-id');
  975. const tag = billsTags.find(x => {return x.id == tagId});
  976. if (tag) {
  977. const obj = $('#bills-tag-' + tag.id);
  978. $('[name=tag-view]', obj).hide();
  979. obj.append(getTagEditHtml(tag));
  980. }
  981. });
  982. $('body').on('click', '#tag-edit-cancel', function () {
  983. const obj = $('[name=tag-edit]').parent();
  984. $('[name=tag-edit]').remove();
  985. $('[name=tag-view]', obj).show();
  986. });
  987. $('body').on('click', '#tag-del', function () {
  988. const obj = $('[name=tag-edit]').parent();
  989. postData(setting.updateUrl, {del: parseInt(obj.attr('tag-id'))}, function (result) {
  990. if (!result.del) return;
  991. const bills = updateDatasAndShow(result);
  992. setting.afterModify && setting.afterModify(bills);
  993. });
  994. });
  995. $('body').on('click', '#tag-edit-ok', function () {
  996. const obj = $('[name=tag-edit]').parent();
  997. const data = {
  998. id: parseInt(obj.attr('tag-id')),
  999. share: $('#tag-share')[0].checked,
  1000. comment: $('#tag-comment').val(),
  1001. color: $('#tag-change-color').attr('tag-color'),
  1002. };
  1003. postData(setting.updateUrl, {update: data}, function (result) {
  1004. if (!result.update) return;
  1005. const bills = updateDatasAndShow(result);
  1006. setting.afterModify && setting.afterModify(bills);
  1007. });
  1008. });
  1009. $('body').on('click', '[name=tag-color]', function () {
  1010. const tagClass = this.classList[1];
  1011. const ci = classIndexes.find(tc => {return tc.tagClass === tagClass});
  1012. const tcc = $('#tag-change-color');
  1013. tcc.attr('tag-color', ci.color);
  1014. tcc.find('i').attr('class', 'fa fa-tag ' + tagClass);
  1015. });
  1016. $('body').on('click', '[tagType]', function () {
  1017. const tagClass = this.getAttribute('tagType');
  1018. if (tagClass === 'all') {
  1019. $('#bills-tag-filter').attr('class', 'fa fa-list-ol');
  1020. } else {
  1021. $('#bills-tag-filter').attr('class', 'fa fa-tag ' + tagClass);
  1022. }
  1023. searchTagsAndShow();
  1024. });
  1025. // 防抖
  1026. function debounce(fun, delay) {
  1027. let timer = null;
  1028. return function () {
  1029. if (timer) {
  1030. clearTimeout(timer);
  1031. }
  1032. timer = setTimeout(fun, delay);
  1033. }
  1034. }
  1035. $('#bills-tag-list').bind('scroll', debounce(function (e) {
  1036. const obj = $('#bills-tag-list');
  1037. var sum = obj[0].scrollHeight;
  1038. if (sum <= obj.scrollTop() + obj.height()) {
  1039. loadViewTags();
  1040. }
  1041. }, 300));
  1042. $('#bills-tag-clear').bind('click', () => {
  1043. if (!$('#bills-tag-keyword').val()) return;
  1044. $('#bills-tag-keyword').val('');
  1045. searchTagsAndShow();
  1046. });
  1047. $('#bills-tag-search').bind('click', () => {searchTagsAndShow();});
  1048. $('#bills-tag-keyword').bind('keydown', e => {if (e.keyCode === 13) searchTagsAndShow();});
  1049. return { loadDatas, updateDatasAndShow, show, getBillsTagsColor, getBillsTagsInfo, refreshBillsTagView, afterDeleteBills }
  1050. };
  1051. $.sumLoadMiss = function (setting) {
  1052. if (!setting.spreadSetting) {
  1053. setting.spreadSetting = {
  1054. cols: [
  1055. { title: '清单编号', field: 'b_code', width: 80, formatter: '@' },
  1056. { title: '清单名称', field: 'name', width: 120, formatter: '@' },
  1057. { title: '单位', field: 'unit', width: 50, formatter: '@' },
  1058. { title: setting.type === 'stage' ? '合同数量' : '数量', field: 'qty', width: 60 },
  1059. { title: '变更数量', field: 'qc_qty', width: 60, visible: setting.type === 'stage' },
  1060. {
  1061. title: '类型', field: 'type', width: 100, getValue: function (x) {
  1062. switch (x.type) {
  1063. case 'less': return '数量变少';
  1064. case 'miss': return '找不到清单';
  1065. case 'qc-conflict': return '变更冲突(已调用变更令)';
  1066. default: return '';
  1067. }
  1068. }
  1069. }
  1070. ],
  1071. emptyRows: 0,
  1072. headRows: 1,
  1073. headRowHeight: [32],
  1074. defaultRowHeight: 21,
  1075. headerFont: '12px 微软雅黑',
  1076. font: '12px 微软雅黑',
  1077. selectedBackColor: '#fffacd',
  1078. readOnly: true,
  1079. };
  1080. }
  1081. const clearMissData = function () {
  1082. if (setting.storeKey) removeLocalCache(setting.storeKey);
  1083. };
  1084. const autoShowHistory = function (show) {
  1085. if (setting.storeKey) {
  1086. setLocalCache(setting.storeKey + '-showHis', show.toString());
  1087. }
  1088. };
  1089. if (setting.selector && setting.relaSpread) {
  1090. const resultId = setting.id + '-spread';
  1091. const obj = $(setting.selector);
  1092. obj.html(
  1093. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  1094. ' </div>'
  1095. );
  1096. autoFlashHeight();
  1097. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  1098. const sheet = spread.getActiveSheet();
  1099. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  1100. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  1101. if (!setting.relaSpread) return;
  1102. const sheet = info.sheet;
  1103. const data = sheet.zh_data;
  1104. if (!data) { return }
  1105. const curBills = data[info.row];
  1106. if (!curBills) { return }
  1107. if (curBills.ledger_id) {
  1108. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  1109. if (setting.afterLocated) {
  1110. setting.afterLocated();
  1111. }
  1112. } else {
  1113. toastr.warning('找不到该清单');
  1114. }
  1115. });
  1116. const loadMissData = function (data, his = false) {
  1117. const sourceTree = setting.relaSpread.getActiveSheet().zh_tree;
  1118. if (!sourceTree) return;
  1119. for (const d of data) {
  1120. d.serialNo = sourceTree.getNodeIndex(sourceTree.getItems(d.ledger_id)) + 1;
  1121. }
  1122. data.sort(function (a, b) {
  1123. return a.serialNo - b.serialNo;
  1124. });
  1125. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  1126. if (!his && setting.storeKey) {
  1127. setLocalCache(setting.storeKey, JSON.stringify(data));
  1128. }
  1129. $(setting.tabSelector).show();
  1130. };
  1131. const showMissList = function () {
  1132. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  1133. $('a', '.side-menu').removeClass('active');
  1134. tab.addClass('active');
  1135. $('.tab-content .tab-pane').removeClass('active');
  1136. tabPanel.addClass('active');
  1137. showSideTools(true);
  1138. spread.refresh();
  1139. if (setting.afterShow) setting.afterShow();
  1140. };
  1141. const loadHisMissData = function () {
  1142. if (setting.storeKey) {
  1143. const storeStr = getLocalCache(setting.storeKey);
  1144. const storeData = storeStr ? JSON.parse(storeStr) : [];
  1145. if (storeData.length > 0) {
  1146. loadMissData(storeData, true);
  1147. const showHis = getLocalCache(setting.storeKey + '-showHis');
  1148. if (showHis === 'true') {
  1149. showMissList();
  1150. removeLocalCache(setting.storeKey + '-showHis');
  1151. }
  1152. }
  1153. }
  1154. };
  1155. return {
  1156. spread: spread,
  1157. loadMissData: loadMissData,
  1158. clearMissData: clearMissData,
  1159. loadHisMissData: loadHisMissData,
  1160. show: showMissList,
  1161. autoShowHistory: autoShowHistory,
  1162. };
  1163. } else {
  1164. const loadMissData = function (data) {
  1165. if (setting.storeKey) {
  1166. setLocalCache(setting.storeKey, JSON.stringify(data));
  1167. }
  1168. };
  1169. return {
  1170. loadErrorData: loadMissData,
  1171. clearErrorData: clearMissData,
  1172. autoShowHistory: autoShowHistory,
  1173. };
  1174. }
  1175. };
  1176. $.stdLib = function (setting) {
  1177. if (!setting.selector) return;
  1178. const obj = $(setting.selector);
  1179. const stdLibHtml = setting.libs.map(l => {
  1180. return `<option value="${l.id}" >${l.name}</option>`;
  1181. });
  1182. const relaSelect = {
  1183. showLevel: `std-${setting.stdType}-sl`,
  1184. searchText: `std-${setting.stdType}-st`,
  1185. searchResult: `std-${setting.stdType}-sr`,
  1186. searchPre: `std-${setting.stdType}-sp`,
  1187. searchNext: `std-${setting.stdType}-sn`,
  1188. searchClose: `std-${setting.stdType}-sc`,
  1189. };
  1190. obj.html(
  1191. '<div class="sjs-bar d-flex">\n' +
  1192. ' <div class="dropdown mr-1">\n' +
  1193. ' <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' +
  1194. ' <i class="fa fa-list-ol"></i> 显示层级\n' +
  1195. ' </button>\n' +
  1196. ' <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">\n' +
  1197. ` <a class="dropdown-item" href="javascript: void(0);" tag="1" name="${relaSelect.showLevel}">第一层</a>\n` +
  1198. ` <a class="dropdown-item" href="javascript: void(0);" tag="2" name="${relaSelect.showLevel}">第二层</a>\n` +
  1199. ` <a class="dropdown-item" href="javascript: void(0);" tag="3" name="${relaSelect.showLevel}">第三层</a>\n` +
  1200. ` <a class="dropdown-item" href="javascript: void(0);" tag="4" name="${relaSelect.showLevel}">第四层</a>\n` +
  1201. ` <a class="dropdown-item" href="javascript: void(0);" tag="5" name="${relaSelect.showLevel}">第五层</a>\n` +
  1202. ` <a class="dropdown-item" href="javascript: void(0);" tag="last" name="${relaSelect.showLevel}">最底层</a>\n` +
  1203. ' </div>\n' +
  1204. ' </div>' +
  1205. ` <div class="input-group input-group-sm pr-1"><select class="form-control form-control-sm col-auto">${stdLibHtml.join('')}</select></div>\n` +
  1206. ' <div class="ml-1">\n' +
  1207. ' <div class="dropdown">\n' +
  1208. ' <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1209. ' <i class="fa fa-search"></i>\n' +
  1210. ' </button>\n' +
  1211. ' <div class="dropdown-menu dropdown-menu-right">\n' +
  1212. ' <div class="px-2 border-primary border-1 d-flex" style="width: 300px">\n' +
  1213. ' <div class="d-inline-block">\n' +
  1214. ' <div class="input-group input-group-sm mr-1">' +
  1215. ` <input type="text" class="form-control" placeholder="输入编号/名称查找" id="${relaSelect.searchText}">\n` +
  1216. ` <div class="input-group-append" ><span class="input-group-text" id="${relaSelect.searchResult}">结果:0</span></div>\n` +
  1217. ' <div class="input-group-append" >\n' +
  1218. ` <button class="btn btn-outline-secondary" type="button" title="上一个" id="${relaSelect.searchPre}"><i class="fa fa-angle-double-left"></i></button>\n` +
  1219. ` <button class="btn btn-outline-secondary" type="button" title="下一个" id="${relaSelect.searchNext}"><i class="fa fa-angle-double-right"></i></button>\n` +
  1220. ' </div>\n' +
  1221. ' </div>\n' +
  1222. ' </div>\n' +
  1223. ` <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` +
  1224. ' </div>\n' +
  1225. ' </div>\n' +
  1226. ' </div>\n' +
  1227. ' </div>' +
  1228. '</div>\n' +
  1229. `<div id="std-${setting.stdType}-spread" class="sjs-sh"></div>\n`
  1230. );
  1231. autoFlashHeight();
  1232. const pathTree = createNewPathTree('base', setting.treeSetting);
  1233. const spreadSetting = {
  1234. cols: [
  1235. {title: '名称', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  1236. {title: '单位', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  1237. ],
  1238. treeCol: 0,
  1239. emptyRows: 0,
  1240. headRows: 1,
  1241. headRowHeight: [32],
  1242. defaultRowHeight: 21,
  1243. headerFont: '12px 微软雅黑',
  1244. font: '12px 微软雅黑',
  1245. headColWidth: [30],
  1246. selectedBackColor: '#fffacd',
  1247. readOnly: true,
  1248. stdType: setting.stdType,
  1249. };
  1250. spreadSetting.cols.unshift(setting.stdType === 'xmj'
  1251. ? {title: '项目节编号', field: 'code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'}
  1252. : {title: '清单编号', field: 'b_code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'});
  1253. const spread = SpreadJsObj.createNewSpread($(`#std-${setting.stdType}-spread`)[0]);
  1254. const sheet = spread.getActiveSheet();
  1255. if (setting.spreadSetting) setting.spreadSetting.stdType = setting.stdType;
  1256. SpreadJsObj.initSheet(sheet, setting.spreadSetting || spreadSetting);
  1257. if (setting.cellDoubleClick) sheet.bind(spreadNS.Events.CellDoubleClick, setting.cellDoubleClick);
  1258. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  1259. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  1260. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1261. }
  1262. });
  1263. sheet.bind(spreadNS.Events.TopRowChanged, function (e, info) {
  1264. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1265. });
  1266. const cacheLib = [];
  1267. const cacheKey = {
  1268. lib: setting.page + '-' + setting.tid + '-' + setting.stdType,
  1269. };
  1270. $('select', setting.selector).change(function () {
  1271. loadLib(parseInt(this.value), true);
  1272. });
  1273. (function (select, sheet) {
  1274. $(select).click(function () {
  1275. if (!sheet.zh_tree) return;
  1276. const tag = $(this).attr('tag');
  1277. const tree = sheet.zh_tree;
  1278. setTimeout(() => {
  1279. switch (tag) {
  1280. case "1":
  1281. case "2":
  1282. case "3":
  1283. case "4":
  1284. case "5":
  1285. tree.expandByLevel(parseInt(tag));
  1286. SpreadJsObj.refreshTreeRowVisible(sheet);
  1287. break;
  1288. case "last":
  1289. tree.expandByCustom(() => { return true; });
  1290. SpreadJsObj.refreshTreeRowVisible(sheet);
  1291. break;
  1292. }
  1293. }, 100);
  1294. });
  1295. })(`a[name=${relaSelect.showLevel}]`, sheet);
  1296. const loadLib = function(listId, reload = false) {
  1297. cacheKey.node = cacheLib.lib + '-' + listId;
  1298. const locateMemory = function () {
  1299. if (!reload) {
  1300. SpreadJsObj.loadTopAndSelect(sheet, cacheKey.node);
  1301. } else {
  1302. removeLocalCache(cacheKey.node);
  1303. }
  1304. };
  1305. const cacheData = cacheLib.find(function (lib) {
  1306. return lib.id === listId;
  1307. });
  1308. if (cacheData) {
  1309. pathTree.loadDatas(cacheData.data);
  1310. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1311. locateMemory();
  1312. setLocalCache(cacheKey.lib, listId);
  1313. } else {
  1314. postData(`/std-lib/get-data`, {stdType: setting.stdType, list_id: listId}, function (data) {
  1315. cacheLib.push({id: listId, data: data});
  1316. pathTree.loadDatas(data);
  1317. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1318. locateMemory();
  1319. setLocalCache(cacheKey.lib, listId);
  1320. });
  1321. }
  1322. };
  1323. const _loadCacheLib = function() {
  1324. let libId = getLocalCache(cacheKey.lib);
  1325. if (libId) {
  1326. $('select', setting.selector).val(libId);
  1327. loadLib(parseInt(libId));
  1328. } else {
  1329. loadLib(parseInt($('select', setting.selector).val()));
  1330. }
  1331. };
  1332. _loadCacheLib();
  1333. const searchObj = {
  1334. result: [],
  1335. cur: 0,
  1336. searchStdNode: function() {
  1337. const keyword = $(`#${relaSelect.searchText}`).val();
  1338. searchObj.result = keyword ? pathTree.nodes.filter(x => {
  1339. return x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0;
  1340. }) : [];
  1341. // searchObj.result = [];
  1342. // for (const x of pathTree.nodes) {
  1343. // if (x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0) {
  1344. // searchObj.result.push(x);
  1345. // }
  1346. // }
  1347. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${searchObj.result.length}`;
  1348. searchObj.cur = 0;
  1349. if (searchObj.result.length > 0) {
  1350. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]));
  1351. }
  1352. },
  1353. searchPre: function () {
  1354. if (searchObj.result.length === 0) return;
  1355. searchObj.cur = searchObj.cur === 0 ? searchObj.result.length - 1 : this.cur - 1;
  1356. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1357. },
  1358. searchNext: function () {
  1359. if (searchObj.result.length === 0) return;
  1360. searchObj.cur = searchObj.cur === searchObj.result.length - 1 ? 0 : searchObj.cur + 1;
  1361. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1362. },
  1363. clear: function () {
  1364. $(`#${relaSelect.searchText}`).val('');
  1365. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${0}`;
  1366. searchObj.result = [];
  1367. searchObj.cur = 0;
  1368. }
  1369. };
  1370. $(`#${relaSelect.searchText}`).change(searchObj.searchStdNode);
  1371. $(`#${relaSelect.searchPre}`).click(function (e) {
  1372. searchObj.searchPre();
  1373. e.stopPropagation();
  1374. });
  1375. $(`#${relaSelect.searchNext}`).click(function (e) {
  1376. searchObj.searchNext();
  1377. e.stopPropagation();
  1378. });
  1379. $(`#${relaSelect.searchClose}`).click(function (e) {
  1380. searchObj.clear();
  1381. e.stopPropagation();
  1382. });
  1383. return { spread }
  1384. };
  1385. $.gljLib = function (setting) {
  1386. if (!setting.selector) return;
  1387. const obj = $(setting.selector);
  1388. const stdLibHtml = setting.libs.map(l => {
  1389. return `<option value="${l.id}" >${l.name}</option>`;
  1390. });
  1391. const relaSelect = {
  1392. showLevel: `std-${setting.stdType}-sl`,
  1393. searchText: `std-${setting.stdType}-st`,
  1394. searchResult: `std-${setting.stdType}-sr`,
  1395. searchPre: `std-${setting.stdType}-sp`,
  1396. searchNext: `std-${setting.stdType}-sn`,
  1397. searchClose: `std-${setting.stdType}-sc`,
  1398. };
  1399. obj.html(
  1400. '<div class="sjs-bar d-flex">\n' +
  1401. ` <div class="input-group input-group-sm pr-1"><select class="form-control form-control-sm col-auto">${stdLibHtml.join('')}</select></div>\n` +
  1402. ' <div class="ml-1">\n' +
  1403. ' <div class="dropdown">\n' +
  1404. ' <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
  1405. ' <i class="fa fa-search"></i>\n' +
  1406. ' </button>\n' +
  1407. ' <div class="dropdown-menu dropdown-menu-right">\n' +
  1408. ' <div class="px-2 border-primary border-1 d-flex" style="width: 300px">\n' +
  1409. ' <div class="d-inline-block">\n' +
  1410. ' <div class="input-group input-group-sm mr-1">' +
  1411. ` <input type="text" class="form-control" placeholder="输入编号/名称查找" id="${relaSelect.searchText}">\n` +
  1412. ` <div class="input-group-append" ><span class="input-group-text" id="${relaSelect.searchResult}">结果:0</span></div>\n` +
  1413. ' <div class="input-group-append" >\n' +
  1414. ` <button class="btn btn-outline-secondary" type="button" title="上一个" id="${relaSelect.searchPre}"><i class="fa fa-angle-double-left"></i></button>\n` +
  1415. ` <button class="btn btn-outline-secondary" type="button" title="下一个" id="${relaSelect.searchNext}"><i class="fa fa-angle-double-right"></i></button>\n` +
  1416. ' </div>\n' +
  1417. ' </div>\n' +
  1418. ' </div>\n' +
  1419. ` <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` +
  1420. ' </div>\n' +
  1421. ' </div>\n' +
  1422. ' </div>\n' +
  1423. ' </div>' +
  1424. '</div>\n' +
  1425. `<div id="std-${setting.stdType}-spread" class="${setting.spreadClassCss ? setting.spreadClassCss : 'sjs-sh'}"></div>\n`
  1426. );
  1427. autoFlashHeight();
  1428. const pathTree = createNewPathTree('base', setting.treeSetting);
  1429. const spreadSetting = {
  1430. cols: [
  1431. {title: '名称', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  1432. {title: '单位', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  1433. ],
  1434. treeCol: 0,
  1435. emptyRows: 0,
  1436. headRows: 1,
  1437. headRowHeight: [32],
  1438. defaultRowHeight: 21,
  1439. headerFont: '12px 微软雅黑',
  1440. font: '12px 微软雅黑',
  1441. headColWidth: [30],
  1442. selectedBackColor: '#fffacd',
  1443. readOnly: true,
  1444. stdType: setting.stdType,
  1445. };
  1446. spreadSetting.cols.unshift(setting.stdType === 'xmj'
  1447. ? {title: '项目节编号', field: 'code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'}
  1448. : {title: '清单编号', field: 'b_code', hAlign: 0, width: 150, formatter: '@', cellType: 'tree'});
  1449. const spread = SpreadJsObj.createNewSpread($(`#std-${setting.stdType}-spread`)[0]);
  1450. const sheet = spread.getActiveSheet();
  1451. SpreadJsObj.initSheet(sheet, setting.spreadSetting ? setting.spreadSetting : spreadSetting);
  1452. if (setting.cellDoubleClick) sheet.bind(spreadNS.Events.CellDoubleClick, setting.cellDoubleClick);
  1453. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  1454. if (!info.oldSelections || !info.oldSelections[0] || info.newSelections[0].row !== info.oldSelections[0].row) {
  1455. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1456. }
  1457. });
  1458. sheet.bind(spreadNS.Events.TopRowChanged, function (e, info) {
  1459. SpreadJsObj.saveTopAndSelect(info.sheet, cacheKey.node);
  1460. });
  1461. const cacheLib = [];
  1462. const cacheKey = {
  1463. lib: setting.page + '-' + setting.tid + '-' + setting.stdType,
  1464. };
  1465. $('select', setting.selector).change(function () {
  1466. loadLib(parseInt(this.value), true);
  1467. });
  1468. const loadLib = function(listId, reload = false) {
  1469. cacheKey.node = cacheLib.lib + '-' + listId;
  1470. const locateMemory = function () {
  1471. if (!reload) {
  1472. SpreadJsObj.loadTopAndSelect(sheet, cacheKey.node);
  1473. } else {
  1474. removeLocalCache(cacheKey.node);
  1475. }
  1476. };
  1477. const cacheData = cacheLib.find(function (lib) {
  1478. return lib.id === listId;
  1479. });
  1480. if (cacheData) {
  1481. pathTree.loadDatas(cacheData.data);
  1482. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1483. locateMemory();
  1484. setLocalCache(cacheKey.lib, listId);
  1485. } else {
  1486. postData(`/std-lib/get-data`, {stdType: setting.stdType, list_id: listId}, function (data) {
  1487. cacheLib.push({id: listId, data: data});
  1488. pathTree.loadDatas(data);
  1489. SpreadJsObj.loadSheetData(sheet, 'tree', pathTree);
  1490. locateMemory();
  1491. setLocalCache(cacheKey.lib, listId);
  1492. });
  1493. }
  1494. };
  1495. const _loadCacheLib = function() {
  1496. let libId = getLocalCache(cacheKey.lib);
  1497. if (libId) {
  1498. $('select', setting.selector).val(libId);
  1499. loadLib(parseInt(libId));
  1500. } else {
  1501. loadLib(parseInt($('select', setting.selector).val()));
  1502. }
  1503. };
  1504. _loadCacheLib();
  1505. const searchObj = {
  1506. result: [],
  1507. cur: 0,
  1508. searchStdNode: function() {
  1509. const keyword = $(`#${relaSelect.searchText}`).val();
  1510. searchObj.result = keyword ? pathTree.nodes.filter(x => {
  1511. return x.code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0;
  1512. }) : [];
  1513. // searchObj.result = [];
  1514. // for (const x of pathTree.nodes) {
  1515. // if (x.code.indexOf(keyword) >= 0 || x.b_code.indexOf(keyword) >= 0 || x.name.indexOf(keyword) >= 0) {
  1516. // searchObj.result.push(x);
  1517. // }
  1518. // }
  1519. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${searchObj.result.length}`;
  1520. searchObj.cur = 0;
  1521. if (searchObj.result.length > 0) {
  1522. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]));
  1523. }
  1524. },
  1525. searchPre: function () {
  1526. if (searchObj.result.length === 0) return;
  1527. searchObj.cur = searchObj.cur === 0 ? searchObj.result.length - 1 : this.cur - 1;
  1528. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1529. },
  1530. searchNext: function () {
  1531. if (searchObj.result.length === 0) return;
  1532. searchObj.cur = searchObj.cur === searchObj.result.length - 1 ? 0 : searchObj.cur + 1;
  1533. SpreadJsObj.locateTreeNode(sheet, pathTree.getNodeKey(searchObj.result[searchObj.cur]), true);
  1534. },
  1535. clear: function () {
  1536. $(`#${relaSelect.searchText}`).val('');
  1537. $(`#${relaSelect.searchResult}`)[0].innerText = `结果:${0}`;
  1538. searchObj.result = [];
  1539. searchObj.cur = 0;
  1540. }
  1541. };
  1542. $(`#${relaSelect.searchText}`).change(searchObj.searchStdNode);
  1543. $(`#${relaSelect.searchPre}`).click(function (e) {
  1544. searchObj.searchPre();
  1545. e.stopPropagation();
  1546. });
  1547. $(`#${relaSelect.searchNext}`).click(function (e) {
  1548. searchObj.searchNext();
  1549. e.stopPropagation();
  1550. });
  1551. $(`#${relaSelect.searchClose}`).click(function (e) {
  1552. searchObj.clear();
  1553. e.stopPropagation();
  1554. });
  1555. return { spread }
  1556. };
  1557. })(jQuery);