This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/styleguide/assets/javascripts/discourse/lib/styleguide.js
David Taylor c139767055
DEV: Remove Ember.TEMPLATES and centralize template resolution rules (#19220)
In the past, the result of template compilation would be stored directly in `Ember.TEMPLATES`. Following the move to more modern ember-cli-based compilation, templates are now compiled to es6 modules. To handle forward/backwards compatibility during these changes we had logic in `discourse-boot` which would extract templates from the es6 modules and store them into the legacy-style `Ember.TEMPLATES` object.

This commit removes that shim, and updates our resolver to fetch templates directly from es6 modules. This is closer to how 'vanilla' Ember handles template resolution. We still have a lot of discourse-specific logic, but now it is centralised in one location and should be easier to understand and normalize in future.

This commit should not introduce any behaviour change.
2022-11-29 10:24:35 +00:00

75 lines
1.8 KiB
JavaScript

import DiscourseTemplateMap from "discourse-common/lib/discourse-template-map";
let _allCategories = null;
let _sectionsById = {};
let _notes = {};
export const CATEGORIES = ["atoms", "molecules", "organisms"];
export function sectionById(id) {
// prime cache
allCategories();
return _sectionsById[id];
}
function sortSections(a, b) {
let result = a.priority - b.priority;
if (result === 0) {
return a.id < b.id ? -1 : 1;
}
return result;
}
export function allCategories() {
if (_allCategories) {
return _allCategories;
}
let categories = {};
let paths = CATEGORIES.join("|");
// Find a list of sections based on what templates are available
// eslint-disable-next-line no-undef
DiscourseTemplateMap.keys().forEach((e) => {
let regexp = new RegExp(`styleguide\/(${paths})\/(\\d+)?\\-?([^\\/]+)$`);
let matches = e.match(regexp);
if (matches) {
let section = {
id: matches[3],
priority: parseInt(matches[2] || "100", 10),
category: matches[1],
templateName: e.replace(/^.*styleguide\//, ""),
};
if (!categories[section.category]) {
categories[section.category] = [];
}
categories[section.category].push(section);
_sectionsById[section.id] = section;
}
// Look for notes
regexp = new RegExp(`components\/notes\/(\\d+)?\\-?([^\\/]+)$`);
matches = e.match(regexp);
if (matches) {
_notes[matches[2]] = e.replace(/^.*notes\//, "");
}
});
_allCategories = [];
CATEGORIES.forEach((c) => {
let sections = categories[c];
if (sections) {
_allCategories.push({
id: c,
sections: sections.sort(sortSections),
});
}
});
return _allCategories;
}
export function findNote(section) {
return _notes[section.id];
}