std_billsGuidance_lib.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/11
  7. * @version
  8. */
  9. const billsGuidance = (function () {
  10. const libSel = $('#stdBillsGuidanceLibSelect');
  11. //工作内容
  12. let stdBillsJobData = [];
  13. //项目特征
  14. let stdBillsFeatureData = [];
  15. const bills = {
  16. dom: $('#billsGuidance_bills'),
  17. workBook: null,
  18. cache: [],
  19. tree: null,
  20. controller: null,
  21. treeSetting: {
  22. treeCol: 0,
  23. emptyRows: 0,
  24. headRows: 1,
  25. headRowHeight: [40],
  26. defaultRowHeight: 21,
  27. cols: [{
  28. width: 200,
  29. readOnly: true,
  30. head: {
  31. titleNames: ["项目编码"],
  32. spanCols: [1],
  33. spanRows: [1],
  34. vAlign: [1],
  35. hAlign: [1],
  36. font: ["Arial"]
  37. },
  38. data: {
  39. field: "code",
  40. vAlign: 1,
  41. hAlign: 0,
  42. font: "Arial"
  43. }
  44. }, {
  45. width: 200,
  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. headers: [
  64. {name: '项目编码', dataCode: 'code', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'},
  65. {name: '项目名称', dataCode: 'name', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'}
  66. ],
  67. events: {
  68. SelectionChanging: function (sender, info) {
  69. billsInitSel(info.newSelections[0].row);
  70. },
  71. CellDoubleClick: function (sender, args) {
  72. let node = bills.tree.items[args.row];
  73. if(!node){
  74. return;
  75. }
  76. if(node.children.length === 0){
  77. //插入清单
  78. let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, node);
  79. if(insert){
  80. //插入选中的定额
  81. let addRationDatas = getInsertRationData(getCheckedRows());
  82. insertRations(addRationDatas);
  83. }
  84. }
  85. else {
  86. node.setExpanded(!node.expanded);
  87. //设置展开收起状态
  88. sessionStorage.setItem('stdBillsGuidanceExpState', bills.tree.getExpState(bills.tree.items));
  89. renderSheetFunc(args.sheet, function () {
  90. let iCount = node.posterityCount(), i, child;
  91. for (i = 0; i < iCount; i++) {
  92. child = bills.tree.items[args.row + i + 1];
  93. args.sheet.setRowVisible(args.row + i + 1, child.visible, args.sheetArea);
  94. }
  95. args.sheet.invalidateLayout();
  96. });
  97. args.sheet.repaint();
  98. }
  99. }
  100. }
  101. };
  102. //项目指引类型
  103. const itemType = {
  104. job: 0,
  105. ration: 1
  106. };
  107. const guideItem = {
  108. dom: $('#billsGuidance_items'),
  109. workBook: null,
  110. tree: null,
  111. controller: null,
  112. treeSetting: {
  113. treeCol: 0,
  114. emptyRows: 0,
  115. headRows: 1,
  116. headRowHeight: [40],
  117. defaultRowHeight: 21,
  118. cols: [{
  119. width: 300,
  120. readOnly: false,
  121. head: {
  122. titleNames: ["项目指引"],
  123. spanCols: [1],
  124. spanRows: [1],
  125. vAlign: [1],
  126. hAlign: [1],
  127. font: ["Arial"]
  128. },
  129. data: {
  130. field: "name",
  131. vAlign: 1,
  132. hAlign: 0,
  133. font: "Arial"
  134. }
  135. },
  136. {
  137. width: 50,
  138. readOnly: false,
  139. head: {
  140. titleNames: ["选择"],
  141. spanCols: [1],
  142. spanRows: [1],
  143. vAlign: [1],
  144. hAlign: [1],
  145. font: ["Arial"]
  146. },
  147. data: {
  148. field: "select",
  149. vAlign: 1,
  150. hAlign: 1,
  151. font: "Arial"
  152. }
  153. }
  154. ]
  155. },
  156. headers: [
  157. {name: '项目指引', dataCode: 'name', width: 300, vAlign: 'center', hAlign: 'left', formatter: '@'},
  158. {name: '选择', dataCode: 'name', width: 50, vAlign: 'center', hAlign: 'center', formatter: '@'},
  159. ],
  160. events: {
  161. EditStarting: function (sender, args) {
  162. if(guideItem.headers[args.col]['dataCode'] === 'name'){
  163. args.cancel = true;
  164. }
  165. },
  166. ButtonClicked: function (sender, args) {
  167. if(args.sheet.isEditing()){
  168. args.sheet.endEdit(true);
  169. }
  170. },
  171. CellDoubleClick: function (sender, args) {
  172. if(guideItem.headers[args.col]['dataCode'] === 'name'){
  173. if(!bills.tree.selected){
  174. return;
  175. }
  176. insertRations(getInsertRationData([args.row]));
  177. }
  178. }
  179. }
  180. };
  181. const options = {
  182. workBook: {
  183. tabStripVisible: false,
  184. allowContextMenu: false,
  185. allowCopyPasteExcelStyle : false,
  186. allowExtendPasteRange: false,
  187. allowUserDragDrop : false,
  188. allowUserDragFill: false,
  189. scrollbarMaxAlign : true
  190. },
  191. sheet: {
  192. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  193. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  194. }
  195. };
  196. //渲染时方法,停止渲染
  197. //@param {Object}sheet {Function}func @return {void}
  198. function renderSheetFunc(sheet, func){
  199. sheet.suspendEvent();
  200. sheet.suspendPaint();
  201. if(func){
  202. func();
  203. }
  204. sheet.resumeEvent();
  205. sheet.resumePaint();
  206. }
  207. //设置表选项
  208. //@param {Object}workBook {Object}opts @return {void}
  209. function setOptions (workBook, opts) {
  210. for(let opt in opts.workBook){
  211. workBook.options[opt] = opts.workBook[opt];
  212. }
  213. for(let opt in opts.sheet){
  214. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  215. }
  216. }
  217. //建表头
  218. //@param {Object}sheet {Array}headers @return {void}
  219. function buildHeader(sheet, headers) {
  220. let fuc = function () {
  221. sheet.setColumnCount(headers.length);
  222. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  223. for(let i = 0, len = headers.length; i < len; i++){
  224. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  225. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  226. if(headers[i].formatter){
  227. sheet.setFormatter(-1, i, headers[i].formatter);
  228. }
  229. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  230. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  231. }
  232. };
  233. renderSheetFunc(sheet, fuc);
  234. }
  235. //表监听事件
  236. //@param {Object}workBook @return {void}
  237. function bindEvent(workBook, events) {
  238. if(Object.keys(events).length === 0){
  239. return;
  240. }
  241. const Events = GC.Spread.Sheets.Events;
  242. for(let event in events){
  243. workBook.bind(Events[event], events[event]);
  244. }
  245. }
  246. //建表
  247. //@param {Object}module @return {void}
  248. function buildSheet(module) {
  249. if(!module.workBook){
  250. module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
  251. let sheet = module.workBook.getActiveSheet();
  252. if(module === bills){
  253. //默认初始可控制焦点在清单表中
  254. module.workBook.focus();
  255. sheet.options.isProtected = true;
  256. sheet.name('stdBillsGuidance_bills');
  257. }
  258. if(module === guideItem){
  259. sheet.options.isProtected = true;
  260. sheet.getRange(-1, 1, -1, 1).locked(false);
  261. sheet.getRange(-1, 0, -1, 1).locked(true);
  262. }
  263. setOptions(module.workBook, options);
  264. buildHeader(module.workBook.getActiveSheet(), module.headers);
  265. bindEvent(module.workBook, module.events);
  266. }
  267. }
  268. //清空表数据
  269. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  270. function cleanData(sheet, headers, rowCount){
  271. renderSheetFunc(sheet, function () {
  272. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  273. if (rowCount > 0) {
  274. sheet.setRowCount(rowCount);
  275. }
  276. });
  277. }
  278. //初始化各工作表
  279. //@param {Array}modules @return {void}
  280. function initWorkBooks(modules){
  281. for(let module of modules){
  282. buildSheet(module);
  283. }
  284. }
  285. //初始化并输出树
  286. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  287. function initTree(module, sheet, treeSetting, datas){
  288. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  289. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting);
  290. module.tree.loadDatas(datas);
  291. if(module === bills){
  292. initExpandStat();
  293. }
  294. module.controller.showTreeData();
  295. }
  296. //项目指引表焦点控制
  297. //@param {Number}row @return {void}
  298. function guideItemInitSel(row){
  299. let billsNode = bills.tree.selected;
  300. let node = null;
  301. if(billsNode && billsNode.guidance.tree){
  302. node = billsNode.guidance.tree.items[row];
  303. if(node){
  304. billsNode.guidance.tree.selected = node;
  305. }
  306. }
  307. }
  308. //根据项目指引的类型设置单元格类型,定额类型的项目指引为复选框
  309. //@param {Array}nodes @return {void}
  310. function setItemCellType(nodes){
  311. //设置单元格类型
  312. const base = new GC.Spread.Sheets.CellTypes.Base();
  313. const checkBox = new GC.Spread.Sheets.CellTypes.CheckBox();
  314. const sheet = guideItem.workBook.getActiveSheet();
  315. renderSheetFunc(sheet, function(){
  316. for(let node of nodes){
  317. sheet.setCellType(node.serialNo(), 1, node.data.type === itemType.ration ? checkBox : base);
  318. }
  319. });
  320. }
  321. //清单表焦点控制
  322. //@param {Number}row @return {void}
  323. function billsInitSel(row){
  324. let guideSheet = guideItem.workBook.getActiveSheet();
  325. cleanData(guideSheet, guideItem.headers, -1);
  326. let node = bills.tree.items[row];
  327. if(!node){
  328. return;
  329. }
  330. bills.tree.selected = node;
  331. if(!node.guidance.tree){
  332. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: libSel.val(), billsID: node.getID()}, function (rstData) {
  333. initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
  334. setItemCellType(node.guidance.tree.items);
  335. //项目指引初始焦点
  336. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  337. });
  338. }
  339. else{
  340. node.guidance.controller.showTreeData();
  341. setItemCellType(node.guidance.tree.items);
  342. //项目指引初始焦点
  343. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  344. }
  345. }
  346. //初始化清单的工作内容和项目特征
  347. //@param {Number}billsLibId @return {void}
  348. function initJobAndCharacter(billsLibId){
  349. CommonAjax.post('/stdBillsEditor/getJobContent', {userId: userID, billsLibId: billsLibId}, function (datas) {
  350. stdBillsJobData = datas;
  351. });
  352. CommonAjax.post('/stdBillsEditor/getItemCharacter', {userId: userID, billsLibId: billsLibId}, function (datas) {
  353. stdBillsFeatureData = datas;
  354. });
  355. }
  356. //初始化清单展开收起状态
  357. //@return {void}
  358. function initExpandStat(){
  359. //读取展开收起状态
  360. let currentExpState = sessionStorage.getItem('stdBillsGuidanceExpState');
  361. if(currentExpState){
  362. bills.tree.setExpandedByState(bills.tree.items, currentExpState);
  363. }
  364. //非叶子节点默认收起
  365. else{
  366. bills.tree.setRootExpanded(bills.tree.roots, false);
  367. }
  368. }
  369. //初始选择清单指引库
  370. //@param {Number}libID @return {void}
  371. function libInitSel(libID){
  372. //获取清单
  373. CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function(rstData){
  374. //获取清单库中的工作内容和项目特征
  375. initJobAndCharacter(rstData.guidanceLib.billsLibId);
  376. initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, rstData.bills);
  377. //每一棵项目指引树挂在清单节点上
  378. for(let node of bills.tree.items){
  379. node.guidance = {tree: null, controller: null};
  380. }
  381. //默认初始节点
  382. billsInitSel(0);
  383. });
  384. }
  385. //初始化清单指引库
  386. //@param {Array}libDats @return {void}
  387. function initLibs(libDatas){
  388. libSel.empty();
  389. let selectedLib = sessionStorage.getItem('stdBillsGuidance');
  390. for(let libData of libDatas){
  391. let opt = $('<option>').val(libData.id).text(libData.name);
  392. if(selectedLib && libData.id == selectedLib){
  393. opt.attr('selected', 'selected');
  394. }
  395. libSel.append(opt);
  396. }
  397. //初始默认选择
  398. libInitSel(libSel.select().val());
  399. }
  400. //初始化视图
  401. //@param {void} @return {void}
  402. function initViews(){
  403. let modules = [bills, guideItem];
  404. initWorkBooks(modules);
  405. }
  406. //获取选中的行
  407. //@return {Array}
  408. function getCheckedRows(){
  409. let rst = [];
  410. let itemSheet = guideItem.workBook.getActiveSheet();
  411. for(let row = 0; row < itemSheet.getRowCount(); row++){
  412. let rowV = itemSheet.getValue(row, 1);
  413. if(rowV){
  414. rst.push(row);
  415. }
  416. }
  417. return rst;
  418. }
  419. //获取选中的定额数据
  420. //@param {Array}rows @return {Array}
  421. function getInsertRationData(rows){
  422. let rst = [];
  423. for(let row of rows){
  424. let node = bills.tree.selected.guidance.tree.items[row];
  425. if(node && node.data.type === itemType.ration){
  426. rst.push({itemQuery: {userID: userID, ID: node.data.rationID}, rationType: rationType.ration});
  427. }
  428. }
  429. return rst;
  430. }
  431. //插入定额
  432. //@return {void}
  433. function insertRations(addRationDatas){
  434. if(addRationDatas.length > 0){
  435. projectObj.project.Ration.addMultiRation(addRationDatas, function () {
  436. //恢复
  437. let sheet = guideItem.workBook.getActiveSheet();
  438. renderSheetFunc(sheet, function () {
  439. for(let row = 0; row < sheet.getRowCount(); row++){
  440. if(sheet.getValue(row, 1)){
  441. sheet.setValue(row, 1, false);
  442. }
  443. }
  444. });
  445. });
  446. }
  447. }
  448. //各按钮监听事件
  449. //@return {void}
  450. function bindBtn(){
  451. //打开清单指引库
  452. $('#stdBillsGuidanceTab').click(function () {
  453. if(libSel.children().length === 0){
  454. initLibs(projectInfoObj.projectInfo.engineeringInfo.billsGuidance_lib);
  455. }
  456. });
  457. //更改清单指引库
  458. $('#stdBillsGuidanceLibSelect').change(function () {
  459. libInitSel($(this).select().val());
  460. //记住选项
  461. sessionStorage.setItem('stdBillsGuidance', $(this).select().val());
  462. //清除展开收起状态sessionStorage
  463. sessionStorage.removeItem('stdBillsGuidanceExpState');
  464. });
  465. //插入定额
  466. $('#guidanceInsertRation').click(function () {
  467. let addRationDatas = getInsertRationData(getCheckedRows());
  468. insertRations(addRationDatas);
  469. });
  470. //插入清单
  471. $('#guidanceInsertBills').click(function () {
  472. //插入清单
  473. if(!bills.tree.selected){
  474. return;
  475. }
  476. if(bills.tree.selected.children.length === 0){
  477. let insert = billsLibObj.insertBills(stdBillsJobData, stdBillsFeatureData, bills.tree.selected);
  478. if(insert){
  479. //插入选中的定额
  480. let addRationDatas = getInsertRationData(getCheckedRows());
  481. insertRations(addRationDatas);
  482. }
  483. }
  484. });
  485. }
  486. //刷新表
  487. //@return {void}
  488. function refreshWorkBook(){
  489. if(bills.workBook){
  490. bills.workBook.refresh();
  491. }
  492. if(guideItem.workBook){
  493. guideItem.workBook.refresh();
  494. }
  495. }
  496. return {initViews, bindBtn, refreshWorkBook, bills};
  497. })();
  498. $(document).ready(function(){
  499. billsGuidance.initViews();
  500. billsGuidance.bindBtn();
  501. });