block_lib.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Created by CSL on 2018-09-19.
  3. */
  4. var blockLibObj = {
  5. mainSpread: null,
  6. mainSheet: null,
  7. mainTree: null,
  8. mainTreeController: null,
  9. mainSetting: {
  10. "emptyRowHeader": true,
  11. "rowHeaderWidth": 15,
  12. "emptyRows":0,
  13. "headRows":1,
  14. "headRowHeight":[30],
  15. "defaultRowHeight": 21,
  16. "treeCol": 0,
  17. "cols":[{
  18. "width":400,
  19. "readOnly": false,
  20. "head":{
  21. "titleNames":["名称"],
  22. "spanCols":[1],
  23. "spanRows":[1],
  24. "vAlign":[1],
  25. "hAlign":[1],
  26. "font":["Arial"]
  27. },
  28. "data":{
  29. "field":"name",
  30. "vAlign":1,
  31. "hAlign":0,
  32. "font":"Arial"
  33. }
  34. }]
  35. },
  36. mainDatas: [],
  37. buildSheet: function () {
  38. $.bootstrapLoading.start();
  39. let me = this;
  40. me.mainDatas = [
  41. {ID: 1, ParentID: -1, NextSiblingID: 8, name: '我的块模板库1', type: 0},
  42. {ID: 2, ParentID: 1, NextSiblingID: 3, name: '分类1', type: 1},
  43. {ID: 3, ParentID: 1, NextSiblingID: -1, name: '分类2', type: 1},
  44. {ID: 4, ParentID: 2, NextSiblingID: 5, name: '子类A', type: 1},
  45. {ID: 5, ParentID: 2, NextSiblingID: -1, name: '子类B', type: 1},
  46. {ID: 6, ParentID: 4, NextSiblingID: 7, name: '块1', type: 2},
  47. {ID: 7, ParentID: 4, NextSiblingID: -1, name: '块2', type: 2},
  48. {ID: 8, ParentID: -1, NextSiblingID: -1, name: '我的块模板库2', type: 0}
  49. ];
  50. if (me.mainSpread) {
  51. me.mainSpread.destroy();
  52. me.mainSpread = null;
  53. };
  54. me.mainSpread = SheetDataHelper.createNewSpread($('#div_block_tree')[0]);
  55. me.mainSheet = me.mainSpread.getSheet(0);
  56. me.mainSheet.name('blockLibSheet');
  57. sheetCommonObj.spreadDefaultStyle(me.mainSpread);
  58. // me.mainSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, this.onCellDoubleClick);
  59. var showblockTree = function (datas) {
  60. me.mainTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: false});
  61. me.mainTreeController = TREE_SHEET_CONTROLLER.createNew(me.mainTree, me.mainSheet, me.mainSetting);
  62. me.mainTree.loadDatas(datas);
  63. me.mainTreeController.showTreeData();
  64. me.mainSheet.getRange(-1, 0, -1, 1).cellType(me.getTreeCell(me.mainTree));
  65. /* me.mainTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  66. rationLibObj.loadSectionRations(node && node.children.length === 0 ? node.getID() : null);
  67. });
  68. if (me.mainTree.firstNode() && me.mainTree.firstNode().length === 0) {
  69. rationLibObj.loadSectionRations(me.mainTree.firstNode().getID());
  70. } else {
  71. rationLibObj.loadSectionRations();
  72. };*/
  73. };
  74. /* CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID}, function (datas) {
  75. showblockTree(datas);
  76. $.bootstrapLoading.end();
  77. }, function () {
  78. showblockTree([]);
  79. $.bootstrapLoading.end();
  80. });*/
  81. showblockTree(me.mainDatas);
  82. $.bootstrapLoading.end();
  83. },
  84. getTreeCell: function (tree) {
  85. let me = this;
  86. let indent = 20, levelIndent = -5, halfBoxLength = 5, halfExpandLength = 3, imgWidth = 14, imgHeight = 14;
  87. let TreeCell = function () {};
  88. TreeCell.prototype = new GC.Spread.Sheets.CellTypes.Text();
  89. TreeCell.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  90. if (style.backColor) {
  91. ctx.save();
  92. ctx.fillStyle = style.backColor;
  93. ctx.fillRect(x, y, w, h);
  94. ctx.restore();
  95. } else {
  96. ctx.clearRect(x, y, w, h);
  97. };
  98. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  99. ctx.save();
  100. ctx.translate(0.5, 0.5);
  101. ctx.beginPath();
  102. ctx.moveTo(x1, y1);
  103. ctx.lineTo(x2, y2);
  104. ctx.strokeStyle = color;
  105. ctx.stroke();
  106. ctx.restore();
  107. };
  108. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  109. let rect = {}, h1, h2, offset = 1;
  110. rect.top = centerY - halfBoxLength;
  111. rect.bottom = centerY + halfBoxLength;
  112. rect.left = centerX - halfBoxLength;
  113. rect.right = centerX + halfBoxLength;
  114. if (rect.left < x + w) {
  115. rect.right = Math.min(rect.right, x + w);
  116. ctx.save();
  117. ctx.translate(0.5, 0.5);
  118. ctx.strokeStyle = 'black';
  119. ctx.beginPath();
  120. ctx.moveTo(rect.left, rect.top);
  121. ctx.lineTo(rect.left, rect.bottom);
  122. ctx.lineTo(rect.right, rect.bottom);
  123. ctx.lineTo(rect.right, rect.top);
  124. ctx.lineTo(rect.left, rect.top);
  125. ctx.stroke();
  126. ctx.fillStyle = 'white';
  127. ctx.fill();
  128. ctx.restore();
  129. // Draw Horizontal Line
  130. h1 = centerX - halfExpandLength;
  131. h2 = Math.min(centerX + halfExpandLength, x + w);
  132. if (h2 > h1) {
  133. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  134. }
  135. // Draw Vertical Line
  136. if (!expanded && (centerX < x + w)) {
  137. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  138. }
  139. }
  140. };
  141. let node = tree.items[options.row];
  142. if (!node) return;
  143. let showTreeLine = true;
  144. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  145. let x1 = centerX + indent / 2;
  146. let centerY = Math.floor((y + (y + h)) / 2);
  147. let y1;
  148. // Draw Horizontal Line、Image、sibling Vertical Line
  149. if (centerX < x + w) {
  150. // Draw Horizontal Line
  151. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  152. // Draw Image
  153. let imgId;
  154. if (node.data.type === 0) imgId = 'blockLib_pic'
  155. else if (node.data.type === 1) imgId = 'folder_pic'
  156. else if (node.data.type === 2) {
  157. imgId = 'block_pic';
  158. };
  159. let img = document.getElementById(imgId);
  160. ctx.drawImage(img, centerX+indent/2+3, centerY - 7, imgWidth, imgHeight);
  161. // Draw Vertical Line
  162. y1 = node.isLast() ? centerY : y + h;
  163. if (node.isFirst() && !node.parent/*.parent*/) {
  164. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  165. } else {
  166. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  167. }
  168. }
  169. // Draw Expand Box
  170. if (node.children.length > 0) {
  171. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  172. }
  173. // Draw Parent Line
  174. var curNode = node.parent, parentCenterX = centerX - indent - levelIndent;
  175. while (curNode) {
  176. if (!curNode.isLast()) {
  177. if (parentCenterX < x + w) {
  178. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  179. }
  180. }
  181. curNode = curNode.parent;
  182. parentCenterX -= (indent + levelIndent);
  183. }
  184. // Draw Text
  185. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3;
  186. w = w - (node.depth() + 1) * indent - node.depth() * levelIndent - imgWidth - 3;
  187. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  188. };
  189. TreeCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  190. let info = {x: x, y: y, row: context.row, col: context.col, cellStyle: cellStyle, cellRect: cellRect, sheetArea: context.sheetArea};
  191. let node = tree.items[info.row];
  192. let offset = -1;
  193. let centerX = info.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  194. let text = context.sheet.getText(info.row, info.col);
  195. let value = context.sheet.getValue(info.row, info.col);
  196. let acStyle = context.sheet.getActualStyle(info.row, info.col),
  197. zoom = context.sheet.zoom();
  198. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: context.sheet, row: info.row, col: info.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  199. if(info.x > centerX + halfBoxLength && info.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength){
  200. info.isReservedLocation = true;
  201. }
  202. return info;
  203. };
  204. TreeCell.prototype.processMouseDown = function (hitinfo) {
  205. let offset = -1;
  206. let node = tree.items[hitinfo.row];
  207. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  208. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  209. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  210. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  211. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  212. zoom = hitinfo.sheet.zoom();
  213. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  214. //(图标+名字)区域
  215. function withingClickArea(){
  216. return hitinfo.x > centerX + halfBoxLength && hitinfo.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength;
  217. }
  218. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  219. node.setExpanded(!node.expanded);
  220. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  221. let iCount = node.posterityCount(), i, child;
  222. for (i = 0; i < iCount; i++) {
  223. child = tree.items[hitinfo.row + i + 1];
  224. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  225. }
  226. hitinfo.sheet.invalidateLayout();
  227. });
  228. hitinfo.sheet.repaint();
  229. }
  230. };
  231. TreeCell.prototype.processMouseMove = function (hitInfo) {
  232. let sheet = hitInfo.sheet;
  233. let div = sheet.getParent().getHost();
  234. let canvasId = div.id + "vp_vp";
  235. /* let canvas = $(`#${canvasId}`)[0];
  236. //改变鼠标图案
  237. if (sheet && hitInfo.isReservedLocation) {
  238. canvas.style.cursor='pointer';
  239. return true;
  240. }else{
  241. canvas.style.cursor='default';
  242. }*/
  243. return false;
  244. };
  245. TreeCell.prototype.processMouseEnter = function (hitinfo) {
  246. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  247. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  248. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  249. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  250. zoom = hitinfo.sheet.zoom();
  251. let node = me.mainTree.items[hitinfo.row];
  252. let nodeIndent = node ? (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3 : 0;
  253. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  254. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  255. if(textLength > cellWidth - nodeIndent){
  256. TREE_SHEET_HELPER.showTipsDiv(text,{pos: {}},hitinfo);
  257. }
  258. };
  259. TreeCell.prototype.processMouseLeave = function (hitinfo) {
  260. let me = this;
  261. TREE_SHEET_HELPER.tipDiv = 'hide';
  262. if (TREE_SHEET_HELPER._toolTipElement) {
  263. $(TREE_SHEET_HELPER._toolTipElement).hide();
  264. TREE_SHEET_HELPER._toolTipElement = null;
  265. };
  266. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  267. };
  268. return new TreeCell();
  269. },
  270. insert: function (insertType){ // 1 后兄弟; 2 孩子。
  271. let tree = blockLibObj.mainTree;
  272. let select = tree.selected;
  273. if (!select) select = tree.items[0];
  274. let pID = (insertType == 1) ? select.getParentID() : select.getID();
  275. let nID = (insertType == 1) ? select.getNextSiblingID() : -1;
  276. let newNode = tree.insert(pID, nID);
  277. newNode.data.name = '新建';
  278. if (!newNode.parent)
  279. newNode.data.type = 0
  280. else
  281. newNode.data.type = 1;
  282. tree.selected = newNode;
  283. let sheet = blockLibObj.mainSheet;
  284. sheet.suspendPaint();
  285. sheet.suspendEvent();
  286. let idx = tree.items.indexOf(newNode);
  287. sheet.addRows(idx, 1);
  288. sheet.getRange(idx, 0, 1, 1).locked(false);
  289. sheet.setValue(idx, 0, newNode.data.name);
  290. sheet.setSelection(idx, 0, 1, 1);
  291. sheet.resumeEvent();
  292. sheet.resumePaint();
  293. }
  294. };
  295. $(document).ready(function(){
  296. $('#blockLibTab').on('click', function (){
  297. if ($("#kmbk").is(":visible")){
  298. if (!blockLibObj.mainSpread){
  299. blockLibObj.buildSheet();
  300. };
  301. }
  302. });
  303. $('#btn_block_newFolder').on('click', function (){
  304. blockLibObj.insert(1);
  305. });
  306. $('#btn_block_newSubFolder').on('click', function (){
  307. blockLibObj.insert(2);
  308. });
  309. });