From c6370a347921fd51be83edeae5f13c75a0e4360a Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 3 May 2013 17:55:04 -0400 Subject: [PATCH] Add ability to edit the uncategorized category name, color, and text_color in a modal --- .../javascripts/admin/models/site_setting.js | 7 +++ .../javascripts/discourse/models/category.js | 4 ++ .../modal/edit_category.js.handlebars | 35 ++++++++------- .../views/modal/edit_category_view.js | 44 ++++++++++++++----- app/models/category_list.rb | 2 + app/models/site_setting.rb | 2 + config/locales/client.en.yml | 2 + config/locales/server.en.yml | 2 + 8 files changed, 70 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/admin/models/site_setting.js b/app/assets/javascripts/admin/models/site_setting.js index 79eeeca7d..36da3578c 100644 --- a/app/assets/javascripts/admin/models/site_setting.js +++ b/app/assets/javascripts/admin/models/site_setting.js @@ -98,6 +98,13 @@ Discourse.SiteSetting.reopenClass({ result.set('diags', settings.diags); }); return result; + }, + + update: function(key, value) { + return Discourse.ajax(Discourse.getURL("/admin/site_settings/") + key, { + type: 'PUT', + data: { value: value } + }); } }); diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 95c75709b..f99f8058a 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -20,6 +20,10 @@ Discourse.Category = Discourse.Model.extend({ return this.get('topic_count') > Discourse.SiteSettings.category_featured_topics; }.property('topic_count'), + isUncategorized: function() { + return (!this.get('id') && this.get('name')); + }.property('id', 'name'), + save: function(args) { var url = Discourse.getURL("/categories"); if (this.get('id')) { 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 c4bd1a936..c86e26649 100644 --- a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars @@ -8,25 +8,26 @@ {{view Discourse.TextField valueBinding="name" placeholderKey="category.name_placeholder" maxlength="50"}} -
- + {{#unless isUncategorized}} +
+ - {{#if description}} - {{description}} - {{else}} - {{i18n category.no_description}} - {{/if}} - {{#if topic_url}} -
- - {{/if}} -
- -
- - {{view Discourse.HotnessView hotnessBinding="hotness"}} -
+ {{#if description}} + {{description}} + {{else}} + {{i18n category.no_description}} + {{/if}} + {{#if topic_url}} +
+ + {{/if}} +
+
+ + {{view Discourse.HotnessView hotnessBinding="hotness"}} +
+ {{/unless}}
diff --git a/app/assets/javascripts/discourse/views/modal/edit_category_view.js b/app/assets/javascripts/discourse/views/modal/edit_category_view.js index 57cd0bafc..257429be7 100644 --- a/app/assets/javascripts/discourse/views/modal/edit_category_view.js +++ b/app/assets/javascripts/discourse/views/modal/edit_category_view.js @@ -47,6 +47,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({ title: function() { if (this.get('category.id')) return Em.String.i18n("category.edit_long"); + if (this.get('category.isUncategorized')) return Em.String.i18n("category.edit_uncategorized"); return Em.String.i18n("category.create"); }.property('category.id'), @@ -57,6 +58,7 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({ buttonTitle: function() { if (this.get('saving')) return Em.String.i18n("saving"); + if (this.get('category.isUncategorized')) return Em.String.i18n("save"); return (this.get('category.id') ? Em.String.i18n("category.save") : Em.String.i18n("category.create")); }.property('saving', 'category.id'), @@ -78,6 +80,8 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({ categoryView.set('id', categoryView.get('category.slug')); categoryView.set('loading', false); }); + } else if( this.get('category.isUncategorized') ) { + this.set('category', this.get('category')); } else { this.set('category', Discourse.Category.create({ color: 'AB9364', text_color: 'FFFFFF', hotness: 5 })); } @@ -92,17 +96,35 @@ Discourse.EditCategoryView = Discourse.ModalBodyView.extend({ saveCategory: function() { var categoryView = this; this.set('saving', true); - this.get('category').save().then(function(result) { - // success - $('#discourse-modal').modal('hide'); - var url = Discourse.getURL("/category/") + (Discourse.Utilities.categoryUrlId(result.category)); - Discourse.URL.redirectTo(url); - }, function(errors) { - // errors - if(errors.length === 0) errors.push(Em.String.i18n("category.creation_error")); - categoryView.displayErrors(errors); - categoryView.set('saving', false); - }); + if( this.get('category.isUncategorized') ) { + $.when( + Discourse.SiteSetting.update('uncategorized_color', this.get('category.color')), + Discourse.SiteSetting.update('uncategorized_text_color', this.get('category.text_color')), + Discourse.SiteSetting.update('uncategorized_name', this.get('category.name')) + ).then(function() { + // success + $('#discourse-modal').modal('hide'); + var url = Discourse.getURL("/category/") + categoryView.get('category.name'); + Discourse.URL.redirectTo(url); + }, function(errors) { + // errors + if(errors.length === 0) errors.push(Em.String.i18n("category.save_error")); + categoryView.displayErrors(errors); + categoryView.set('saving', false); + }); + } else { + this.get('category').save().then(function(result) { + // success + $('#discourse-modal').modal('hide'); + var url = Discourse.getURL("/category/") + (Discourse.Utilities.categoryUrlId(result.category)); + Discourse.URL.redirectTo(url); + }, function(errors) { + // errors + if(errors.length === 0) errors.push(Em.String.i18n("category.creation_error")); + categoryView.displayErrors(errors); + categoryView.set('saving', false); + }); + } }, deleteCategory: function() { diff --git a/app/models/category_list.rb b/app/models/category_list.rb index 36cd37e88..9883767c7 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -32,6 +32,8 @@ class CategoryList uncategorized = Category.new({name: SiteSetting.uncategorized_name, slug: Slug.for(SiteSetting.uncategorized_name), + color: SiteSetting.uncategorized_color, + text_color: SiteSetting.uncategorized_text_color, featured_topics: uncategorized_topics}.merge(totals)) # Find the appropriate place to insert it: diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 3e924dd89..524f996fa 100755 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -86,6 +86,8 @@ class SiteSetting < ActiveRecord::Base setting(:newuser_max_mentions_per_post, 2) setting(:uncategorized_name, 'uncategorized') + setting(:uncategorized_color, 'AB9364'); + setting(:uncategorized_text_color, 'FFFFFF'); setting(:unique_posts_mins, Rails.env.test? ? 0 : 5) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 126812512..f7ba5a66e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -735,11 +735,13 @@ en: none: '(no category)' edit: 'edit' edit_long: "Edit Category" + edit_uncategorized: "Edit Uncategorized" view: 'View Topics in Category' delete: 'Delete Category' create: 'Create Category' save: 'Save Category' creation_error: There has been an error during the creation of the category. + save_error: There was an error saving the category. more_posts: "view all {{posts}}..." name: "Category Name" description: "Description" diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 01736627c..2a95916cd 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -497,6 +497,8 @@ en: previous_visit_timeout_hours: "How long a visit lasts before we consider it the 'previous' visit, in hours" uncategorized_name: "The default category for topics that have no category in the /categories page" + uncategorized_color: "The background color of the badge for the category with topics that have no category" + uncategorized_text_color: "The text color of the badge for the category with topics that have no category" rate_limit_create_topic: "How many seconds, after creating a topic, before you can create another topic" rate_limit_create_post: "How many seconds, after creating a post, before you can create another post"