multipleFilterMasonry.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (function($){
  2. 'use strict';
  3. $.fn.multipleFilterMasonry = function(options){
  4. var cache=[];
  5. var filters = [];
  6. if(options.selectorType === 'list') {
  7. $(options.filtersGroupSelector).children().each(function() {
  8. filters.push($(this).data('filter'));
  9. });
  10. }
  11. //the main job of the function is to cache the item,because we are going to filter the items later
  12. var init = function($container){
  13. $container.find(options.itemSelector).each(function(){
  14. cache.push($(this));
  15. });
  16. $container.masonry(options);
  17. };
  18. //filter items in cache
  19. var filterItems = function(selector){
  20. var result=[];
  21. $(cache).each(function(item){
  22. $(selector).each(function(index,sel) {
  23. if(cache[item].is(sel)){
  24. if($.inArray(cache[item], result) === -1) result.push(cache[item]);
  25. }
  26. });
  27. });
  28. return result;
  29. };
  30. //reload masonry
  31. var reload = function($container,items){
  32. $container.empty();
  33. $(items).each(function(){
  34. $($container).append($(this));
  35. });
  36. $container.masonry('reloadItems');
  37. $container.masonry();
  38. };
  39. // Hash filter
  40. var hashFilter = function($container) {
  41. var hash = window.location.hash.replace("#", "");
  42. if($.inArray(hash, filters) !== -1) {
  43. reload($container, $('.' + hash));
  44. }
  45. };
  46. var proc = function($container){
  47. $(options.filtersGroupSelector).find('input[type=checkbox]').each(function(){
  48. $(this).change(function(){
  49. var selector = [];
  50. $(options.filtersGroupSelector).find('input[type=checkbox]').each( function() {
  51. if ( $(this).is(':checked') ) {
  52. selector.push( '.' + $(this).val() );
  53. }
  54. });
  55. var items = cache;
  56. if (selector.length > 0) {
  57. items = filterItems(selector);
  58. }
  59. reload($container,items);
  60. });
  61. });
  62. };
  63. var procUL = function($container){
  64. $(options.filtersGroupSelector).children().each(function(){
  65. $(this).click(function(){
  66. $(options.filtersGroupSelector).children().removeClass('selected');
  67. window.location.hash = $(this).data('filter');
  68. var selector = [];
  69. selector.push( '.' + $(this).data('filter') );
  70. $(this).addClass('selected');
  71. var items = cache;
  72. if (selector.length > 0) {
  73. items = filterItems(selector);
  74. }
  75. reload($container,items);
  76. });
  77. });
  78. hashFilter($container);
  79. $(options.filtersGroupSelector).children().removeClass('selected');
  80. $('.filters li[data-filter='+window.location.hash.replace("#", "")+']').addClass('selected');
  81. };
  82. return this.each(function() {
  83. var $$ = $(this);
  84. init($$);
  85. options.selectorType === 'list' ? procUL($$) : proc($$);
  86. });
  87. };
  88. }(window.jQuery));