Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. $ = require('jquery')
  2. _ = require('lodash')
  3. Backbone = require('backbone')
  4. Backbone.$ = $;
  5. Facetr = require('backbone.facetr')
  6. ProductModel = require('../models/ProductModel')
  7. PriceFilterSlider = require('./PriceFilterSlider')
  8. ProductsCollection = require('../collections/ProductsCollection')
  9. FilterFacetsController = require('./FilterFacetsController')
  10. require('lazyimg')
  11.  
  12. class ProductFilterController extends Backbone.View
  13.  
  14. el: null
  15.  
  16. defaults:
  17. sidebarActiveClass: 'active'
  18. sidebarElement: ''
  19. productElem: '.js-product'
  20. facetElem: ''
  21.  
  22. events:
  23. "click .js-filter-toggler": "toggleSidebar"
  24.  
  25. initialize: (options) ->
  26. @options = _.extend({}, @defaults, options)
  27.  
  28. # Initialize Products Collection
  29. @productsCollection = new ProductsCollection()
  30. @cgCollection = new ProductsCollection()
  31. @sizeCollection = new ProductsCollection()
  32. @facetsController = new FilterFacetsController()
  33.  
  34. @priceSlider = new PriceFilterSlider({
  35. el: '.js-price-slider'
  36. })
  37.  
  38. # Listen for FilterStateModel change
  39. @listenTo Backbone, "filter:set", @setFilter
  40. @listenTo Backbone, "filter:unset", @resetFilter
  41. @listenTo @priceSlider, 'priceSlider:stop', @filterByPrice
  42.  
  43. # Get Products from DOM
  44. @getProducts()
  45.  
  46. # Get All the Products from DOM
  47. getProducts: ->
  48. $(@options.productElem).each (index, element) =>
  49. @addProductToCollection(index, element)
  50.  
  51. # Create Product model and add to Products Collection
  52. addProductToCollection: (index, element) ->
  53. data = $(element).data('productattrs')
  54. product = new ProductModel({
  55. id: parseInt(data.id)
  56. index: parseInt(index)
  57. published: parseInt(data.published)
  58. sold: parseInt(data.sold)
  59. price: parseInt(data.price)
  60. size: _.values(data.size)
  61. cg: _.values(data.cg)
  62. })
  63.  
  64. # Initialise secondary collections for determining availability
  65. @cgCollection.add(product)
  66. # We need to add an empty facet for colours to get the list of available. We don't filter by this, but need it...
  67. Facetr(@cgCollection).facet('cg')
  68. @sizeCollection.add(product)
  69. Facetr(@sizeCollection).facet('size')
  70. @productsCollection.add(product)
  71.  
  72. filterStateModelChange: (model) ->
  73. for key, val of model.attributes
  74. for filterVal in val
  75. Facetr(@productsCollection).facet(key).value(filterVal)
  76. @showFiltered()
  77.  
  78. setFilter: (key, value) ->
  79. Facetr(@productsCollection).facet(key).value(value)
  80. if key != 'size'
  81. Facetr(@sizeCollection).facet(key).value(value)
  82. if key != 'cg'
  83. Facetr(@cgCollection).facet(key).value(value)
  84. @showAvailability()
  85. @showFiltered()
  86.  
  87. resetFilter: (key, filterVal) ->
  88. Facetr(@productsCollection).facet(key).removeValue(filterVal)
  89. if key != 'size'
  90. Facetr(@sizeCollection).facet(key).removeValue(filterVal)
  91. if key != 'cg'
  92. Facetr(@cgCollection).facet(key).removeValue(filterVal)
  93. @showAvailability()
  94. @showFiltered()
  95.  
  96. showAvailability: () ->
  97. console.log("SHOWING AVAILABILITY");
  98. filterData = Facetr(@cgCollection).toJSON()
  99. console.log(filterData)
  100. found = false
  101. for item in filterData
  102. if item.data.name == 'cg'
  103. found = true
  104. values = item.values
  105.  
  106. for obj in values
  107. @setFilterAvailability('cg', obj)
  108. # Not in the list? then it's not filtered, so show everything
  109. if !found
  110. @makeFacetFullyAvailable('cg')
  111.  
  112. filterData = Facetr(@sizeCollection).toJSON()
  113. console.log(filterData)
  114. found = false
  115. for item in filterData
  116. if item.data.name == 'size'
  117. found = true
  118. values = item.values
  119.  
  120. for obj in values
  121. @setFilterAvailability('size', obj)
  122. # Not in the list? then it's not filtered, so show everything
  123. if !found
  124. @makeFacetFullyAvailable('size')
  125.  
  126. makeFacetFullyAvailable: (facetName) ->
  127. console.log("Making " + facetName + " fully available")
  128. $("input[data-filter=#{facetName}]").each(input) ->
  129. input.parent().css("opacity", 1.0)
  130.  
  131. setFilterAvailability: (inputName, obj) ->
  132. value = obj.value
  133. if value
  134. input = $("input[data-filter=#{inputName}][data-val='#{value}']")
  135. if obj.activeCount == 0
  136. input.parent().css("opacity", 0.5)
  137. else
  138. input.parent().css("opacity", 1.0)
  139.  
  140. showFiltered: () ->
  141. $(@options.productElem).hide()
  142. for product in @productsCollection.models
  143. productBox = document.getElementById('product-' + product.id).style.display = 'inline-block'
  144. $(".lazy-img").lazyload()
  145.  
  146. filterByPrice: (min, max) ->
  147. Facetr(@productsCollection).addFilter 'price', (model) =>
  148. model.get('price') >= min && model.get('price') < max
  149. # TODO: THIS DOESN'T WORK
  150. Facetr(@cgCollection).addFilter 'price', (model) =>
  151. model.get('price') >= min && model.get('price') < max
  152. Facetr(@sizeCollection).addFilter 'price', (model) =>
  153. model.get('price') >= min && model.get('price') < max
  154. @showFiltered()
  155.  
  156. # Toggle Sidebar Animation
  157. toggleSidebar: (e) ->
  158. e.preventDefault()
  159. $('.products-filter-outer').toggleClass('is-opened')
  160. $('body').toggleClass('filter--is-opened')
  161.  
  162. module.exports = ProductFilterController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement