|
1 | | -$('#project-list'). |
2 | | - masonry({ transitionDuration: 0, columnWidth: 260 }). |
3 | | - css({visibility: 'visible'}); |
4 | | -$('footer').css({visibility: 'visible'}); |
5 | | - |
6 | | -$('#search-query').on('change keyup paste input', function(event) { |
7 | | - var query = $.trim(event.target.value.toLowerCase()); |
8 | | - var projects = $('#project-list li'); |
9 | | - if (query == '') { |
10 | | - projects.show(); |
| 1 | +function resizeMasonryItem(item){ |
| 2 | + /* Get the grid object, its row-gap, and the size of its implicit rows */ |
| 3 | + var grid = document.querySelector('#project-list'), |
| 4 | + rowGap = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-row-gap')), |
| 5 | + rowHeight = parseInt(window.getComputedStyle(grid).getPropertyValue('grid-auto-rows')); |
| 6 | + |
| 7 | + /* |
| 8 | + * Spanning for any brick = S |
| 9 | + * Grid's row-gap = G |
| 10 | + * Size of grid's implicitly create row-track = R |
| 11 | + * Height of item content = H |
| 12 | + * Net height of the item = H1 = H + G |
| 13 | + * Net height of the implicit row-track = T = G + R |
| 14 | + * S = H1 / T |
| 15 | + */ |
| 16 | + var rowSpan = Math.ceil((item.querySelector('.masonry-content').getBoundingClientRect().height+rowGap)/(rowHeight+rowGap)); |
| 17 | + |
| 18 | + /* Set the spanning as calculated above (S) */ |
| 19 | + item.style.gridRowEnd = 'span '+rowSpan; |
| 20 | +} |
| 21 | + |
| 22 | +function resizeAllMasonryItems(){ |
| 23 | + // Get all item class objects in one list |
| 24 | + var allItems = document.querySelectorAll('#project-list > .category'); |
| 25 | + |
| 26 | + /* |
| 27 | + * Loop through the above list and execute the spanning function to |
| 28 | + * each list-item (i.e. each masonry item) |
| 29 | + */ |
| 30 | + for(var i=0;i<allItems.length;i++){ |
| 31 | + resizeMasonryItem(allItems[i]); |
| 32 | + } |
| 33 | + |
| 34 | + document.querySelector('#project-list').style.visibility = 'visible'; |
| 35 | +} |
| 36 | + |
| 37 | +var masonryEvents = ['load', 'resize']; |
| 38 | +masonryEvents.forEach( function(event) { |
| 39 | + window.addEventListener(event, resizeAllMasonryItems); |
| 40 | +} ); |
| 41 | + |
| 42 | +function applySearch(rawQuery){ |
| 43 | + var query = rawQuery.toLowerCase().trim(); |
| 44 | + |
| 45 | + var categories = document.querySelectorAll('#project-list > .category'); |
| 46 | + var seenCategories = []; |
| 47 | + var projects = document.querySelectorAll('#project-list li'); |
| 48 | + |
| 49 | + for(var i=0; i<projects.length;i++){ |
| 50 | + var project = projects[i]; |
| 51 | + if (project.dataset.search.indexOf(query) == -1) { |
| 52 | + project.style.display = 'none'; |
11 | 53 | } else { |
12 | | - projects.hide().filter('[data-search*="' + query + '"]').show(); |
| 54 | + var projectCategory = project.parentNode.parentNode.parentNode; |
| 55 | + projectCategory.style.display = ''; |
| 56 | + project.style.display = ''; |
| 57 | + seenCategories.push(projectCategory); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + for(var i=0; i<categories.length;i++){ |
| 62 | + var category = categories[i]; |
| 63 | + if(seenCategories.indexOf(category) == -1){ |
| 64 | + category.style.display = 'none'; |
13 | 65 | } |
14 | | - $('#project-list .category').show().not(':has(li:visible)').hide(); |
15 | | - $('#project-list').masonry('layout'); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +var searchEvents = ['change', 'keyup', 'paste', 'input']; |
| 70 | +searchQuery = document.querySelector('#search-query'); |
| 71 | +searchEvents.forEach(function(event){ |
| 72 | + searchQuery.addEventListener(event, function(event){ applySearch(event.target.value) }); |
16 | 73 | }); |
17 | 74 |
|
18 | | -$('#search-query').focus().change(); |
| 75 | +window.addEventListener('load', function(){ |
| 76 | + applySearch(searchQuery.value); |
| 77 | +}); |
0 commit comments