Commit 4add7f65 authored by Sean McGivern's avatar Sean McGivern

Fix comments on collapsed and expanded diffs

We can't save the HTML as it was on page load, because comments etc. add
content that we would lose if we kept the initial HTML. Instead, shuffle
elements around.
parent c082d92f
class @SingleDiff class @SingleDiff
WRAPPER = '<div class="diff-content diff-wrap-lines"></div>'
LOADING_HTML = '<i class="fa fa-spinner fa-spin"></i>' LOADING_HTML = '<i class="fa fa-spinner fa-spin"></i>'
ERROR_HTML = '<div class="nothing-here-block"><i class="fa fa-warning"></i> Could not load diff</div>' ERROR_HTML = '<div class="nothing-here-block"><i class="fa fa-warning"></i> Could not load diff</div>'
COLLAPSED_HTML = '<div class="nothing-here-block diff-collapsed">This diff is collapsed. Click to expand it.</div>' COLLAPSED_HTML = '<div class="nothing-here-block diff-collapsed">This diff is collapsed. Click to expand it.</div>'
...@@ -7,51 +8,47 @@ class @SingleDiff ...@@ -7,51 +8,47 @@ class @SingleDiff
constructor: (@file) -> constructor: (@file) ->
@content = $('.diff-content', @file) @content = $('.diff-content', @file)
@diffForPath = @content.find('[data-diff-for-path]').data 'diff-for-path' @diffForPath = @content.find('[data-diff-for-path]').data 'diff-for-path'
@setOpenState() @isOpen = !@diffForPath
$('.file-title > a', @file).on 'click', @toggleDiff
@enableToggleOnContent()
setOpenState: ->
if @diffForPath if @diffForPath
@isOpen = false @collapsedContent = @content
@loadingContent = $(WRAPPER).addClass('loading').html(LOADING_HTML).hide()
@content = null
@collapsedContent.after(@loadingContent)
else else
@isOpen = true @collapsedContent = $(WRAPPER).html(COLLAPSED_HTML).hide()
@contentHTML = @content.html() @content.after(@collapsedContent)
return
enableToggleOnContent: -> @collapsedContent.on 'click', @toggleDiff
@content.find('.nothing-here-block.diff-collapsed').on 'click', @toggleDiff
$('.file-title > a', @file).on 'click', @toggleDiff
toggleDiff: (e) => toggleDiff: (e) =>
e.preventDefault() e.preventDefault()
@isOpen = !@isOpen @isOpen = !@isOpen
if not @isOpen and not @hasError if not @isOpen and not @hasError
@content.html COLLAPSED_HTML @content.hide()
@enableToggleOnContent @collapsedContent.show()
return else if @content
if @contentHTML @collapsedContent.hide()
@setContentHTML() @content.show()
else else
@getContentHTML() @getContentHTML()
return
getContentHTML: -> getContentHTML: ->
@content.html(LOADING_HTML).addClass 'loading' @collapsedContent.hide()
@loadingContent.show()
$.get @diffForPath, (data) => $.get @diffForPath, (data) =>
@loadingContent.hide()
if data.html if data.html
@setContentHTML data.html @content = $(data.html)
@content.syntaxHighlight()
else else
@hasError = true @hasError = true
@content.html ERROR_HTML @content = $(ERROR_HTML)
@content.removeClass 'loading' @collapsedContent.after(@content)
return return
setContentHTML: (contentHTML) ->
@contentHTML = contentHTML if contentHTML
@content.html @contentHTML
@content.syntaxHighlight()
$.fn.singleDiff = -> $.fn.singleDiff = ->
return @each -> return @each ->
if not $.data this, 'singleDiff' if not $.data this, 'singleDiff'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment