billsGuidance.js 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/1
  7. * @version
  8. */
  9. const billsGuidance = (function () {
  10. function _isDef(v) {
  11. return typeof v !== 'undefined' && v !== null;
  12. }
  13. function sortByCode(arr) {
  14. function recurCompare(a, b, index){
  15. if (a[index] && !b[index]) {
  16. return 1;
  17. } else if (!a[index] && b[index]) {
  18. return -1;
  19. } else if (a[index] && b[index]) {
  20. let aV = a[index],
  21. bV = b[index];
  22. if (!isNaN(aV) && !isNaN(bV)) {
  23. aV = parseFloat(a[index]);
  24. bV = parseFloat(b[index]);
  25. }
  26. if (aV > bV) {
  27. return 1;
  28. } else if (aV < bV) {
  29. return -1;
  30. } else {
  31. return recurCompare(a, b, index + 1);
  32. }
  33. }
  34. return 0;
  35. }
  36. arr.sort(function (a, b) {
  37. if (!_isDef(a.code) || !_isDef(b.code)) {
  38. return 0;
  39. }
  40. let aArr = a.code.split('-'),
  41. bArr = b.code.split('-');
  42. return recurCompare(aArr, bArr, 0);
  43. });
  44. }
  45. const locked = lockUtil.getLocked();
  46. let moduleName = 'stdBillsGuidance';
  47. //上下拖动的拖动条高度
  48. const verticalResize = 10;
  49. //自执行函数全局变量定义
  50. const libID = getQueryString('libID');
  51. //总工作内容数据
  52. let stdBillsJobData = [];
  53. //总项目特征数据
  54. let stdBillsFeatureData = [];
  55. const bills = {
  56. dom: $('#billsSpread'),
  57. workBook: null,
  58. cache: [],
  59. tree: null,
  60. controller: null,
  61. treeSetting: {
  62. treeCol: 0,
  63. emptyRows: 0,
  64. headRows: 1,
  65. headRowHeight: [40],
  66. defaultRowHeight: 21,
  67. cols: [{
  68. width: 200,
  69. readOnly: true,
  70. showHint: true,
  71. head: {
  72. titleNames: ["项目编码"],
  73. spanCols: [1],
  74. spanRows: [1],
  75. vAlign: [1],
  76. hAlign: [1],
  77. font: ["Arial"]
  78. },
  79. data: {
  80. field: "code",
  81. vAlign: 1,
  82. hAlign: 0,
  83. font: "Arial"
  84. }
  85. }, {
  86. width: 200,
  87. readOnly: true,
  88. head: {
  89. titleNames: ["项目名称"],
  90. spanCols: [1],
  91. spanRows: [1],
  92. vAlign: [1],
  93. hAlign: [1],
  94. font: ["Arial"]
  95. },
  96. data: {
  97. field: "name",
  98. vAlign: 1,
  99. hAlign: 0,
  100. font: "Arial"
  101. }
  102. }]
  103. },
  104. headers: [
  105. {name: '项目编码', dataCode: 'code', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'},
  106. {name: '项目名称', dataCode: 'name', width: 200, vAlign: 'center', hAlign: 'left', formatter: '@'}
  107. ],
  108. events: {
  109. SelectionChanged: function (sender, info) {
  110. billsInitSel(info.newSelections[0].row, info.oldSelections[0]);
  111. }
  112. }
  113. };
  114. const selectedBgColor = '#DFE8F9';
  115. const searchBgColor = 'lemonChiffon';
  116. //项目指引类型
  117. const itemType = {
  118. job: 0,
  119. ration: 1
  120. };
  121. //项目指引复制整块localStorage key
  122. const itemCopyBlockKey = 'guideItemCopyBlock';
  123. const updateType = {
  124. create: 'create',
  125. update: 'update',
  126. del: 'delete'
  127. };
  128. //项目指引节点状态:展开全部、收起定额
  129. const itemExpandState = {
  130. expand: 1,
  131. contract: 0
  132. };
  133. //项目指引当前节点展开收缩状态,默认展开全部
  134. let curExpandState = 1;
  135. const guideItem = {
  136. dom: $('#guideItemSpread'),
  137. workBook: null,
  138. tree: null,
  139. controller: null,
  140. treeSetting: {
  141. treeCol: 0,
  142. emptyRows: 0,
  143. headRows: 1,
  144. headRowHeight: [40],
  145. defaultRowHeight: 21,
  146. cols: [{
  147. width: 400,
  148. readOnly: locked,
  149. head: {
  150. titleNames: ["项目指引"],
  151. spanCols: [1],
  152. spanRows: [1],
  153. vAlign: [1],
  154. hAlign: [1],
  155. font: ["Arial"]
  156. },
  157. data: {
  158. field: "name",
  159. vAlign: 1,
  160. hAlign: 0,
  161. font: "Arial"
  162. }
  163. },
  164. {
  165. width: 40,
  166. readOnly: true,
  167. head: {
  168. titleNames: ["输出特征"],
  169. spanCols: [1],
  170. spanRows: [1],
  171. vAlign: [1],
  172. hAlign: [1],
  173. font: ["Arial"]
  174. },
  175. data: {
  176. field: "outputItemCharacter",
  177. vAlign: 1,
  178. hAlign: 1,
  179. font: "Arial"
  180. }
  181. },
  182. {
  183. width: 40,
  184. readOnly: true,
  185. head: {
  186. titleNames: ["必填"],
  187. spanCols: [1],
  188. spanRows: [1],
  189. vAlign: [1],
  190. hAlign: [1],
  191. font: ["Arial"]
  192. },
  193. data: {
  194. field: "required",
  195. vAlign: 1,
  196. hAlign: 1,
  197. font: "Arial"
  198. }
  199. }
  200. ]
  201. },
  202. headers: [
  203. {name: '项目指引', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  204. {name: '输出特征', dataCode: 'outputItemCharacter', width: 40, vAlign: 'center', hAlign: 'center'},
  205. {name: '必填', dataCode: 'required', width: 40, vAlign: 'center', hAlign: 'center'},
  206. ],
  207. events: {
  208. SelectionChanged: function (sender, info) {
  209. guideItemInitSel(info.newSelections[0].row)
  210. },
  211. EditEnded: function (sender, args) {
  212. edit(args.sheet, [{row: args.row, col: args.col}]);
  213. },
  214. ButtonClicked: function (sender, args) {
  215. edit(args.sheet, [{row: args.row, col: args.col}]);
  216. },
  217. RangeChanged: function (sender, args) {
  218. edit(args.sheet, args.changedCells);
  219. },
  220. CellDoubleClick: function(sender, args) {
  221. locateAtRation(args.row);
  222. }
  223. }
  224. };
  225. //定额章节树
  226. const section = {
  227. dom: $('#sectionSpread'),
  228. workBook: null,
  229. cache: [],
  230. tree: null,
  231. controller: null,
  232. treeSetting: {
  233. treeCol: 0,
  234. emptyRows: 0,
  235. headRows: 1,
  236. headRowHeight: [40],
  237. defaultRowHeight: 21,
  238. cols: [{
  239. width: 400,
  240. readOnly: true,
  241. head: {
  242. titleNames: ["名称"],
  243. spanCols: [1],
  244. spanRows: [1],
  245. vAlign: [1],
  246. hAlign: [1],
  247. font: ["Arial"]
  248. },
  249. data: {
  250. field: "name",
  251. vAlign: 1,
  252. hAlign: 0,
  253. font: "Arial"
  254. }
  255. }]
  256. },
  257. headers: [
  258. {name: '名称', dataCode: 'name', width: 400, vAlign: 'center', hAlign: 'left', formatter: '@'},
  259. ],
  260. events: {
  261. SelectionChanged: function (sender, info) {
  262. sectionInitSel(info.newSelections[0].row)
  263. }
  264. }
  265. };
  266. const ration = {
  267. dom: $('#rationSpread'),
  268. workBook: null,
  269. datas: [],//所有的数据,搜索定额时,从所有数据中筛选
  270. cache: [],//显示在表格上的数据,添加定额可以有效根据行识别定额
  271. headers: [
  272. {name: '选择', dataCode: 'select', width: 50, vAlign: 'center', hAlign: 'center'},
  273. {name: '编码', dataCode: 'code', width: 110, vAlign: 'center', hAlign: 'left', formatter: '@'},
  274. {name: '名称', dataCode: 'name', width: 250, vAlign: 'center', hAlign: 'left', formatter: '@'},
  275. {name: '单位', dataCode: 'unit', width: 100, vAlign: 'center', hAlign: 'left', formatter: '@'}
  276. ],
  277. events: {
  278. ButtonClicked: function (sender, args) {
  279. if(args.sheet.isEditing()){
  280. args.sheet.endEdit(true);
  281. }
  282. },
  283. CellDoubleClick: function (sender, args) {
  284. if(ration.headers[args.col].dataCode === 'name'){
  285. let insertDatas = getInsertRations([args.row]);
  286. if(insertDatas.length > 0){
  287. insert(insertDatas, false);
  288. }
  289. }
  290. }
  291. }
  292. };
  293. const options = {
  294. workBook: {
  295. tabStripVisible: false,
  296. allowContextMenu: false,
  297. allowCopyPasteExcelStyle : false,
  298. allowExtendPasteRange: false,
  299. allowUserDragDrop : false,
  300. allowUserDragFill: false,
  301. scrollbarMaxAlign : true
  302. },
  303. sheet: {
  304. protectionOptions: {allowResizeRows: true, allowResizeColumns: true},
  305. clipBoardOptions: GC.Spread.Sheets.ClipboardPasteOptions.values
  306. }
  307. };
  308. //渲染时方法,停止渲染
  309. //@param {Object}sheet {Function}func @return {void}
  310. function renderSheetFunc(sheet, func){
  311. sheet.suspendEvent();
  312. sheet.suspendPaint();
  313. if(func){
  314. func();
  315. }
  316. sheet.resumeEvent();
  317. sheet.resumePaint();
  318. }
  319. //设置表选项
  320. //@param {Object}workBook {Object}opts @return {void}
  321. function setOptions (workBook, opts) {
  322. for(let opt in opts.workBook){
  323. workBook.options[opt] = opts.workBook[opt];
  324. }
  325. for(let opt in opts.sheet){
  326. workBook.getActiveSheet().options[opt] = opts.sheet[opt];
  327. }
  328. }
  329. //建表头
  330. //@param {Object}sheet {Array}headers @return {void}
  331. function buildHeader(sheet, headers) {
  332. let fuc = function () {
  333. sheet.setColumnCount(headers.length);
  334. sheet.setRowHeight(0, 40, GC.Spread.Sheets.SheetArea.colHeader);
  335. for(let i = 0, len = headers.length; i < len; i++){
  336. sheet.setValue(0, i, headers[i].name, GC.Spread.Sheets.SheetArea.colHeader);
  337. sheet.setColumnWidth(i, headers[i].width, GC.Spread.Sheets.SheetArea.colHeader);
  338. sheet.getCell(0, i, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  339. if(headers[i].formatter){
  340. sheet.setFormatter(-1, i, headers[i].formatter);
  341. }
  342. sheet.getRange(-1, i, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[headers[i]['hAlign']]);
  343. sheet.getRange(-1, i, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[headers[i]['vAlign']]);
  344. }
  345. };
  346. renderSheetFunc(sheet, fuc);
  347. }
  348. //表监听事件
  349. //@param {Object}workBook @return {void}
  350. function bindEvent(workBook, events) {
  351. if(Object.keys(events).length === 0){
  352. return;
  353. }
  354. const Events = GC.Spread.Sheets.Events;
  355. for(let event in events){
  356. workBook.bind(Events[event], events[event]);
  357. }
  358. }
  359. //建表
  360. //@param {Object}module @return {void}
  361. function buildSheet(module) {
  362. if(!module.workBook){
  363. module.workBook = new GC.Spread.Sheets.Workbook(module.dom[0], {sheetCount: 1});
  364. let sheet = module.workBook.getActiveSheet();
  365. if(module === bills){
  366. sheet.name('stdBillsGuidance_bills');
  367. //默认初始可控制焦点在清单表中
  368. module.workBook.focus();
  369. sheet.options.isProtected = true;
  370. }
  371. else if(module === ration){
  372. sheet.options.isProtected = true;
  373. sheet.getRange(-1, 0, -1, 1).locked(locked);
  374. sheet.getRange(-1, 1, -1, -1).locked(true);
  375. }
  376. else if(module === guideItem){
  377. sheetCommonObj.bindEscKey(module.workBook, [{sheet: sheet, editStarting: null, editEnded: module.events.EditEnded}]);
  378. }
  379. setOptions(module.workBook, options);
  380. buildHeader(module.workBook.getActiveSheet(), module.headers);
  381. bindEvent(module.workBook, module.events);
  382. }
  383. lockUtil.lockSpreads([module.workBook], locked);
  384. if (locked) {
  385. // 锁定表格后双击事件失效了,但是需要双击定位,因此重新绑定双击
  386. if (module === guideItem) {
  387. module.workBook.bind('CellDoubleClick', module.events.CellDoubleClick);
  388. }
  389. }
  390. }
  391. //清空表数据
  392. //@param {Object}sheet {Array}headers {Number}rowCount @return {void}
  393. function cleanData(sheet, headers, rowCount){
  394. renderSheetFunc(sheet, function () {
  395. sheet.clear(-1, 0, -1, headers.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  396. if (rowCount >= 0) {
  397. sheet.setRowCount(rowCount);
  398. }
  399. });
  400. }
  401. //根据清单获取项目指引
  402. //@param {String}guidanceLibID {Number}billsID {Function}callback @return {void}
  403. function getItemsByBills(guidanceLibID, billsID, callback){
  404. CommonAjax.post('/billsGuidance/api/getItemsByBills', {guidanceLibID: guidanceLibID, billsID: billsID}, function (rstData) {
  405. if(callback){
  406. callback(rstData);
  407. }
  408. });
  409. }
  410. //清单表焦点控制
  411. //@param {Number}row @return {void}
  412. function billsInitSel(row, oldSel){
  413. let guideSheet = guideItem.workBook.getActiveSheet();
  414. cleanData(guideSheet, guideItem.headers, -1);
  415. let node = bills.tree.items[row];
  416. if(!node){
  417. return;
  418. }
  419. const billSheet = bills.workBook.getActiveSheet();
  420. setBgColor(billSheet, row, selectedBgColor);
  421. if (oldSel && row !== oldSel.row) {
  422. const orgNode = bills.tree.items[oldSel.row]
  423. setBgColor(billSheet, oldSel.row, orgNode && orgNode.isSearch ? searchBgColor : 'white');
  424. }
  425. bills.tree.selected = node;
  426. //显示备注
  427. $('.main-side-bottom').find('textarea').val(node.data.comment ? node.data.comment : '');
  428. if(!node.guidance.tree){
  429. getItemsByBills(libID, node.data.ID, function (rstData) {
  430. initTree(node.guidance, guideSheet, guideItem.treeSetting, rstData);
  431. setNodesExpandState(node.guidance.tree.items, curExpandState);
  432. showCheckBox(guideSheet, node.guidance.tree.items);
  433. renderSheetFunc(guideSheet, function () {
  434. TREE_SHEET_HELPER.refreshNodesVisible(node.guidance.tree.roots, guideSheet, true);
  435. });
  436. //设置底色
  437. setNodesColor(guideSheet, node.guidance.tree.items);
  438. //项目指引初始焦点
  439. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  440. });
  441. } else{
  442. setNodesExpandState(node.guidance.tree.items, curExpandState);
  443. node.guidance.controller.showTreeData();
  444. showCheckBox(guideSheet, node.guidance.tree.items);
  445. //设置底色
  446. setNodesColor(guideSheet, node.guidance.tree.items);
  447. //项目指引初始焦点
  448. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  449. }
  450. }
  451. function showCheckBox(sheet, nodes) {
  452. // const checkBoxType = locked ? sheetCommonObj.getReadOnlyCheckBox() : sheetCommonObj.getCheckBox();
  453. const checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  454. const baseType = new GC.Spread.Sheets.CellTypes.Base();
  455. renderSheetFunc(sheet, function () {
  456. nodes.forEach(node => {
  457. const row = node.serialNo();
  458. if (node.depth() % 2 === 0 && _isDef(node.data.type) && node.data.type === itemType.job) {
  459. sheet.setCellType(row, 1, checkBoxType);
  460. sheet.setCellType(row, 2, checkBoxType);
  461. sheet.setValue(row, 1, node.data.outputItemCharacter || false);
  462. sheet.setValue(row, 2, node.data.required || false);
  463. } else {
  464. sheet.setCellType(row, 1, baseType);
  465. sheet.setCellType(row, 2, baseType);
  466. sheet.setValue(row, 1, '');
  467. sheet.setValue(row, 2, '');
  468. }
  469. })
  470. });
  471. }
  472. //设置项目节点展开收起状态:展开全部、收起定额
  473. //@param {Array}nodes(当前清单下的所有项目指引节点) {Number}expandState(展开全部1或收起定额0).
  474. function setNodesExpandState(nodes, expandState) {
  475. if(expandState === itemExpandState.contract) {
  476. //找出所有定额的父节点
  477. let rations = _.filter(nodes, function (node) {
  478. return node.data.type === itemType.ration;
  479. });
  480. let rationParentIDs = [];
  481. for(let ration of rations){
  482. if(ration.data.ParentID != -1){
  483. rationParentIDs.push(ration.data.ParentID);
  484. }
  485. }
  486. rationParentIDs = Array.from(new Set(rationParentIDs));
  487. let rationParentNodes = _.filter(nodes, function (node) {
  488. return rationParentIDs.includes(node.data.ID);
  489. });
  490. //收起定额
  491. for(let node of rationParentNodes){
  492. node.setExpanded(false);
  493. }
  494. } else {
  495. for(let node of nodes){
  496. node.setExpanded(true);
  497. }
  498. }
  499. }
  500. // 设置行底色
  501. function setBgColor(sheet, row, color) {
  502. const style = new GC.Spread.Sheets.Style();
  503. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  504. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  505. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  506. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  507. style.backColor = color;
  508. sheet.setStyle(row, -1, style);
  509. }
  510. //根据奇偶层级设置节点底色,奇数层为蓝色(树节点深度为偶数)
  511. //@param {Object}sheet {Array}nodes @return {void}
  512. function setNodesColor(sheet, nodes) {
  513. renderSheetFunc(sheet, function () {
  514. for(let node of nodes){
  515. const nDepth = node.depth();
  516. const color = nDepth % 2 == 0 && _isDef(node.data.type) && node.data.type === itemType.job ? selectedBgColor : 'White';
  517. setBgColor(sheet, node.serialNo(), color);
  518. }
  519. });
  520. }
  521. //选中的节点是否全是同层节点
  522. //@param {Object}sheet {Array}items @return {Boolean}
  523. function itemsSameDepth(sheet, items) {
  524. let sels = sheet.getSelections();
  525. if(sels.length === 0 || items.length === 0){
  526. return false;
  527. }
  528. let depths = [];
  529. for(let i = 0; i < sels[0].rowCount; i++){
  530. let row = sels[0].row + i;
  531. let node = items[row];
  532. if(node){
  533. depths.push(node.depth());
  534. }
  535. }
  536. }
  537. //节点子项是否全是工作内容
  538. //@param {Object}node @return {Boolean}
  539. function allJobChildren(node){
  540. for(let c of node.children){
  541. if(c.data.type === itemType.ration){
  542. return false;
  543. }
  544. }
  545. return true;
  546. }
  547. //节点子项是否全是定额
  548. //@param {Object}node @return {Boolean}
  549. function allRationChildren(node){
  550. for(let c of node.children){
  551. if(c.data.type === itemType.job){
  552. return false;
  553. }
  554. }
  555. return true;
  556. }
  557. //刷新按钮有效性
  558. //@param {Object}node @return {void}
  559. function refreshBtn(node){
  560. if (locked) {
  561. return;
  562. }
  563. //全部设为无效
  564. $('.tools-btn').children().addClass('disabled');
  565. $('#insertRation').addClass('disabled');
  566. $('#insertAll').addClass('disabled');
  567. $('.main-bottom-content').find('textarea').attr('readonly', true);
  568. //插入
  569. if(bills.tree.selected && bills.tree.selected.guidance.tree){
  570. $('#insert').removeClass('disabled');
  571. if(node && node.data.type === itemType.ration){
  572. $('#insert').addClass('disabled');
  573. }
  574. }
  575. //删除
  576. if(node){
  577. $('#del').removeClass('disabled');
  578. }
  579. if(node && node.data.type === itemType.job){
  580. //升级
  581. if(node.parent){
  582. $('#upLevel').removeClass('disabled');
  583. if(node.nextSibling && node.children.length > 0 && !allJobChildren(node)){
  584. $('#upLevel').addClass('disabled');
  585. }
  586. }
  587. //降级
  588. if(node.preSibling){
  589. $('#downLevel').removeClass('disabled');
  590. if(node.preSibling.children.length > 0 && !allJobChildren(node.preSibling)){
  591. $('#downLevel').addClass('disabled');
  592. }
  593. }
  594. }
  595. //上移
  596. if(node && node.preSibling){
  597. $('#upMove').removeClass('disabled')
  598. }
  599. //下移
  600. if(node && node.nextSibling){
  601. $('#downMove').removeClass('disabled');
  602. }
  603. //收起定额、展开全部
  604. $('#expandContract').removeClass('disabled');
  605. //插入定额
  606. if(node && (node.children.length === 0 || allRationChildren(node))){
  607. $('#insertRation').removeClass('disabled');
  608. $('#insertAll').removeClass('disabled');
  609. }
  610. //备注,奇数节点可用
  611. if(node && (node.depth() + 1) % 2 === 1 && node.data.type !== itemType.ration){
  612. $('.main-bottom-content').find('textarea').attr('readonly', false);
  613. }
  614. }
  615. //项目指引表焦点控制
  616. //@param {Number}row @return {void}
  617. function guideItemInitSel(row){
  618. let billsNode = bills.tree.selected;
  619. let node = null;
  620. if(billsNode && billsNode.guidance.tree){
  621. node = billsNode.guidance.tree.items[row];
  622. if(node){
  623. billsNode.guidance.tree.selected = node;
  624. //显示备注
  625. $('.main-bottom-content').find('textarea').val(node.data.comment ? node.data.comment : '');
  626. }
  627. }
  628. refreshBtn(node);
  629. }
  630. //初始化当前库名
  631. //@param {String} @return {void}
  632. function initLibName(libName) {
  633. $('#libName')[0].outerHTML = $('#libName')[0].outerHTML.replace("XXX清单指引", libName);
  634. }
  635. //初始化各工作表
  636. //@param {Array}modules @return {void}
  637. function initWorkBooks(modules){
  638. for(let module of modules){
  639. buildSheet(module);
  640. }
  641. }
  642. function tipDivCheck(){
  643. setTimeout(function () {
  644. let tips = $('#autoTip');
  645. if(ration.tipDiv == 'show'){
  646. return;
  647. } else if(ration.tipDiv == 'hide'&&tips){
  648. tips.hide();
  649. ration._toolTipElement = null;
  650. }
  651. },600)
  652. }
  653. //获取悬浮提示单元格
  654. //@param {Object}sheet @return {Object}
  655. function getTipCellType(sheet) {
  656. let setting = {};
  657. let TipCellType = function () {};
  658. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  659. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  660. return {
  661. x: x,
  662. y: y,
  663. row: context.row,
  664. col: context.col,
  665. cellStyle: cellStyle,
  666. cellRect: cellRect,
  667. sheet: context.sheet,
  668. sheetArea: context.sheetArea
  669. };
  670. };
  671. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  672. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  673. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  674. if(tag !== undefined && tag){
  675. text = tag;
  676. }
  677. if(sheet && sheet.getParent().qo){
  678. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  679. }
  680. let delayTimes = 500; //延时时间
  681. let now_timeStamp = +new Date();
  682. this.tipTimeStamp = now_timeStamp;
  683. let me = this;
  684. setTimeout(function () {
  685. if(now_timeStamp - me.tipTimeStamp == 0){//鼠标停下的时候才显示
  686. if (setting.pos && text && text !== '') {
  687. //固定不显示的div,存储文本获取固定div宽度,toolTipElement由于显示和隐藏,获取宽度不正确
  688. if(!me._fixedTipElement){
  689. let div = $('#fixedTip1')[0];
  690. if (!div) {
  691. div = document.createElement("div");
  692. $(div).css("padding", 5)
  693. .attr("id", 'fixedTip');
  694. $(div).hide();
  695. document.body.insertBefore(div, null);
  696. }
  697. me._fixedTipElement = div;
  698. }
  699. $(me._fixedTipElement).width('');
  700. $(me._fixedTipElement).html(text);
  701. if (!me._toolTipElement) {
  702. let div = $('#autoTip1')[0];
  703. if (!div) {
  704. div = document.createElement("div");
  705. $(div).css("position", "absolute")
  706. .css("border", "1px #C0C0C0 solid")
  707. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  708. .css("font", "0.9rem Calibri")
  709. .css("background", "white")
  710. .css("padding", 5)
  711. .attr("id", 'autoTip1');
  712. $(div).hide();
  713. document.body.insertBefore(div, null);
  714. }
  715. me._toolTipElement = div;
  716. $(me._toolTipElement).width('');
  717. //实时读取位置信息
  718. if(hitinfo.sheet && hitinfo.sheet.getParent().qo){
  719. setting.pos = SheetDataHelper.getObjPos(hitinfo.sheet.getParent().qo);
  720. }
  721. $(me._toolTipElement).html(text);
  722. //定额库定额特殊处理
  723. if($(hitinfo.sheet.getParent().qo).attr('id') === 'rationSpread'){
  724. let divWidth = $(me._fixedTipElement).width(),
  725. divHeight = $(me._fixedTipElement).height();
  726. if(divWidth > 600){
  727. divWidth = 590;
  728. $(me._toolTipElement).width(divWidth);
  729. }
  730. let top = setting.pos.y + hitinfo.y - divHeight < 0 ? 0 : setting.pos.y + hitinfo.cellRect.y - divHeight;
  731. $(me._toolTipElement).css("top", top).css("left", setting.pos.x - divWidth);
  732. }
  733. else{
  734. $(me._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  735. }
  736. //名称
  737. if(hitinfo.col === 2){
  738. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  739. zoom = hitinfo.sheet.zoom();
  740. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  741. let textLength = me.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  742. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  743. if(textLength > cellWidth){
  744. $(me._toolTipElement).css("top", setting.pos.y + hitinfo.y +15).css("left", setting.pos.x + hitinfo.x + 15);
  745. $(me._toolTipElement).show("fast");
  746. ration.tipDiv = 'show';//做个标记
  747. }
  748. }
  749. else {
  750. $(me._toolTipElement).show("fast");
  751. ration.tipDiv = 'show';//做个标记
  752. }
  753. }
  754. }
  755. }
  756. },delayTimes);
  757. };
  758. TipCellType.prototype.processMouseLeave = function (hininfo) {
  759. this.tipTimeStamp = +new Date();
  760. ration.tipDiv = 'hide';
  761. if (this._toolTipElement) {
  762. $(this._toolTipElement).hide();
  763. this._toolTipElement = null;
  764. }
  765. tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  766. }
  767. return new TipCellType();
  768. }
  769. //输出表数据(定额表)
  770. //@param {Object}sheet {Array}headers {Array}datas @return {void}
  771. function showData(sheet, headers, datas){
  772. let fuc = function () {
  773. sheet.setRowCount(datas.length);
  774. //复选框
  775. let checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
  776. let tipCellType = getTipCellType(sheet);
  777. sheet.setCellType(-1, 0, checkBoxType);
  778. for(let col = 0, cLen = headers.length; col < cLen; col++){
  779. for(let row = 0, rLen = datas.length; row < rLen; row++){
  780. sheet.setValue(row, col, datas[row][headers[col]['dataCode']]);
  781. if(col === 1){
  782. sheet.setTag(row, col, datas[row]['hint']);
  783. }
  784. }
  785. }
  786. sheet.setCellType(-1, 1, tipCellType);
  787. sheet.setCellType(-1, 2, tipCellType);
  788. };
  789. renderSheetFunc(sheet, fuc);
  790. }
  791. //根据定额章节树ID获取定额(从数据缓存中获取,定额数据一开始一次性拉取)
  792. //@param {Number}sectionId {Array}rations @return {Array}
  793. function getRationsBySectionId(sectionId, rations) {
  794. if(!sectionId || !rations){
  795. return [];
  796. }
  797. return _.filter(rations, {sectionId});
  798. }
  799. //定额章节树焦点控制
  800. //@param {Number}row @return {void}
  801. function sectionInitSel(row) {
  802. let rationSheet = ration.workBook.getActiveSheet();
  803. let sectionNode = section.tree ? section.tree.items[row] : null;
  804. if(sectionNode && sectionNode.children.length === 0){
  805. let sectionRations = getRationsBySectionId(sectionNode.data.ID, ration.datas);
  806. ration.cache = sectionRations;
  807. showData(rationSheet, ration.headers, sectionRations);
  808. }
  809. else {
  810. cleanData(rationSheet, ration.headers, 0);
  811. }
  812. }
  813. //初始化定额条目
  814. //@param {Number}rationLibId @return {void}
  815. function initRationItems(rationLibId, successCb){
  816. $.bootstrapLoading.start();
  817. //获取定额章节树
  818. let sectionSheet = section.workBook.getActiveSheet();
  819. CommonAjax.post('/rationRepository/api/getRationTree', {rationLibId: rationLibId}, function (sectionDatas) {
  820. //获取所有定额数据
  821. let reqEntity = {rationLibId: rationLibId, showHint: true, returnFields: '-_id code ID sectionId name unit basePrice rationGljList jobContent annotation'};
  822. CommonAjax.post('/rationRepository/api/getRationItemsByLib', reqEntity, function (rstData) {
  823. section.cache = sectionDatas;
  824. initTree(section, section.workBook.getActiveSheet(), section.treeSetting, sectionDatas);
  825. //初始焦点在第一行(切换库)
  826. sectionSheet.setActiveCell(0, 0);
  827. sortByCode(rstData);
  828. /* rstData.sort(function (a, b) {
  829. let rst = 0;
  830. if(a.code > b.code){
  831. rst = 1;
  832. }
  833. else if(a.code < b.code){
  834. rst = -1;
  835. }
  836. return rst;
  837. }); */
  838. ration.datas = rstData;
  839. sectionInitSel(0);
  840. $.bootstrapLoading.end();
  841. if (successCb) {
  842. successCb();
  843. }
  844. }, function () {
  845. $.bootstrapLoading.end();
  846. });
  847. }, function () {
  848. $.bootstrapLoading.end();
  849. });
  850. }
  851. // 可用的定额库ID
  852. const rationLibIDs = [];
  853. //初始化定额库选择
  854. //@param {String}compilationId @return {void}
  855. function initRationLibs(compilationId){
  856. CommonAjax.post('/rationRepository/api/getRationLibsByCompilation', {compilationId: compilationId}, function (rstData) {
  857. $('#rationLibSel').empty();
  858. for(let rationLib of rstData){
  859. rationLibIDs.push(+rationLib.ID);
  860. let opt = `<option value="${rationLib.ID}">${rationLib.dispName}</option>`;
  861. $('#rationLibSel').append(opt);
  862. }
  863. //初始选择
  864. initRationItems(parseInt($('#rationLibSel').select().val()));
  865. $('#rationLibSel').change(function () {
  866. let rationLibId = parseInt($(this).select().val());
  867. initRationItems(rationLibId);
  868. })
  869. });
  870. }
  871. // 变更定额库
  872. function changeRationLib(libID, successCb) {
  873. $('#rationLibSel').val(libID);
  874. initRationItems(libID, successCb);
  875. }
  876. // 设置清单名称文本色
  877. function setBillsForeColor(billsNodes) {
  878. const sheet = bills.workBook.getActiveSheet();
  879. renderSheetFunc(sheet, function () {
  880. for(let bills of billsNodes){
  881. const row = bills.serialNo();
  882. sheet.setStyle(row, 1, { foreColor: bills.data.hasGuide ? 'darkgreen' : 'black' });
  883. }
  884. });
  885. }
  886. //清单设置悬浮提示信息
  887. //@param {Array}billsNodes(清单节点) {Array}jobs(总的工作内容数据) {Array}items(总的项目特征数据)
  888. function setBillsHint(billsNodes, jobs, items) {
  889. let jobsMapping = {},
  890. itemsMapping = {};
  891. for(let job of jobs){
  892. jobsMapping[job.id] = job;
  893. }
  894. for(let item of items){
  895. itemsMapping[item.id] = item;
  896. }
  897. let tagInfo = [];
  898. for(let billsNode of billsNodes){
  899. let hintArr = [];
  900. let billsItems = billsNode.data.items;
  901. if(billsItems.length > 0){
  902. //项目特征
  903. hintArr.push('项目特征:');
  904. }
  905. let itemCount = 1,
  906. jobCount = 1;
  907. for(let billsItem of billsItems){
  908. let itemData = itemsMapping[billsItem.id];
  909. if(itemData){
  910. //特征值
  911. let eigens = [];
  912. for(let eigen of itemData.itemValue){
  913. eigens.push(eigen.value);
  914. }
  915. eigens = eigens.join(';');
  916. hintArr.push(`${itemCount}.${itemData.content}${eigens === '' ? '' : ': ' + eigens}`);
  917. itemCount ++;
  918. }
  919. }
  920. //工作内容
  921. let billsJobs = billsNode.data.jobs;
  922. if(billsJobs.length > 0){
  923. hintArr.push('工作内容:');
  924. }
  925. for(let billsJob of billsJobs){
  926. let jobData = jobsMapping[billsJob.id];
  927. if(jobData){
  928. hintArr.push(`${jobCount}.${jobData.content}`);
  929. jobCount ++;
  930. }
  931. }
  932. /*if(billsNode.data.ruleText && billsNode.data.ruleText !== ''){
  933. hintArr.push('工程量计算规则:');
  934. hintArr.push(billsNode.data.ruleText);
  935. }
  936. if(billsNode.data.recharge && billsNode.data.recharge !== ''){
  937. hintArr.push('补注:');
  938. hintArr.push(billsNode.data.recharge);
  939. }*/
  940. if(hintArr.length > 0){
  941. tagInfo.push({row: billsNode.serialNo(), value: hintArr.join('\n')});
  942. }
  943. }
  944. let sheet = bills.workBook.getActiveSheet();
  945. renderSheetFunc(sheet, function () {
  946. for(let tagI of tagInfo){
  947. sheet.setTag(tagI.row, 0, tagI.value);
  948. }
  949. });
  950. }
  951. //初始化清单的工作内容和项目特征
  952. //@param {Number}billsLibId {Function}callback @return {void}
  953. function initJobAndCharacter(billsLibId, callback){
  954. CommonAjax.post('/stdBillsEditor/getJobContent', {billsLibId: billsLibId}, function (datas) {
  955. stdBillsJobData = datas;
  956. CommonAjax.post('/stdBillsEditor/getItemCharacter', {billsLibId: billsLibId}, function (datas) {
  957. stdBillsFeatureData = datas;
  958. if(callback){
  959. callback();
  960. }
  961. });
  962. });
  963. }
  964. let billsLibId = 0;
  965. //获取指引库信息及关联的清单
  966. //@param {Number}libID {Function}callback @return {Object}
  967. function getLibWithBills(libID, callback){
  968. CommonAjax.post('/billsGuidance/api/getLibWithBills', {libID: libID}, function (rstData) {
  969. billsLibId = rstData.guidanceLib.billsLibId;
  970. initRationLibs(rstData.guidanceLib.compilationId);
  971. bills.cache = rstData.bills;
  972. initLibName(rstData.guidanceLib.name);
  973. /*initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, bills.cache);
  974. //每一棵项目指引树挂在清单节点上
  975. for(let node of bills.tree.items){
  976. node.guidance = {tree: null, controller: null};
  977. }
  978. //默认初始节点
  979. billsInitSel(0);
  980. if(callback){
  981. callback(rstData);
  982. }*/
  983. let initDataCallback = function () {
  984. initTree(bills, bills.workBook.getActiveSheet(), bills.treeSetting, bills.cache);
  985. //每一棵项目指引树挂在清单节点上
  986. for(let node of bills.tree.items){
  987. node.guidance = {tree: null, controller: null};
  988. }
  989. //默认初始节点
  990. billsInitSel(0);
  991. if(callback){
  992. callback(rstData);
  993. }
  994. };
  995. initJobAndCharacter(rstData.guidanceLib.billsLibId, initDataCallback);
  996. }, function (msg) {
  997. window.location.href = '/billsGuidance/main';
  998. });
  999. }
  1000. //初始化并输出树
  1001. //@param {Object}module {Object}sheet {Object}treeSetting {Array}datas
  1002. function initTree(module, sheet, treeSetting, datas){
  1003. module.tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
  1004. module.controller = TREE_SHEET_CONTROLLER.createNew(module.tree, sheet, treeSetting);
  1005. module.tree.loadDatas(datas);
  1006. if(module === bills){
  1007. module.tree.roots.forEach(root => {
  1008. root.setExpanded(false);
  1009. })
  1010. }
  1011. module.controller.showTreeData();
  1012. if(module === bills){
  1013. setBillsHint(bills.tree.items, stdBillsJobData, stdBillsFeatureData);
  1014. setBillsForeColor(bills.tree.items);
  1015. }
  1016. }
  1017. //更新清单备注
  1018. function updateBillsComment(updateData, callback, errCB) {
  1019. CommonAjax.post('/stdBillsEditor/updateBills', updateData, function () {
  1020. if (callback) {
  1021. callback();
  1022. }
  1023. }, function () {
  1024. if(errCB) {
  1025. errCB();
  1026. }
  1027. });
  1028. }
  1029. //更新项目指引
  1030. //@param {Array}updateDatas {Function}callback @return {void}
  1031. function updateGuideItems(updateDatas, callback, errCB){
  1032. CommonAjax.post('/billsGuidance/api/updateItems', {updateDatas: updateDatas}, function (rstData) {
  1033. if(callback){
  1034. callback(rstData);
  1035. }
  1036. }, function () {
  1037. if(errCB) {
  1038. errCB();
  1039. }
  1040. $.bootstrapLoading.end();
  1041. });
  1042. }
  1043. //编辑后自动去除换行符回车符
  1044. const deESC = /[\n]/g;
  1045. //项目指引编辑
  1046. //@param {Object}sheet {Array}cells
  1047. function edit(sheet, cells){
  1048. let updateDatas = [];
  1049. //同步节点数据
  1050. let syncDatas = [];
  1051. for(let cell of cells){
  1052. const field = guideItem.headers[cell.col].dataCode;
  1053. let node = bills.tree.selected.guidance.tree.items[cell.row];
  1054. if (field === 'name') {
  1055. let text = sheet.getValue(cell.row, cell.col);
  1056. text = text ? text.toString() : '';
  1057. text = text.replace(deESC, '');
  1058. sheet.setValue(cell.row, cell.col, text);
  1059. if(node.data.name != text){
  1060. syncDatas.push({node: node, text: text, field, cell});
  1061. updateDatas.push({updateType: updateType.update, findData: {ID: node.getID()}, updateData: {name: text}});
  1062. }
  1063. } else if (field === 'outputItemCharacter' || field === 'required') {
  1064. const val = !sheet.getValue(cell.row, cell.col);
  1065. sheet.setValue(cell.row, cell.col, val);
  1066. syncDatas.push({node: node, text: val, field, cell });
  1067. updateDatas.push({updateType: updateType.update, findData: {ID: node.getID()}, updateData: {[field]: val}});
  1068. }
  1069. }
  1070. if(updateDatas.length > 0){
  1071. updateGuideItems(updateDatas, function () {
  1072. for(let syncData of syncDatas){
  1073. syncData.node.data[syncData.field] = syncData.text;
  1074. }
  1075. }, function () {
  1076. //失败恢复
  1077. renderSheetFunc(sheet, function () {
  1078. for(let syncData of syncDatas){
  1079. sheet.setValue(syncData.node.serialNo(), syncData.cell.col, syncData.node.data[syncData.field] ? syncData.node.data[syncData.field] : '');
  1080. }
  1081. });
  1082. });
  1083. }
  1084. }
  1085. //项目指引插入,支持一次插入多条数据
  1086. //@param {Array}datas {Boolean}tobeChild(插入成为子项) {Function}callback @return {void}
  1087. function insert(datas, tobeChild, callback = null){
  1088. $.bootstrapLoading.start();
  1089. let sheet = guideItem.workBook.getActiveSheet();
  1090. let controller = bills.tree.selected.guidance.controller;
  1091. let selected = bills.tree.selected.guidance.tree.selected;
  1092. let updateDatas = [];
  1093. //建立数组下标索引
  1094. let newDataIndex = {};
  1095. for(let i = 0; i < datas.length; i++){
  1096. let newNodeData = {
  1097. libID: libID, ID: uuid.v1(), ParentID: selected ? selected.getParentID() : -1, NextSiblingID: selected ? selected.getNextSiblingID() : -1,
  1098. billsID: bills.tree.selected.getID()
  1099. };
  1100. //定额类型插入当前工作内容焦点行,
  1101. if(selected && ((selected.data.type === itemType.job && datas[i].type === itemType.ration) || tobeChild)){
  1102. newNodeData.ParentID = selected.getID();
  1103. newNodeData.NextSiblingID = -1;
  1104. }
  1105. Object.assign(newNodeData, datas[i]);
  1106. newDataIndex[i] = newNodeData;
  1107. }
  1108. for(let i = 0; i < datas.length; i++){
  1109. //第一个节点
  1110. if(i === 0){
  1111. //非插入成子节点,更新选中节点NestSiblingID
  1112. if(selected && !((selected.data.type === itemType.job && datas[i].type === itemType.ration) || tobeChild)){
  1113. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: newDataIndex[i].ID}});
  1114. }
  1115. }
  1116. //非最后一个节点
  1117. if(i !== datas.length - 1){
  1118. newDataIndex[i].NextSiblingID = newDataIndex[i + 1].ID;
  1119. }
  1120. updateDatas.push({updateType: updateType.create, updateData: newDataIndex[i]});
  1121. }
  1122. updateGuideItems(updateDatas, function () {
  1123. for(let updateData of updateDatas){
  1124. if(updateData.updateType === updateType.create){
  1125. let newNode = controller.insertByIDS(updateData.updateData.ID, updateData.updateData.ParentID, updateData.updateData.NextSiblingID);
  1126. //同步data
  1127. Object.assign(newNode.data, updateData.updateData);
  1128. const row = newNode.serialNo();
  1129. sheet.setValue(row, 0, newNode.data.name);
  1130. if (newNode.data.outputItemCharacter !== undefined) {
  1131. sheet.setValue(row, 1, newNode.data.outputItemCharacter);
  1132. }
  1133. showCheckBox(sheet, [newNode]);
  1134. refreshBtn(newNode);
  1135. }
  1136. }
  1137. if (!bills.tree.selected.data.hasGuide && bills.tree.selected.guidance.tree.items.length) {
  1138. bills.tree.selected.data.hasGuide = true;
  1139. setBillsForeColor([bills.tree.selected]);
  1140. }
  1141. if(callback){
  1142. callback();
  1143. }
  1144. setNodesColor(sheet, bills.tree.selected.guidance.tree.items);
  1145. guideItem.workBook.focus(true);
  1146. $.bootstrapLoading.end();
  1147. });
  1148. }
  1149. // 删除定额(嵌套删除自身及后代定额)
  1150. function delRations() {
  1151. const selected = bills.tree.selected.guidance.tree.selected;
  1152. if (!selected) {
  1153. return;
  1154. }
  1155. if (selected.data.type === itemType.ration) {
  1156. del({ row: selected.serialNo(), col: 0, rowCount: 1, colCount: 1 });
  1157. return;
  1158. }
  1159. const rations = selected.getPosterity().filter(item => item.data.type === itemType.ration);
  1160. if (!rations.length) {
  1161. return;
  1162. }
  1163. const updateDatas = rations.map(item => ({ updateType: updateType.del, findData: { ID: item.data.ID } }));
  1164. const rationIDs = rations.map(r => r.data.ID);
  1165. $.bootstrapLoading.start();
  1166. updateGuideItems(updateDatas, function () {
  1167. const guideSheet = guideItem.workBook.getSheet(0);
  1168. const treeNodes = bills.tree.selected.guidance.tree.items.filter(item => !rationIDs.includes(item.data.ID));
  1169. const state = bills.tree.selected.guidance.tree.getExpState(treeNodes);
  1170. const treeData = treeNodes.map(item => item.data);
  1171. initTree(bills.tree.selected.guidance, guideSheet, guideItem.treeSetting, treeData);
  1172. bills.tree.selected.guidance.tree.setExpandedByState(bills.tree.selected.guidance.tree.items, state);
  1173. renderSheetFunc(guideSheet, function () {
  1174. TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.selected.guidance.tree.roots, guideSheet, true);
  1175. });
  1176. //设置底色
  1177. setNodesColor(guideSheet, bills.tree.selected.guidance.tree.items);
  1178. //项目指引初始焦点
  1179. guideItemInitSel(guideSheet.getActiveRowIndex() ? guideSheet.getActiveRowIndex() : 0);
  1180. $.bootstrapLoading.end();
  1181. guideItem.workBook.focus(true)
  1182. });
  1183. }
  1184. //项目指引删除操作
  1185. //@return {void}
  1186. function del(selArea = null){
  1187. $.bootstrapLoading.start();
  1188. let controller = bills.tree.selected.guidance.controller;
  1189. let selNodes = [];
  1190. let sheet = guideItem.workBook.getSheet(0);
  1191. let sel = selArea ? selArea : sheet.getSelections()[0];
  1192. if(sel){
  1193. sel.row = sel.row === -1 ? 0 : sel.row;
  1194. for(let i = 0; i < sel.rowCount; i++){
  1195. if(bills.tree.selected.guidance.tree.items[sel.row + i]){
  1196. selNodes.push(bills.tree.selected.guidance.tree.items[sel.row + i]);
  1197. }
  1198. }
  1199. }
  1200. //选中的块节点
  1201. let blockNodes = getBlockNodes(selNodes);
  1202. let updateDatas = [];
  1203. function getDelDatas(nodes){
  1204. for (let node of nodes) {
  1205. updateDatas.push({updateType: updateType.del, findData: {ID: node.getID()}});
  1206. if (node.children.length > 0) {
  1207. getDelDatas(node.children);
  1208. }
  1209. }
  1210. }
  1211. getDelDatas(blockNodes);
  1212. //更新相关的前节点
  1213. for (let node of blockNodes) {
  1214. if (node.preSibling && !blockNodes.includes(node.preSibling)) {
  1215. let next = node;
  1216. while (next.nextSibling && blockNodes.includes(next.nextSibling)) {
  1217. next = next.nextSibling;
  1218. }
  1219. updateDatas.push({updateType: updateType.update, findData: {ID: node.preSibling.getID()}, updateData: {NextSiblingID: next.getNextSiblingID()}});
  1220. }
  1221. }
  1222. updateGuideItems(updateDatas, function () {
  1223. console.log(blockNodes);
  1224. controller.m_delete(blockNodes);
  1225. guideItemInitSel(sheet.getActiveRowIndex());
  1226. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1227. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1228. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1229. if (bills.tree.selected.data.hasGuide && !bills.tree.selected.guidance.tree.items.length) {
  1230. bills.tree.selected.data.hasGuide = false;
  1231. setBillsForeColor([bills.tree.selected]);
  1232. }
  1233. $.bootstrapLoading.end();
  1234. guideItem.workBook.focus(true)
  1235. });
  1236. }
  1237. //项目指引升级
  1238. //@return {void}
  1239. function upLevel(){
  1240. $.bootstrapLoading.start();
  1241. let controller = bills.tree.selected.guidance.controller;
  1242. let selected = bills.tree.selected.guidance.tree.selected;
  1243. let updateDatas = [];
  1244. //更新父节点
  1245. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getParentID()}, updateData: {NextSiblingID: selected.getID()}});
  1246. //更新选中节点
  1247. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()},
  1248. updateData: {ParentID: selected.parent.getParentID(), NextSiblingID: selected.parent.getNextSiblingID()}});
  1249. if(selected.nextSibling && selected.children.length > 0){
  1250. //更新选中节点最末子节点
  1251. let lastChild = selected.children[selected.children.length - 1];
  1252. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: -1}});
  1253. }
  1254. //选中节点的所有后兄弟节点成为选中节点的子项
  1255. let selectedNextIDs = [];
  1256. let sNext = selected.nextSibling;
  1257. while(sNext){
  1258. selectedNextIDs.push(sNext.getID());
  1259. sNext = sNext.nextSibling;
  1260. }
  1261. for(let sID of selectedNextIDs){
  1262. updateDatas.push({updateType: updateType.update, findData: {ID: sID}, updateData: {ParentID: selected.getID()}});
  1263. }
  1264. updateGuideItems(updateDatas, function () {
  1265. controller.upLevel();
  1266. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1267. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1268. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1269. $.bootstrapLoading.end();
  1270. guideItem.workBook.focus(true)//31574
  1271. });
  1272. }
  1273. //项目指引降级
  1274. //@return {void}
  1275. function downLevel(){
  1276. $.bootstrapLoading.start();
  1277. let controller = bills.tree.selected.guidance.controller;
  1278. let selected = bills.tree.selected.guidance.tree.selected;
  1279. let updateDatas = [];
  1280. //更新前兄弟节点
  1281. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1282. //更新前兄弟节点最末子节点
  1283. if(selected.preSibling.children.length > 0){
  1284. let lastChild = selected.preSibling.children[selected.preSibling.children.length - 1];
  1285. updateDatas.push({updateType: updateType.update, findData: {ID: lastChild.getID()}, updateData: {NextSiblingID: selected.getID()}});
  1286. }
  1287. //更新选中节点
  1288. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {ParentID: selected.preSibling.getID(), NextSiblingID: -1}});
  1289. updateGuideItems(updateDatas, function () {
  1290. controller.downLevel();
  1291. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1292. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1293. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1294. $.bootstrapLoading.end();
  1295. guideItem.workBook.focus(true)
  1296. });
  1297. }
  1298. //项目指引上移
  1299. //@return {void}
  1300. function upMove(){
  1301. $.bootstrapLoading.start();
  1302. let controller = bills.tree.selected.guidance.controller;
  1303. let selected = bills.tree.selected.guidance.tree.selected;
  1304. let updateDatas = [];
  1305. //更新前节点
  1306. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1307. //更新前前节点
  1308. if(selected.preSibling.preSibling){
  1309. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.preSibling.getID()}, updateData: {NextSiblingID: selected.getID()}});
  1310. }
  1311. //更新选中节点
  1312. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.preSibling.getID()}});
  1313. updateGuideItems(updateDatas, function () {
  1314. controller.upMove();
  1315. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1316. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1317. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1318. $.bootstrapLoading.end();
  1319. guideItem.workBook.focus(true)
  1320. });
  1321. }
  1322. //项目指引下移
  1323. //@return {void}
  1324. function downMove(){
  1325. $.bootstrapLoading.start();
  1326. let controller = bills.tree.selected.guidance.controller;
  1327. let selected = bills.tree.selected.guidance.tree.selected;
  1328. let updateDatas = [];
  1329. //更新下节点
  1330. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getNextSiblingID()}, updateData: {NextSiblingID: selected.getID()}});
  1331. //更新前节点
  1332. if(selected.preSibling){
  1333. updateDatas.push({updateType: updateType.update, findData: {ID: selected.preSibling.getID()}, updateData: {NextSiblingID: selected.getNextSiblingID()}});
  1334. }
  1335. //更新选中节点
  1336. updateDatas.push({updateType: updateType.update, findData: {ID: selected.getID()}, updateData: {NextSiblingID: selected.nextSibling.getNextSiblingID()}});
  1337. updateGuideItems(updateDatas, function () {
  1338. controller.downMove();
  1339. refreshBtn(bills.tree.selected.guidance.tree.selected);
  1340. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1341. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1342. $.bootstrapLoading.end();
  1343. guideItem.workBook.focus(true)
  1344. });
  1345. }
  1346. //获取定额类型的项目指引名称,通过定额转换
  1347. //@param {Object}ration @return {String}
  1348. function getRationItemName(ration){
  1349. let arr = [];
  1350. arr.push(ration.code ? ration.code : '');
  1351. arr.push(ration.name ? ration.name : '');
  1352. arr.push(ration.basePrice ? ration.basePrice : '');
  1353. let rst = arr.join(' ');
  1354. rst += `/${ration.unit ? ration.unit : ''}`;
  1355. return rst;
  1356. }
  1357. //获取选中的定额表行
  1358. //@return {Array}
  1359. function getCheckedRationRows(all){
  1360. let rst = [];
  1361. let sheet = ration.workBook.getActiveSheet();
  1362. for(let i = 0; i < sheet.getRowCount(); i++){
  1363. // 全选
  1364. if (all) {
  1365. rst.push(i);
  1366. continue;
  1367. }
  1368. let checked = sheet.getValue(i, 0);
  1369. if(checked){
  1370. rst.push(i);
  1371. }
  1372. }
  1373. return rst;
  1374. }
  1375. //清空选中定额表行
  1376. //@param {Array}rows @return {void}
  1377. function clearCheckedRation(rows) {
  1378. let sheet = ration.workBook.getActiveSheet();
  1379. renderSheetFunc(sheet, function () {
  1380. for(let row of rows){
  1381. sheet.setValue(row, 0, 0);
  1382. }
  1383. });
  1384. }
  1385. //获取要插入的定额数据
  1386. //@param {Array}rows @return {Array}
  1387. function getInsertRations(rows){
  1388. let rst = [];
  1389. //当前已存在定额
  1390. let curRationIems = [];
  1391. let selected = bills.tree.selected.guidance.tree.selected;
  1392. if(selected){
  1393. if(selected.data.type === itemType.job){
  1394. curRationIems = selected.children;
  1395. }
  1396. else {
  1397. curRationIems = selected.parent ? selected.parent.children : selected.tree.roots;
  1398. }
  1399. }
  1400. for(let row of rows){
  1401. let selRation = ration.cache[row];
  1402. if(selRation){
  1403. //添加的定额是否已存在,不重复添加
  1404. let isExist = false;
  1405. for(let curRation of curRationIems){
  1406. if(curRation.data.rationID == selRation.ID){
  1407. isExist = true;
  1408. break;
  1409. }
  1410. }
  1411. if(!isExist){
  1412. rst.push({type: itemType.ration, name: getRationItemName(selRation), rationID: selRation.ID});
  1413. }
  1414. }
  1415. }
  1416. return rst;
  1417. }
  1418. //获取块节点父项不存在于选中节点中的节点
  1419. //@param {Array}nodes(选中的节点) @return {Array}
  1420. function getBlockNodes(nodes) {
  1421. let nodeMapping = {};
  1422. for(let node of nodes){
  1423. nodeMapping[node.data.ID] = node;
  1424. }
  1425. //块节点,父项不存在于选中节点中的节点
  1426. let blockNodes = [];
  1427. for(let node of nodes){
  1428. if(!nodeMapping[node.data.ParentID]){
  1429. blockNodes.push(node);
  1430. }
  1431. }
  1432. return blockNodes;
  1433. }
  1434. //允许复制整块,如果有多个块节点,且块节点的父项不同,则不可复制
  1435. //@param {Array}nodes(块节点) @return {Boolean}
  1436. function canCopyBlock(nodes) {
  1437. if(!nodes || nodes.length === 0){
  1438. return false;
  1439. }
  1440. let pID = nodes[0].data.ParentID;
  1441. for(let node of nodes){
  1442. if(node.data.ParentID !== pID){
  1443. return false;
  1444. }
  1445. }
  1446. return true;
  1447. }
  1448. //允许粘贴整块 有粘贴数据,节点存在,如果粘贴到的节点为定额数据,粘贴数据为全定额数据
  1449. //@param {Object}node(粘贴到的节点)
  1450. function canPasteBlock(node) {
  1451. let pasteDatas = JSON.parse(getLocalCache(itemCopyBlockKey));
  1452. if(!pasteDatas || pasteDatas.length === 0){
  1453. return false;
  1454. }
  1455. if(!node){
  1456. return false;
  1457. }
  1458. //若粘贴到定额节点,则数据须全为定额
  1459. if(node.data.type === itemType.ration){
  1460. for(let data of pasteDatas){
  1461. if(data.type !== itemType.ration){
  1462. return false;
  1463. }
  1464. }
  1465. }
  1466. //若粘贴到非定额节点,则粘贴的顶层数据须全为非定额
  1467. else {
  1468. let topDatas = _.filter(pasteDatas, {ParentID: -1});
  1469. for(let topData of topDatas){
  1470. if(topData.type === itemType.ration){
  1471. return false;
  1472. }
  1473. }
  1474. }
  1475. return true;
  1476. }
  1477. //复制整块,将块节点下所有节点数据复制一份,并且重新生成ID、ParentID、NextSiblingID,使用localStorage存储
  1478. //@param {Array}nodes(块节点) @return {void}
  1479. function copyBlocks(nodes) {
  1480. nodes = _.cloneDeep(nodes);
  1481. //将块节点的ParentID暂时设置为-1
  1482. for(let topNode of nodes){
  1483. topNode.data.ParentID = -1;
  1484. }
  1485. let copyDatas = [];
  1486. let copyNodes = [];
  1487. //获取块节点包含的所有节点(包括自己)
  1488. function containNodes(nodes) {
  1489. for(let node of nodes){
  1490. copyNodes.push(node);
  1491. if(node.children.length > 0){
  1492. containNodes(node.children);
  1493. }
  1494. }
  1495. }
  1496. containNodes(nodes);
  1497. for(let node of copyNodes){
  1498. copyDatas.push(node.data);
  1499. }
  1500. console.log(`copyDatas`);
  1501. console.log(copyDatas);
  1502. setLocalCache(itemCopyBlockKey, JSON.stringify(copyDatas));
  1503. }
  1504. //粘贴整块,整块数据粘贴到相关节点,并成为其后项
  1505. //@param {Object}node(粘贴到的节点) @return {void}
  1506. function pasteBlock(node) {
  1507. let itemObj = bills.tree.selected.guidance;
  1508. let pasteDatas = JSON.parse(getLocalCache(itemCopyBlockKey));
  1509. //整理ID
  1510. let IDMapping = {};
  1511. for(let data of pasteDatas){
  1512. data.newID = uuid.v1();
  1513. IDMapping[data.ID] = data;
  1514. }
  1515. for(let data of pasteDatas){
  1516. let nextData = IDMapping[data.NextSiblingID];
  1517. data.NextSiblingID = nextData ? nextData.newID : -1;
  1518. let parentData = IDMapping[data.ParentID];
  1519. data.ParentID = parentData ? parentData.newID : -1;
  1520. }
  1521. for(let data of pasteDatas){
  1522. data.ID = data.newID;
  1523. delete data.newID;
  1524. }
  1525. let updateDatas = [];
  1526. //将最顶层的块数据的ParentID设置成粘贴到节点的ParentID,并设置新的billsID
  1527. let topDatas = _.filter(pasteDatas, {ParentID: -1});
  1528. for(let topData of topDatas){
  1529. topData.ParentID = node.getParentID();
  1530. }
  1531. //更新数据
  1532. //更新插入的最末顶层数据NextSiblingID
  1533. if(node.nextSibling){
  1534. topDatas[topDatas.length - 1].NextSiblingID = node.getNextSiblingID();
  1535. }
  1536. //新建节点
  1537. for(let data of pasteDatas){
  1538. data.libID = libID;
  1539. data.billsID = node.data.billsID;
  1540. delete data._id;
  1541. updateDatas.push({updateType: updateType.create, updateData: data});
  1542. }
  1543. console.log(`pasteDatas`);
  1544. console.log(pasteDatas);
  1545. //更新粘贴到的节点的NextSiblingID
  1546. updateDatas.push({updateType: updateType.update, findData: {ID: node.data.ID}, updateData: {NextSiblingID: topDatas[0].ID}})
  1547. $.bootstrapLoading.start();
  1548. updateGuideItems(updateDatas, function (rstData) {
  1549. $.bootstrapLoading.end();
  1550. node.data.NextSiblingID = topDatas[0].ID;
  1551. let newNodes = itemObj.tree.insertDatasTo(node.data, pasteDatas);
  1552. cleanData(guideItem.workBook.getActiveSheet(), guideItem.headers, -1);
  1553. itemObj.controller.showTreeData();
  1554. setNodesColor(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1555. showCheckBox(guideItem.workBook.getActiveSheet(), bills.tree.selected.guidance.tree.items);
  1556. }, function () {
  1557. $.bootstrapLoading.end();
  1558. });
  1559. }
  1560. // 双击定位定额
  1561. async function locateAtRation(row) {
  1562. const node = bills.tree.selected.guidance.tree.items[row];
  1563. if (!node || !node.data.rationID) {
  1564. return;
  1565. }
  1566. const rationItem = ration.datas.find(item => item.ID === node.data.rationID);
  1567. // 当前库没有找到,需要后端查找
  1568. if (!rationItem) {
  1569. const findedRation = await ajaxPost('/rationRepository/api/getRationByID',{ ID: node.data.rationID });
  1570. if (!findedRation || !rationLibIDs.includes(findedRation.rationRepId)) {
  1571. alert('无法定位此定额,此定额已被删除');
  1572. return;
  1573. }
  1574. changeRationLib(findedRation.rationRepId, () => {
  1575. located(findedRation.ID, findedRation.sectionId);
  1576. })
  1577. } else {
  1578. located(rationItem.ID, rationItem.sectionId);
  1579. }
  1580. function located(rationID, sectionID) {
  1581. if (!section.tree) {
  1582. return;
  1583. }
  1584. const sectionNode = section.tree.nodes[section.tree.prefix + sectionID];
  1585. if (!sectionNode) {
  1586. return;
  1587. }
  1588. // 定位到对应章节树
  1589. const sectionSheet = section.workBook.getActiveSheet();
  1590. const sectionRow = sectionNode.serialNo();
  1591. expandSearchNodes(sectionSheet, [sectionNode], section.tree.roots);
  1592. sectionSheet.setSelection(sectionRow, 0, 1, 1);
  1593. sectionSheet.showRow(sectionRow, GC.Spread.Sheets.VerticalPosition.center);
  1594. sectionInitSel(sectionRow);
  1595. // 定位到对应定额
  1596. const locatedRationRow = ration.cache.findIndex(item => item.ID === rationID);
  1597. if (locatedRationRow !== -1) {
  1598. const rationSheet = ration.workBook.getActiveSheet();
  1599. rationSheet.setSelection(locatedRationRow, 1, 1, 1);
  1600. rationSheet.showRow(locatedRationRow, GC.Spread.Sheets.VerticalPosition.center);
  1601. }
  1602. }
  1603. }
  1604. //初始化右键菜单
  1605. //@return {void}
  1606. function initContextMenu() {
  1607. $.contextMenu({
  1608. selector: '#guideItemSpread',
  1609. build: function($triggerElement, e){
  1610. //控制允许右键菜单在哪个位置出现
  1611. let sheet = guideItem.workBook.getSheet(0);
  1612. let offset = $("#guideItemSpread").offset(),
  1613. x = e.pageX - offset.left,
  1614. y = e.pageY - offset.top;
  1615. let target = sheet.hitTest(x, y);
  1616. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  1617. let sel = sheet.getSelections()[0];
  1618. if(sel && sel.rowCount === 1){
  1619. sheet.setActiveCell(target.row, target.col);
  1620. }
  1621. sel = sheet.getSelections()[0];
  1622. let selNodes = [];
  1623. if(sel){
  1624. sel.row = sel.row === -1 ? 0 : sel.row;
  1625. for(let i = 0; i < sel.rowCount; i++){
  1626. if(bills.tree.selected.guidance.tree.items[sel.row + i]){
  1627. selNodes.push(bills.tree.selected.guidance.tree.items[sel.row + i]);
  1628. }
  1629. }
  1630. }
  1631. //块节点
  1632. let blockNodes = getBlockNodes(selNodes);
  1633. //右键在多选内则不重设焦点
  1634. if(!sel || sel.rowCount === 1 || !(target.row >= sel.row && target.row <= sel.row + sel.rowCount - 1)){
  1635. sheet.setActiveCell(target.row, target.col);
  1636. }
  1637. guideItemInitSel(target.row);
  1638. return {
  1639. callback: function(){},
  1640. items: {
  1641. "copy": {
  1642. name: "复制整块",
  1643. disabled: function () {
  1644. return locked || !canCopyBlock(blockNodes);
  1645. },
  1646. icon: "fa-copy",
  1647. callback: function (key, opt) {
  1648. copyBlocks(blockNodes);
  1649. }},
  1650. "paste": {
  1651. name: "粘贴整块",
  1652. disabled: function () {
  1653. let pasteNode = bills.tree.selected.guidance.tree.items[target.row];
  1654. return locked || !canPasteBlock(pasteNode);
  1655. },
  1656. icon: "fa-paste",
  1657. callback: function (key, opt) {
  1658. let pasteNode = bills.tree.selected.guidance.tree.items[target.row];
  1659. pasteBlock(pasteNode);
  1660. }},
  1661. "delRations": {
  1662. name: '删除定额',
  1663. disabled: function () {
  1664. let node = bills.tree.selected.guidance.tree.items[target.row];
  1665. return locked || !node
  1666. },
  1667. icon: "fa-remove",
  1668. callback: function (key, opt) {
  1669. $('#delRationAlert').modal('show');
  1670. }
  1671. },
  1672. "del": {
  1673. name: '删除',
  1674. disabled: function () {
  1675. let node = bills.tree.selected.guidance.tree.items[target.row];
  1676. return locked || !node
  1677. },
  1678. icon: "fa-remove",
  1679. callback: function (key, opt) {
  1680. $('#delAlert').modal('show');
  1681. }
  1682. },
  1683. "insertSibling": {
  1684. name: '插入行',
  1685. disabled: function () {
  1686. let node = bills.tree.selected.guidance.tree.items[target.row];
  1687. return locked || !node || node.data.type !== itemType.job;
  1688. },
  1689. icon: "fa-arrow-left",
  1690. callback: function (key, opt) {
  1691. insert([{type: itemType.job, name: ''}], false);
  1692. }
  1693. },
  1694. "insertChild": {
  1695. name: '插入子项',
  1696. disabled: function () {
  1697. let node = bills.tree.selected.guidance.tree.items[target.row];
  1698. return locked || !node || node.data.type !== itemType.job || !allJobChildren(node);
  1699. },
  1700. icon: 'fa-arrow-left',
  1701. callback: function (key, opt) {
  1702. insert([{type: itemType.job, name: ''}], true);
  1703. }
  1704. }
  1705. }
  1706. };
  1707. }
  1708. else{
  1709. return false;
  1710. }
  1711. }
  1712. });
  1713. }
  1714. // 替换定额
  1715. function replaceRation(newRation, orgItem) {
  1716. if (newRation.ID === orgItem.data.rationID) {
  1717. return;
  1718. }
  1719. $.bootstrapLoading.start();
  1720. const updateAttrs = {name: getRationItemName(newRation), rationID: newRation.ID};
  1721. const updateData = [{updateType: updateType.update, findData: {ID: orgItem.getID()}, updateData: updateAttrs }];
  1722. updateGuideItems(updateData, function () {
  1723. Object.assign(orgItem.data, updateAttrs);
  1724. const row = orgItem.serialNo();
  1725. guideItem.workBook.getSheet(0).setValue(row, 0, orgItem.data.name);
  1726. $.bootstrapLoading.end();
  1727. });
  1728. }
  1729. // 初始化定额右键菜单
  1730. function initRationContextMenu() {
  1731. $.contextMenu({
  1732. selector: '#rationSpread',
  1733. build: function($triggerElement, e){
  1734. //控制允许右键菜单在哪个位置出现
  1735. let sheet = ration.workBook.getActiveSheet();;
  1736. let offset = $("#rationSpread").offset(),
  1737. x = e.pageX - offset.left,
  1738. y = e.pageY - offset.top;
  1739. let target = sheet.hitTest(x, y);
  1740. if(target.hitTestType === 3 && typeof target.row !== 'undefined' && typeof target.col !== 'undefined'){//在表格内
  1741. let sel = sheet.getSelections()[0];
  1742. if(sel && sel.rowCount === 1){
  1743. sheet.setActiveCell(target.row, target.col);
  1744. }
  1745. sel = sheet.getSelections()[0];
  1746. if(sel){
  1747. sel.row = sel.row === -1 ? 0 : sel.row;
  1748. }
  1749. //右键在多选内则不重设焦点
  1750. if(!sel || sel.rowCount === 1 || !(target.row >= sel.row && target.row <= sel.row + sel.rowCount - 1)){
  1751. sheet.setActiveCell(target.row, target.col);
  1752. }
  1753. return {
  1754. callback: function(){},
  1755. items: {
  1756. "replace": {
  1757. name: "替换定额",
  1758. disabled: function () {
  1759. return locked || !bills.tree.selected || !bills.tree.selected.guidance || !bills.tree.selected.guidance.tree || !bills.tree.selected.guidance.tree.selected || bills.tree.selected.guidance.tree.selected.data.type !== itemType.ration;
  1760. },
  1761. icon: "fa-copy",
  1762. callback: function (key, opt) {
  1763. const row = sheet.getActiveRowIndex();
  1764. if (!ration.cache[row]) {
  1765. return;
  1766. }
  1767. replaceRation(ration.cache[row], bills.tree.selected.guidance.tree.selected);
  1768. // copyBlocks(blockNodes);
  1769. }},
  1770. }
  1771. };
  1772. }
  1773. else{
  1774. return false;
  1775. }
  1776. }
  1777. });
  1778. }
  1779. //展开至搜索出来点的节点
  1780. //@param {Array}nodes @return {void}
  1781. function expandSearchNodes(sheet, nodes, roots){
  1782. renderSheetFunc(sheet, function () {
  1783. function expParentNode(node){
  1784. if(node.parent){
  1785. if (!node.parent.expanded) {
  1786. node.parent.setExpanded(true);
  1787. }
  1788. expParentNode(node.parent);
  1789. }
  1790. }
  1791. for(let node of nodes){
  1792. expParentNode(node);
  1793. }
  1794. TREE_SHEET_HELPER.refreshNodesVisible(roots, sheet, true);
  1795. });
  1796. }
  1797. // 清空搜索高亮
  1798. function clearHighLight(sheet) {
  1799. renderSheetFunc(sheet, () => {
  1800. for (let i = 0; i < sheet.getRowCount(); i++){
  1801. setBgColor(sheet, i, 'white')
  1802. }
  1803. });
  1804. }
  1805. // 清空搜索状态
  1806. // 关闭搜索清单结果
  1807. function closeSearchBills(sheet) {
  1808. if (!bills.tree) {
  1809. return;
  1810. }
  1811. $('#searchBillsResult').hide();
  1812. bills.tree.items.forEach(node => {
  1813. node.isSearch = false;
  1814. });
  1815. clearHighLight(sheet);
  1816. setBgColor(sheet, sheet.getActiveRowIndex(), selectedBgColor);
  1817. }
  1818. // 搜索清单
  1819. function searchBills() {
  1820. if (!bills.tree || !bills.workBook) {
  1821. return;
  1822. }
  1823. const sheet = bills.workBook.getActiveSheet();
  1824. const str = $('#searchBillText').val().trim();
  1825. // 空搜索字符,关闭搜索
  1826. if (!str) {
  1827. if ($('#searchBillsResult').is(':visible')) {
  1828. closeSearchBills(sheet);
  1829. }
  1830. return;
  1831. }
  1832. // 过滤清单
  1833. const result = bills.tree.items.filter(item => {
  1834. const codeIs = item.data.code ? item.data.code.indexOf(str) !== -1 : false;
  1835. const nameIs = item.data.name ? item.data.name.indexOf(str) !== -1 : false;
  1836. return codeIs || nameIs;
  1837. });
  1838. if (!result.length) {
  1839. closeSearchBills(sheet);
  1840. return;
  1841. }
  1842. // 显示搜索结果
  1843. $("#searchBillsResult").show();
  1844. $('#searchBillsCount').text(result.length);
  1845. //展开搜索出来的节点
  1846. expandSearchNodes(sheet, result, bills.tree.roots);
  1847. // 标黄结果
  1848. clearHighLight(sheet);
  1849. const col = sheet.getActiveColumnIndex();
  1850. renderSheetFunc(sheet, function () {
  1851. bills.controller.setTreeSelected(result[0]);
  1852. bills.tree.items.forEach(node => {
  1853. if (result.includes(node)) {
  1854. setBgColor(sheet, node.serialNo(), searchBgColor);
  1855. node.isSearch = true; // 标记为搜索结果,防止被焦点行变更恢复颜色
  1856. } else {
  1857. node.isSearch = false;
  1858. }
  1859. })
  1860. });
  1861. //搜索初始定位
  1862. const row = sheet.getActiveRowIndex();
  1863. sheet.setSelection(result[0].serialNo(), col, 1, 1);
  1864. billsInitSel(result[0].serialNo(), { row });
  1865. sheet.showRow(result[0].serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  1866. let curIndex = 0;
  1867. // 上一条
  1868. $('#preBill').unbind('click');
  1869. $('#preBill').bind('click', () => {
  1870. const node = result[curIndex - 1];
  1871. if (!node) {
  1872. return;
  1873. }
  1874. curIndex -= 1;
  1875. const col = sheet.getActiveColumnIndex();
  1876. const row = node.serialNo();
  1877. const orgRow = sheet.getActiveRowIndex();
  1878. sheet.setSelection(row, col, 1, 1);
  1879. billsInitSel(row, { row: orgRow });
  1880. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  1881. });
  1882. // 下一条
  1883. $('#nextBills').unbind('click');
  1884. $('#nextBills').bind('click', () => {
  1885. const node = result[curIndex + 1];
  1886. if (!node) {
  1887. return;
  1888. }
  1889. curIndex += 1;
  1890. const col = sheet.getActiveColumnIndex();
  1891. const row = node.serialNo();
  1892. const orgRow = sheet.getActiveRowIndex();
  1893. sheet.setSelection(row, col, 1, 1);
  1894. billsInitSel(row, { row: orgRow });
  1895. sheet.showRow(row, GC.Spread.Sheets.VerticalPosition.center);
  1896. });
  1897. }
  1898. //初始化个按钮点击
  1899. //@return {void}
  1900. function initBtn(){
  1901. $('#insert').click(function () {
  1902. insert([{type: itemType.job, name: '', outputItemCharacter: true }], false);
  1903. });
  1904. $('#delConfirm').click(function () {
  1905. del();
  1906. $('#delAlert').modal('hide');
  1907. });
  1908. $('#delRationConfirm').click(function () {
  1909. delRations();
  1910. $('#delRationAlert').modal('hide');
  1911. });
  1912. $('#del').click(function () {
  1913. $('#delAlert').modal('show');
  1914. });
  1915. $('#upLevel').click(function () {
  1916. upLevel();
  1917. });
  1918. $('#downLevel').click(function () {
  1919. downLevel();
  1920. });
  1921. $('#upMove').click(function () {
  1922. upMove();
  1923. });
  1924. $('#downMove').click(function () {
  1925. downMove();
  1926. });
  1927. // 收起清单至第二层
  1928. $('#expandToSecond').click(function() {
  1929. if (!bills.tree || !bills.workBook) {
  1930. return;
  1931. }
  1932. bills.tree.roots.forEach(root => {
  1933. root.setExpanded(true);
  1934. root.children.forEach(item => {
  1935. item.setExpanded(false);
  1936. });
  1937. });
  1938. const billSheet = bills.workBook.getActiveSheet();
  1939. renderSheetFunc(billSheet, function () {
  1940. TREE_SHEET_HELPER.refreshNodesVisible(bills.tree.roots, billSheet, true);
  1941. });
  1942. })
  1943. //收起定额、展开全部
  1944. $('#expandContract').click(function () {
  1945. //目前状态时展开全部节点状态,点击则收起定额
  1946. let tree = bills.tree.selected.guidance.tree,
  1947. itemSheet = guideItem.workBook.getActiveSheet();
  1948. if(curExpandState === itemExpandState.expand){
  1949. curExpandState = itemExpandState.contract;
  1950. $(this).html('<i class="fa fa-plus-square-o" aria-hidden="true"></i> 展开全部');
  1951. setNodesExpandState(tree.items, itemExpandState.contract);
  1952. } else {
  1953. curExpandState = itemExpandState.expand;
  1954. $(this).html('<i class="fa fa-minus-square-o" aria-hidden="true"></i> 收起定额');
  1955. setNodesExpandState(tree.items, itemExpandState.expand);
  1956. }
  1957. renderSheetFunc(itemSheet, function () {
  1958. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, itemSheet, true);
  1959. });
  1960. });
  1961. // 插入选中定额
  1962. $('#insertRation').click(function () {
  1963. let checkedRows = getCheckedRationRows();
  1964. let insertDatas = getInsertRations(checkedRows);
  1965. if(insertDatas.length > 0){
  1966. insert(insertDatas, false, function () {
  1967. //清空选择
  1968. clearCheckedRation(checkedRows);
  1969. });
  1970. }
  1971. else {
  1972. clearCheckedRation(checkedRows);
  1973. }
  1974. });
  1975. // 插入全部定额
  1976. $('#insertAll').click(function () {
  1977. let isAll = true;
  1978. let checkedRows = getCheckedRationRows(isAll);
  1979. let insertDatas = getInsertRations(checkedRows);
  1980. if(insertDatas.length > 0){
  1981. insert(insertDatas, false);
  1982. }
  1983. });
  1984. // 搜索清单
  1985. $("#searchBillBtn").click(searchBills);
  1986. $('#searchBillText').keyup(function (e) {
  1987. delayKeyup(function () {
  1988. $('#searchBillBtn').click();
  1989. });
  1990. });
  1991. // 关闭搜索清单
  1992. $('#closeSearchBills').click(() => {
  1993. if (!bills.workBook) {
  1994. return;
  1995. }
  1996. const sheet = bills.workBook.getActiveSheet();
  1997. closeSearchBills(sheet);
  1998. })
  1999. //搜索定额
  2000. $('#searchBtn').click(function () {
  2001. let searchStr = $('#searchText').val();
  2002. if(!searchStr || searchStr === ''){
  2003. ration.cache = ration.datas;
  2004. }
  2005. else{
  2006. let reg = new RegExp(searchStr, 'i');
  2007. ration.cache = _.filter(ration.datas, function (data) {
  2008. return reg.test(data.code) || reg.test(data.name);
  2009. });
  2010. }
  2011. $('.top-content').hide();
  2012. $('#deResize').hide();
  2013. $('#searchCount').text(`搜索结果: ${ration.cache.length}`);
  2014. $('#rationSearchResult').show();
  2015. autoFlashHeight();
  2016. ration.workBook.refresh();
  2017. let rationSheet = ration.workBook.getActiveSheet();
  2018. renderSheetFunc(rationSheet, function () {
  2019. clearCheckedRation(getCheckedRationRows());
  2020. showData(rationSheet, ration.headers, ration.cache);
  2021. })
  2022. });
  2023. //关闭搜索
  2024. $('#rationSearchResult a').click(function () {
  2025. $('.top-content').show();
  2026. $('#deResize').show();
  2027. $('#rationSearchResult').hide();
  2028. autoFlashHeight();
  2029. renderSheetFunc(ration.workBook.getActiveSheet(), function () {
  2030. clearCheckedRation(getCheckedRationRows());
  2031. });
  2032. section.workBook.refresh();
  2033. ration.workBook.refresh();
  2034. $('#searchText').val('');
  2035. //恢复章节树下的定额
  2036. sectionInitSel(section.workBook.getActiveSheet().getActiveRowIndex());
  2037. });
  2038. let keyupTime = 0,
  2039. delayTime = 500;
  2040. function delayKeyup(callback) {
  2041. let nowTime = Date.now();
  2042. keyupTime = nowTime;
  2043. setTimeout(function () {
  2044. if (nowTime - keyupTime == 0) {
  2045. callback();
  2046. }
  2047. }, delayTime);
  2048. }
  2049. //执行搜索
  2050. $('#searchText').keyup(function (e) {
  2051. delayKeyup(function () {
  2052. $('#searchBtn').click();
  2053. });
  2054. });
  2055. //编辑清单备注
  2056. $('.main-side-bottom').find('textarea').keyup(function () {
  2057. let me = this;
  2058. let node = bills.tree.selected;
  2059. let comment = $(me).val();
  2060. delayKeyup(function () {
  2061. if (node) {
  2062. let updateData = {lastOperator: userAccount, billsLibId: billsLibId, updateId: node.getID(), field: 'comment', data: comment};
  2063. updateBillsComment(updateData, function () {
  2064. node.data.comment = comment;
  2065. }, function() {
  2066. $(me).val(node.data.comment || '');
  2067. })
  2068. }
  2069. });
  2070. });
  2071. //编辑选项备注
  2072. $('.main-bottom-content').find('textarea').keyup(function () {
  2073. let me = this;
  2074. let node = bills.tree.selected.guidance.tree.selected;
  2075. let comment = $(me).val();
  2076. delayKeyup(function () {
  2077. if(node){
  2078. let updateDatas = [{updateType: updateType.update, findData: {ID: node.getID()}, updateData: {comment: comment}}];
  2079. updateGuideItems(updateDatas, function (rstData) {
  2080. node.data.comment = comment;
  2081. }, function() {
  2082. $(me).val(node.data.comment || '');
  2083. });
  2084. }
  2085. });
  2086. });
  2087. //定额高度拖动调整
  2088. let heightEleObj = {
  2089. module: moduleName,
  2090. resize: $('#deResize'),
  2091. top: $('#topContent'),
  2092. topSpread: $('#sectionSpread'),
  2093. bottom: $('#bottomContent'),
  2094. bottomSpread: $('#rationSpread')
  2095. },
  2096. heightLimit = {
  2097. min: 150,
  2098. max: `$(window).height()-$('.header').height()-$('.sidebar-tools-bar').height()-150-10`,
  2099. notTopSpread: 0,
  2100. notBottomSpread: 0,
  2101. };
  2102. SlideResize.verticalSlide(heightEleObj, heightLimit, function () {
  2103. if(section.workBook){
  2104. section.workBook.refresh();
  2105. }
  2106. if(ration.workBook){
  2107. ration.workBook.refresh();
  2108. }
  2109. });
  2110. /*slideResize(rationLibResizeEles, {min: 147, max: 680}, 'height', function() {
  2111. //autoFlashHeight();
  2112. if(section.workBook){
  2113. section.workBook.refresh();
  2114. }
  2115. if(ration.workBook){
  2116. ration.workBook.refresh();
  2117. }
  2118. });*/
  2119. //左右拖动
  2120. //清单表与项目指引表
  2121. let leftElesObj = {};
  2122. leftElesObj.module = moduleName;
  2123. leftElesObj.resize = $('#slideResizeLeft');
  2124. leftElesObj.parent = $('#dataRow');
  2125. leftElesObj.left = $('#leftContent');
  2126. leftElesObj.right = $('#midContent');
  2127. SlideResize.horizontalSlide(leftElesObj, {min: 200, max: `$('#dataRow').width() - $('#rightContent').width() - 200`}, function () {
  2128. refreshALlWorkBook();
  2129. });
  2130. //人材机表与人材机组成物表
  2131. let rightElesObj = {};
  2132. rightElesObj.module = moduleName;
  2133. rightElesObj.resize = $('#slideResizeRight');
  2134. rightElesObj.parent = $('#dataRow');
  2135. rightElesObj.left = $('#midContent');
  2136. rightElesObj.right = $('#rightContent');
  2137. SlideResize.horizontalSlide(rightElesObj, {min: 200, max: `$('#dataRow').width() - $('#leftContent').width() - 200`}, function () {
  2138. refreshALlWorkBook();
  2139. });
  2140. }
  2141. //刷新全部工作簿
  2142. //@return {void}
  2143. function refreshALlWorkBook() {
  2144. if (bills.workBook) {
  2145. bills.workBook.refresh();
  2146. }
  2147. if (guideItem.workBook) {
  2148. guideItem.workBook.refresh();
  2149. }
  2150. if (section.workBook) {
  2151. section.workBook.refresh();
  2152. }
  2153. if (ration.workBook) {
  2154. ration.workBook.refresh();
  2155. }
  2156. $('.main-side-bottom').find('textarea').height($('.main-side-bottom').height() - 20);
  2157. $('.main-side-bottom').find('textarea').width($('.main-side-bottom').width() - 25);
  2158. $('.main-bottom-content').find('textarea').height($('.main-bottom-content').height() - 20);
  2159. $('.main-bottom-content').find('textarea').width($('.main-bottom-content').width() - 25);
  2160. }
  2161. //读取拖动相关
  2162. //@return {void}
  2163. function initSlideSize() {
  2164. //定额表上下
  2165. let heightEleObj = {
  2166. module: moduleName,
  2167. top: $('#topContent'),
  2168. topSpread: $('#sectionSpread'),
  2169. bottom: $('#bottomContent'),
  2170. bottomSpread: $('#rationSpread')
  2171. };
  2172. if (!$('#rationSearchResult').is(':visible')) {
  2173. SlideResize.loadVerticalHeight(heightEleObj.module, heightEleObj,
  2174. {totalHeight: `$(window).height()-$('.header').height()-$('.sidebar-tools-bar').height()-10`,
  2175. notTopSpread: 0, notBottomSpread: 0}, function () {
  2176. if(section.workBook){
  2177. section.workBook.refresh();
  2178. }
  2179. if(ration.workBook){
  2180. ration.workBook.refresh();
  2181. }
  2182. });
  2183. }
  2184. //水平
  2185. SlideResize.loadHorizonWidth(moduleName, [$('#slideResizeLeft'), $('#slideResizeRight')], [$('#leftContent'), $('#midContent'), $('#rightContent')], function () {
  2186. refreshALlWorkBook();
  2187. });
  2188. }
  2189. //初始化视图
  2190. //@param {void} @return {void}
  2191. function initViews(){
  2192. let modules = [bills, guideItem, section, ration];
  2193. initWorkBooks(modules);
  2194. lockUtil.lockTools($(document.body), locked);
  2195. getLibWithBills(libID);
  2196. initBtn();
  2197. initContextMenu();
  2198. initRationContextMenu();
  2199. initSlideSize();
  2200. }
  2201. return {initViews, initSlideSize};
  2202. })();
  2203. $(document).ready(function () {
  2204. billsGuidance.initViews();
  2205. });