2018-06-15 23:03:24 +08:00
|
|
|
import { findHelper } from "discourse-common/lib/helpers";
|
2019-11-01 23:03:18 +08:00
|
|
|
import { get } from "@ember/object";
|
2019-10-04 22:06:08 +08:00
|
|
|
import deprecated from "discourse-common/lib/deprecated";
|
2019-11-06 02:43:49 +08:00
|
|
|
import { classify, dasherize } from "@ember/string";
|
2016-08-31 23:30:51 +08:00
|
|
|
|
2016-11-04 02:15:26 +08:00
|
|
|
const _options = {};
|
|
|
|
|
|
|
|
export function setResolverOption(name, value) {
|
|
|
|
_options[name] = value;
|
|
|
|
}
|
|
|
|
|
2016-12-20 00:19:10 +08:00
|
|
|
export function getResolverOption(name) {
|
|
|
|
return _options[name];
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:30:51 +08:00
|
|
|
function parseName(fullName) {
|
2016-12-20 00:19:10 +08:00
|
|
|
const nameParts = fullName.split(":");
|
|
|
|
const type = nameParts[0];
|
|
|
|
let fullNameWithoutType = nameParts[1];
|
2018-06-15 23:03:24 +08:00
|
|
|
const namespace = get(this, "namespace");
|
2016-12-20 00:19:10 +08:00
|
|
|
const root = namespace;
|
2016-08-31 23:30:51 +08:00
|
|
|
|
|
|
|
return {
|
2016-12-20 00:19:10 +08:00
|
|
|
fullName,
|
|
|
|
type,
|
|
|
|
fullNameWithoutType,
|
|
|
|
name: fullNameWithoutType,
|
|
|
|
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]) {
|
2017-07-06 02:14:30 +08:00
|
|
|
const module = requirejs(routerPath, null, null, true);
|
2016-08-31 23:30:51 +08:00
|
|
|
return module.default;
|
|
|
|
}
|
|
|
|
return this._super(parsedName);
|
|
|
|
},
|
|
|
|
|
|
|
|
normalize(fullName) {
|
2019-10-04 22:06:08 +08:00
|
|
|
if (fullName === "app-events:main") {
|
|
|
|
deprecated(
|
|
|
|
"`app-events:main` has been replaced with `service:app-events`",
|
|
|
|
{ since: "2.4.0" }
|
|
|
|
);
|
|
|
|
return "service:app-events";
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const split = fullName.split(":");
|
2016-08-31 23:30:51 +08:00
|
|
|
if (split.length > 1) {
|
|
|
|
const appBase = `${baseName}/${split[0]}s/`;
|
2018-06-15 23:03:24 +08:00
|
|
|
const adminBase = "admin/" + split[0] + "s/";
|
2016-08-31 23:30:51 +08:00
|
|
|
|
|
|
|
// Allow render 'admin/templates/xyz' too
|
2018-06-15 23:03:24 +08:00
|
|
|
split[1] = split[1].replace(".templates", "").replace("/templates", "");
|
2016-08-31 23:30:51 +08:00
|
|
|
|
|
|
|
// Try slashes
|
2019-11-06 02:43:49 +08:00
|
|
|
let dashed = dasherize(split[1].replace(/\./g, "/"));
|
2018-06-15 23:03:24 +08:00
|
|
|
if (
|
|
|
|
requirejs.entries[appBase + dashed] ||
|
|
|
|
requirejs.entries[adminBase + dashed]
|
|
|
|
) {
|
2016-08-31 23:30:51 +08:00
|
|
|
return split[0] + ":" + dashed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with dashes instead of slashes
|
2019-11-06 02:43:49 +08:00
|
|
|
dashed = dasherize(split[1].replace(/\./g, "-"));
|
2018-06-15 23:03:24 +08:00
|
|
|
if (
|
|
|
|
requirejs.entries[appBase + dashed] ||
|
|
|
|
requirejs.entries[adminBase + dashed]
|
|
|
|
) {
|
2016-08-31 23:30:51 +08:00
|
|
|
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.
|
2018-06-15 23:03:24 +08:00
|
|
|
const suffix = parsedName.type + "s/" + parsedName.fullNameWithoutType,
|
2019-11-06 02:43:49 +08:00
|
|
|
dashed = dasherize(suffix),
|
2018-06-15 23:03:24 +08:00
|
|
|
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
|
|
|
|
);
|
|
|
|
});
|
2016-08-31 23:30:51 +08:00
|
|
|
|
|
|
|
var module;
|
|
|
|
if (moduleName) {
|
2017-07-06 02:14:30 +08:00
|
|
|
module = requirejs(moduleName, null, null, true /* force sync */);
|
2018-06-15 23:03:24 +08:00
|
|
|
if (module && module["default"]) {
|
|
|
|
module = module["default"];
|
|
|
|
}
|
2016-08-31 23:30:51 +08:00
|
|
|
}
|
|
|
|
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) {
|
2018-06-15 23:03:24 +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
|
|
|
},
|
|
|
|
|
2016-12-20 00:19:10 +08:00
|
|
|
findConnectorTemplate(parsedName) {
|
2018-06-15 23:03:24 +08:00
|
|
|
const full = parsedName.fullNameWithoutType.replace("components/", "");
|
|
|
|
if (full.indexOf("connectors") === 0) {
|
2016-12-20 00:19:10 +08:00
|
|
|
return Ember.TEMPLATES[`javascripts/${full}`];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-31 23:30:51 +08:00
|
|
|
resolveTemplate(parsedName) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return (
|
|
|
|
this.findPluginMobileTemplate(parsedName) ||
|
|
|
|
this.findPluginTemplate(parsedName) ||
|
|
|
|
this.findMobileTemplate(parsedName) ||
|
|
|
|
this.findTemplate(parsedName) ||
|
|
|
|
this.findLoadingTemplate(parsedName) ||
|
|
|
|
this.findConnectorTemplate(parsedName) ||
|
|
|
|
Ember.TEMPLATES.not_found
|
|
|
|
);
|
2016-08-31 23:30:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
findPluginTemplate(parsedName) {
|
2018-06-15 23:03: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) {
|
2018-06-15 23:03:24 +08:00
|
|
|
var pluginParsedName = this.parseName(
|
|
|
|
parsedName.fullName.replace(
|
|
|
|
"template:",
|
|
|
|
"template:javascripts/mobile/"
|
|
|
|
)
|
|
|
|
);
|
2016-08-31 23:30:51 +08:00
|
|
|
return this.findTemplate(pluginParsedName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findMobileTemplate(parsedName) {
|
2016-11-04 02:15:26 +08:00
|
|
|
if (_options.mobileView) {
|
2018-06-15 23:03:24 +08:00
|
|
|
var mobileParsedName = this.parseName(
|
|
|
|
parsedName.fullName.replace("template:", "template:mobile/")
|
|
|
|
);
|
2016-08-31 23:30:51 +08:00
|
|
|
return this.findTemplate(mobileParsedName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
findTemplate(parsedName) {
|
|
|
|
const withoutType = parsedName.fullNameWithoutType,
|
2018-06-15 23:03:24 +08:00
|
|
|
slashedType = withoutType.replace(/\./g, "/"),
|
|
|
|
decamelized = withoutType.decamelize(),
|
|
|
|
dashed = decamelized.replace(/\./g, "-").replace(/\_/g, "-"),
|
|
|
|
templates = Ember.TEMPLATES;
|
|
|
|
|
|
|
|
return (
|
|
|
|
this._super(parsedName) ||
|
|
|
|
templates[slashedType] ||
|
|
|
|
templates[withoutType] ||
|
|
|
|
templates[withoutType.replace(/\.raw$/, "")] ||
|
|
|
|
templates[dashed] ||
|
|
|
|
templates[decamelized.replace(/\./, "/")] ||
|
|
|
|
templates[decamelized.replace(/\_/, "/")] ||
|
|
|
|
templates[`${baseName}/templates/${withoutType}`] ||
|
|
|
|
this.findAdminTemplate(parsedName) ||
|
|
|
|
this.findUnderscoredTemplate(parsedName)
|
|
|
|
);
|
2016-08-31 23:30:51 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
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();
|
2018-06-15 23:03:24 +08:00
|
|
|
if (decamelized.indexOf("components") === 0) {
|
2018-01-11 02:14:16 +08:00
|
|
|
let comPath = `admin/templates/${decamelized}`;
|
2018-06-15 23:03:24 +08:00
|
|
|
const compTemplate =
|
|
|
|
Ember.TEMPLATES[`javascripts/${comPath}`] || Ember.TEMPLATES[comPath];
|
|
|
|
if (compTemplate) {
|
|
|
|
return compTemplate;
|
|
|
|
}
|
2016-08-31 23:30:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (decamelized === "javascripts/admin") {
|
2018-06-15 23:03:24 +08:00
|
|
|
return Ember.TEMPLATES["admin/templates/admin"];
|
2016-08-31 23:30:51 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
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/")]
|
|
|
|
);
|
2016-08-31 23:30:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|