FileDiffView.coffee 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. define [
  2. "libs/mustache"
  3. "libs/backbone"
  4. ], ()->
  5. FileDiffView = Backbone.View.extend
  6. className: "fileDiffView"
  7. template: $("#fileDiffTemplate").html()
  8. events:
  9. "click .nav .raw" : "loadRawFile"
  10. render: ->
  11. $(@el).html Mustache.to_html(@template, @modelView())
  12. return this
  13. modelView: ->
  14. fileView = @model.toJSON()
  15. fileView.id = @model.cid
  16. fileView.url = @model.url()
  17. fileView.headerClass = "updated"
  18. fileView.deleted = false
  19. if fileView.sections? and fileView.sections.length > 0
  20. fileView.diff = true
  21. for section in fileView.sections
  22. currentOldLine = section.old_start_line
  23. currentNewLine = section.new_start_line
  24. for line in section.lines
  25. line.old_number = currentOldLine
  26. line.new_number = currentNewLine
  27. if line.type == "added"
  28. currentNewLine += 1
  29. line.added = true
  30. if line.type == "removed"
  31. currentOldLine += 1
  32. line.removed = true
  33. if line.type == "unchanged"
  34. currentNewLine += 1
  35. currentOldLine += 1
  36. line.unchanged = true
  37. if fileView.type == "renamed"
  38. fileView.moved = true
  39. fileView.newPath = fileView.path
  40. fileView.path = fileView.oldPath
  41. if fileView.type == "created"
  42. fileView.headerClass = "created"
  43. if fileView.type == "deleted"
  44. fileView.headerClass = "deleted"
  45. fileView.deleted = true
  46. return fileView
  47. loadRawFile: ->
  48. unless @loadedRawFile
  49. view = this
  50. @model.fetch
  51. success: (model) ->
  52. view.$(".rawFileContent").text(model.get("content"))
  53. @loadedRawFile = true