tree_sheet_helper.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. },
  84. massOperationSheet: function (sheet, Operation) {
  85. sheet.suspendPaint();
  86. sheet.suspendEvent();
  87. Operation();
  88. sheet.resumeEvent();
  89. sheet.resumePaint();
  90. },
  91. refreshNodesVisible: function (nodes, sheet, recursive) {
  92. nodes.forEach(function (node) {
  93. var iRow;
  94. iRow = node.serialNo();
  95. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  96. if (recursive) {
  97. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  98. }
  99. })
  100. },
  101. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  102. nodes.forEach(function (node) {
  103. let iRow = node.serialNo();
  104. if(typeof projectObj !== 'undefined'){
  105. let nodeStyle = projectObj.getNodeColorStyle(sheet, node);
  106. if(node.data.bgColour){
  107. nodeStyle.backColor = node.data.bgColour;
  108. }
  109. if(nodeStyle){
  110. sheet.setStyle(iRow, -1, nodeStyle);
  111. }
  112. }
  113. setting.cols.forEach(function (colSetting, iCol) {
  114. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  115. if(typeof projectObj !== 'undefined'){
  116. let boldFontStyle = projectObj.getBoldFontStyle(node, colSetting);
  117. if(boldFontStyle){
  118. sheet.setStyle(iRow, iCol, boldFontStyle);
  119. }
  120. }
  121. // var getFieldText = function () {
  122. // var fields = colSetting.data.field.split('.');
  123. // var validField = fields.reduce(function (field1, field2) {
  124. // if (eval('node.data.' + field1)) {
  125. // return field1 + '.' + field2
  126. // } else {
  127. // return field1;
  128. // }
  129. // });
  130. // if (eval('node.data.' + validField)) {
  131. // return eval('node.data.' + validField);
  132. // } else {
  133. // return '';
  134. // }
  135. // };
  136. var getFieldText2 = function () {
  137. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  138. for (iField = 0; iField < fields.length; iField++) {
  139. if (data[fields[iField]]) {
  140. data = data[fields[iField]];
  141. } else {
  142. return '';
  143. }
  144. }
  145. return data;
  146. };
  147. if(colSetting.data.field=="quantity"){
  148. let tag = node.data.quantityEXP?node.data.quantityEXP:'';
  149. sheet.setTag(iRow, iCol,tag);
  150. }
  151. if (colSetting.data.getText && Object.prototype.toString.apply(colSetting.data.getText) === "[object Function]") {
  152. cell.value(colSetting.data.getText(node));
  153. } else {
  154. cell.value(getFieldText2());
  155. }
  156. if (colSetting.data.cellType && Object.prototype.toString.apply(colSetting.data.cellType) !== "[object String]") {
  157. cell.cellType(colSetting.data.cellType(node));
  158. }
  159. if(colSetting.data.autoHeight == true){
  160. colSetting.setAutoHeight(cell,node);
  161. }
  162. if(colSetting.editChecking&&colSetting.editChecking(node)){
  163. cell.locked(true);
  164. }else if (colSetting.readOnly) {
  165. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  166. cell.locked(colSetting.readOnly(node));
  167. } else {
  168. cell.locked(true);
  169. }
  170. } else {
  171. cell.locked(false);
  172. }
  173. });
  174. if(setting.setAutoFitRow){
  175. setting.setAutoFitRow(sheet,node)
  176. }
  177. if (recursive) {
  178. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  179. }
  180. });
  181. },
  182. refreshChildrenVisiable:function(sheet,tree,node,row){
  183. let iCount = node.posterityCount(), i, child;
  184. for (i = 0; i < iCount; i++) {
  185. child = tree.items[row + i + 1];
  186. sheet.setRowVisible(row + i + 1, child.visible, GC.Spread.Sheets.SheetArea.viewport);
  187. }
  188. sheet.invalidateLayout();
  189. },
  190. showTreeData: function (setting, sheet, tree) {
  191. let indent = 20;
  192. let levelIndent = -5;
  193. let halfBoxLength = 5;
  194. let halfExpandLength = 3;
  195. let TreeNodeCellType = function () {
  196. };
  197. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  198. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  199. if (style.backColor) {
  200. ctx.save();
  201. ctx.fillStyle = style.backColor;
  202. ctx.fillRect(x, y, w, h);
  203. ctx.restore();
  204. } else {
  205. ctx.clearRect(x, y, w, h);
  206. }
  207. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  208. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  209. ctx.save();
  210. // ����ƫ����
  211. ctx.translate(0.5, 0.5);
  212. ctx.beginPath();
  213. ctx.moveTo(x1, y1);
  214. ctx.lineTo(x2, y2);
  215. ctx.strokeStyle = color;
  216. ctx.stroke();
  217. ctx.restore();
  218. };
  219. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  220. let rect = {}, h1, h2, offset = 1;
  221. rect.top = centerY - halfBoxLength;
  222. rect.bottom = centerY + halfBoxLength;
  223. rect.left = centerX - halfBoxLength;
  224. rect.right = centerX + halfBoxLength;
  225. if (rect.left < x + w) {
  226. rect.right = Math.min(rect.right, x + w);
  227. ctx.save();
  228. // ����ƫ����
  229. ctx.translate(0.5, 0.5);
  230. ctx.strokeStyle = 'black';
  231. ctx.beginPath();
  232. ctx.moveTo(rect.left, rect.top);
  233. ctx.lineTo(rect.left, rect.bottom);
  234. ctx.lineTo(rect.right, rect.bottom);
  235. ctx.lineTo(rect.right, rect.top);
  236. ctx.lineTo(rect.left, rect.top);
  237. ctx.stroke();
  238. ctx.fillStyle = 'white';
  239. ctx.fill();
  240. ctx.restore();
  241. // Draw Horizontal Line
  242. h1 = centerX - halfExpandLength;
  243. h2 = Math.min(centerX + halfExpandLength, x + w);
  244. if (h2 > h1) {
  245. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  246. }
  247. // Draw Vertical Line
  248. if (!expanded && (centerX < x + w)) {
  249. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  250. }
  251. }
  252. }
  253. let node = tree.items[options.row];
  254. let showTreeLine = true;
  255. if (!node) { return; }
  256. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  257. let x1 = centerX + indent / 2;
  258. let centerY = Math.floor((y + (y + h)) / 2);
  259. let y1;
  260. // Draw Sibling Line
  261. if (showTreeLine) {
  262. // Draw Horizontal Line
  263. if (centerX < x + w) {
  264. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  265. }
  266. // Draw Vertical Line
  267. if (centerX < x + w) {
  268. y1 = node.isLast() ? centerY : y + h;
  269. if (node.isFirst() && !node.parent) {
  270. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  271. } else {
  272. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  273. }
  274. }
  275. }
  276. // Draw Expand Box
  277. if (node.children.length > 0) {
  278. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  279. }
  280. // Draw Parent Line
  281. if (showTreeLine) {
  282. var parent = node.parent, parentCenterX = centerX - indent - levelIndent;
  283. while (parent) {
  284. if (!parent.isLast()) {
  285. if (parentCenterX < x + w) {
  286. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  287. }
  288. }
  289. parent = parent.parent;
  290. parentCenterX -= (indent + levelIndent);
  291. }
  292. };
  293. // Draw Text
  294. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent;
  295. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  296. };
  297. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  298. return {
  299. x: x,
  300. y: y,
  301. row: context.row,
  302. col: context.col,
  303. cellStyle: cellStyle,
  304. cellRect: cellRect,
  305. sheetArea: context.sheetArea
  306. };
  307. };
  308. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  309. let offset = -1;
  310. let node = tree.items[hitinfo.row];
  311. tree.selected = node;
  312. if (!node || node.children.length === 0) { return; }
  313. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  314. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  315. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  316. node.setExpanded(!node.expanded);
  317. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  318. let iCount = node.posterityCount(), i, child;
  319. for (i = 0; i < iCount; i++) {
  320. child = tree.items[hitinfo.row + i + 1];
  321. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  322. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  323. }
  324. hitinfo.sheet.invalidateLayout();
  325. });
  326. hitinfo.sheet.repaint();
  327. }
  328. };
  329. let TipCellType = function () {};
  330. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  331. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  332. return {
  333. x: x,
  334. y: y,
  335. row: context.row,
  336. col: context.col,
  337. cellStyle: cellStyle,
  338. cellRect: cellRect,
  339. sheet: context.sheet,
  340. sheetArea: context.sheetArea
  341. };
  342. };
  343. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  344. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  345. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  346. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  347. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  348. zoom = hitinfo.sheet.zoom();
  349. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  350. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  351. let dataField = setting.cols[hitinfo.col].data.field;
  352. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).wordWrap()==true){
  353. return;
  354. }
  355. if(dataField === 'name' || dataField === 'itemCharacterText' || dataField === 'jobContentText' || dataField === 'adjustState'){
  356. if(hitinfo.sheet.getParent() === projectObj.mainSpread && textLength <= cellWidth)
  357. return;
  358. }
  359. if(dataField=="quantity"){
  360. text = tag;
  361. }else if(tag !== undefined && tag) {
  362. text = tag;
  363. }
  364. if (setting.pos && text && text !== '') {
  365. if (!this._toolTipElement) {
  366. let div = $('#autoTip')[0];
  367. if (!div) {
  368. div = document.createElement("div");
  369. $(div).css("position", "absolute")
  370. .css("border", "1px #C0C0C0 solid")
  371. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  372. .css("font", "9pt Arial")
  373. .css("background", "white")
  374. .css("padding", 5)
  375. .attr("id", 'autoTip');
  376. $(div).hide();
  377. document.body.insertBefore(div, null);
  378. }
  379. this._toolTipElement = div;
  380. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  381. $(this._toolTipElement).show("fast");
  382. TREE_SHEET_HELPER.tipDiv = 'show';//做个标记
  383. }
  384. }
  385. };
  386. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  387. let me = this;
  388. TREE_SHEET_HELPER.tipDiv = 'hide';
  389. if (me._toolTipElement) {
  390. $(me._toolTipElement).hide();
  391. me._toolTipElement = null;
  392. };
  393. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  394. }
  395. TREE_SHEET_HELPER.protectdSheet(sheet);
  396. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  397. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  398. sheet.showRowOutline(false);
  399. if (setting.defaultRowHeight) {
  400. sheet.defaults.rowHeight = setting.defaultRowHeight;
  401. }
  402. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  403. sheet.getRange(tree.count(), -1, setting.emptyRows, -1).locked(true);
  404. setting.cols.forEach(function (colSetting, iCol) {
  405. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  406. if (colSetting.showHint) {
  407. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  408. }
  409. });
  410. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  411. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  412. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  413. });
  414. },
  415. tipDivCheck(){
  416. setTimeout(function () {
  417. let tips = $('#autoTip');
  418. if(TREE_SHEET_HELPER.tipDiv == 'show'){
  419. return;
  420. } else if(TREE_SHEET_HELPER.tipDiv == 'hide'&&tips){
  421. tips.hide();
  422. TREE_SHEET_HELPER._toolTipElement = null;
  423. }
  424. },600)
  425. }
  426. };