jquery.flot.events.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. import angular from 'angular';
  2. import $ from 'jquery';
  3. import _ from 'lodash';
  4. import Drop from 'tether-drop';
  5. /** @ngInject */
  6. export function createAnnotationToolip(element, event, plot) {
  7. const injector = angular.element(document).injector();
  8. const content = document.createElement('div');
  9. content.innerHTML = '<annotation-tooltip event="event" on-edit="onEdit()"></annotation-tooltip>';
  10. injector.invoke([
  11. '$compile',
  12. '$rootScope',
  13. ($compile, $rootScope) => {
  14. const eventManager = plot.getOptions().events.manager;
  15. const tmpScope = $rootScope.$new(true);
  16. tmpScope.event = event;
  17. tmpScope.onEdit = () => {
  18. eventManager.editEvent(event);
  19. };
  20. $compile(content)(tmpScope);
  21. tmpScope.$digest();
  22. tmpScope.$destroy();
  23. const drop = new Drop({
  24. target: element[0],
  25. content: content,
  26. position: 'bottom center',
  27. classes: 'drop-popover drop-popover--annotation',
  28. openOn: 'hover',
  29. hoverCloseDelay: 200,
  30. tetherOptions: {
  31. constraints: [{ to: 'window', pin: true, attachment: 'both' }],
  32. },
  33. });
  34. drop.open();
  35. drop.on('close', () => {
  36. setTimeout(() => {
  37. drop.destroy();
  38. });
  39. });
  40. },
  41. ]);
  42. }
  43. let markerElementToAttachTo = null;
  44. /** @ngInject */
  45. export function createEditPopover(element, event, plot) {
  46. const eventManager = plot.getOptions().events.manager;
  47. if (eventManager.editorOpen) {
  48. // update marker element to attach to (needed in case of legend on the right
  49. // when there is a double render pass and the inital marker element is removed)
  50. markerElementToAttachTo = element;
  51. return;
  52. }
  53. // mark as openend
  54. eventManager.editorOpened();
  55. // set marker elment to attache to
  56. markerElementToAttachTo = element;
  57. // wait for element to be attached and positioned
  58. setTimeout(() => {
  59. const injector = angular.element(document).injector();
  60. const content = document.createElement('div');
  61. content.innerHTML = '<event-editor panel-ctrl="panelCtrl" event="event" close="close()"></event-editor>';
  62. injector.invoke([
  63. '$compile',
  64. '$rootScope',
  65. ($compile, $rootScope) => {
  66. const scope = $rootScope.$new(true);
  67. let drop;
  68. scope.event = event;
  69. scope.panelCtrl = eventManager.panelCtrl;
  70. scope.close = () => {
  71. drop.close();
  72. };
  73. $compile(content)(scope);
  74. scope.$digest();
  75. drop = new Drop({
  76. target: markerElementToAttachTo[0],
  77. content: content,
  78. position: 'bottom center',
  79. classes: 'drop-popover drop-popover--form',
  80. openOn: 'click',
  81. tetherOptions: {
  82. constraints: [{ to: 'window', pin: true, attachment: 'both' }],
  83. },
  84. });
  85. drop.open();
  86. eventManager.editorOpened();
  87. drop.on('close', () => {
  88. // need timeout here in order call drop.destroy
  89. setTimeout(() => {
  90. eventManager.editorClosed();
  91. scope.$destroy();
  92. drop.destroy();
  93. });
  94. });
  95. },
  96. ]);
  97. }, 100);
  98. }
  99. /*
  100. * jquery.flot.events
  101. *
  102. * description: Flot plugin for adding events/markers to the plot
  103. * version: 0.2.5
  104. * authors:
  105. * Alexander Wunschik <alex@wunschik.net>
  106. * Joel Oughton <joeloughton@gmail.com>
  107. * Nicolas Joseph <www.nicolasjoseph.com>
  108. *
  109. * website: https://github.com/mojoaxel/flot-events
  110. *
  111. * released under MIT License and GPLv2+
  112. */
  113. /**
  114. * A class that allows for the drawing an remove of some object
  115. */
  116. export class DrawableEvent {
  117. _object: any;
  118. _drawFunc: any;
  119. _clearFunc: any;
  120. _moveFunc: any;
  121. _position: any;
  122. _width: any;
  123. _height: any;
  124. /** @ngInject */
  125. constructor(object, drawFunc, clearFunc, moveFunc, left, top, width, height) {
  126. this._object = object;
  127. this._drawFunc = drawFunc;
  128. this._clearFunc = clearFunc;
  129. this._moveFunc = moveFunc;
  130. this._position = { left: left, top: top };
  131. this._width = width;
  132. this._height = height;
  133. }
  134. width() {
  135. return this._width;
  136. }
  137. height() {
  138. return this._height;
  139. }
  140. position() {
  141. return this._position;
  142. }
  143. draw() {
  144. this._drawFunc(this._object);
  145. }
  146. clear() {
  147. this._clearFunc(this._object);
  148. }
  149. getObject() {
  150. return this._object;
  151. }
  152. moveTo(position) {
  153. this._position = position;
  154. this._moveFunc(this._object, this._position);
  155. }
  156. }
  157. /**
  158. * Event class that stores options (eventType, min, max, title, description) and the object to draw.
  159. */
  160. export class VisualEvent {
  161. _parent: any;
  162. _options: any;
  163. _drawableEvent: any;
  164. _hidden: any;
  165. /** @ngInject */
  166. constructor(options, drawableEvent) {
  167. this._options = options;
  168. this._drawableEvent = drawableEvent;
  169. this._hidden = false;
  170. }
  171. visual() {
  172. return this._drawableEvent;
  173. }
  174. getOptions() {
  175. return this._options;
  176. }
  177. getParent() {
  178. return this._parent;
  179. }
  180. isHidden() {
  181. return this._hidden;
  182. }
  183. hide() {
  184. this._hidden = true;
  185. }
  186. unhide() {
  187. this._hidden = false;
  188. }
  189. }
  190. /**
  191. * A Class that handles the event-markers inside the given plot
  192. */
  193. export class EventMarkers {
  194. _events: any;
  195. _types: any;
  196. _plot: any;
  197. eventsEnabled: any;
  198. /** @ngInject */
  199. constructor(plot) {
  200. this._events = [];
  201. this._types = [];
  202. this._plot = plot;
  203. this.eventsEnabled = false;
  204. }
  205. getEvents() {
  206. return this._events;
  207. }
  208. setTypes(types) {
  209. return (this._types = types);
  210. }
  211. /**
  212. * create internal objects for the given events
  213. */
  214. setupEvents(events) {
  215. const parts = _.partition(events, 'isRegion');
  216. const regions = parts[0];
  217. events = parts[1];
  218. $.each(events, (index, event) => {
  219. const ve = new VisualEvent(event, this._buildDiv(event));
  220. this._events.push(ve);
  221. });
  222. $.each(regions, (index, event) => {
  223. const vre = new VisualEvent(event, this._buildRegDiv(event));
  224. this._events.push(vre);
  225. });
  226. this._events.sort((a, b) => {
  227. const ao = a.getOptions(),
  228. bo = b.getOptions();
  229. if (ao.min > bo.min) {
  230. return 1;
  231. }
  232. if (ao.min < bo.min) {
  233. return -1;
  234. }
  235. return 0;
  236. });
  237. }
  238. /**
  239. * draw the events to the plot
  240. */
  241. drawEvents() {
  242. // var o = this._plot.getPlotOffset();
  243. $.each(this._events, (index, event) => {
  244. // check event is inside the graph range
  245. if (this._insidePlot(event.getOptions().min) && !event.isHidden()) {
  246. event.visual().draw();
  247. } else {
  248. event
  249. .visual()
  250. .getObject()
  251. .hide();
  252. }
  253. });
  254. }
  255. /**
  256. * update the position of the event-markers (e.g. after scrolling or zooming)
  257. */
  258. updateEvents() {
  259. const o = this._plot.getPlotOffset();
  260. let left;
  261. let top;
  262. const xaxis = this._plot.getXAxes()[this._plot.getOptions().events.xaxis - 1];
  263. $.each(this._events, (index, event) => {
  264. top = o.top + this._plot.height() - event.visual().height();
  265. left = xaxis.p2c(event.getOptions().min) + o.left - event.visual().width() / 2;
  266. event.visual().moveTo({ top: top, left: left });
  267. });
  268. }
  269. /**
  270. * remove all events from the plot
  271. */
  272. _clearEvents() {
  273. $.each(this._events, (index, val) => {
  274. val.visual().clear();
  275. });
  276. this._events = [];
  277. }
  278. /**
  279. * create a DOM element for the given event
  280. */
  281. _buildDiv(event) {
  282. const that = this;
  283. const container = this._plot.getPlaceholder();
  284. const o = this._plot.getPlotOffset();
  285. const xaxis = this._plot.getXAxes()[this._plot.getOptions().events.xaxis - 1];
  286. let top, left, color, markerSize, markerShow, lineStyle, lineWidth;
  287. let markerTooltip;
  288. // map the eventType to a types object
  289. const eventTypeId = event.eventType;
  290. if (this._types === null || !this._types[eventTypeId] || !this._types[eventTypeId].color) {
  291. color = '#666';
  292. } else {
  293. color = this._types[eventTypeId].color;
  294. }
  295. if (this._types === null || !this._types[eventTypeId] || !this._types[eventTypeId].markerSize) {
  296. markerSize = 8; //default marker size
  297. } else {
  298. markerSize = this._types[eventTypeId].markerSize;
  299. }
  300. if (this._types === null || !this._types[eventTypeId] || this._types[eventTypeId].markerShow === undefined) {
  301. markerShow = true;
  302. } else {
  303. markerShow = this._types[eventTypeId].markerShow;
  304. }
  305. if (this._types === null || !this._types[eventTypeId] || this._types[eventTypeId].markerTooltip === undefined) {
  306. markerTooltip = true;
  307. } else {
  308. markerTooltip = this._types[eventTypeId].markerTooltip;
  309. }
  310. if (this._types == null || !this._types[eventTypeId] || !this._types[eventTypeId].lineStyle) {
  311. lineStyle = 'dashed'; //default line style
  312. } else {
  313. lineStyle = this._types[eventTypeId].lineStyle.toLowerCase();
  314. }
  315. if (this._types == null || !this._types[eventTypeId] || this._types[eventTypeId].lineWidth === undefined) {
  316. lineWidth = 1; //default line width
  317. } else {
  318. lineWidth = this._types[eventTypeId].lineWidth;
  319. }
  320. let topOffset = xaxis.options.eventSectionHeight || 0;
  321. topOffset = topOffset / 3;
  322. top = o.top + this._plot.height() + topOffset;
  323. left = xaxis.p2c(event.min) + o.left;
  324. const line = $('<div class="events_line flot-temp-elem"></div>')
  325. .css({
  326. position: 'absolute',
  327. opacity: 0.8,
  328. left: left + 'px',
  329. top: 8,
  330. width: lineWidth + 'px',
  331. height: this._plot.height() + topOffset * 0.8,
  332. 'border-left-width': lineWidth + 'px',
  333. 'border-left-style': lineStyle,
  334. 'border-left-color': color,
  335. color: color,
  336. })
  337. .appendTo(container);
  338. if (markerShow) {
  339. const marker = $('<div class="events_marker"></div>').css({
  340. position: 'absolute',
  341. left: -markerSize - Math.round(lineWidth / 2) + 'px',
  342. 'font-size': 0,
  343. 'line-height': 0,
  344. width: 0,
  345. height: 0,
  346. 'border-left': markerSize + 'px solid transparent',
  347. 'border-right': markerSize + 'px solid transparent',
  348. });
  349. marker.appendTo(line);
  350. if (
  351. this._types[eventTypeId] &&
  352. this._types[eventTypeId].position &&
  353. this._types[eventTypeId].position.toUpperCase() === 'BOTTOM'
  354. ) {
  355. marker.css({
  356. top: top - markerSize - 8 + 'px',
  357. 'border-top': 'none',
  358. 'border-bottom': markerSize + 'px solid ' + color,
  359. });
  360. } else {
  361. marker.css({
  362. top: '0px',
  363. 'border-top': markerSize + 'px solid ' + color,
  364. 'border-bottom': 'none',
  365. });
  366. }
  367. marker.data({
  368. event: event,
  369. });
  370. const mouseenter = function(this: any) {
  371. createAnnotationToolip(marker, $(this).data('event'), that._plot);
  372. };
  373. if (event.editModel) {
  374. createEditPopover(marker, event.editModel, that._plot);
  375. }
  376. const mouseleave = () => {
  377. that._plot.clearSelection();
  378. };
  379. if (markerTooltip) {
  380. marker.css({ cursor: 'help' });
  381. marker.hover(mouseenter, mouseleave);
  382. }
  383. }
  384. const drawableEvent = new DrawableEvent(
  385. line,
  386. function drawFunc(obj) {
  387. obj.show();
  388. },
  389. obj => {
  390. obj.remove();
  391. },
  392. (obj, position) => {
  393. obj.css({
  394. top: position.top,
  395. left: position.left,
  396. });
  397. },
  398. left,
  399. top,
  400. line.width(),
  401. line.height()
  402. );
  403. return drawableEvent;
  404. }
  405. /**
  406. * create a DOM element for the given region
  407. */
  408. _buildRegDiv(event) {
  409. const that = this;
  410. const container = this._plot.getPlaceholder();
  411. const o = this._plot.getPlotOffset();
  412. const xaxis = this._plot.getXAxes()[this._plot.getOptions().events.xaxis - 1];
  413. let top, left, lineWidth, regionWidth, lineStyle, color, markerTooltip;
  414. // map the eventType to a types object
  415. const eventTypeId = event.eventType;
  416. if (this._types === null || !this._types[eventTypeId] || !this._types[eventTypeId].color) {
  417. color = '#666';
  418. } else {
  419. color = this._types[eventTypeId].color;
  420. }
  421. if (this._types === null || !this._types[eventTypeId] || this._types[eventTypeId].markerTooltip === undefined) {
  422. markerTooltip = true;
  423. } else {
  424. markerTooltip = this._types[eventTypeId].markerTooltip;
  425. }
  426. if (this._types == null || !this._types[eventTypeId] || this._types[eventTypeId].lineWidth === undefined) {
  427. lineWidth = 1; //default line width
  428. } else {
  429. lineWidth = this._types[eventTypeId].lineWidth;
  430. }
  431. if (this._types == null || !this._types[eventTypeId] || !this._types[eventTypeId].lineStyle) {
  432. lineStyle = 'dashed'; //default line style
  433. } else {
  434. lineStyle = this._types[eventTypeId].lineStyle.toLowerCase();
  435. }
  436. const topOffset = 2;
  437. top = o.top + this._plot.height() + topOffset;
  438. const timeFrom = Math.min(event.min, event.timeEnd);
  439. const timeTo = Math.max(event.min, event.timeEnd);
  440. left = xaxis.p2c(timeFrom) + o.left;
  441. const right = xaxis.p2c(timeTo) + o.left;
  442. regionWidth = right - left;
  443. _.each([left, right], position => {
  444. const line = $('<div class="events_line flot-temp-elem"></div>').css({
  445. position: 'absolute',
  446. opacity: 0.8,
  447. left: position + 'px',
  448. top: 8,
  449. width: lineWidth + 'px',
  450. height: this._plot.height() + topOffset,
  451. 'border-left-width': lineWidth + 'px',
  452. 'border-left-style': lineStyle,
  453. 'border-left-color': color,
  454. color: color,
  455. });
  456. line.appendTo(container);
  457. });
  458. const region = $('<div class="events_marker region_marker flot-temp-elem"></div>').css({
  459. position: 'absolute',
  460. opacity: 0.5,
  461. left: left + 'px',
  462. top: top,
  463. width: Math.round(regionWidth + lineWidth) + 'px',
  464. height: '0.5rem',
  465. 'border-left-color': color,
  466. color: color,
  467. 'background-color': color,
  468. });
  469. region.appendTo(container);
  470. region.data({
  471. event: event,
  472. });
  473. const mouseenter = function(this: any) {
  474. createAnnotationToolip(region, $(this).data('event'), that._plot);
  475. };
  476. if (event.editModel) {
  477. createEditPopover(region, event.editModel, that._plot);
  478. }
  479. const mouseleave = () => {
  480. that._plot.clearSelection();
  481. };
  482. if (markerTooltip) {
  483. region.css({ cursor: 'help' });
  484. region.hover(mouseenter, mouseleave);
  485. }
  486. const drawableEvent = new DrawableEvent(
  487. region,
  488. function drawFunc(obj) {
  489. obj.show();
  490. },
  491. obj => {
  492. obj.remove();
  493. },
  494. (obj, position) => {
  495. obj.css({
  496. top: position.top,
  497. left: position.left,
  498. });
  499. },
  500. left,
  501. top,
  502. region.width(),
  503. region.height()
  504. );
  505. return drawableEvent;
  506. }
  507. /**
  508. * check if the event is inside visible range
  509. */
  510. _insidePlot(x) {
  511. const xaxis = this._plot.getXAxes()[this._plot.getOptions().events.xaxis - 1];
  512. const xc = xaxis.p2c(x);
  513. return xc > 0 && xc < xaxis.p2c(xaxis.max);
  514. }
  515. }
  516. /**
  517. * initialize the plugin for the given plot
  518. */
  519. /** @ngInject */
  520. export function init(this: any, plot) {
  521. /*jshint validthis:true */
  522. const that = this;
  523. const eventMarkers = new EventMarkers(plot);
  524. plot.getEvents = () => {
  525. return eventMarkers._events;
  526. };
  527. plot.hideEvents = () => {
  528. $.each(eventMarkers._events, (index, event) => {
  529. event
  530. .visual()
  531. .getObject()
  532. .hide();
  533. });
  534. };
  535. plot.showEvents = () => {
  536. plot.hideEvents();
  537. $.each(eventMarkers._events, (index, event) => {
  538. event.hide();
  539. });
  540. that.eventMarkers.drawEvents();
  541. };
  542. // change events on an existing plot
  543. plot.setEvents = events => {
  544. if (eventMarkers.eventsEnabled) {
  545. eventMarkers.setupEvents(events);
  546. }
  547. };
  548. plot.hooks.processOptions.push((plot, options) => {
  549. // enable the plugin
  550. if (options.events.data != null) {
  551. eventMarkers.eventsEnabled = true;
  552. }
  553. });
  554. plot.hooks.draw.push(plot => {
  555. const options = plot.getOptions();
  556. if (eventMarkers.eventsEnabled) {
  557. // check for first run
  558. if (eventMarkers.getEvents().length < 1) {
  559. eventMarkers.setTypes(options.events.types);
  560. eventMarkers.setupEvents(options.events.data);
  561. } else {
  562. eventMarkers.updateEvents();
  563. }
  564. }
  565. eventMarkers.drawEvents();
  566. });
  567. }
  568. const defaultOptions = {
  569. events: {
  570. data: null,
  571. types: null,
  572. xaxis: 1,
  573. position: 'BOTTOM',
  574. },
  575. };
  576. $.plot.plugins.push({
  577. init: init,
  578. options: defaultOptions,
  579. name: 'events',
  580. version: '0.2.5',
  581. });