FEATURE: allow plugins to overwrite handlebars templates

This commit is contained in:
Régis Hanol 2013-12-31 16:40:45 +01:00
parent d150bc20cf
commit 7fd88a52c9
6 changed files with 43 additions and 9 deletions

View File

@ -1,8 +1,9 @@
<%
require_asset ("./main_include.js")
# Include plugin javascripts
DiscoursePluginRegistry.javascripts.each do |js|
require_asset(js)
end
# Include plugin javascripts/handlebars templates
DiscoursePluginRegistry.javascripts.each { |js| require_asset(js) }
DiscoursePluginRegistry.handlebars.each { |hb| require_asset(hb) }
%>

View File

@ -16,7 +16,15 @@ Discourse.Resolver = Ember.DefaultResolver.extend({
@returns {Template} the template (if found)
**/
resolveTemplate: function(parsedName) {
return this.findMobileTemplate(parsedName) || this.findTemplate(parsedName) || Ember.TEMPLATES.not_found;
return this.findPluginTemplate(parsedName) ||
this.findMobileTemplate(parsedName) ||
this.findTemplate(parsedName) ||
Ember.TEMPLATES.not_found;
},
findPluginTemplate: function(parsedName) {
var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/"));
return this.findTemplate(pluginParsedName);
},
findMobileTemplate: function(parsedName) {
@ -49,4 +57,4 @@ Discourse.Resolver = Ember.DefaultResolver.extend({
}
}
});
});

View File

@ -36,3 +36,4 @@
//= require_tree ./discourse/templates
//= require_tree ./discourse/routes
//= require_tree ./discourse/initializers

View File

@ -7,6 +7,7 @@ class DiscoursePluginRegistry
attr_accessor :javascripts
attr_accessor :server_side_javascripts
attr_accessor :stylesheets
attr_accessor :handlebars
# Default accessor values
def javascripts
@ -20,6 +21,10 @@ class DiscoursePluginRegistry
def stylesheets
@stylesheets ||= Set.new
end
def handlebars
@handlebars ||= Set.new
end
end
def register_js(filename, options={})
@ -48,10 +53,15 @@ class DiscoursePluginRegistry
self.class.stylesheets
end
def handlebars
self.class.handlebars
end
def self.clear
self.javascripts = nil
self.server_side_javascripts = nil
self.stylesheets = nil
self.handlebars = nil
end
def self.setup(plugin_class)

View File

@ -93,7 +93,6 @@ class Plugin::Instance
@javascripts << js
end
def register_asset(file,opts=nil)
full_path = File.dirname(path) << "/assets/" << file
assets << full_path
@ -154,10 +153,12 @@ class Plugin::Instance
end
unless assets.blank?
assets.each do |asset|
if asset =~ /\.js$|.js.erb$/
if asset =~ /\.js$|\.js\.erb$/
DiscoursePluginRegistry.javascripts << asset
elsif asset =~ /\.css$|\.scss$/
DiscoursePluginRegistry.stylesheets << asset
elsif asset =~ /\.js\.handlebars$$/
DiscoursePluginRegistry.handlebars << asset
end
end

View File

@ -5,7 +5,7 @@ var lookup = function(lookupString, expectedTemplate, message) {
equal(Discourse.__container__.lookup(lookupString, {singleton: false}), expectedTemplate, message);
};
var setTemplates = function(lookupStrings) {
var setTemplates = function(lookupStrings) {
lookupStrings.forEach(function(lookupString) {
Ember.TEMPLATES[lookupString] = lookupString;
});
@ -95,6 +95,19 @@ test("resolves mobile templates to 'mobile/' namespace", function() {
lookup("template:baz", "baz", "falling back to a normal version when mobile version is not present");
});
test("resolves plugin templates to 'javascripts/' namespace", function() {
setTemplates([
"javascripts/foo",
"bar",
"javascripts/bar",
"baz"
]);
lookup("template:foo", "javascripts/foo", "finding plugin version even if normal one is not present");
lookup("template:bar", "javascripts/bar", "preferring plugin version when both versions are present");
lookup("template:baz", "baz", "falling back to a normal version when plugin version is not present");
});
test("resolves templates with 'admin' prefix to 'admin/templates/' namespace", function() {
setTemplates([
"admin/templates/foo",