not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
8.4 KiB

  1. // Rebase require directory
  2. requirejs.config({
  3. baseUrl: "lib"
  4. });
  5. // Default library url
  6. var defaultUrlLibrary = (document.location.protocol.substr(0,5)=="https"?"https":"http") + "://sugarizer.org/content/books.php";
  7. // Vue main app
  8. var app = new Vue({
  9. el: '#app',
  10. components: { 'ebook-reader': EbookReader, 'library-viewer': LibraryViewer, 'toolbar': Toolbar, 'localization': Localization, 'popup': Popup, 'tutorial': Tutorial },
  11. data: {
  12. currentBook: null,
  13. currentEpub: null,
  14. currentView: LibraryViewer,
  15. currentLibrary: {database: []},
  16. timer: null
  17. },
  18. created: function() {
  19. requirejs(["sugar-web/activity/activity", "sugar-web/env"], function(activity, env) {
  20. // Initialize Sugarizer
  21. activity.setup();
  22. });
  23. },
  24. mounted: function() {
  25. // Load last library from Journal
  26. var vm = this;
  27. requirejs(["sugar-web/activity/activity", "sugar-web/env"], function(activity, env) {
  28. env.getEnvironment(function(err, environment) {
  29. // Use buddy color for background
  30. env.getEnvironment(function(err, environment) {
  31. document.getElementById("canvas").style.backgroundColor = environment.user.colorvalue.fill;
  32. });
  33. // Load context
  34. if (environment.objectId) {
  35. activity.getDatastoreObject().loadAsText(function(error, metadata, data) {
  36. if (error==null && data!=null) {
  37. var parsed = JSON.parse(data);
  38. vm.currentLibrary = parsed.library;
  39. if (parsed.current !== undefined) {
  40. vm.currentBook = vm.currentLibrary.database[parsed.current];
  41. vm.currentEpub = ePub(vm.currentLibrary.information.fileprefix+vm.currentBook.file);
  42. vm.currentView = EbookReader;
  43. } else if (vm.currentLibrary.database.length == 0) {
  44. vm.loadLibrary(defaultUrlLibrary);
  45. }
  46. document.getElementById("spinner").style.visibility = "hidden";
  47. }
  48. });
  49. } else {
  50. vm.loadLibrary(defaultUrlLibrary);
  51. }
  52. });
  53. });
  54. // Handle resize
  55. window.addEventListener("resize", function() {
  56. vm.onResize();
  57. });
  58. // Handle unfull screen buttons (b)
  59. document.getElementById("unfullscreen-button").addEventListener('click', function() {
  60. vm.unfullscreen();
  61. });
  62. },
  63. updated: function() {
  64. if (this.currentView === EbookReader) {
  65. this.$refs.view.render(this.currentEpub, this.currentBook.location);
  66. }
  67. },
  68. methods: {
  69. localized: function() {
  70. this.$refs.toolbar.localized(this.$refs.localization);
  71. this.$refs.tutorial.localized(this.$refs.localization);
  72. },
  73. loadLibrary: function(url) {
  74. var vm = this;
  75. vm.currentLibrary = {database: []};
  76. defaultUrlLibrary = url;
  77. document.getElementById("spinner").style.visibility = "visible";
  78. axios.get(url+"?lang="+vm.$refs.localization.code)
  79. .then(function(response) {
  80. vm.currentLibrary = response.data;
  81. document.getElementById("spinner").style.visibility = "hidden";
  82. document.getElementById("cloudwarning").style.visibility = "hidden";
  83. })
  84. .catch(function(error) {
  85. document.getElementById("spinner").style.visibility = "hidden";
  86. document.getElementById("cloudwarning").style.visibility = "visible";
  87. });
  88. },
  89. saveContext: function() {
  90. if (this.currentView === EbookReader) {
  91. this.currentBook.location = this.$refs.view.getLocation();
  92. } else {
  93. this.currentLibrary = this.$refs.view.library;
  94. }
  95. },
  96. switchView: function() {
  97. this.saveContext();
  98. if (this.currentView === EbookReader) {
  99. this.currentView = LibraryViewer;
  100. } else {
  101. if (this.currentBook) {
  102. this.currentView = EbookReader;
  103. }
  104. }
  105. },
  106. // Handle fullscreen mode
  107. fullscreen: function() {
  108. document.getElementById("main-toolbar").style.opacity = 0;
  109. document.getElementById("canvas").style.top = "0px";
  110. document.getElementById("unfullscreen-button").style.visibility = "visible";
  111. if (this.currentView === EbookReader) {
  112. var reader = this.$refs.view;
  113. reader.render(this.currentEpub, reader.getLocation());
  114. }
  115. },
  116. unfullscreen: function() {
  117. document.getElementById("main-toolbar").style.opacity = 1;
  118. document.getElementById("canvas").style.top = "55px";
  119. document.getElementById("unfullscreen-button").style.visibility = "hidden";
  120. if (this.currentView === EbookReader) {
  121. var reader = this.$refs.view;
  122. reader.render(this.currentEpub, reader.getLocation());
  123. }
  124. },
  125. // Handling popup settings
  126. setLibraryUrl: function() {
  127. var titleOk = this.$refs.localization.get("Ok"),
  128. titleCancel = this.$refs.localization.get("Cancel"),
  129. titleSettings = this.$refs.localization.get("Settings"),
  130. titleUrl = this.$refs.localization.get("Url");
  131. this.$refs.settings.show({
  132. content: `
  133. <div id='popup-toolbar' class='toolbar' style='padding: 0'>
  134. <button class='toolbutton pull-right' id='popup-ok-button' title='`+titleOk+`' style='outline:none;background-image: url(lib/sugar-web/graphics/icons/actions/dialog-ok.svg)'></button>
  135. <button class='toolbutton pull-right' id='popup-cancel-button' title='`+titleCancel+`' style='outline:none;background-image: url(lib/sugar-web/graphics/icons/actions/dialog-cancel.svg)'></button>
  136. <div style='position: absolute; top: 20px; left: 60px;'>`+titleSettings+`</div>
  137. </div>
  138. <div id='popup-container' style='width: 100%; overflow:auto'>
  139. <div class='popup-label'>`+titleUrl+`</div>
  140. <input id='input' class='popup-input'/>
  141. </div>`,
  142. closeButton: false,
  143. modalStyles: {
  144. backgroundColor: "white",
  145. height: "160px",
  146. width: "600px",
  147. maxWidth: "90%"
  148. }
  149. });
  150. },
  151. settingsShown: function() {
  152. var vm = this;
  153. document.getElementById('popup-container').style.height = (document.getElementById("popup-toolbar").parentNode.offsetHeight - 55*2) + "px";
  154. document.getElementById('popup-ok-button').addEventListener('click', function() {
  155. vm.$refs.settings.close(true);
  156. });
  157. document.getElementById('popup-cancel-button').addEventListener('click', function() {
  158. vm.$refs.settings.close();
  159. });
  160. document.getElementById('input').value = defaultUrlLibrary;
  161. },
  162. settingsClosed: function(result) {
  163. if (result) {
  164. this.loadLibrary(document.getElementById('input').value);
  165. }
  166. this.$refs.settings.destroy();
  167. },
  168. // Handle events
  169. onBookSelected: function(book) {
  170. if (this.currentView === LibraryViewer) {
  171. // Load book
  172. var vm = this;
  173. vm.currentBook = book;
  174. Vue.set(book, 'spinner', true);
  175. vm.currentEpub = new ePub.Book();
  176. vm.currentEpub.open(vm.currentLibrary.information.fileprefix+vm.currentBook.file).then(function() {
  177. vm.currentView = EbookReader;
  178. book.spinner = false;
  179. }, function() {
  180. console.log("Error loading "+vm.currentLibrary.information.fileprefix+vm.currentBook.file);
  181. book.spinner = false;
  182. });
  183. }
  184. },
  185. onResize: function() {
  186. var vm = this;
  187. if (vm.currentView === EbookReader) {
  188. var reader = vm.$refs.view;
  189. if (this.timer) {
  190. window.clearTimeout(this.timer);
  191. }
  192. this.timer = window.setTimeout(function() {
  193. vm.currentBook.location = reader.getLocation();
  194. reader.render(vm.currentEpub, vm.currentBook.location);
  195. this.timer = null;
  196. }, 500);
  197. }
  198. },
  199. onHelp: function() {
  200. var options = {};
  201. options.switchbutton = this.$refs.toolbar.$refs.switchbutton.$el;
  202. options.settingsbutton = this.$refs.toolbar.$refs.settings.$el;
  203. options.fullscreenbutton = this.$refs.toolbar.$refs.fullscreen.$el;
  204. if (this.currentView === LibraryViewer && this.$refs.view.$refs.item0 && this.$refs.view.$refs.item0[0]) {
  205. options.book = this.$refs.view.$refs.item0[0].$el;
  206. } else if (this.currentView === EbookReader) {
  207. options.prevbutton = document.getElementById("left");
  208. options.nextbutton = document.getElementById("right");
  209. }
  210. this.$refs.tutorial.show(options);
  211. },
  212. onStop: function() {
  213. // Save current library in Journal on Stop
  214. var vm = this;
  215. vm.saveContext();
  216. requirejs(["sugar-web/activity/activity"], function(activity) {
  217. console.log("writing...");
  218. var context = {
  219. library: { information:vm.currentLibrary.information, database:vm.currentLibrary.database }
  220. };
  221. if (vm.currentView === EbookReader) {
  222. context.current = vm.currentLibrary.database.indexOf(vm.currentBook);
  223. }
  224. var jsonData = JSON.stringify(context);
  225. activity.getDatastoreObject().setDataAsText(jsonData);
  226. activity.getDatastoreObject().save(function(error) {
  227. if (error === null) {
  228. console.log("write done.");
  229. } else {
  230. console.log("write failed.");
  231. }
  232. });
  233. });
  234. }
  235. }
  236. });