Guest User

Untitled

a guest
Sep 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. 'use strict'
  2.  
  3. # Templates:
  4. T =
  5. frontView: '<img alt="<%= slug %>" src="<%= pic %>" />'
  6. thumbView: '<img alt="" src="<%= thumb %>" class="<%= state %>" />'
  7.  
  8.  
  9. class Picture extends Backbone.Model
  10.  
  11. defaults:
  12. slug: ''
  13. thumb: ''
  14. pic: ''
  15. state: ''
  16.  
  17. initialize: ->
  18. slug = @get('pic')
  19. try
  20. slug = /([^\.\/]+)\.[^\.]*?$/.exec(slug)[1]
  21. catch e
  22. console.error e
  23. @set 'slug', slug
  24.  
  25. getRoute: ->
  26. "#pic/#{@get('slug')}"
  27.  
  28. select: ->
  29. @set state: 'selected'
  30.  
  31. deselect: ->
  32. @set state: ''
  33.  
  34.  
  35. class Pictures extends Backbone.Collection
  36.  
  37. url: 'pictures.json'
  38.  
  39. model: Picture
  40.  
  41. select: (model) ->
  42. @selected.deselect() if @selected?
  43. @selected = model
  44. model.select()
  45. @trigger 'pictures:selected', model
  46.  
  47. selectedPicture: ->
  48. @selected
  49.  
  50.  
  51. class FrontView extends Backbone.View
  52.  
  53. el: $('#front')
  54.  
  55. template: _.template(T.frontView)
  56.  
  57. render: ->
  58. $(@el).html @template @model.selectedPicture().toJSON()
  59. @
  60.  
  61.  
  62. class ThumbView extends Backbone.View
  63.  
  64. template: _.template(T.thumbView)
  65.  
  66. events:
  67. click: "selectPicture"
  68.  
  69. tagName: 'li'
  70.  
  71. initialize: ->
  72. @model.bind 'change', @render, @
  73.  
  74. render: ->
  75. $(@el).html @template @model.toJSON()
  76. @
  77.  
  78. selectPicture: ->
  79. @trigger 'thumb:selected', @model
  80.  
  81.  
  82. class AppView extends Backbone.View
  83.  
  84. events:
  85. "click #front img": "showNextPicture"
  86.  
  87. el: $("#container")
  88.  
  89. initialize: ->
  90. @pictures = new Pictures
  91. @pictures.fetch
  92. async: false,
  93. error: (_model, resp) ->
  94. throw new Error "JSON sux?: " + resp.responseText
  95.  
  96. # Used for simple prev / next pic functions:
  97. @pictures.each (pic, index) -> pic.set 'index', index
  98.  
  99. @pictures.select @pictures.at 0
  100. @frontview = new FrontView(model: @pictures)
  101.  
  102. render: ->
  103. @frontview.render()
  104. $thumbsEl = @$('#thumbs')
  105. console.log @pictures
  106. $thumbsEl.html('')
  107. @pictures.each (t) =>
  108. view = new ThumbView(model: t).render()
  109. view.bind 'thumb:selected', @selectPicture, @
  110. $thumbsEl.append view.el
  111.  
  112. selectPicture: (pic) ->
  113. @pictures.select pic
  114. app_router.navigate pic.getRoute()
  115. @render()
  116.  
  117. _incrementPic: (deltaIndex) ->
  118. i = @pictures.selectedPicture().get 'index'
  119. l = @pictures.length
  120. (i + l + deltaIndex) % l
  121.  
  122. showNextPicture: =>
  123. @selectPicture @pictures.at @_incrementPic 1
  124.  
  125. showLastPicture: =>
  126. @selectPicture @pictures.at @_incrementPic -1
  127.  
  128. onKeyDown: (e) =>
  129. switch e.which
  130. # Left key:
  131. when 37
  132. @showLastPicture()
  133. false
  134. # Space, Right key:
  135. when 32, 39
  136. @showNextPicture()
  137. false
  138.  
  139.  
  140. class AppRouter extends Backbone.Router
  141.  
  142. routes:
  143. "pic/:slug": "showPicture"
  144. "*args": "defaultAction"
  145.  
  146. showPicture: (slug) ->
  147. pic = app.pictures.find (pic) ->
  148. slug is pic.get 'slug'
  149. app.selectPicture pic
  150.  
  151. defaultAction: (args) ->
  152. @navigate app.pictures.selectedPicture().getRoute()
  153.  
  154.  
  155. app = new AppView
  156. app.render()
  157. app_router = new AppRouter
  158. Backbone.history.start()
  159.  
  160. $(window).on
  161. keydown: app.onKeyDown
  162. # Let's not forget mobile / tablet users:
  163. swipeleft: app.showNextPicture
  164. swiperight: app.showLastPicture
  165.  
  166.  
  167. # For convenience while developing:
  168. window.app = app
Add Comment
Please, Sign In to add comment