Skip to content
This repository has been archived by the owner on Dec 9, 2018. It is now read-only.

Commit

Permalink
Upgrade javascript helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
frodenas committed Oct 11, 2012
1 parent 93bfea9 commit 854ce06
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 229 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/chosen.jquery.min.js 100644 → 100755

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/excanvas.min.js 100644 → 100755

Large diffs are not rendered by default.

280 changes: 142 additions & 138 deletions app/assets/javascripts/jquery.dataTables.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/assets/javascripts/jquery.flot.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/jquery.flot.pie.min.js

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions app/assets/javascripts/jquery.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/assets/javascripts/modernizr.js

Large diffs are not rendered by default.

208 changes: 132 additions & 76 deletions app/assets/javascripts/rails.js
Expand Up @@ -45,6 +45,17 @@
* });
*/

// Cut down on the number if issues from people inadvertently including jquery_ujs twice
// by detecting and raising an error when it happens.
var alreadyInitialized = function() {
var events = $._data(document, 'events');
return events && events.click && $.grep(events.click, function(e) { return e.namespace === 'rails'; }).length;
}

if ( alreadyInitialized() ) {
$.error('jquery-ujs has already been loaded!');
}

// Shorthand to make it a little easier to call public rails functions from within rails.js
var rails;

Expand All @@ -59,7 +70,7 @@
formSubmitSelector: 'form',

// Form input elements bound by jquery-ujs
formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not(button[type])',
formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',

// Form input elements disabled during form submission
disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
Expand Down Expand Up @@ -99,14 +110,20 @@
return $.ajax(options);
},

// Default way to get an element's href. May be overridden at $.rails.href.
href: function(element) {
return element.attr('href');
},

// Submits "remote" forms and links with ajax
handleRemote: function(element) {
var method, url, data,
crossDomain = element.data('cross-domain') || null,
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType),
options;
var method, url, data, elCrossDomain, crossDomain, withCredentials, dataType, options;

if (rails.fire(element, 'ajax:before')) {
elCrossDomain = element.data('cross-domain');
crossDomain = elCrossDomain === undefined ? null : elCrossDomain;
withCredentials = element.data('with-credentials') || null;
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);

if (element.is('form')) {
method = element.attr('method');
Expand All @@ -125,12 +142,12 @@
if (element.data('params')) data = data + "&" + element.data('params');
} else {
method = element.data('method');
url = element.attr('href');
url = rails.href(element);
data = element.data('params') || null;
}

options = {
type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
type: method || 'GET', data: data, dataType: dataType,
// stopping the "ajax:beforeSend" event will cancel the ajax request
beforeSend: function(xhr, settings) {
if (settings.dataType === undefined) {
Expand All @@ -146,12 +163,18 @@
},
error: function(xhr, status, error) {
element.trigger('ajax:error', [xhr, status, error]);
}
},
xhrFields: {
withCredentials: withCredentials
},
crossDomain: crossDomain
};
// Only pass url to `ajax` options if not blank
if (url) { options.url = url; }

return rails.ajax(options);
var jqxhr = rails.ajax(options);
element.trigger('ajax:send', jqxhr);
return jqxhr;
} else {
return false;
}
Expand All @@ -160,7 +183,7 @@
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
handleMethod: function(link) {
var href = link.attr('href'),
var href = rails.href(link),
method = link.data('method'),
target = link.attr('target'),
csrf_token = $('meta[name=csrf-token]').attr('content'),
Expand Down Expand Up @@ -228,12 +251,21 @@

// Helper function which checks for blank inputs in a form that match the specified CSS selector
blankInputs: function(form, specifiedSelector, nonBlank) {
var inputs = $(), input,
selector = specifiedSelector || 'input,textarea';
form.find(selector).each(function() {
var inputs = $(), input, valueToCheck,
selector = specifiedSelector || 'input,textarea',
allInputs = form.find(selector);

allInputs.each(function() {
input = $(this);
// Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
if (nonBlank ? input.val() : !input.val()) {
valueToCheck = input.is(':checkbox,:radio') ? input.is(':checked') : input.val();
// If nonBlank and valueToCheck are both truthy, or nonBlank and valueToCheck are both falsey
if (!valueToCheck === !nonBlank) {

// Don't count unchecked required radio if other radio with same name is checked
if (input.is(':radio') && allInputs.filter('input:radio:checked[name="' + input.attr('name') + '"]').length) {
return true; // Skip to next input
}

inputs = inputs.add(input);
}
});
Expand Down Expand Up @@ -270,7 +302,7 @@
element.data('ujs:enable-with', element.html()); // store enabled state
element.html(element.data('disable-with')); // set to disabled state
element.bind('click.railsDisable', function(e) { // prevent further clicking
return rails.stopEverything(e)
return rails.stopEverything(e);
});
},

Expand All @@ -287,87 +319,111 @@

};

$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
if (rails.fire($(document), 'rails:attachBindings')) {

$(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
rails.enableElement($(this));
});
$.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});

$(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
var link = $(this), method = link.data('method'), data = link.data('params');
if (!rails.allowAction(link)) return rails.stopEverything(e);
$(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
rails.enableElement($(this));
});

if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
$(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
var link = $(this), method = link.data('method'), data = link.data('params');
if (!rails.allowAction(link)) return rails.stopEverything(e);

if (link.data('remote') !== undefined) {
if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
if (link.is(rails.linkDisableSelector)) rails.disableElement(link);

if (rails.handleRemote(link) === false) { rails.enableElement(link); }
return false;
if (link.data('remote') !== undefined) {
if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }

} else if (link.data('method')) {
rails.handleMethod(link);
return false;
}
});
var handleRemote = rails.handleRemote(link);
// response from rails.handleRemote() will either be false or a deferred object promise.
if (handleRemote === false) {
rails.enableElement(link);
} else {
handleRemote.error( function() { rails.enableElement(link); } );
}
return false;

