{"version":3,"file":"pm_course_favorites.min.js","sources":["https:\/\/e-learning.fra.europa.eu\/theme\/snap\/amd\/src\/pm_course_favorites.js"],"sourcesContent":["\/**\n * This file is part of Moodle - http:\/\/moodle.org\/\n *\n * Moodle is free software: you can redistribute it and\/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * Moodle is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with Moodle. If not, see .\n *\n * @package\n * @copyright Copyright (c) 2016 Open LMS (https:\/\/www.openlms.net)\n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\/\n\n\/**\n * Course card favoriting.\n *\/\ndefine(['jquery', 'core\/ajax', 'core\/notification', 'core\/log', 'theme_snap\/model_view', 'theme_snap\/ajax_notification'],\n function($, ajax, notification, log, mview, ajaxNotify) {\n return function() {\n log.enableAll(true);\n\n \/**\n * The ajax call has returned a new course_card renderable.\n *\n * @method reloadCourseCardTemplate\n * @param {object} renderable - coursecard renderable\n * @param {jQuery} cardEl - coursecard element\n * @returns {Promise}\n *\/\n var reloadCourseCardTemplate = function(renderable, cardEl) {\n var dfd = $.Deferred();\n mview(cardEl, 'theme_snap\/course_cards');\n var callback = function() {\n var button = $(cardEl).find('.favoritetoggle');\n $(button).removeClass('ajaxing');\n $(button).focus();\n };\n $(cardEl).trigger('modelUpdate', [renderable, callback]);\n $(cardEl).on('modelUpdated', function(e) {\n dfd.resolve(e);\n });\n return dfd.promise();\n };\n\n \/**\n * Get course card course id.\n * @param {jQuery} cardEl\n * @returns {int}\n *\/\n var getCardId = function(cardEl) {\n return parseInt($(cardEl).find('.coursecard-body').data('courseid'));\n };\n\n \/**\n * Get course card full name.\n * @param {jQuery} cardEl\n * @param {null|bool} lowerCase\n * @returns {*|jQuery}\n *\/\n var getCardTitle = function(cardEl, lowerCase) {\n \/\/ The title comes back in lower case by default as it's used for case insensitive sorting.\n if (lowerCase === undefined) {\n lowerCase = true;\n }\n var title = $(cardEl).find('.coursecard-coursename').html();\n if (lowerCase) {\n title = title.toLowerCase();\n }\n return title;\n };\n\n \/**\n * Get index of card within list.\n *\n * @param {jQuery} cardEl\n * @param {jQuery} cards\n * @returns {number}\n *\/\n var getCardIndex = function(cardEl, cards) {\n if (cards.length === 0) {\n return -1;\n }\n \/\/ The sort variable is purely for sorting the cards by name.\n var sort = [],\n sortItem = {};\n\n cards.each(function() {\n sortItem = {\n title: getCardTitle(this),\n card: this\n };\n sort.push(sortItem);\n });\n \/\/ Add the item we are inserting to the list.\n sortItem = {\n title: getCardTitle(cardEl),\n card: cardEl\n };\n sort.push(sortItem);\n sort.sort(function(a, b) {\n var aId = getCardId(a.card);\n var bId = getCardId(b.card);\n if (a.title === b.title) {\n if (aId === bId) {\n return 0;\n }\n return aId > bId ? 1 : -1;\n }\n return a.title > b.title ? 1 : -1;\n });\n return sort.indexOf(sortItem);\n };\n\n \/**\n * Move card into alphabetical place in list.\n * @param {jQuery} cardEl\n * @param {string} listSelector\n * @param {string} listSelectorWhenEmpty\n * @param {bool} prependWhenEmpty\n * @param {function} onMoveComplete\n *\/\n var moveCard = function(cardEl, listSelector, listSelectorWhenEmpty, prependWhenEmpty, onMoveComplete) {\n\n var cardEls = $(listSelector);\n var idx = getCardIndex(cardEl, cardEls);\n var insIdx = idx + 1;\n\n log.debug('Moving card element into position ' + insIdx +\n ' of list (size = ' + cardEls.length + ') : ' + listSelector);\n\n if (insIdx > 0) {\n if (insIdx <= cardEls.length) {\n log.debug('Moving card before position ' + insIdx + ' using selector ' + listSelector);\n $(listSelector).eq(idx).before(cardEl);\n } else {\n log.debug('Moving card after position ' + cardEls.length + ' using selector ' + listSelector);\n $(listSelector).eq(cardEls.length - 1).after(cardEl);\n }\n } else {\n log.debug('Destination ' + listSelector + ' empty');\n if (prependWhenEmpty) {\n log.debug('prepending to ' + listSelectorWhenEmpty);\n $(listSelectorWhenEmpty).prepend(cardEl);\n } else {\n log.debug('appending to ' + listSelectorWhenEmpty);\n $(listSelectorWhenEmpty).append(cardEl);\n }\n }\n\n if (typeof (onMoveComplete) === 'function') {\n onMoveComplete();\n }\n };\n\n \/**\n * Move card element out of favorites.\n * @param {jQuery} cardEl\n * @param {function} onMoveComplete\n * @returns {void}\n *\/\n var moveOutOfFavorites = function(cardEl, onMoveComplete) {\n var container;\n \/\/ Check there are courses which are not hidden.\n \/\/ When this is 0 we only have hidden courses, so container is #snap-pm-courses-current-cards.\n var publishedcount = $('#snap-pm-courses-current .coursecard:not([data-hidden=\"true\"])').length;\n \/\/ Special stuff for when moving a hidden course.\n if ($(cardEl).data('hidden') === true && publishedcount > 0) {\n container = '#snap-pm-courses-hidden-cards';\n \/\/ Open hidden courses section.\n $('#snap-pm-courses-hidden').addClass('state-visible');\n $('#snap-pm-courses-hidden-cards').collapse('show');\n } else {\n window.console.log('not a hidden card');\n container = '#snap-pm-courses-current-cards';\n }\n moveCard(cardEl, container + ' .coursecard:not(.favorited)', container, false, onMoveComplete);\n };\n\n \/**\n * Favorite a course.\n * @param {jQuery} button - button clicked on to favorite the course.\n *\/\n var favoriteCourse = function(button) {\n if ($(button).hasClass('ajaxing')) {\n return;\n }\n\n $(button).addClass('ajaxing');\n\n var favorited = $(button).attr('aria-pressed') === 'true' ? 0 : 1;\n var cardEl = $($(button).parents('.coursecard')[0]);\n var shortname = $(cardEl).data('shortname');\n\n var doAjax = function(jsid) {\n return ajax.call([\n {\n methodname: 'theme_snap_course_card',\n args: {courseshortname: shortname, favorited: favorited},\n fail: function(response) {\n $(button).removeClass('ajaxing');\n ajaxNotify.ifErrorShowBestMsg(response);\n }\n }\n ], true, true)[0].then(function(response) {\n return reloadCourseCardTemplate(response, cardEl);\n }).then(function() {\n M.util.js_complete(jsid);\n });\n };\n\n var jsid;\n if (favorited === 1) {\n jsid = 'favourite_' + new Date().getTime().toString(16) + (Math.floor(Math.random() * 1000));\n M.util.js_pending(jsid);\n \/\/ Move to favorites.\n moveCard(cardEl, '#snap-pm-courses-current-cards .coursecard.favorited', '#snap-pm-courses-current-cards', true,\n function() {\n doAjax(jsid);\n }\n );\n } else {\n jsid = 'unfavourite_' + new Date().getTime().toString(16) + (Math.floor(Math.random() * 1000));\n M.util.js_pending(jsid);\n moveOutOfFavorites(cardEl,\n function() {\n doAjax(jsid);\n }\n );\n }\n };\n\n \/**\n * On clicking favourite toggle. (Delegated).\n *\/\n $(\"#snap-pm\").on(\"click\", \".favoritetoggle\", function(e) {\n e.preventDefault();\n e.stopPropagation();\n favoriteCourse(this);\n });\n };\n }\n);\n"],"names":["define","$","ajax","notification","log","mview","ajaxNotify","enableAll","getCardId","cardEl","parseInt","find","data","getCardTitle","lowerCase","undefined","title","html","toLowerCase","moveCard","listSelector","listSelectorWhenEmpty","prependWhenEmpty","onMoveComplete","cardEls","idx","cards","length","sort","sortItem","each","this","card","push","a","b","aId","bId","indexOf","getCardIndex","insIdx","debug","eq","before","after","prepend","append","favoriteCourse","button","hasClass","addClass","jsid","favorited","attr","parents","shortname","doAjax","call","methodname","args","courseshortname","fail","response","removeClass","ifErrorShowBestMsg","then","renderable","dfd","Deferred","trigger","focus","on","e","resolve","promise","reloadCourseCardTemplate","M","util","js_complete","Date","getTime","toString","Math","floor","random","js_pending","container","publishedcount","collapse","window","console","moveOutOfFavorites","preventDefault","stopPropagation"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwBAA,wCAAO,CAAC,SAAU,YAAa,oBAAqB,WAAY,wBAAyB,iCACrF,SAASC,EAAGC,KAAMC,aAAcC,IAAKC,MAAOC,mBACjC,WACHF,IAAIG,WAAU,OA8BVC,UAAY,SAASC,eACdC,SAAST,EAAEQ,QAAQE,KAAK,oBAAoBC,KAAK,cASxDC,aAAe,SAASJ,OAAQK,gBAEdC,IAAdD,YACAA,WAAY,OAEZE,MAAQf,EAAEQ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