2b40f35ace08a510bcf7d31ef69897126f3a0909
[moodle.git] / message / amd / build / message_drawer_view_overview_section.min.js
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"]',MUTED_ICON_CONTAINER:'[data-region="muted-icon-container"]',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"),e.get_string("totalconversations","core_message",b).done(function(a){c.attr("aria-label",a)});var g=b>20?20:b,h=Array.apply(null,Array(g)).map(function(){return!0});f.render(o.CONVERSATIONS_LIST_ITEMS_PLACEHOLDER,{placeholders:h}).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),e.get_string("unreadconversations","core_message",b).done(function(a){c.attr("aria-label",a)}),b>0&&c.removeClass("hidden")},y=function(b){return b.map(function(b){return Object.keys(b).reduce(function(c,d){return a.isArray(b[d])?c[d.toLowerCase()]=y(b[d]):c[d.toLowerCase()]=b[d],c},{})},[])},z=function(b,c){b=y(b);var d=b.map(function(b){var d=b.messages.length?b.messages[b.messages.length-1]:null,e={id:b.id,imageurl:b.imageurl,name:b.name,subname:b.subname,unreadcount:b.unreadcount,ismuted:b.ismuted,lastmessagedate:d?d.timecreated:null,sentfromcurrentuser:d?d.useridfrom==c:null,lastmessage:d?a(d.text).text()||d.text:null},f=null;return b.type==m.CONVERSATION_TYPES.SELF?f=b.members[0]:b.type==m.CONVERSATION_TYPES.PRIVATE&&(f=b.members.reduce(function(a,b){return a||b.id==c||(a=b),a},null)),null!==f&&(e.userid=f.id,e.showonlinestatus=f.showonlinestatus,e.isonline=f.isonline,e.isblocked=f.isblocked),b.type==m.CONVERSATION_TYPES.PUBLIC&&(e.lastsendername=b.members.reduce(function(a,b){return a||b.id!=d.useridfrom||(a=b.fullname),a},null)),e});return d.forEach(function(a){(new Date).toDateString()==new Date(1e3*a.lastmessagedate).toDateString()&&(a.istoday=!0)}),f.render(o.CONVERSATIONS_LIST,{conversations:d})},A=function(a,b,d){var e=null,f=!0;if(a&&a.length){var g=a.filter(function(a){return a!=m.CONVERSATION_TYPES.SELF});f=a.length!=g.length,e=g[0]}return function(a,g){return h.getConversations(g,e,p+1,d,b,f).then(function(b){var c=b.conversations;return c.length>p?c=c.slice(0,-1):l.setLoadedAll(a,!0),d+=p,c.forEach(function(a){q[a.id]=a}),c})["catch"](c.exception)}},B=function(a){return a.find(n.SECTION_TOTAL_COUNT)},C=function(a){return a.find(n.SECTION_UNREAD_COUNT)},D=function(a){if(r){var b=B(a),c=parseInt(b.text());c+=1,b.text(c)}},E=function(a){if(r){var b=B(a),c=parseInt(b.text());c-=1,b.text(c)}},F=function(a){if(s){var b=C(a),c=parseInt(b.text());c-=1,b.text(c),c<1&&b.addClass("hidden")}},G=function(a,b){return a.find('[data-conversation-id="'+b+'"]')},H=function(a,b){return a.find('[data-user-id="'+b+'"]')},I=function(a){a.find(n.MUTED_ICON_CONTAINER).removeClass("hidden")},J=function(a){a.find(n.MUTED_ICON_CONTAINER).addClass("hidden")},K=function(a){a.find(n.BLOCKED_ICON_CONTAINER).removeClass("hidden")},L=function(a){a.find(n.BLOCKED_ICON_CONTAINER).addClass("hidden")},M=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,d.messages[i-1].useridfrom=j.userFrom.id);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]=y([d])[0],(new Date).toDateString()==new Date(1e3*k.lastmessagedate).toDateString()&&(k.istoday=!0),f.render(o.CONVERSATIONS_LIST,{conversations:[k]}).then(function(a){var c=l.getContentContainer(b);return c.prepend(a)}).then(function(){return D(b)})["catch"](c.exception)},N=function(a,b){b.remove(),E(a);var c=a.find(n.CONVERSATION);if(!c.length){var d=l.getRoot(a);l.hideContent(d),l.showEmptyMessage(d)}},O=function(a,b){var c=b.find(n.UNREAD_COUNT);c.text("0"),c.addClass("hidden"),F(a)},P=function(e,f,g,h,m,o){var p=l.getRoot(f),r=function(a){var b=parseInt(a.type,10);return!(h&&h.indexOf(b)<0||m&&!a.isFavourite||!m&&a.isFavourite)},s=f.find(n.TOGGLE);f.css("min-height",s.outerHeight()),f.on("show.bs.collapse",function(){u(f),l.show(p,g,function(a,b,c){return z(b,c).then(function(b){return a.append(b),b})})}),f.on("hidden.bs.collapse",function(){v(f)}),d.subscribe(i.CONTACT_BLOCKED,function(a){var b=H(f,a);b.length&&K(b)}),d.subscribe(i.CONTACT_UNBLOCKED,function(a){var b=H(f,a);b.length&&L(b)}),d.subscribe(i.CONVERSATION_SET_MUTED,function(a){var b=a.id,c=G(f,b);c.length&&I(c)}),d.subscribe(i.CONVERSATION_UNSET_MUTED,function(a){var b=a.id,c=G(f,b);c.length&&J(c)}),d.subscribe(i.CONVERSATION_NEW_LAST_MESSAGE,function(a){if(r(a)){var b=a.id,d=G(f,b);if(d.length){var e=l.getContentContainer(f);z([a],a.loggedInUserId).then(function(a){return e.prepend(a),d.remove(),a})["catch"](c.exception)}else M(f,a)}}),d.subscribe(i.CONVERSATION_DELETED,function(a){var b=G(f,a);delete q[a],b.length&&N(f,b)}),d.subscribe(i.CONVERSATION_READ,function(a){var b=G(f,a);b.length&&O(f,b)}),d.subscribe(i.CONVERSATION_SET_FAVOURITE,function(a){var b=null;r(a)?(b=G(f,a.id),b.length||M(f,a)):(b=G(f,a.id),b.length&&N(f,b))}),d.subscribe(i.CONVERSATION_UNSET_FAVOURITE,function(a){var b=null;r(a)?(b=G(f,a.id),b.length||M(f,a)):(b=G(f,a.id),b.length&&N(f,b))}),b.define(f,[b.events.activate]),f.on(b.events.activate,n.CONVERSATION,function(b,c){var d=a(b.target).closest(n.CONVERSATION),f=d.attr("data-conversation-id"),g=q[f];j.go(e,k.VIEW_CONVERSATION,g,o),c.originalEvent.preventDefault()})},Q=function(b,c,d,e,f,g,h,i,j){var k=a(d);if(!k.attr("data-init")){var m=A(f,g,0);if(P(b,k,m,f,g,j),t(k)){u(k);var n=l.getRoot(k);l.show(n,m,function(a,b,c){return z(b,c).then(function(b){return a.append(b),b})})}h.then(function(a){w(k,a),r=!0})["catch"](function(){}),i.then(function(a){x(k,a),s=!0})["catch"](function(){}),k.attr("data-init",!0)}};return{show:Q,isVisible:t}});