billsElf.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/11/25
  7. * @version
  8. */
  9. /*
  10. * 造价书下方清单精灵、清单指引
  11. * */
  12. //选项单选多选状态(按住alt为多选) 单选:0 多选:1
  13. let billsGuidanceSelMode = 0;
  14. const BillsSub = (function() {
  15. //清单子树挂载的地方,selected:当前选中的清单,mapping:以前九位清单编码为索引, 'xxx' : {sub: {datas, tree, controller}}
  16. let bills = {selected: null, mapping: {}};
  17. // 指引类型
  18. const itemType = {
  19. // 工作内容
  20. job: 0,
  21. // 定额
  22. ration: 1
  23. };
  24. // 库类型
  25. const libType = {
  26. // 清单指引
  27. guidance: 1,
  28. // 清单精灵
  29. elf: 2
  30. };
  31. // 清单精灵
  32. const elfItem = {
  33. dom: $('#billsSubItems'),
  34. workBook: null,
  35. tree: null,
  36. controller: null,
  37. treeSetting: {
  38. treeCol: 0,
  39. emptyRows: 0,
  40. headRows: 1,
  41. headRowHeight: [40],
  42. defaultRowHeight: 21,
  43. cols: [
  44. {
  45. width: 250,
  46. readOnly: true,
  47. head: {
  48. titleNames: ["施工工序"],
  49. spanCols: [1],
  50. spanRows: [1],
  51. vAlign: [1],
  52. hAlign: [1],
  53. font: ["Arial"]
  54. },
  55. data: {
  56. field: "name",
  57. vAlign: 1,
  58. hAlign: 0,
  59. font: "Arial"
  60. }
  61. },
  62. {
  63. width: 250,
  64. readOnly: false,
  65. head: {
  66. titleNames: ["选项"],
  67. spanCols: [1],
  68. spanRows: [1],
  69. vAlign: [1],
  70. hAlign: [1],
  71. font: ["Arial"]
  72. },
  73. data: {
  74. field: "options",
  75. vAlign: 1,
  76. hAlign: 0,
  77. font: "Arial"
  78. }
  79. }
  80. ]
  81. },
  82. headers: [
  83. {name: '施工工序', dataCode: 'name', width: 180, rateWidth: 0.5, vAlign: 'center', hAlign: 'center', formatter: '@'},
  84. {name: '选项', dataCode: 'options', width: 180, rateWidth: 0.5, vAlign: 'center', hAlign: 'left', formatter: '@'},
  85. ],
  86. rowHeaderWidth:25,
  87. events: {
  88. SelectionChanging: function (sender, info) {
  89. elfItemInitSel(info.newSelections[0].row);
  90. },
  91. CellClick: function (sender, args) {
  92. if(elfItem.headers[args.col]['dataCode'] === 'options' && args.sheetArea === 3){
  93. if(!args.sheet.getCell(args.row, args.col).locked() && !args.sheet.isEditing()){
  94. args.sheet.startEdit();
  95. }
  96. }
  97. },
  98. ClipboardPasting: function (sender, info) {
  99. info.cancel = true;
  100. }
  101. }
  102. };
  103. // 清单指引
  104. const guideItem = {
  105. dom: $('#billsSubItems'),
  106. workBook: null,
  107. tree: null,
  108. controller: null,
  109. treeSetting: {
  110. treeCol: 0,
  111. emptyRows: 0,
  112. headRows: 1,
  113. headRowHeight: [40],
  114. defaultRowHeight: 21,
  115. cols: [
  116. {
  117. width: 420,
  118. readOnly: true,
  119. head: {
  120. titleNames: ["项目指引"],
  121. spanCols: [1],
  122. spanRows: [1],
  123. vAlign: [1],
  124. hAlign: [1],
  125. font: ["Arial"]
  126. },
  127. data: {
  128. field: "name",
  129. vAlign: 1,
  130. hAlign: 0,
  131. font: "Arial"
  132. }
  133. },
  134. {
  135. width: 35,
  136. readOnly: false,
  137. head: {
  138. titleNames: ["选择"],
  139. spanCols: [1],
  140. spanRows: [1],
  141. vAlign: [1],
  142. hAlign: [1],
  143. font: ["Arial"]
  144. },
  145. data: {
  146. field: "select",
  147. vAlign: 1,
  148. hAlign: 1,
  149. font: "Arial"
  150. }
  151. }
  152. ]
  153. },
  154. headers: [
  155. {name: '项目指引', dataCode: 'name', width: 300, vAlign: 'center', hAlign: 'left', formatter: '@'},
  156. {name: '选择', dataCode: 'select', width: 35, vAlign: 'center', hAlign: 'center', formatter: '@'},
  157. ],
  158. rowHeaderWidth:25,
  159. events: {
  160. /*EditStarting: function (sender, args) {
  161. if(!bills.tree || guideItem.headers[args.col]['dataCode'] === 'name'){
  162. args.cancel = true;
  163. }
  164. },*/
  165. ButtonClicked: function (sender, args) {
  166. if(args.sheet.isEditing()){
  167. args.sheet.endEdit(true);
  168. }
  169. //refreshInsertRation();
  170. },
  171. }
  172. };
  173. // 目前的模块:清单精灵或清单指引,默认是清单指引
  174. let curModule = guideItem;
  175. // 切换目前的模块
  176. // 1:清单指引 2:清单精灵
  177. function switchModule(type) {
  178. let libText;
  179. if (type === libType.guidance) {
  180. curModule = guideItem;
  181. libText = '清单指引';
  182. // 动态按钮
  183. $('#guidanceInsertRation').show();
  184. $('#elfInsertRation').hide();
  185. $('#elfInsertSingle').hide();
  186. } else {
  187. curModule = elfItem;
  188. libText = '清单精灵';
  189. // 动态按钮
  190. $('#guidanceInsertRation').hide();
  191. $('#elfInsertRation').show();
  192. $('#elfInsertSingle').show();
  193. }
  194. $('#qdjlTools').show();
  195. // 库名称、清单子菜单名称动态显示
  196. $('#linkQDJL').text(libText);
  197. // 监听按钮事件
  198. bindListener();
  199. }
  200. const options = {
  201. workBook: {
  202. tabStripVisible: false,
  203. allowContextMenu: false,
  204. allowCopyPasteExcelStyle : false,
  205. allowExtendPasteRange: false,
  206. allowUserDragDrop : false,
  207. allowUserDragFill: false,
  208. scrollbarMaxAlign : true
  209. },
  210. sheet: {
  211. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  212. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  213. }
  214. };
  215. //渲染时方法,停止渲染
  216. //@param {Object}sheet {Function}func @return {void}
  217. function renderSheetFunc(sheet, func){
  218. sheet.suspendEvent();
  219. sheet.suspendPaint();
  220. if(func){
  221. func();
  222. }
  223. sheet.resumeEvent();
  224. sheet.resumePaint();
  225. }
  226. //设置表选项
  227. //@param {Object}workBook {Object}opts @return {void}
  228. function setOptions(workBook, opts) {
  229. for(let opt in opts.workBook){
  230. workBook.options[opt] = opts.workBook[opt];
  231. }
  232. for(let opt in opts.sheet){
  233. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  234. }
  235. }
  236. //建表头
  237. //@param {Object}sheet {Array}headers @return {void}
  238. function buildHeader(sheet, headers) {
  239. let fuc = function () {
  240. sheet.setColumnCount(headers.length);
  241. sheet.setRowHeight(0, 30, GC.Spread.Sheets.SheetArea.colHeader);
  242. sheet.setColumnWidth(0, sheet.getParent() === bills.workBook ? 15 : 25, GC.Spread.Sheets.SheetArea.rowHeader);
  243. if(sheet.getParent() === elfItem.workBook || sheet.getParent() === guideItem.workBook){
  244. sheet.setRowHeight(0, 20, GC.Spread.Sheets.SheetArea.colHeader);
  245. }
  246. for(let i = 0, len = headers.length; i < len; i++){
  247. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  248. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  249. if(headers[i].formatter){
  250. sheet.setFormatter(-1, i, headers[i].formatter);
  251. }
  252. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  253. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  254. }
  255. };
  256. renderSheetFunc(sheet, fuc);
  257. }
  258. //表监听事件
  259. //@param {Object}workBook @return {void}
  260. function bindEvent(workBook, events) {
  261. if(Object.keys(events).length === 0){
  262. return;
  263. }
  264. const Events = GC.Spread.Sheets.Events;
  265. for(let event in events){
  266. workBook.bind(Events[event], events[event]);
  267. }
  268. }
  269. //根据宽度比例设置列宽
  270. //@param {Object}workBook {Number}workBookWidth {Array}headers @return {void}
  271. function setColumnWidthByRate() {
  272. let workBook = elfItem.workBook,
  273. workBookWidth = ($(window).width() - $('.main').find('.main-nav').width() - $('.main-side').width()) * 5 / 6,
  274. headers = elfItem.headers;
  275. if(workBook){
  276. workBookWidth -= 55;
  277. const sheet = workBook.getActiveSheet();
  278. sheet.suspendEvent();
  279. sheet.suspendPaint();
  280. for(let col = 0; col < headers.length; col++){
  281. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  282. let width = workBookWidth * headers[col]['rateWidth'];
  283. if(headers[col]['dataCode'] === 'options'){
  284. width = width;
  285. }
  286. sheet.setColumnWidth(col, width, GC.Spread.Sheets.SheetArea.colHeader)
  287. }
  288. else {
  289. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  290. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  291. }
  292. }
  293. }
  294. sheet.resumeEvent();
  295. sheet.resumePaint();
  296. }
  297. }
  298. //建表
  299. //@param {Object}module @return {void}
  300. function buildSheet() {
  301. console.log(curModule);
  302. if(!curModule.workBook){
  303. curModule.workBook = new GC.Spread.Sheets.Workbook(curModule.dom[0], {sheetCount: 1});
  304. sheetCommonObj.spreadDefaultStyle(curModule.workBook);
  305. let sheet = curModule.workBook.getActiveSheet();
  306. /*sheet.options.isProtected = true;
  307. sheet.getRange(-1, 0, -1, 1).locked(true);
  308. sheet.getRange(-1, 1, -1, 1).locked(false);*/
  309. if(curModule.rowHeaderWidth) {
  310. sheet.setColumnWidth(0, curModule.rowHeaderWidth, GC.Spread.Sheets.SheetArea.rowHeader);
  311. }
  312. setOptions(curModule.workBook, options);
  313. buildHeader(curModule.workBook.getActiveSheet(), curModule.headers);
  314. bindEvent(curModule.workBook, curModule.events);
  315. }
  316. }
  317. //刷新表
  318. //@return {void}
  319. function refreshWorkBook(){
  320. //计算内部的表格高度
  321. if ($('#qdjl').is(':visible')) {
  322. let totalHeight = $('#qdjl').height(),
  323. elfToolsHeight = $('#qdjlTools').height();
  324. $('#billsSubItems').height(totalHeight - elfToolsHeight);
  325. if (curModule.workBook) {
  326. curModule.workBook.refresh();
  327. }
  328. }
  329. }
  330. //清空表数据
  331. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  332. function cleanData(sheet, headers, rowCount){
  333. renderSheetFunc(sheet, function () {
  334. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  335. if (rowCount > 0) {
  336. sheet.setRowCount(rowCount);
  337. } else {
  338. sheet.setRowCount(0);
  339. }
  340. });
  341. }
  342. //初始化并输出树
  343. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  344. function initTree(module, sheet, treeSetting, datas){
  345. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  346. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting, false);
  347. module.tree.loadDatas(datas);
  348. module.controller.showTreeData();
  349. }
  350. //清单精灵表焦点控制
  351. //@param {Number}row @return {void}
  352. function elfItemInitSel(row){
  353. let billsNode = bills.selected;
  354. let node = null;
  355. if(billsNode && billsNode.sub.tree){
  356. node = billsNode.sub.tree.items[row];
  357. if(node){
  358. billsNode.sub.tree.selected = node;
  359. }
  360. }
  361. }
  362. // 清单精灵数据初始化
  363. function initElf(sheet, elf, treeData) {
  364. //定额数据删除编号信息,(编码后+空格才会去除编码)
  365. for(let rData of treeData){
  366. if(rData.type === itemType.ration){
  367. let nameArr = rData.name.split(' ');
  368. if(nameArr.length > 1){
  369. nameArr.splice(0, 1);
  370. rData.name = nameArr.join(' ');
  371. }
  372. }
  373. }
  374. elf.sub.datas = treeData;
  375. //第一层节点数据
  376. let firstLevelDatas = _.filter(treeData, function (data) {
  377. return data.ParentID == -1;
  378. });
  379. //第一层初始数据的选项显示
  380. for(let fData of firstLevelDatas){
  381. let options = getOptions(fData, treeData);
  382. fData.options = options.length > 0 ? options[0].name : '';
  383. //下挂的选项
  384. fData.optionsData = options && options.length > 0 ? _.cloneDeep(options) : [];
  385. fData.optionChecked = options && options.length > 0 ? [_.cloneDeep(options[0])] : [];
  386. }
  387. renderSheetFunc(sheet, function () {
  388. initTree(elf.sub, sheet, elfItem.treeSetting, firstLevelDatas);
  389. //初始选择选项
  390. let initOptsOpr = [];
  391. for(let elfNode of elf.sub.tree.items){
  392. if(elfNode.data.optionsData.length > 0){
  393. initOptsOpr.push({node: elfNode, data: elfNode.data.optionsData[0]});
  394. }
  395. }
  396. for(let opr of initOptsOpr){
  397. insertNodeByData(opr.node, opr.data);
  398. }
  399. TREE_SHEET_HELPER.refreshTreeNodeData(elfItem.treeSetting, sheet, elf.sub.tree.items, false);
  400. setOptionsCellType(elf.sub.tree.items);
  401. //项目指引初始焦点
  402. elfItemInitSel(sheet.getActiveRowIndex() ? sheet.getActiveRowIndex() : 0);
  403. });
  404. }
  405. // 已经初始化过的清单精灵数据,重新展示
  406. function reshowElf(sheet, elf) {
  407. renderSheetFunc(sheet, function () {
  408. elf.sub.controller.showTreeData();
  409. setOptionsCellType(elf.sub.tree.items);
  410. //清单精灵初始焦点
  411. elfItemInitSel(sheet.getActiveRowIndex() ? sheet.getActiveRowIndex() : 0);
  412. });
  413. }
  414. //根据项目指引的类型设置单元格类型,定额类型的项目指引为复选框
  415. //@param {Array}nodes @return {void}
  416. function setItemCellType(nodes){
  417. //设置单元格类型
  418. const base = new GC.Spread.Sheets.CellTypes.Base();
  419. const checkBox = new GC.Spread.Sheets.CellTypes.CheckBox();
  420. const sheet = guideItem.workBook.getActiveSheet();
  421. renderSheetFunc(sheet, function(){
  422. for(let node of nodes){
  423. sheet.setCellType(node.serialNo(), 1, node.data.type === itemType.ration ? checkBox : base);
  424. }
  425. });
  426. }
  427. // 清单指引数据初始化
  428. function initGuidance(sheet, guidance, treeData) {
  429. guidance.sub.datas = treeData;
  430. renderSheetFunc(sheet, function () {
  431. initTree(guidance.sub, sheet, guideItem.treeSetting, treeData);
  432. //TREE_SHEET_HELPER.refreshTreeNodeData(guideItem.treeSetting, sheet, guidance.sub.tree.items, false);
  433. setItemCellType(guidance.sub.tree.items);
  434. });
  435. }
  436. // 已经初始化过的清单指引数据,重新展示
  437. function reshowGuidance(sheet, guidance) {
  438. renderSheetFunc(sheet, function () {
  439. guidance.sub.controller.showTreeData();
  440. setItemCellType(guidance.sub.tree.items);
  441. });
  442. }
  443. //清单焦点变换-清单子界面操作,获取清单前九位编码的标准清单清单精灵选项 或 清单指引数据
  444. //@param {String}code @return {void}
  445. function billsSelSub(code) {
  446. let sheet = curModule.workBook.getActiveSheet();
  447. cleanData(sheet, curModule.headers, -1);
  448. if (!code || code === '') {
  449. return;
  450. }
  451. let nineCode = code.substr(0, 9);
  452. //查看此清单映射是否存在此编码映射数据,不存在,则新建映射
  453. if (!bills.mapping[nineCode]) {
  454. bills.mapping[nineCode] = {sub: {datas: [], tree: null, controller: null}};
  455. }
  456. let container = bills.mapping[nineCode];
  457. bills.selected = container;
  458. if(!container.sub.tree){
  459. let guidanceLibID;
  460. if (projectObj.project.projectInfo.engineeringInfo && projectObj.project.projectInfo.engineeringInfo.billsGuidance_lib) {
  461. guidanceLibID = projectObj.project.projectInfo.engineeringInfo.billsGuidance_lib[0]
  462. ? projectObj.project.projectInfo.engineeringInfo.billsGuidance_lib[0].id
  463. : null;
  464. }
  465. CommonAjax.post('/billsGuidance/api/getItemsByCode', {guidanceLibID: guidanceLibID, code: nineCode}, function (rstData) {
  466. curModule === elfItem
  467. ? initElf(sheet, container, rstData)
  468. : initGuidance(sheet, container, rstData);
  469. });
  470. } else{
  471. curModule === elfItem
  472. ? reshowElf(sheet, container)
  473. : reshowGuidance(sheet, container);
  474. }
  475. }
  476. //获取选项的深度
  477. //@param {Object}opt {Array}options(当前清单所有选项) @return {Array}
  478. function getOptionDepth(opt, options) {
  479. let parent = _.find(options, {ID: opt.ParentID});
  480. let depth = 0;
  481. while (parent){
  482. depth++;
  483. parent = _.find(options, {ID: parent.ParentID});
  484. }
  485. return depth;
  486. }
  487. //获取施工工序含有的选项(即当前施工工序的子项),获取的顺序按照NextSiblingID排序
  488. //@param {Object}process {Array}datas @return {Array}
  489. function getOptions(process, datas) {
  490. let rst = [];
  491. if(!process || !process.ID){
  492. return [];
  493. }
  494. let options = _.filter(datas, function (data) {
  495. return data.ParentID == process.ID;
  496. });
  497. if(options.length === 0){
  498. return [];
  499. }
  500. //根据NextSiblingID排序
  501. let IDMapping = {};
  502. for(let opt of options){
  503. IDMapping[opt.ID] = {self: opt, next: null, pre: null};
  504. }
  505. for(let opt of options){
  506. let next = IDMapping[opt.NextSiblingID] ? IDMapping[opt.NextSiblingID] : null;
  507. if(next){
  508. next.pre = IDMapping[opt.ID];
  509. IDMapping[opt.ID]['next'] = next;
  510. }
  511. }
  512. let first = null,
  513. rank = 0;
  514. for(let ID in IDMapping){
  515. let obj = IDMapping[ID];
  516. if(!obj.pre){
  517. first = obj;
  518. break;
  519. }
  520. }
  521. while(first){
  522. rank++;
  523. first.self.rank = rank;
  524. rst.push(first.self);
  525. first = first.next;
  526. }
  527. //兼容同层节点NextSibling错误的情况下,同层节点还是能显示出来(但是无法保证正确的顺序)
  528. //兼容模式下,不按照NextSibling排序,直接按照options元素位置排序
  529. if (rank !== options.length) {
  530. rst = [];
  531. rank = 0;
  532. for (let opt of options) {
  533. rank++;
  534. opt.rank = rank;
  535. rst.push(opt);
  536. }
  537. }
  538. return rst;
  539. }
  540. //设置清单精灵选项单元格
  541. //@param {Array}nodes @return {void}
  542. function setOptionsCellType(nodes) {
  543. let elfSheet = elfItem.workBook.getActiveSheet();
  544. for(let node of nodes){
  545. if(node.data.optionsData && node.data.optionsData.length > 0){
  546. elfSheet.getCell(node.serialNo(), 1).locked(false).cellType(getOptionsCellType());
  547. }
  548. else {
  549. elfSheet.getCell(node.serialNo(), 1).locked(true).cellType(new GC.Spread.Sheets.CellTypes.Base());
  550. }
  551. }
  552. }
  553. //递归插入节点:原始项目指引数据奇数层为需要插入的节点,偶数层为下拉选项
  554. //@param {Object}node(当前操作的节点) {Object}data(选项) @return {void}
  555. function insertNodeByData(node, data) {
  556. let elfSheet = elfItem.workBook.getActiveSheet();
  557. let sameDepthNodes = node.children;
  558. let insertNextSiblingID = -1,
  559. insertParentID = node.data.ID;
  560. //当前操作节点的选项
  561. let nodeOpts = getOptions(node.data, bills.selected.sub.datas);
  562. let subOpts = getOptions(data, bills.selected.sub.datas);
  563. let dataDepth = getOptionDepth(data, bills.selected.sub.datas);
  564. if(subOpts.length >0 && subOpts[0].type !== itemType.ration){
  565. if((dataDepth + 1) % 2 === 0){
  566. //排序后的数据
  567. let dataWithRank = _.find(nodeOpts, {ID: data.ID});
  568. //确定插入位置
  569. for(let subOpt of subOpts){
  570. for(let subNode of sameDepthNodes){
  571. //同层节点原本选项数据
  572. let subNodeOptData = _.find(bills.selected.sub.datas, {ID: subNode.data.ID});
  573. //同层节点原本父选项数据
  574. let subNodeOptParent = _.find(bills.selected.sub.datas, {ID: subNodeOptData.ParentID});
  575. let subNodeOptParentWithRank = _.find(nodeOpts, {ID: subNodeOptParent.ID});
  576. //父项顺序决定插入位置
  577. if(dataWithRank.rank < subNodeOptParentWithRank.rank){
  578. insertNextSiblingID = subNode.data.ID;
  579. break;
  580. }
  581. //父项顺序相同,根据子项顺序决定插入位置
  582. else if(dataWithRank.rank = subNodeOptParentWithRank.rank){
  583. if(subOpt.rank < subNode.data.rank){
  584. insertNextSiblingID = subNode.data.ID;
  585. break;
  586. }
  587. }
  588. }
  589. let sub2Opts = getOptions(subOpt, bills.selected.sub.datas);
  590. subOpt.options = sub2Opts.length > 0 ? sub2Opts[0].name : '';
  591. let cloneOpt = _.cloneDeep(subOpt);//不改变原本的数据,比如ParentID
  592. cloneOpt.optionChecked = sub2Opts.length > 0 ? [_.cloneDeep(sub2Opts[0])] : [];
  593. cloneOpt.optionsData = sub2Opts.length > 0 ? _.cloneDeep(sub2Opts) : [];
  594. let newNode = node.tree.insertByData(cloneOpt, insertParentID, insertNextSiblingID);
  595. elfSheet.addRows(newNode.serialNo(), 1);
  596. node.tree.selected = newNode;
  597. elfSheet.setSelection(newNode.serialNo(), elfSheet.getSelections()[0].col, 1, 1);
  598. if(sub2Opts.length > 0 && sub2Opts[0].type !== itemType.ration){
  599. insertNodeByData(newNode, sub2Opts[0]);
  600. }
  601. }
  602. }
  603. else {
  604. insertNodeByData(node, subOpts[0]);
  605. }
  606. }
  607. }
  608. //获取选项下拉多选单元格
  609. //@param {void} @return {void}
  610. function getOptionsCellType() {
  611. let me = this;
  612. let elfSheet= elfItem.workBook.getActiveSheet();
  613. function OptionsCellType() {
  614. this.isEscKey=false;
  615. this.displayText='';
  616. }
  617. function setOptionsDiv($editor, node, cellRect, cellStyle, top) {
  618. if(!node){
  619. return '';
  620. }
  621. let height = cellRect.height;
  622. let options = getOptions(node.data, bills.selected.sub.datas);
  623. top = options.length > 6 ? top - 6 * height : top - options.length * height;
  624. let $editInput = $(`<div style="height: ${height}px; background: ${cellStyle.backColor};overflow: hidden; white-space: nowrap; text-overflow: ellipsis">${node.data.options}</div>`),
  625. $optDiv = $(`<div style="position: fixed; width: ${cellRect.width}px; top: ${top}px;background: ${cellStyle.backColor};border: 1px solid; overflow: auto; height: ${options.length > 6 ? height*6+5 : height*options.length+5}px; font-size: 0.9rem;"></div>`);
  626. for(let opt of options){
  627. let $opt = $(`<div title="${opt.name ? opt.name : ''}" class="elf-options" style="cursor: pointer; height: ${height}px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis"></div>`),
  628. $optInput = $(`<input rank="${opt.rank}" value="${opt.ID}" style="cursor: pointer; margin-left: 5px; vertical-align: middle" type="checkbox"
  629. ${node.data.optionChecked && _.find(node.data.optionChecked, {ID: opt.ID}) ? 'checked' : ''} ${projectReadOnly ? 'disabled' : ''}>`);
  630. $opt.text(`${opt.name ? opt.name : ''}`);
  631. $opt.prepend($optInput);
  632. $optDiv.append($opt);
  633. //选项复选框点击监听
  634. if (!projectReadOnly) {
  635. $opt.click(function () {
  636. //单选
  637. if(billsGuidanceSelMode === 0){
  638. let $allInput = $optDiv.find('input');
  639. for(let input of $allInput){
  640. $(input).prop('checked', false);
  641. }
  642. $($optInput).prop('checked', 'checked');
  643. elfItem.workBook.getSheet(0).endEdit();
  644. } else {//多选
  645. }
  646. });
  647. }
  648. }
  649. $editor.append($editInput);
  650. $editor.append($optDiv);
  651. }
  652. //选择后处理
  653. function doAfterSel(node) {
  654. let checkedSels = $('.elf-options').find('input:checked');
  655. let checkedNameArr = [],
  656. optionChecked= [];
  657. for(let checkSel of checkedSels){
  658. let opt = _.cloneDeep(_.find(bills.selected.sub.datas, {ID: $(checkSel).val()}));
  659. opt.rank = $(checkSel).attr('rank');
  660. checkedNameArr.push(opt.name);
  661. optionChecked.push(opt);
  662. }
  663. this.displayText = checkedNameArr.length > 0 ? checkedNameArr.join(';') : '';
  664. node.data.options = this.displayText;
  665. node.data.optionChecked = optionChecked;
  666. //删除节点
  667. let deleteNodes = getDeleteNodes(node, optionChecked);
  668. for(let dNode of deleteNodes){
  669. elfSheet.deleteRows(dNode.serialNo(), dNode.posterityCount() + 1);
  670. node.tree.delete(dNode);
  671. }
  672. //插入节点
  673. for(let perCheked of optionChecked){
  674. let exist = false;
  675. let subOpts = getOptions(perCheked, bills.selected.sub.datas);
  676. for(let subNode of node.children){
  677. for(let subOpt of subOpts){
  678. if(subNode.data.ID === subOpt.ID){
  679. exist = true;
  680. break;
  681. }
  682. }
  683. }
  684. //不重复且不为定额时插入
  685. if(!exist && perCheked.type !== itemType.ration){
  686. insertNodeByData(node, perCheked);//这里递归,默认第一个
  687. }
  688. }
  689. TREE_SHEET_HELPER.refreshTreeNodeData(elfItem.treeSetting, elfSheet, node.tree.items, false);
  690. setOptionsCellType(node.tree.items);
  691. }
  692. //获取删除节点
  693. function getDeleteNodes(node, optionChecked) {
  694. let rst = [];
  695. for(let subNode of node.children){
  696. let exist = false;
  697. for(let perChecked of optionChecked){
  698. let subOpts = getOptions(perChecked, bills.selected.sub.datas);
  699. for(let subOpt of subOpts){
  700. if(subNode.data.ID === subOpt.ID){
  701. exist = true;
  702. break;
  703. }
  704. }
  705. }
  706. if(!exist){
  707. rst.push(subNode);
  708. }
  709. }
  710. return rst;
  711. }
  712. OptionsCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  713. OptionsCellType.prototype.createEditorElement = function (context) {
  714. let element = document.createElement("div");//这里创建的,会自动销毁
  715. return element
  716. };
  717. OptionsCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  718. if (editorContext) {
  719. let $editor = $(editorContext);
  720. $editor.css("position", "fixed");
  721. $editor.css("background", "white");
  722. $editor.css("width", cellRect.width);
  723. $editor.attr("gcUIElement", "gcEditingInput");
  724. //编辑文本框距离浏览器的top
  725. let top = $('.header').height() + $('#zaojiashu').find('.toolsbar').height() + $('#top_div').height() + $('#bottom_div_ul').height() + $('#qdjlTools').height() + $('.resize-y').height();
  726. let node = bills.selected.sub.tree.items[elfSheet.getActiveRowIndex()];
  727. setOptionsDiv($editor, node, cellRect, cellStyle, top + cellRect.y);
  728. this.isEscKey = false;
  729. }
  730. }
  731. OptionsCellType.prototype.deactivateEditor = function (editorContext, context) {
  732. };
  733. OptionsCellType.prototype.setEditorValue = function (editor, value, context) {
  734. this.displayText = value;
  735. };
  736. OptionsCellType.prototype.getEditorValue = function (editor, context) {
  737. let me = this;
  738. let node = bills.selected.sub.tree.items[elfSheet.getActiveRowIndex()];
  739. if(this.isEscKey !=true){
  740. renderSheetFunc(elfSheet, function () {
  741. doAfterSel.call(me, node);
  742. });
  743. }
  744. this.isEscKey = false;
  745. return this.displayText;
  746. };
  747. OptionsCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  748. };
  749. OptionsCellType.prototype.isReservedKey = function (e, context) {
  750. //cell type handle tab key by itself
  751. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  752. return false;
  753. };
  754. OptionsCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  755. return {
  756. x: x,
  757. y: y,
  758. row: context.row,
  759. col: context.col,
  760. cellStyle: cellStyle,
  761. cellRect: cellRect,
  762. sheetArea: context.sheetArea
  763. };
  764. };
  765. return new OptionsCellType();
  766. }
  767. //原本清单存在此定额
  768. function existTheRation(nodes, rationID) {
  769. return nodes.find(node => node.data && node.data.stdID == rationID)
  770. }
  771. // 获取清单指生成的定额数据(不允许重复插入)
  772. function getInsertGuidanceRationData() {
  773. let rst = [];
  774. if(!bills.selected || !bills.selected.sub){
  775. return rst;
  776. }
  777. let tree = bills.selected.sub.tree;
  778. if(!tree){
  779. return rst;
  780. }
  781. let mainSelected = projectObj.project.mainTree.selected,
  782. mainSelRationNodes = mainSelected.children.filter(node => node.data && node.data.type === rationType.ration);
  783. // 从指引表从获取勾选的定额数据
  784. let sheet = guideItem.workBook.getSheet(0),
  785. rowCount = sheet.getRowCount();
  786. for (let row = 0; row < rowCount; row++) {
  787. let data = tree.items[row].data;
  788. // 勾选的定额,且该定额在目标清单下不存在才插入
  789. let isChecked = sheet.getValue(row, 1);
  790. if (isChecked
  791. && data.type === itemType.ration
  792. && !existTheRation(mainSelRationNodes, data.rationID)) {
  793. rst.push({
  794. itemQuery: {
  795. userID,
  796. ID: data.rationID
  797. },
  798. rationType: rationType.ration
  799. });
  800. }
  801. }
  802. return rst;
  803. }
  804. //获取清单精灵生成的定额数据(跳过重复,不允许重复插入)
  805. //@return {Array}
  806. function getInsertElfRationData() {
  807. let rst = [];
  808. if(!bills.selected || !bills.selected.sub){
  809. return rst;
  810. }
  811. let tree = bills.selected.sub.tree;
  812. if(!tree){
  813. return rst;
  814. }
  815. let mainSelected = projectObj.project.mainTree.selected,
  816. mainSelRationNodes = mainSelected.children.filter(node => node.data && node.data.type === rationType.ration);
  817. //造价书当前选中清单下的定额
  818. for(let node of tree.items){
  819. for(let perChecked of node.data.optionChecked){
  820. //选项直接是定额
  821. if(perChecked.type === itemType.ration && !existTheRation(mainSelRationNodes, perChecked.rationID)){
  822. rst.push({itemQuery: {userID: userID, ID: perChecked.rationID}, rationType: rationType.ration});
  823. }
  824. //选项下子选项是定额
  825. else {
  826. let rationOpts = getOptions(perChecked, bills.selected.sub.datas);
  827. for(let ration of rationOpts){
  828. if(ration.type === itemType.ration && !existTheRation(mainSelRationNodes, ration.rationID)){
  829. rst.push({itemQuery: {userID: userID, ID: ration.rationID}, rationType: rationType.ration});
  830. }
  831. }
  832. }
  833. }
  834. }
  835. return rst;
  836. }
  837. //获取清单精灵插入单条定额的数据
  838. //@return {Array}
  839. function getInsertElfSingleRation() {
  840. let rst = [];
  841. if (!bills.selected || !bills.selected.sub) {
  842. return rst;
  843. }
  844. let tree = bills.selected.sub.tree;
  845. if (!tree) {
  846. return rst;
  847. }
  848. let elfSelected = tree.selected;
  849. if (!elfSelected || !elfSelected.data.optionChecked || !elfSelected.data.optionChecked[0]) {
  850. return rst;
  851. }
  852. let mainSelected = projectObj.project.mainTree.selected,
  853. mainSelRationNodes = mainSelected.children.filter(node => node.data && node.data.type === rationType.ration);
  854. //选中的节点第一个选项时定额选项或第一个选项下的子选项时定额选项
  855. let firstOptionChecked = elfSelected.data.optionChecked[0];
  856. if (firstOptionChecked.type === itemType.ration && !existTheRation(mainSelRationNodes, firstOptionChecked.rationID)) {
  857. rst.push({itemQuery: {userID: userID, ID: firstOptionChecked.rationID}, rationType: rationType.ration});
  858. } else {
  859. let rationOpts = getOptions(firstOptionChecked, bills.selected.sub.datas);
  860. for(let ration of rationOpts){
  861. if(ration.type === itemType.ration && !existTheRation(mainSelRationNodes, ration.rationID)){
  862. rst.push({itemQuery: {userID: userID, ID: ration.rationID}, rationType: rationType.ration});
  863. break;
  864. }
  865. }
  866. }
  867. return rst;
  868. }
  869. //插入定额
  870. //@return {void}
  871. function insertRations(addRationDatas){
  872. if(addRationDatas.length > 0){
  873. projectObj.project.Ration.addMultiRation(addRationDatas, function () {
  874. projectObj.setActiveCell('quantity', true);
  875. });
  876. }
  877. }
  878. function handleClick(getRationFunc) {
  879. if (!projectObj.project.Ration.canAdd(projectObj.project.mainTree.selected)) {
  880. return;
  881. }
  882. let addRationDatas = getRationFunc();
  883. insertRations(addRationDatas);
  884. }
  885. //各监听事件
  886. //@return {void}
  887. function bindListener(){
  888. // 插入定额
  889. $('#guidanceInsertRation').click(function () {
  890. handleClick(getInsertGuidanceRationData);
  891. });
  892. // 应用选项
  893. $('#elfInsertRation').click(function () {
  894. handleClick(getInsertElfRationData);
  895. });
  896. // 应用单条
  897. $('#elfInsertSingle').click(function () {
  898. handleClick(getInsertElfSingleRation);
  899. });
  900. }
  901. return {
  902. switchModule,
  903. buildSheet,
  904. refreshWorkBook,
  905. billsSelSub,
  906. setColumnWidthByRate
  907. };
  908. })();