Skip to content

Commit

Permalink
changed method names to enableSubmitButton/disableSubmitButton, intro…
Browse files Browse the repository at this point in the history
…duced configs for change html and html, changed code for csrfToken
  • Loading branch information
yashikagarg13 committed Oct 9, 2015
1 parent cf3fac4 commit 5b11d7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
28 changes: 14 additions & 14 deletions js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Freya.get_popup = function (url, $button, currentButtonHtml) {
})
.always(function () {
if ($button !== undefined ) {
Freya.removeSpinner($button, currentButtonHtml);
Freya.enableSubmitBtn($button, currentButtonHtml);
}
});
};
Expand Down Expand Up @@ -90,7 +90,7 @@ $(function () {
var confirm = $button.attr('data-confirm');

//To prevent multisending
var currentButtonHtml = Freya.addSpinner($button);
var currentButtonHtml = Freya.disableSubmitBtn($button);

function handle() {
$.ajax({
Expand All @@ -102,7 +102,7 @@ $(function () {
done_handler(data, textStatus, jqXHR, $button);
})
.always(function () {
Freya.removeSpinner($button, currentButtonHtml);
Freya.enableSubmitBtn($button, currentButtonHtml);
});
}

Expand All @@ -124,7 +124,7 @@ $(function () {
var $submit = $form.find('button[type="submit"]');

// To prevent multisending
var currentButtonHtml = Freya.addSpinner($submit);
var currentButtonHtml = Freya.disableSubmitBtn($submit);

// Update URL if form.method == GET
if ($form.attr('method') == 'GET') {
Expand All @@ -147,7 +147,7 @@ $(function () {
done_handler(data, textStatus, jqXHR, $form);
})
.always(function () {
Freya.removeSpinner($submit, currentButtonHtml);
Freya.enableSubmitBtn($submit, currentButtonHtml);
});
}

Expand All @@ -163,13 +163,13 @@ $(function () {

//To prevent multisending
if (!confirm) {
var currentButtonHtml = Freya.addSpinner($button);
var currentButtonHtml = Freya.disableSubmitBtn($button);
}

function handle() {
if (csrfToken) {
if (LocalStorage.get('conf.csrfToken')) {
var setting = { beforeSend: function (xhr) {
xhr.setRequestHeader("X-CSRFToken", csrfToken);
xhr.setRequestHeader("X-CSRFToken", LocalStorage.get('conf.csrfToken'))
}}
}
$.ajax($.extend(setting, {
Expand All @@ -182,7 +182,7 @@ $(function () {
})
.always(function () {
if(!confirm) {
Freya.removeSpinner($button, currentButtonHtml);
Freya.enableSubmitBtn($button, currentButtonHtml);
}
});
}
Expand All @@ -206,7 +206,7 @@ $(function () {
var $button = $(this);

// To prevent multisending
var currentButtonHtml = Freya.addSpinner($button);
var currentButtonHtml = Freya.disableSubmitBtn($button);

formData.push({ name: this.name, value: this.value });
formData.push({ name: 'next', value: document.URL });
Expand All @@ -219,7 +219,7 @@ $(function () {
done_handler(data, textStatus, jqXHR, $form);
})
.always(function () {
Freya.removeSpinner($button, currentButtonHtml);
Freya.enableSubmitBtn($button, currentButtonHtml);
});
});

Expand All @@ -230,7 +230,7 @@ $(function () {
var url = $button.attr('action');

// To prevent multisending
var currentButtonHtml = Freya.addSpinner($button);
var currentButtonHtml = Freya.disableSubmitBtn($button);
Freya.get_popup(url, $button, currentButtonHtml);
});

Expand All @@ -240,7 +240,7 @@ $(function () {

//To prevent multisending
var $button = $(this);
var currentButtonHtml = Freya.addSpinner($button);
var currentButtonHtml = Freya.disableSubmitBtn($button);

var $form = $button.closest('form');
var $popup = $form.closest('.modal[data-api="ajax.popup"]');
Expand Down Expand Up @@ -278,7 +278,7 @@ $(function () {
}
})
.always(function () {
Freya.removeSpinner($button, currentButtonHtml);
Freya.enableSubmitBtn($button, currentButtonHtml);
});
});
});
21 changes: 15 additions & 6 deletions js/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"use strict";

var Freya = Freya || {};
Freya.confs = {
changeSubmitButtonHTML: true,
submitButtonHTMLForAjaxInProcess: '<span class="icon-spinner animate-spin"></span>'
};


// Get real width and height even for hidden element
Freya.getRealDimensions = function ($el, outer) {
Expand Down Expand Up @@ -72,16 +77,20 @@ Freya.sanitize_html = function(value, allowedTags) {
});
};

Freya.addSpinner = function($btn) {
Freya.disableSubmitBtn = function($btn) {
$btn.prop("disabled", true);
var currentHTML = $btn.html();
$btn.html('<span class="icon-spinner animate-spin"></span>');
return currentHTML;
if(Freya.confs.changeSubmitButtonHTML) {
var currentHTML = $btn.html();
$btn.html(Freya.confs.submitButtonHTMLForAjaxInProcess);
return currentHTML;
}
};

Freya.removeSpinner = function($btn, previousHTML) {
Freya.enableSubmitBtn = function($btn, previousHTML) {
$btn.prop("disabled", false);
$btn.html(previousHTML);
if(Freya.confs.changeSubmitButtonHTML) {
$btn.html(previousHTML);
}
};

// Tests for sanitize_html
Expand Down

0 comments on commit 5b11d7d

Please sign in to comment.