tree_sheet_helper.js 16 KB

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