tree_sheet_helper.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_HELPER = {
  5. getObjPos: function (obj) {
  6. let target = obj;
  7. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  8. target = obj.offsetParent;
  9. while (target) {
  10. pos.x += target.offsetLeft;
  11. pos.y += target.offsetTop;
  12. target = target.offsetParent;
  13. }
  14. return pos;
  15. },
  16. /**
  17. * 初始化setting,需要提示单元格text时,必须先初始化setting
  18. * @param obj
  19. * @param setting
  20. */
  21. initSetting: function (obj, setting) {
  22. setting.pos = this.getObjPos(obj);
  23. },
  24. createNewSpread: function (obj) {
  25. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  26. spread.options.tabStripVisible = false;
  27. spread.options.scrollbarMaxAlign = true;
  28. spread.options.cutCopyIndicatorVisible = false;
  29. spread.options.allowCopyPasteExcelStyle = false;
  30. spread.options.allowUserDragDrop = false;
  31. spread.getActiveSheet().setRowCount(3);
  32. return spread;
  33. },
  34. getSheetCellStyle: function (setting) {
  35. var style = new GC.Spread.Sheets.Style();
  36. //style.locked = setting.readOnly ? true : false;
  37. style.name = setting.id;
  38. style.font = setting.data.font;
  39. style.hAlign = setting.data.hAlign;
  40. style.vAlign = setting.data.vAlign;
  41. style.wordWrap = setting.data.wordWrap;
  42. if (setting.data.formatter) {
  43. style.formatter = setting.data.formatter;
  44. }
  45. return style;
  46. },
  47. loadSheetHeader: function (setting, sheet) {
  48. this.massOperationSheet(sheet, function () {
  49. if (setting.frozenCols) {
  50. sheet.frozenColumnCount(setting.frozenCols);
  51. }
  52. sheet.setColumnCount(setting.cols.length);
  53. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  54. setting.headRowHeight.forEach(function (rowHeight, index) {
  55. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  56. });
  57. setting.cols.forEach(function (col, index) {
  58. var i, iRow = 0, cell;
  59. for (i = 0; i < col.head.spanCols.length; i++) {
  60. if (col.head.spanCols[i] !== 0) {
  61. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  62. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  63. }
  64. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  65. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  66. }
  67. iRow += col.head.spanRows[i];
  68. };
  69. sheet.setColumnWidth(index, col.width);
  70. sheet.setColumnVisible(index, col.visible && true);
  71. });
  72. });
  73. },
  74. protectdSheet: function (sheet) {
  75. var option = {
  76. allowSelectLockedCells: true,
  77. allowSelectUnlockedCells: true,
  78. allowResizeRows: true,
  79. allowResizeColumns: true
  80. };
  81. sheet.options.protectionOptions = option;
  82. sheet.options.isProtected = true;
  83. sheet.options.allowCellOverflow = false;
  84. },
  85. massOperationSheet: function (sheet, Operation) {
  86. sheet.suspendPaint();
  87. sheet.suspendEvent();
  88. Operation();
  89. sheet.resumeEvent();
  90. sheet.resumePaint();
  91. },
  92. refreshNodesVisible: function (nodes, sheet, recursive) {
  93. nodes.forEach(function (node) {
  94. var iRow;
  95. iRow = node.serialNo();
  96. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  97. if (recursive) {
  98. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  99. }
  100. })
  101. },
  102. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  103. nodes.forEach(function (node) {
  104. let iRow = node.serialNo();
  105. if(typeof projectObj !== 'undefined'){
  106. let nodeStyle = projectObj.getNodeColorStyle(sheet, node);
  107. if(node.data.bgColour){
  108. nodeStyle.backColor = node.data.bgColour;
  109. }
  110. if(nodeStyle){
  111. sheet.setStyle(iRow, -1, nodeStyle);
  112. }
  113. }
  114. setting.cols.forEach(function (colSetting, iCol) {
  115. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  116. if(typeof projectObj !== 'undefined'){
  117. let boldFontStyle = projectObj.getBoldFontStyle(node, colSetting);
  118. if(boldFontStyle){
  119. sheet.setStyle(iRow, iCol, boldFontStyle);
  120. }
  121. }
  122. // var getFieldText = function () {
  123. // var fields = colSetting.data.field.split('.');
  124. // var validField = fields.reduce(function (field1, field2) {
  125. // if (eval('node.data.' + field1)) {
  126. // return field1 + '.' + field2
  127. // } else {
  128. // return field1;
  129. // }
  130. // });
  131. // if (eval('node.data.' + validField)) {
  132. // return eval('node.data.' + validField);
  133. // } else {
  134. // return '';
  135. // }
  136. // };
  137. var getFieldText2 = function () {
  138. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  139. for (iField = 0; iField < fields.length; iField++) {
  140. if (data[fields[iField]] && data[fields[iField]]!='0') {
  141. data = data[fields[iField]];
  142. } else {
  143. return '';
  144. }
  145. }
  146. return data;
  147. };
  148. if(colSetting.data.field=="quantity"){
  149. let tag = node.data.quantityEXP?node.data.quantityEXP:'';
  150. sheet.setTag(iRow, iCol,tag);
  151. }
  152. if (colSetting.data.getText && Object.prototype.toString.apply(colSetting.data.getText) === "[object Function]") {
  153. cell.value(colSetting.data.getText(node));
  154. } else {
  155. cell.value(getFieldText2());
  156. }
  157. if (colSetting.data.cellType && Object.prototype.toString.apply(colSetting.data.cellType) !== "[object String]") {
  158. cell.cellType(colSetting.data.cellType(node));
  159. }
  160. if(colSetting.data.autoHeight == true){
  161. colSetting.setAutoHeight(cell,node);
  162. }
  163. if(colSetting.editChecking&&colSetting.editChecking(node)){
  164. cell.locked(true);
  165. }else if (colSetting.readOnly) {
  166. if(typeof projectReadOnly !== 'undefined' && projectReadOnly){
  167. cell.locked(true);
  168. }else {
  169. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  170. cell.locked(colSetting.readOnly(node));
  171. } else {
  172. cell.locked(true);
  173. }
  174. }
  175. } else {
  176. cell.locked(typeof projectReadOnly !== 'undefined' && projectReadOnly ? true : false);
  177. }
  178. });
  179. if(setting.setAutoFitRow){
  180. setting.setAutoFitRow(sheet,node)
  181. }
  182. if (recursive) {
  183. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  184. }
  185. });
  186. },
  187. refreshChildrenVisiable:function(sheet,tree,node,row,visiable){
  188. let iCount = node.posterityCount(), i, child;
  189. for (i = 0; i < iCount; i++) {
  190. child = tree.items[row + i +1];
  191. sheet.setRowVisible(row + i + 1, visiable?visiable:child.visible, GC.Spread.Sheets.SheetArea.viewport);
  192. }
  193. sheet.invalidateLayout();
  194. },
  195. showTreeData: function (setting, sheet, tree) {
  196. let indent = 20;
  197. let levelIndent = -5;
  198. let halfBoxLength = 5;
  199. let halfExpandLength = 3;
  200. let TreeNodeCellType = function () {
  201. };
  202. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  203. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  204. if (style.backColor) {
  205. ctx.save();
  206. ctx.fillStyle = style.backColor;
  207. ctx.fillRect(x, y, w, h);
  208. ctx.restore();
  209. } else {
  210. ctx.clearRect(x, y, w, h);
  211. }
  212. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  213. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  214. ctx.save();
  215. // ����ƫ����
  216. ctx.translate(0.5, 0.5);
  217. ctx.beginPath();
  218. ctx.moveTo(x1, y1);
  219. ctx.lineTo(x2, y2);
  220. ctx.strokeStyle = color;
  221. ctx.stroke();
  222. ctx.restore();
  223. };
  224. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  225. let rect = {}, h1, h2, offset = 1;
  226. rect.top = centerY - halfBoxLength;
  227. rect.bottom = centerY + halfBoxLength;
  228. rect.left = centerX - halfBoxLength;
  229. rect.right = centerX + halfBoxLength;
  230. if (rect.left < x + w) {
  231. rect.right = Math.min(rect.right, x + w);
  232. ctx.save();
  233. // ����ƫ����
  234. ctx.translate(0.5, 0.5);
  235. ctx.strokeStyle = 'black';
  236. ctx.beginPath();
  237. ctx.moveTo(rect.left, rect.top);
  238. ctx.lineTo(rect.left, rect.bottom);
  239. ctx.lineTo(rect.right, rect.bottom);
  240. ctx.lineTo(rect.right, rect.top);
  241. ctx.lineTo(rect.left, rect.top);
  242. ctx.stroke();
  243. ctx.fillStyle = 'white';
  244. ctx.fill();
  245. ctx.restore();
  246. // Draw Horizontal Line
  247. h1 = centerX - halfExpandLength;
  248. h2 = Math.min(centerX + halfExpandLength, x + w);
  249. if (h2 > h1) {
  250. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  251. }
  252. // Draw Vertical Line
  253. if (!expanded && (centerX < x + w)) {
  254. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  255. }
  256. }
  257. }
  258. let node = tree.items[options.row];
  259. let showTreeLine = true;
  260. if (!node) { return; }
  261. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  262. let x1 = centerX + indent / 2;
  263. let centerY = Math.floor((y + (y + h)) / 2);
  264. let y1;
  265. // Draw Sibling Line
  266. if (showTreeLine) {
  267. // Draw Horizontal Line
  268. if (centerX < x + w) {
  269. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  270. }
  271. // Draw Vertical Line
  272. if (centerX < x + w) {
  273. y1 = node.isLast() ? centerY : y + h;
  274. if (node.isFirst() && !node.parent) {
  275. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  276. } else {
  277. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  278. }
  279. }
  280. }
  281. // Draw Expand Box
  282. if (node.children.length > 0) {
  283. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  284. }
  285. // Draw Parent Line
  286. if (showTreeLine) {
  287. var parent = node.parent, parentCenterX = centerX - indent - levelIndent;
  288. while (parent) {
  289. if (!parent.isLast()) {
  290. if (parentCenterX < x + w) {
  291. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  292. }
  293. }
  294. parent = parent.parent;
  295. parentCenterX -= (indent + levelIndent);
  296. }
  297. };
  298. // Draw Text
  299. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent;
  300. w = w - (node.depth() + 1) * indent - node.depth() * levelIndent;
  301. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  302. };
  303. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  304. return {
  305. x: x,
  306. y: y,
  307. row: context.row,
  308. col: context.col,
  309. cellStyle: cellStyle,
  310. cellRect: cellRect,
  311. sheetArea: context.sheetArea
  312. };
  313. };
  314. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  315. let offset = -1;
  316. let node = tree.items[hitinfo.row];
  317. tree.selected = node;
  318. if (!node || node.children.length === 0) { return; }
  319. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  320. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  321. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  322. node.setExpanded(!node.expanded);
  323. let sheetName = hitinfo.sheet.name();
  324. if(sheetName === 'stdBillsLib_bills'){
  325. sessionStorage.setItem('stdBillsLibExpState', billsLibObj.stdBillsTree.getExpState(billsLibObj.stdBillsTree.items));
  326. }
  327. else if(sheetName === 'stdRationLib_chapter'){
  328. sessionStorage.setItem('stdRationLibExpState', rationLibObj.tree.getExpState(rationLibObj.tree.items));
  329. }
  330. else if(sheetName === 'stdBillsGuidance_bills'){
  331. sessionStorage.setItem('stdBillsGuidanceExpState', billsGuidance.bills.tree.getExpState(billsGuidance.bills.tree.items));
  332. }
  333. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  334. let iCount = node.posterityCount(), i, child;
  335. for (i = 0; i < iCount; i++) {
  336. child = tree.items[hitinfo.row + i + 1];
  337. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  338. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  339. }
  340. hitinfo.sheet.invalidateLayout();
  341. });
  342. hitinfo.sheet.repaint();
  343. }
  344. };
  345. let TipCellType = function () {};
  346. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  347. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  348. return {
  349. x: x,
  350. y: y,
  351. row: context.row,
  352. col: context.col,
  353. cellStyle: cellStyle,
  354. cellRect: cellRect,
  355. sheet: context.sheet,
  356. sheetArea: context.sheetArea
  357. };
  358. };
  359. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  360. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  361. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  362. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  363. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  364. zoom = hitinfo.sheet.zoom();
  365. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  366. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  367. let dataField = setting.cols[hitinfo.col].data.field;
  368. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).wordWrap()==true){
  369. return;
  370. }
  371. if(dataField === 'name' || dataField === 'itemCharacterText' || dataField === 'jobContentText' || dataField === 'adjustState'){
  372. if((hitinfo.sheet.getParent() === projectObj.mainSpread||hitinfo.sheet.getParent() === tender_obj.tenderSpread) && textLength <= cellWidth)
  373. return;
  374. }
  375. if(dataField=="quantity"){
  376. text = tag;
  377. }else if(tag !== undefined && tag) {
  378. text = tag;
  379. }
  380. if (setting.pos && text && text !== '') {
  381. if (!this._toolTipElement) {
  382. let div = $('#autoTip')[0];
  383. if (!div) {
  384. div = document.createElement("div");
  385. $(div).css("position", "absolute")
  386. .css("border", "1px #C0C0C0 solid")
  387. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  388. .css("font", "9pt Arial")
  389. .css("background", "white")
  390. .css("padding", 5)
  391. .attr("id", 'autoTip');
  392. $(div).hide();
  393. document.body.insertBefore(div, null);
  394. }
  395. this._toolTipElement = div;
  396. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  397. $(this._toolTipElement).show("fast");
  398. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  399. }
  400. }
  401. };
  402. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  403. let me = this;
  404. TREE_SHEET_HELPER.tipDiv = 'hide';
  405. if (me._toolTipElement) {
  406. $(me._toolTipElement).hide();
  407. me._toolTipElement = null;
  408. };
  409. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  410. }
  411. TREE_SHEET_HELPER.protectdSheet(sheet);
  412. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  413. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  414. sheet.showRowOutline(false);
  415. if (setting.defaultRowHeight) {
  416. sheet.defaults.rowHeight = setting.defaultRowHeight;
  417. }
  418. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  419. sheet.getRange(tree.count(), -1, setting.emptyRows, -1).locked(true);
  420. setting.cols.forEach(function (colSetting, iCol) {
  421. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  422. if (colSetting.showHint) {
  423. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  424. }
  425. if(colSetting.formatter){
  426. sheet.setFormatter(-1, iCol, colSetting.formatter, GC.Spread.Sheets.SheetArea.viewport);
  427. }
  428. });
  429. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  430. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  431. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  432. });
  433. },
  434. tipDivCheck(){
  435. setTimeout(function () {
  436. let tips = $('#autoTip');
  437. if(TREE_SHEET_HELPER.tipDiv == 'show'){
  438. return;
  439. } else if(TREE_SHEET_HELPER.tipDiv == 'hide'&&tips){
  440. tips.hide();
  441. TREE_SHEET_HELPER._toolTipElement = null;
  442. }
  443. },600)
  444. }
  445. };