tree_sheet_helper.js 16 KB

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