tree_sheet_helper.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. if (setting.frozenCols) {
  49. sheet.frozenColumnCount(setting.frozenCols);
  50. }
  51. sheet.setColumnCount(setting.cols.length);
  52. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  53. setting.headRowHeight.forEach(function (rowHeight, index) {
  54. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  55. });
  56. setting.cols.forEach(function (col, index) {
  57. var i, iRow = 0, cell;
  58. for (i = 0; i < col.head.spanCols.length; i++) {
  59. if (col.head.spanCols[i] !== 0) {
  60. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  61. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  62. }
  63. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  64. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  65. }
  66. iRow += col.head.spanRows[i];
  67. };
  68. sheet.setColumnWidth(index, col.width);
  69. sheet.setColumnVisible(index, col.visible && true);
  70. });
  71. },
  72. protectdSheet: function (sheet) {
  73. var option = {
  74. allowSelectLockedCells: true,
  75. allowSelectUnlockedCells: true,
  76. allowResizeRows: true,
  77. allowResizeColumns: true
  78. };
  79. sheet.options.protectionOptions = option;
  80. sheet.options.isProtected = true;
  81. },
  82. massOperationSheet: function (sheet, Operation) {
  83. sheet.suspendPaint();
  84. sheet.suspendEvent();
  85. Operation();
  86. sheet.resumeEvent();
  87. sheet.resumePaint();
  88. },
  89. refreshNodesVisible: function (nodes, sheet, recursive) {
  90. nodes.forEach(function (node) {
  91. var iRow;
  92. iRow = node.serialNo();
  93. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  94. if (recursive) {
  95. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  96. }
  97. })
  98. },
  99. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  100. nodes.forEach(function (node) {
  101. setting.cols.forEach(function (colSetting, iCol) {
  102. var iRow = node.serialNo();
  103. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  104. // var getFieldText = function () {
  105. // var fields = colSetting.data.field.split('.');
  106. // var validField = fields.reduce(function (field1, field2) {
  107. // if (eval('node.data.' + field1)) {
  108. // return field1 + '.' + field2
  109. // } else {
  110. // return field1;
  111. // }
  112. // });
  113. // if (eval('node.data.' + validField)) {
  114. // return eval('node.data.' + validField);
  115. // } else {
  116. // return '';
  117. // }
  118. // };
  119. var getFieldText2 = function () {
  120. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  121. for (iField = 0; iField < fields.length; iField++) {
  122. if (data[fields[iField]]) {
  123. data = data[fields[iField]];
  124. } else {
  125. return '';
  126. }
  127. }
  128. return data;
  129. };
  130. if(colSetting.data.field=="quantity"){
  131. let tag = node.data.quantityEXP?node.data.quantityEXP:'';
  132. sheet.setTag(iRow, iCol,tag);
  133. }
  134. if (colSetting.data.getText && Object.prototype.toString.apply(colSetting.data.getText) === "[object Function]") {
  135. cell.value(colSetting.data.getText(node));
  136. } else {
  137. cell.value(getFieldText2());
  138. }
  139. if (colSetting.data.cellType && Object.prototype.toString.apply(colSetting.data.cellType) !== "[object String]") {
  140. cell.cellType(colSetting.data.cellType(node));
  141. }
  142. if (colSetting.readOnly) {
  143. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  144. cell.locked(colSetting.readOnly(node));
  145. } else {
  146. cell.locked(true);
  147. }
  148. } else {
  149. cell.locked(false);
  150. }
  151. });
  152. sheet.autoFitRow(node.serialNo());
  153. if (recursive) {
  154. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  155. }
  156. });
  157. },
  158. showTreeData: function (setting, sheet, tree) {
  159. let indent = 20;
  160. let halfBoxLength = 5;
  161. let halfExpandLength = 3;
  162. let TreeNodeCellType = function () {
  163. };
  164. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  165. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  166. if (style.backColor) {
  167. ctx.save();
  168. ctx.fillStyle = style.backColor;
  169. ctx.fillRect(x, y, w, h);
  170. ctx.restore();
  171. } else {
  172. ctx.clearRect(x, y, w, h);
  173. }
  174. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  175. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  176. ctx.save();
  177. // ����ƫ����
  178. ctx.translate(0.5, 0.5);
  179. ctx.beginPath();
  180. ctx.moveTo(x1, y1);
  181. ctx.lineTo(x2, y2);
  182. ctx.strokeStyle = color;
  183. ctx.stroke();
  184. ctx.restore();
  185. };
  186. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  187. let rect = {}, h1, h2, offset = 1;
  188. rect.top = centerY - halfBoxLength;
  189. rect.bottom = centerY + halfBoxLength;
  190. rect.left = centerX - halfBoxLength;
  191. rect.right = centerX + halfBoxLength;
  192. if (rect.left < x + w) {
  193. rect.right = Math.min(rect.right, x + w);
  194. ctx.save();
  195. // ����ƫ����
  196. ctx.translate(0.5, 0.5);
  197. ctx.strokeStyle = 'black';
  198. ctx.beginPath();
  199. ctx.moveTo(rect.left, rect.top);
  200. ctx.lineTo(rect.left, rect.bottom);
  201. ctx.lineTo(rect.right, rect.bottom);
  202. ctx.lineTo(rect.right, rect.top);
  203. ctx.lineTo(rect.left, rect.top);
  204. ctx.stroke();
  205. ctx.fillStyle = 'white';
  206. ctx.fill();
  207. ctx.restore();
  208. // Draw Horizontal Line
  209. h1 = centerX - halfExpandLength;
  210. h2 = Math.min(centerX + halfExpandLength, x + w);
  211. if (h2 > h1) {
  212. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  213. }
  214. // Draw Vertical Line
  215. if (!expanded && (centerX < x + w)) {
  216. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  217. }
  218. }
  219. }
  220. let node = tree.items[options.row];
  221. let showTreeLine = true;
  222. if (!node) { return; }
  223. let centerX = Math.floor(x) + node.depth() * indent + indent / 2;
  224. let x1 = centerX + indent / 2;
  225. let centerY = Math.floor((y + (y + h)) / 2);
  226. let y1;
  227. // Draw Sibling Line
  228. if (showTreeLine) {
  229. // Draw Horizontal Line
  230. if (centerX < x + w) {
  231. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  232. }
  233. // Draw Vertical Line
  234. if (centerX < x + w) {
  235. y1 = node.isLast() ? centerY : y + h;
  236. if (node.isFirst() && !node.parent) {
  237. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  238. } else {
  239. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  240. }
  241. }
  242. }
  243. // Draw Expand Box
  244. if (node.children.length > 0) {
  245. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  246. }
  247. // Draw Parent Line
  248. if (showTreeLine) {
  249. var parent = node.parent, parentCenterX = centerX - indent;
  250. while (parent) {
  251. if (!parent.isLast()) {
  252. if (parentCenterX < x + w) {
  253. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  254. }
  255. }
  256. parent = parent.parent;
  257. parentCenterX -= indent;
  258. }
  259. };
  260. // Draw Text
  261. x = x + (node.depth() + 1) * indent;
  262. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  263. };
  264. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  265. return {
  266. x: x,
  267. y: y,
  268. row: context.row,
  269. col: context.col,
  270. cellStyle: cellStyle,
  271. cellRect: cellRect,
  272. sheetArea: context.sheetArea
  273. };
  274. };
  275. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  276. let offset = -1;
  277. let node = tree.items[hitinfo.row];
  278. tree.selected = node;
  279. if (!node || node.children.length === 0) { return; }
  280. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + indent / 2;
  281. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  282. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  283. node.setExpanded(!node.expanded);
  284. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  285. let iCount = node.posterityCount(), i, child;
  286. for (i = 0; i < iCount; i++) {
  287. child = tree.items[hitinfo.row + i + 1];
  288. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  289. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  290. }
  291. hitinfo.sheet.invalidateLayout();
  292. });
  293. hitinfo.sheet.repaint();
  294. }
  295. };
  296. let TipCellType = function () {};
  297. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  298. TipCellType.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. sheet: context.sheet,
  307. sheetArea: context.sheetArea
  308. };
  309. };
  310. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  311. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  312. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  313. if(setting.cols[hitinfo.col].data.field=="quantity"){
  314. text = tag;
  315. }else if(tag !== undefined && tag) {
  316. text = tag;
  317. }
  318. if (setting.pos && text && text !== '') {
  319. if (!this._toolTipElement) {
  320. let div = $('#autoTip')[0];
  321. if (!div) {
  322. div = document.createElement("div");
  323. $(div).css("position", "absolute")
  324. .css("border", "1px #C0C0C0 solid")
  325. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  326. .css("font", "9pt Arial")
  327. .css("background", "white")
  328. .css("padding", 5)
  329. .attr("id", 'autoTip');
  330. $(div).hide();
  331. document.body.insertBefore(div, null);
  332. }
  333. this._toolTipElement = div;
  334. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  335. $(this._toolTipElement).show("fast");
  336. }
  337. }
  338. };
  339. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  340. if (this._toolTipElement) {
  341. $(this._toolTipElement).hide();
  342. this._toolTipElement = null;
  343. }
  344. }
  345. TREE_SHEET_HELPER.protectdSheet(sheet);
  346. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  347. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  348. sheet.showRowOutline(false);
  349. if (setting.defaultRowHeight) {
  350. sheet.defaults.rowHeight = setting.defaultRowHeight;
  351. }
  352. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  353. setting.cols.forEach(function (colSetting, iCol) {
  354. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  355. if (colSetting.showHint) {
  356. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  357. }
  358. });
  359. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  360. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  361. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  362. });
  363. }
  364. };