(function(){"use strict";RB.RelatedObjectSelectorView=Backbone.View.extend({className:"related-object-select",searchPlaceholderText:"",template:_.template('<select placeholder="<%- searchPlaceholderText %>"\n        class="related-object-options"></select>\n<% if (multivalued) { %>\n<ul class="related-object-selected"></ul>\n<% } %>'),initialize:function initialize(options){this.options=options;this._$input=options.$input;this._selectizeOptions=options.selectizeOptions;this._selectedIDs=new Map;_.bindAll(this,"renderOption")},render:function render(){var _this=this;var self=this;this.$el.html(this.template({searchPlaceholderText:this.searchPlaceholderText,multivalued:this.options.multivalued}));this._$selected=this.$(".related-object-selected");var renderItem=this.options.multivalued?function(){return""}:this.renderOption;var selectizeOptions=_.defaults(this._selectizeOptions,{copyClassesToDropdown:true,dropdownParent:"body",preload:"focus",render:{item:renderItem,option:this.renderOption},load:function load(query,callback){self.loadOptions(query,function(data){return callback(data.filter(function(item){return!self._selectedIDs.has(item.id)}))})},onChange:function onChange(selected){if(selected){self._onItemSelected(this.options[selected],true);if(self.options.multivalued){this.removeOption(selected)}}if(self.options.multivalued){this.clear()}}});if(!this.options.multivalued&&this.options.initialOptions.length){var item=this.options.initialOptions[0];selectizeOptions.options=this.options.initialOptions;selectizeOptions.items=[item[selectizeOptions.valueField]]}this.$("select").selectize(selectizeOptions);if(this.options.multivalued){this.options.initialOptions.forEach(function(item){return _this._onItemSelected(item,false)})}this._$input.after(this.$el);return this},_updateInput:function _updateInput(){this._$input.val(Array.from(this._selectedIDs.keys()).join(","))},_onItemSelected:function _onItemSelected(item,addToInput){var _this2=this;if(this.options.multivalued){var $li=$("<li>").html(this.renderOption(item));var $items=this._$selected.children();var text=$li.text();$('<span class="remove-item fa fa-close">').click(function(){return _this2._onItemRemoved($li,item)}).appendTo($li);var attached=false;for(var i=0;i<$items.length;i++){var $item=$items.eq(i);if($item.text().localeCompare(text)>0){$item.before($li);attached=true;break}}if(!attached){$li.appendTo(this._$selected)}this._selectedIDs.set(item.id,item);if(addToInput){this._updateInput()}}else{this._selectedIDs=new Map([[item.id,item]]);this._updateInput()}},_onItemRemoved:function _onItemRemoved($li,item){$li.remove();this._selectedIDs.delete(item.id);this._updateInput()},renderOption:function renderOption(){return""},loadOptions:function loadOptions(query,callback){callback()}});"use strict";(function(){var optionTemplate=_.template('<div>\n<% if (useAvatars && avatarHTML) { %>\n <%= avatarHTML %>\n<% } %>\n<% if (fullname) { %>\n <span class="title"><%- fullname %></span>\n <span class="description">(<%- username %>)</span>\n<% } else { %>\n <span class="title"><%- username %></span>\n<% } %>\n</div>');RB.RelatedUserSelectorView=RB.RelatedObjectSelectorView.extend({searchPlaceholderText:gettext("Search for users..."),initialize:function initialize(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._useAvatars=!!options.useAvatars},renderOption:function renderOption(item){return optionTemplate(_.extend({useAvatars:this._useAvatars},item))},loadOptions:function loadOptions(query,callback){var params={fullname:1,"only-fields":"avatar_html,fullname,id,username","only-links":"","render-avatars-at":"20"};if(query.length!==0){params.q=query}$.ajax({type:"GET",url:""+SITE_ROOT+this._localSitePrefix+"api/users/",data:params,success:function success(results){callback(results.users.map(function(u){return{avatarHTML:u.avatar_html[20],fullname:u.fullname,id:u.id,username:u.username}}))},error:function error(){for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++){args[_key]=arguments[_key]}console.log("User query failed",args);callback()}})}})})()}).call(this);
