From 1d2b7bb7b4f39c12d60b60fb3389627295ad3c18 Mon Sep 17 00:00:00 2001 From: Swashata Ghosh Date: Sun, 22 Sep 2024 13:02:51 +0530 Subject: [PATCH] [js] [checkout] [refactor] Extract jQuery.form library for reusability. --- assets/js/jquery.form.js | 1 + assets/scripts/jquery.form.js | 45 +++++++++++++++++++++++++++++++++ gulptasks/scripts.js | 1 + templates/checkout/frame.php | 47 +---------------------------------- 4 files changed, 48 insertions(+), 46 deletions(-) create mode 100644 assets/js/jquery.form.js create mode 100644 assets/scripts/jquery.form.js diff --git a/assets/js/jquery.form.js b/assets/js/jquery.form.js new file mode 100644 index 00000000..a5d9f896 --- /dev/null +++ b/assets/js/jquery.form.js @@ -0,0 +1 @@ +jQuery((function(n){n.extend({form:function(r,e,t){null==t&&(t="POST"),null==e&&(e={});var o=n("
").attr({method:t,action:r}).css({display:"none"}),a=function(r,e){if(n.isArray(e))for(var t=0;t").attr({type:"hidden",name:String(r),value:String(e)}))};for(var i in e)e.hasOwnProperty(i)&&a(i,e[i]);return o.appendTo("body")}})})); \ No newline at end of file diff --git a/assets/scripts/jquery.form.js b/assets/scripts/jquery.form.js new file mode 100644 index 00000000..8e6db61f --- /dev/null +++ b/assets/scripts/jquery.form.js @@ -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 = $('').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($('').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'); + } + }); +}); \ No newline at end of file diff --git a/gulptasks/scripts.js b/gulptasks/scripts.js index 6f5fa9b2..154c52bc 100644 --- a/gulptasks/scripts.js +++ b/gulptasks/scripts.js @@ -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) => { diff --git a/templates/checkout/frame.php b/templates/checkout/frame.php index 1e677200..535eae0a 100755 --- a/templates/checkout/frame.php +++ b/templates/checkout/frame.php @@ -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' ); @@ -185,52 +186,6 @@