1(function ($) { 2 3 $(document).ready(function() { 4 5 var $featuredContent = $('.featured-content'); 6 $featuredContent.each( function() { 7 var $widget = $(this); 8 9 10 var $catSelector = $(this).find('.category-selector'); 11 $catSelector.change( function() { 12 13 var newValue = $(this).val(); 14 var currentComponents = getFragmentComponents(); 15 16 // Avoid replacing the subcat and page with default values if the category isn't actually changing. 17 if (currentComponents.cat == newValue) return; 18 19 updateUrlFragment({ cat:newValue, subcat:null, page:null }); 20 21 }); 22 23 24 $subcatButton = $(this).find('a.category-link'); 25 $subcatButton.click( function(ev) { 26 27 ev.preventDefault(); 28 29 var subcat = $(this).attr('data-slug'); 30 var currentComponents = getFragmentComponents(); 31 32 $(this) 33 .addClass('active') 34 .siblings().removeClass('active'); 35 36 // Avoid replacing the page with default values if the subcategory isn't actually changing. 37 if (currentComponents.subcat == subcat) return; 38 39 updateUrlFragment({ subcat:subcat, page:null }); 40 41 }); 42 43 44 if($(this).hasClass('featured-content-grid')) { 45 $(this).find(".pagination").each( function () { 46 var $pages = $(this).siblings("ul").find(".page"); 47 var $pageButtons = $(this).find(".pagination-button"); 48 49 $pageButtons.click(function(ev){ 50 var pageNum = $(this).attr("data-page-num"); 51 updateUrlFragment({ page:pageNum }); 52 53 $pageButtons.removeClass("active"); 54 $(this).addClass("active"); 55 56 var $myPage = $pages.filter("[data-page-num='" + pageNum + "']"); 57 $(document).scrollTop($myPage.parents('.featured-content').offset().top - 50); 58 59 }); 60 61 }); 62 } 63 64 65 function updateUrlFragment(newComponents) { 66 67 var components = getFragmentComponents(); 68 var newFragment = ""; 69 70 if (typeof newComponents.cat !== "undefined") components.cat = newComponents.cat; 71 if (typeof newComponents.subcat !== "undefined") components.subcat = newComponents.subcat; 72 if (typeof newComponents.page !== "undefined") components.page = newComponents.page; 73 74 for (var componentName in components) { 75 if ( components[componentName] !== null ) { 76 var fragmentPiece = componentName + '-' + components[componentName]; 77 if ( newFragment != "" ) newFragment += '--' 78 newFragment += fragmentPiece; 79 } 80 } 81 82 window.location.hash = newFragment; 83 84 } 85 86 function getFragmentComponents() { 87 88 var Fragment = window.location.hash; 89 Fragment = Fragment.replace("#", ""); 90 var components = Fragment.split("--"); 91 92 var cat = null; 93 var subcat = null; 94 var page = null; 95 96 // Extract category, subcategory, and page from the fragment, each if present and ignore junk info: 97 for (i=0; i<components.length; i++) { 98 var componentParts = components[i].split('-'); 99 var componentName = componentParts[0]; 100 var componentValue = componentParts[1]; 101 102 if ( componentName == "cat" ) cat = componentValue; 103 if ( componentName == "subcat" ) subcat = componentValue; 104 if ( componentName == "page" ) page = componentValue; 105 } 106 107 return { 108 cat:cat, 109 subcat:subcat, 110 page:page 111 }; 112 } 113 114 function applyNavigationFragment(fragment) { 115 /* 116 Take a fragment constructed by buildUrlFragment, deconstruct it into a set of navigation specs, 117 and apply them. This way browser navigation will be compatible with the refreshing content. 118 */ 119 120 121 var fragmentComponents = getFragmentComponents(); 122 var cat = fragmentComponents.cat; 123 var subcat = fragmentComponents.subcat; 124 var page = fragmentComponents.page; 125 126 // Validate that a category has been supplied. The subcat will be meaningless without first having a designated parent category. 127 if ( cat === null ) { 128 applyPaginationState($widget, page); 129 return; 130 } 131 132 // Default all categories and subcategories to hidden 133 $widget.find(".category-contents").hide(); 134 135 var $activeCat = $widget.children(".category-contents.category-contents-" + cat); 136 137 // Show the active category 138 $activeCat.show(); 139 140 // Make sure that the category selector is synced with the category that was activated based on the fragment: 141 $catSelector.val(cat); 142 143 // Check whether this category has subcategories. This will change the location where we will look for 144 // pagination. 145 if ( $activeCat.hasClass('has-children') ) { 146 147 var $activeSubcat = null; 148 // Display the subcategory and activate the associated button. Default the first if not provided. 149 if ( subcat === null ) { 150 151 $activeSubcat = $activeCat.find(".category-contents").first(); 152 $activeSubcat.show(); 153 $activeCat.find(".category-link").first() 154 .addClass('active') 155 .siblings().removeClass('active'); 156 157 } else { 158 159 $activeSubcat = $activeCat.find(".category-contents-" + subcat); 160 $activeSubcat.show(); 161 $activeCat.find(".category-link[data-slug='" + subcat + "']") 162 .addClass('active') 163 .siblings().removeClass('active'); 164 165 } 166 167 applyPaginationState($activeSubcat, page); 168 169 } else { 170 171 applyPaginationState($activeCat, page); 172 173 } 174 175 } 176 177 function applyPaginationState($container, page) { 178 /* 179 Given a containing element and the page identifier, applies the pagination state. 180 */ 181 182 // Default to the first page 183 if ( page === null ) page = 0; 184 185 // Display the active page from the fragment, and activate the associated button 186 $container 187 .find(".page") 188 .hide() 189 .filter("[data-page-num='" + page + "']") 190 .show(); 191 $container 192 .find(".pagination-button") 193 .filter("[data-page-num='" + page + "']") 194 .addClass('active') 195 .siblings().removeClass('active'); 196 197 198 } 199 200 // initialize the category selection 201 $catSelector.change(); 202 203 $(window).on('hashchange', applyNavigationFragment); 204 // Initialize navigation state based on the URL fragment if, for example, the 'back' button was clicked. 205 applyNavigationFragment(); 206 207 208 }); 209 210 }); 211 212})(jQuery); 213
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.