Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. function Gallery() {
  2. this.tag = $('.tags-list .tag');
  3. this.gridItem = $('.grid .grid-item');
  4. this.activeTags = ['toate'];
  5. this.itemsList = [];
  6. this.activeItems = [];
  7. this.noItem = false;
  8. this.init();
  9. }
  10.  
  11. Gallery.prototype.init = function () {
  12. this.masonry();
  13. this.itemsListGen();
  14. };
  15.  
  16. Gallery.prototype.itemsListGen = function () {
  17. var _this = this;
  18.  
  19. $(this.gridItem).each(function () {
  20.  
  21. var tags = $(this).attr('data-tag');
  22. tags = tags.trim();
  23.  
  24. // creates an array of objects with every item's id and tags
  25. _this.itemsList.push({
  26. 'id': $(this).attr('id'),
  27. 'tags': tags.split(' ')
  28. });
  29. });
  30.  
  31. };
  32.  
  33. Gallery.prototype.masonry = function () {
  34. $('.grid').masonry({
  35. // options
  36. columnWidth: '.grid-sizer',
  37. itemSelector: '.grid-item',
  38. percentPosition: true,
  39. hash: false
  40.  
  41. });
  42. };
  43.  
  44. Gallery.prototype.tagClick = function (e) {
  45. e.preventDefault();
  46. // currentTag has the clicked tag
  47. var currentTag = $(e.currentTarget).attr('data-tag-slug');
  48. $(e.currentTarget).toggleClass('active');
  49.  
  50. if (this.noItem) {
  51. $('.no-item').remove();
  52. this.noItem = false;
  53. }
  54.  
  55. // refresh current filters
  56. this.refreshTags(currentTag);
  57.  
  58. if ($(e.currentTarget).attr('data-tag-slug') === 'toate' || this.activeTags.length < 1) {
  59. this.showAll();
  60. } else {
  61.  
  62. // return validated items based on filters
  63. this.returnValidItems();
  64.  
  65. // refresh the layout with the current items
  66. this.refreshMasonry();
  67. }
  68.  
  69. this.refreshGallery();
  70. };
  71.  
  72. Gallery.prototype.refreshTags = function (e) {
  73. // checks if the current tag is already active
  74. // if it's active, it removes it and checks if there is any other active tag or should trigger 'toate'
  75. // if it's not active, it adds it to the activeTags and removes 'toate'
  76.  
  77. if (this.activeTags.includes(e)) {
  78. this.activeTags.splice(this.activeTags.indexOf(e), 1);
  79.  
  80. } else {
  81. this.activeTags.push(e);
  82.  
  83. if (this.activeTags.includes('toate')) {
  84. this.removeToate();
  85. }
  86. }
  87.  
  88. };
  89.  
  90. Gallery.prototype.refreshGallery = function () {
  91. $(document).unbind('click.fb-start');
  92.  
  93. var _this = this;
  94. $(this.gridItem).find('a').attr('data-fancybox', 'galerie');
  95. for (var i = 0; i < this.activeItems.length; i++) {
  96. var id = _this.activeItems[i];
  97. $('#' + id).find('a').attr('data-fancybox', _this.activeTags);
  98. }
  99.  
  100. $(this.gridItem).find('a').fancybox();
  101. };
  102.  
  103. Gallery.prototype.returnValidItems = function () {
  104. // debugger;
  105. var newValidatedItems = [];
  106.  
  107. for (var i = 0; i < this.itemsList.length; i++) {
  108. var itemTags = this.itemsList[i].tags;
  109.  
  110. for (var j = 0; j < this.activeTags.length; j++) {
  111. if (itemTags.includes(this.activeTags[j])) {
  112. if (!newValidatedItems.includes(this.itemsList[i].id)) {
  113. newValidatedItems.push(this.itemsList[i].id);
  114. }
  115. } else if (newValidatedItems.includes(this.itemsList[i].id)) {
  116. newValidatedItems.splice(newValidatedItems.indexOf(this.itemsList[i].id), 1);
  117. break;
  118. } else {
  119. break;
  120. }
  121. }
  122. }
  123.  
  124. this.activeItems = newValidatedItems;
  125. if (this.activeItems.length < 1) {
  126. this.noItemFound();
  127. }
  128. };
  129.  
  130. Gallery.prototype.refreshMasonry = function () {
  131. $('.grid-item').hide();
  132.  
  133. for (var i = 0; i < this.activeItems.length; i++) {
  134. $('#' + this.activeItems[i]).show();
  135. }
  136.  
  137. $('.grid').masonry();
  138. };
  139.  
  140. Gallery.prototype.showAll = function () {
  141. console.log('show all');
  142. this.activeTags = ['toate'];
  143. this.activeItems = [];
  144. $('.grid-item').show();
  145. $('.tags-list').find('.active').removeClass('active');
  146. $('.tags-list').find('.toate a').addClass('active');
  147. $('.grid').masonry();
  148. };
  149.  
  150. Gallery.prototype.removeToate = function () {
  151. $('.tags-list li.toate > a').removeClass('active');
  152. this.activeTags.splice(this.activeTags.indexOf('toate'), 1);
  153. };
  154.  
  155. Gallery.prototype.noItemFound = function () {
  156. this.noItem = true;
  157. $('.grid').prepend('<div class="no-item text-center">Nu există nicio poză conform selecţiei</div>');
  158. };
  159.  
  160. $(document).ready(function () {
  161. var gallery = new Gallery();
  162. $('.tags-list .tag a').on('click', $.proxy(gallery.tagClick, gallery));
  163.  
  164. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement