imageInserter.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, document, Image, FileReader, window, runtime, ops, gui */
  25. define("webodf/editor/widgets/imageInserter", [
  26. "dijit/form/Button",
  27. "webodf/editor/EditorSession"],
  28. function (Button, EditorSession) {
  29. "use strict";
  30. var ImageInserter = function (callback) {
  31. var self = this,
  32. widget = {},
  33. insertImageButton,
  34. editorSession,
  35. fileLoader,
  36. textController,
  37. imageController;
  38. /**
  39. *
  40. * @param {!string} mimetype
  41. * @param {!string} content base64 encoded string
  42. * @param {!number} width
  43. * @param {!number} height
  44. */
  45. function insertImage(mimetype, content, width, height) {
  46. textController.removeCurrentSelection();
  47. imageController.insertImage(mimetype, content, width, height);
  48. }
  49. /**
  50. * @param {!string} content as datauri
  51. * @param {!string} mimetype
  52. * @return {undefined}
  53. */
  54. function insertImageOnceLoaded(mimetype, content) {
  55. var hiddenImage = new Image();
  56. hiddenImage.style.position = "absolute";
  57. hiddenImage.style.left = "-99999px";
  58. document.body.appendChild(hiddenImage);
  59. hiddenImage.onload = function () {
  60. // remove the data:image/jpg;base64, bit
  61. content = content.substring(content.indexOf(",") + 1);
  62. insertImage(mimetype, content, hiddenImage.width, hiddenImage.height);
  63. // clean up
  64. document.body.removeChild(hiddenImage);
  65. self.onToolDone();
  66. };
  67. hiddenImage.src = content;
  68. }
  69. function fileSelectHandler(evt) {
  70. var file, files, reader;
  71. files = (evt.target && evt.target.files) || (evt.dataTransfer && evt.dataTransfer.files);
  72. if (files && files.length === 1) {
  73. file = files[0];
  74. reader = new FileReader();
  75. reader.onloadend = function () {
  76. if (reader.readyState === 2) {
  77. insertImageOnceLoaded(file.type, reader.result);
  78. } else {
  79. runtime.log("Image could not be loaded");
  80. self.onToolDone();
  81. }
  82. };
  83. reader.readAsDataURL(file);
  84. }
  85. }
  86. function createFileLoader() {
  87. var form = document.createElement("form"),
  88. input = document.createElement("input");
  89. form.appendChild(input);
  90. form.id = "imageForm";
  91. form.style.display = "none";
  92. input.id = "imageLoader";
  93. input.setAttribute("type", "file");
  94. input.setAttribute("accept", "image/*");
  95. input.addEventListener("change", fileSelectHandler, false);
  96. document.body.appendChild(form);
  97. return {input: input, form: form};
  98. }
  99. insertImageButton = new Button({
  100. label: runtime.tr("Insert Image"),
  101. disabled: true,
  102. showLabel: false,
  103. iconClass: "dijitEditorIcon dijitEditorIconInsertImage",
  104. onClick: function () {
  105. if (!fileLoader) {
  106. fileLoader = createFileLoader();
  107. }
  108. fileLoader.form.reset();
  109. fileLoader.input.click();
  110. }
  111. });
  112. widget.children = [insertImageButton];
  113. widget.startup = function () {
  114. widget.children.forEach(function (element) {
  115. element.startup();
  116. });
  117. };
  118. widget.placeAt = function (container) {
  119. widget.children.forEach(function (element) {
  120. element.placeAt(container);
  121. });
  122. return widget;
  123. };
  124. function enableButtons(isEnabled) {
  125. widget.children.forEach(function (element) {
  126. element.setAttribute('disabled', !isEnabled);
  127. });
  128. }
  129. function handleCursorMoved(cursor) {
  130. if (imageController.isEnabled()) {
  131. var disabled = cursor.getSelectionType() === ops.OdtCursor.RegionSelection;
  132. // LO/AOO pops up the picture/frame option dialog if image is selected when pressing the button
  133. // Since we only support inline images, disable the button for now.
  134. insertImageButton.setAttribute('disabled', disabled);
  135. }
  136. }
  137. this.setEditorSession = function (session) {
  138. if (editorSession) {
  139. editorSession.unsubscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  140. imageController.unsubscribe(gui.ImageController.enabledChanged, enableButtons);
  141. }
  142. editorSession = session;
  143. if (editorSession) {
  144. textController = editorSession.sessionController.getTextController();
  145. imageController = editorSession.sessionController.getImageController();
  146. editorSession.subscribe(EditorSession.signalCursorMoved, handleCursorMoved);
  147. imageController.subscribe(gui.ImageController.enabledChanged, enableButtons);
  148. enableButtons(imageController.isEnabled());
  149. } else {
  150. enableButtons(false);
  151. }
  152. };
  153. /*jslint emptyblock: true*/
  154. this.onToolDone = function () {};
  155. /*jslint emptyblock: false*/
  156. callback(widget);
  157. };
  158. return ImageInserter;
  159. });