billsElf.js 35 KB

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