Skip to content

Commit

Permalink
Improved client side link validation with ajax page test
Browse files Browse the repository at this point in the history
Fixes #1929 and #1927
  • Loading branch information
keram authored and parndt committed Sep 6, 2012
1 parent c582873 commit 581204a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 58 deletions.
41 changes: 24 additions & 17 deletions core/app/assets/javascripts/refinery/admin.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -296,47 +296,54 @@ init_tooltips = function(args){
};

var link_tester = {
initialised: false
, init: function(test_url, test_email) {

if (!this.initialised) {
this.test_url = test_url;
this.test_email = test_email;
this.initialised = true;
}
},
email_re : new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i),
url_re : new RegExp(/^(https?|ftp):\/\/(-\.)?([^\s/?\.#-]+\.?)+(\/[^\s]*)?$/i),
page_re : new RegExp('^(https?:\/\/' + document.location.host + '|\/[a-z0-9]+)'),

email: function(value, callback) {
if (value != "") {
$.getJSON(link_tester.test_email, {email: value}, function(data){
callback(data.result == 'success');
});
callback(link_tester.email_re.test(value));
}
},

url: function(value, callback) {
if (value != "") {
$.getJSON(link_tester.test_url, {'url': value}, function(data){
callback(data.result == 'success');
});
if (link_tester.page_re.test(value)) {
link_tester.page(value, callback);
} else {
callback(link_tester.url_re.test(value));
}
}
},

page: function(value, callback) {
var valid = false;
$.ajax({
url: value,
timeout: 5000,
success: function() {
valid = true;
},
complete: function () {
callback(valid);
}
});
},

validate_textbox: function(validation_method, textbox_id, callback) {
var icon = '';
var loader_img = $("<img id='" + textbox_id.replace('#','') + "_test_loader' src='<%= asset_path 'refinery/ajax-loader.gif' %>' alt='Testing...' style='display: none;'/>");
var result_span = $("<span id='" + textbox_id.replace('#','') + "_test_result'></span>");

loader_img.insertAfter($(textbox_id));
result_span.insertAfter(loader_img);

$(textbox_id).bind('paste blur',function(){
$(textbox_id).stop(true); // Clear the current queue; if we weren't checking yet, cancel it.
$(textbox_id + '_test_loader').hide();
$(textbox_id + '_test_result').hide();
$(textbox_id + '_test_result').removeClass('success_icon').removeClass('failure_icon');

if (this.value != "" && this.value[0] != "/") {
if (this.value != "") {
// Wait 300ms before checking.
$(textbox_id).delay(300).queue(function () {
$(textbox_id + '_test_loader').show();
Expand Down
34 changes: 0 additions & 34 deletions pages/app/controllers/refinery/admin/pages_dialogs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,6 @@ def link_to
end
end

def test_url
result = 'failure'
begin
timeout(5) do
unless params[:url].blank?
url = URI.parse(params[:url])
if url.host.nil? && params[:url].start_with?('/')
url.host = URI.parse(request.url).host
end

result = 'success' if [Net::HTTPSuccess, Net::HTTPRedirection].
include? Net::HTTP.get_response(url)
end
end

rescue
# the result is already set to failure.
end

render :json => {:result => result}
end

def test_email
if params[:email].present?
valid = params[:email] =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

render :json => if valid
{:result => 'success'}
else
{:result => 'failure'}
end
end
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
<% content_for :javascripts do %>
<script>
$(document).ready(function(){
link_tester.init('<%= refinery.test_url_admin_pages_dialogs_path %>',
'<%= refinery.test_email_admin_pages_dialogs_path %>');

link_tester.validate_url_textbox("#page_link_url")
});
</script>
Expand Down
2 changes: 0 additions & 2 deletions pages/app/views/refinery/admin/pages_dialogs/link_to.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@
<% content_for :javascripts do %>
<script>
$(document).ready(function(){
link_tester.init('<%= refinery.test_url_admin_pages_dialogs_path %>',
'<%= refinery.test_email_admin_pages_dialogs_path %>');
link_dialog.init();

$('.link_list a').click(function(e){
Expand Down
2 changes: 0 additions & 2 deletions pages/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
resources :pages_dialogs, :only => [] do
collection do
get :link_to
get :test_url
get :test_email
end
end

Expand Down

0 comments on commit 581204a

Please sign in to comment.