zoomSlider.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. /*global define, require, gui*/
  25. define("webodf/editor/widgets/zoomSlider", [
  26. "dijit/form/HorizontalSlider"],
  27. function (HorizontalSlider) {
  28. "use strict";
  29. // The slider zooms from -1 to +1, which corresponds
  30. // to zoom levels of 1/extremeZoomFactor to extremeZoomFactor.
  31. return function ZoomSlider(callback) {
  32. var self = this,
  33. editorSession,
  34. slider,
  35. extremeZoomFactor = 4;
  36. function updateSlider(zoomLevel) {
  37. slider.set('value', Math.log(zoomLevel) / Math.log(extremeZoomFactor), false);
  38. }
  39. this.setEditorSession = function (session) {
  40. var zoomHelper;
  41. if (editorSession) {
  42. editorSession.getOdfCanvas().getZoomHelper().unsubscribe(gui.ZoomHelper.signalZoomChanged, updateSlider);
  43. }
  44. editorSession = session;
  45. if (editorSession) {
  46. zoomHelper = editorSession.getOdfCanvas().getZoomHelper();
  47. zoomHelper.subscribe(gui.ZoomHelper.signalZoomChanged, updateSlider);
  48. updateSlider(zoomHelper.getZoomLevel());
  49. }
  50. slider.setAttribute('disabled', !editorSession);
  51. };
  52. /*jslint emptyblock: true*/
  53. this.onToolDone = function () {};
  54. /*jslint emptyblock: false*/
  55. // init
  56. function init() {
  57. slider = new HorizontalSlider({
  58. name: 'zoomSlider',
  59. disabled: true,
  60. value: 0,
  61. minimum: -1,
  62. maximum: 1,
  63. discreteValues: 0.01,
  64. intermediateChanges: true,
  65. style: {
  66. width: '150px',
  67. height: '25px',
  68. float: 'right'
  69. }
  70. });
  71. slider.onChange = function (value) {
  72. if (editorSession) {
  73. editorSession.getOdfCanvas().getZoomHelper().setZoomLevel(Math.pow(extremeZoomFactor, value));
  74. }
  75. self.onToolDone();
  76. };
  77. return callback(slider);
  78. }
  79. init();
  80. };
  81. });