cs_tools.js 81 KB

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