$(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
var link = $(this);
if (!rails.allowAction(link)) return rails.stopEverything(e);
} else if (link.data('method')) {
rails.handleMethod(link);
return false;
}
});

rails.handleRemote(link);
return false;
});
$(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
var link = $(this);
if (!rails.allowAction(link)) return rails.stopEverything(e);

$(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
var form = $(this),
remote = form.data('remote') !== undefined,
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
rails.handleRemote(link);
return false;
});

if (!rails.allowAction(form)) return rails.stopEverything(e);
$(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
var form = $(this),
remote = form.data('remote') !== undefined,
blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);

// skip other logic when required values are missing or file upload is present
if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return rails.stopEverything(e);
}
if (!rails.allowAction(form)) return rails.stopEverything(e);

if (remote) {
if (nonBlankFileInputs) {
return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
// skip other logic when required values are missing or file upload is present
if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
return rails.stopEverything(e);
}

// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
if (!$.support.submitBubbles && $().jquery < '1.7' && rails.callFormSubmitBindings(form, e) === false) return rails.stopEverything(e);
if (remote) {
if (nonBlankFileInputs) {
// slight timeout so that the submit button gets properly serialized
// (make it easy for event handler to serialize form without disabled values)
setTimeout(function(){ rails.disableFormElements(form); }, 13);
var aborted = rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);

rails.handleRemote(form);
return false;
// re-enable form elements if event bindings return false (canceling normal form submission)
if (!aborted) { setTimeout(function(){ rails.enableFormElements(form); }, 13); }

} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ rails.disableFormElements(form); }, 13);
}
});
return aborted;
}

// If browser does not support submit bubbling, then this live-binding will be called before direct
// bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
if (!$.support.submitBubbles && $().jquery < '1.7' && rails.callFormSubmitBindings(form, e) === false) return rails.stopEverything(e);

rails.handleRemote(form);
return false;

} else {
// slight timeout so that the submit button gets properly serialized
setTimeout(function(){ rails.disableFormElements(form); }, 13);
}
});

$(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
var button = $(this);

$(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
var button = $(this);
if (!rails.allowAction(button)) return rails.stopEverything(event);

if (!rails.allowAction(button)) return rails.stopEverything(event);
// register the pressed submit button
var name = button.attr('name'),
data = name ? {name:name, value:button.val()} : null;

// register the pressed submit button
var name = button.attr('name'),
data = name ? {name:name, value:button.val()} : null;
button.closest('form').data('ujs:submit-button', data);
});

button.closest('form').data('ujs:submit-button', data);
});
$(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
if (this == event.target) rails.disableFormElements($(this));
});

$(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
if (this == event.target) rails.disableFormElements($(this));
});
$(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
if (this == event.target) rails.enableFormElements($(this));
});

$(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
if (this == event.target) rails.enableFormElements($(this));
});
$(function(){
// making sure that all forms have actual up-to-date token(cached forms contain old one)
csrf_token = $('meta[name=csrf-token]').attr('content');
csrf_param = $('meta[name=csrf-param]').attr('content');
$('form input[name="' + csrf_param + '"]').val(csrf_token);
});
}

})( jQuery );
4 changes: 2 additions & 2 deletions app/views/layouts/_javascripts.html.haml
@@ -1,7 +1,7 @@
= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
:javascript
window.jQuery || document.write('<script src="#{asset_path("jquery.min.js")}"><\/script>')
= javascript_include_tag "//cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"
= javascript_include_tag "//cdn.jquerytools.org/1.2.7/all/jquery.tools.min.js"
:javascript
$(function() {
I18n.defaultLocale = '#{I18n.default_locale}';
Expand Down

0 comments on commit 854ce06

Please sign in to comment.