revieweditor.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (C) 2014 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 document, window, runtime, FileReader, alert, Uint8Array, Blob, saveAs, Wodo*/
  25. function createReviewEditor() {
  26. "use strict";
  27. var editor = null,
  28. editorOptions,
  29. loadedFilename;
  30. /*jslint emptyblock: true*/
  31. /**
  32. * @return {undefined}
  33. */
  34. function startEditing() {
  35. }
  36. /*jslint emptyblock: false*/
  37. /**
  38. * extract document url from the url-fragment
  39. *
  40. * @return {?string}
  41. */
  42. function guessDocUrl() {
  43. var pos, docUrl = String(document.location);
  44. // If the URL has a fragment (#...), try to load the file it represents
  45. pos = docUrl.indexOf('#');
  46. if (pos !== -1) {
  47. docUrl = docUrl.substr(pos + 1);
  48. } else {
  49. docUrl = "welcome.odt";
  50. }
  51. return docUrl || null;
  52. }
  53. function fileSelectHandler(evt) {
  54. var file, files, reader;
  55. files = (evt.target && evt.target.files) ||
  56. (evt.dataTransfer && evt.dataTransfer.files);
  57. function onLoadEnd() {
  58. if (reader.readyState === 2) {
  59. runtime.registerFile(file.name, reader.result);
  60. loadedFilename = file.name;
  61. editor.openDocumentFromUrl(loadedFilename, startEditing);
  62. }
  63. }
  64. if (files && files.length === 1) {
  65. if (!editor.isDocumentModified() ||
  66. window.confirm("There are unsaved changes to the file. Do you want to discard them?")) {
  67. editor.closeDocument(function() {
  68. file = files[0];
  69. reader = new FileReader();
  70. reader.onloadend = onLoadEnd;
  71. reader.readAsArrayBuffer(file);
  72. });
  73. }
  74. } else {
  75. alert("File could not be opened in this browser.");
  76. }
  77. }
  78. function enhanceRuntime() {
  79. var openedFiles = {},
  80. readFile = runtime.readFile;
  81. runtime.readFile = function (path, encoding, callback) {
  82. var array;
  83. if (openedFiles.hasOwnProperty(path)) {
  84. array = new Uint8Array(openedFiles[path]);
  85. callback(undefined, array);
  86. } else {
  87. return readFile(path, encoding, callback);
  88. }
  89. };
  90. runtime.registerFile = function (path, data) {
  91. openedFiles[path] = data;
  92. };
  93. }
  94. function createFileLoadForm() {
  95. var form = document.createElement("form"),
  96. input = document.createElement("input");
  97. function internalHandler(evt) {
  98. if (input.value !== "") {
  99. fileSelectHandler(evt);
  100. }
  101. // reset to "", so selecting the same file next time still trigger the change handler
  102. input.value = "";
  103. }
  104. form.appendChild(input);
  105. form.style.display = "none";
  106. input.id = "fileloader";
  107. input.setAttribute("type", "file");
  108. input.addEventListener("change", internalHandler, false);
  109. document.body.appendChild(form);
  110. }
  111. function load() {
  112. var form = document.getElementById("fileloader");
  113. if (!form) {
  114. enhanceRuntime();
  115. createFileLoadForm();
  116. form = document.getElementById("fileloader");
  117. }
  118. form.click();
  119. }
  120. function save() {
  121. function saveByteArrayLocally(err, data) {
  122. if (err) {
  123. alert(err);
  124. return;
  125. }
  126. // TODO: odfcontainer should have a property mimetype
  127. var mimetype = "application/vnd.oasis.opendocument.text",
  128. filename = loadedFilename || "doc.odt",
  129. blob = new Blob([data.buffer], {type: mimetype});
  130. saveAs(blob, filename);
  131. // TODO: hm, saveAs could fail or be cancelled
  132. editor.setDocumentModified(false);
  133. }
  134. editor.getDocumentAsByteArray(saveByteArrayLocally);
  135. }
  136. editorOptions = {
  137. loadCallback: load,
  138. saveCallback: save,
  139. modus: Wodo.MODUS_REVIEW,
  140. allFeaturesEnabled: true
  141. };
  142. function onEditorCreated(err, e) {
  143. var docUrl = guessDocUrl();
  144. if (err) {
  145. // something failed unexpectedly
  146. alert(err);
  147. return;
  148. }
  149. editor = e;
  150. editor.setUserData({
  151. fullName: "Curious WebODF-Reviewer",
  152. color: "black"
  153. });
  154. window.addEventListener("beforeunload", function (e) {
  155. var confirmationMessage = "There are unsaved changes to the file.";
  156. if (editor.isDocumentModified()) {
  157. // Gecko + IE
  158. (e || window.event).returnValue = confirmationMessage;
  159. // Webkit, Safari, Chrome etc.
  160. return confirmationMessage;
  161. }
  162. });
  163. if (docUrl) {
  164. loadedFilename = docUrl;
  165. editor.openDocumentFromUrl(docUrl, startEditing);
  166. }
  167. }
  168. Wodo.createTextEditor('editorContainer', editorOptions, onEditorCreated);
  169. }