jquery.flot.navigate-interaction.Test.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("flot navigate plugin interactions", function () {
  4. 'use strict';
  5. var placeholder, plot, eventHolder;
  6. var options = {
  7. xaxes: [{
  8. autoScale: 'exact'
  9. }],
  10. yaxes: [{
  11. autoScale: 'exact'
  12. }],
  13. zoom: {
  14. interactive: true,
  15. active: true,
  16. amount: 10
  17. },
  18. pan: {
  19. interactive: true,
  20. active: true
  21. }
  22. };
  23. beforeEach(function () {
  24. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  25. .find('#test-container');
  26. jasmine.clock().install();
  27. });
  28. afterEach(function () {
  29. jasmine.clock().uninstall();
  30. });
  31. it('do smart pans on mouse drag by default', function () {
  32. var oldFrameRate = options.pan.frameRate;
  33. options.pan.frameRate = -1;
  34. plot = $.plot(placeholder, [
  35. [[0, 0],
  36. [10, 10]]
  37. ], options);
  38. eventHolder = plot.getEventHolder();
  39. var xaxis = plot.getXAxes()[0];
  40. var yaxis = plot.getYAxes()[0];
  41. // drag almost horizontally snap to x direction
  42. simulate.mouseDown(eventHolder, 50, 70);
  43. simulate.mouseMove(eventHolder, 50, 70);
  44. simulate.mouseMove(eventHolder, 50 + plot.width(), 80);
  45. expect(xaxis.min).toBe(-10);
  46. expect(xaxis.max).toBe(0);
  47. expect(yaxis.min).toBe(0);
  48. expect(yaxis.max).toBe(10);
  49. simulate.mouseUp(eventHolder, 50 + plot.width(), 80);
  50. expect(xaxis.min).toBe(-10);
  51. expect(xaxis.max).toBe(0);
  52. expect(yaxis.min).toBe(0);
  53. expect(yaxis.max).toBe(10);
  54. // drag almost vertically snap to y direction
  55. simulate.mouseDown(eventHolder, 50, 70);
  56. simulate.mouseMove(eventHolder, 50, 70);
  57. simulate.mouseMove(eventHolder, 60, 70 + plot.height());
  58. expect(xaxis.min).toBe(-10);
  59. expect(xaxis.max).toBe(0);
  60. expect(yaxis.min).toBe(10);
  61. expect(yaxis.max).toBe(20);
  62. simulate.mouseUp(eventHolder, 60, 70 + plot.height());
  63. expect(xaxis.min).toBe(-10);
  64. expect(xaxis.max).toBe(0);
  65. expect(yaxis.min).toBe(10);
  66. expect(yaxis.max).toBe(20);
  67. // cover finite frame rate case
  68. plot.destroy();
  69. options.pan.frameRate = 10;
  70. plot = $.plot(placeholder, [
  71. [[0, 0],
  72. [10, 10]]
  73. ], options);
  74. eventHolder = plot.getEventHolder();
  75. xaxis = plot.getXAxes()[0];
  76. yaxis = plot.getYAxes()[0];
  77. // drag diagonally do not snap
  78. simulate.mouseDown(eventHolder, plot.width() - 50, plot.height() - 70);
  79. simulate.mouseMove(eventHolder, plot.width() - 50, plot.height() - 70);
  80. jasmine.clock().tick(100);
  81. simulate.mouseMove(eventHolder, -50, -70);
  82. jasmine.clock().tick(100);
  83. expect(xaxis.min).toBe(10);
  84. expect(xaxis.max).toBe(20);
  85. expect(yaxis.min).toBe(-10);
  86. expect(yaxis.max).toBe(0);
  87. simulate.mouseUp(eventHolder, -50, -70);
  88. expect(xaxis.min).toBe(10);
  89. expect(xaxis.max).toBe(20);
  90. expect(yaxis.min).toBe(-10);
  91. expect(yaxis.max).toBe(0);
  92. options.pan.frameRate = oldFrameRate;
  93. });
  94. it('do non-smart pans on mouse drag in non-smart pan mode', function () {
  95. var oldPanMode = options.pan.mode;
  96. options.pan.mode = 'manual';
  97. var oldFrameRate = options.pan.frameRate;
  98. options.pan.frameRate = -1;
  99. plot = $.plot(placeholder, [
  100. [[0, 0],
  101. [10, 10]]
  102. ], options);
  103. eventHolder = plot.getEventHolder();
  104. var xaxis = plot.getXAxes()[0];
  105. var yaxis = plot.getYAxes()[0];
  106. // drag almost horizontally do not snap
  107. var movement = { x: [50, 50 + plot.width()], y: [70, 80] };
  108. simulate.mouseDown(eventHolder, movement.x[0], movement.y[0]);
  109. simulate.mouseMove(eventHolder, movement.x[0], movement.y[0]);
  110. simulate.mouseMove(eventHolder, movement.x[1], movement.y[1]);
  111. expect(xaxis.min).toBe(-10);
  112. expect(xaxis.max).toBe(0);
  113. expect(yaxis.min).toBeGreaterThan(0);
  114. expect(yaxis.max).toBeGreaterThan(10);
  115. simulate.mouseUp(eventHolder, movement.x[1], movement.y[1]);
  116. // cover finite frame rate case
  117. plot.destroy();
  118. options.pan.frameRate = 10;
  119. plot = $.plot(placeholder, [
  120. [[0, 0],
  121. [10, 10]]
  122. ], options);
  123. eventHolder = plot.getEventHolder();
  124. xaxis = plot.getXAxes()[0];
  125. yaxis = plot.getYAxes()[0];
  126. // drag almost vertically do not snap
  127. movement = { x: [50, 60], y: [70, 70 + plot.height()] };
  128. simulate.mouseDown(eventHolder, movement.x[0], movement.y[0]);
  129. simulate.mouseMove(eventHolder, movement.x[0], movement.y[0]);
  130. jasmine.clock().tick(100);
  131. simulate.mouseMove(eventHolder, movement.x[1], movement.y[1]);
  132. jasmine.clock().tick(100);
  133. expect(xaxis.min).toBeLessThan(0);
  134. expect(xaxis.max).toBeLessThan(10);
  135. expect(yaxis.min).toBe(10);
  136. expect(yaxis.max).toBe(20);
  137. simulate.mouseUp(eventHolder, movement.x[1], movement.y[1]);
  138. options.pan.mode = oldPanMode;
  139. options.pan.frameRate = oldFrameRate;
  140. });
  141. it('lock smart pan snap direction on mouse drag in smart-lock pan mode', function () {
  142. var oldPanMode = options.pan.mode;
  143. options.pan.mode = 'smartLock';
  144. var oldFrameRate = options.pan.frameRate;
  145. options.pan.frameRate = -1;
  146. plot = $.plot(placeholder, [
  147. [[0, 0],
  148. [10, 10]]
  149. ], options);
  150. eventHolder = plot.getEventHolder();
  151. var xaxis = plot.getXAxes()[0];
  152. var yaxis = plot.getYAxes()[0];
  153. var initialXmin = xaxis.min,
  154. initialXmax = xaxis.max,
  155. initialYmin = yaxis.min,
  156. initialYmax = yaxis.max;
  157. // drag almost horizontally then vertically snap to x direction
  158. simulate.mouseDown(eventHolder, 50, 70);
  159. simulate.mouseMove(eventHolder, 50, 70);
  160. simulate.mouseMove(eventHolder, 50 + plot.width() / 2, 80);
  161. simulate.mouseMove(eventHolder, 50 + plot.width(), 70 + plot.height());
  162. simulate.mouseUp(eventHolder, 50 + plot.width(), 70 + plot.height());
  163. expect(xaxis.min).toBeLessThan(initialXmin);
  164. expect(xaxis.max).toBeLessThan(initialXmax);
  165. expect(yaxis.min).toBe(initialYmin);
  166. expect(yaxis.max).toBe(initialYmax);
  167. // drag almost vertically then horizontally snap to y direction
  168. plot.recenter({});
  169. simulate.mouseDown(eventHolder, 50, 70);
  170. simulate.mouseMove(eventHolder, 50, 70);
  171. simulate.mouseMove(eventHolder, 60, 70 + plot.height());
  172. simulate.mouseMove(eventHolder, 50 + plot.width(), 70 + plot.height());
  173. simulate.mouseUp(eventHolder, 50 + plot.width(), 70 + plot.height());
  174. expect(xaxis.min).toBe(initialXmin);
  175. expect(xaxis.max).toBe(initialXmax);
  176. expect(yaxis.min).toBeGreaterThan(initialYmin);
  177. expect(yaxis.max).toBeGreaterThan(initialYmax);
  178. options.pan.mode = oldPanMode;
  179. options.pan.frameRate = oldFrameRate;
  180. });
  181. it('do not move graph on mouse drag if pan mode is invalid', function () {
  182. var oldPanMode = options.pan.mode;
  183. options.pan.mode = '';
  184. var oldFrameRate = options.pan.frameRate;
  185. options.pan.frameRate = -1;
  186. plot = $.plot(placeholder, [
  187. [[0, 0],
  188. [10, 10]]
  189. ], options);
  190. eventHolder = plot.getEventHolder();
  191. var xaxis = plot.getXAxes()[0];
  192. var yaxis = plot.getYAxes()[0];
  193. var initialXmin = xaxis.min,
  194. initialXmax = xaxis.max,
  195. initialYmin = yaxis.min,
  196. initialYmax = yaxis.max;
  197. // do not drag in all cases
  198. simulate.mouseDown(eventHolder, 50, 70);
  199. simulate.mouseMove(eventHolder, 50, 70);
  200. simulate.mouseMove(eventHolder, 50 + plot.width() / 2, 70);
  201. simulate.mouseUp(eventHolder, 50 + plot.width(), 70);
  202. expect(xaxis.min).toBe(initialXmin);
  203. expect(xaxis.max).toBe(initialXmax);
  204. expect(yaxis.min).toBe(initialYmin);
  205. expect(yaxis.max).toBe(initialYmax);
  206. simulate.mouseDown(eventHolder, 50, 70);
  207. simulate.mouseMove(eventHolder, 50, 70);
  208. simulate.mouseMove(eventHolder, 50, 70 + plot.height());
  209. simulate.mouseUp(eventHolder, 50, 70 + plot.height());
  210. expect(xaxis.min).toBe(initialXmin);
  211. expect(xaxis.max).toBe(initialXmax);
  212. expect(yaxis.min).toBe(initialYmin);
  213. expect(yaxis.max).toBe(initialYmax);
  214. options.pan.mode = oldPanMode;
  215. options.pan.frameRate = oldFrameRate;
  216. });
  217. it('zooms out on mouse scroll down', function () {
  218. plot = $.plot(placeholder, [
  219. [[0, 0],
  220. [10, 10]]
  221. ], options);
  222. var xaxis = plot.getXAxes()[0];
  223. var yaxis = plot.getYAxes()[0];
  224. var clientX = plot.getPlotOffset().left + xaxis.p2c(0);
  225. var clientY = plot.getPlotOffset().top + yaxis.p2c(0);
  226. eventHolder = plot.getEventHolder();
  227. simulate.mouseWheel(eventHolder, clientX, clientY, 0, 100);
  228. /*
  229. I would really like better precission but:
  230. * the browsers may place the graph to fractional pixel coordinates
  231. * we can only deliver mouse events at integer coordinates
  232. * so we can't align precisely our mouse clicks with a point specified in plot coordinates
  233. hence our precission sucks.
  234. But this test isn't about precission, so we are fine
  235. */
  236. expect(xaxis.min).toBeCloseTo(0, 0);
  237. expect(xaxis.max).toBeCloseTo(100, 0);
  238. expect(yaxis.min).toBeCloseTo(0, 0);
  239. expect(yaxis.max).toBeCloseTo(100, 0);
  240. });
  241. it('zooms in on mouse scroll up', function () {
  242. plot = $.plot(placeholder, [
  243. [[0, 0],
  244. [10, 10]]
  245. ], options);
  246. var xaxis = plot.getXAxes()[0];
  247. var yaxis = plot.getYAxes()[0];
  248. var clientX = plot.getPlotOffset().left + xaxis.p2c(0);
  249. var clientY = plot.getPlotOffset().top + yaxis.p2c(0);
  250. eventHolder = plot.getEventHolder();
  251. simulate.mouseWheel(eventHolder, clientX, clientY, 0, -100);
  252. /*
  253. I would really like better precission but:
  254. * the browsers may place the graph to fractional pixel coordinates
  255. * we can only deliver mouse events at integer coordinates
  256. * so we can't align precisely our mouse clicks with a point specified in plot coordinates
  257. hence our precission sucks.
  258. But this test isn't about precission, so we are fine
  259. */
  260. expect(xaxis.min).toBeCloseTo(0, 1);
  261. expect(xaxis.max).toBeCloseTo(1, 1);
  262. expect(yaxis.min).toBeCloseTo(0, 1);
  263. expect(yaxis.max).toBeCloseTo(1, 1);
  264. });
  265. it('constrains the mouse scroll zoom to the hovered axis ', function () {
  266. plot = $.plot(placeholder, [
  267. [[0, 0],
  268. [10, 10]]
  269. ], options);
  270. var xaxis = plot.getXAxes()[0];
  271. var yaxis = plot.getYAxes()[0];
  272. var clientX = plot.getPlotOffset().left + xaxis.p2c(0);
  273. var clientY = xaxis.box.top + xaxis.box.height/2;
  274. eventHolder = plot.getEventHolder();
  275. simulate.mouseWheel(eventHolder, clientX, clientY, 0, -100);
  276. expect(xaxis.min).toBeCloseTo(0, 1);
  277. expect(xaxis.max).toBeCloseTo(1, 1);
  278. expect(yaxis.min).toBeCloseTo(0, 1);
  279. expect(yaxis.max).toBeCloseTo(10, 1);
  280. });
  281. it('zooms out proportional with the deltaY on Mac platforms', function () {
  282. var smallAmount = 0.4,
  283. largerAmount = 0.8,
  284. plot1Ranges = plotAndScroll(smallAmount),
  285. plot2Ranges = plotAndScroll(largerAmount);
  286. expect(plot1Ranges.xaxisMin).toBeLessThan(plot2Ranges.xaxisMin);
  287. expect(plot1Ranges.xaxisMax).toBeGreaterThan(plot2Ranges.xaxisMax);
  288. expect(plot1Ranges.yaxisMin).toBeLessThan(plot2Ranges.yaxisMin);
  289. expect(plot1Ranges.yaxisMax).toBeGreaterThan(plot2Ranges.yaxisMax);
  290. });
  291. it('zooms out regardless the deltaY value on non Mac platforms', function () {
  292. var smallAmount = 40,
  293. largerAmount = 80,
  294. plot1Ranges = plotAndScroll(smallAmount),
  295. plot2Ranges = plotAndScroll(largerAmount);
  296. expect(plot2Ranges.xaxisMin).toBeCloseTo(plot1Ranges.xaxisMin);
  297. expect(plot2Ranges.xaxisMax).toBeCloseTo(plot1Ranges.xaxisMax);
  298. expect(plot2Ranges.yaxisMin).toBeCloseTo(plot1Ranges.yaxisMin);
  299. expect(plot2Ranges.yaxisMax).toBeCloseTo(plot1Ranges.yaxisMax);
  300. });
  301. function plotAndScroll(amount) {
  302. plot = $.plot(placeholder, [
  303. [[0, 0],
  304. [10, 10]]
  305. ], options);
  306. var xaxis = plot.getXAxes()[0];
  307. var yaxis = plot.getYAxes()[0];
  308. var clientX = plot.getPlotOffset().left + xaxis.p2c(5);
  309. var clientY = plot.getPlotOffset().top + yaxis.p2c(5);
  310. eventHolder = plot.getEventHolder();
  311. simulate.mouseWheel(eventHolder, clientX, clientY, 0, amount);
  312. return {
  313. xaxisMin: xaxis.min,
  314. xaxisMax: xaxis.max,
  315. yaxisMin: yaxis.min,
  316. yaxisMax: yaxis.max
  317. };
  318. }
  319. it('zooms mode handles event on mouse dblclick', function () {
  320. plot = $.plot(placeholder, [
  321. [[0, 0],
  322. [10, 10]]
  323. ], {
  324. xaxes: [{
  325. autoScale: 'exact'
  326. }],
  327. yaxes: [{
  328. autoScale: 'exact'
  329. }],
  330. zoom: {
  331. interactive: false,
  332. highlighted: true
  333. },
  334. pan: {
  335. interactive: true,
  336. highlighted: true
  337. },
  338. selection: {
  339. mode: 'smart',
  340. }});
  341. var xaxis = plot.getXAxes()[0];
  342. var yaxis = plot.getYAxes()[0];
  343. var clientX = plot.getPlotOffset().left + xaxis.p2c(0);
  344. var clientY = plot.getPlotOffset().top + yaxis.p2c(0);
  345. eventHolder = plot.getEventHolder();
  346. var spy = spyOn(eventHolder, 'ondblclick').and.callThrough();
  347. var spyRecenter = jasmine.createSpy('spy');
  348. $(plot.getPlaceholder()).on('re-center', spyRecenter);
  349. simulate.dblclick(eventHolder, 10, 20);
  350. expect(spy).toHaveBeenCalled();
  351. expect(spyRecenter).toHaveBeenCalled();
  352. });
  353. it('shows that the eventHolder is cleared through shutdown when the plot is replaced', function() {
  354. plot = $.plot(placeholder, [[]], options);
  355. var eventHolder = plot.getEventHolder(),
  356. spy = spyOn(eventHolder, 'removeEventListener').and.callThrough();
  357. plot = $.plot(placeholder, [[]], options);
  358. expect(spy).toHaveBeenCalledWith('mousewheel', jasmine.any(Function), jasmine.any(Boolean))
  359. expect(spy).toHaveBeenCalledWith('dblclick', jasmine.any(Function), jasmine.any(Boolean));
  360. });
  361. it('do recenter for double click by default', () => {
  362. plot = $.plot(placeholder, [
  363. [[0, 0], [10, 10]]
  364. ], {
  365. xaxes: [{ autoScale: 'exact' }],
  366. yaxes: [{ autoScale: 'exact' }],
  367. });
  368. var eventHolder = plot.getEventHolder(),
  369. xaxis = plot.getXAxes()[0],
  370. yaxis = plot.getYAxes()[0],
  371. spyRecenter = jasmine.createSpy('spy');
  372. $(plot.getPlaceholder()).on('re-center', spyRecenter);
  373. plot.pan({ left: 10, top: 10});
  374. simulate.dblclick(eventHolder, 200, 150);
  375. expect(xaxis.options.offset).toEqual({ below: 0, above: 0 });
  376. expect(yaxis.options.offset).toEqual({ below: 0, above: 0 });
  377. expect(spyRecenter).toHaveBeenCalled();
  378. });
  379. it('do not recenter for double click if recenter is disabled', () => {
  380. plot = $.plot(placeholder, [
  381. [[0, 0], [10, 10]]
  382. ], {
  383. xaxes: [{ autoScale: 'exact' }],
  384. yaxes: [{ autoScale: 'exact' }],
  385. pan: { interactive: true },
  386. zoom: { interactive: true },
  387. recenter: { interactive: false },
  388. });
  389. var eventHolder = plot.getEventHolder(),
  390. xaxis = plot.getXAxes()[0],
  391. yaxis = plot.getYAxes()[0],
  392. spyRecenter = jasmine.createSpy('spy');
  393. $(plot.getPlaceholder()).on('re-center', spyRecenter);
  394. plot.pan({ left: 10, top: 10});
  395. simulate.dblclick(eventHolder, 200, 150);
  396. expect(xaxis.options.offset).not.toEqual({ below: 0, above: 0 });
  397. expect(yaxis.options.offset).not.toEqual({ below: 0, above: 0 });
  398. expect(spyRecenter).not.toHaveBeenCalled();
  399. });
  400. });