tree_sheet_helper.js 19 KB

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