8bbe9abc34abe6a7345a43e607acb99ae71bb63d2186726e8dd7dee0448d057b1b9ebd1bc4daf0e6519708d2bca33250657ca0c80c96096d7f72045fbc46b6 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  10. var _color = _interopRequireDefault(require("@jiaminghi/color"));
  11. var _bezierCurve = _interopRequireDefault(require("@jiaminghi/bezier-curve"));
  12. var _util = require("../plugin/util");
  13. var _graphs = _interopRequireDefault(require("../config/graphs"));
  14. var _graph = _interopRequireDefault(require("./graph.class"));
  15. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  16. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  17. /**
  18. * @description Class of CRender
  19. * @param {Object} canvas Canvas DOM
  20. * @return {CRender} Instance of CRender
  21. */
  22. var CRender = function CRender(canvas) {
  23. (0, _classCallCheck2["default"])(this, CRender);
  24. if (!canvas) {
  25. console.error('CRender Missing parameters!');
  26. return;
  27. }
  28. var ctx = canvas.getContext('2d');
  29. var clientWidth = canvas.clientWidth,
  30. clientHeight = canvas.clientHeight;
  31. var area = [clientWidth, clientHeight];
  32. canvas.setAttribute('width', clientWidth);
  33. canvas.setAttribute('height', clientHeight);
  34. /**
  35. * @description Context of the canvas
  36. * @type {Object}
  37. * @example ctx = canvas.getContext('2d')
  38. */
  39. this.ctx = ctx;
  40. /**
  41. * @description Width and height of the canvas
  42. * @type {Array}
  43. * @example area = [300,100]
  44. */
  45. this.area = area;
  46. /**
  47. * @description Whether render is in animation rendering
  48. * @type {Boolean}
  49. * @example animationStatus = true|false
  50. */
  51. this.animationStatus = false;
  52. /**
  53. * @description Added graph
  54. * @type {[Graph]}
  55. * @example graphs = [Graph, Graph, ...]
  56. */
  57. this.graphs = [];
  58. /**
  59. * @description Color plugin
  60. * @type {Object}
  61. * @link https://github.com/jiaming743/color
  62. */
  63. this.color = _color["default"];
  64. /**
  65. * @description Bezier Curve plugin
  66. * @type {Object}
  67. * @link https://github.com/jiaming743/BezierCurve
  68. */
  69. this.bezierCurve = _bezierCurve["default"]; // bind event handler
  70. canvas.addEventListener('mousedown', mouseDown.bind(this));
  71. canvas.addEventListener('mousemove', mouseMove.bind(this));
  72. canvas.addEventListener('mouseup', mouseUp.bind(this));
  73. };
  74. /**
  75. * @description Clear canvas drawing area
  76. * @return {Undefined} Void
  77. */
  78. exports["default"] = CRender;
  79. CRender.prototype.clearArea = function () {
  80. var _this$ctx;
  81. var area = this.area;
  82. (_this$ctx = this.ctx).clearRect.apply(_this$ctx, [0, 0].concat((0, _toConsumableArray2["default"])(area)));
  83. };
  84. /**
  85. * @description Add graph to render
  86. * @param {Object} config Graph configuration
  87. * @return {Graph} Graph instance
  88. */
  89. CRender.prototype.add = function () {
  90. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  91. var name = config.name;
  92. if (!name) {
  93. console.error('add Missing parameters!');
  94. return;
  95. }
  96. var graphConfig = _graphs["default"].get(name);
  97. if (!graphConfig) {
  98. console.warn('No corresponding graph configuration found!');
  99. return;
  100. }
  101. var graph = new _graph["default"](graphConfig, config);
  102. if (!graph.validator(graph)) return;
  103. graph.render = this;
  104. this.graphs.push(graph);
  105. this.sortGraphsByIndex();
  106. this.drawAllGraph();
  107. return graph;
  108. };
  109. /**
  110. * @description Sort the graph by index
  111. * @return {Undefined} Void
  112. */
  113. CRender.prototype.sortGraphsByIndex = function () {
  114. var graphs = this.graphs;
  115. graphs.sort(function (a, b) {
  116. if (a.index > b.index) return 1;
  117. if (a.index === b.index) return 0;
  118. if (a.index < b.index) return -1;
  119. });
  120. };
  121. /**
  122. * @description Delete graph in render
  123. * @param {Graph} graph The graph to be deleted
  124. * @return {Undefined} Void
  125. */
  126. CRender.prototype.delGraph = function (graph) {
  127. if (typeof graph.delProcessor !== 'function') return;
  128. graph.delProcessor(this);
  129. this.graphs = this.graphs.filter(function (graph) {
  130. return graph;
  131. });
  132. this.drawAllGraph();
  133. };
  134. /**
  135. * @description Delete all graph in render
  136. * @return {Undefined} Void
  137. */
  138. CRender.prototype.delAllGraph = function () {
  139. var _this = this;
  140. this.graphs.forEach(function (graph) {
  141. return graph.delProcessor(_this);
  142. });
  143. this.graphs = this.graphs.filter(function (graph) {
  144. return graph;
  145. });
  146. this.drawAllGraph();
  147. };
  148. /**
  149. * @description Draw all the graphs in the render
  150. * @return {Undefined} Void
  151. */
  152. CRender.prototype.drawAllGraph = function () {
  153. var _this2 = this;
  154. this.clearArea();
  155. this.graphs.filter(function (graph) {
  156. return graph && graph.visible;
  157. }).forEach(function (graph) {
  158. return graph.drawProcessor(_this2, graph);
  159. });
  160. };
  161. /**
  162. * @description Animate the graph whose animation queue is not empty
  163. * and the animationPause is equal to false
  164. * @return {Promise} Animation Promise
  165. */
  166. CRender.prototype.launchAnimation = function () {
  167. var _this3 = this;
  168. var animationStatus = this.animationStatus;
  169. if (animationStatus) return;
  170. this.animationStatus = true;
  171. return new Promise(function (resolve) {
  172. animation.call(_this3, function () {
  173. _this3.animationStatus = false;
  174. resolve();
  175. }, Date.now());
  176. });
  177. };
  178. /**
  179. * @description Try to animate every graph
  180. * @param {Function} callback Callback in animation end
  181. * @param {Number} timeStamp Time stamp of animation start
  182. * @return {Undefined} Void
  183. */
  184. function animation(callback, timeStamp) {
  185. var graphs = this.graphs;
  186. if (!animationAble(graphs)) {
  187. callback();
  188. return;
  189. }
  190. graphs.forEach(function (graph) {
  191. return graph.turnNextAnimationFrame(timeStamp);
  192. });
  193. this.drawAllGraph();
  194. requestAnimationFrame(animation.bind(this, callback, timeStamp));
  195. }
  196. /**
  197. * @description Find if there are graph that can be animated
  198. * @param {[Graph]} graphs
  199. * @return {Boolean}
  200. */
  201. function animationAble(graphs) {
  202. return graphs.find(function (graph) {
  203. return !graph.animationPause && graph.animationFrameState.length;
  204. });
  205. }
  206. /**
  207. * @description Handler of CRender mousedown event
  208. * @return {Undefined} Void
  209. */
  210. function mouseDown(e) {
  211. var graphs = this.graphs;
  212. var hoverGraph = graphs.find(function (graph) {
  213. return graph.status === 'hover';
  214. });
  215. if (!hoverGraph) return;
  216. hoverGraph.status = 'active';
  217. }
  218. /**
  219. * @description Handler of CRender mousemove event
  220. * @return {Undefined} Void
  221. */
  222. function mouseMove(e) {
  223. var offsetX = e.offsetX,
  224. offsetY = e.offsetY;
  225. var position = [offsetX, offsetY];
  226. var graphs = this.graphs;
  227. var activeGraph = graphs.find(function (graph) {
  228. return graph.status === 'active' || graph.status === 'drag';
  229. });
  230. if (activeGraph) {
  231. if (!activeGraph.drag) return;
  232. if (typeof activeGraph.move !== 'function') {
  233. console.error('No move method is provided, cannot be dragged!');
  234. return;
  235. }
  236. activeGraph.moveProcessor(e);
  237. activeGraph.status = 'drag';
  238. return;
  239. }
  240. var hoverGraph = graphs.find(function (graph) {
  241. return graph.status === 'hover';
  242. });
  243. var hoverAbleGraphs = graphs.filter(function (graph) {
  244. return graph.hover && (typeof graph.hoverCheck === 'function' || graph.hoverRect);
  245. });
  246. var hoveredGraph = hoverAbleGraphs.find(function (graph) {
  247. return graph.hoverCheckProcessor(position, graph);
  248. });
  249. if (hoveredGraph) {
  250. document.body.style.cursor = hoveredGraph.style.hoverCursor;
  251. } else {
  252. document.body.style.cursor = 'default';
  253. }
  254. var hoverGraphMouseOuterIsFun = false,
  255. hoveredGraphMouseEnterIsFun = false;
  256. if (hoverGraph) hoverGraphMouseOuterIsFun = typeof hoverGraph.mouseOuter === 'function';
  257. if (hoveredGraph) hoveredGraphMouseEnterIsFun = typeof hoveredGraph.mouseEnter === 'function';
  258. if (!hoveredGraph && !hoverGraph) return;
  259. if (!hoveredGraph && hoverGraph) {
  260. if (hoverGraphMouseOuterIsFun) hoverGraph.mouseOuter(e, hoverGraph);
  261. hoverGraph.status = 'static';
  262. return;
  263. }
  264. if (hoveredGraph && hoveredGraph === hoverGraph) return;
  265. if (hoveredGraph && !hoverGraph) {
  266. if (hoveredGraphMouseEnterIsFun) hoveredGraph.mouseEnter(e, hoveredGraph);
  267. hoveredGraph.status = 'hover';
  268. return;
  269. }
  270. if (hoveredGraph && hoverGraph && hoveredGraph !== hoverGraph) {
  271. if (hoverGraphMouseOuterIsFun) hoverGraph.mouseOuter(e, hoverGraph);
  272. hoverGraph.status = 'static';
  273. if (hoveredGraphMouseEnterIsFun) hoveredGraph.mouseEnter(e, hoveredGraph);
  274. hoveredGraph.status = 'hover';
  275. }
  276. }
  277. /**
  278. * @description Handler of CRender mouseup event
  279. * @return {Undefined} Void
  280. */
  281. function mouseUp(e) {
  282. var graphs = this.graphs;
  283. var activeGraph = graphs.find(function (graph) {
  284. return graph.status === 'active';
  285. });
  286. var dragGraph = graphs.find(function (graph) {
  287. return graph.status === 'drag';
  288. });
  289. if (activeGraph && typeof activeGraph.click === 'function') activeGraph.click(e, activeGraph);
  290. graphs.forEach(function (graph) {
  291. return graph && (graph.status = 'static');
  292. });
  293. if (activeGraph) activeGraph.status = 'hover';
  294. if (dragGraph) dragGraph.status = 'hover';
  295. }
  296. /**
  297. * @description Clone Graph
  298. * @param {Graph} graph The target to be cloned
  299. * @return {Graph} Cloned graph
  300. */
  301. CRender.prototype.clone = function (graph) {
  302. var style = graph.style.getStyle();
  303. var clonedGraph = _objectSpread({}, graph, {
  304. style: style
  305. });
  306. delete clonedGraph.render;
  307. clonedGraph = (0, _util.deepClone)(clonedGraph, true);
  308. return this.add(clonedGraph);
  309. };