ODFViewerPlugin.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (C) 2012 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 runtime, document, odf, gui, console, webodf*/
  25. function ODFViewerPlugin() {
  26. "use strict";
  27. function init(callback) {
  28. var lib = document.createElement('script'),
  29. pluginCSS;
  30. lib.async = false;
  31. lib.src = './webodf.js';
  32. lib.type = 'text/javascript';
  33. lib.onload = function () {
  34. runtime.loadClass('gui.HyperlinkClickHandler');
  35. runtime.loadClass('odf.OdfCanvas');
  36. runtime.loadClass('ops.Session');
  37. runtime.loadClass('gui.CaretManager');
  38. runtime.loadClass("gui.HyperlinkTooltipView");
  39. runtime.loadClass('gui.SessionController');
  40. runtime.loadClass('gui.SvgSelectionView');
  41. runtime.loadClass('gui.SelectionViewManager');
  42. runtime.loadClass('gui.ShadowCursor');
  43. runtime.loadClass('gui.SessionView');
  44. callback();
  45. };
  46. document.getElementsByTagName('head')[0].appendChild(lib);
  47. pluginCSS = document.createElement('link');
  48. pluginCSS.setAttribute("rel", "stylesheet");
  49. pluginCSS.setAttribute("type", "text/css");
  50. pluginCSS.setAttribute("href", "./ODFViewerPlugin.css");
  51. document.head.appendChild(pluginCSS);
  52. }
  53. // that should probably be provided by webodf
  54. function nsResolver(prefix) {
  55. var ns = {
  56. 'draw' : "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
  57. 'presentation' : "urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
  58. 'text' : "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
  59. 'office' : "urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  60. };
  61. return ns[prefix] || console.log('prefix [' + prefix + '] unknown.');
  62. }
  63. var self = this,
  64. pluginName = "WebODF",
  65. pluginURL = "http://webodf.org",
  66. odfCanvas = null,
  67. odfElement = null,
  68. initialized = false,
  69. root = null,
  70. documentType = null,
  71. pages = [],
  72. currentPage = null;
  73. this.initialize = function (viewerElement, documentUrl) {
  74. // If the URL has a fragment (#...), try to load the file it represents
  75. init(function () {
  76. var session,
  77. sessionController,
  78. sessionView,
  79. odtDocument,
  80. shadowCursor,
  81. selectionViewManager,
  82. caretManager,
  83. localMemberId = 'localuser',
  84. hyperlinkTooltipView,
  85. eventManager;
  86. odfElement = document.getElementById('canvas');
  87. odfCanvas = new odf.OdfCanvas(odfElement);
  88. odfCanvas.load(documentUrl);
  89. odfCanvas.addListener('statereadychange', function () {
  90. root = odfCanvas.odfContainer().rootElement;
  91. initialized = true;
  92. documentType = odfCanvas.odfContainer().getDocumentType(root);
  93. if (documentType === 'text') {
  94. odfCanvas.enableAnnotations(true, false);
  95. session = new ops.Session(odfCanvas);
  96. odtDocument = session.getOdtDocument();
  97. shadowCursor = new gui.ShadowCursor(odtDocument);
  98. sessionController = new gui.SessionController(session, localMemberId, shadowCursor, {});
  99. eventManager = sessionController.getEventManager();
  100. caretManager = new gui.CaretManager(sessionController);
  101. selectionViewManager = new gui.SelectionViewManager(gui.SvgSelectionView);
  102. sessionView = new gui.SessionView({
  103. caretAvatarsInitiallyVisible: false
  104. }, localMemberId, session, sessionController.getSessionConstraints(), caretManager, selectionViewManager);
  105. selectionViewManager.registerCursor(shadowCursor);
  106. hyperlinkTooltipView = new gui.HyperlinkTooltipView(odfCanvas,
  107. sessionController.getHyperlinkClickHandler().getModifier);
  108. eventManager.subscribe("mousemove", hyperlinkTooltipView.showTooltip);
  109. eventManager.subscribe("mouseout", hyperlinkTooltipView.hideTooltip);
  110. var op = new ops.OpAddMember();
  111. op.init({
  112. memberid: localMemberId,
  113. setProperties: {
  114. fillName: runtime.tr("Unknown Author"),
  115. color: "blue"
  116. }
  117. });
  118. session.enqueue([op]);
  119. sessionController.insertLocalCursor();
  120. }
  121. self.onLoad();
  122. });
  123. });
  124. };
  125. this.isSlideshow = function () {
  126. return documentType === 'presentation';
  127. };
  128. this.onLoad = function () {};
  129. this.getWidth = function () {
  130. return odfElement.clientWidth;
  131. };
  132. this.getHeight = function () {
  133. return odfElement.clientHeight;
  134. };
  135. this.fitToWidth = function (width) {
  136. odfCanvas.fitToWidth(width);
  137. };
  138. this.fitToHeight = function (height) {
  139. odfCanvas.fitToHeight(height);
  140. };
  141. this.fitToPage = function (width, height) {
  142. odfCanvas.fitToContainingElement(width, height);
  143. };
  144. this.fitSmart = function (width) {
  145. odfCanvas.fitSmart(width);
  146. };
  147. this.getZoomLevel = function () {
  148. return odfCanvas.getZoomLevel();
  149. };
  150. this.setZoomLevel = function (value) {
  151. odfCanvas.setZoomLevel(value);
  152. };
  153. // return a list of tuples (pagename, pagenode)
  154. this.getPages = function () {
  155. var pageNodes = Array.prototype.slice.call(root.getElementsByTagNameNS(nsResolver('draw'), 'page')),
  156. pages = [],
  157. i,
  158. tuple;
  159. for (i = 0; i < pageNodes.length; i += 1) {
  160. tuple = [
  161. pageNodes[i].getAttribute('draw:name'),
  162. pageNodes[i]
  163. ];
  164. pages.push(tuple);
  165. }
  166. return pages;
  167. };
  168. this.showPage = function (n) {
  169. odfCanvas.showPage(n);
  170. };
  171. this.getPluginName = function () {
  172. return pluginName;
  173. };
  174. this.getPluginVersion = function () {
  175. var version;
  176. if (String(typeof webodf) !== "undefined") {
  177. version = webodf.Version;
  178. } else {
  179. version = "Unknown";
  180. }
  181. return version;
  182. };
  183. this.getPluginURL = function () {
  184. return pluginURL;
  185. };
  186. }