2016-10-25 01:49:17 +08:00
|
|
|
import { findHelper } from 'discourse-common/lib/helpers';
|
2016-08-31 23:30:51 +08:00
|
|
|
|
2016-10-25 01:49:17 +08:00
|
|
|
/* global requirejs, require */
|
2016-08-31 23:30:51 +08:00
|
|
|
var classify = Ember.String.classify;
|
|
|
|
var get = Ember.get;
|
|
|
|
|
2016-11-04 02:15:26 +08:00
|
|
|
const _options = {};
|
|
|
|
|
|
|
|
export function setResolverOption(name, value) {
|
|
|
|
_options[name] = value;
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:30:51 +08:00
|
|
|
function parseName(fullName) {
|
2016-12-16 23:29:30 +08:00
|
|
|
const nameParts = fullName.split(":"),
|
|
|
|
type = nameParts[0], fullNameWithoutType = nameParts[1],
|
|
|
|
name = fullNameWithoutType,
|
|
|
|
namespace = get(this, 'namespace'),
|
|
|
|
root = namespace;
|
2016-08-31 23:30:51 +08:00
|
|
|
|
|
|
|
return {
|
2016-12-16 23:29:30 +08:00
|
|
|
fullName: fullName,
|
|
|
|
type: type,
|
|
|
|
fullNameWithoutType: fullNameWithoutType,
|
|
|
|
name: name,
|
|
|
|
root: root,
|
2016-08-31 23:30:51 +08:00
|
|
|
resolveMethodName: "resolve" + classify(type)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildResolver(baseName) {
|
|
|
|
return Ember.DefaultResolver.extend({
|
|
|
|
parseName,
|
|
|
|
|
|
|
|
resolveRouter(parsedName) {
|
|
|
|
const routerPath = `${baseName}/router`;
|
|
|
|
if (requirejs.entries[routerPath]) {
|
|
|
|
const module = require(routerPath, null, null, true);
|
|
|
|
return module.default;
|
|
|
|
}
|
|
|
|
return this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
normalize(fullName) {
|
|
|
|
const split = fullName.split(':');
|
|
|
|
if (split.length > 1) {
|
|
|
|
const appBase = `${baseName}/${split[0]}s/`;
|
|
|
|
const adminBase = 'admin/' + split[0] + 's/';
|
|
|
|
|
|
|
|
// Allow render 'admin/templates/xyz' too
|
|
|
|
split[1] = split[1].replace('.templates', '').replace('/templates', '');
|
|
|
|
|
|
|
|
// Try slashes
|
|
|
|
let dashed = Ember.String.dasherize(split[1].replace(/\./g, '/'));
|
|
|
|
if (requirejs.entries[appBase + dashed] || requirejs.entries[adminBase + dashed]) {
|
|
|
|
return split[0] + ":" + dashed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with dashes instead of slashes
|
|
|
|
dashed = Ember.String.dasherize(split[1].replace(/\./g, '-'));
|
|
|
|
if (requirejs.entries[appBase + dashed] || requirejs.entries[adminBase + dashed]) {
|
|
|
|
return split[0] + ":" + dashed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._super(fullName);
|
|
|
|
},
|
|
|
|
|
|
|
|
customResolve(parsedName) {
|
|
|
|
// If we end with the name we want, use it. This allows us to define components within plugins.
|
|
|
|
const suffix = parsedName.type + 's/' + parsedName.fullNameWithoutType,
|
|
|
|
dashed = Ember.String.dasherize(suffix),
|
|
|
|
moduleName = Object.keys(requirejs.entries).find(function(e) {
|
|
|
|
return (e.indexOf(suffix, e.length - suffix.length) !== -1) ||
|
|
|
|
(e.indexOf(dashed, e.length - dashed.length) !== -1);
|
|
|
|
});
|
|
|
|
|
|
|
|
var module;
|
|
|
|
if (moduleName) {
|
|
|
|
module = require(moduleName, null, null, true /* force sync */);
|
|
|
|
if (module && module['default']) { module = module['default']; }
|
|
|
|
}
|
|
|
|
return module;
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveWidget(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveAdapter(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveModel(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveView(parsedName) {
|
2016-11-12 05:31:08 +08:00
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
2016-08-31 23:30:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveHelper(parsedName) {
|
2016-10-25 01:49:17 +08:00
|
|
|
return findHelper(parsedName.fullNameWithoutType) ||
|
|
|
|
this.customResolve(parsedName) ||
|
|
|
|
this._super(parsedName);
|
2016-08-31 23:30:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveController(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
resolveComponent(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
2016-10-20 05:12:25 +08:00
|
|
|
resolveService(parsedName) {
|
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
2016-08-31 23:30:51 +08:00
|
|
|
resolveRoute(parsedName) {
|
2016-11-12 05:31:08 +08:00
|
|
|
return this.customResolve(parsedName) || this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
findLoadingTemplate(parsedName) {
|
|
|
|
if (parsedName.fullNameWithoutType.match(/loading$/)) {
|
|
|
|
return Ember.TEMPLATES.loading;
|
|
|
|
}
|
2016-08-31 23:30:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resolveTemplate(parsedName) {
|
|
|
|
return this.findPluginMobileTemplate(parsedName) ||
|
|
|
|
this.findPluginTemplate(parsedName) ||
|
|
|
|
this.findMobileTemplate(parsedName) ||
|
|
|
|
this.findTemplate(parsedName) ||
|
2016-11-12 05:31:08 +08:00
|
|
|
this.findLoadingTemplate(parsedName) ||
|
2016-08-31 23:30:51 +08:00
|
|
|
Ember.TEMPLATES.not_found;
|
|
|
|
},
|
|
|
|
|
|
|
|
findPluginTemplate(parsedName) {
|
2016-12-02 04:37:24 +08:00
|
|
|
const pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/"));
|
2016-08-31 23:30:51 +08:00
|
|
|
return this.findTemplate(pluginParsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
findPluginMobileTemplate(parsedName) {
|
2016-11-04 02:15:26 +08:00
|
|
|
if (_options.mobileView) {
|
2016-08-31 23:30:51 +08:00
|
|
|
var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/mobile/"));
|
|
|
|
return this.findTemplate(pluginParsedName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findMobileTemplate(parsedName) {
|
2016-11-04 02:15:26 +08:00
|
|
|
if (_options.mobileView) {
|
2016-08-31 23:30:51 +08:00
|
|
|
var mobileParsedName = this.parseName(parsedName.fullName.replace("template:", "template:mobile/"));
|
|
|
|
return this.findTemplate(mobileParsedName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findTemplate(parsedName) {
|
|
|
|
const withoutType = parsedName.fullNameWithoutType,
|
|
|
|
slashedType = withoutType.replace(/\./g, '/'),
|
|
|
|
decamelized = withoutType.decamelize(),
|
|
|
|
dashed = decamelized.replace(/\./g, '-').replace(/\_/g, '-'),
|
|
|
|
templates = Ember.TEMPLATES;
|
|
|
|
|
|
|
|
return this._super(parsedName) ||
|
|
|
|
templates[slashedType] ||
|
|
|
|
templates[withoutType] ||
|
2016-10-26 23:19:29 +08:00
|
|
|
templates[withoutType.replace(/\.raw$/, '')] ||
|
2016-08-31 23:30:51 +08:00
|
|
|
templates[dashed] ||
|
|
|
|
templates[decamelized.replace(/\./, '/')] ||
|
|
|
|
templates[decamelized.replace(/\_/, '/')] ||
|
|
|
|
templates[`${baseName}/templates/${withoutType}`] ||
|
|
|
|
this.findAdminTemplate(parsedName) ||
|
|
|
|
this.findUnderscoredTemplate(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
findUnderscoredTemplate(parsedName) {
|
|
|
|
var decamelized = parsedName.fullNameWithoutType.decamelize();
|
|
|
|
var underscored = decamelized.replace(/\-/g, "_");
|
|
|
|
return Ember.TEMPLATES[underscored];
|
|
|
|
},
|
|
|
|
|
|
|
|
// Try to find a template within a special admin namespace, e.g. adminEmail => admin/templates/email
|
|
|
|
// (similar to how discourse lays out templates)
|
|
|
|
findAdminTemplate(parsedName) {
|
|
|
|
var decamelized = parsedName.fullNameWithoutType.decamelize();
|
|
|
|
|
|
|
|
if (decamelized.indexOf('components') === 0) {
|
|
|
|
const compTemplate = Ember.TEMPLATES['admin/templates/' + decamelized];
|
|
|
|
if (compTemplate) { return compTemplate; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decamelized === "javascripts/admin") {
|
|
|
|
return Ember.TEMPLATES['admin/templates/admin'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decamelized.indexOf('admin') === 0 || decamelized.indexOf('javascripts/admin') === 0) {
|
|
|
|
decamelized = decamelized.replace(/^admin\_/, 'admin/templates/');
|
|
|
|
decamelized = decamelized.replace(/^admin\./, 'admin/templates/');
|
|
|
|
decamelized = decamelized.replace(/\./g, '_');
|
|
|
|
|
|
|
|
const dashed = decamelized.replace(/_/g, '-');
|
|
|
|
return Ember.TEMPLATES[decamelized] ||
|
|
|
|
Ember.TEMPLATES[dashed] ||
|
|
|
|
Ember.TEMPLATES[dashed.replace('admin-', 'admin/')];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|