localfileeditor.js 5.8 KB

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