You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
3.3 KiB

define(["jquery","core/custom_interaction_events","core/notification","core/pubsub","core/paged_content_paging_bar","core/templates","core_course/events","core_course/repository"],function(a,b,c,d,e,f,g,h){var i=10,j={CARD_CONTAINER:'[data-region="card-deck"]',COURSE_IS_FAVOURITE:'[data-region="is-favourite"]',CONTENT:'[data-region="view-content"]',EMPTY_MESSAGE:'[data-region="empty-message"]',LOADING_PLACEHOLDER:'[data-region="loading-placeholder"]',PAGING_BAR:'[data-region="paging-bar"]',PAGING_BAR_NEXT:'[data-control="next"]',PAGING_BAR_PREVIOUS:'[data-control="previous"]'},k=!1,l=[],m=null,n=null,o=0,p=1,q=function(a){a.find(j.EMPTY_MESSAGE).removeClass("hidden"),a.find(j.LOADING_PLACEHOLDER).addClass("hidden"),a.find(j.CONTENT).addClass("hidden")},r=function(a){a.find(j.CONTENT).removeClass("hidden"),a.find(j.EMPTY_MESSAGE).addClass("hidden"),a.find(j.LOADING_PLACEHOLDER).addClass("hidden")},s=function(a){var b=a.find(j.PAGING_BAR);b.css("opacity",1),b.css("visibility","visible"),b.attr("aria-hidden","false")},t=function(a){var b=a.find(j.PAGING_BAR);b.css("opacity",0),b.css("visibility","hidden"),b.attr("aria-hidden","true")},u=function(a,b){l.forEach(function(a){a.attr("data-course-id")==b&&a.find(j.COURSE_IS_FAVOURITE).removeClass("hidden")})},v=function(a,b){l.forEach(function(a){a.attr("data-course-id")==b&&a.find(j.COURSE_IS_FAVOURITE).addClass("hidden")})},w=function(b){var d=b.map(function(a){return f.render("block_recentlyaccessedcourses/course-card",a)});return a.when.apply(null,d).then(function(){var b=[];return d.forEach(function(d){d.then(function(c){b.push(a(c))})["catch"](c.exception)}),b})},x=function(a){return h.getLastAccessedCourses(a,i).then(function(a){return w(a)})},y=function(a){var b=a.find(j.CONTENT).find(j.CARD_CONTAINER),c=parseFloat(a.css("width")),d=l.length,f=0;if(n||(b.html(l[0]),n=l[0].outerWidth(!0)),p=Math.floor(c/n),o+p<d)f=o;else{var g=o+p-d;f=o-g,f=f>=0?f:0}var h=l.slice(f,f+p),i=h.reduce(function(a,b){return a+b.attr("data-course-id")},"");if(l.length>h.length?(b.addClass("justify-content-center"),b.removeClass("justify-content-start")):(b.removeClass("justify-content-center"),b.addClass("justify-content-start")),m!=i){var k=a.find(e.rootSelector);b.html(h),m=i,p>=l.length?t(a):(s(a),0===o?e.disablePreviousControlButtons(k):e.enablePreviousControlButtons(k),o+p>=l.length?e.disableNextControlButtons(k):e.enableNextControlButtons(k))}},z=function(c){var e=null,f=!1;d.subscribe(g.favourited,function(a){u(c,a)}),d.subscribe(g.unfavorited,function(a){v(c,a)}),d.subscribe("nav-drawer-toggle-start",function(){if(k&&l.length&&!f){f=!0;var a=0,b=function(){setTimeout(function(){y(c),a++,a<5&&f&&b()},100)};b(c)}}),d.subscribe("nav-drawer-toggle-end",function(){f=!1}),a(window).on("resize",function(){k&&l.length&&(e||(e=setTimeout(function(){e=null,y(c)},66)))}),b.define(c,[b.events.activate]),c.on(b.events.activate,j.PAGING_BAR_NEXT,function(b,d){var e=a(b.target).closest(j.PAGING_BAR_NEXT);e.hasClass("disabled")||(o+=p,y(c)),d.originalEvent.preventDefault()}),c.on(b.events.activate,j.PAGING_BAR_PREVIOUS,function(b,d){var e=a(b.target).closest(j.PAGING_BAR_PREVIOUS);e.hasClass("disabled")||(o-=p,o=o<0?0:o,y(c)),d.originalEvent.preventDefault()})},A=function(b,d){d=a(d),z(d),x(b).then(function(a){l=a,k=!0,l.length?(r(d),y(d)):q(d)})["catch"](c.exception)};return{init:A}});