(function(){RB.RelatedObjectSelectorView=Backbone.View.extend({className:"related-object-select",_template:_.template(['<select placeholder="<%- searchPlaceholderText %>" ','        class="related-object-options"></select>','<ul class="related-object-selected"></ul>'].join("")),initialize:function(options){this.options=options;this._$input=options.$input;this._selectizeOptions=options.selectizeOptions;this._selectedIDs={}},render:function(){var self=this,i,item;this.$el.html(this._template({searchPlaceholderText:gettext("Search for users...")}));this._$selected=this.$(".related-object-selected");this.$("select").selectize(_.defaults(this._selectizeOptions,{copyClassesToDropdown:true,dropdownParent:"body",preload:"focus",render:{item:function(){return""},option:_.bind(this.renderOption,this)},load:function(query,callback){self.loadOptions(query,function(data){callback(data.filter(function(item){return!self._selectedIDs.hasOwnProperty(item.id)}))})},onChange:function(selected){if(selected){self._onItemSelected(this.options[selected],true);this.removeOption(selected)}this.clear()}}));for(i=0;i<this.options.initialOptions.length;i++){item=this.options.initialOptions[i];this._onItemSelected(item,false)}this._$input.after(this.$el);return this},_updateInput:function(){this._$input.val(_.keys(this._selectedIDs).join(","))},_onItemSelected:function(item,addToInput){var $li=$("<li>").html(this.renderOption(item)),$items=this._$selected.children(),text=$li.text(),attached=false,compareStrings=RB.RelatedObjectSelectorView.compareStrings,i,$item;$('<span class="remove-item fa fa-close">').click(_.bind(this._onItemRemoved,this,$li,item)).appendTo($li);for(i=0;i<$items.length;i++){$item=$items.eq(i);if(compareStrings($item.text(),text)>0){$item.before($li);attached=true;break}}if(!attached){$li.appendTo(this._$selected)}this._selectedIDs[item.id]=item;if(addToInput){this._updateInput()}},_onItemRemoved:function($li,item){$li.remove();delete this._selectedIDs[item.id];this._updateInput()},renderOption:function(){return""},loadOptions:function(query,callback){callback()}},{compareStrings:function(a,b){if(String.prototype.localeCompare!==undefined){return a.localeCompare(b)}else{return a===b?0:a<b?-1:1}}});RB.RelatedUserSelectorView=RB.RelatedObjectSelectorView.extend({_optionTemplate:_.template(["<div>","<% if (useGravatars && avatarURL) { %>",' <img src="<%- avatarURL %>">',"<% } %>","<% if (fullname) { %>",' <span class="title"><%- fullname %></span>',' <span class="description">(<%- username %>)</span>',"<% } else { %>",' <span class="title"><%- username %></span>',"<% } %>","</div>"].join("")),initialize:function(options){RB.RelatedObjectSelectorView.prototype.initialize.call(this,_.defaults({selectizeOptions:{searchField:["fullname","username"],sortField:[{field:"fullname"},{field:"username"}],valueField:"username"}},options));this._localSitePrefix=options.localSitePrefix||"";this._useGravatars=!!options.useGravatars},renderOption:function(item){return this._optionTemplate({useGravatars:this._useGravatars,avatarURL:item.avatar_url,fullname:item.fullname,username:item.username})},loadOptions:function(query,callback){var params={fullname:1,"only-fields":"avatar_url,fullname,id,username","only-links":""};if(query.length!==0){params.q=query}$.ajax({type:"GET",url:SITE_ROOT+this._localSitePrefix+"api/users/",data:params,success:function(results){callback(results.users)},error:function(){console.log("User query failed",arguments);callback()}})}})}).call(this);