]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/helpers/Technique.js
basic content editing
[alttp.git] / resources / js / helpers / Technique.js
index ea60cc71653d91c50887050af7843e671dfe2647..7388ccb97bb5c3d7cb8816277a4e340235eef0f8 100644 (file)
@@ -1,4 +1,44 @@
+import i18n from '../i18n';
+
+export const getLink = tech => {
+       if (tech.type === 'dungeon') {
+               return `/dungeons/${tech.name}`;
+       }
+       if (tech.type === 'location') {
+               return `/locations/${tech.name}`;
+       }
+       if (tech.type === 'mode') {
+               return `/modes/${tech.name}`;
+       }
+       if (tech.type === 'ruleset') {
+               return `/rulesets/${tech.name}`;
+       }
+       return `/tech/${tech.name}`;
+};
+
+export const getRelations = (tech, type) => {
+       const rs = (tech && tech.relations) || [];
+       return type ? rs.filter(r => r && r.pivot && r.pivot.type === type) : rs;
+};
+
+export const hasRelations = (tech, type) => getRelations(tech, type).length > 0;
+
+export const getLanguages = tech => ['en', ...tech.translations.map(t => t.locale)];
+
+export const getMatchedLocale = (tech, lang) => {
+       const direct = tech.translations.find(t => t.locale === lang);
+       if (direct) {
+               return direct.locale;
+       }
+       const sameLang = tech.translations.find(t => t.locale.substr(0, 2) === lang.substr(0, 2));
+       if (sameLang) {
+               return sameLang.locale;
+       }
+       return 'en';
+};
+
 export const getTranslation = (tech, prop, lang) => {
+       if (!tech) return '';
        const direct = tech.translations.find(t => t.locale === lang);
        if (direct) {
                return direct[prop];
@@ -10,6 +50,16 @@ export const getTranslation = (tech, prop, lang) => {
        return tech[prop];
 };
 
+export const compareTranslation = (prop, lang) => (a, b) =>
+       getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang));
+
+export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language));
+
 export default {
+       compareTranslation,
+       getLanguages,
+       getRelations,
        getTranslation,
+       hasRelations,
+       sorted,
 };