MDL-63457 block_myoverview: Rerender the paged content and jump
authorBas Brands <bas@moodle.com>
Thu, 25 Oct 2018 14:32:33 +0000 (16:32 +0200)
committerPeter <peter@moodle.com>
Mon, 29 Oct 2018 00:36:59 +0000 (08:36 +0800)
Jump to the page the user was last on after hiding

blocks/myoverview/amd/build/view.min.js
blocks/myoverview/amd/src/view.js
lib/amd/build/paged_content.min.js
lib/amd/build/paged_content_factory.min.js
lib/amd/build/paged_content_pages.min.js
lib/amd/src/paged_content.js
lib/amd/src/paged_content_factory.js
lib/amd/src/paged_content_pages.js

index 4fec7f0..b1ea751 100644 (file)
Binary files a/blocks/myoverview/amd/build/view.min.js and b/blocks/myoverview/amd/build/view.min.js differ
index 91ce046..6002ed6 100644 (file)
@@ -232,17 +232,18 @@ function(
         }).catch(Notification.exception);
     };
 
-    var toggleHiddenFromCard = function(root, courseId, status) {
-        var element = root.find('[data-course-id="' + courseId + '"][data-region="course-content"]');
-
-        loadedPages.forEach(function(courseList) {
-            courseList.courses.forEach(function(course, index) {
-                if (course.id == courseId) {
-                    courseList.courses[index].hidden = status;
-                    element.css('display', 'none');
-                }
-            });
-        });
+    /**
+     * Initialize the Paged Content and jump to the active Page.
+     *
+     * @param {Object} root The course overview container
+     * @param {object} content The content element for the courses view.
+     */
+    var initializeJumpto = function(root, content) {
+
+        var pagingBar = root.find('[data-region="paging-bar"]');
+        var jumpto = pagingBar.attr('data-active-page-number');
+
+        initializePagedContent(root, content, jumpto);
     };
 
     /**
@@ -315,7 +316,7 @@ function(
      * @param {object} root The root element for the courses view.
      * @param {object} content The content element for the courses view.
      */
-    var initializePagedContent = function(root, content) {
+    var initializePagedContent = function(root, content, jumpto) {
         var filters = getFilterValues(root);
 
         var pagedContentPromise = PagedContentFactory.createWithLimit(
@@ -345,7 +346,8 @@ function(
 
                 return promises;
             },
-            DEFAULT_PAGED_CONTENT_CONFIG
+            DEFAULT_PAGED_CONTENT_CONFIG,
+            jumpto
         );
 
         pagedContentPromise.then(function(html, js) {
@@ -358,7 +360,7 @@ function(
      *
      * @param {Object} root The myoverview block container element.
      */
-    var registerEventListeners = function(root) {
+    var registerEventListeners = function(root, content) {
         CustomEvents.define(root, [
             CustomEvents.events.activate
         ]);
@@ -395,7 +397,7 @@ function(
             };
             Repository.updateUserPreferences(request);
 
-            toggleHiddenFromCard(root, id, true);
+            initializeJumpto(root, content);
             data.originalEvent.preventDefault();
         });
 
@@ -414,7 +416,7 @@ function(
 
             Repository.updateUserPreferences(request);
 
-            toggleHiddenFromCard(root, id, false);
+            initializeJumpto(root, content);
             data.originalEvent.preventDefault();
         });
     };
index 860f755..852965f 100644 (file)
Binary files a/lib/amd/build/paged_content.min.js and b/lib/amd/build/paged_content.min.js differ
index 62c3166..16502e7 100644 (file)
Binary files a/lib/amd/build/paged_content_factory.min.js and b/lib/amd/build/paged_content_factory.min.js differ
index ae2015f..90cd257 100644 (file)
Binary files a/lib/amd/build/paged_content_pages.min.js and b/lib/amd/build/paged_content_pages.min.js differ
index 9067ca5..f531209 100644 (file)
@@ -45,7 +45,7 @@ function(
      *                                              content page. See core/paged_content_pages for
      *                                              more defails.
      */
-    var init = function(root, renderPagesContentCallback) {
+    var init = function(root, renderPagesContentCallback, jumpto) {
         root = $(root);
         var pagesContainer = root.find(Pages.rootSelector);
         var pagingBarContainer = root.find(PagingBar.rootSelector);
@@ -53,10 +53,13 @@ function(
         var pagingBarLimitSelectorContainer = root.find(PagingBarLimitSelector.rootSelector);
         var id = root.attr('id');
 
-        Pages.init(pagesContainer, id, renderPagesContentCallback);
+        Pages.init(pagesContainer, id, renderPagesContentCallback, jumpto);
 
         if (pagingBarContainer.length) {
             PagingBar.init(pagingBarContainer, id);
+            if (jumpto > 1) {
+                PagingBar.showPage(pagingBarContainer, jumpto, id);
+            }
         }
 
         if (pagingBarLimitSelectorContainer.length) {
index 696eeeb..879bae9 100644 (file)
@@ -386,8 +386,8 @@ function(
      * @param  {object} config  Configuration options provided by the client.
      * @return {promise} Resolved with jQuery HTML and string JS.
      */
-    var createWithLimit = function(itemsPerPage, renderPagesContentCallback, config) {
-        return createWithTotalAndLimit(null, itemsPerPage, renderPagesContentCallback, config);
+    var createWithLimit = function(itemsPerPage, renderPagesContentCallback, config, jumpto) {
+        return createWithTotalAndLimit(null, itemsPerPage, renderPagesContentCallback, config, jumpto);
     };
 
     /**
@@ -412,7 +412,7 @@ function(
      * @param  {object} config  Configuration options provided by the client.
      * @return {promise} Resolved with jQuery HTML and string JS.
      */
-    var createWithTotalAndLimit = function(numberOfItems, itemsPerPage, renderPagesContentCallback, config) {
+    var createWithTotalAndLimit = function(numberOfItems, itemsPerPage, renderPagesContentCallback, config, jumpto) {
         config = config || {};
 
         var deferred = $.Deferred();
@@ -424,7 +424,7 @@ function(
 
                 var container = html;
 
-                PagedContent.init(container, renderPagesContentCallback);
+                PagedContent.init(container, renderPagesContentCallback, jumpto);
 
                 deferred.resolve(html, js);
                 return;
index e76069a..593dcd0 100644 (file)
@@ -181,14 +181,17 @@ define(
      * @param {string} id A unique id for this instance.
      * @param {function} renderPagesContentCallback Render pages content.
      */
-    var showPages = function(root, pagesData, id, renderPagesContentCallback) {
+    var showPages = function(root, pagesData, id, renderPagesContentCallback, jumpto) {
         var existingPages = [];
         var newPageData = [];
         var newPagesPromise = $.Deferred();
-
+        var shownewpage = true;
         // Check which of the pages being requests have previously been rendered
         // so that we only ask for new pages to be rendered by the callback.
         pagesData.forEach(function(pageData) {
+            if ((jumpto > 1) && (pageData.pageNumber != jumpto)) {
+                shownewpage = false;
+            }
             var pageNumber = pageData.pageNumber;
             var existingPage = findPage(root, pageNumber);
             if (existingPage.length) {
@@ -239,9 +242,11 @@ define(
             var pagesToShow = existingPages.concat(newPages);
             // Hide all existing pages.
             root.find(SELECTORS.PAGE_REGION).addClass('hidden');
-            // Show each of the pages that were requested.
+            // Show each of the pages that were requested.;
             pagesToShow.forEach(function(page) {
-                page.removeClass('hidden');
+                if (shownewpage) {
+                    page.removeClass('hidden');
+                }
             });
 
             return;
@@ -289,11 +294,13 @@ define(
      * @param {string} id A unique id for this instance.
      * @param {function} renderPagesContentCallback Render pages content.
      */
-    var init = function(root, id, renderPagesContentCallback) {
+    var init = function(root, id, renderPagesContentCallback, jumpto) {
         root = $(root);
 
         PubSub.subscribe(id + PagedContentEvents.SHOW_PAGES, function(pagesData) {
-            showPages(root, pagesData, id, renderPagesContentCallback);
+            showPages(root, pagesData, id, renderPagesContentCallback, jumpto);
+            // Reset the jumpto number after using it once else it will only ever show the one page.
+            jumpto = 0;
         });
 
         PubSub.subscribe(id + PagedContentEvents.SET_ITEMS_PER_PAGE_LIMIT, function() {