masonry.filter.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* -- Filter Plugin -- */
  2. (function ($) {
  3. 'use strict';
  4. $.fn.masonryFilter = function (options) {
  5. //reload masonry
  6. var reload = function ($container) {
  7. setTimeout(function () {
  8. $container.masonry("layout");
  9. }, 100);
  10. };
  11. var process = function ($container) {
  12. var items = $container.masonry("getAllItems"),
  13. revealItems = [],
  14. hideItems = [];
  15. $.each(items, function(i) {
  16. var item = items[i];
  17. var elm = $(item.element),
  18. shouldShow = options.filter && options.filter.call(elm);
  19. if (shouldShow) {
  20. if (item.isHidden) {
  21. // -- Have to set this property so masonry does
  22. // not include hidden items when calling "layout"
  23. item.isIgnored = false;
  24. revealItems.push(item);
  25. }
  26. } else {
  27. if (!item.isHidden) {
  28. // -- Easier to set this property directy rather than
  29. // using the "ignore" method, as it takes in a DOM
  30. // element rather than the masonry item object.
  31. item.isIgnored = true;
  32. hideItems.push(item);
  33. }
  34. }
  35. });
  36. $container.masonry('hide', hideItems);
  37. $container.masonry('reveal', revealItems);
  38. reload($container);
  39. };
  40. return this.each(function () {
  41. var self = $(this);
  42. process(self);
  43. });
  44. };
  45. }(window.jQuery));