MDL-64139 message: load conversation counts async
[moodle.git] / message / amd / build / message_drawer_view_overview_section.min.js
blob779e7d6e8d3e9b5cd4e9e18914f3b25c2306f2e7
1 define(["jquery","core/custom_interaction_events","core/notification","core/pubsub","core/str","core/templates","core/user_date","core_message/message_repository","core_message/message_drawer_events","core_message/message_drawer_router","core_message/message_drawer_routes","core_message/message_drawer_lazy_load_list","core_message/message_drawer_view_conversation_constants"],function(a,b,c,d,e,f,g,h,i,j,k,l,m){var n={TOGGLE:'[data-region="toggle"]',CONVERSATION:"[data-conversation-id]",BLOCKED_ICON_CONTAINER:'[data-region="contact-icon-blocked"]',LAST_MESSAGE:'[data-region="last-message"]',LAST_MESSAGE_DATE:'[data-region="last-message-date"]',UNREAD_COUNT:'[data-region="unread-count"]',SECTION_TOTAL_COUNT:'[data-region="section-total-count"]',SECTION_TOTAL_COUNT_CONTAINER:'[data-region="section-total-count-container"]',SECTION_UNREAD_COUNT:'[data-region="section-unread-count"]',PLACEHOLDER_CONTAINER:'[data-region="placeholder-container"]'},o={CONVERSATIONS_LIST:"core_message/message_drawer_conversations_list",CONVERSATIONS_LIST_ITEMS_PLACEHOLDER:"core_message/message_drawer_conversations_list_items_placeholder"},p=50,q={},r=!1,s=!1,t=function(a){return l.getRoot(a).hasClass("show")},u=function(a){a.addClass("expanded")},v=function(a){a.removeClass("expanded")},w=function(a,b){var c=a.find(n.SECTION_TOTAL_COUNT_CONTAINER),d=c.find(n.SECTION_TOTAL_COUNT);d.text(b),c.removeClass("hidden");var e=b>20?20:b,g=Array.apply(null,Array(e)).map(function(){return!0});f.render(o.CONVERSATIONS_LIST_ITEMS_PLACEHOLDER,{placeholders:g}).then(function(b){var c=a.find(n.PLACEHOLDER_CONTAINER);c.html(b)})["catch"](function(){})},x=function(a,b){var c=a.find(n.SECTION_UNREAD_COUNT);c.text(b),b>0&&c.removeClass("hidden")},y=function(b,d,e){var g=d.map(function(b){var c=b.messages.length?b.messages[b.messages.length-1]:null,d={id:b.id,imageurl:b.imageurl,name:b.name,subname:b.subname,unreadcount:b.unreadcount,lastmessagedate:c?c.timecreated:null,sentfromcurrentuser:c?c.useridfrom==e:null,lastmessage:c?a(c.text).text()||c.text:null};if(b.type==m.CONVERSATION_TYPES.PRIVATE){var f=b.members.reduce(function(a,b){return a||b.id==e||(a=b),a},null);d.userid=f.id,d.showonlinestatus=f.showonlinestatus,d.isonline=f.isonline,d.isblocked=f.isblocked}return d});return f.render(o.CONVERSATIONS_LIST,{conversations:g}).then(function(a){return b.append(a),a})["catch"](c.exception)},z=function(a,b,d){return function(e,f){return h.getConversations(f,a,p+1,d,b).then(function(a){var b=a.conversations;return b.length>p?b=b.slice(0,-1):l.setLoadedAll(e,!0),d+=p,b.forEach(function(a){q[a.id]=a}),b})["catch"](c.exception)}},A=function(a){return a.find(n.SECTION_TOTAL_COUNT)},B=function(a){return a.find(n.SECTION_UNREAD_COUNT)},C=function(a){if(r){var b=A(a),c=parseInt(b.text());c+=1,b.text(c)}},D=function(a){if(r){var b=A(a),c=parseInt(b.text());c-=1,b.text(c)}},E=function(a){if(s){var b=B(a),c=parseInt(b.text());c-=1,b.text(c),c<1&&b.addClass("hidden")}},F=function(a,b){return a.find('[data-conversation-id="'+b+'"]')},G=function(a,b){return a.find('[data-user-id="'+b+'"]')},H=function(a){a.find(n.BLOCKED_ICON_CONTAINER).removeClass("hidden")},I=function(a){a.find(n.BLOCKED_ICON_CONTAINER).addClass("hidden")},J=function(b,c){var d=c.messages[c.messages.length-1],f="",h=[{key:"you",component:"core_message"},{key:"strftimetime24",component:"core_langconfig"}];return e.get_strings(h).then(function(a){return f=a[0],g.get([{timestamp:d.timeCreated,format:a[1]}])}).then(function(a){return a[0]}).then(function(c){var e=a(d.text).text();return d.fromLoggedInUser&&(e=f+" "+e),b.find(n.LAST_MESSAGE).html(e),b.find(n.LAST_MESSAGE_DATE).text(c).removeClass("hidden"),c})},K=function(b,d){var e=b.find(n.CONVERSATION),g="";if(!e.length){var h=l.getRoot(b);l.showContent(h),l.hideEmptyMessage(h)}var i=d.messages.length,j=i?d.messages[i-1]:null;j&&(g=a(j.text).text()||j.text);var k={id:d.id,name:d.name,subname:d.subname,lastmessagedate:j?j.timeCreated:null,sentfromcurrentuser:j?j.fromLoggedInUser:null,lastmessage:g,imageurl:d.imageUrl};return q[d.id]=d,f.render(o.CONVERSATIONS_LIST,{conversations:[k]}).then(function(a){var c=l.getContentContainer(b);return c.prepend(a)}).then(function(){return C(b)})["catch"](c.exception)},L=function(a,b){b.remove(),D(a);var c=a.find(n.CONVERSATION);if(!c.length){var d=l.getRoot(a);l.hideContent(d),l.showEmptyMessage(d)}},M=function(a,b){var c=b.find(n.UNREAD_COUNT);c.text("0"),c.addClass("hidden"),E(a)},N=function(c,e,f,g){var h=l.getRoot(c),m=c.find(n.TOGGLE);c.css("min-height",m.outerHeight()),c.on("show.bs.collapse",function(){u(c),l.show(h,e,y)}),c.on("hidden.bs.collapse",function(){v(c)}),d.subscribe(i.CONTACT_BLOCKED,function(a){var b=G(c,a);b.length&&H(b)}),d.subscribe(i.CONTACT_UNBLOCKED,function(a){var b=G(c,a);b.length&&I(b)}),d.subscribe(i.CONVERSATION_NEW_LAST_MESSAGE,function(a){if(!(f&&a.type!=f||g&&!a.isFavourite||!g&&a.isFavourite)){var b=a.id,d=F(c,b);d.length?J(d,a):K(c,a)}}),d.subscribe(i.CONVERSATION_DELETED,function(a){var b=F(c,a);b.length&&L(c,b)}),d.subscribe(i.CONVERSATION_READ,function(a){var b=F(c,a);b.length&&M(c,b)}),d.subscribe(i.CONVERSATION_SET_FAVOURITE,function(a){var b=null;!g||f&&f!=a.type?f==a.type&&(b=F(c,a.id),b.length&&L(c,b)):(b=F(c,a.id),b.length||K(c,a))}),d.subscribe(i.CONVERSATION_UNSET_FAVOURITE,function(a){var b=null;g?(b=F(c,a.id),b.length&&L(c,b)):f==a.type&&(b=F(c,a.id),b.length||K(c,a))}),b.define(c,[b.events.activate]),c.on(b.events.activate,n.CONVERSATION,function(b,c){var d=a(b.target).closest(n.CONVERSATION),e=d.attr("data-conversation-id"),f=q[e];j.go(k.VIEW_CONVERSATION,f),c.originalEvent.preventDefault()})},O=function(b,c,d,e,f){if(b=a(b),!b.attr("data-init")){var g=z(c,d,0);if(N(b,g,c,d),t(b)){u(b);var h=l.getRoot(b);l.show(h,g,y)}e.then(function(a){w(b,a),r=!0})["catch"](function(){}),f.then(function(a){x(b,a),r=!0})["catch"](function(){}),b.attr("data-init",!0)}};return{show:O,isVisible:t}});