Filter on site settings across all categories

This commit is contained in:
Neil Lalonde 2013-11-14 12:37:41 -05:00
parent 1e37abc310
commit df213d7b23
10 changed files with 102 additions and 29 deletions

View file

@ -0,0 +1,19 @@
Discourse.AdminSiteSettingsCategoryController = Ember.ObjectController.extend({
categoryNameKey: null,
needs: ['adminSiteSettings'],
filteredContent: function() {
if (!this.get('categoryNameKey')) { return Em.A(); }
var category = this.get('controllers.adminSiteSettings.content').find(function(siteSettingCategory) {
return siteSettingCategory.nameKey === this.get('categoryNameKey');
}, this);
if (category) {
return category.siteSettings;
} else {
return Em.A();
}
}.property('controllers.adminSiteSettings.content', 'categoryNameKey')
});

View file

@ -15,31 +15,47 @@ Discourse.AdminSiteSettingsController = Ember.ArrayController.extend(Discourse.P
@property filteredContent @property filteredContent
**/ **/
filteredContent: function() { filterContent: function() {
// If we have no content, don't bother filtering anything // If we have no content, don't bother filtering anything
if (!this.present('content')) return null; if (!this.present('allSiteSettings')) return;
var filter; var filter;
if (this.get('filter')) { if (this.get('filter')) {
filter = this.get('filter').toLowerCase(); filter = this.get('filter').toLowerCase();
} }
var adminSettingsController = this; if ((filter === undefined || filter.length < 1) && !this.get('onlyOverridden')) {
this.set('model', this.get('allSiteSettings'));
return;
}
var maxResults = Em.isNone(filter) ? this.get('content.length') : 20; var self = this,
return _.first(this.get('content').filter(function(item, index, enumerable) { matches,
if (adminSettingsController.get('onlyOverridden') && !item.get('overridden')) return false; matchesGroupedByCategory = Em.A();
if (filter) {
if (item.get('setting').toLowerCase().indexOf(filter) > -1) return true; _.each(this.get('allSiteSettings'), function(settingsCategory) {
if (item.get('description').toLowerCase().indexOf(filter) > -1) return true; matches = settingsCategory.siteSettings.filter(function(item) {
if (item.get('value').toLowerCase().indexOf(filter) > -1) return true; if (self.get('onlyOverridden') && !item.get('overridden')) return false;
return false; if (filter) {
if (item.get('setting').toLowerCase().indexOf(filter) > -1) return true;
if (item.get('description').toLowerCase().indexOf(filter) > -1) return true;
if (item.get('value').toLowerCase().indexOf(filter) > -1) return true;
return false;
} else {
return true;
}
});
if (matches.length > 0) {
matchesGroupedByCategory.pushObject({
nameKey: settingsCategory.nameKey,
name: settingsCategory.name,
siteSettings: matches});
} }
});
return true; this.set('model', matchesGroupedByCategory);
}), maxResults); }.observes('filter', 'onlyOverridden'),
}.property('filter', 'content.@each', 'onlyOverridden'),
actions: { actions: {

View file

@ -118,7 +118,7 @@ Discourse.SiteSetting.reopenClass({
categories[s.category].pushObject(Discourse.SiteSetting.create(s)); categories[s.category].pushObject(Discourse.SiteSetting.create(s));
}); });
_.each(categoryNames, function(n) { _.each(categoryNames, function(n) {
result.pushObject({nameKey: n, name: I18n.t('admin.site_settings.categories.' + n),siteSettings: categories[n]}); result.pushObject({nameKey: n, name: I18n.t('admin.site_settings.categories.' + n), siteSettings: categories[n]});
}); });
return result; return result;
}); });

View file

@ -8,11 +8,6 @@
**/ **/
Discourse.AdminSiteSettingsCategoryRoute = Discourse.Route.extend({ Discourse.AdminSiteSettingsCategoryRoute = Discourse.Route.extend({
model: function(params) { model: function(params) {
var category = this.modelFor('adminSiteSettings').find(function(siteSettingCategory) { this.controllerFor('adminSiteSettingsCategory').set('categoryNameKey', params.category_id);
return siteSettingCategory.nameKey === params.category_id;
});
if (category) {
return category.siteSettings;
}
} }
}); });

View file

@ -9,6 +9,10 @@
Discourse.AdminSiteSettingsRoute = Discourse.Route.extend({ Discourse.AdminSiteSettingsRoute = Discourse.Route.extend({
model: function() { model: function() {
return Discourse.SiteSetting.findAll(); return Discourse.SiteSetting.findAll();
},
afterModel: function(siteSettings) {
this.controllerFor('adminSiteSettings').set('allSiteSettings', siteSettings);
} }
}); });

