ScaleManager.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Storytelling
  5. * (C) Copyright IBM Corp. 2014, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['baglass/core-client/js/core-client/ui/core/Events'], function (Events) {
  9. var ScaleManager = Events.extend({
  10. defaultPixelsPerSecond: 1,
  11. minScale: 1,
  12. maxScale: 250,
  13. scale: 1,
  14. duration: 0,
  15. availableWidth: 0,
  16. init: function init() {
  17. ScaleManager.inherited('init', this, arguments);
  18. },
  19. updateDuration: function updateDuration(duration, availableWidth) {
  20. this.duration = duration;
  21. this.availableWidth = availableWidth;
  22. },
  23. scaleToFit: function scaleToFit() {
  24. var scale = this._calculateFitToSizeScale();
  25. this.setScale(scale);
  26. },
  27. /**
  28. * @param step - a scale factor to apply
  29. */
  30. stepScale: function stepScale(step) {
  31. if (step !== 0) {
  32. this.setScale(this.scale * step);
  33. }
  34. },
  35. setScale: function setScale(scale) {
  36. var previousScale = this.scale;
  37. this.scale = Math.max(this.minScale, Math.min(this.maxScale, scale));
  38. this.trigger('scale:change', { scale: this.scale, previousScale: previousScale });
  39. },
  40. getScale: function getScale() {
  41. return this.scale;
  42. },
  43. getScaleToPixelRatio: function getScaleToPixelRatio() {
  44. return this.defaultPixelsPerSecond;
  45. },
  46. getMinScale: function getMinScale() {
  47. return this.minScale;
  48. },
  49. getMaxScale: function getMaxScale() {
  50. return this.maxScale;
  51. },
  52. convertTimeToPosition: function convertTimeToPosition(time) {
  53. return time / 1000 * this.defaultPixelsPerSecond * (this.scale || 1);
  54. },
  55. convertPositionToTime: function convertPositionToTime(position) {
  56. return position / (this.defaultPixelsPerSecond * (this.scale || 1)) * 1000;
  57. },
  58. /*
  59. * Helpers.
  60. */
  61. _calculateFitToSizeScale: function _calculateFitToSizeScale() {
  62. var position = this.convertTimeToPosition(this.duration);
  63. var ratio = position > 0 ? this.availableWidth / position : 1;
  64. return this.scale * ratio;
  65. }
  66. });
  67. return ScaleManager;
  68. });
  69. //# sourceMappingURL=ScaleManager.js.map