app/assets/javascripts/utils/article_utils.js
import { ARTICLES_PER_PAGE } from '../constants';
export default class ArticleUtils {
// Returns article or item, based on home Wiki project.
static articlesOrItems(project) {
return project === 'wikidata' ? 'items' : 'articles';
}
// Returns article or item message, based on articles or items keyword.
static articlesOrItemsMsg(messageKey, articlesOrItems, defaultarticlesOrItems = 'articles') {
return I18n.t(`${articlesOrItems}.${messageKey}`, {
defaults: [{ scope: `${defaultarticlesOrItems}.${messageKey}` }]
});
}
// Returns article or item message, based on home Wiki project.
static I18n(messageKey, project) {
const articlesOrItems = ArticleUtils.articlesOrItems(project);
return ArticleUtils.articlesOrItemsMsg(messageKey, articlesOrItems);
}
static projectSuffix(project, messageKey) {
return project === 'wikidata' ? `${messageKey}_wikidata` : messageKey;
}
// The following mapping is also present in article.rb file and,
// wiki.rb has mapping of wikis to namespaces. Hence, any modifications
// in namespaces should also reflect in the corresponding files.
static NamespaceIdMapping = {
0: 'main',
1: 'talk',
2: 'user',
4: 'project',
6: 'file',
8: 'mediaWiki',
10: 'template',
12: 'help',
14: 'category',
104: 'page',
108: 'book',
110: 'wikijunior',
114: 'translation',
118: 'draft',
120: 'property',
122: 'query',
146: 'lexeme',
100: {
wiktionary: 'appendix',
wikisource: 'portal',
wikiversity: 'school'
},
102: {
wikisource: 'author',
wikibooks: 'cookbook',
wikiversity: 'portal'
},
106: {
wiktionary: 'rhymes',
wikisource: 'index',
wikiversity: 'collection'
}
};
// Get namespace id from its title
static getNamespaceId(namespace) {
const ns_id_mapping = this.NamespaceIdMapping;
const ns_id = Object.keys(ns_id_mapping).find(ns => ns_id_mapping[ns] === namespace);
return Number(ns_id);
}
}
export const getPageRange = (currentPage, numberOfArticles) => {
return `${(currentPage - 1) * ARTICLES_PER_PAGE + 1} - ${Math.min(numberOfArticles, currentPage * ARTICLES_PER_PAGE)}`;
};