MDL-63880 core_course: Move common templates to core_course
[moodle.git] / blocks / myoverview / amd / build / view.min.js
1 define(["jquery","block_myoverview/repository","core/paged_content_factory","core/pubsub","core/custom_interaction_events","core/notification","core/templates","core_course/events","block_myoverview/selectors","core/paged_content_events"],function(a,b,c,d,e,f,g,h,i,j){var k={COURSE_REGION:'[data-region="course-view-content"]',ACTION_HIDE_COURSE:'[data-action="hide-course"]',ACTION_SHOW_COURSE:'[data-action="show-course"]',ACTION_ADD_FAVOURITE:'[data-action="add-favourite"]',ACTION_REMOVE_FAVOURITE:'[data-action="remove-favourite"]',FAVOURITE_ICON:'[data-region="favourite-icon"]',ICON_IS_FAVOURITE:'[data-region="is-favourite"]',ICON_NOT_FAVOURITE:'[data-region="not-favourite"]',PAGED_CONTENT_CONTAINER:'[data-region="page-container"]'},l={COURSES_CARDS:"block_myoverview/view-cards",COURSES_LIST:"block_myoverview/view-list",COURSES_SUMMARY:"block_myoverview/view-summary",NOCOURSES:"core_course/no-courses"},m=[12,24,48],n=[],o=0,p=0,q=0,r=null,s=function(a){var b=a.find(i.courseView.region);return{display:b.attr("data-display"),grouping:b.attr("data-grouping"),sort:b.attr("data-sort")}},t={ignoreControlWhileLoading:!0,controlPlacementBottom:!0,persistentLimitKey:"block_myoverview_user_paging_preference"},u=function(a,c){return b.getEnrolledCoursesByTimeline({offset:o,limit:c,classification:a.grouping,sort:a.sort})},v=function(a,b){return a.find(k.FAVOURITE_ICON+'[data-course-id="'+b+'"]')},w=function(a,b){return a.find('[data-region="paged-content-page"][data-page="'+b+'"]')},x=function(a){return a.attr("data-course-id")},y=function(a,b){var c=v(a,b),d=c.find(k.ICON_IS_FAVOURITE);d.addClass("hidden"),d.attr("aria-hidden",!0);var e=c.find(k.ICON_NOT_FAVOURITE);e.removeClass("hidden"),e.attr("aria-hidden",!1)},z=function(a,b){var c=v(a,b),d=c.find(k.ICON_IS_FAVOURITE);d.removeClass("hidden"),d.attr("aria-hidden",!1);var e=c.find(k.ICON_NOT_FAVOURITE);e.addClass("hidden"),e.attr("aria-hidden",!0)},A=function(a,b){return a.find('[data-action="add-favourite"][data-course-id="'+b+'"]')},B=function(a,b){return a.find('[data-action="remove-favourite"][data-course-id="'+b+'"]')},C=function(a,b){var c=B(a,b),e=A(a,b);F(b,!0).then(function(g){g?(d.publish(h.favourited),c.removeClass("hidden"),e.addClass("hidden"),z(a,b)):f.alert("Starring course failed","Could not change favourite state")})["catch"](f.exception)},D=function(a,b){var c=B(a,b),e=A(a,b);F(b,!1).then(function(g){g?(d.publish(h.unfavorited),c.addClass("hidden"),e.removeClass("hidden"),y(a,b)):f.alert("Starring course failed","Could not change favourite state")})["catch"](f.exception)},E=function(b,d){var e=x(d),h=b.find('[data-region="paging-bar"]'),i=parseInt(h.attr("data-active-page-number")),j=n[i],k=j.courses.reduce(function(a,b){return e!=b.id&&a.push(b),a},[]);if(void 0!=n[i+1]){var l=n[i+1].courses.slice(0,1);n.forEach(function(b,c){if(c>i){var d=[];void 0!=n[c+1]&&(d=n[c+1].courses.slice(0,1)),n[c].courses=a.merge(n[c].courses.slice(1),d)}}),k=a.merge(k,l)}if(p==i+1&&0==n[i+1].courses.length){var m=b.find('[data-region="paged-content-container"]');c.resetLastPageNumber(a(m).attr("id"),i)}n[i].courses=k,o--;var q=w(b,i);G(b,n[i]).then(function(a,b){return g.replaceNodeContents(q,a,b)})["catch"](f.exception),n.forEach(function(a,c){if(c>i){var d=w(b,c);d.remove()}})},F=function(a,c){return b.setFavouriteCourses({courses:[{id:a,favourite:c}]}).then(function(b){return 0==b.warnings.length&&(n.forEach(function(b){b.courses.forEach(function(d,e){d.id==a&&(b.courses[e].isfavourite=c)})}),!0)})["catch"](f.exception)},G=function(a,b){var c=s(a),d="";if(d="cards"==c.display?l.COURSES_CARDS:"list"==c.display?l.COURSES_LIST:l.COURSES_SUMMARY,b.courses.length)return g.render(d,{courses:b.courses});var e=a.find(i.courseView.region).attr("data-nocoursesimg");return g.render(l.NOCOURSES,{nocoursesimg:e})},H=function(a){this.find(i.courseView.region).attr("data-paging",a)},I=function(a,b){var c=b+j.SET_ITEMS_PER_PAGE_LIMIT;d.subscribe(c,H.bind(a))},J=function(b){r="block_myoverview_"+b.attr("id")+"_"+Math.random();var d=m,e=parseInt(b.find(i.courseView.region).attr("data-paging"),10);e&&(d=m.map(function(a){var b=!1;return a==e&&(b=!0),{value:a,active:b}}));var h=s(b),j=a.extend({},t);j.eventNamespace=r;var k=c.createWithLimit(d,function(c,d){var e=[];return c.forEach(function(c){var g=c.pageNumber,i=c.limit;if(q!=i&&(n=[],o=0,p=0),p==g)return d.allItemsLoaded(p),void e.push(G(b,n[g]));q=i,void 0==n[g+1]&&void 0==n[g]&&(i*=2);var j=u(h,i).then(function(e){var f=e.courses,h=0,i=[];if(void 0!=n[g]){i=n[g].courses;var j=i.length;j<c.limit&&(h=c.limit-j,i=a.merge(n[g].courses,f.slice(0,h)))}else h=c.limit,i=f.slice(0,c.limit);n[g]={courses:i};var k=f.slice(h,f.length);return k.length&&(n[g+1]={courses:k}),n[g].courses.length<c.limit?(p=g,d.allItemsLoaded(g)):void 0!=n[g+1]&&n[g+1].courses.length<c.limit&&(p=g+1),o=e.nextoffset,G(b,n[g])})["catch"](f.exception);e.push(j)}),e},j);k.then(function(a,c){return I(b,r),g.replaceNodeContents(b.find(i.courseView.region),a,c)})["catch"](f.exception)},K=function(c){e.define(c,[e.events.activate]),c.on(e.events.activate,k.ACTION_ADD_FAVOURITE,function(b,d){var e=a(b.target).closest(k.ACTION_ADD_FAVOURITE),f=x(e);C(c,f),d.originalEvent.preventDefault()}),c.on(e.events.activate,k.ACTION_REMOVE_FAVOURITE,function(b,d){var e=a(b.target).closest(k.ACTION_REMOVE_FAVOURITE),f=x(e);D(c,f),d.originalEvent.preventDefault()}),c.on(e.events.activate,k.FAVOURITE_ICON,function(a,b){b.originalEvent.preventDefault()}),c.on(e.events.activate,k.ACTION_HIDE_COURSE,function(d,e){var f=a(d.target).closest(k.ACTION_HIDE_COURSE),g=x(f),h={preferences:[{type:"block_myoverview_hidden_course_"+g,value:!0}]};b.updateUserPreferences(h),E(c,f),e.originalEvent.preventDefault()}),c.on(e.events.activate,k.ACTION_SHOW_COURSE,function(d,e){var f=a(d.target).closest(k.ACTION_SHOW_COURSE),g=x(f),h={preferences:[{type:"block_myoverview_hidden_course_"+g,value:null}]};b.updateUserPreferences(h),E(c,f),e.originalEvent.preventDefault()})},L=function(b){b=a(b),n=[],p=0,o=0,J(b),b.attr("data-init")||(K(b),b.attr("data-init",!0))},M=function(a){n.length>0?n.forEach(function(b,c){var d=w(a,c);G(a,b).then(function(a,b){return g.replaceNodeContents(d,a,b)})["catch"](f.exception)}):L(a)};return{init:L,reset:M}});