Convert all initializers to Ember initializers
This commit is contained in:
parent
a2a07a9852
commit
a6670c6c83
|
@ -104,6 +104,7 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, {
|
|||
Default is false, for next run loop. If unsure, use false.
|
||||
**/
|
||||
addInitializer: function(init, immediate) {
|
||||
Em.warn("`Discouse.addInitializer` is deprecated. Export an Ember initializer instead.");
|
||||
Discourse.initializers = Discourse.initializers || [];
|
||||
Discourse.initializers.push({fn: init, immediate: !!immediate});
|
||||
},
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
/**
|
||||
Discourse does some server side rendering of HTML, such as the `cooked` contents of
|
||||
posts. The downside of this in an Ember app is the links will not go through the router.
|
||||
This jQuery code intercepts clicks on those links and routes them properly.
|
||||
**/
|
||||
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
$('#main').on('click.discourse', 'a', function(e) {
|
||||
if (e.isDefaultPrevented() || e.shiftKey || e.metaKey || e.ctrlKey) { return; }
|
||||
|
||||
var $currentTarget = $(e.currentTarget),
|
||||
href = $currentTarget.attr('href');
|
||||
|
||||
if (!href ||
|
||||
href === '#' ||
|
||||
$currentTarget.attr('target') ||
|
||||
$currentTarget.data('ember-action') ||
|
||||
$currentTarget.data('auto-route') ||
|
||||
$currentTarget.hasClass('ember-view') ||
|
||||
$currentTarget.hasClass('lightbox') ||
|
||||
href.indexOf("mailto:") === 0 ||
|
||||
(href.match(/^http[s]?:\/\//i) && !href.match(new RegExp("^http:\\/\\/" + window.location.hostname, "i")))) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
Discourse.URL.routeTo(href);
|
||||
return false;
|
||||
});
|
||||
|
||||
}, true);
|
|
@ -1,36 +0,0 @@
|
|||
/**
|
||||
Initialize the message bus to receive messages.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
// We don't use the message bus in testing
|
||||
if (Discourse.testing) { return; }
|
||||
|
||||
Discourse.MessageBus.alwaysLongPoll = Discourse.Environment === "development";
|
||||
Discourse.MessageBus.start();
|
||||
|
||||
Discourse.MessageBus.subscribe("/global/asset-version", function(version){
|
||||
Discourse.set("assetVersion", version);
|
||||
|
||||
if(Discourse.get("requiresRefresh")) {
|
||||
// since we can do this transparently for people browsing the forum
|
||||
// hold back the message a couple of hours
|
||||
setTimeout(function() {
|
||||
bootbox.confirm(I18n.lookup("assets_changed_confirm"), function(result){
|
||||
if (result) {
|
||||
document.location.reload();
|
||||
}
|
||||
});
|
||||
}, 1000 * 60 * 120);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// initialize read-only mode and subscribe to updates via the message bus
|
||||
Discourse.set("isReadOnly", Discourse.Site.currentProp("is_readonly"));
|
||||
Discourse.MessageBus.subscribe("/site/read-only", function (enabled) {
|
||||
Discourse.set("isReadOnly", enabled);
|
||||
});
|
||||
|
||||
Discourse.KeyValueStore.init("discourse_", Discourse.MessageBus);
|
||||
}, true);
|
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
Discourse does some server side rendering of HTML, such as the `cooked` contents of
|
||||
posts. The downside of this in an Ember app is the links will not go through the router.
|
||||
This jQuery code intercepts clicks on those links and routes them properly.
|
||||
**/
|
||||
export default {
|
||||
name: "click-interceptor",
|
||||
initialize: function() {
|
||||
$('#main').on('click.discourse', 'a', function(e) {
|
||||
if (e.isDefaultPrevented() || e.shiftKey || e.metaKey || e.ctrlKey) { return; }
|
||||
|
||||
var $currentTarget = $(e.currentTarget),
|
||||
href = $currentTarget.attr('href');
|
||||
|
||||
if (!href ||
|
||||
href === '#' ||
|
||||
$currentTarget.attr('target') ||
|
||||
$currentTarget.data('ember-action') ||
|
||||
$currentTarget.data('auto-route') ||
|
||||
$currentTarget.hasClass('ember-view') ||
|
||||
$currentTarget.hasClass('lightbox') ||
|
||||
href.indexOf("mailto:") === 0 ||
|
||||
(href.match(/^http[s]?:\/\//i) && !href.match(new RegExp("^http:\\/\\/" + window.location.hostname, "i")))) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
Discourse.URL.routeTo(href);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
Keep track of when the browser is in focus.
|
||||
**/
|
||||
export default {
|
||||
name: 'focus-event',
|
||||
|
||||
initialize: function() {
|
||||
|
||||
// Default to true
|
||||
Discourse.set('hasFocus', true);
|
||||
|
||||
$(window).focus(function() {
|
||||
Discourse.setProperties({hasFocus: true, notify: false});
|
||||
}).blur(function() {
|
||||
Discourse.set('hasFocus', false);
|
||||
});
|
||||
}
|
||||
};
|
|
@ -1,16 +0,0 @@
|
|||
/**
|
||||
Keep track of when the browser is in focus.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
// Default to true
|
||||
this.set('hasFocus', true);
|
||||
|
||||
var self = this;
|
||||
$(window).focus(function() {
|
||||
self.setProperties({hasFocus: true, notify: false});
|
||||
}).blur(function() {
|
||||
self.set('hasFocus', false);
|
||||
});
|
||||
|
||||
}, true);
|
|
@ -1,7 +0,0 @@
|
|||
/**
|
||||
Initializes the `Discourse.Mobile` helper object.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
Discourse.Mobile.init();
|
||||
}, true);
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
/*global Mousetrap:true*/
|
||||
|
||||
/**
|
||||
Initialize Global Keyboard Shortcuts
|
||||
**/
|
||||
export default {
|
||||
name: "keyboard-shortcuts",
|
||||
initialize: function() {
|
||||
Discourse.KeyboardShortcuts.bindEvents(Mousetrap);
|
||||
}
|
||||
};
|
|
@ -1,8 +0,0 @@
|
|||
/*global Mousetrap:true*/
|
||||
|
||||
/**
|
||||
Initialize Global Keyboard Shortcuts
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
Discourse.KeyboardShortcuts.bindEvents(Mousetrap);
|
||||
});
|
|
@ -0,0 +1,75 @@
|
|||
/**
|
||||
Use the message bus for live reloading of components for faster development.
|
||||
**/
|
||||
export default {
|
||||
name: "live-development",
|
||||
initialize: function() {
|
||||
|
||||
// subscribe to any site customizations that are loaded
|
||||
$('link.custom-css').each(function() {
|
||||
var split = this.href.split("/"),
|
||||
id = split[split.length - 1].split(".css")[0],
|
||||
self = this;
|
||||
|
||||
return Discourse.MessageBus.subscribe("/file-change/" + id, function(data) {
|
||||
if (!$(self).data('orig')) {
|
||||
$(self).data('orig', self.href);
|
||||
}
|
||||
var orig = $(self).data('orig'),
|
||||
sp = orig.split(".css?");
|
||||
|
||||
self.href = sp[0] + ".css?" + data;
|
||||
});
|
||||
});
|
||||
|
||||
// Custom header changes
|
||||
$('header.custom').each(function() {
|
||||
var header = $(this);
|
||||
return Discourse.MessageBus.subscribe("/header-change/" + $(this).data('key'), function(data) {
|
||||
return header.html(data);
|
||||
});
|
||||
});
|
||||
|
||||
// Observe file changes
|
||||
Discourse.MessageBus.subscribe("/file-change", function(data) {
|
||||
Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
|
||||
_.each(data,function(me) {
|
||||
|
||||
if (me === "refresh") {
|
||||
// Refresh if necessary
|
||||
document.location.reload(true);
|
||||
} else if (me.name.substr(-10) === "handlebars") {
|
||||
|
||||
// Reload handlebars
|
||||
var js = me.name.replace(".handlebars", "").replace("app/assets/javascripts", "/assets");
|
||||
$LAB.script(js + "?hash=" + me.hash).wait(function() {
|
||||
var templateName;
|
||||
templateName = js.replace(".js", "").replace("/assets/", "");
|
||||
return _.each(Ember.View.views, function(view) {
|
||||
if (view.get('templateName') === templateName) {
|
||||
view.set('templateName', 'empty');
|
||||
view.rerender();
|
||||
Em.run.schedule('afterRender', function() {
|
||||
view.set('templateName', templateName);
|
||||
view.rerender();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
} else {
|
||||
$('link').each(function() {
|
||||
// TODO: stop bundling css in DEV please
|
||||
if (true || (this.href.match(me.name) && me.hash)) {
|
||||
if (!$(this).data('orig')) {
|
||||
$(this).data('orig', this.href);
|
||||
}
|
||||
var orig = $(this).data('orig');
|
||||
this.href = orig + (orig.indexOf('?') >= 0 ? "&hash=" : "?hash=") + me.hash;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
|
@ -1,73 +0,0 @@
|
|||
/**
|
||||
Use the message bus for live reloading of components for faster development.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
// subscribe to any site customizations that are loaded
|
||||
$('link.custom-css').each(function() {
|
||||
var split = this.href.split("/"),
|
||||
id = split[split.length - 1].split(".css")[0],
|
||||
self = this;
|
||||
|
||||
return Discourse.MessageBus.subscribe("/file-change/" + id, function(data) {
|
||||
if (!$(self).data('orig')) {
|
||||
$(self).data('orig', self.href);
|
||||
}
|
||||
var orig = $(self).data('orig'),
|
||||
sp = orig.split(".css?");
|
||||
|
||||
self.href = sp[0] + ".css?" + data;
|
||||
});
|
||||
});
|
||||
|
||||
// Custom header changes
|
||||
$('header.custom').each(function() {
|
||||
var header = $(this);
|
||||
return Discourse.MessageBus.subscribe("/header-change/" + $(this).data('key'), function(data) {
|
||||
return header.html(data);
|
||||
});
|
||||
});
|
||||
|
||||
// Observe file changes
|
||||
return Discourse.MessageBus.subscribe("/file-change", function(data) {
|
||||
Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
|
||||
_.each(data,function(me) {
|
||||
|
||||
if (me === "refresh") {
|
||||
// Refresh if necessary
|
||||
document.location.reload(true);
|
||||
} else if (me.name.substr(-10) === "handlebars") {
|
||||
|
||||
// Reload handlebars
|
||||
var js = me.name.replace(".handlebars", "").replace("app/assets/javascripts", "/assets");
|
||||
$LAB.script(js + "?hash=" + me.hash).wait(function() {
|
||||
var templateName;
|
||||
templateName = js.replace(".js", "").replace("/assets/", "");
|
||||
return _.each(Ember.View.views, function(view) {
|
||||
if (view.get('templateName') === templateName) {
|
||||
view.set('templateName', 'empty');
|
||||
view.rerender();
|
||||
Em.run.schedule('afterRender', function() {
|
||||
view.set('templateName', templateName);
|
||||
view.rerender();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
} else {
|
||||
$('link').each(function() {
|
||||
// TODO: stop bundling css in DEV please
|
||||
if (true || (this.href.match(me.name) && me.hash)) {
|
||||
if (!$(this).data('orig')) {
|
||||
$(this).data('orig', this.href);
|
||||
}
|
||||
var orig = $(this).data('orig');
|
||||
this.href = orig + (orig.indexOf('?') >= 0 ? "&hash=" : "?hash=") + me.hash;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}, true);
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
Initialize the message bus to receive messages.
|
||||
**/
|
||||
export default {
|
||||
name: "message-bus",
|
||||
initialize: function() {
|
||||
|
||||
// We don't use the message bus in testing
|
||||
if (Discourse.testing) { return; }
|
||||
|
||||
Discourse.MessageBus.alwaysLongPoll = Discourse.Environment === "development";
|
||||
Discourse.MessageBus.start();
|
||||
|
||||
Discourse.MessageBus.subscribe("/global/asset-version", function(version){
|
||||
Discourse.set("assetVersion", version);
|
||||
|
||||
if(Discourse.get("requiresRefresh")) {
|
||||
// since we can do this transparently for people browsing the forum
|
||||
// hold back the message a couple of hours
|
||||
setTimeout(function() {
|
||||
bootbox.confirm(I18n.lookup("assets_changed_confirm"), function(result){
|
||||
if (result) {
|
||||
document.location.reload();
|
||||
}
|
||||
});
|
||||
}, 1000 * 60 * 120);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// initialize read-only mode and subscribe to updates via the message bus
|
||||
Discourse.set("isReadOnly", Discourse.Site.currentProp("is_readonly"));
|
||||
Discourse.MessageBus.subscribe("/site/read-only", function (enabled) {
|
||||
Discourse.set("isReadOnly", enabled);
|
||||
});
|
||||
|
||||
Discourse.KeyValueStore.init("discourse_", Discourse.MessageBus);
|
||||
}
|
||||
};
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
Initializes the `Discourse.Mobile` helper object.
|
||||
**/
|
||||
export default {
|
||||
name: 'mobile',
|
||||
|
||||
initialize: function() {
|
||||
Discourse.Mobile.init();
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
Sets up the PageTracking hook.
|
||||
**/
|
||||
export default {
|
||||
name: "page-tracking",
|
||||
after: 'register-discourse-location',
|
||||
|
||||
initialize: function() {
|
||||
var pageTracker = Discourse.PageTracker.current();
|
||||
pageTracker.start();
|
||||
|
||||
// Out of the box, Discourse tries to track google analytics
|
||||
// if it is present
|
||||
if (typeof window._gaq !== 'undefined') {
|
||||
pageTracker.on('change', function() {
|
||||
window._gaq.push(['_trackPageview']);
|
||||
});
|
||||
}
|
||||
|
||||
// Also use Universal Analytics if it is present
|
||||
if (typeof window.ga !== 'undefined') {
|
||||
pageTracker.on('change', function() {
|
||||
window.ga('send', 'pageview');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
|
@ -1,22 +0,0 @@
|
|||
/**
|
||||
Sets up the PageTracking hook.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
var pageTracker = Discourse.PageTracker.current();
|
||||
pageTracker.start();
|
||||
|
||||
// Out of the box, Discourse tries to track google analytics
|
||||
// if it is present
|
||||
if (typeof window._gaq !== 'undefined') {
|
||||
pageTracker.on('change', function() {
|
||||
window._gaq.push(['_trackPageview']);
|
||||
});
|
||||
}
|
||||
|
||||
// Also use Universal Analytics if it is present
|
||||
if (typeof window.ga !== 'undefined') {
|
||||
pageTracker.on('change', function() {
|
||||
window.ga('send', 'pageview');
|
||||
});
|
||||
}
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
Updates the relative ages of dates on the screen.
|
||||
**/
|
||||
export default {
|
||||
name: "relative-ages",
|
||||
initialize: function() {
|
||||
setInterval(function(){
|
||||
Discourse.Formatter.updateRelativeAge($('.relative-date'));
|
||||
}, 60 * 1000);
|
||||
}
|
||||
};
|
|
@ -1,12 +0,0 @@
|
|||
/**
|
||||
Updates the relative ages of dates on the screen.
|
||||
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
setInterval(function(){
|
||||
Discourse.Formatter.updateRelativeAge($('.relative-date'));
|
||||
}, 60 * 1000);
|
||||
|
||||
}, true);
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/*global Modernizr:true*/
|
||||
|
||||
/**
|
||||
Initializes the `Discourse.Capabilities` singleton by sniffing out the browser
|
||||
capabilities.
|
||||
**/
|
||||
export default {
|
||||
name: "sniff-capabilities",
|
||||
initialize: function() {
|
||||
var $html = $('html'),
|
||||
touch = $html.hasClass('touch') || (Modernizr.prefixed("MaxTouchPoints", navigator) > 1),
|
||||
caps = Discourse.Capabilities.current();
|
||||
|
||||
// Store the touch ability in our capabilities object
|
||||
caps.set('touch', touch);
|
||||
$html.addClass(touch ? 'discourse-touch' : 'discourse-no-touch');
|
||||
|
||||
// Detect Android
|
||||
if (navigator) {
|
||||
var ua = navigator.userAgent;
|
||||
caps.set('android', ua && ua.indexOf('Android') !== -1);
|
||||
}
|
||||
|
||||
// We consider high res a device with 1280 horizontal pixels. High DPI tablets like
|
||||
// iPads should report as 1024.
|
||||
caps.set('highRes', Modernizr.mq("only screen and (min-width: 1280px)"));
|
||||
}
|
||||
};
|
|
@ -1,26 +0,0 @@
|
|||
/*global Modernizr:true*/
|
||||
|
||||
/**
|
||||
Initializes the `Discourse.Capabilities` singleton by sniffing out the browser
|
||||
capabilities.
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
var $html = $('html'),
|
||||
touch = $html.hasClass('touch') || (Modernizr.prefixed("MaxTouchPoints", navigator) > 1),
|
||||
caps = Discourse.Capabilities.current();
|
||||
|
||||
// Store the touch ability in our capabilities object
|
||||
caps.set('touch', touch);
|
||||
$html.addClass(touch ? 'discourse-touch' : 'discourse-no-touch');
|
||||
|
||||
// Detect Android
|
||||
if (navigator) {
|
||||
var ua = navigator.userAgent;
|
||||
caps.set('android', ua && ua.indexOf('Android') !== -1);
|
||||
}
|
||||
|
||||
// We consider high res a device with 1280 horizontal pixels. High DPI tablets like
|
||||
// iPads should report as 1024.
|
||||
caps.set('highRes', Modernizr.mq("only screen and (min-width: 1280px)"));
|
||||
|
||||
}, true);
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
Subscribes to user events on the message bus
|
||||
**/
|
||||
export default {
|
||||
name: "subscribe-user-notifications",
|
||||
after: 'message-bus',
|
||||
initialize: function() {
|
||||
var user = Discourse.User.current();
|
||||
if (user) {
|
||||
var bus = Discourse.MessageBus;
|
||||
bus.callbackInterval = Discourse.SiteSettings.polling_interval;
|
||||
bus.enableLongPolling = true;
|
||||
bus.baseUrl = Discourse.getURL("/");
|
||||
|
||||
if (user.admin || user.moderator) {
|
||||
bus.subscribe("/flagged_counts", function(data) {
|
||||
user.set('site_flagged_posts_count', data.total);
|
||||
});
|
||||
}
|
||||
bus.subscribe("/notification/" + user.get('id'), (function(data) {
|
||||
user.set('unread_notifications', data.unread_notifications);
|
||||
user.set('unread_private_messages', data.unread_private_messages);
|
||||
}), user.notification_channel_position);
|
||||
|
||||
bus.subscribe("/categories", function(data){
|
||||
var site = Discourse.Site.current();
|
||||
_.each(data.categories,function(c){
|
||||
site.updateCategory(c);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
|
@ -1,33 +0,0 @@
|
|||
/**
|
||||
Updates the relative ages of dates on the screen.
|
||||
|
||||
**/
|
||||
Discourse.addInitializer(function() {
|
||||
|
||||
var user = Discourse.User.current();
|
||||
if (user) {
|
||||
var bus = Discourse.MessageBus;
|
||||
bus.callbackInterval = Discourse.SiteSettings.polling_interval;
|
||||
bus.enableLongPolling = true;
|
||||
bus.baseUrl = Discourse.getURL("/");
|
||||
|
||||
if (user.admin || user.moderator) {
|
||||
bus.subscribe("/flagged_counts", function(data) {
|
||||
user.set('site_flagged_posts_count', data.total);
|
||||
});
|
||||
}
|
||||
bus.subscribe("/notification/" + user.get('id'), (function(data) {
|
||||
user.set('unread_notifications', data.unread_notifications);
|
||||
user.set('unread_private_messages', data.unread_private_messages);
|
||||
}), user.notification_channel_position);
|
||||
|
||||
bus.subscribe("/categories", function(data){
|
||||
var site = Discourse.Site.current();
|
||||
_.each(data.categories,function(c){
|
||||
site.updateCategory(c);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}, true);
|
||||
|
Loading…
Reference in New Issue