block_lib.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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": 9,
  17. "cols":[{
  18. "width":400,
  19. "readOnly": true,
  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: 2, ParentID: -1, NextSiblingID: 3, name: '分类2', type: 1},
  42. {ID: 3, ParentID: -1, NextSiblingID: -1, name: '分类3', type: 1},
  43. {ID: 6, ParentID: 2, NextSiblingID: 7, name: '块1', type: 2},
  44. {ID: 7, ParentID: 2, NextSiblingID: -1, name: '块2', type: 2},
  45. {ID: 52, ParentID: -1, NextSiblingID: 53, name: '分类52', type: 1},
  46. {ID: 55, ParentID: -1, NextSiblingID: -1, name: '分类55', type: 1},
  47. {ID: 88, ParentID: 52, NextSiblingID: -1, name: '块88', type: 2}
  48. ];
  49. if (me.mainSpread) {
  50. me.mainSpread.destroy();
  51. me.mainSpread = null;
  52. };
  53. me.mainSpread = SheetDataHelper.createNewSpread($('#div_block_tree')[0]);
  54. // me.mainSpread = TREE_SHEET_HELPER.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.mainTree.selected = me.mainTree.items[0];
  66. /* me.mainTreeController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
  67. rationLibObj.loadSectionRations(node && node.children.length === 0 ? node.getID() : null);
  68. });
  69. if (me.mainTree.firstNode() && me.mainTree.firstNode().length === 0) {
  70. rationLibObj.loadSectionRations(me.mainTree.firstNode().getID());
  71. } else {
  72. rationLibObj.loadSectionRations();
  73. };*/
  74. };
  75. /* CommonAjax.post('/complementaryRation/api/getRationTree', {userId: userID, rationRepId: rationLibID}, function (datas) {
  76. showblockTree(datas);
  77. $.bootstrapLoading.end();
  78. }, function () {
  79. showblockTree([]);
  80. $.bootstrapLoading.end();
  81. });*/
  82. showblockTree(me.mainDatas);
  83. $.bootstrapLoading.end();
  84. },
  85. getTreeCell: function (tree) {
  86. let me = this;
  87. let indent = 20, levelIndent = -5, halfBoxLength = 5, halfExpandLength = 3, imgWidth = 14, imgHeight = 14;
  88. let TreeCell = function () {};
  89. TreeCell.prototype = new GC.Spread.Sheets.CellTypes.Text();
  90. TreeCell.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  91. if (style.backColor) {
  92. ctx.save();
  93. ctx.fillStyle = style.backColor;
  94. ctx.fillRect(x, y, w, h);
  95. ctx.restore();
  96. } else {
  97. ctx.clearRect(x, y, w, h);
  98. };
  99. let drawLine = function (canvas, x1, y1, x2, y2, color) {
  100. ctx.save();
  101. ctx.translate(0.5, 0.5);
  102. ctx.beginPath();
  103. ctx.moveTo(x1, y1);
  104. ctx.lineTo(x2, y2);
  105. ctx.strokeStyle = color;
  106. ctx.stroke();
  107. ctx.restore();
  108. };
  109. let drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  110. let rect = {}, h1, h2, offset = 1;
  111. rect.top = centerY - halfBoxLength;
  112. rect.bottom = centerY + halfBoxLength;
  113. rect.left = centerX - halfBoxLength;
  114. rect.right = centerX + halfBoxLength;
  115. if (rect.left < x + w) {
  116. rect.right = Math.min(rect.right, x + w);
  117. ctx.save();
  118. ctx.translate(0.5, 0.5);
  119. ctx.strokeStyle = 'black';
  120. ctx.beginPath();
  121. ctx.moveTo(rect.left, rect.top);
  122. ctx.lineTo(rect.left, rect.bottom);
  123. ctx.lineTo(rect.right, rect.bottom);
  124. ctx.lineTo(rect.right, rect.top);
  125. ctx.lineTo(rect.left, rect.top);
  126. ctx.stroke();
  127. ctx.fillStyle = 'white';
  128. ctx.fill();
  129. ctx.restore();
  130. // Draw Horizontal Line
  131. h1 = centerX - halfExpandLength;
  132. h2 = Math.min(centerX + halfExpandLength, x + w);
  133. if (h2 > h1) {
  134. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  135. }
  136. // Draw Vertical Line
  137. if (!expanded && (centerX < x + w)) {
  138. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  139. }
  140. }
  141. };
  142. let node = tree.items[options.row];
  143. if (!node) return;
  144. let showTreeLine = true;
  145. let centerX = Math.floor(x) + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  146. let x1 = centerX + indent / 2;
  147. let centerY = Math.floor((y + (y + h)) / 2);
  148. let y1;
  149. // Draw Horizontal Line、Image、sibling Vertical Line
  150. if (centerX < x + w) {
  151. // Draw Horizontal Line
  152. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  153. // Draw Image
  154. let imgId;
  155. if (node.data.type === 0) imgId = 'blockLib_pic'
  156. else if (node.data.type === 1) imgId = 'folder_pic'
  157. else if (node.data.type === 2) {
  158. imgId = 'block_pic';
  159. };
  160. let img = document.getElementById(imgId);
  161. ctx.drawImage(img, centerX+indent/2+3, centerY - 7, imgWidth, imgHeight);
  162. // Draw Vertical Line
  163. y1 = node.isLast() ? centerY : y + h;
  164. if (node.isFirst() && !node.parent/*.parent*/) {
  165. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  166. } else {
  167. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  168. }
  169. }
  170. // Draw Expand Box
  171. if (node.children.length > 0) {
  172. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  173. }
  174. // Draw Parent Line
  175. var curNode = node.parent, parentCenterX = centerX - indent - levelIndent;
  176. while (curNode) {
  177. if (!curNode.isLast()) {
  178. if (parentCenterX < x + w) {
  179. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  180. }
  181. }
  182. curNode = curNode.parent;
  183. parentCenterX -= (indent + levelIndent);
  184. }
  185. // Draw Text
  186. x = x + (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3;
  187. w = w - (node.depth() + 1) * indent - node.depth() * levelIndent - imgWidth - 3;
  188. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  189. };
  190. TreeCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  191. let info = {x: x, y: y, row: context.row, col: context.col, cellStyle: cellStyle, cellRect: cellRect, sheetArea: context.sheetArea};
  192. let node = tree.items[info.row];
  193. let offset = -1;
  194. let centerX = info.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  195. let text = context.sheet.getText(info.row, info.col);
  196. let value = context.sheet.getValue(info.row, info.col);
  197. let acStyle = context.sheet.getActualStyle(info.row, info.col),
  198. zoom = context.sheet.zoom();
  199. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: context.sheet, row: info.row, col: info.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  200. if(info.x > centerX + halfBoxLength && info.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength){
  201. info.isReservedLocation = true;
  202. }
  203. return info;
  204. };
  205. TreeCell.prototype.processMouseDown = function (hitinfo) {
  206. let offset = -1;
  207. let node = tree.items[hitinfo.row];
  208. let centerX = hitinfo.cellRect.x + offset + node.depth() * indent + node.depth() * levelIndent + indent / 2;
  209. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  210. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  211. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  212. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  213. zoom = hitinfo.sheet.zoom();
  214. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  215. //(图标+名字)区域
  216. function withingClickArea(){
  217. return hitinfo.x > centerX + halfBoxLength && hitinfo.x < centerX + halfBoxLength + imgWidth + indent/2+3 + textLength;
  218. }
  219. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  220. node.setExpanded(!node.expanded);
  221. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  222. let iCount = node.posterityCount(), i, child;
  223. for (i = 0; i < iCount; i++) {
  224. child = tree.items[hitinfo.row + i + 1];
  225. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  226. }
  227. hitinfo.sheet.invalidateLayout();
  228. });
  229. hitinfo.sheet.repaint();
  230. }
  231. };
  232. TreeCell.prototype.processMouseMove = function (hitInfo) {
  233. let sheet = hitInfo.sheet;
  234. let div = sheet.getParent().getHost();
  235. let canvasId = div.id + "vp_vp";
  236. /* let canvas = $(`#${canvasId}`)[0];
  237. //改变鼠标图案
  238. if (sheet && hitInfo.isReservedLocation) {
  239. canvas.style.cursor='pointer';
  240. return true;
  241. }else{
  242. canvas.style.cursor='default';
  243. }*/
  244. return false;
  245. };
  246. TreeCell.prototype.processMouseEnter = function (hitinfo) {
  247. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  248. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  249. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  250. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  251. zoom = hitinfo.sheet.zoom();
  252. let node = me.mainTree.items[hitinfo.row];
  253. let nodeIndent = node ? (node.depth() + 1) * indent + node.depth() * levelIndent + imgWidth + 3 : 0;
  254. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  255. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  256. if(textLength > cellWidth - nodeIndent){
  257. TREE_SHEET_HELPER.showTipsDiv(text,{pos: {}},hitinfo);
  258. }
  259. };
  260. TreeCell.prototype.processMouseLeave = function (hitinfo) {
  261. let me = this;
  262. TREE_SHEET_HELPER.tipDiv = 'hide';
  263. if (TREE_SHEET_HELPER._toolTipElement) {
  264. $(TREE_SHEET_HELPER._toolTipElement).hide();
  265. TREE_SHEET_HELPER._toolTipElement = null;
  266. };
  267. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  268. };
  269. return new TreeCell();
  270. },
  271. newNode: function (nodeType, nodeName, categoryID){ // 1 分类 2 块文件
  272. let tree = blockLibObj.mainTree;
  273. let pID = -1, nID = -1;
  274. let select = tree.selected;
  275. let newNode = null;
  276. if (nodeType == 1){
  277. if (!select) {
  278. nID = -1;
  279. }
  280. else if (select.data.type == 1){
  281. nID = select.getNextSiblingID();
  282. }
  283. else if (select.data.type == 2){
  284. nID = select.parent.getNextSiblingID();
  285. };
  286. }
  287. else if (nodeType == 2) {
  288. /* if (!select) {
  289. pID = tree.items[0].getID();
  290. }
  291. else if (select.data.type == 1){
  292. pID = select.getID();
  293. }
  294. else if (select.data.type == 2){
  295. pID = select.getParentID();
  296. nID = select.getNextSiblingID();
  297. };*/
  298. pID = categoryID;
  299. nID = -1;
  300. }
  301. newNode = tree.insert(pID, nID);
  302. newNode.data.type = nodeType;
  303. newNode.data.name = nodeName;
  304. tree.selected = newNode;
  305. let sheet = blockLibObj.mainSheet;
  306. sheet.suspendPaint();
  307. sheet.suspendEvent();
  308. let idx = tree.items.indexOf(newNode);
  309. sheet.addRows(idx, 1);
  310. sheet.getRange(idx, 0, 1, 1).locked(true);
  311. sheet.setValue(idx, 0, newNode.data.name);
  312. sheet.setSelection(idx, 0, 1, 1);
  313. sheet.resumeEvent();
  314. sheet.resumePaint();
  315. },
  316. reName: function (node, newName){
  317. node.data.name = newName;
  318. let nodes = blockLibObj.mainTree.items;
  319. let idx = -1;
  320. for (let i = 0; i < nodes.length; i++) {
  321. if (node == nodes[i]){
  322. idx = i;
  323. break;
  324. }
  325. }
  326. blockLibObj.mainSheet.setValue(idx, 0, newName);
  327. },
  328. getCategories: function () {
  329. let nodes = [], node = blockLibObj.mainTree.items[0];
  330. nodes.push(node);
  331. while (node.nextSibling != null){
  332. node = node.nextSibling;
  333. nodes.push(node);
  334. };
  335. return nodes;
  336. }
  337. };
  338. $(document).ready(function(){
  339. $('#blockLibTab').on('click', function (){
  340. if ($("#kmbk").is(":visible")){
  341. if (!blockLibObj.mainSpread){
  342. blockLibObj.buildSheet();
  343. };
  344. }
  345. });
  346. $('#btn_block_newFolder').on('click', function (){
  347. $('#input_block_newFolder').val('');
  348. });
  349. $('#btn_block_newFolder_add').on('click', function (){
  350. let name = $('#input_block_newFolder').val();
  351. if (name != '') blockLibObj.newNode(1, name);
  352. });
  353. $('#btn_block_reName').on('click', function (){
  354. let select = blockLibObj.mainTree.selected;
  355. // $('#lbl_block_reName').text(select.data.name);
  356. $('#input_block_reName').val(select.data.name);
  357. });
  358. $('#btn_block_reName_OK').on('click', function (){
  359. let select = blockLibObj.mainTree.selected;
  360. let oldName = select.data.name;
  361. let newName = $('#input_block_reName').val();
  362. if (oldName != newName) blockLibObj.reName(select, newName);
  363. });
  364. });