REFACTOR: Remove many `Discourse.Category.list()` calls

This commit is contained in:
Robin Ward 2017-11-01 15:08:18 -04:00
parent 91438849d2
commit 0a69f2bc77
16 changed files with 39 additions and 40 deletions

View File

@ -10,9 +10,12 @@ export default Ember.Component.extend({
@computed()
categories() {
return Discourse.Category.list();
return this.site.get('categoriesList');
},
@computed('category.can_edit')
showCategoryEdit: canEdit => canEdit,
@computed("filterMode")
navItems(filterMode) {
// we don't want to show the period in the navigation bar since it's in a dropdown

View File

@ -9,13 +9,13 @@ export default buildCategoryPanel('general', {
// background colors are available as a pipe-separated string
backgroundColors: function() {
const categories = Discourse.Category.list();
const categories = this.site.get('categoriesList');
return this.siteSettings.category_colors.split("|").map(function(i) { return i.toUpperCase(); }).concat(
categories.map(function(c) { return c.color.toUpperCase(); }) ).uniq();
}.property(),
usedBackgroundColors: function() {
const categories = Discourse.Category.list();
const categories = this.site.get('categoriesList');
const category = this.get('category');
// If editing a category, don't include its color:
@ -25,9 +25,7 @@ export default buildCategoryPanel('general', {
}.property('category.id', 'category.color'),
parentCategories: function() {
return Discourse.Category.list().filter(function (c) {
return !c.get('parentCategory');
});
return this.site.get('categoriesList').filter(c => !c.get('parentCategory'));
}.property(),
categoryBadgePreview: function() {

View File

@ -69,6 +69,7 @@ export default Ember.Controller.extend({
topic: null,
linkLookup: null,
whisperOrUnlistTopic: Ember.computed.or('model.whisper', 'model.unlistTopic'),
categories: Ember.computed.alias('site.categoriesList'),
@computed('model.replyingToTopic', 'model.creatingPrivateMessage', 'model.targetUsernames')
focusTarget(replyingToTopic, creatingPM, usernames) {
@ -396,10 +397,6 @@ export default Ember.Controller.extend({
},
categories: function() {
return Discourse.Category.list();
}.property(),
disableSubmit: Ember.computed.or("model.loading", "isUploading"),
save(force) {
@ -654,7 +651,7 @@ export default Ember.Controller.extend({
if (!splitCategory[1]) {
category = this.site.get('categories').findBy('nameLower', splitCategory[0].toLowerCase());
} else {
const categories = Discourse.Category.list();
const categories = this.site.get('categories');
const mainCategory = categories.findBy('nameLower', splitCategory[0].toLowerCase());
category = categories.find(function(item) {
return item && item.get('nameLower') === splitCategory[1].toLowerCase() && item.get('parent_category_id') === mainCategory.id;

View File

@ -65,7 +65,7 @@ export default Ember.Controller.extend(ModalFunctionality, {
saveCategory() {
const self = this,
model = this.get('model'),
parentCategory = Discourse.Category.list().findBy('id', parseInt(model.get('parent_category_id'), 10));
parentCategory = this.site.get('categories').findBy('id', parseInt(model.get('parent_category_id'), 10));
this.set('saving', true);
model.set('parentCategory', parentCategory);

View File

@ -58,6 +58,8 @@ export default Ember.Controller.extend(BulkTopicSelection, {
max_posts: null,
q: null,
categories: Ember.computed.alias('site.categoriesList'),
queryParams: ['order', 'ascending', 'status', 'state', 'search', 'max_posts', 'q'],
navItems: function() {
@ -68,10 +70,6 @@ export default Ember.Controller.extend(BulkTopicSelection, {
return Discourse.SiteSettings.show_filter_by_tag;
}.property('category'),
categories: function() {
return Discourse.Category.list();
}.property(),
showAdminControls: function() {
return !this.get('additionalTags') && this.get('canAdminTag') && !this.get('category');
}.property('additionalTags', 'canAdminTag', 'category'),

View File

@ -752,9 +752,7 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
return selectedPostsUsername !== undefined;
},
categories: function() {
return Discourse.Category.list();
}.property(),
categories: Ember.computed.alias('site.categoriesList'),
canSelectAll: Em.computed.not('allPostsSelected'),

View File

@ -205,9 +205,7 @@ Category.reopenClass({
},
list() {
return Discourse.SiteSettings.fixed_category_positions ?
Discourse.Site.currentProp('categories') :
Discourse.Site.currentProp('sortedCategories');
return Discourse.Site.currentProp('categoriesList');
},
listByActivity() {

View File

@ -85,7 +85,7 @@ const Composer = RestModel.extend({
@computed("privateMessage", "archetype.hasOptions")
showCategoryChooser(isPrivateMessage, hasOptions) {
const manyCategories = Discourse.Category.list().length > 1;
const manyCategories = this.site.get('categories').length > 1;
return !isPrivateMessage && (hasOptions || manyCategories);
},
@ -481,7 +481,7 @@ const Composer = RestModel.extend({
this.set('categoryId', opts.categoryId || this.get('topic.category.id'));
if (!this.get('categoryId') && this.get('creatingTopic')) {
const categories = Discourse.Category.list();
const categories = this.site.get('categories');
if (categories.length === 1) {
this.set('categoryId', categories[0].get('id'));
}

View File

@ -111,6 +111,7 @@ NavItem.reopenClass({
opts = opts || {};
if (anonymous && !Discourse.Site.currentProp('anonymous_top_menu_items').includes(testName)) return null;
if (!Discourse.Category.list() && testName === "categories") return null;
if (!Discourse.Site.currentProp('top_menu_items').includes(testName)) return null;

View File

@ -54,6 +54,14 @@ const Site = RestModel.extend({
return result;
},
// Returns it in the correct order, by setting
@computed
categoriesList() {
return this.siteSettings.fixed_category_positions ?
this.get('categories') :
this.get('sortedCategories');
},
postActionTypeById(id) {
return this.get("postActionByIdLookup.action" + id);
},

View File

@ -135,7 +135,7 @@ const Topic = RestModel.extend({
const categoryName = this.get('categoryName');
let category;
if (categoryName) {
category = Discourse.Category.list().findBy('name', categoryName);
category = this.site.get('categories').findBy('name', categoryName);
}
this.set('category', category);
}.observes('categoryName'),

View File

@ -36,14 +36,14 @@ export default {
app.register('topic-tracking-state:main', topicTrackingState, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, 'topicTrackingState', 'topic-tracking-state:main'));
const site = Discourse.Site.current();
app.register('site:main', site, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, 'site', 'site:main'));
const siteSettings = Discourse.SiteSettings;
app.register('site-settings:main', siteSettings, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, 'siteSettings', 'site-settings:main'));
const site = Discourse.Site.current();
app.register('site:main', site, { instantiate: false });
ALL_TARGETS.forEach(t => app.inject(t, 'site', 'site:main'));
app.register('search-service:main', SearchService);
ALL_TARGETS.forEach(t => app.inject(t, 'searchService', 'search-service:main'));

View File

@ -17,7 +17,7 @@
action=createTopic
disabled=createTopicDisabled}}
{{#if category.can_edit}}
{{#if showCategoryEdit}}
{{d-button
class="btn-default edit-category"
action=editCategory

View File

@ -130,7 +130,7 @@ export default createWidget('hamburger-menu', {
const hideUncategorized = !this.siteSettings.allow_uncategorized_topics;
const isStaff = Discourse.User.currentProp('staff');
const categories = Discourse.Category.list().reject((c) => {
const categories = this.site.get('categoriesList').reject((c) => {
if (c.get('parentCategory.show_subcategory_list')) { return true; }
if (hideUncategorized && c.get('isUncategorizedCategory') && !isStaff) { return true; }
return false;

View File

@ -1,7 +1,6 @@
/* global QUnit, fixtures */
/* global QUnit, resetSite */
import sessionFixtures from 'fixtures/session-fixtures';
import siteFixtures from 'fixtures/site-fixtures';
import HeaderComponent from 'discourse/components/site-header';
import { forceMobile, resetMobile } from 'discourse/lib/mobile';
import { resetPluginApi } from 'discourse/lib/plugin-api';
@ -60,7 +59,6 @@ export function acceptance(name, options) {
HeaderComponent.reopen({examineDockHeader: function() { }});
resetExtraClasses();
const siteJson = siteFixtures['site.json'].site;
if (options.beforeEach) {
options.beforeEach.call(this);
}
@ -78,7 +76,7 @@ export function acceptance(name, options) {
}
if (options.site) {
Discourse.Site.resetCurrent(Discourse.Site.create(jQuery.extend(true, {}, siteJson, options.site)));
resetSite(Discourse.SiteSettings, options.site);
}
clearOutletCache();
@ -93,8 +91,7 @@ export function acceptance(name, options) {
}
flushMap();
Discourse.User.resetCurrent();
Discourse.Site.resetCurrent(Discourse.Site.create(jQuery.extend(true, {}, fixtures['site.json'].site)));
resetSite(Discourse.SiteSettings);
resetExtraClasses();
clearOutletCache();
clearHTMLCache();

View File

@ -81,10 +81,11 @@ function dup(obj) {
return jQuery.extend(true, {}, obj);
}
function resetSite() {
function resetSite(siteSettings, extras) {
var createStore = require('helpers/create-store').default;
var siteAttrs = dup(fixtures['site.json'].site);
var siteAttrs = $.extend({}, fixtures['site.json'].site, extras || {});
siteAttrs.store = createStore();
siteAttrs.siteSettings = siteSettings;
Discourse.Site.resetCurrent(Discourse.Site.create(siteAttrs));
}
@ -105,7 +106,7 @@ QUnit.testStart(function(ctx) {
Discourse.BaseUrl = "localhost";
Discourse.Session.resetCurrent();
Discourse.User.resetCurrent();
resetSite();
resetSite(Discourse.SiteSettings);
_DiscourseURL.redirectedTo = null;
_DiscourseURL.redirectTo = function(url) {