import { buildResolver } from "discourse-common/resolver"; import { default as computed, observes } from "ember-addons/ember-computed-decorators"; const _pluginCallbacks = []; const Discourse = Ember.Application.extend({ rootElement: "#main", _docTitle: document.title, RAW_TEMPLATES: {}, __widget_helpers: {}, showingSignup: false, customEvents: { paste: "paste" }, getURL(url) { if (!url) return url; // if it's a non relative URL, return it. if (url !== "/" && !/^\/[^\/]/.test(url)) return url; if (url.indexOf(Discourse.BaseUri) !== -1) return url; if (url[0] !== "/") url = "/" + url; return Discourse.BaseUri + url; }, getURLWithCDN(url) { url = Discourse.getURL(url); // only relative urls if (Discourse.CDN && /^\/[^\/]/.test(url)) { url = Discourse.CDN + url; } else if (Discourse.S3CDN) { url = url.replace(Discourse.S3BaseUrl, Discourse.S3CDN); } return url; }, Resolver: buildResolver("discourse"), @observes("_docTitle", "hasFocus", "contextCount", "notificationCount") _titleChanged() { let title = this.get("_docTitle") || Discourse.SiteSettings.title; // if we change this we can trigger changes on document.title // only set if changed. if ($("title").text() !== title) { $("title").text(title); } var displayCount = this.get("displayCount"); if (displayCount > 0 && !Discourse.User.currentProp("dynamic_favicon")) { title = `(${displayCount}) ${title}`; } document.title = title; }, @computed("contextCount", "notificationCount") displayCount() { return Discourse.User.current() && Discourse.User.currentProp("title_count_mode") === "notifications" ? this.get("notificationCount") : this.get("contextCount"); }, @observes("contextCount", "notificationCount") faviconChanged() { if (Discourse.User.currentProp("dynamic_favicon")) { let url = Discourse.SiteSettings.site_favicon_url; // Since the favicon is cached on the browser for a really long time, we // append the favicon_url as query params to the path so that the cache // is not used when the favicon changes. if (/^http/.test(url)) { url = Discourse.getURL("/favicon/proxied?" + encodeURIComponent(url)); } var displayCount = this.get("displayCount"); new window.Favcount(url).set(displayCount); } }, // The classes of buttons to show on a post @computed postButtons() { return Discourse.SiteSettings.post_menu.split("|").map(function(i) { return i.replace(/\+/, "").capitalize(); }); }, updateContextCount(count) { this.set("contextCount", count); }, updateNotificationCount(count) { if (!this.get("hasFocus")) { this.set("notificationCount", count); } }, incrementBackgroundContextCount() { if (!this.get("hasFocus")) { this.set("backgroundNotify", true); this.set("contextCount", (this.get("contextCount") || 0) + 1); } }, @observes("hasFocus") resetCounts() { if (this.get("hasFocus") && this.get("backgroundNotify")) { this.set("contextCount", 0); } this.set("backgroundNotify", false); if (this.get("hasFocus")) { this.set("notificationCount", 0); } }, authenticationComplete(options) { // TODO, how to dispatch this to the controller without the container? const loginController = Discourse.__container__.lookup("controller:login"); return loginController.authenticationComplete(options); }, // Start up the Discourse application by running all the initializers we've defined. start() { $("noscript").remove(); Object.keys(requirejs._eak_seen).forEach(function(key) { if (/\/pre\-initializers\//.test(key)) { const module = requirejs(key, null, null, true); if (!module) { throw new Error(key + " must export an initializer."); } const init = module.default; const oldInitialize = init.initialize; init.initialize = function() { oldInitialize.call(this, Discourse.__container__, Discourse); }; Discourse.initializer(init); } }); Object.keys(requirejs._eak_seen).forEach(function(key) { if (/\/initializers\//.test(key)) { const module = requirejs(key, null, null, true); if (!module) { throw new Error(key + " must export an initializer."); } const init = module.default; const oldInitialize = init.initialize; init.initialize = function() { oldInitialize.call(this, Discourse.__container__, Discourse); }; Discourse.instanceInitializer(init); } }); // Plugins that are registered via `