tree_sheet_helper.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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]]) {
  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 (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  167. cell.locked(colSetting.readOnly(node));
  168. } else {
  169. cell.locked(true);
  170. }
  171. } else {
  172. cell.locked(false);
  173. }
  174. });
  175. if(setting.setAutoFitRow){
  176. setting.setAutoFitRow(sheet,node)
  177. }
  178. if (recursive) {
  179. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  180. }
  181. });
  182. },
  183. refreshChildrenVisiable:function(sheet,tree,node,row,visiable){
  184. let iCount = node.posterityCount(), i, child;
  185. for (i = 0; i < iCount; i++) {
  186. child = tree.items[row + i +1];
  187. sheet.setRowVisible(row + i + 1, visiable?visiable:child.visible, GC.Spread.Sheets.SheetArea.viewport);
  188. }
  189. sheet.invalidateLayout();
  190. },
  191. showTreeData: function (setting, sheet, tree) {
  192. let indent = 20;
  193. let levelIndent = -5;
  194. let halfBoxLength = 5;
  195. let halfExpandLength = 3;
  196. let TreeNodeCellType = function () {
  197. };
  198. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  199. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  200. if (style.backColor) {
  201. ctx.save();
  202. ctx.fillStyle = style.backColor;
  203. ctx.fillRect(x, y, w, h);
  204. ctx.restore();
  205. } else {
  206. ctx.clearRect(x, y, w, h);
  207. }
  208. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  209. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  210. ctx.save();
  211. // ����ƫ����
  212. ctx.translate(0.5, 0.5);
  213. ctx.beginPath();
  214. ctx.moveTo(x1, y1);
  215. ctx.lineTo(x2, y2);
  216. ctx.strokeStyle = color;
  217. ctx.stroke();
  218. ctx.restore();
  219. };
  220. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  221. let rect = {}, h1, h2, offset = 1;
  222. rect.top = centerY - halfBoxLength;
  223. rect.bottom = centerY + halfBoxLength;
  224. rect.left = centerX - halfBoxLength;
  225. rect.right = centerX + halfBoxLength;
  226. if (rect.left < x + w) {
  227. rect.right = Math.min(rect.right, x + w);
  228. ctx.save();
  229. // ����ƫ����
  230. ctx.translate(0.5, 0.5);
  231. ctx.strokeStyle = 'black';
  232. ctx.beginPath();
  233. ctx.moveTo(rect.left, rect.top);
  234. ctx.lineTo(rect.left, rect.bottom);
  235. ctx.lineTo(rect.right, rect.bottom);
  236. ctx.lineTo(rect.right, rect.top);
  237. ctx.lineTo(rect.left, rect.top);
  238. ctx.stroke();
  239. ctx.fillStyle = 'white';
  240. ctx.fill();
  241. ctx.restore();
  242. // Draw Horizontal Line
  243. h1 = centerX - halfExpandLength;
  244. h2 = Math.min(centerX + halfExpandLength, x + w);
  245. if (h2 > h1) {
  246. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  247. }
  248. // Draw Vertical Line
  249. if (!expanded && (centerX < x + w)) {
  250. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  251. }
  252. }
  253. }
  254. let node = tree.items[options.row];
  255. let showTreeLine = true;
  256. if (!node) { return; }
  257. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  258. let x1 = centerX + indent / 2;
  259. let centerY = Math.floor((y + (y + h)) / 2);
  260. let y1;
  261. // Draw Sibling Line
  262. if (showTreeLine) {
  263. // Draw Horizontal Line
  264. if (centerX < x + w) {
  265. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  266. }
  267. // Draw Vertical Line
  268. if (centerX < x + w) {
  269. y1 = node.isLast() ? centerY : y + h;
  270. if (node.isFirst() && !node.parent) {
  271. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  272. } else {
  273. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  274. }
  275. }
  276. }
  277. // Draw Expand Box
  278. if (node.children.length > 0) {
  279. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  280. }
  281. // Draw Parent Line
  282. if (showTreeLine) {
  283. var parent = node.parent, parentCenterX = centerX - indent - levelIndent;
  284. while (parent) {
  285. if (!parent.isLast()) {
  286. if (parentCenterX < x + w) {
  287. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  288. }
  289. }
  290. parent = parent.parent;
  291. parentCenterX -= (indent + levelIndent);
  292. }
  293. };
  294. // Draw Text
  295. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent;
  296. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  297. };
  298. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  299. return {
  300. x: x,
  301. y: y,
  302. row: context.row,
  303. col: context.col,
  304. cellStyle: cellStyle,
  305. cellRect: cellRect,
  306. sheetArea: context.sheetArea
  307. };
  308. };
  309. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  310. let offset = -1;
  311. let node = tree.items[hitinfo.row];
  312. tree.selected = node;
  313. if (!node || node.children.length === 0) { return; }
  314. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  315. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  316. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  317. console.log(`hitinfo.sheet.name()`);
  318. console.log(hitinfo.sheet.name());
  319. node.setExpanded(!node.expanded);
  320. let sheetName = hitinfo.sheet.name();
  321. if(sheetName === 'stdBillsLib_bills'){
  322. sessionStorage.setItem('stdBillsLibExpState', billsLibObj.stdBillsTree.getExpState(billsLibObj.stdBillsTree.items));
  323. }
  324. else if(sheetName === 'stdRationLib_chapter'){
  325. sessionStorage.setItem('stdRationLibExpState', rationLibObj.tree.getExpState(rationLibObj.tree.items));
  326. }
  327. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  328. let iCount = node.posterityCount(), i, child;
  329. for (i = 0; i < iCount; i++) {
  330. child = tree.items[hitinfo.row + i + 1];
  331. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  332. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  333. }
  334. hitinfo.sheet.invalidateLayout();
  335. });
  336. hitinfo.sheet.repaint();
  337. }
  338. };
  339. let TipCellType = function () {};
  340. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  341. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  342. return {
  343. x: x,
  344. y: y,
  345. row: context.row,
  346. col: context.col,
  347. cellStyle: cellStyle,
  348. cellRect: cellRect,
  349. sheet: context.sheet,
  350. sheetArea: context.sheetArea
  351. };
  352. };
  353. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  354. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  355. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  356. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  357. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  358. zoom = hitinfo.sheet.zoom();
  359. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  360. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  361. let dataField = setting.cols[hitinfo.col].data.field;
  362. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).wordWrap()==true){
  363. return;
  364. }
  365. if(dataField === 'name' || dataField === 'itemCharacterText' || dataField === 'jobContentText' || dataField === 'adjustState'){
  366. if((hitinfo.sheet.getParent() === projectObj.mainSpread||hitinfo.sheet.getParent() === tender_obj.tenderSpread) && textLength <= cellWidth)
  367. return;
  368. }
  369. if(dataField=="quantity"){
  370. text = tag;
  371. }else if(tag !== undefined && tag) {
  372. text = tag;
  373. }
  374. if (setting.pos && text && text !== '') {
  375. if (!this._toolTipElement) {
  376. let div = $('#autoTip')[0];
  377. if (!div) {
  378. div = document.createElement("div");
  379. $(div).css("position", "absolute")
  380. .css("border", "1px #C0C0C0 solid")
  381. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  382. .css("font", "9pt Arial")
  383. .css("background", "white")
  384. .css("padding", 5)
  385. .attr("id", 'autoTip');
  386. $(div).hide();
  387. document.body.insertBefore(div, null);
  388. }
  389. this._toolTipElement = div;
  390. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  391. $(this._toolTipElement).show("fast");
  392. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  393. }
  394. }
  395. };
  396. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  397. let me = this;
  398. TREE_SHEET_HELPER.tipDiv = 'hide';
  399. if (me._toolTipElement) {
  400. $(me._toolTipElement).hide();
  401. me._toolTipElement = null;
  402. };
  403. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  404. }
  405. TREE_SHEET_HELPER.protectdSheet(sheet);
  406. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  407. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  408. sheet.showRowOutline(false);
  409. if (setting.defaultRowHeight) {
  410. sheet.defaults.rowHeight = setting.defaultRowHeight;
  411. }
  412. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  413. sheet.getRange(tree.count(), -1, setting.emptyRows, -1).locked(true);
  414. setting.cols.forEach(function (colSetting, iCol) {
  415. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  416. if (colSetting.showHint) {
  417. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  418. }
  419. if(colSetting.formatter){
  420. sheet.setFormatter(-1, iCol, colSetting.formatter, GC.Spread.Sheets.SheetArea.viewport);
  421. }
  422. });
  423. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  424. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  425. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  426. });
  427. },
  428. tipDivCheck(){
  429. setTimeout(function () {
  430. let tips = $('#autoTip');
  431. if(TREE_SHEET_HELPER.tipDiv == 'show'){
  432. return;
  433. } else if(TREE_SHEET_HELPER.tipDiv == 'hide'&&tips){
  434. tips.hide();
  435. TREE_SHEET_HELPER._toolTipElement = null;
  436. }
  437. },600)
  438. }
  439. };