Advertisement
Guest User

Untitled

a guest
Jul 14th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.56 KB | None | 0 0
  1. define [
  2.  
  3. 'collections/items'
  4. 'layouts/section'
  5. 'helpers/mediator'
  6. 'models/item'
  7. 'views/sections/item'
  8. 'views/sections/action'
  9. 'views/sections/cinema'
  10.  
  11. ], (Items, SectionLayout, Mediator, Item, ItemView, ActionView, CinemaView) ->
  12.  
  13. class SectionsController extends Marionette.Controller
  14.  
  15. timeout: 500
  16.  
  17. resetObjects: ->
  18. @modals = {}
  19. @collections = {}
  20. @filtredCollections = {}
  21. @filtredBoxesEls = {}
  22. @filtredEls = {}
  23.  
  24. @default =
  25. filters:
  26. section: ''
  27. search: ''
  28. action_types: []
  29. categories: []
  30.  
  31. @filters =
  32. section: ''
  33. search: ''
  34. action_types: []
  35. categories: []
  36.  
  37. initialize: (options) ->
  38. @resetObjects()
  39. @viewType = 'map'
  40.  
  41. @$el = options.$el
  42.  
  43. # Items's collections for /items/:id
  44. @collectionItems = new Items
  45.  
  46. # Items's collections for /actions/list
  47. @collectionActions = new Items
  48. @collection = new Items
  49. @collectionActions.type = 'actions'
  50.  
  51. delegateEvents: ->
  52. Mediator.on "document:click", (e) =>
  53. @sectionLayout.filterView.dropdownView.hide()
  54. @sectionLayout.filterView.categoriesView.hide()
  55.  
  56. Mediator.on "boxes:show", =>
  57. if @collection.length == 0
  58. @collection.once 'sync', => @sectionLayout.boxesView.onShow()
  59. else
  60. @sectionLayout.boxesView.onShow()
  61.  
  62. Mediator.on "section:filter:search", (str) =>
  63. @search(str)
  64.  
  65. $(document).scroll (e) =>
  66. if $('body').height()+$('body').scrollTop() >= $('body')[0].scrollHeight-200
  67. @sectionLayout.boxesView.itemsCount+=10
  68. @update()
  69.  
  70. Mediator.on "section:filter:category", (category) =>
  71. added = (i = _.indexOf(@filters.categories, category.title)) >= 0
  72.  
  73. if category.active
  74. @filters.categories.push category.title
  75. else
  76. @filters.categories[i] = null
  77.  
  78. @filters.categories = _.compact(@filters.categories)
  79.  
  80. @update()
  81.  
  82. Mediator.on "section:filter:action", (filters) =>
  83. @filters.action_types = filters
  84. @update()
  85.  
  86. Mediator.on "section:filter:clear", =>
  87. @clear()
  88. @update()
  89.  
  90. Mediator.on "section:switch:view", =>
  91. if @sectionLayout.$el.hasClass('map')
  92. Mediator.trigger 'navigate', "/#{@sectionLayout.section}"
  93. else
  94. Mediator.trigger 'navigate', "/#{@sectionLayout.section}/map"
  95.  
  96. Mediator.on "item:choose", (id) => @onChooseItem(id)
  97. Mediator.on "cinema:choose", => @openCinemaModal()
  98.  
  99. Mediator.on "item:close", (view) => @onCloseItem(view)
  100.  
  101. clear: ->
  102. @currendItemModal.hide() if not _.isUndefined(@currendItemModal)
  103. @filters = _.clone(@default.filters)
  104.  
  105. @filters.categories = []
  106. @filters.action_types = []
  107.  
  108. @sectionLayout.filterView.clear()
  109.  
  110. @sectionLayout.filterView.$el
  111. .find('.js-filter__item')
  112. .removeClass('is-active')
  113.  
  114. @sectionLayout.filterView.$el
  115. .find('.filter-droplist__item')
  116. .removeClass('active')
  117.  
  118. search: (src) =>
  119. clearTimeout @timerSearch if @timerSearch
  120. @filters.search = src
  121. @timerSearch = setTimeout((=> @update() ), @timeout )
  122.  
  123. go: (section, uid, view) ->
  124.  
  125. if _.isEmpty @sectionLayout
  126. @sectionLayout = new SectionLayout()
  127. @sectionLayout.setSection(section)
  128. @sectionLayout.render()
  129. @dropdownView = @sectionLayout.filterView.dropdownView
  130. @delegateEvents()
  131.  
  132. @sectionLayout.setSection(section)
  133.  
  134. if not _.isEmpty(view)
  135. @sectionLayout.goToMap()
  136. else
  137. @sectionLayout.goToBoxes()
  138.  
  139. _.defer =>
  140. if _.isUndefined(uid) or _.isNull(uid)
  141. @goToSection(section)
  142. else
  143. @goToItem(section, uid)
  144.  
  145. @sectionLayout
  146.  
  147. goToItem: (section, uid) ->
  148.  
  149. id = arr[0] if (arr = uid.split('-')).length > 0
  150. collection = if section == 'actions' then @collectionActions else @collectionItems
  151. model = collection.get(id)
  152.  
  153. View = if section == 'actions' then ActionView else ItemView
  154. @modals[id] ||= if _.isUndefined(model) then new View(id: id) else new View(model: model)
  155.  
  156. @modals[id].onRender = =>
  157. collection.add @modals[id].model
  158. @openItemModal(@modals[id])
  159.  
  160. @modals[id].render()
  161.  
  162. goToSection: (section) ->
  163. if section == 'kino'
  164. @sectionLayout.goToMap()
  165. @openCinemaModal()
  166.  
  167. @filterBySection(section)
  168.  
  169. openItemModal: (view) ->
  170. @currentItemModal = view
  171. @dropdownView.hide()
  172.  
  173. if _.isEmpty @currendItemModal
  174. view.open()
  175. else
  176. @currendItemModal.close(=>view.open())
  177.  
  178. @currendItemModal = view
  179. model = view.model
  180.  
  181. floor = _.first(model.get('floor'))
  182. @checkFloor(floor)
  183.  
  184. if model.get('map_x') and model.get('map_y')
  185. Map.map.setView([model.get('map_x'), model.get('map_y')-0.2], 11, {animate: true})
  186. if _.has Map.popups, model.get('id')
  187. latlng = [model.get('map_x'), model.get('map_y')]
  188. if not _.isUndefined Map.popups[model.get('id')]._map
  189. Map.popups[model.get('id')].openPopup(latlng)
  190.  
  191. checkFloor: (floor) ->
  192. $(".leaflet-control-layers-base input:eq(#{floor-1})").trigger('click')
  193.  
  194. renderItems: (collection) ->
  195. @dropdownView.render(collection)
  196.  
  197. onCloseItem: (view) ->
  198. Mediator.trigger 'navigate', "/#{@sectionLayout.section}/map"
  199.  
  200. model = view.model
  201. @sectionLayout.filterView.clear()
  202. @sectionLayout.filterView.dropdownView.hide()
  203. @sectionLayout.filterView.categoriesView.hide()
  204. if _.has(Map.popups, model.get('id')) and
  205. not _.isUndefined(Map.popups[model.get('id')]) and
  206. not _.isUndefined Map.popups[model.get('id')]._map
  207. Map.popups[model.get('id')].closePopup()
  208.  
  209. Map.clearFocus()
  210.  
  211. onChooseItem: (id) ->
  212. model = @collection.get(id)
  213. if _.isUndefined(model)
  214. model = new Item(id: id)
  215. model.once 'sync', => @navigateToModel(model)
  216. model.fetch()
  217. else
  218. @navigateToModel(model)
  219.  
  220. navigateToModel: (model) ->
  221. if _.indexOf(['shops', 'food', 'entertain', 'kids', 'service'], model.get('section')) < 0
  222. url = "/actions/map/#{model.get('id')}-#{model.get('title')}"
  223. else
  224. url = "/#{model.get('section')}/map/#{model.get('id')}-#{model.get('title')}"
  225. Mediator.trigger( 'navigate', url )
  226.  
  227. filterBySection: (section) ->
  228. floor = 1
  229. floor = 4 if section == 'food' or section == 'kino'
  230.  
  231. if _.isUndefined(@sectionLayout.mapView)
  232. Mediator.once 'map:render', => @checkFloor(floor)
  233. else
  234. @checkFloor(floor)
  235.  
  236. @clear()
  237. @sectionLayout.setSection(section)
  238.  
  239. @default.filters.section = section
  240. @filters.section = section
  241.  
  242. @dropdownView.hide()
  243. @currentSection = section
  244.  
  245. Map.clearFocus() unless _.isUndefined(Map.map)
  246.  
  247. if _.isUndefined(@collections[section])
  248. @collections[section] = new Items()
  249. @collections[section].type = section
  250.  
  251. @collection = @collections[section]
  252.  
  253. @collection.on 'sync', =>
  254. @updateBoxes(@collection)
  255. @updateCategories(@collection)
  256. @update()
  257.  
  258. @collection.fetch()
  259. else
  260. @collection = @collections[section]
  261. @updateBoxes(@collection)
  262. @updateCategories(@collection)
  263. @update()
  264.  
  265. filterByTitle: (str, collection) ->
  266. collection.filter(
  267. (m) -> $($.parseHTML(m.get('title'))).text().toLowerCase().search(str.toLowerCase())>=0
  268. )
  269.  
  270. filterByAction: (filters, collection) ->
  271. collection.filter(
  272. (m) ->(_.intersection(m.get('action_types'), filters)).length > 0
  273. )
  274.  
  275. filterByCategory: (categories, collection) ->
  276. collection.filter(
  277. (m) -> (_.indexOf(categories, m.get('category'))) >= 0
  278. )
  279.  
  280. update: ->
  281. return if _.isUndefined(@collection) or @collection.length == 0
  282.  
  283. ##
  284. # Isotope filtering
  285. ##
  286.  
  287. filters = []
  288.  
  289. action_types = _.clone(@filters.action_types)
  290. categories = _.clone(@filters.categories)
  291.  
  292. if action_types.length == 0 and categories.length == 0
  293. filter_class = @sectionLayout.boxesView.getPaginationClass()
  294. else
  295. if action_types.length == 0 or categories.length == 0
  296. _filters = if action_types.length == 0 then categories else action_types
  297. for filter in _filters
  298. do (filter) -> filters.push(".#{filter}")
  299. filter_class = filters.join(', ')
  300. else
  301. for a in action_types
  302. do (a) ->
  303. for c in categories
  304. do (c) -> filters.push ".#{a}.#{c}"
  305. filter_class = filters.join(', ')
  306.  
  307. str = @filters.search.trim()
  308.  
  309. @sectionLayout.boxesView.filtered(filter_class, str)
  310.  
  311. @sectionLayout.boxesView.loadImage()
  312. @$el.imagesLoaded => @sectionLayout.boxesView.$el.isotope()
  313.  
  314. collection = @collection
  315.  
  316. key = JSON.stringify(@filters)
  317.  
  318. if _.has @filtredCollections, key
  319. @updateFloorControls(@filtredCollections[key])
  320. return @renderItems @filtredCollections[key]
  321.  
  322. if not _.isEmpty(@filters.search.trim())
  323. models = @filterByTitle(@filters.search.trim(), collection)
  324. collection = new Backbone.Collection models
  325.  
  326. if not _.isEmpty(@filters.action_types)
  327. models = @filterByAction(@filters.action_types, collection)
  328. collection = new Backbone.Collection models
  329.  
  330. if not _.isEmpty(@filters.categories)
  331. models = @filterByCategory(@filters.categories, collection)
  332. collection = new Backbone.Collection models
  333.  
  334. @filtredCollections[key] = collection
  335. @updateFloorControls(collection)
  336.  
  337. @renderItems collection
  338.  
  339. updateFloorControls: (collection) ->
  340. floor_obj_counts = _.groupBy(_.flatten(collection.pluck('floor')))
  341. $(".leaflet-control-layers-base label").removeClass('show-counter')
  342. _.each floor_obj_counts, (arr, floor) ->
  343. $(".leaflet-control-layers-base label:nth-child(#{floor})")
  344. .addClass('show-counter')
  345. .attr('data-content', arr.length)
  346.  
  347. updateBoxes: (collection) ->
  348. if collection.type == 'actions'
  349.  
  350. if @collectionActions.length > 0 and collection.length <= @collectionActions.length
  351. collection = @collectionActions
  352. else
  353. models = []
  354. collection.each (model) =>
  355. return if _.isUndefined(childs = model.get('childs'))
  356. values = _.values(childs)
  357. for v in values
  358. do (v) ->
  359. v.floor = model.get 'floor'
  360. v.parent = model
  361. models.push values
  362. @collectionActions.reset(_.flatten(models), {parse:true})
  363. collection = @collectionActions
  364.  
  365. @sectionLayout.boxesView.render(collection)
  366. @sectionLayout.boxesView.$el.ready(
  367. =>
  368. @$el.find('#box').html @sectionLayout.boxesView.$el
  369. @$el.find('#box').ready( => @sectionLayout.boxesView.onShow() )
  370. )
  371.  
  372. updateCategories: (collection) ->
  373. categories = {}
  374. collection.each (m) =>
  375. return if _.isEmpty m.get('category_title')
  376. categories[m.get('category')] = {
  377. category: m.get('category')
  378. category_title: m.get('category_title')
  379. }
  380.  
  381. @sectionLayout.filterView.categoriesView.collection.reset(_.values(categories))
  382. @sectionLayout.filterView.categoriesView.render()
  383.  
  384. ##
  385. #
  386. # TODO Статичная страница
  387. #
  388. openCinemaModal: ->
  389. setTimeout ( =>
  390. @modals['cinema'] ||= new CinemaView()
  391. @modals['cinema'].onRender = =>
  392. @openItemModal(@modals['cinema'])
  393. @modals['cinema'].render()
  394. ), 500
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement