block_lib.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /**
  2. * 块模板库管理。
  3. * Created by CSL on 2018-09-19.
  4. */
  5. var blockLibObj = {
  6. libs: [],
  7. activeLib: null,
  8. mainSpread: null,
  9. mainSheet: null,
  10. mainTree: null,
  11. mainTreeController: null,
  12. mainSetting: {
  13. "emptyRowHeader": true,
  14. "rowHeaderWidth": 15,
  15. "emptyRows":0,
  16. "headRows":1,
  17. "headRowHeight":[30],
  18. "defaultRowHeight": 21,
  19. "treeCol": 9,
  20. "cols":[{
  21. "width":400,
  22. "readOnly": true,
  23. "head":{
  24. "titleNames":["名称"],
  25. "spanCols":[1],
  26. "spanRows":[1],
  27. "vAlign":[1],
  28. "hAlign":[1],
  29. "font":["Arial"]
  30. },
  31. "data":{
  32. "field":"nodeName",
  33. "vAlign":1,
  34. "hAlign":0,
  35. "font":"Arial"
  36. }
  37. }]
  38. },
  39. mainDatas: [],
  40. billSpread: null,
  41. billSheet: null,
  42. billSetting: {
  43. header: [
  44. {headerName: "项目编码", headerWidth: 90, dataCode: "code", dataType: "String", hAlign: "center"},
  45. {headerName: "项目名称", headerWidth: 100, dataCode: "name", dataType: "String"},
  46. {headerName: "单位", headerWidth: 40, dataCode: "unit", dataType: "String", hAlign: "center"},
  47. {headerName: "综合单价", headerWidth: 70, dataCode: "unitFee", dataType: "Number"},
  48. {headerName: "项目特征", headerWidth: 160, dataCode: "itemCharacterText", dataType: "String"}
  49. ],
  50. view: {
  51. lockColumns: [0, 1, 2, 3, 4]
  52. }
  53. },
  54. rationSpread: null,
  55. rationSheet: null,
  56. rationSetting: {
  57. header: [
  58. {headerName: "编码", headerWidth: 45, dataCode: "code", dataType: "String", hAlign: "center"},
  59. {headerName: "名称", headerWidth: 100, dataCode: "name", dataType: "String"},
  60. {headerName: "单位", headerWidth: 40, dataCode: "unit", dataType: "String", hAlign: "center"},
  61. {headerName: "含量", headerWidth: 40, dataCode: "contain", dataType: "Number"},
  62. {headerName: "取费专业", headerWidth: 70, dataCode: "programName", dataType: "String", hAlign: "center"},
  63. {headerName: "综合单价", headerWidth: 70, dataCode: "unitFee", dataType: "Number"},
  64. {headerName: "子目换算状态", headerWidth: 90, dataCode: "adjustState", dataType: "String"}
  65. ],
  66. view: {
  67. lockColumns: [0, 1, 2, 3, 4, 5, 6]
  68. }
  69. },
  70. cloneType: null,
  71. buildSheet: async function () {
  72. $.bootstrapLoading.start();
  73. let me = this;
  74. /* me.mainDatas = [
  75. {ID: 1, ParentID: -1, NextSiblingID: 2, nodeName: '分类1', type: 1},
  76. {ID: 2, ParentID: -1, NextSiblingID: 3, nodeName: '分类2', type: 1},
  77. {ID: 3, ParentID: -1, NextSiblingID: 4, nodeName: '分类3', type: 1},
  78. // {ID: 105, ParentID: 1, NextSiblingID: 106, name: '块1', type: 2},
  79. // {ID: 106, ParentID: 1, NextSiblingID: 107, name: '块2', type: 2},
  80. {ID: 7, ParentID: -1, NextSiblingID: 8, nodeName: '分类7', type: 1},
  81. {ID: 9, ParentID: -1, NextSiblingID: -1, nodeName: '分类9', type: 1}//,
  82. // {ID: 201, ParentID: 52, NextSiblingID: -1, nodeName: '块201', type: 2}
  83. ];*/
  84. if (me.libs.length > 0) me.libs.splice(0, me.libs.length);
  85. if (me.mainSpread) {
  86. me.mainSpread.destroy();
  87. me.mainSpread = null;
  88. };
  89. if (me.billSpread) {
  90. me.billSpread.destroy();
  91. me.billSpread = null;
  92. };
  93. if (me.rationSpread) {
  94. me.rationSpread.destroy();
  95. me.rationSpread = null;
  96. };
  97. let namesAndLib = await ajaxPost('/blockLib/getLibNamesAndFirstLib', {userID: userID, compilationID: projectInfoObj.projectInfo.compilation});
  98. me.mainDatas = namesAndLib.firstLib.datas;
  99. me.libs.push(namesAndLib.firstLib);
  100. me.activeLib = namesAndLib.firstLib;
  101. function getLibNamesHtml(libsArr) {
  102. let result = '';
  103. for (let lib of libsArr) {
  104. result += '<option value="' + lib.libID + '">' + lib.libName + '</option>';
  105. };
  106. return result;
  107. };
  108. let html = getLibNamesHtml(namesAndLib.libNames);
  109. $("#select_block_lib_names").html(html);
  110. me.mainSpread = SheetDataHelper.createNewSpread($('#div_block_tree')[0]);
  111. // me.mainSpread = TREE_SHEET_HELPER.createNewSpread($('#div_block_tree')[0]);
  112. me.mainSheet = me.mainSpread.getSheet(0);
  113. me.mainSheet.name('blockLibSheet');
  114. sheetCommonObj.spreadDefaultStyle(me.mainSpread);
  115. function showBlockTree(datas) {
  116. me.mainTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  117. me.mainTreeController = TREE_SHEET_CONTROLLER.createNew(me.mainTree, me.mainSheet, me.mainSetting);
  118. me.mainTree.loadDatas(datas);
  119. me.mainTreeController.showTreeData();
  120. me.mainSheet.getRange(-1, 0, -1, 1).cellType(me.getTreeCell(me.mainTree));
  121. me.mainTree.selected = me.mainTree.items[0];
  122. me.mainTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  123. blockLibObj.loadDetailDatas(node);
  124. });
  125. };
  126. showBlockTree(me.mainDatas);
  127. me.billSpread = sheetCommonObj.buildSheet($('#div_block_bill')[0], me.billSetting, 1);
  128. me.billSheet = me.billSpread.getSheet(0);
  129. sheetCommonObj.spreadDefaultStyle(me.billSpread);
  130. me.billSheet.setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  131. me.rationSpread = sheetCommonObj.buildSheet($('#div_block_ration')[0], me.rationSetting, 1);
  132. me.rationSheet = me.rationSpread.getSheet(0);
  133. sheetCommonObj.spreadDefaultStyle(me.rationSpread);
  134. me.rationSheet.setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  135. me.loadTreeContextMenu();
  136. me.mainSpread.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  137. me.mainSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onCellDoubleClick);
  138. $.bootstrapLoading.end();
  139. },
  140. loadDetailDatas: function (node){
  141. let me = this;
  142. if (!node) return;
  143. if (node.data.type == 2){
  144. let bill = node.data;
  145. let rations = bill.children;
  146. sheetCommonObj.showData(me.billSheet, me.billSetting, [bill]);
  147. let rCount = (rations.length > 0) ? rations.length : 1;
  148. me.rationSheet.setRowCount(rCount, GC.Spread.Sheets.SheetArea.viewport);
  149. sheetCommonObj.showData(me.rationSheet, me.rationSetting, rations);
  150. }
  151. else{
  152. sheetCommonObj.cleanSheet(me.billSheet, me.billSetting, 1);
  153. sheetCommonObj.cleanSheet(me.rationSheet, me.rationSetting, 1);
  154. }
  155. },
  156. getTreeCell: function (tree) {
  157. let me = this;
  158. let indent = 20, levelIndent = -5, halfBoxLength = 5, halfExpandLength = 3, imgWidth = 14, imgHeight = 14;
  159. let TreeCell = function () {};
  160. TreeCell.prototype = new GC.Spread.Sheets.CellTypes.Text();
  161. TreeCell.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  162. if (style.backColor) {
  163. ctx.save();
  164. ctx.fillStyle = style.backColor;
  165. ctx.fillRect(x, y, w, h);
  166. ctx.restore();
  167. } else {
  168. ctx.clearRect(x, y, w, h);
  169. };
  170. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  171. ctx.save();
  172. ctx.translate(0.5, 0.5);
  173. ctx.beginPath();
  174. ctx.moveTo(x1, y1);
  175. ctx.lineTo(x2, y2);
  176. ctx.strokeStyle = color;
  177. ctx.stroke();
  178. ctx.restore();
  179. };
  180. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  181. let rect = {}, h1, h2, offset = 1;
  182. rect.top = centerY - halfBoxLength;
  183. rect.bottom = centerY + halfBoxLength;
  184. rect.left = centerX - halfBoxLength;
  185. rect.right = centerX + halfBoxLength;
  186. if (rect.left < x + w) {
  187. rect.right = Math.min(rect.right, x + w);
  188. ctx.save();
  189. ctx.translate(0.5, 0.5);
  190. ctx.strokeStyle = 'black';
  191. ctx.beginPath();
  192. ctx.moveTo(rect.left, rect.top);
  193. ctx.lineTo(rect.left, rect.bottom);
  194. ctx.lineTo(rect.right, rect.bottom);
  195. ctx.lineTo(rect.right, rect.top);
  196. ctx.lineTo(rect.left, rect.top);
  197. ctx.stroke();
  198. ctx.fillStyle = 'white';
  199. ctx.fill();
  200. ctx.restore();
  201. // Draw Horizontal Line
  202. h1 = centerX - halfExpandLength;
  203. h2 = Math.min(centerX + halfExpandLength, x + w);
  204. if (h2 > h1) {
  205. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  206. }
  207. // Draw Vertical Line
  208. if (!expanded && (centerX < x + w)) {
  209. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  210. }
  211. }
  212. };
  213. let node = tree.items[options.row];
  214. if (!node) return;
  215. let showTreeLine = true;
  216. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  217. let x1 = centerX + indent / 2;
  218. let centerY = Math.floor((y + (y + h)) / 2);
  219. let y1;
  220. // Draw Horizontal Line、Image、sibling Vertical Line
  221. if (centerX < x + w) {
  222. // Draw Horizontal Line
  223. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  224. // Draw Image
  225. let imgId;
  226. if (node.data.type === 0) imgId = 'blockLib_pic'
  227. else if (node.data.type === 1) imgId = 'folder_pic'
  228. else if (node.data.type === 2) {
  229. imgId = 'block_pic';
  230. };
  231. let img = document.getElementById(imgId);
  232. ctx.drawImage(img, centerX+indent/2+3, centerY - 7, imgWidth, imgHeight);
  233. // Draw Vertical Line
  234. y1 = node.isLast() ? centerY : y + h;
  235. if (node.isFirst() && !node.parent/*.parent*/) {
  236. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  237. } else {
  238. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  239. }
  240. }
  241. // Draw Expand Box
  242. if (node.children.length > 0) {
  243. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  244. }
  245. // Draw Parent Line
  246. var curNode = node.parent, parentCenterX = centerX - indent - levelIndent;
  247. while (curNode) {
  248. if (!curNode.isLast()) {
  249. if (parentCenterX < x + w) {
  250. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  251. }
  252. }
  253. curNode = curNode.parent;
  254. parentCenterX -= (indent + levelIndent);
  255. }
  256. // Draw Text
  257. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3;
  258. w = w - (node.depth() + 1) * indent - node.depth() * levelIndent - imgWidth - 3;
  259. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  260. };
  261. TreeCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  262. let info = {x: x, y: y, row: context.row, col: context.col, cellStyle: cellStyle, cellRect: cellRect, sheetArea: context.sheetArea};
  263. let node = tree.items[info.row];
  264. let offset = -1;
  265. let centerX = info.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  266. let text = context.sheet.getText(info.row, info.col);
  267. let value = context.sheet.getValue(info.row, info.col);
  268. let acStyle = context.sheet.getActualStyle(info.row, info.col),
  269. zoom = context.sheet.zoom();
  270. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: context.sheet, row: info.row, col: info.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  271. if(info.x > centerX + halfBoxLength && info.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength){
  272. info.isReservedLocation = true;
  273. }
  274. return info;
  275. };
  276. TreeCell.prototype.processMouseDown = function (hitinfo) {
  277. let offset = -1;
  278. let node = tree.items[hitinfo.row];
  279. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  280. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  281. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  282. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  283. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  284. zoom = hitinfo.sheet.zoom();
  285. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  286. //(图标+名字)区域
  287. function withingClickArea(){
  288. return hitinfo.x > centerX + halfBoxLength && hitinfo.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength;
  289. }
  290. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  291. node.setExpanded(!node.expanded);
  292. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  293. let iCount = node.posterityCount(), i, child;
  294. for (i = 0; i < iCount; i++) {
  295. child = tree.items[hitinfo.row + i + 1];
  296. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  297. }
  298. hitinfo.sheet.invalidateLayout();
  299. });
  300. hitinfo.sheet.repaint();
  301. }
  302. };
  303. TreeCell.prototype.processMouseMove = function (hitInfo) {
  304. let sheet = hitInfo.sheet;
  305. let div = sheet.getParent().getHost();
  306. let canvasId = div.id + "vp_vp";
  307. /* let canvas = $(`#${canvasId}`)[0];
  308. //改变鼠标图案
  309. if (sheet && hitInfo.isReservedLocation) {
  310. canvas.style.cursor='pointer';
  311. return true;
  312. }else{
  313. canvas.style.cursor='default';
  314. }*/
  315. return false;
  316. };
  317. TreeCell.prototype.processMouseEnter = function (hitinfo) {
  318. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  319. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  320. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  321. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  322. zoom = hitinfo.sheet.zoom();
  323. let node = me.mainTree.items[hitinfo.row];
  324. let nodeIndent = node ? (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3 : 0;
  325. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  326. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  327. if(textLength > cellWidth - nodeIndent){
  328. TREE_SHEET_HELPER.showTipsDiv(text,{pos: {}},hitinfo);
  329. }
  330. };
  331. TreeCell.prototype.processMouseLeave = function (hitinfo) {
  332. let me = this;
  333. TREE_SHEET_HELPER.tipDiv = 'hide';
  334. if (TREE_SHEET_HELPER._toolTipElement) {
  335. $(TREE_SHEET_HELPER._toolTipElement).hide();
  336. TREE_SHEET_HELPER._toolTipElement = null;
  337. };
  338. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  339. };
  340. return new TreeCell();
  341. },
  342. newNode: async function (nodeType, nodeName, categoryID, source){ // 1 分类(只用前两个参数) 2 块文件
  343. if (nodeName == '') return;
  344. let tree = blockLibObj.mainTree;
  345. let ID = uuid.v1();
  346. let pID = (nodeType == 2) ? categoryID : -1;
  347. let nID = -1;
  348. // 先生成临时结点数据用于提交入库,成功后才生成树结点,并在UI上刷新显示。
  349. let temp = {};
  350. temp.data = {
  351. ID: ID,
  352. ParentID: pID,
  353. NextSiblingID: nID,
  354. libID: blockLibObj.activeLib.libID,
  355. type: nodeType,
  356. nodeName: nodeName
  357. };
  358. if (nodeType == 2)
  359. blockLibObj.assignData(temp, source);
  360. try {
  361. let obj = {
  362. libID: blockLibObj.activeLib.libID,
  363. nodeID: temp.data.ID,
  364. create: temp.data
  365. };
  366. await ajaxPost('/blockLib/saveBlock', obj);
  367. let newN = tree.insertByID(ID, pID, nID);
  368. newN.data = temp.data;
  369. tree.selected = newN;
  370. let sheet = blockLibObj.mainSheet;
  371. sheet.suspendPaint();
  372. sheet.suspendEvent();
  373. let idx = tree.items.indexOf(newN);
  374. sheet.addRows(idx, 1);
  375. sheet.getRange(idx, 0, 1, 1).locked(true);
  376. sheet.setValue(idx, 0, newN.data.nodeName);
  377. sheet.setSelection(idx, 0, 1, 1);
  378. sheet.resumeEvent();
  379. sheet.resumePaint();
  380. }
  381. catch (err) {
  382. console.log(err.message);
  383. return;
  384. };
  385. },
  386. assignData: function (block, source){
  387. block.data.compilationID = source.compilationID;
  388. block.data.copyTime = source.copyTime;
  389. block.data.firstNodeType = source.firstNodeType;
  390. block.data.isFBFX = source.isFBFX;
  391. let bill = source.datas[0];
  392. block.data.code = bill.code;
  393. block.data.name = bill.name;
  394. block.data.unit = bill.unit;
  395. block.data.itemCharacterText = bill.itemCharacterText;
  396. block.data.unitFee = (bill.feesIndex && bill.feesIndex.common) ? bill.feesIndex.common.unitFee : 0;
  397. block.data.children = bill.children;
  398. for (let r of bill.children){
  399. r.unitFee = (r.feesIndex && r.feesIndex.common) ? r.feesIndex.common.unitFee : 0;
  400. if (r.programID)
  401. r.programName = projectObj.project.calcProgram.compiledTemplateMaps[r.programID];
  402. // delete r.ID; // 这个不能删!
  403. delete r.billsItemID;
  404. delete r.fees;
  405. delete r.feesIndex;
  406. };
  407. },
  408. reName: async function (node, newName){
  409. if (newName == '') return;
  410. let obj = {
  411. libID: blockLibObj.activeLib.libID,
  412. nodeID: node.data.ID,
  413. update: {nodeName: newName}
  414. };
  415. await ajaxPost('/blockLib/saveBlock', obj);
  416. node.data.nodeName = newName;
  417. let idx = blockLibObj.mainTree.items.indexOf(node);
  418. blockLibObj.mainSheet.setValue(idx, 0, newName);
  419. },
  420. moveBlock: function (parentID) {
  421. // this.mainTreeController.moveTo(parentID);
  422. },
  423. delete: async function () {
  424. let obj = {
  425. libID: blockLibObj.activeLib.libID,
  426. nodeID: blockLibObj.mainTree.selected.data.ID,
  427. delete: true
  428. };
  429. await ajaxPost('/blockLib/saveBlock', obj);
  430. this.mainTreeController.delete();
  431. },
  432. getCategories: function () {
  433. let nodes = [], node = blockLibObj.mainTree.items[0];
  434. nodes.push(node);
  435. while (node.nextSibling != null){
  436. node = node.nextSibling;
  437. nodes.push(node);
  438. };
  439. return nodes;
  440. },
  441. curIsBlock: function () {
  442. return this.mainTree.selected.data.type == 2;
  443. },
  444. curIsCategory: function () {
  445. return this.mainTree.selected.data.type == 1;
  446. },
  447. getSameNameNode: function(name){
  448. let rst = null;
  449. let nodes = blockLibObj.mainTree.items;
  450. for (let i = 0; i < nodes.length; i++) {
  451. let node = nodes[i];
  452. if (node.data.nodeName == name){
  453. rst = node;
  454. break;
  455. }
  456. }
  457. return rst;
  458. },
  459. refreshSpread: function (){
  460. if (this.mainSpread)
  461. this.mainSpread.refresh();
  462. if (this.billSpread)
  463. this.billSpread.refresh();
  464. if (this.rationSpread)
  465. this.rationSpread.refresh();
  466. },
  467. loadTreeContextMenu: function (){
  468. let me = this;
  469. $.contextMenu({
  470. selector: '#div_block_tree',
  471. build: function ($trigger, e) {
  472. SheetDataHelper.safeRightClickSelection($trigger, e, me.mainSpread);
  473. me.onEnterCell();
  474. },
  475. items: {
  476. "oneToOneClone": {
  477. name: '一对一克隆',
  478. icon: "fa-stop",
  479. disabled: function () {
  480. let ok = me.curIsBlock() && calcTools.isLeafBill(projectObj.project.mainTree.selected);
  481. return !ok;
  482. },
  483. visible: function(key, opt){
  484. return true;
  485. },
  486. callback: function (key, opt) {
  487. blockLibObj.cloneType = 1;
  488. $("#div_cloneOptions").modal({show: true});
  489. }
  490. },
  491. "oneToMoreClone": {
  492. name: '一对多克隆',
  493. icon: "fa-th-list",
  494. disabled: function () {
  495. let ok = me.curIsBlock() && calcTools.isParentBill(projectObj.project.mainTree.selected);
  496. return !ok;
  497. },
  498. visible: function(key, opt){
  499. return true;
  500. },
  501. callback: function (key, opt) {
  502. blockLibObj.cloneType = 2;
  503. $("#div_cloneOptions").modal({show: true});
  504. }
  505. },
  506. "moreToMoreClone": {
  507. name: '多对多克隆',
  508. icon: "fa-th",
  509. disabled: function () {
  510. let ok = me.curIsCategory() && calcTools.isParentBill(projectObj.project.mainTree.selected);
  511. return !ok;
  512. },
  513. visible: function(key, opt){
  514. return true;
  515. },
  516. callback: function (key, opt) {
  517. blockLibObj.cloneType = 3;
  518. $("#div_cloneOptions").modal({show: true});
  519. }
  520. },
  521. "delete": {
  522. name: '删除',
  523. icon: "delete",
  524. disabled: function () {
  525. },
  526. visible: function(key, opt){
  527. return true;
  528. },
  529. callback: function (key, opt) {
  530. let name = hintBox.fontRed(me.mainTree.selected.data.nodeName);
  531. hintBox.infoBox('操作确认', `确定要删除"${name}"吗?`, 2, function () {
  532. me.delete();
  533. });
  534. }
  535. },
  536. "moveBlock": {
  537. name: '移动模板',
  538. icon: "cut",
  539. disabled: function () {
  540. return true;
  541. },
  542. visible: function(key, opt){
  543. // return me.curIsBlock();
  544. return false;
  545. },
  546. callback: function (key, opt) {
  547. }
  548. }
  549. }
  550. });
  551. },
  552. onEnterCell: function (sender, args) {
  553. let me = blockLibObj;
  554. me.mainTree.selected = me.mainTree.items[me.mainSheet.getActiveRowIndex()];
  555. },
  556. onCellDoubleClick: function (sender, args) {
  557. let projectNode = projectObj.project.mainTree.selected;
  558. if (!calcTools.isLeafBill(projectNode)) return;
  559. blockLibObj.cloneType = 1;
  560. $("#div_cloneOptions").modal({show: true});
  561. },
  562. oneToOneClone: function (projectNode, block, options) {
  563. let canClone = true;
  564. if (options.checkCode)
  565. canClone = canClone && (projectNode.data.code.substr(0, 9) == block.data.code.substr(0, 9));
  566. if (options.checkName)
  567. canClone = canClone && (projectNode.data.name == block.data.name);
  568. if (options.checkUnit)
  569. canClone = canClone && (projectNode.data.unit == block.data.unit);
  570. if (!canClone) return;
  571. if (options.overwriteRations)
  572. projectObj.project.Bills.deleteChildren(projectNode);
  573. /* 这里封装成伟城的块文件格式,可直接使用伟城的“粘贴块”接口。
  574. 但这里结构要作出调整:忽略叶子清单层,直接从定额开始(跟粘贴块有区别),始终强制在叶子清单下插入定额。
  575. 该操作前提:当前块文件的全部数据已从后台取到前台。 */
  576. let vBlock_WC = {
  577. compilationID: block.data.compilationID,
  578. copyTime: block.data.copyTime,
  579. firstNodeType: 1, // 强制改成1 (因为是从清单下的定额开始。清单自身的还是保留,暂不使用使用)。
  580. isFBFX: block.data.isFBFX,
  581. zeroQuantity: options.zeroQuantity,
  582. datas: block.data.children // rations
  583. };
  584. vBlock_WC = JSON.parse(JSON.stringify(vBlock_WC));
  585. BlockController.confirmPaste(vBlock_WC, projectNode, 'sub');
  586. },
  587. checkShow: async function () { // 这里需要处理异步:模板库装载完再弹出位置选择窗。
  588. if (!$("#kmbk").is(":visible")){ // 如果还没显示
  589. if (!blockLibObj.mainSpread){
  590. await blockLibObj.buildSheet();
  591. };
  592. $('#blockLibTab').click(); // 强制显示
  593. };
  594. $("#div_createBlocks").modal({show: true});
  595. }
  596. };
  597. $(document).ready(function(){ // 这里不需要处理异步:因为不需要弹出位置选择窗。
  598. $('#blockLibTab').on('click', function (){
  599. if ($("#kmbk").is(":visible")){ // 显示状态下
  600. if (!blockLibObj.mainSpread){
  601. blockLibObj.buildSheet();
  602. };
  603. }
  604. });
  605. $('#btn_block_newFolder').on('click', function (){
  606. $('#input_block_newFolder').val('');
  607. });
  608. $('#btn_block_newFolder_add').on('click', function (){
  609. let name = $('#input_block_newFolder').val();
  610. blockLibObj.newNode(1, name);
  611. });
  612. $('#btn_block_reName').on('click', function (){
  613. let select = blockLibObj.mainTree.selected;
  614. $('#input_block_reName').val(select.data.nodeName);
  615. });
  616. $('#btn_block_reName_OK').on('click', function (){
  617. let select = blockLibObj.mainTree.selected;
  618. let oldName = select.data.nodeName;
  619. let newName = $('#input_block_reName').val();
  620. if (oldName != newName) blockLibObj.reName(select, newName);
  621. });
  622. });