{"version":3,"file":"paged_content_paging_dropdown.min.js","sources":["../src/paged_content_paging_dropdown.js"],"sourcesContent":["// 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/**\n * Javascript to manage the paging dropdown control.\n *\n * @module core/paged_content_paging_dropdown\n * @copyright 2018 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(\n [\n 'jquery',\n 'core/custom_interaction_events',\n 'core/paged_content_events',\n 'core/pubsub'\n ],\n function(\n $,\n CustomEvents,\n PagedContentEvents,\n PubSub\n ) {\n\n var SELECTORS = {\n ROOT: '[data-region=\"paging-dropdown-container\"]',\n DROPDOWN_ITEM: '[data-region=\"dropdown-item\"]',\n DROPDOWN_TOGGLE: '[data-region=\"dropdown-toggle\"]',\n ACTIVE_DROPDOWN_ITEM: '[data-region=\"dropdown-item\"].active',\n CARET: '[data-region=\"caret\"]'\n };\n\n /**\n * Get the page number.\n *\n * @param {jquery} item The dropdown item.\n * @returns {Number}\n */\n var getPageNumber = function(item) {\n return parseInt(item.attr('data-page-number'), 10);\n };\n\n /**\n * Get all paging dropdown items.\n *\n * @param {jquery} root The root element.\n * @returns {jquery} A jquery object with all items.\n */\n var getAllItems = function(root) {\n return root.find(SELECTORS.DROPDOWN_ITEM);\n };\n\n /**\n * Get all paging dropdown items with lower page numbers than the given\n * dropdown item.\n *\n * @param {jquery} root The root element.\n * @param {jquery} item The dropdown item.\n * @returns {jquery} A jquery object with all items.\n */\n var getPreviousItems = function(root, item) {\n var pageNumber = getPageNumber(item);\n return getAllItems(root).filter(function(index, element) {\n return getPageNumber($(element)) < pageNumber;\n });\n };\n\n /**\n * Get the number of items to be loaded for the dropdown item.\n *\n * @param {jquery} item The dropdown item.\n * @returns {Number}\n */\n var getLimit = function(item) {\n return parseInt(item.attr('data-item-count'), 10);\n };\n\n /**\n * Get the offset of items from the start of the itemset for the given\n * dropdown item.\n *\n * @param {jquery} root The root element.\n * @param {jquery} item The dropdown item.\n * @returns {Number}\n */\n var getOffset = function(root, item) {\n if (item.attr('data-offset') != undefined) {\n return parseInt(item.attr('data-offset'), 10);\n }\n\n var offset = 0;\n\n getPreviousItems(root, item).each(function(index, prevItem) {\n prevItem = $(prevItem);\n offset += getLimit(prevItem);\n });\n\n item.attr('data-offset', offset);\n return offset;\n };\n\n /**\n * Get the active dropdown item.\n *\n * @param {jquery} root The root element.\n * @returns {jquery} The active dropdown item.\n */\n var getActiveItem = function(root) {\n return root.find(SELECTORS.ACTIVE_DROPDOWN_ITEM);\n };\n\n /**\n * Create the event payload for the list of dropdown items. The event payload\n * is an array of objects with one object per dropdown item.\n *\n * Each payload object contains the page number, limit, and offset for the\n * corresponding dropdown item.\n *\n * For example: If we had 3 dropdown items with incrementing page numbers loading\n * 25 items per page then the generated payload would look like:\n * [\n * {\n * pageNumber: 1,\n * limit: 25,\n * offset: 0\n * },\n * {\n * pageNumber: 2,\n * limit: 25,\n * offset: 25\n * },\n * {\n * pageNumber: 3,\n * limit: 25,\n * offset: 50\n * }\n * ]\n *\n * @param {jquery} root The root element.\n * @param {jquery} items The dropdown items.\n * @returns {object[]} The payload for the event.\n */\n var generateEventPayload = function(root, items) {\n return items.map(function(index, item) {\n item = $(item);\n return {\n pageNumber: getPageNumber(item),\n limit: getLimit(item),\n offset: getOffset(root, item),\n };\n }).get();\n };\n\n /**\n * Add page number attributes to each of the given items. The page numbers\n * start at 1 and increment by 1 for each item, e.g. 1, 2, 3 etc.\n *\n * @param {jquery} items The dropdown items.\n */\n var generatePageNumbers = function(items) {\n items.each(function(index, item) {\n item = $(item);\n item.attr('data-page-number', index + 1);\n });\n };\n\n /**\n * Make the given item active by setting the active class on it and firing\n * the SHOW_PAGES event for the paged content to show the appropriate\n * pages.\n *\n * @param {jquery} root The root element.\n * @param {jquery} item The dropdown item.\n * @param {string} id A unique id for this instance.\n */\n var setActiveItem = function(root, item, id) {\n var prevItems = getPreviousItems(root, item);\n var allItems = prevItems.add(item);\n var eventPayload = generateEventPayload(root, allItems);\n var toggle = root.find(SELECTORS.DROPDOWN_TOGGLE);\n var caret = toggle.find(SELECTORS.CARET);\n\n getActiveItem(root).removeClass('active');\n item.addClass('active');\n\n // Update the dropdown toggle to show which item is selected.\n toggle.html(item.text());\n // Bootstrap 2 compatibility.\n toggle.append(caret);\n // Fire the event to tell the content to update.\n PubSub.publish(id + PagedContentEvents.SHOW_PAGES, eventPayload);\n };\n\n /**\n * Initialise the module by firing the SHOW_PAGES event for an existing\n * active page found and setting up the event listener for the user to select\n * new pages.\n *\n * @param {object} root The root element.\n * @param {string} id A unique id for this instance.\n */\n var init = function(root, id) {\n root = $(root);\n var items = getAllItems(root);\n generatePageNumbers(items);\n\n var activeItem = getActiveItem(root);\n if (activeItem.length) {\n // Fire the first event for the content to make sure it's visible.\n setActiveItem(root, activeItem, id);\n }\n\n CustomEvents.define(root, [\n CustomEvents.events.activate\n ]);\n\n root.on(CustomEvents.events.activate, SELECTORS.DROPDOWN_ITEM, function(e, data) {\n var item = $(e.target).closest(SELECTORS.DROPDOWN_ITEM);\n setActiveItem(root, item, id);\n\n data.originalEvent.preventDefault();\n });\n };\n\n return {\n init: init,\n rootSelector: SELECTORS.ROOT,\n };\n});\n"],"names":["define","$","CustomEvents","PagedContentEvents","PubSub","SELECTORS","getPageNumber","item","parseInt","attr","getAllItems","root","find","getPreviousItems","pageNumber","filter","index","element","getLimit","getOffset","undefined","offset","each","prevItem","getActiveItem","setActiveItem","id","eventPayload","items","map","limit","get","generateEventPayload","add","toggle","caret","removeClass","addClass","html","text","append","publish","SHOW_PAGES","init","generatePageNumbers","activeItem","length","events","activate","on","e","data","target","closest","originalEvent","preventDefault","rootSelector"],"mappings":";;;;;;;AAsBAA,4CACI,CACI,SACA,iCACA,4BACA,gBAEJ,SACIC,EACAC,aACAC,mBACAC,YAGAC,wBAEe,gCAFfA,0BAGiB,kCAHjBA,+BAIsB,uCAJtBA,gBAKO,wBASPC,cAAgB,SAASC,aAClBC,SAASD,KAAKE,KAAK,oBAAqB,KAS/CC,YAAc,SAASC,aAChBA,KAAKC,KAAKP,0BAWjBQ,iBAAmB,SAASF,KAAMJ,UAC9BO,WAAaR,cAAcC,aACxBG,YAAYC,MAAMI,QAAO,SAASC,MAAOC,gBACrCX,cAAcL,EAAEgB,UAAYH,eAUvCI,SAAW,SAASX,aACbC,SAASD,KAAKE,KAAK,mBAAoB,KAW9CU,UAAY,SAASR,KAAMJ,SACKa,MAA5Bb,KAAKE,KAAK,sBACHD,SAASD,KAAKE,KAAK,eAAgB,QAG1CY,OAAS,SAEbR,iBAAiBF,KAAMJ,MAAMe,MAAK,SAASN,MAAOO,UAC9CA,SAAWtB,EAAEsB,UACbF,QAAUH,SAASK,aAGvBhB,KAAKE,KAAK,cAAeY,QAClBA,QASPG,cAAgB,SAASb,aAClBA,KAAKC,KAAKP,iCAmEjBoB,cAAgB,SAASd,KAAMJ,KAAMmB,QAGjCC,aApCmB,SAAShB,KAAMiB,cAC/BA,MAAMC,KAAI,SAASb,MAAOT,aAC7BA,KAAON,EAAEM,MACF,CACHO,WAAYR,cAAcC,MAC1BuB,MAAOZ,SAASX,MAChBc,OAAQF,UAAUR,KAAMJ,UAE7BwB,MA4BgBC,CAAqBrB,KAFxBE,iBAAiBF,KAAMJ,MACd0B,IAAI1B,OAEzB2B,OAASvB,KAAKC,KAAKP,2BACnB8B,MAAQD,OAAOtB,KAAKP,iBAExBmB,cAAcb,MAAMyB,YAAY,UAChC7B,KAAK8B,SAAS,UAGdH,OAAOI,KAAK/B,KAAKgC,QAEjBL,OAAOM,OAAOL,OAEd/B,OAAOqC,QAAQf,GAAKvB,mBAAmBuC,WAAYf,qBAkChD,CACHgB,KAxBO,SAAShC,KAAMe,IACtBf,KAAOV,EAAEU,MA3Ca,SAASiB,OAC/BA,MAAMN,MAAK,SAASN,MAAOT,OACvBA,KAAON,EAAEM,OACJE,KAAK,mBAAoBO,MAAQ,MA0C1C4B,CADYlC,YAAYC,WAGpBkC,WAAarB,cAAcb,MAC3BkC,WAAWC,QAEXrB,cAAcd,KAAMkC,WAAYnB,IAGpCxB,aAAaF,OAAOW,KAAM,CACtBT,aAAa6C,OAAOC,WAGxBrC,KAAKsC,GAAG/C,aAAa6C,OAAOC,SAAU3C,yBAAyB,SAAS6C,EAAGC,UACnE5C,KAAON,EAAEiD,EAAEE,QAAQC,QAAQhD,yBAC/BoB,cAAcd,KAAMJ,KAAMmB,IAE1ByB,KAAKG,cAAcC,qBAMvBC,aAzMM"}