overleaf/services/web/public/coffee/track-changes/ChangeListView.coffee

199 lines
4.6 KiB
CoffeeScript
Raw Normal View History

define [
2014-03-07 06:24:11 -05:00
"moment"
"libs/mustache"
"libs/backbone"
2014-03-07 06:24:11 -05:00
], (moment)->
ChangeListView = Backbone.View.extend
template: $("#changeListTemplate").html()
events:
"scroll" : "loadUntilFull"
2014-03-06 09:08:12 -05:00
initialize: () ->
@itemViews = []
@atEndOfCollection = false
self = this
@collection.on "add", (model) ->
self.addItem model
@collection.on "reset", (collection) ->
self.addItem model for model in collection.models
2014-03-06 11:32:18 -05:00
@selectedFromIndex = 0
@selectedToIndex = 0
@render()
@hideLoading()
render: ->
@$el.html Mustache.to_html @template
@$el.css
overflow: "scroll"
this
addItem: (model) ->
2014-03-06 11:32:18 -05:00
index = @collection.indexOf(model)
view = new ChangeListItemView(model : model)
@itemViews.push view
elementAtIndex = @$(".change-list").children()[index]
view.$el.insertBefore(elementAtIndex)
2014-03-06 11:32:18 -05:00
2014-03-06 09:08:12 -05:00
view.on "click", (e, v) =>
2014-03-06 11:32:18 -05:00
@selectedToIndex = index
@selectedFromIndex = index
@resetAllSelectors()
@triggerChangeDiff()
view.on "selected:to", (e, v) =>
@selectedToIndex = index
@resetAllSelectors()
@triggerChangeDiff()
view.on "selected:from", (e, v) =>
@selectedFromIndex = index
@resetAllSelectors()
@triggerChangeDiff()
view.resetSelector(index, @selectedFromIndex, @selectedToIndex)
resetAllSelectors: () ->
for view, i in @itemViews
view.resetSelector(i, @selectedFromIndex, @selectedToIndex)
triggerChangeDiff: () ->
@trigger "change_diff", @collection.models[@selectedFromIndex], @collection.models[@selectedToIndex]
listShorterThanContainer: ->
@$el.height() > @$(".change-list").height()
atEndOfListView: ->
@$el.scrollTop() + @$el.height() >= @$(".change-list").height() - 30
loadUntilFull: (e, callback) ->
if (@listShorterThanContainer() or @atEndOfListView()) and not @atEndOfCollection and not @loading
@showLoading()
@hideEmptyMessage()
@collection.fetchNextBatch
error: =>
@hideLoading()
@showEmptyMessageIfCollectionEmpty()
callback() if callback?
success: (collection, response) =>
@hideLoading()
if response.updates.length == @collection.batchSize
@loadUntilFull(e, callback)
else
@atEndOfCollection = true
@showEmptyMessageIfCollectionEmpty()
callback() if callback?
else
callback() if callback?
showEmptyMessageIfCollectionEmpty: ()->
if @collection.isEmpty()
@$(".empty-message").show()
else
@$(".empty-message").hide()
hideEmptyMessage: () ->
@$(".empty-message").hide()
showLoading: ->
@loading = true
@$(".loading-changes").show()
hideLoading: ->
@loading = false
@$(".loading-changes").hide()
ChangeListItemView = Backbone.View.extend
tagName: "li"
2014-03-06 09:08:12 -05:00
events:
2014-03-06 11:32:18 -05:00
"click .change-description" : "onClick"
"click .change-selector-from" : "onFromSelectorClick"
"click .change-selector-to" : "onToSelectorClick"
template : $("#changeListItemTemplate").html()
initialize: ->
@render()
render: ->
@$el.html Mustache.to_html(@template, @modelView())
return this
modelView: ->
2014-03-07 06:24:11 -05:00
modelView = {
hue: @model.get("user").hue()
2014-03-07 06:24:11 -05:00
date: moment(parseInt(@model.get("end_ts"), 10)).calendar()
name: @model.get("user").name()
2014-03-07 06:24:11 -05:00
}
# modelView.start_ts = util.formatDate(modelView.start_ts)
# modelView.end_ts = util.formatDate(modelView.end_ts)
return modelView
2014-03-06 09:08:12 -05:00
onClick: (e) ->
e.preventDefault()
@trigger "click", e, @
2014-03-06 11:32:18 -05:00
onToSelectorClick: (e) ->
@trigger "selected:to", e, @
onFromSelectorClick: (e) ->
@trigger "selected:from", e, @
isSelectedFrom: () ->
@$(".change-selector-from").is(":checked")
isSelectedTo: () ->
@$(".change-selector-to").is(":checked")
hideFromSelector: () ->
@$(".change-selector-from").hide()
showFromSelector: () ->
@$(".change-selector-from").show()
hideToSelector: () ->
@$(".change-selector-to").hide()
showToSelector: () ->
@$(".change-selector-to").show()
setFromChecked: (checked) ->
@$(".change-selector-from").prop("checked", checked)
setToChecked: (checked) ->
@$(".change-selector-to").prop("checked", checked)
setSelected: () ->
@$el.addClass("selected")
setUnselected: () ->
@$el.removeClass("selected")
resetSelector: (myIndex, selectedFromIndex, selectedToIndex) ->
if myIndex >= selectedToIndex
@showFromSelector()
else
@hideFromSelector()
if myIndex <= selectedFromIndex
@showToSelector()
else
@hideToSelector()
if selectedToIndex <= myIndex <= selectedFromIndex
@setSelected()
else
@setUnselected()
@setFromChecked(myIndex == selectedFromIndex)
@setToChecked(myIndex == selectedToIndex)
return ChangeListView