VersionListView.coffee 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. define [
  2. "./util"
  3. "history/VersionDetailView"
  4. "libs/mustache"
  5. "libs/backbone"
  6. ], (util, VersionDetailView)->
  7. VersionListView = Backbone.View.extend
  8. template: $("#versionListTemplate").html()
  9. events:
  10. "scroll" : "loadUntilFull"
  11. initialize: ->
  12. @itemViews = []
  13. @atEndOfCollection = false
  14. self = this
  15. @collection.on "add", (model) ->
  16. self.addItem model
  17. @collection.on "reset", (collection) ->
  18. self.addItem model for model in collection.models
  19. @render()
  20. @hideLoading()
  21. render: ->
  22. @$el.html Mustache.to_html @template
  23. @$el.css
  24. overflow: "scroll"
  25. this
  26. addItem: (model) ->
  27. view = new VersionListItemView(model : model)
  28. @itemViews.push view
  29. index = @collection.indexOf(model)
  30. elementAtIndex = @$("#version-list").children()[index]
  31. view.$el.insertBefore(elementAtIndex)
  32. listShorterThanContainer: ->
  33. @$el.height() > @$("#version-list").height()
  34. atEndOfListView: ->
  35. @$el.scrollTop() + @$el.height() >= @$("#version-list").height() - 30
  36. loadUntilFull: (e, callback) ->
  37. if (@listShorterThanContainer() or @atEndOfListView()) and not @atEndOfCollection and not @loading
  38. @showLoading()
  39. @hideEmptyMessage()
  40. @collection.fetchNextBatch
  41. error: =>
  42. @hideLoading()
  43. @atEndOfCollection = true
  44. @showEmptyMessageIfCollectionEmpty()
  45. callback() if callback?
  46. success: =>
  47. @hideLoading()
  48. @loadUntilFull(e, callback)
  49. else
  50. callback() if callback?
  51. showEmptyMessageIfCollectionEmpty: ()->
  52. if @collection.isEmpty()
  53. @$(".empty-message").show()
  54. else
  55. @$(".empty-message").hide()
  56. hideEmptyMessage: () ->
  57. @$(".empty-message").hide()
  58. showLoading: ->
  59. @loading = true
  60. @$(".loading").show()
  61. hideLoading: ->
  62. @loading = false
  63. @$(".loading").hide()
  64. VersionListItemView = Backbone.View.extend
  65. tagName: "li"
  66. events:
  67. "click a": "showVersionDetail"
  68. template : $("#versionListItemTemplate").html()
  69. initialize: ->
  70. @render()
  71. render: ->
  72. @$el.html Mustache.to_html(@template, @modelView())
  73. return this
  74. modelView: ->
  75. modelView = @model.toJSON()
  76. modelView.date = util.formatDate(modelView.date)
  77. return modelView
  78. showVersionDetail: ->
  79. $("#versionListArea .active").removeClass "active"
  80. @$el.addClass "active"
  81. @model.fetch success: (model) ->
  82. view = new VersionDetailView(model: model)
  83. view.render()
  84. return VersionListView