zoomSlider.js 3.0 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*/
  25. define("webodf/editor/widgets/zoomSlider", [
  26. "dijit/form/HorizontalSlider",
  27. "dijit/form/NumberTextBox",
  28. "webodf/editor/EditorSession"],
  29. function (HorizontalSlider, NumberTextBox, EditorSession) {
  30. "use strict";
  31. // The slider zooms from -1 to +1, which corresponds
  32. // to zoom levels of 1/extremeZoomFactor to extremeZoomFactor.
  33. return function ZoomSlider(callback) {
  34. var self = this,
  35. editorSession,
  36. slider,
  37. extremeZoomFactor = 4;
  38. function updateSlider(zoomLevel) {
  39. slider.set('value', Math.log(zoomLevel) / Math.log(extremeZoomFactor), false);
  40. }
  41. this.setEditorSession = function(session) {
  42. var zoomHelper;
  43. if (editorSession) {
  44. editorSession.getOdfCanvas().getZoomHelper().unsubscribe(gui.ZoomHelper.signalZoomChanged, updateSlider);
  45. }
  46. editorSession = session;
  47. if (editorSession) {
  48. zoomHelper = editorSession.getOdfCanvas().getZoomHelper();
  49. zoomHelper.subscribe(gui.ZoomHelper.signalZoomChanged, updateSlider);
  50. updateSlider(zoomHelper.getZoomLevel());
  51. }
  52. slider.setAttribute('disabled', !editorSession);
  53. };
  54. this.onToolDone = function () {};
  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. });