1class ShopaholicOrder { 2 constructor() { 3 this.sOrderFormClass = '_shopaholic-order-form'; 4 this.sOrderFieldClass = '_shopaholic-order-field'; 5 this.sGroupAttribute = 'data-group'; 6 this.sFieldAttribute = 'data-field'; 7 this.obAjaxRequestCallback = null; 8 9 this.sComponentMethod = 'MakeOrder::onCreate'; 10 this.obOrder = { 11 order: { 12 property: {}, 13 }, 14 user: {}, 15 shipping_address: {}, 16 billing_address: {}, 17 }; 18 } 19 20 /** 21 * Prepare request object with order data 22 * Send ajax request and create order 23 */ 24 create() { 25 this.prepareRequestObject(); 26 this.sendAjaxRequest(); 27 } 28 29 /** 30 * Prepare request object from order form 31 */ 32 prepareRequestObject() 33 { 34 const obOrderForm = document.querySelector(`.${this.sOrderFormClass}`); 35 if (!obOrderForm) { 36 return; 37 } 38 39 const obFieldList = obOrderForm.querySelectorAll(`.${this.sOrderFieldClass}`); 40 if (!obFieldList || obFieldList.length === 0) { 41 return; 42 } 43 44 obFieldList.forEach((obFieldNode) => { 45 this.addFieldValue(obFieldNode); 46 }); 47 } 48 49 /** 50 * Process node and add field value in request object 51 * @param {node} obFieldNode 52 */ 53 addFieldValue(obFieldNode) { 54 const sGroupOriginal = obFieldNode.getAttribute(this.sGroupAttribute); 55 const sGroup = !!sGroupOriginal ? sGroupOriginal.replace(/-/g, '_').toLowerCase(): sGroupOriginal; 56 let sFieldOriginal = obFieldNode.getAttribute(this.sFieldAttribute); 57 let sField = !!sFieldOriginal ? sFieldOriginal.replace(/-/g, '_').toLowerCase() : sFieldOriginal; 58 if (!sField) { 59 sField = obFieldNode.name.replace(/-/g, '_').toLowerCase(); 60 } 61 62 const {type: sType} = obFieldNode; 63 64 if ((sType === 'radio' || sType === 'checkbox') && !obFieldNode.checked) { 65 return; 66 } 67 68 const sValue = obFieldNode.value; 69 if (sGroup) { 70 sField = `${sGroup}.${sField}`; 71 } 72 73 this.addValueToObject(sField, sValue); 74 } 75 76 /** 77 * Add field value in request object 78 * @param {string} sField 79 * @param {string} sValue 80 */ 81 addValueToObject(sField, sValue) { 82 const arFieldList = sField.split('.'); 83 let obValueObject = this.obOrder; 84 let sLastField = null; 85 86 arFieldList.forEach((sFieldPart) => { 87 if (sLastField !== null && obValueObject[sLastField] === null) {
88 obValueObject[sLastField] = {}; 89 } 90 91 if (sLastField !== null) { 92 obValueObject = obValueObject[sLastField]; 93 } 94 95 sLastField = sFieldPart; 96 if (obValueObject[sFieldPart] === undefined) { 97 obValueObject[sFieldPart] = null; 98 } 99 }); 100 101 if (obValueObject[sLastField] === null) { 102 obValueObject[sLastField] = sValue; 103 } else if (!!obValueObject[sLastField]) { 104 obValueObject[sLastField] = [obValueObject[sLastField]]; 105 obValueObject[sLastField].push(sValue); 106 } 107 } 108 109 /** 110 * Send ajax request and create order 111 */ 112 sendAjaxRequest() { 113 114 let obRequestData = {data: this.obOrder}; 115 116 if (this.obAjaxRequestCallback !== null) { 117 obRequestData = this.obAjaxRequestCallback(obRequestData); 118 } 119 120 $.request(this.sComponentMethod, obRequestData); 121 } 122 123 /** 124 * Set ajax request callback 125 * 126 * @param {function} obCallback 127 * @returns {ShopaholicOrder} 128 */ 129 setAjaxRequestCallback(obCallback) { 130 this.obAjaxRequestCallback = obCallback; 131 132 return this; 133 } 134} 135export {ShopaholicOrder};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.