Created
April 29, 2014 16:04
-
-
Save Piioo/11404643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Template: | |
<div> | |
{{#if category}} | |
<div class="item"> | |
{{categoryBadge category}} | |
<i {{action removeCategory}} class="fa fa-times"></i> | |
</div> | |
{{else}} | |
<input class='interests-group auto-complete{{unbound view.index}}' type='text'> | |
{{/if}} | |
<div> | |
Component: | |
Discourse.InterestsGroupComponent = Ember.Component.extend({ | |
layoutName: 'components/preferences/interests-group', | |
tags: Em.computed.alias('category.tags'), | |
init: function(){ | |
this._super(); | |
var self = this; | |
self.set('category', this.get('interests')[this.get('index')]); | |
}, | |
didInsertElement: function(){ | |
var self = this; | |
var category = this.get('category'); | |
var item = Em.A(); | |
if(category) { | |
item.addObject(category); | |
} | |
self.$('input.auto-complete'+self.get('index')).autocomplete({ | |
items: item, | |
single: true, | |
allowAny: false, | |
dataSource: function(term){ | |
return Discourse.Category.list().filter(function(category){ | |
var regex = new RegExp(term, "i"); | |
return category.get("name").match(regex) && | |
!_.contains(self.get('blacklist'), category) ; | |
}); | |
}, | |
onChangeItems: function(items) { | |
var link = _.first(items); | |
if(!link){ | |
self.set("category.tags", Em.A()); | |
self.set("category", undefined); | |
return; | |
} | |
var slug = link.match(/href=['"]\/category\/([^'"]+)/)[1]; | |
var category = Discourse.Category.findSingleBySlug(slug); | |
if(!self.get('category') || category.id != self.get('category').id) { | |
self.set("category", category); | |
if (!self.get('category')) { | |
self.set('category.tags', Em.A()); | |
} | |
} | |
}, | |
template: Discourse.InterestsGroupComponent.templateFunction(), | |
transformComplete: function(category) { | |
return Discourse.HTML.categoryBadge(category, {allowUncategorized: false}); | |
} | |
}); | |
}, | |
actions: { | |
removeTag: function(toRm){ | |
this.get("tags").removeObject(toRm.toString()); | |
}, | |
removeCategory: function(){ | |
this.set('category.tags', Em.A()); | |
this.set("category", undefined); | |
} | |
} | |
}); | |
Discourse.InterestsGroupComponent.reopenClass({ | |
templateFunction: function(){ | |
this.compiled = this.compiled || Handlebars.compile("<div class='autocomplete'>" + | |
"<ul>" + | |
"{{#each options}}" + | |
"<li>" + | |
"{{categoryLinkRaw this allowUncategorized=false}}" + | |
"</li>" + | |
"{{/each}}" + | |
"</ul>" + | |
"</div>"); | |
return this.compiled; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment