1(function() { 2 var SOURCES = window.TEXT_VARIABLES.sources; 3 function queryString() { 4 // This function is anonymous, is executed immediately and 5 // the return value is assigned to QueryString! 6 var i = 0, queryObj = {}, pair; 7 var queryStr = window.location.search.substring(1); 8 var queryArr = queryStr.split('&'); 9 for (i = 0; i < queryArr.length; i++) { 10 pair = queryArr[i].split('='); 11 // If first entry with this name 12 if (typeof queryObj[pair[0]] === 'undefined') { 13 queryObj[pair[0]] = pair[1]; 14 // If second entry with this name 15 } else if (typeof queryObj[pair[0]] === 'string') { 16 queryObj[pair[0]] = [queryObj[pair[0]], pair[1]]; 17 // If third or later entry with this name 18 } else { 19 queryObj[pair[0]].push(pair[1]); 20 } 21 } 22 return queryObj; 23 } 24 25 var setUrlQuery = (function() { 26 var baseUrl = window.location.href.split('?')[0]; 27 return function(query) { 28 if (typeof query === 'string') { 29 window.history.replaceState(null, '', baseUrl + query); 30 } else { 31 window.history.replaceState(null, '', baseUrl); 32 } 33 }; 34 })(); 35 36 window.Lazyload.js(SOURCES.jquery, function() { 37 var $tags = $('.js-tags'); 38 var $articleTags = $tags.find('button'); 39 var $tagShowAll = $tags.find('.tag-button--all'); 40 var $result = $('.js-result'); 41 var $sections = $result.find('section'); 42 var sectionArticles = []; 43 var $lastFocusButton = null; 44 var sectionTopArticleIndex = []; 45 var hasInit = false; 46 47 $sections.each(function() { 48 sectionArticles.push($(this).find('.item')); 49 }); 50 51 function init() { 52 var i, index = 0; 53 for (i = 0; i < $sections.length; i++) { 54 sectionTopArticleIndex.push(index); 55 index += $sections.eq(i).find('.item').length; 56 } 57 sectionTopArticleIndex.push(index); 58 } 59 60 function searchButtonsByTag(_tag/*raw tag*/) { 61 if (!_tag) { 62 return $tagShowAll; 63 } 64 var _buttons = $articleTags.filter('[data-encode="' + _tag + '"]'); 65 if (_buttons.length === 0) { 66 return $tagShowAll; 67 } 68 return _buttons; 69 } 70 function buttonFocus(target) { 71 if (target) { 72 target.addClass('focus'); 73 $lastFocusButton && !$lastFocusButton.is(target) && $lastFocusButton.removeClass('focus'); 74 $lastFocusButton = target; 75 } 76 } 77 78 function tagSelect (tag/*raw tag*/, target) { 79 var result = {}, $articles; 80 var i, j, k, _tag; 81 82 for (i = 0; i < sectionArticles.length; i++) { 83 $articles = sectionArticles[i]; 84 for (j = 0; j < $articles.length; j++) { 85 if (tag === '' || tag === undefined) { 86 result[i] || (result[i] = {}); 87 result[i][j] = true; 88 } else { 89 var tags = $articles.eq(j).data('tags').split(','); 90 for (k = 0; k < tags.length; k++) { 91 if (tags[k] === tag) { 92 result[i] || (result[i] = {}); 93 result[i][j] = true; break; 94 } 95 } 96 } 97 } 98 } 99 100 for (i = 0; i < sectionArticles.length; i++) { 101 result[i] && $sections.eq(i).removeClass('d-none'); 102 result[i] || $sections.eq(i).addClass('d-none'); 103 for (j = 0; j < sectionArticles[i].length; j++) { 104 if (result[i] && result[i][j]) { 105 sectionArticles[i].eq(j).removeClass('d-none'); 106 } else { 107 sectionArticles[i].eq(j).addClass('d-none'); 108 } 109 } 110 } 111 112 hasInit || ($result.removeClass('d-none'), hasInit = true); 113 114 115 if (target) { 116 buttonFocus(target); 117 _tag = target.attr('data-encode'); 118 if (_tag === '' || typeof _tag !== 'string') { 119 setUrlQuery(); 120 } else { 121 setUrlQuery('?tag=' + _tag); 122 } 123 } else { 124 buttonFocus(searchButtonsByTag(tag)); 125 } 126 } 127 128 var query = queryString(), _tag = query.tag; 129 init(); tagSelect(_tag); 130 $tags.on('click', 'button', function() { 131 tagSelect($(this).data('encode'), $(this)); 132 }); 133 134 }); 135})(); 136