Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. import Shuffle from 'shufflejs';
  2.  
  3. class Demo {
  4. constructor(element) {
  5. this.element = element;
  6. this.shuffle = new Shuffle(element, {
  7. itemSelector: '.picture-item',
  8. sizer: element.querySelector('.my-sizer-element'),
  9. });
  10.  
  11. // Log events.
  12. this.addShuffleEventListeners();
  13. this._activeFilters = [];
  14. this.addFilterButtons();
  15. this.addSorting();
  16. this.addSearchFilter();
  17. }
  18.  
  19. /**
  20. * Shuffle uses the CustomEvent constructor to dispatch events. You can listen
  21. * for them like you normally would (with jQuery for example).
  22. */
  23. addShuffleEventListeners() {
  24. this.shuffle.on(Shuffle.EventType.LAYOUT, (data) => {
  25. console.log('layout. data:', data);
  26. });
  27. this.shuffle.on(Shuffle.EventType.REMOVED, (data) => {
  28. console.log('removed. data:', data);
  29. });
  30. }
  31.  
  32. addFilterButtons() {
  33. const options = document.querySelector('.filter-options');
  34. if (!options) {
  35. return;
  36. }
  37.  
  38. const filterButtons = Array.from(options.children);
  39. const onClick = this._handleFilterClick.bind(this);
  40. filterButtons.forEach((button) => {
  41. button.addEventListener('click', onClick, false);
  42. });
  43. }
  44.  
  45. _handleFilterClick(evt) {
  46. const btn = evt.currentTarget;
  47. const isActive = btn.classList.contains('active');
  48. const btnGroup = btn.getAttribute('data-group');
  49.  
  50. this._removeActiveClassFromChildren(btn.parentNode);
  51.  
  52. let filterGroup;
  53. if (isActive) {
  54. btn.classList.remove('active');
  55. filterGroup = Shuffle.ALL_ITEMS;
  56. } else {
  57. btn.classList.add('active');
  58. filterGroup = btnGroup;
  59. }
  60.  
  61. this.shuffle.filter(filterGroup);
  62. }
  63.  
  64. _removeActiveClassFromChildren(parent) {
  65. const { children } = parent;
  66. for (let i = children.length - 1; i >= 0; i--) {
  67. children[i].classList.remove('active');
  68. }
  69. }
  70.  
  71. addSorting() {
  72. const buttonGroup = document.querySelector('.sort-options');
  73. if (!buttonGroup) {
  74. return;
  75. }
  76. buttonGroup.addEventListener('change', this._handleSortChange.bind(this));
  77. }
  78.  
  79. _handleSortChange(evt) {
  80. // Add and remove `active` class from buttons.
  81. const buttons = Array.from(evt.currentTarget.children);
  82. buttons.forEach((button) => {
  83. if (button.querySelector('input').value === evt.target.value) {
  84. button.classList.add('active');
  85. } else {
  86. button.classList.remove('active');
  87. }
  88. });
  89.  
  90. // Create the sort options to give to Shuffle.
  91. const { value } = evt.target;
  92. let options = {};
  93.  
  94. function sortByDate(element) {
  95. return element.getAttribute('data-date-created');
  96. }
  97.  
  98. function sortByTitle(element) {
  99. return element.getAttribute('data-title').toLowerCase();
  100. }
  101.  
  102. if (value === 'date-created') {
  103. options = {
  104. reverse: true,
  105. by: sortByDate,
  106. };
  107. } else if (value === 'title') {
  108. options = {
  109. by: sortByTitle,
  110. };
  111. }
  112. this.shuffle.sort(options);
  113. }
  114.  
  115. // Advanced filtering
  116. addSearchFilter() {
  117. const searchInput = document.querySelector('.js-shuffle-search');
  118. if (!searchInput) {
  119. return;
  120. }
  121. searchInput.addEventListener('keyup', this._handleSearchKeyup.bind(this));
  122. }
  123.  
  124. /**
  125. * Filter the shuffle instance by items with a title that matches the search input.
  126. * @param {Event} evt Event object.
  127. */
  128. _handleSearchKeyup(evt) {
  129. const searchText = evt.target.value.toLowerCase();
  130. this.shuffle.filter((element, shuffle) => {
  131. // If there is a current filter applied, ignore elements that don't match it.
  132. if (shuffle.group !== Shuffle.ALL_ITEMS) {
  133. // Get the item's groups.
  134. const groups = JSON.parse(element.getAttribute('data-groups'));
  135. const isElementInCurrentGroup = groups.indexOf(shuffle.group) !== -1;
  136. // Only search elements in the current group
  137. if (!isElementInCurrentGroup) {
  138. return false;
  139. }
  140. }
  141. const titleElement = element.querySelector('.picture-item__title');
  142. const titleText = titleElement.textContent.toLowerCase().trim();
  143. return titleText.indexOf(searchText) !== -1;
  144. });
  145. }
  146. }
  147.  
  148. export default Demo;
  149.  
  150. import React, { Component } from 'react';
  151. import Shuffle from 'shufflejs';
  152. import './Homebase.scss';
  153. import Demo from './Homebasescript.js';
  154.  
  155.  
  156. class homebase extends Component {
  157.  
  158. componentDidMount () {
  159. document.addEventListener('DOMContentLoaded', () => {
  160. window.demo = new Demo(document.getElementById('grid'));
  161. });
  162. }
  163.  
  164. render() {
  165. return (
  166.  
  167. <section className="about-page">
  168.  
  169. <div className="container-about">
  170. <div className="row-about">
  171. <div className="col-12@sm">
  172. <h1 className="about-title">Home Base Designs</h1>
  173. </div>
  174. </div>
  175. </div>
  176.  
  177. <div className="container-about">
  178. <div className="row-about">
  179. <div className="col-4@sm col-3@md">
  180. <div className="filters-group filters-group-left">
  181. <label htmlFor="filters-search-input" className="filter-label filter-color">Search</label>
  182. <input className="textfield filter__search js-shuffle-search" type="search" id="filters-search-input" />
  183. </div>
  184. </div>
  185. </div>
  186.  
  187. <div className="row-about">
  188. <div className="col-12@sm filters-group-wrap">
  189. <div className="filters-group filters-group-right">
  190. <p className="filter-label filter-color">Filter</p>
  191. <div className="btn-group filter-options">
  192. <button className="btn btn--primary" data-group="war">War</button>
  193. <button className="btn btn--primary" data-group="trophy">Trophy</button>
  194. <button className="btn btn--primary" data-group="farm">Farm</button>
  195. <button className="btn btn--primary" data-group="fun">Fun</button>
  196. </div>
  197. </div>
  198. <fieldset className="filters-group">
  199. <legend className="filter-label filter-color">Sort</legend>
  200. <div className="btn-group sort-options">
  201. <label className="btn active">
  202. <input type="radio" name="sort-value" value="dom" defaultChecked /> Default </label>
  203. <label className="btn">
  204. <input type="radio" name="sort-value" value="title" /> Title </label>
  205. <label className="btn">
  206. <input type="radio" name="sort-value" value="date-created" /> Date Created </label>
  207. </div>
  208. </fieldset>
  209. </div>
  210. </div>
  211. </div>
  212. </section>
  213.  
  214. );
  215. };
  216. };
  217.  
  218. export default homebase;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement