Skip to content

Commit

Permalink
[js] [checkout] [refactor] Extract jQuery.form library for reusability.
Browse files Browse the repository at this point in the history
  • Loading branch information
swashata committed Sep 30, 2024
1 parent 55b8178 commit 1d2b7bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
1 change: 1 addition & 0 deletions assets/js/jquery.form.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions assets/scripts/jquery.form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// http://stackoverflow.com/questions/4583703/jquery-post-request-not-ajax
jQuery(function ($) {
$.extend({
form: function (url, data, method) {
if (method == null) method = 'POST';
if (data == null) data = {};

var form = $('<form>').attr({
method: method,
action: url
}).css({
display: 'none'
});

var addData = function (name, data) {
if ($.isArray(data)) {
for (var i = 0; i < data.length; i++) {
var value = data[i];
addData(name + '[]', value);
}
} else if (typeof data === 'object') {
for (var key in data) {
if (data.hasOwnProperty(key)) {
addData(name + '[' + key + ']', data[key]);
}
}
} else if (data != null) {
form.append($('<input>').attr({
type : 'hidden',
name : String(name),
value: String(data)
}));
}
};

for (var key in data) {
if (data.hasOwnProperty(key)) {
addData(key, data[key]);
}
}

return form.appendTo('body');
}
});
});
1 change: 1 addition & 0 deletions gulptasks/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const jsOutput = 'assets/js';
const jsSources = {
['nojquery.ba-postmessage']: `${jsSourceDir}/nojquery.ba-postmessage.js`,
['postmessage'] : `${jsSourceDir}/postmessage.js`,
['jquery.form'] : `${jsSourceDir}/jquery.form.js`,
};

exports.getSdkJSCompilers = (isProd) => {
Expand Down
47 changes: 1 addition & 46 deletions templates/checkout/frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
wp_enqueue_script( 'json2' );
fs_enqueue_local_script( 'postmessage', 'nojquery.ba-postmessage.js' );
fs_enqueue_local_script( 'fs-postmessage', 'postmessage.js' );
fs_enqueue_local_script( 'fs-form', 'jquery.form.js' );
fs_enqueue_local_style( 'fs_common', '/admin/common.css' );

fs_enqueue_local_style( 'fs_checkout', '/admin/checkout.css' );
Expand Down Expand Up @@ -185,52 +186,6 @@
<div id="fs_checkout" class="wrap fs-section fs-full-size-wrapper">
<div id="fs_frame"></div>
<script type="text/javascript">
// http://stackoverflow.com/questions/4583703/jquery-post-request-not-ajax
jQuery(function ($) {
$.extend({
form: function (url, data, method) {
if (method == null) method = 'POST';
if (data == null) data = {};

var form = $('<form>').attr({
method: method,
action: url
}).css({
display: 'none'
});

var addData = function (name, data) {
if ($.isArray(data)) {
for (var i = 0; i < data.length; i++) {
var value = data[i];
addData(name + '[]', value);
}
} else if (typeof data === 'object') {
for (var key in data) {
if (data.hasOwnProperty(key)) {
addData(name + '[' + key + ']', data[key]);
}
}
} else if (data != null) {
form.append($('<input>').attr({
type : 'hidden',
name : String(name),
value: String(data)
}));
}
};

for (var key in data) {
if (data.hasOwnProperty(key)) {
addData(key, data[key]);
}
}

return form.appendTo('body');
}
});
});

(function ($) {
$(function () {

Expand Down

0 comments on commit 1d2b7bb

Please sign in to comment.