This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
forked from gollum/gollum
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preview functionality to frontend.
- Loading branch information
Showing
9 changed files
with
274 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
lib/gollum/frontend/public/javascript/jquery.previewable_comment_form.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(function($){ | ||
$.fn.previewableCommentForm = function(options){ | ||
var opts = $.extend({}, $.fn.previewableCommentForm.defaults, options); | ||
|
||
return this.each(function(){ | ||
var wrapper = $(this) | ||
var input = wrapper.find('textarea') | ||
var output = wrapper.find('.content-body') | ||
var error = wrapper.prev('.comment-form-error') | ||
var button = wrapper.find('.form-actions button') | ||
var text = input.val() | ||
var dirty = false | ||
var request = null | ||
|
||
dirtyInputs = $.merge(wrapper.find('.preview-dirty'), input) | ||
dirtyInputs.blur(function(){ | ||
if (text != input.val()){ | ||
dirty = true | ||
text = input.val() | ||
} | ||
updatePreview() | ||
}) | ||
|
||
var updatePreview = function(force){ | ||
if (!dirty && !force) return | ||
if ($.trim(text) == ""){ // empty input | ||
output.html("<p>Nothing to preview</p>") | ||
return | ||
} | ||
output.html("<p>Loading preview…</p>") | ||
if (request) request.abort() | ||
var params = $.extend({"text": text}, opts.previewOptions) | ||
request = $.post(opts.previewUrl, params, function(data) { | ||
output.html(data) | ||
opts.onSuccess.call(output) | ||
}) | ||
} | ||
updatePreview(true) | ||
|
||
// Custom validation | ||
wrapper.closest('form').submit(function(){ | ||
error.hide() | ||
if ( $.trim(input.val()) == "" ){ | ||
error.show() | ||
return false | ||
} | ||
button.attr('disabled', 'disabled') | ||
}) | ||
}) | ||
} | ||
|
||
$.fn.previewableCommentForm.defaults = { | ||
previewUrl: "/preview", | ||
previewOptions: {}, | ||
onSuccess: function() { } | ||
} | ||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
jQuery.fn.tabs = function(){ | ||
|
||
var getAnchor = function(str) { | ||
return /#([a-z][\w.:-]*)$/i.exec(str)[1]; | ||
} | ||
|
||
var windowHash = window.location.hash.substr(1); | ||
|
||
return this.each(function(){ | ||
var selectedLink = null; | ||
var selectedContainer = null; | ||
|
||
$(this).find('li a').each(function(){ | ||
// Find & hide the container | ||
var container = $('#' + getAnchor(this.href)); | ||
if (container == []) return; | ||
container.hide(); | ||
// Setup the click handlers for the tab links | ||
$(this).click(function(){ | ||
var self = $(this) | ||
|
||
var switchTab = function(){ | ||
if (selectedContainer) selectedContainer.hide(); | ||
if (selectedLink) selectedLink.removeClass('selected'); | ||
|
||
selectedContainer = container.show(); | ||
selectedLink = self.addClass('selected'); | ||
} | ||
|
||
if (self.attr('ajax')){ | ||
self.addClass('loading') | ||
$.ajax({ | ||
url: self.attr('ajax'), | ||
success: function(data){ | ||
container.html(data) | ||
self.removeClass('loading') | ||
self[0].removeAttribute('ajax') | ||
switchTab() | ||
}, | ||
failure: function(data){ | ||
alert("An error occured, please reload the page") | ||
} | ||
}) | ||
}else{ | ||
switchTab() | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
if ($(this).hasClass('selected')) $(this).click(); | ||
}); | ||
|
||
// Try to match window.location.hash | ||
$(this).find("li a[href='#" + windowHash + "']").click() | ||
// Show one tab by default | ||
if (selectedContainer == null) $($(this).find('li a')[0]).click(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.