// Rebase require directory requirejs.config({ baseUrl: "lib" }); // Default library url var defaultUrlLibrary = (document.location.protocol.substr(0,5)=="https"?"https":"http") + "://sugarizer.org/content/books.php"; // Vue main app var app = new Vue({ el: '#app', components: { 'ebook-reader': EbookReader, 'library-viewer': LibraryViewer, 'toolbar': Toolbar, 'localization': Localization, 'popup': Popup, 'tutorial': Tutorial }, data: { currentBook: null, currentEpub: null, currentView: LibraryViewer, currentLibrary: {database: []}, timer: null }, created: function() { requirejs(["sugar-web/activity/activity", "sugar-web/env"], function(activity, env) { // Initialize Sugarizer activity.setup(); }); }, mounted: function() { // Load last library from Journal var vm = this; requirejs(["sugar-web/activity/activity", "sugar-web/env"], function(activity, env) { env.getEnvironment(function(err, environment) { // Use buddy color for background env.getEnvironment(function(err, environment) { document.getElementById("canvas").style.backgroundColor = environment.user.colorvalue.fill; }); // Load context if (environment.objectId) { activity.getDatastoreObject().loadAsText(function(error, metadata, data) { if (error==null && data!=null) { var parsed = JSON.parse(data); vm.currentLibrary = parsed.library; if (parsed.current !== undefined) { vm.currentBook = vm.currentLibrary.database[parsed.current]; vm.currentEpub = ePub(vm.currentLibrary.information.fileprefix+vm.currentBook.file); vm.currentView = EbookReader; } else if (vm.currentLibrary.database.length == 0) { vm.loadLibrary(defaultUrlLibrary); } document.getElementById("spinner").style.visibility = "hidden"; } }); } else { vm.loadLibrary(defaultUrlLibrary); } }); }); // Handle resize window.addEventListener("resize", function() { vm.onResize(); }); // Handle unfull screen buttons (b) document.getElementById("unfullscreen-button").addEventListener('click', function() { vm.unfullscreen(); }); }, updated: function() { if (this.currentView === EbookReader) { this.$refs.view.render(this.currentEpub, this.currentBook.location); } }, methods: { localized: function() { this.$refs.toolbar.localized(this.$refs.localization); this.$refs.tutorial.localized(this.$refs.localization); }, loadLibrary: function(url) { var vm = this; vm.currentLibrary = {database: []}; defaultUrlLibrary = url; document.getElementById("spinner").style.visibility = "visible"; axios.get(url+"?lang="+vm.$refs.localization.code) .then(function(response) { vm.currentLibrary = response.data; document.getElementById("spinner").style.visibility = "hidden"; document.getElementById("cloudwarning").style.visibility = "hidden"; }) .catch(function(error) { document.getElementById("spinner").style.visibility = "hidden"; document.getElementById("cloudwarning").style.visibility = "visible"; }); }, saveContext: function() { if (this.currentView === EbookReader) { this.currentBook.location = this.$refs.view.getLocation(); } else { this.currentLibrary = this.$refs.view.library; } }, switchView: function() { this.saveContext(); if (this.currentView === EbookReader) { this.currentView = LibraryViewer; } else { if (this.currentBook) { this.currentView = EbookReader; } } }, // Handle fullscreen mode fullscreen: function() { document.getElementById("main-toolbar").style.opacity = 0; document.getElementById("canvas").style.top = "0px"; document.getElementById("unfullscreen-button").style.visibility = "visible"; if (this.currentView === EbookReader) { var reader = this.$refs.view; reader.render(this.currentEpub, reader.getLocation()); } }, unfullscreen: function() { document.getElementById("main-toolbar").style.opacity = 1; document.getElementById("canvas").style.top = "55px"; document.getElementById("unfullscreen-button").style.visibility = "hidden"; if (this.currentView === EbookReader) { var reader = this.$refs.view; reader.render(this.currentEpub, reader.getLocation()); } }, // Handling popup settings setLibraryUrl: function() { var titleOk = this.$refs.localization.get("Ok"), titleCancel = this.$refs.localization.get("Cancel"), titleSettings = this.$refs.localization.get("Settings"), titleUrl = this.$refs.localization.get("Url"); this.$refs.settings.show({ content: `