diff --git a/app/assets/javascripts/discourse/components/utilities.js b/app/assets/javascripts/discourse/components/utilities.js index 9936a67ba02..c96efb2cf76 100644 --- a/app/assets/javascripts/discourse/components/utilities.js +++ b/app/assets/javascripts/discourse/components/utilities.js @@ -39,10 +39,11 @@ Discourse.Utilities = { // Create a badge like category link categoryLink: function(category) { - var color, name, description, result; + var color, textColor, name, description, result; if (!category) return ""; color = Em.get(category, 'color'); + textColor = Em.get(category, 'text_color'); name = Em.get(category, 'name'); description = Em.get(category, 'description'); @@ -52,7 +53,7 @@ Discourse.Utilities = { // Add description if we have it if (description) result += "title=\"" + description + "\" "; - return result + "style=\"background-color: #" + color + "\">" + name + ""; + return result + "style=\"background-color: #" + color + "; color: #" + textColor + ";\">" + name + ""; }, avatarUrl: function(username, size, template) { diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 4bf87ca0caa..461faadb2fc 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -13,8 +13,8 @@ Discourse.Category = Discourse.Model.extend({ }).property('name'), style: (function() { - return "background-color: #" + (this.get('color')); - }).property('color'), + return "background-color: #" + (this.get('category.color')) + "; color: #" + (this.get('category.text_color')) + ";"; + }).property('color', 'text_color'), moreTopics: (function() { return this.get('topic_count') > Discourse.SiteSettings.category_featured_topics; @@ -32,7 +32,8 @@ Discourse.Category = Discourse.Model.extend({ return this.ajax(url, { data: { name: this.get('name'), - color: this.get('color') + color: this.get('color'), + text_color: this.get('text_color') }, type: this.get('id') ? 'PUT' : 'POST', success: function(result) { return args.success(result); }, diff --git a/app/assets/javascripts/discourse/templates/excerpt/category.js.handlebars b/app/assets/javascripts/discourse/templates/excerpt/category.js.handlebars index db20ce8f6c4..2a57235f3b8 100644 --- a/app/assets/javascripts/discourse/templates/excerpt/category.js.handlebars +++ b/app/assets/javascripts/discourse/templates/excerpt/category.js.handlebars @@ -1,5 +1,5 @@
- {{unbound view.name}} + {{unbound view.name}} {{#if view.excerpt}}
diff --git a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars index 7c87ec498ac..1317e44b395 100644 --- a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars @@ -20,11 +20,22 @@
- + -
- #{{view Discourse.TextField valueBinding="view.category.color" placeholderKey="category.color_placeholder" maxlength="6"}} - {{i18n preview}} +
+ {{i18n preview}} + +
+ {{i18n category.background_color}}: + #{{view Discourse.TextField valueBinding="view.category.color" placeholderKey="category.color_placeholder" maxlength="6"}} + {{view Discourse.ColorsView colorsBinding="view.predefinedColors" valueBinding="view.category.color"}} +
+ +
+ {{i18n category.foreground_color}}: + #{{view Discourse.TextField valueBinding="view.category.text_color" placeholderKey="category.color_placeholder" maxlength="6"}} + {{view Discourse.ColorsView colorsBinding="view.predefinedColors" valueBinding="view.category.text_color"}} +
diff --git a/app/assets/javascripts/discourse/templates/search/category_result.js.handlebars b/app/assets/javascripts/discourse/templates/search/category_result.js.handlebars index 7cbee1710ac..0bf3bfba785 100644 --- a/app/assets/javascripts/discourse/templates/search/category_result.js.handlebars +++ b/app/assets/javascripts/discourse/templates/search/category_result.js.handlebars @@ -1,6 +1,6 @@ {{#with view.content}} - {{unbound title}} + {{unbound title}} {{/with}} diff --git a/app/assets/javascripts/discourse/views/combobox_view_category.js b/app/assets/javascripts/discourse/views/combobox_view_category.js index cac5e069a1e..55f8d463efd 100644 --- a/app/assets/javascripts/discourse/views/combobox_view_category.js +++ b/app/assets/javascripts/discourse/views/combobox_view_category.js @@ -8,12 +8,13 @@ **/ Discourse.ComboboxViewCategory = Discourse.ComboboxView.extend({ none: 'category.none', - dataAttributes: ['color', 'description'], + dataAttributes: ['color', 'text_color', 'description'], template: function(text, templateData) { if (!templateData.color) return text; - var result = " 1 JOIN posts_search s on s.id = p.id @@ -74,7 +77,8 @@ module Search '/category/' || c.slug AS url, c.name AS title, NULL AS email, - c.color + c.color, + c.text_color FROM categories AS c JOIN categories_search s on s.id = c.id WHERE s.search_data @@ TO_TSQUERY(:locale, :query) @@ -168,6 +172,7 @@ module Search end row.delete('email') row.delete('color') unless type == 'category' + row.delete('text_color') unless type == 'category' grouped[type] ||= [] grouped[type] << row diff --git a/spec/controllers/categories_controller_spec.rb b/spec/controllers/categories_controller_spec.rb index 73979ed656c..f0016998274 100644 --- a/spec/controllers/categories_controller_spec.rb +++ b/spec/controllers/categories_controller_spec.rb @@ -14,22 +14,26 @@ describe CategoriesController do it "raises an exception when they don't have permission to create it" do Guardian.any_instance.expects(:can_create?).with(Category, nil).returns(false) - xhr :post, :create, name: 'hello', color: '#ff0' + xhr :post, :create, name: 'hello', color: 'ff0', text_color: 'fff' response.should be_forbidden end it 'raises an exception when the name is missing' do - lambda { xhr :post, :create, color: '#ff0' }.should raise_error(Discourse::InvalidParameters) + lambda { xhr :post, :create, color: 'ff0', text_color: 'fff' }.should raise_error(Discourse::InvalidParameters) end it 'raises an exception when the color is missing' do - lambda { xhr :post, :create, name: 'hello' }.should raise_error(Discourse::InvalidParameters) + lambda { xhr :post, :create, name: 'hello', text_color: 'fff' }.should raise_error(Discourse::InvalidParameters) + end + + it 'raises an exception when the text color is missing' do + lambda { xhr :post, :create, name: 'hello', color: 'ff0' }.should raise_error(Discourse::InvalidParameters) end describe 'failure' do before do @category = Fabricate(:category, user: @user) - xhr :post, :create, name: @category.name, color: '#ff0' + xhr :post, :create, name: @category.name, color: 'ff0', text_color: 'fff' end it { should_not respond_with(:success) } @@ -42,7 +46,7 @@ describe CategoriesController do describe 'success' do before do - xhr :post, :create, name: 'hello', color: '#ff0' + xhr :post, :create, name: 'hello', color: 'ff0', text_color: 'fff' end it 'creates a category' do @@ -97,22 +101,26 @@ describe CategoriesController do it "raises an exception if they don't have permission to edit it" do Guardian.any_instance.expects(:can_edit?).returns(false) - xhr :put, :update, id: @category.slug, name: 'hello', color: '#ff0' + xhr :put, :update, id: @category.slug, name: 'hello', color: 'ff0', text_color: 'fff' response.should be_forbidden end it "requires a name" do - lambda { xhr :put, :update, id: @category.slug, color: '#fff' }.should raise_error(Discourse::InvalidParameters) + lambda { xhr :put, :update, id: @category.slug, color: 'fff', text_color: '0ff' }.should raise_error(Discourse::InvalidParameters) end it "requires a color" do - lambda { xhr :put, :update, id: @category.slug, name: 'asdf'}.should raise_error(Discourse::InvalidParameters) + lambda { xhr :put, :update, id: @category.slug, name: 'asdf', text_color: '0ff' }.should raise_error(Discourse::InvalidParameters) + end + + it "requires a text color" do + lambda { xhr :put, :update, id: @category.slug, name: 'asdf', color: 'fff' }.should raise_error(Discourse::InvalidParameters) end describe 'failure' do before do @other_category = Fabricate(:category, name: 'Other', user: @user ) - xhr :put, :update, id: @category.id, name: @other_category.name, color: '#ff0' + xhr :put, :update, id: @category.id, name: @other_category.name, color: 'ff0', text_color: 'fff' end it 'returns errors on a duplicate category name' do @@ -126,7 +134,7 @@ describe CategoriesController do describe 'success' do before do - xhr :put, :update, id: @category.id, name: 'science', color: '#000' + xhr :put, :update, id: @category.id, name: 'science', color: '000', text_color: '0ff' @category.reload end @@ -135,7 +143,11 @@ describe CategoriesController do end it 'updates the color' do - @category.color.should == '#000' + @category.color.should == '000' + end + + it 'updates the text color' do + @category.text_color.should == '0ff' end end