on-demand release 4.0dev+
[moodle.git] / blocks / recentlyaccessedcourses / amd / build / main.min.js
1 define ("block_recentlyaccessedcourses/main",["jquery","core/custom_interaction_events","core/notification","core/pubsub","core/paged_content_paging_bar","core/templates","core_course/events","core_course/repository","core/aria"],function(a,b,c,d,e,f,g,h,i){var j={BLOCK_CONTAINER:"[data-region=\"recentlyaccessedcourses\"]",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");i.unhide(b)},t=function(a){var b=a.find(j.PAGING_BAR);b.css("opacity",0);b.css("visibility","hidden");i.hide(b)},u=function(a,b){l.forEach(function(a){if(a.attr("data-course-id")==b){a.find(j.COURSE_IS_FAVOURITE).removeClass("hidden")}})},v=function(a,b){l.forEach(function(a){if(a.attr("data-course-id")==b){a.find(j.COURSE_IS_FAVOURITE).addClass("hidden")}})},w=function(b){var d=a(j.BLOCK_CONTAINER).data("displaycoursecategory"),e=b.map(function(a){a.showcoursecategory=d;return f.render("block_recentlyaccessedcourses/course-card",a)});return a.when.apply(null,e).then(function(){var b=[];e.forEach(function(d){d.then(function(c){b.push(a(c))}).catch(c.exception)});return b})},x=function(a){return h.getLastAccessedCourses(a,10).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);if(o+p<d){f=o}else{var g=o+p-d;f=o-g;f=0<=f?f:0}if(0===p){p=1}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")}else{b.removeClass("justify-content-center");b.addClass("justify-content-start")}if(m!=i){var k=a.find(e.rootSelector);b.html(h);m=i;if(p>=l.length){t(a)}else{s(a);if(0===o){e.disablePreviousControlButtons(k)}else{e.enablePreviousControlButtons(k)}if(o+p>=l.length){e.disableNextControlButtons(k)}else{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){return}f=!0;var a=0,b=function(){setTimeout(function(){y(c);a++;if(5>a&&f){b()}},100)};b(c)});d.subscribe("nav-drawer-toggle-end",function(){f=!1});a(window).on("resize",function(){if(!k||!l.length){return}if(!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);if(!e.hasClass("disabled")){o=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);if(!e.hasClass("disabled")){o=o-p;o=0>o?0:o;y(c)}d.originalEvent.preventDefault()})};return{init:function init(b,d){d=a(d);z(d);x(b).then(function(a){l=a;k=!0;if(l.length){r(d);y(d)}else{q(d)}}).catch(c.exception)}}});
2 //# sourceMappingURL=main.min.js.map