std_billsGuidance_lib.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/11
  7. * @version
  8. */
  9. //清单指引/精灵获取完清单数据后的回调函数
  10. let doAfterLoadGuidance = null;
  11. //选项单选多选状态(按住alt为多选) 单选:0 多选:1
  12. let billsGuidanceSelMode = 0;
  13. const billsGuidance = (function () {
  14. let currentLib = null;
  15. //库类型
  16. const libType = {'guidance': 1, 'elf': 2}; //清单指引、清单精灵
  17. const libTypeText = {1: '清单指引', 2: '清单精灵'};
  18. const libSel = $('#stdBillsGuidanceLibSelect');
  19. //工作内容
  20. let stdBillsJobData = [];
  21. //项目特征
  22. let stdBillsFeatureData = [];
  23. const bills = {
  24. dom: $('#billsGuidance_bills'),
  25. workBook: null,
  26. cache: [],
  27. tree: null,
  28. controller: null,
  29. treeSetting: {
  30. emptyRowHeader: true,
  31. rowHeaderWidth: 15,
  32. treeCol: 0,
  33. emptyRows: 0,
  34. headRows: 1,
  35. headRowHeight: [40],
  36. defaultRowHeight: 21,
  37. cols: [{
  38. width: 140,
  39. readOnly: true,
  40. showHint: true,
  41. head: {
  42. titleNames: ["项目编码"],
  43. spanCols: [1],
  44. spanRows: [1],
  45. vAlign: [1],
  46. hAlign: [1],
  47. font: ["Arial"]
  48. },
  49. data: {
  50. field: "code",
  51. vAlign: 1,
  52. hAlign: 0,
  53. font: "Arial"
  54. }
  55. }, {
  56. width: 190,
  57. readOnly: true,
  58. head: {
  59. titleNames: ["项目名称"],
  60. spanCols: [1],
  61. spanRows: [1],
  62. vAlign: [1],
  63. hAlign: [1],
  64. font: ["Arial"]
  65. },
  66. data: {
  67. field: "name",
  68. vAlign: 1,
  69. hAlign: 0,
  70. font: "Arial"
  71. }
  72. },
  73. {
  74. width: 45,
  75. readOnly: true,
  76. head: {
  77. titleNames: ["计量单位"],
  78. spanCols: [1],
  79. spanRows: [1],
  80. vAlign: [1],
  81. hAlign: [1],
  82. font: ["Arial"]
  83. },
  84. data: {
  85. field: "unit",
  86. vAlign: 1,
  87. hAlign: 1,
  88. font: "Arial"
  89. }
  90. }
  91. ]
  92. },
  93. headers: [
  94. {name: '项目编码', dataCode: 'code', width: 140, vAlign: 'center', hAlign: 'left', formatter: '@'},
  95. {name: '项目名称', dataCode: 'name', width: 190, vAlign: 'center', hAlign: 'left', formatter: '@'},
  96. {name: '单位', dataCode: 'unit', width: 45, vAlign: 'center', hAlign: 'center', formatter: '@'},
  97. ],
  98. events: {
  99. SelectionChanging: function (sender, info) {
  100. billsInitSel(info.newSelections[0].row);
  101. },
  102. CellDoubleClick: function (sender, args) {
  103. if(!bills.tree){
  104. return;
  105. }
  106. let node = bills.tree.items[args.row];
  107. if(!node){
  108. return;
  109. }
  110. if(node.children.length === 0){
  111. billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, node);
  112. }
  113. else {
  114. node.setExpanded(!node.expanded);
  115. //设置展开收起状态
  116. sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
  117. renderSheetFunc(args.sheet, function () {
  118. let iCount = node.posterityCount(), i, child;
  119. for (i = 0; i < iCount; i++) {
  120. child = bills.tree.items[args.row + i + 1];
  121. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  122. }
  123. args.sheet.invalidateLayout();
  124. });
  125. args.sheet.repaint();
  126. }
  127. }
  128. }
  129. };
  130. //项目指引类型
  131. const itemType = {
  132. job: 0,
  133. ration: 1
  134. };
  135. const guideItem = {
  136. dom: $('#billsGuidance_items'),
  137. workBook: null,
  138. tree: null,
  139. controller: null,
  140. treeSetting: {
  141. treeCol: 1,
  142. emptyRows: 0,
  143. headRows: 1,
  144. headRowHeight: [40],
  145. defaultRowHeight: 21,
  146. cols: [
  147. {
  148. width: 35,
  149. readOnly: false,
  150. head: {
  151. titleNames: ["选择"],
  152. spanCols: [1],
  153. spanRows: [1],
  154. vAlign: [1],
  155. hAlign: [1],
  156. font: ["Arial"]
  157. },
  158. data: {
  159. field: "select",
  160. vAlign: 1,
  161. hAlign: 1,
  162. font: "Arial"
  163. }
  164. },
  165. {
  166. width: 420,
  167. readOnly: false,
  168. head: {
  169. titleNames: ["项目指引"],
  170. spanCols: [1],
  171. spanRows: [1],
  172. vAlign: [1],
  173. hAlign: [1],
  174. font: ["Arial"]
  175. },
  176. data: {
  177. field: "name",
  178. vAlign: 1,
  179. hAlign: 0,
  180. font: "Arial"
  181. }
  182. }
  183. ]
  184. },
  185. headers: [
  186. {name: '选择', dataCode: 'select', width: 35, vAlign: 'center', hAlign: 'center', formatter: '@'},
  187. {name: '项目指引', dataCode: 'name', width: 300, vAlign: 'center', hAlign: 'left', formatter: '@'},
  188. ],
  189. events: {
  190. EditStarting: function (sender, args) {
  191. if(!bills.tree || guideItem.headers[args.col]['dataCode'] === 'name'){
  192. args.cancel = true;
  193. }
  194. },
  195. ButtonClicked: function (sender, args) {
  196. if(args.sheet.isEditing()){
  197. args.sheet.endEdit(true);
  198. }
  199. refreshInsertRation();
  200. },
  201. CellDoubleClick: function (sender, args) {
  202. if(!bills.tree || !bills.tree.selected){
  203. return;
  204. }
  205. let node = bills.tree.selected.guidance.tree.selected;
  206. if(!node){
  207. return;
  208. }
  209. if(node.children.length === 0){
  210. if(guideItem.headers[args.col]['dataCode'] === 'name'){
  211. insertRations(getInsertRationData([args.row]));
  212. }
  213. }
  214. else {
  215. node.setExpanded(!node.expanded);
  216. renderSheetFunc(args.sheet, function () {
  217. let iCount = node.posterityCount(), i, child;
  218. for (i = 0; i < iCount; i++) {
  219. child = bills.tree.selected.guidance.tree.items[args.row + i + 1];
  220. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  221. }
  222. args.sheet.invalidateLayout();
  223. });
  224. args.sheet.repaint();
  225. }
  226. }
  227. }
  228. };
  229. const elfItem = {
  230. dom: $('#billsGuidance_items'),
  231. workBook: null,
  232. tree: null,
  233. controller: null,
  234. treeSetting: {
  235. treeCol: 0,
  236. emptyRows: 0,
  237. headRows: 1,
  238. headRowHeight: [40],
  239. defaultRowHeight: 21,
  240. cols: [
  241. {
  242. width: 250,
  243. readOnly: true,
  244. head: {
  245. titleNames: ["施工工序"],
  246. spanCols: [1],
  247. spanRows: [1],
  248. vAlign: [1],
  249. hAlign: [1],
  250. font: ["Arial"]
  251. },
  252. data: {
  253. field: "name",
  254. vAlign: 1,
  255. hAlign: 0,
  256. font: "Arial"
  257. }
  258. },
  259. {
  260. width: 250,
  261. readOnly: false,
  262. head: {
  263. titleNames: ["选项"],
  264. spanCols: [1],
  265. spanRows: [1],
  266. vAlign: [1],
  267. hAlign: [1],
  268. font: ["Arial"]
  269. },
  270. data: {
  271. field: "options",
  272. vAlign: 1,
  273. hAlign: 0,
  274. font: "Arial"
  275. }
  276. }
  277. ]
  278. },
  279. headers: [
  280. {name: '施工工序', dataCode: 'name', width: 250, rateWidth: 0.5, vAlign: 'center', hAlign: 'center', formatter: '@'},
  281. {name: '选项', dataCode: 'options', width: 250, rateWidth: 0.5, vAlign: 'center', hAlign: 'left', formatter: '@'},
  282. ],
  283. events: {
  284. CellClick: function (sender, args) {
  285. if(elfItem.headers[args.col]['dataCode'] === 'options' && args.sheetArea === 3){
  286. if(!args.sheet.getCell(args.row, args.col).locked() && !args.sheet.isEditing()){
  287. args.sheet.startEdit();
  288. }
  289. }
  290. },
  291. ClipboardPasting: function (sender, info) {
  292. info.cancel = true;
  293. }
  294. }
  295. };
  296. const options = {
  297. workBook: {
  298. tabStripVisible: false,
  299. allowContextMenu: false,
  300. allowCopyPasteExcelStyle : false,
  301. allowExtendPasteRange: false,
  302. allowUserDragDrop : false,
  303. allowUserDragFill: false,
  304. scrollbarMaxAlign : true
  305. },
  306. sheet: {
  307. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  308. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  309. }
  310. };
  311. //渲染时方法,停止渲染
  312. //@param {Object}sheet {Function}func @return {void}
  313. function renderSheetFunc(sheet, func){
  314. sheet.suspendEvent();
  315. sheet.suspendPaint();
  316. if(func){
  317. func();
  318. }
  319. sheet.resumeEvent();
  320. sheet.resumePaint();
  321. }
  322. //设置表选项
  323. //@param {Object}workBook {Object}opts @return {void}
  324. function setOptions (workBook, opts) {
  325. for(let opt in opts.workBook){
  326. workBook.options[opt] = opts.workBook[opt];
  327. }
  328. for(let opt in opts.sheet){
  329. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  330. }
  331. }
  332. //建表头
  333. //@param {Object}sheet {Array}headers @return {void}
  334. function buildHeader(sheet, headers) {
  335. let fuc = function () {
  336. sheet.setColumnCount(headers.length);
  337. sheet.setRowHeight(0, 30, GC.Spread.Sheets.SheetArea.colHeader);
  338. sheet.setColumnWidth(0, sheet.getParent() === bills.workBook ? 15 : 25, GC.Spread.Sheets.SheetArea.rowHeader);
  339. if(sheet.getParent() === elfItem.workBook || sheet.getParent() === guideItem.workBook){
  340. sheet.setRowHeight(0, 25, GC.Spread.Sheets.SheetArea.colHeader);
  341. }
  342. for(let i = 0, len = headers.length; i < len; i++){
  343. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  344. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  345. if(headers[i].formatter){
  346. sheet.setFormatter(-1, i, headers[i].formatter);
  347. }
  348. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  349. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  350. }
  351. };
  352. renderSheetFunc(sheet, fuc);
  353. }
  354. //表监听事件
  355. //@param {Object}workBook @return {void}
  356. function bindEvent(workBook, events) {
  357. if(Object.keys(events).length === 0){
  358. return;
  359. }
  360. const Events = GC.Spread.Sheets.Events;
  361. for(let event in events){
  362. workBook.bind(Events[event], events[event]);
  363. }
  364. }
  365. //根据宽度比例设置列宽
  366. //@param {Object}workBook {Number}workBookWidth {Array}headers @return {void}
  367. function setColumnWidthByRate(workBook, workBookWidth, headers) {
  368. if(workBook){
  369. const sheet = workBook.getActiveSheet();
  370. sheet.suspendEvent();
  371. sheet.suspendPaint();
  372. for(let col = 0; col < headers.length; col++){
  373. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  374. let width = workBookWidth * headers[col]['rateWidth'];
  375. if(headers[col]['dataCode'] === 'options'){
  376. width = width - 70;
  377. }
  378. sheet.setColumnWidth(col, width, GC.Spread.Sheets.SheetArea.colHeader)
  379. }
  380. else {
  381. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  382. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  383. }
  384. }
  385. }
  386. sheet.resumeEvent();
  387. sheet.resumePaint();
  388. }
  389. }
  390. //建表
  391. //@param {Object}module @return {void}
  392. function buildSheet(module) {
  393. if(!module.workBook){
  394. module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
  395. sheetCommonObj.spreadDefaultStyle(module.workBook);
  396. let sheet = module.workBook.getActiveSheet();
  397. if(module === bills){
  398. //默认初始可控制焦点在清单表中
  399. module.workBook.focus();
  400. sheet.options.isProtected = true;
  401. sheet.name('stdBillsGuidance_bills');
  402. //设置悬浮提示
  403. TREE_SHEET_HELPER.initSetting(bills.dom[0], bills.treeSetting);
  404. }
  405. if(module === guideItem){
  406. sheet.options.isProtected = true;
  407. sheet.getRange(-1, 0, -1, 1).locked(false);
  408. sheet.getRange(-1, 1, -1, 1).locked(true);
  409. }
  410. if(module === elfItem){
  411. sheet.options.isProtected = true;
  412. sheet.getRange(-1, 0, -1, 1).locked(true);
  413. sheet.getRange(-1, 1, -1, 1).locked(false);
  414. }
  415. setOptions(module.workBook, options);
  416. buildHeader(module.workBook.getActiveSheet(), module.headers);
  417. if(module === elfItem){
  418. setColumnWidthByRate(elfItem.workBook, $('#zy').width(), elfItem.headers)
  419. }
  420. bindEvent(module.workBook, module.events);
  421. }
  422. }
  423. //清空表数据
  424. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  425. function cleanData(sheet, headers, rowCount){
  426. renderSheetFunc(sheet, function () {
  427. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  428. if (rowCount > 0) {
  429. sheet.setRowCount(rowCount);
  430. }
  431. });
  432. }
  433. //初始化各工作表
  434. //@param {Array}modules @return {void}
  435. function initWorkBooks(modules){
  436. for(let module of modules){
  437. buildSheet(module);
  438. }
  439. }
  440. //初始化并输出树
  441. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  442. function initTree(module, sheet, treeSetting, datas){
  443. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  444. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting, false);
  445. module.tree.loadDatas(datas);
  446. if(module === bills){
  447. initExpandStat();
  448. }
  449. module.controller.showTreeData();
  450. if(module === bills){
  451. setBillsHint(bills.tree.items, stdBillsJobData, stdBillsFeatureData);
  452. }
  453. }
  454. //项目指引表焦点控制
  455. //@param {Number}row @return {void}
  456. function guideItemInitSel(row){
  457. let billsNode = bills.tree.selected;
  458. let node = null;
  459. if(billsNode && billsNode.guidance.tree){
  460. node = billsNode.guidance.tree.items[row];
  461. if(node){
  462. billsNode.guidance.tree.selected = node;
  463. }
  464. }
  465. }
  466. //清单精灵表焦点控制
  467. //@param {Number}row @return {void}
  468. function elfItemInitSel(row){
  469. let billsNode = bills.tree.selected;
  470. let node = null;
  471. if(billsNode && billsNode.elf.tree){
  472. node = billsNode.elf.tree.items[row];
  473. if(node){
  474. billsNode.elf.tree.selected = node;
  475. }
  476. }
  477. }
  478. //根据项目指引的类型设置单元格类型,定额类型的项目指引为复选框
  479. //@param {Array}nodes @return {void}
  480. function setItemCellType(nodes){
  481. //设置单元格类型
  482. const base = new GC.Spread.Sheets.CellTypes.Base();
  483. const checkBox = new GC.Spread.Sheets.CellTypes.CheckBox();
  484. const sheet = guideItem.workBook.getActiveSheet();
  485. renderSheetFunc(sheet, function(){
  486. for(let node of nodes){
  487. sheet.setCellType(node.serialNo(), 0, node.data.type === itemType.ration ? checkBox : base);
  488. }
  489. });
  490. }
  491. //清单表焦点控制
  492. //@param {Number}row @return {void}
  493. function billsInitSel(row){
  494. if(currentLib.type && currentLib.type === libType.elf){
  495. billsSelElf(row);
  496. }else {
  497. billsSelGuidance(row);
  498. }
  499. }
  500. //清单焦点变换-清单指引操作
  501. //@param {Number}row @return {void}
  502. function billsSelGuidance(row){
  503. let guideSheet = guideItem.workBook.getActiveSheet();
  504. cleanData(guideSheet, guideItem.headers, -1);
  505. if(!bills.tree){
  506. return;
  507. }
  508. let node = bills.tree.items[row];
  509. if(!node){
  510. return;
  511. }
  512. bills.tree.selected = node;
  513. refreshInsertRation();
  514. if(!node.guidance.tree){
  515. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: libSel.val(), billsID: node.getID()}, function (rstData) {
  516. initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
  517. setItemCellType(node.guidance.tree.items);
  518. //项目指引初始焦点
  519. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  520. });
  521. }
  522. else{
  523. node.guidance.controller.showTreeData();
  524. setItemCellType(node.guidance.tree.items);
  525. //项目指引初始焦点
  526. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  527. }
  528. }
  529. //清单焦点变换-清单精灵操作
  530. //@param {Number}row @return {void}
  531. function billsSelElf(row) {
  532. let elfSheet = elfItem.workBook.getActiveSheet();
  533. cleanData(elfSheet, elfItem.headers, -1);
  534. if(!bills.tree){
  535. return;
  536. }
  537. let node = bills.tree.items[row];
  538. if(!node){
  539. return;
  540. }
  541. bills.tree.selected = node;
  542. if(!node.elf.tree){
  543. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: libSel.val(), billsID: node.getID()}, function (rstData) {
  544. //定额数据删除编号信息
  545. for(let rData of rstData){
  546. if(rData.type === itemType.ration){
  547. let nameArr = rData.name.split(' ');
  548. if(nameArr.length > 0){
  549. nameArr.splice(0, 1);
  550. rData.name = nameArr.join(' ');
  551. }
  552. }
  553. }
  554. node.elf.datas = rstData;
  555. //第一层节点数据
  556. let firstLevelDatas = _.filter(rstData, function (data) {
  557. return data.ParentID == -1;
  558. });
  559. //第一层初始数据的选项显示
  560. for(let fData of firstLevelDatas){
  561. let options = getOptions(fData, rstData);
  562. fData.options = options.length > 0 ? options[0].name : '';
  563. //下挂的选项
  564. fData.optionsData = options && options.length > 0 ? _.cloneDeep(options) : [];
  565. fData.optionChecked = options && options.length > 0 ? [_.cloneDeep(options[0])] : [];
  566. }
  567. renderSheetFunc(elfSheet, function () {
  568. initTree(node.elf, elfSheet, elfItem.treeSetting, firstLevelDatas);
  569. //初始选择选项
  570. let initOptsOpr = [];
  571. for(let elfNode of node.elf.tree.items){
  572. if(elfNode.data.optionsData.length > 0){
  573. initOptsOpr.push({node: elfNode, data: elfNode.data.optionsData[0]});
  574. }
  575. }
  576. for(let opr of initOptsOpr){
  577. insertNodeByData(opr.node, opr.data);
  578. }
  579. TREE_SHEET_HELPER.refreshTreeNodeData(elfItem.treeSetting, elfSheet, node.elf.tree.items, false);
  580. setOptionsCellType(node.elf.tree.items);
  581. //项目指引初始焦点
  582. elfItemInitSel(elfSheet.getActiveRowIndex() ? elfSheet.getActiveRowIndex() : 0);
  583. refreshInsertRation();
  584. });
  585. });
  586. }
  587. else{
  588. renderSheetFunc(elfSheet, function () {
  589. node.elf.controller.showTreeData();
  590. setOptionsCellType(node.elf.tree.items);
  591. //项目指引初始焦点
  592. elfItemInitSel(elfSheet.getActiveRowIndex() ? elfSheet.getActiveRowIndex() : 0);
  593. refreshInsertRation();
  594. });
  595. }
  596. }
  597. //获取选项的深度
  598. //@param {Object}opt {Array}options(当前清单所有选项) @return {Array}
  599. function getOptionDepth(opt, options) {
  600. let parent = _.find(options, {ID: opt.ParentID});
  601. let depth = 0;
  602. while (parent){
  603. depth++;
  604. parent = _.find(options, {ID: parent.ParentID});
  605. }
  606. return depth;
  607. }
  608. //获取施工工序含有的选项(即当前施工工序的子项),获取的顺序按照NextSiblingID排序
  609. //@param {Object}process {Array}datas @return {Array}
  610. function getOptions(process, datas) {
  611. let rst = [];
  612. if(!process || !process.ID){
  613. return [];
  614. }
  615. let options = _.filter(datas, function (data) {
  616. return data.ParentID == process.ID;
  617. });
  618. if(options.length === 0){
  619. return [];
  620. }
  621. //根据NextSiblingID排序
  622. let IDMapping = {};
  623. for(let opt of options){
  624. IDMapping[opt.ID] = {self: opt, next: null, pre: null};
  625. }
  626. for(let opt of options){
  627. let next = IDMapping[opt.NextSiblingID] ? IDMapping[opt.NextSiblingID] : null;
  628. if(next){
  629. next.pre = IDMapping[opt.ID];
  630. IDMapping[opt.ID]['next'] = next;
  631. }
  632. }
  633. let first = null,
  634. rank = 0;
  635. for(let ID in IDMapping){
  636. let obj = IDMapping[ID];
  637. if(!obj.pre){
  638. first = obj;
  639. }
  640. }
  641. while(first){
  642. rank++;
  643. first.self.rank = rank;
  644. rst.push(first.self);
  645. first = first.next;
  646. }
  647. return rst;
  648. }
  649. //设置清单精灵选项单元格
  650. //@param {Array}nodes @return {void}
  651. function setOptionsCellType(nodes) {
  652. let elfSheet = elfItem.workBook.getActiveSheet();
  653. for(let node of nodes){
  654. if(node.data.optionsData && node.data.optionsData.length > 0){
  655. elfSheet.getCell(node.serialNo(), 1).locked(false).cellType(getOptionsCellType());
  656. }
  657. else {
  658. elfSheet.getCell(node.serialNo(), 1).locked(true).cellType(new GC.Spread.Sheets.CellTypes.Base());
  659. }
  660. }
  661. }
  662. //递归插入节点:原始项目指引数据奇数层为需要插入的节点,偶数层为下拉选项
  663. //@param {Object}node(当前操作的节点) {Object}data(选项) @return {void}
  664. function insertNodeByData(node, data) {
  665. let elfSheet = elfItem.workBook.getActiveSheet();
  666. let sameDepthNodes = node.children;
  667. let insertNextSiblingID = -1,
  668. insertParentID = node.data.ID;
  669. //当前操作节点的选项
  670. let nodeOpts = getOptions(node.data, bills.tree.selected.elf.datas);
  671. let subOpts = getOptions(data, bills.tree.selected.elf.datas);
  672. let dataDepth = getOptionDepth(data, bills.tree.selected.elf.datas);
  673. if(subOpts.length >0 && subOpts[0].type !== itemType.ration){
  674. if((dataDepth + 1) % 2 === 0){
  675. //排序后的数据
  676. let dataWithRank = _.find(nodeOpts, {ID: data.ID});
  677. //确定插入位置
  678. for(let subOpt of subOpts){
  679. for(let subNode of sameDepthNodes){
  680. //同层节点原本选项数据
  681. let subNodeOptData = _.find(bills.tree.selected.elf.datas, {ID: subNode.data.ID});
  682. //同层节点原本父选项数据
  683. let subNodeOptParent = _.find(bills.tree.selected.elf.datas, {ID: subNodeOptData.ParentID});
  684. let subNodeOptParentWithRank = _.find(nodeOpts, {ID: subNodeOptParent.ID});
  685. //父项顺序决定插入位置
  686. if(dataWithRank.rank < subNodeOptParentWithRank.rank){
  687. insertNextSiblingID = subNode.data.ID;
  688. break;
  689. }
  690. //父项顺序相同,根据子项顺序决定插入位置
  691. else if(dataWithRank.rank = subNodeOptParentWithRank.rank){
  692. if(subOpt.rank < subNode.data.rank){
  693. insertNextSiblingID = subNode.data.ID;
  694. break;
  695. }
  696. }
  697. }
  698. let sub2Opts = getOptions(subOpt, bills.tree.selected.elf.datas);
  699. subOpt.options = sub2Opts.length > 0 ? sub2Opts[0].name : '';
  700. let cloneOpt = _.cloneDeep(subOpt);//不改变原本的数据,比如ParentID
  701. cloneOpt.optionChecked = sub2Opts.length > 0 ? [_.cloneDeep(sub2Opts[0])] : [];
  702. cloneOpt.optionsData = sub2Opts.length > 0 ? _.cloneDeep(sub2Opts) : [];
  703. let newNode = node.tree.insertByData(cloneOpt, insertParentID, insertNextSiblingID);
  704. elfSheet.addRows(newNode.serialNo(), 1);
  705. node.tree.selected = newNode;
  706. elfSheet.setSelection(newNode.serialNo(), elfSheet.getSelections()[0].col, 1, 1);
  707. if(sub2Opts.length > 0 && sub2Opts[0].type !== itemType.ration){
  708. insertNodeByData(newNode, sub2Opts[0]);
  709. }
  710. }
  711. }
  712. else {
  713. insertNodeByData(node, subOpts[0]);
  714. }
  715. }
  716. }
  717. //获取选项下拉多选单元格
  718. //@param {void} @return {void}
  719. function getOptionsCellType() {
  720. let me = this;
  721. let elfSheet= elfItem.workBook.getActiveSheet();
  722. function OptionsCellType() {
  723. this.isEscKey=false;
  724. this.displayText='';
  725. }
  726. /* function getHtml(node, cellRect, cellStyle, top) {
  727. if(!node){
  728. return '';
  729. }
  730. let height = cellRect.height;
  731. top = top.replace('px', '');
  732. let htmlArr = [];
  733. let options = getOptions(node.data, bills.tree.selected.elf.datas);
  734. //let optionsTitle = node.data.options.split(';').join('\n');
  735. htmlArr.push(`<div style="height: ${height}px; background: ${cellStyle.backColor};overflow: hidden; white-space: nowrap; text-overflow: ellipsis">${node.data.options}</div><div id="optDiv" style="position: fixed; width: ${cellRect.width}px; top: ${top - (options.length - 2) * height - 5}px;background: ${cellStyle.backColor};border: 1px solid; overflow: auto; height: ${options.length > 6 ? height*6 : height*options.length+5}px; font-size: 0.9rem;">`);
  736. for(let opt of options){
  737. htmlArr.push(`<div title="${opt.name ? opt.name : ''}" class="elf-options" style="height: ${height}px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
  738. <input rank="${opt.rank}" value="${opt.ID}" style="margin-left: 5px; vertical-align: middle" type="checkbox"
  739. ${node.data.optionChecked && _.find(node.data.optionChecked, {ID: opt.ID}) ? 'checked' : ''}> ${opt.name ? opt.name : ''}</div>`);
  740. }
  741. htmlArr.push(`</div>`);
  742. return htmlArr.join('');
  743. }*/
  744. function setOptionsDiv($editor, node, cellRect, cellStyle, top) {
  745. if(!node){
  746. return '';
  747. }
  748. let height = cellRect.height;
  749. top = top.replace('px', '');
  750. let options = getOptions(node.data, bills.tree.selected.elf.datas);
  751. let $editInput = $(`<div style="height: ${height}px; background: ${cellStyle.backColor};overflow: hidden; white-space: nowrap; text-overflow: ellipsis">${node.data.options}</div>`),
  752. $optDiv = $(`<div style="position: fixed; width: ${cellRect.width}px; top: ${top - (options.length - 2) * height - 5}px;background: ${cellStyle.backColor};border: 1px solid; overflow: auto; height: ${options.length > 6 ? height*6 : height*options.length+5}px; font-size: 0.9rem;"></div>`);
  753. for(let opt of options){
  754. let $opt = $(`<div title="${opt.name ? opt.name : ''}" class="elf-options" style="height: ${height}px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis"></div>`),
  755. $optInput = $(`<input rank="${opt.rank}" value="${opt.ID}" style="margin-left: 5px; vertical-align: middle" type="checkbox"
  756. ${node.data.optionChecked && _.find(node.data.optionChecked, {ID: opt.ID}) ? 'checked' : ''}>`);
  757. $opt.text(`${opt.name ? opt.name : ''}`);
  758. $opt.prepend($optInput);
  759. $optDiv.append($opt);
  760. //选项复选框点击监听
  761. $optInput.click(function () {
  762. //单选
  763. if(billsGuidanceSelMode === 0){
  764. let $allInput = $optDiv.find('input');
  765. for(let input of $allInput){
  766. $(input).prop('checked', false);
  767. }
  768. $(this).prop('checked', 'checked');
  769. } else {//多选
  770. }
  771. });
  772. }
  773. $editor.append($editInput);
  774. $editor.append($optDiv);
  775. }
  776. //选择后处理
  777. function doAfterSel(node) {
  778. let checkedSels = $('.elf-options').find('input:checked');
  779. let checkedNameArr = [],
  780. optionChecked= [];
  781. for(let checkSel of checkedSels){
  782. let opt = _.cloneDeep(_.find(bills.tree.selected.elf.datas, {ID: $(checkSel).val()}));
  783. opt.rank = $(checkSel).attr('rank');
  784. checkedNameArr.push(opt.name);
  785. optionChecked.push(opt);
  786. }
  787. this.displayText = checkedNameArr.length > 0 ? checkedNameArr.join(';') : '';
  788. node.data.options = this.displayText;
  789. node.data.optionChecked = optionChecked;
  790. //删除节点
  791. let deleteNodes = getDeleteNodes(node, optionChecked);
  792. for(let dNode of deleteNodes){
  793. elfSheet.deleteRows(dNode.serialNo(), dNode.posterityCount() + 1);
  794. node.tree.delete(dNode);
  795. }
  796. //插入节点
  797. for(let perCheked of optionChecked){
  798. let exist = false;
  799. let subOpts = getOptions(perCheked, bills.tree.selected.elf.datas);
  800. for(let subNode of node.children){
  801. for(let subOpt of subOpts){
  802. if(subNode.data.ID === subOpt.ID){
  803. exist = true;
  804. break;
  805. }
  806. }
  807. }
  808. //不重复且不为定额时插入
  809. if(!exist && perCheked.type !== itemType.ration){
  810. insertNodeByData(node, perCheked);//这里递归,默认第一个
  811. }
  812. }
  813. TREE_SHEET_HELPER.refreshTreeNodeData(elfItem.treeSetting, elfSheet, node.tree.items, false);
  814. setOptionsCellType(node.tree.items);
  815. refreshInsertRation();
  816. }
  817. //获取删除节点
  818. function getDeleteNodes(node, optionChecked) {
  819. let rst = [];
  820. for(let subNode of node.children){
  821. let exist = false;
  822. for(let perChecked of optionChecked){
  823. let subOpts = getOptions(perChecked, bills.tree.selected.elf.datas);
  824. for(let subOpt of subOpts){
  825. if(subNode.data.ID === subOpt.ID){
  826. exist = true;
  827. break;
  828. }
  829. }
  830. }
  831. if(!exist){
  832. rst.push(subNode);
  833. }
  834. }
  835. return rst;
  836. }
  837. OptionsCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  838. OptionsCellType.prototype.createEditorElement = function (context) {
  839. let element = document.createElement("div");//这里创建的,会自动销毁
  840. return element
  841. };
  842. OptionsCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  843. if (editorContext) {
  844. let $editor = $(editorContext);
  845. $editor.css("position", "fixed");
  846. $editor.css("background", "white");
  847. $editor.css("width", cellRect.width);
  848. $editor.attr("gcUIElement", "gcEditingInput");
  849. let activeCellTop = $editor.parent().parent().css('top');
  850. let node = bills.tree.selected.elf.tree.items[elfSheet.getActiveRowIndex()];
  851. setOptionsDiv($editor, node, cellRect, cellStyle, activeCellTop);
  852. this.isEscKey = false;
  853. }
  854. }
  855. OptionsCellType.prototype.deactivateEditor = function (editorContext, context) {
  856. };
  857. OptionsCellType.prototype.setEditorValue = function (editor, value, context) {
  858. this.displayText = value;
  859. };
  860. OptionsCellType.prototype.getEditorValue = function (editor, context) {
  861. let me = this;
  862. let node = bills.tree.selected.elf.tree.items[elfSheet.getActiveRowIndex()];
  863. if(this.isEscKey !=true){
  864. renderSheetFunc(elfSheet, function () {
  865. doAfterSel.call(me, node);
  866. });
  867. }
  868. this.isEscKey = false;
  869. return this.displayText;
  870. };
  871. OptionsCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  872. };
  873. OptionsCellType.prototype.isReservedKey = function (e, context) {
  874. //cell type handle tab key by itself
  875. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  876. return false;
  877. };
  878. /* OptionsCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  879. if(style.backColor){
  880. ctx.fillStyle = style.backColor;
  881. ctx.fillRect(x, y, w, h);
  882. ctx.save();
  883. }
  884. //边长
  885. const l = 7;
  886. let leftPointX = x + w - 15,
  887. rightPointX = leftPointX + l,
  888. middlePointX = (leftPointX + rightPointX)/2;
  889. const cos30 = Math.cos(2*Math.PI * 30 / 360);
  890. let hL = l * cos30;
  891. let beginY = y + h/2 - hL;
  892. ctx.beginPath();
  893. ctx.moveTo(leftPointX, beginY);
  894. ctx.lineTo(rightPointX, beginY);
  895. ctx.lineTo(middlePointX, beginY + hL);
  896. ctx.fillStyle = 'black';
  897. ctx.fill();
  898. ctx.save();
  899. };*/
  900. // override getHitInfo to allow cell type get mouse messages
  901. OptionsCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  902. return {
  903. x: x,
  904. y: y,
  905. row: context.row,
  906. col: context.col,
  907. cellStyle: cellStyle,
  908. cellRect: cellRect,
  909. sheetArea: context.sheetArea
  910. };
  911. };
  912. return new OptionsCellType();
  913. }
  914. //初始化清单的工作内容和项目特征
  915. //@param {Number}billsLibId {Function}callback @return {void}
  916. function initJobAndCharacter(billsLibId, callback){
  917. CommonAjax.post('/stdBillsEditor/getJobContent', {userId: userID, billsLibId: billsLibId}, function (datas) {
  918. stdBillsJobData = datas;
  919. CommonAjax.post('/stdBillsEditor/getItemCharacter', {userId: userID, billsLibId: billsLibId}, function (datas) {
  920. stdBillsFeatureData = datas;
  921. if(callback){
  922. callback();
  923. }
  924. });
  925. });
  926. }
  927. //初始化清单展开收起状态
  928. //@return {void}
  929. function initExpandStat(){
  930. //读取展开收起状态
  931. let currentExpState = sessionStorage.getItem('stdBillsGuidanceExpState');
  932. if(currentExpState){
  933. bills.tree.setExpandedByState(bills.tree.items, currentExpState);
  934. }
  935. //非叶子节点默认收起
  936. else{
  937. bills.tree.setRootExpanded(bills.tree.roots, false);
  938. }
  939. }
  940. //设置tag以悬浮提示
  941. function setTagForHint(nodes){
  942. let sheet = bills.workBook.getActiveSheet();
  943. renderSheetFunc(sheet, function () {
  944. for(let node of nodes){
  945. sheet.setTag(node.serialNo(), 2, node.data.ruleText ? node.data.ruleText : '');
  946. }
  947. });
  948. }
  949. //根据编码定位至清单精灵库中
  950. //@param {String}code @return {void}
  951. function locateAtBills(code) {
  952. let nineCode = code.substring(0, 9);
  953. let items = bills.tree.items;
  954. let locateBills = _.find(items, function(item){
  955. return item.data.code === nineCode;
  956. });
  957. if(locateBills){
  958. expandSearchNodes([locateBills]);
  959. sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
  960. }
  961. let sheet = bills.workBook.getActiveSheet();
  962. let locateRow = locateBills ? locateBills.serialNo() : 0;
  963. sheet.setActiveCell(locateRow, 0);
  964. billsInitSel(locateRow);
  965. sheet.showRow(locateRow, GC.Spread.Sheets.VerticalPosition.center);
  966. }
  967. //清单设置悬浮提示信息
  968. //@param {Array}billsNodes(清单节点) {Array}jobs(总的工作内容数据) {Array}items(总的项目特征数据)
  969. function setBillsHint(billsNodes, jobs, items) {
  970. let jobsMapping = {},
  971. itemsMapping = {};
  972. for(let job of jobs){
  973. jobsMapping[job.id] = job;
  974. }
  975. for(let item of items){
  976. itemsMapping[item.id] = item;
  977. }
  978. let tagInfo = [];
  979. for(let billsNode of billsNodes){
  980. let hintArr = [];
  981. let billsItems = billsNode.data.items;
  982. if(billsItems.length > 0){
  983. //项目特征
  984. hintArr.push('项目特征:');
  985. }
  986. let itemCount = 1,
  987. jobCount = 1;
  988. for(let billsItem of billsItems){
  989. let itemData = itemsMapping[billsItem.id];
  990. if(itemData){
  991. //特征值
  992. let eigens = [];
  993. for(let eigen of itemData.itemValue){
  994. eigens.push(eigen.value);
  995. }
  996. eigens = eigens.join(';');
  997. hintArr.push(`${itemCount}.${itemData.content}${eigens === '' ? '' : ': ' + eigens}`);
  998. itemCount ++;
  999. }
  1000. }
  1001. //工作内容
  1002. let billsJobs = billsNode.data.jobs;
  1003. if(billsJobs.length > 0){
  1004. hintArr.push('工作内容:');
  1005. }
  1006. for(let billsJob of billsJobs){
  1007. let jobData = jobsMapping[billsJob.id];
  1008. if(jobData){
  1009. hintArr.push(`${jobCount}.${jobData.content}`);
  1010. jobCount ++;
  1011. }
  1012. }
  1013. /*if(billsNode.data.ruleText && billsNode.data.ruleText !== ''){
  1014. hintArr.push('工程量计算规则:');
  1015. hintArr.push(billsNode.data.ruleText);
  1016. }
  1017. if(billsNode.data.recharge && billsNode.data.recharge !== ''){
  1018. hintArr.push('补注:');
  1019. hintArr.push(billsNode.data.recharge);
  1020. }*/
  1021. if(hintArr.length > 0){
  1022. tagInfo.push({row: billsNode.serialNo(), value: hintArr.join('\n')});
  1023. }
  1024. }
  1025. let sheet = bills.workBook.getActiveSheet();
  1026. renderSheetFunc(sheet, function () {
  1027. for(let tagI of tagInfo){
  1028. sheet.setTag(tagI.row, 0, tagI.value);
  1029. }
  1030. });
  1031. }
  1032. //初始选择清单指引库
  1033. //@param {Number}libID @return {void}
  1034. function libInitSel(libID){
  1035. //获取清单
  1036. $.bootstrapLoading.start();
  1037. CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function(rstData){
  1038. currentLib = rstData.guidanceLib;
  1039. if(guideItem.workBook){
  1040. guideItem.workBook.destroy();
  1041. guideItem.workBook = null;
  1042. }
  1043. if(elfItem.workBook){
  1044. elfItem.workBook.destroy();
  1045. elfItem.workBook = null;
  1046. }
  1047. initViews();
  1048. let callback = function () {
  1049. initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, rstData.bills);
  1050. //清单精灵
  1051. if(rstData.guidanceLib.type && rstData.guidanceLib.type == libType.elf){
  1052. $('#stdBillsGuidanceTab').text('清单精灵');
  1053. //每一个清单节点下挂载一棵清单精灵树
  1054. for(let node of bills.tree.items){
  1055. node.elf = {tree: null, controller: null, datas: []}; //挂载全部数据,数据不一定全成为树节点
  1056. }
  1057. }
  1058. //清单指引
  1059. else {
  1060. $('#stdBillsGuidanceTab').text('清单指引');
  1061. //每一棵项目指引树挂在清单节点上
  1062. for(let node of bills.tree.items){
  1063. node.guidance = {tree: null, controller: null};
  1064. }
  1065. }
  1066. //setTagForHint(bills.tree.items);
  1067. //默认初始节点
  1068. billsInitSel(0);
  1069. if(doAfterLoadGuidance){
  1070. doAfterLoadGuidance();
  1071. }
  1072. $.bootstrapLoading.end();
  1073. };
  1074. //获取清单库中的工作内容和项目特征
  1075. initJobAndCharacter(rstData.guidanceLib.billsLibId, callback);
  1076. }, function () {
  1077. $.bootstrapLoading.end();
  1078. });
  1079. }
  1080. //初始化清单指引库
  1081. //@param {Array}libDats @return {void}
  1082. function initLibs(libDatas){
  1083. libSel.empty();
  1084. if(!libDatas){
  1085. return;
  1086. }
  1087. let selectedLib = sessionStorage.getItem('stdBillsGuidance');
  1088. for(let libData of libDatas){
  1089. let opt = $('<option>').val(libData.id).text(libData.name);
  1090. if(selectedLib && libData.id == selectedLib){
  1091. opt.attr('selected', 'selected');
  1092. }
  1093. libSel.append(opt);
  1094. }
  1095. //初始默认选择
  1096. libInitSel(libSel.select().val());
  1097. }
  1098. //初始化视图
  1099. //@param {void} @return {void}
  1100. function initViews(){
  1101. //赋初始高度
  1102. if($('#billsGuidance_bills').height() === 0 || $('#billsGuidance_items').height() === 0){
  1103. let height = $(window).height()-$(".header").height()-$(".toolsbar").height()-$(".tools-bar-height-z").height();
  1104. $('#billsGuidance_bills').height(height / 2);
  1105. $('#billsGuidance_items').height(height / 2);
  1106. }
  1107. let modules = [bills];
  1108. if(currentLib.type && currentLib.type === libType.elf){
  1109. modules.push(elfItem);
  1110. }
  1111. else {
  1112. modules.push(guideItem);
  1113. }
  1114. initWorkBooks(modules);
  1115. }
  1116. //获取选中的行
  1117. //@return {Array}
  1118. function getCheckedRows(){
  1119. let rst = [];
  1120. let itemSheet = guideItem.workBook.getActiveSheet();
  1121. for(let row = 0; row < itemSheet.getRowCount(); row++){
  1122. let rowV = itemSheet.getValue(row, 0);
  1123. if(rowV){
  1124. rst.push(row);
  1125. }
  1126. }
  1127. return rst;
  1128. }
  1129. //获取清单精灵生成的定额数据
  1130. //@return {Array}
  1131. function getInsertElfRationData(){
  1132. let rst = [];
  1133. if(!bills.tree.selected){
  1134. return [];
  1135. }
  1136. if(!bills.tree.selected.elf){
  1137. return [];
  1138. }
  1139. let tree = bills.tree.selected.elf.tree;
  1140. if(!tree){
  1141. return [];
  1142. }
  1143. for(let node of tree.items){
  1144. for(let perChecked of node.data.optionChecked){
  1145. //选项直接是定额
  1146. if(perChecked.type === itemType.ration){
  1147. rst.push({itemQuery: {userID: userID, ID: perChecked.rationID}, rationType: rationType.ration});
  1148. }
  1149. //选项下子选项是定额
  1150. else {
  1151. let rationOpts = getOptions(perChecked, bills.tree.selected.elf.datas);
  1152. for(let ration of rationOpts){
  1153. if(ration.type === itemType.ration){
  1154. rst.push({itemQuery: {userID: userID, ID: ration.rationID}, rationType: rationType.ration});
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. return rst;
  1161. }
  1162. //获取选中的定额数据
  1163. //@param {Array}rows @return {Array}
  1164. function getInsertRationData(rows){
  1165. let rst = [];
  1166. for(let row of rows){
  1167. let node = bills.tree.selected.guidance.tree.items[row];
  1168. if(node && node.data.type === itemType.ration){
  1169. rst.push({itemQuery: {userID: userID, ID: node.data.rationID}, rationType: rationType.ration});
  1170. }
  1171. }
  1172. return rst;
  1173. }
  1174. //插入定额
  1175. //@return {void}
  1176. function insertRations(addRationDatas){
  1177. if(addRationDatas.length > 0){
  1178. projectObj.project.Ration.addMultiRation(addRationDatas, function () {
  1179. //恢复
  1180. if(!currentLib.type || currentLib.type === libType.guidance){
  1181. let sheet = guideItem.workBook.getActiveSheet();
  1182. renderSheetFunc(sheet, function () {
  1183. for(let row = 0; row < sheet.getRowCount(); row++){
  1184. if(sheet.getValue(row, 0)){
  1185. sheet.setValue(row, 0, false);
  1186. }
  1187. }
  1188. });
  1189. }
  1190. refreshInsertRation();
  1191. projectObj.setActiveCell('quantity', true);
  1192. });
  1193. }
  1194. }
  1195. //更新插入定额按钮有效性
  1196. function refreshInsertRation(){
  1197. if(currentLib.type && currentLib.type === libType.elf){
  1198. if(getInsertElfRationData().length > 0){
  1199. $('#guidanceInsertRation').removeClass('disabled');
  1200. }
  1201. else {
  1202. $('#guidanceInsertRation').addClass('disabled');
  1203. }
  1204. }
  1205. else {
  1206. //勾选了定额,插入定额按钮才有效
  1207. if(getCheckedRows().length > 0){
  1208. $('#guidanceInsertRation').removeClass('disabled');
  1209. }
  1210. else {
  1211. $('#guidanceInsertRation').addClass('disabled');
  1212. }
  1213. }
  1214. }
  1215. //展开至搜索出来点的节点
  1216. //@param {Array}nodes @return {void}
  1217. function expandSearchNodes(nodes){
  1218. let that = this;
  1219. let billsSheet = bills.workBook.getActiveSheet();
  1220. renderSheetFunc(billsSheet, function () {
  1221. function expParentNode(node){
  1222. if(node.parent && !node.parent.expanded){
  1223. node.parent.setExpanded(true);
  1224. expParentNode(node.parent);
  1225. }
  1226. }
  1227. for(let node of nodes){
  1228. expParentNode(node);
  1229. }
  1230. TREE_SHEET_HELPER.refreshTreeNodeData(bills.treeSetting, billsSheet, bills.tree.roots, true);
  1231. TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.roots, billsSheet, true);
  1232. });
  1233. }
  1234. //各按钮监听事件
  1235. //@return {void}
  1236. function bindBtn(){
  1237. //打开清单指引库
  1238. $('#stdBillsGuidanceTab').click(function () {
  1239. if(libSel.children().length === 0 && !projectReadOnly && !$(this).hasClass('disabled')){
  1240. initLibs(projectInfoObj.projectInfo.engineeringInfo.billsGuidance_lib);
  1241. }
  1242. });
  1243. //更改清单指引库
  1244. $('#stdBillsGuidanceLibSelect').change(function () {
  1245. //关闭搜索窗口
  1246. $('#billsGuidanceSearchResult').hide();
  1247. billsLibObj.clearHighLight(bills.workBook);
  1248. libInitSel($(this).select().val());
  1249. //记住选项
  1250. sessionStorage.setItem('stdBillsGuidance', $(this).select().val());
  1251. //清除展开收起状态sessionStorage
  1252. sessionStorage.removeItem('stdBillsGuidanceExpState');
  1253. });
  1254. //插入定额
  1255. $('#guidanceInsertRation').click(function () {
  1256. let addRationDatas = currentLib.type && currentLib.type === libType.elf ? getInsertElfRationData() : getInsertRationData(getCheckedRows());
  1257. insertRations(addRationDatas);
  1258. });
  1259. //插入清单
  1260. $('#guidanceInsertBills').click(function () {
  1261. //插入清单
  1262. if(!bills.tree || !bills.tree.selected){
  1263. return;
  1264. }
  1265. if(bills.tree.selected.children.length === 0){
  1266. let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, bills.tree.selected);
  1267. /*if(insert){
  1268. //插入选中的定额
  1269. let addRationDatas = currentLib.type && currentLib.type === libType.elf ? getInsertElfRationData() : getInsertRationData(getCheckedRows());
  1270. insertRations(addRationDatas);
  1271. }*/
  1272. }
  1273. });
  1274. //插入清单和定额
  1275. $('#guidanceInsertBillsAndRation').click(function () {
  1276. //插入清单
  1277. if(!bills.tree || !bills.tree.selected){
  1278. return;
  1279. }
  1280. if(bills.tree.selected.children.length === 0){
  1281. let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, bills.tree.selected);
  1282. if(insert){
  1283. //插入选中的定额
  1284. let addRationDatas = currentLib.type && currentLib.type === libType.elf ? getInsertElfRationData() : getInsertRationData(getCheckedRows());
  1285. insertRations(addRationDatas);
  1286. }
  1287. }
  1288. });
  1289. //搜索
  1290. $('#stdBillsGuidanceSearch>div>button').click(function () {
  1291. if(!bills.tree){
  1292. return;
  1293. }
  1294. let billsSheet = bills.workBook.getActiveSheet();
  1295. billsLibObj.clearHighLight(bills.workBook);
  1296. let keyword = $('#stdBillsGuidanceSearch>input').val();
  1297. if (!keyword || keyword === '') {
  1298. $('#billsGuidanceSearchResult').hide();
  1299. return;
  1300. }
  1301. let result = bills.tree.items.filter(function (item) {
  1302. let codeIs = item.data.code ? item.data.code.indexOf(keyword) !== -1 : false;
  1303. let nameIs = item.data.name ? item.data.name.indexOf(keyword) !== -1 : false;
  1304. return codeIs || nameIs;
  1305. });
  1306. result.sort(function (x, y) {
  1307. return x.serialNo() - y.serialNo();
  1308. });
  1309. if (result.length !== 0) {
  1310. //展开搜索出来的节点
  1311. expandSearchNodes(result);
  1312. //设置记住展开
  1313. sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
  1314. let sel = billsSheet.getSelections();
  1315. renderSheetFunc(billsSheet, function () {
  1316. bills.controller.setTreeSelected(result[0]);
  1317. billsSheet.setSelection(result[0].serialNo(), sel[0].col, 1, 1);
  1318. billsInitSel(result[0].serialNo());
  1319. for (let node of result) {
  1320. billsSheet.getRange(node.serialNo(), -1, 1, -1).backColor('lemonChiffon');
  1321. }
  1322. });
  1323. //搜索初始定位
  1324. billsSheet.showRow(result[0].serialNo(), GC.Spread.Sheets.VerticalPosition.bottom);
  1325. $('#nextBillsGuidance').show();
  1326. $('#nextBillsGuidance').unbind('click');
  1327. $('#nextBillsGuidance').bind('click', function () {
  1328. let cur = bills.tree.selected, resultIndex = result.indexOf(cur), sel = billsSheet.getSelections();
  1329. if (resultIndex === result.length - 1) {
  1330. bills.controller.setTreeSelected(result[0]);
  1331. billsSheet.setSelection(result[0].serialNo(), sel[0].col, 1, 1);
  1332. billsInitSel(result[0].serialNo());
  1333. billsSheet.showRow(result[0].serialNo(), GC.Spread.Sheets.VerticalPosition.bottom);
  1334. } else {
  1335. bills.controller.setTreeSelected(result[resultIndex + 1]);
  1336. billsSheet.setSelection(result[resultIndex + 1].serialNo(), sel[0].col, 1, 1);
  1337. billsInitSel(result[resultIndex + 1].serialNo());
  1338. billsSheet.showRow(result[resultIndex + 1].serialNo(), GC.Spread.Sheets.VerticalPosition.bottom);
  1339. }
  1340. });
  1341. } else {
  1342. billsLibObj.clearHighLight(bills.workBook);
  1343. $('#nextBillsGuidance').hide();
  1344. }
  1345. $('#billsGuidanceSearchResultCount').text('搜索结果:' + result.length);
  1346. $('#billsGuidanceSearchResult').show();
  1347. });
  1348. //搜索框回车
  1349. $('#stdBillsGuidanceSearch>input').bind('keypress', function (event) {
  1350. if(event.keyCode === 13){
  1351. $(this).blur();
  1352. $('#stdBillsGuidanceSearch>div>button').click();
  1353. }
  1354. });
  1355. // 关闭搜索结果
  1356. $('#closeSearchBillsGuidance').click(function () {
  1357. $('#billsGuidanceSearchResult').hide();
  1358. billsLibObj.clearHighLight(bills.workBook);
  1359. refreshWorkBook();
  1360. });
  1361. //监听alt建,确定选项单选多选状态
  1362. $('#billsGuidance_items').keydown(function(e){
  1363. if(e.keyCode === 18){
  1364. billsGuidanceSelMode = 1;
  1365. }
  1366. });
  1367. $('#billsGuidance_items').keyup(function(e){
  1368. if(e.keyCode === 18){
  1369. billsGuidanceSelMode = 0;
  1370. }
  1371. });
  1372. }
  1373. //刷新表
  1374. //@return {void}
  1375. function refreshWorkBook(){
  1376. if(bills.workBook){
  1377. bills.workBook.refresh();
  1378. }
  1379. if(guideItem.workBook){
  1380. guideItem.workBook.refresh();
  1381. }
  1382. if(elfItem.workBook){
  1383. elfItem.workBook.refresh();
  1384. }
  1385. }
  1386. return {initViews, bindBtn, refreshWorkBook, refreshInsertRation, setColumnWidthByRate, locateAtBills, bills, elfItem};
  1387. })();
  1388. $(document).ready(function(){
  1389. billsGuidance.bindBtn();
  1390. });