index.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <link href="../reset.css" rel="stylesheet"/>
  6. <link href="./style.css" rel="stylesheet"/>
  7. <title>MixItUp Demo - Dataset with Pre-rendered Targets</title>
  8. </head>
  9. <body>
  10. <div class="controls" data-ref="controls">
  11. <button type="button" class="control control-filter" data-ref="filter" data-color="all">All</button>
  12. <button type="button" class="control control-filter" data-ref="filter" data-color="green">Green</button>
  13. <button type="button" class="control control-filter" data-ref="filter" data-color="blue">Blue</button>
  14. <button type="button" class="control control-filter" data-ref="filter" data-color="pink">Pink</button>
  15. <button type="button" class="control control-filter" data-ref="filter" data-color="none">None</button>
  16. <button type="button" class="control control-sort" data-ref="sort" data-key="publishedDate" data-order="asc">Asc</button>
  17. <button type="button" class="control control-sort" data-ref="sort" data-key="publishedDate" data-order="desc">Desc</button>
  18. </div>
  19. <div class="container" data-ref="container">
  20. <div class="item green" data-ref="item">
  21. <time class="published-date">2015-10-03</time>
  22. </div>
  23. <div class="item green" data-ref="item">
  24. <time class="published-date">2015-12-22</time>
  25. </div>
  26. <div class="item blue" data-ref="item">
  27. <time class="published-date">2016-02-15</time>
  28. </div>
  29. <div class="item pink" data-ref="item">
  30. <time class="published-date">2016-04-25</time>
  31. </div>
  32. <div class="item green" data-ref="item">
  33. <time class="published-date">2016-05-02</time>
  34. </div>
  35. <div class="item blue" data-ref="item">
  36. <time class="published-date">2016-10-07</time>
  37. </div>
  38. <div class="item pink" data-ref="item">
  39. <time class="published-date">2016-11-13</time>
  40. </div>
  41. <div class="item blue" data-ref="item">
  42. <time class="published-date">2016-12-01</time>
  43. </div>
  44. <div class="gap" data-ref="first-gap"></div>
  45. <div class="gap"></div>
  46. <div class="gap"></div>
  47. </div>
  48. <script src="../mock-api.js"></script>
  49. <script src="../mixitup.min.js"></script>
  50. <script>
  51. // Typically, our data would live in a DB and be retrieved via a JSON API, but in
  52. // this demo we will create a mock dataset using an array literal.
  53. // You will notice that this data exactly matches our pre-rendered targets.
  54. var items = [
  55. {
  56. id: 1,
  57. color: 'green',
  58. publishedDate: '2015-10-03'
  59. },
  60. {
  61. id: 2,
  62. color: 'green',
  63. publishedDate: '2015-12-22'
  64. },
  65. {
  66. id: 3,
  67. color: 'blue',
  68. publishedDate: '2016-02-15'
  69. },
  70. {
  71. id: 4,
  72. color: 'pink',
  73. publishedDate: '2016-04-25'
  74. },
  75. {
  76. id: 5,
  77. color: 'green',
  78. publishedDate: '2016-05-02'
  79. },
  80. {
  81. id: 6,
  82. color: 'blue',
  83. publishedDate: '2016-10-07'
  84. },
  85. {
  86. id: 7,
  87. color: 'pink',
  88. publishedDate: '2016-11-13'
  89. },
  90. {
  91. id: 8,
  92. color: 'blue',
  93. publishedDate: '2016-12-01'
  94. }
  95. ];
  96. // The `Api` class is not part of MixItUp and has been created to mock
  97. // the asyncronous fetching of data for this demo. To use it, we must
  98. // create an api instance, passing in our mock data to represent the
  99. // contents of a DB.
  100. var api = new Api(items);
  101. // As we'll be building and binding our own UI, we'll start with caching
  102. // references to any DOM elements we'll need to work with
  103. var controls = document.querySelector('[data-ref="controls"]');
  104. var filters = document.querySelectorAll('[data-ref="filter"]');
  105. var sorts = document.querySelectorAll('[data-ref="sort"]');
  106. var container = document.querySelector('[data-ref="container"]');
  107. // "Gap" elements are used to maintain even columns in a justified grid. See our
  108. // "MixItUp Grid Layouts" tutorial for more information.
  109. var firstGap = document.querySelector('[data-ref="first-gap"]');
  110. // We'll need to keep track of our active current filter so
  111. // that we can sort within the current filter.
  112. var activeColor = '';
  113. // Instantiate and configure the mixer
  114. var mixer = mixitup(container, {
  115. selectors: {
  116. target: '[data-ref="item"]' // Query targets with an attribute selector to keep our JS and styling classes seperate
  117. },
  118. layout: {
  119. siblingAfter: firstGap // Ensure the first "gap" element is known to mixitup incase of insertion into an empty container
  120. },
  121. load: {
  122. dataset: items // As we have pre-rendered targets, we must "prime" MixItUp with their underlying data
  123. },
  124. data: {
  125. uidKey: 'id' // Our data model must have a unique id. In this case, its key is 'id'
  126. },
  127. render: { // We must provide a target render function incase we need to render new items not in the initial dataset (not used in this demo)
  128. target: function(item) {
  129. return '<div class="item ' + item.color + '" data-ref="item"><time class="published-date">' + item.publishedDate + '</time></div>';
  130. }
  131. }
  132. });
  133. /**
  134. * A helper function to set an active styling class on an active button,
  135. * and remove it from its siblings at the same time.
  136. *
  137. * @param {HTMLElement} activeButton
  138. * @param {HTMLELement[]} siblings
  139. * @return {void}
  140. */
  141. function activateButton(activeButton, siblings) {
  142. var button;
  143. var i;
  144. for (i = 0; i < siblings.length; i++) {
  145. button = siblings[i];
  146. button.classList[button === activeButton ? 'add' : 'remove']('control-active');
  147. }
  148. }
  149. /**
  150. * A click handler to detect the type of button clicked, read off the
  151. * relevent attributes, call the API, and trigger a dataset operation.
  152. *
  153. * @param {HTMLElement} button
  154. * @return {void}
  155. */
  156. function handleButtonClick(button) {
  157. // Define default values for color, sortBy and order
  158. // incase they are not present in the clicked button
  159. var color = activeColor;
  160. var sortBy = 'id';
  161. var order = 'asc';
  162. // If button is already active, or an operation is in progress, ignore the click
  163. if (button.classList.contains('control-active') || mixer.isMixing()) return;
  164. // Else, check what type of button it is, if any
  165. if (button.matches('[data-ref="filter"]')) {
  166. // Filter button
  167. activateButton(button, filters);
  168. color = activeColor = button.getAttribute('data-color');
  169. } else if (button.matches('[data-ref="sort"]')) {
  170. // Sort button
  171. activateButton(button, sorts);
  172. sortBy = button.getAttribute('data-key');
  173. order = button.getAttribute('data-order');
  174. } else {
  175. // Not a button
  176. return;
  177. }
  178. // Now that we have our color filter and sorting order, we can fetch some data from the API.
  179. api.get({
  180. color: color,
  181. $sort_by: sortBy,
  182. $order: order
  183. })
  184. .then(function(items) {
  185. // Our api returns an array of items which we can send
  186. // straight to our mixer using the .dataset() method
  187. return mixer.dataset(items);
  188. })
  189. .then(function(state) {
  190. console.log('fetched ' + state.activeDataset.length + ' items');
  191. })
  192. .catch(console.error.bind(console));
  193. }
  194. // We can now set up a handler to listen for "click" events on our UI buttons
  195. controls.addEventListener('click', function(e) {
  196. handleButtonClick(e.target);
  197. });
  198. // Set controls the active controls on startup to match the default filter and sort
  199. activateButton(controls.querySelector('[data-color="all"]'), filters);
  200. activateButton(controls.querySelector('[data-order="asc"]'), sorts);
  201. // NB: Always remember to destroy the instance with mixer.destroy() when your
  202. // component unmounts to ensure that event handlers are unbound and the
  203. // instance can be garbage collected.
  204. </script>
  205. </body>
  206. </html>