MDL-56431 messaging: Added aria label to filter.
[moodle.git] / message / amd / build / message_area_search.min.js
1 define(["jquery","core/ajax","core/templates","core/notification","core/str","core/custom_interaction_events","core_message/message_area_events"],function(a,b,c,d,e,f,g){function h(a){this.messageArea=a,this._init()}var i={CONTACTS:"[data-region='contacts'][data-region-content='contacts']",CONTACTSAREA:"[data-region='contacts-area']",CONVERSATIONS:"[data-region='contacts'][data-region-content='conversations']",DELETESEARCHFILTER:"[data-region='search-filter-area']",LOADINGICON:".loading-icon",SEARCHBOX:"[data-region='search-box']",SEARCHFILTER:"[data-region='search-filter']",SEARCHFILTERAREA:"[data-region='search-filter-area']",SEARCHRESULTSAREA:"[data-region='search-results-area']",SEARCHTEXTAREA:"[data-region='search-text-area']",SEARCHUSERSINCOURSE:"[data-action='search-users-in-course']"};return h.prototype.messageArea=null,h.prototype._searchArea=null,h.prototype._courseid=null,h.prototype._isLoading=!1,h.prototype._numMessagesDisplayed=0,h.prototype._numMessagesToRetrieve=20,h.prototype._numUsersDisplayed=0,h.prototype._numUsersToRetrieve=20,h.prototype._searchAreas={MESSAGES:"messages",USERS:"users",USERSINCOURSE:"usersincourse"},h.prototype._requestTimeout=null,h.prototype._init=function(){this.messageArea.find(i.SEARCHTEXTAREA).on("input",this._searchRequest.bind(this)),this.messageArea.onDelegateEvent(f.events.activate,i.SEARCHUSERSINCOURSE,function(b){this._setFilter(a(b.currentTarget).html()),this._setPlaceholderText("searchforuser"),this._clearSearchArea(),this._searchArea=this._searchAreas.USERSINCOURSE,this._courseid=a(b.currentTarget).data("courseid"),this._searchUsersInCourse(),this.messageArea.find(i.SEARCHBOX).focus()}.bind(this)),this.messageArea.onDelegateEvent(f.events.activate,i.DELETESEARCHFILTER,function(){this._hideSearchResults(),this._searchArea=this._searchAreas.USERS,this._setPlaceholderText("searchforuserorcourse"),this.messageArea.trigger(g.USERSSEARCHCANCELED),this.messageArea.find(i.SEARCHBOX).focus()}.bind(this)),this.messageArea.onCustomEvent(g.CONVERSATIONSSELECTED,function(){this._hideSearchResults(),this._searchArea=this._searchAreas.MESSAGES,this._setPlaceholderText("searchmessages")}.bind(this)),this.messageArea.onCustomEvent(g.CONTACTSSELECTED,function(){this._hideSearchResults(),this._searchArea=this._searchAreas.USERS,this._setPlaceholderText("searchforuserorcourse")}.bind(this)),this.messageArea.onCustomEvent(g.MESSAGESENT,function(){this._hideSearchResults(),this._searchArea=this._searchAreas.MESSAGES,this._setPlaceholderText("searchmessages")}.bind(this)),f.define(this.messageArea.find(i.SEARCHRESULTSAREA),[f.events.scrollBottom]),this.messageArea.onDelegateEvent(f.events.scrollBottom,i.SEARCHRESULTSAREA,function(){this._searchArea==this._searchAreas.MESSAGES?this._searchMessages():this._searchArea==this._searchAreas.USERSINCOURSE&&this._searchUsersInCourse()}.bind(this)),this._searchArea=this.messageArea.showContactsFirst()?this._searchAreas.USERS:this._searchAreas.MESSAGES},h.prototype._searchRequest=function(){var a=this.messageArea.find(i.SEARCHTEXTAREA+" input").val();return this._requestTimeout&&clearTimeout(this._requestTimeout),""===a.trim()?void(this._searchArea==this._searchAreas.MESSAGES?(this._hideSearchResults(),this.messageArea.trigger(g.MESSAGESEARCHCANCELED)):this._searchArea==this._searchAreas.USERS?(this._hideSearchResults(),this.messageArea.trigger(g.USERSSEARCHCANCELED)):this._searchArea==this._searchAreas.USERSINCOURSE&&(this._clearSearchArea(),this._searchUsersInCourse())):(this.messageArea.find(i.CONVERSATIONS).hide(),this.messageArea.find(i.CONTACTS).hide(),this.messageArea.find(i.SEARCHRESULTSAREA).show(),void(this._searchArea==this._searchAreas.MESSAGES?this._requestTimeout=setTimeout(function(){this._clearSearchArea(),this._numMessagesDisplayed=0,this._searchMessages()}.bind(this),300):this._searchArea==this._searchAreas.USERSINCOURSE?this._requestTimeout=setTimeout(function(){this._clearSearchArea(),this._numUsersDisplayed=0,this._searchUsersInCourse()}.bind(this),300):this._requestTimeout=setTimeout(function(){this._clearSearchArea(),this._numUsersDisplayed=0,this._searchUsers()}.bind(this),300)))},h.prototype._searchMessages=function(){if(this._isLoading)return!1;var a=this.messageArea.find(i.SEARCHBOX).val();this._isLoading=!0;var e=b.call([{methodname:"core_message_data_for_messagearea_search_messages",args:{userid:this.messageArea.getCurrentUserId(),search:a,limitfrom:this._numMessagesDisplayed,limitnum:this._numMessagesToRetrieve}}]),f=0;return c.render("core/loading",{}).then(function(a,b){return c.appendNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),"<div style='text-align:center'>"+a+"</div>",b),e[0]}.bind(this)).then(function(a){return f=a.contacts.length,c.render("core_message/message_area_message_search_results",a)}).then(function(a,b){this.messageArea.find(i.SEARCHRESULTSAREA+" "+i.LOADINGICON).remove(),f>0?(c.appendNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),a,b),this._numMessagesDisplayed+=f):0==this._numMessagesDisplayed&&c.replaceNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),a,b),this._isLoading=!1}.bind(this)).fail(d.exception)},h.prototype._searchUsers=function(){var a=this.messageArea.find(i.SEARCHBOX).val(),e=b.call([{methodname:"core_message_data_for_messagearea_search_users",args:{userid:this.messageArea.getCurrentUserId(),search:a,limitnum:this._numUsersToRetrieve}}]);return c.render("core/loading",{}).then(function(a,b){return c.replaceNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),"<div style='text-align:center'>"+a+"</div>",b),e[0]}.bind(this)).then(function(a){return a.contacts.length>0&&(a.hascontacts=!0),a.courses.length>0&&(a.hascourses=!0),a.noncontacts.length>0&&(a.hasnoncontacts=!0),c.render("core_message/message_area_user_search_results",a)}).then(function(a,b){c.replaceNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),a,b)}.bind(this)).fail(d.exception)},h.prototype._searchUsersInCourse=function(){if(this._isLoading)return!1;var a=this.messageArea.find(i.SEARCHBOX).val();this._isLoading=!0;var e=b.call([{methodname:"core_message_data_for_messagearea_search_users_in_course",args:{userid:this.messageArea.getCurrentUserId(),courseid:this._courseid,search:a,limitfrom:this._numUsersDisplayed,limitnum:this._numUsersToRetrieve}}]),f=0;return c.render("core/loading",{}).then(function(a,b){return c.appendNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),"<div style='text-align:center'>"+a+"</div>",b),e[0]}.bind(this)).then(function(a){return f=a.contacts.length,f>0&&(a.hascontacts=!0),c.render("core_message/message_area_user_search_results",a)}).then(function(a,b){this.messageArea.find(i.SEARCHRESULTSAREA+" "+i.LOADINGICON).remove(),f>0?(c.appendNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),a,b),this._numUsersDisplayed+=f):0==this._numUsersDisplayed&&c.replaceNodeContents(this.messageArea.find(i.SEARCHRESULTSAREA),a,b),this._isLoading=!1}.bind(this)).fail(d.exception)},h.prototype._setPlaceholderText=function(a){return e.get_string(a,"message").then(function(a){this.messageArea.find(i.SEARCHTEXTAREA+" input").attr("placeholder",a)}.bind(this))},h.prototype._setFilter=function(a){this.messageArea.find(i.SEARCHBOX).val(""),this.messageArea.find(i.CONTACTSAREA).addClass("searchfilter"),this.messageArea.find(i.SEARCHFILTERAREA).show(),this.messageArea.find(i.SEARCHFILTER).html(a),e.get_string("removecoursefilter","message",a).then(function(a){this.messageArea.find(i.SEARCHFILTERAREA).attr("aria-label",a)}.bind(this))},h.prototype._clearFilters=function(){this.messageArea.find(i.CONTACTSAREA).removeClass("searchfilter"),this.messageArea.find(i.SEARCHFILTER).empty(),this.messageArea.find(i.SEARCHFILTERAREA).hide(),this.messageArea.find(i.SEARCHFILTERAREA).removeAttr("aria-label")},h.prototype._clearSearchArea=function(){this.messageArea.find(i.SEARCHRESULTSAREA).empty()},h.prototype._hideSearchResults=function(){this._clearFilters(),this.messageArea.find(i.SEARCHTEXTAREA+" input").val(""),this._clearSearchArea(),this.messageArea.find(i.SEARCHRESULTSAREA).hide()},h});