/** A data model that represents a category @class Category @extends Discourse.Model @namespace Discourse @module Discourse **/ Discourse.Category = Discourse.Model.extend({ init: function() { this._super(); this.set("availableGroups", Em.A(this.get("available_groups"))); this.set("groups", Em.A(this.groups)); }, searchContext: function() { return ({ type: 'category', id: this.get('id'), category: this }); }.property('id'), url: function() { return Discourse.getURL("/category/") + (this.get('slug')); }.property('name'), style: function() { 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; }.property('topic_count'), save: function(args) { var url = "/categories"; if (this.get('id')) { url = "/categories/" + (this.get('id')); } return Discourse.ajax(url, { data: { name: this.get('name'), color: this.get('color'), text_color: this.get('text_color'), hotness: this.get('hotness'), secure: this.get('secure'), group_names: this.get('groups').join(","), auto_close_days: this.get('auto_close_days') }, type: this.get('id') ? 'PUT' : 'POST' }); }, destroy: function(callback) { return Discourse.ajax("/categories/" + (this.get('slug') || this.get('id')), { type: 'DELETE' }); }, addGroup: function(group){ this.get("groups").addObject(group); this.get("availableGroups").removeObject(group); }, removeGroup: function(group){ this.get("groups").removeObject(group); this.get("availableGroups").addObject(group); }, // note, this is used in a data attribute, data attributes get downcased // to avoid confusion later on using this naming here. description_text: function(){ return $("