View file

@ -1,9 +1,9 @@
<div class='admin-controls'> <div class='admin-controls'>
<div class='search controls'> <div class='search controls'>
<label> <label>
{{input type="checkbox" checked=onlyOverridden}} {{input type="checkbox" checked=onlyOverridden}}
{{i18n admin.site_settings.show_overriden}} {{i18n admin.site_settings.show_overriden}}
</label> </label>
</div> </div>
<div class='controls'> <div class='controls'>
{{textField value=filter placeholderKey="type_to_filter"}} {{textField value=filter placeholderKey="type_to_filter"}}
@ -12,7 +12,7 @@
<ul class="nav nav-pills"> <ul class="nav nav-pills">
{{#each category in controller}} {{#each category in controller}}
<li>{{#link-to 'adminSiteSettingsCategory' category.nameKey}}{{category.name}}{{/link-to}}</a></li> <li>{{#link-to 'adminSiteSettingsCategory' category.nameKey}}{{category.name}}{{/link-to}}</li>
{{/each}} {{/each}}
</ul> </ul>

View file

@ -1 +1 @@
{{collection contentBinding="content" classNames="form-horizontal settings" itemViewClass="Discourse.SiteSettingView"}} {{collection contentBinding="filteredContent" classNames="form-horizontal settings" itemViewClass="Discourse.SiteSettingView"}}

View file

@ -1418,7 +1418,7 @@ en:
reset: 'reset to default' reset: 'reset to default'
none: 'none' none: 'none'
categories: categories:
mandatory: 'Mandatory' required: 'Required'
users: 'Users' users: 'Users'
posting: 'Posting' posting: 'Posting'
email: 'Email' email: 'Email'

View file

@ -1,4 +1,4 @@
mandatory: required:
title: title:
client: true client: true
default: 'Discourse' default: 'Discourse'

View file

@ -0,0 +1,39 @@
module("Discourse.AdminSiteSettingsController");
test("filter", function() {
var allSettings = Em.A([Ember.Object.create({
nameKey: 'users', name: 'users',
siteSettings: [Discourse.SiteSetting.create({"setting":"username_change_period","description":"x","default":3,"type":"fixnum","value":"3","category":"users"})]
}), Ember.Object.create({
nameKey: 'posting', name: 'posting',
siteSettings: [Discourse.SiteSetting.create({"setting":"display_name_on_posts","description":"x","default":false,"type":"bool","value":"true","category":"posting"})]
})]);
var adminSiteSettingsController = testController(Discourse.AdminSiteSettingsController, allSettings);
adminSiteSettingsController.set('allSiteSettings', allSettings);
equal(adminSiteSettingsController.get('content')[0].nameKey, 'users', "Can get first site setting category's name key.");
adminSiteSettingsController.set('filter', 'username_change');
equal(adminSiteSettingsController.get('content').length, 1, "a. Filter with one match for username_change");
equal(adminSiteSettingsController.get('content')[0].nameKey, "users", "b. Filter with one match for username_change");
equal(adminSiteSettingsController.get('content')[0].siteSettings[0].setting, "username_change_period", "c. Filter with one match for username_change");
adminSiteSettingsController.set('filter', 'name_on');
equal(adminSiteSettingsController.get('content').length, 1, "a. Filter with one match for name_on");
equal(adminSiteSettingsController.get('content')[0].nameKey, "posting", "b. Filter with one match for name_on");
equal(adminSiteSettingsController.get('content')[0].siteSettings[0].setting, "display_name_on_posts", "c. Filter with one match for name_on");
adminSiteSettingsController.set('filter', 'name');
equal(adminSiteSettingsController.get('content').length, 2, "a. Filter with one match for name");
equal(adminSiteSettingsController.get('content')[0].nameKey, "users", "b. Filter with one match for name");
equal(adminSiteSettingsController.get('content')[1].nameKey, "posting", "c. Filter with one match for name");
equal(adminSiteSettingsController.get('content')[0].siteSettings[0].setting, "username_change_period", "d. Filter with one match for name");
equal(adminSiteSettingsController.get('content')[1].siteSettings[0].setting, "display_name_on_posts", "d. Filter with one match for name");
adminSiteSettingsController.set('filter', '');
adminSiteSettingsController.set('onlyOverridden', true);
equal(adminSiteSettingsController.get('content').length, 1, "a. onlyOverridden with one match");
equal(adminSiteSettingsController.get('content')[0].nameKey, "posting", "b. onlyOverridden with one match");
equal(adminSiteSettingsController.get('content')[0].siteSettings[0].setting, "display_name_on_posts", "c. onlyOverridden with one match");
});