1jQuery(document).ready(function ($) { 2 // We must listen to Onsen UI init event because standard document.ready might miss dynamic pages 3 document.addEventListener('init', function (event) { 4 var page = event.target; 5 6 // Check if the initialized page is our cart page (assuming id="cart") 7 // Also check if the wrapper exists in this page 8 if (page.id === 'cart') { 9 initRajaOngkir(page); 10 } 11 }); 12 13 // Fallback: Try running immediately in case it's already in DOM (e.g. static load or missed event) 14 initRajaOngkir(document); 15 16 function initRajaOngkir(context) { 17 const $ctx = $(context); 18 const container = $ctx.find('#cust-kec-dropdowns'); 19 20 // Prevent double injection if already initialized 21 if (container.length === 0 || container.children().length > 0) return; 22 23 const inputName = $ctx.find('#cust-kec'); 24 const inputOrigin = $ctx.find('#origin'); 25 26 // Create Select Elements 27 const selects = { 28 province: $('<select class="text-input text-input--underbar" style="width: 100%; margin-bottom: 10px; padding: 5px;"><option value="">Pilih Provinsi...</option></select>'), 29 city: $('<select class="text-input text-input--underbar" style="width: 100%; margin-bottom: 10px; padding: 5px;"><option value="">Pilih Kota/Kabupaten...</option></select>'), 30 district: $('<select class="text-input text-input--underbar" style="width: 100%; margin-bottom: 10px; padding: 5px;"><option value="">Pilih Kecamatan...</option></select>'), 31 subdistrict: $('<select class="text-input text-input--underbar" style="width: 100%; margin-bottom: 10px; padding: 5px;"><option value="">Pilih Kelurahan/Desa...</option></select>') 32 }; 33 34 // Inject Selects 35 container.append(selects.province); 36 container.append(selects.city); 37 container.append(selects.district); 38 container.append(selects.subdistrict); 39 40 // Initial Load 41 loadProvinces(); 42 43 // --- Helper Functions --- 44 45 function resetSelect(el, placeholder) { 46 el.empty().append('<option value="">' + placeholder + '</option>'); 47 } 48 49 function populateSelect(el, data) { 50 let html = '<option value="">' + el.find('option:first').text() + '</option>'; 51 $.each(data, function (index, item) { 52 let id, name; 53 if (item.subdistrict_id && !item.district_id) { 54 if (item.district_name) { id = item.district_id; name = item.district_name; } 55 } 56 if (!id && item.id) id = item.id; 57 if (!name && item.name) name = item.name; 58 if (item.province_id && !id) id = item.province_id; 59 if (item.province && !name) name = item.province; 60 if (item.city_id && !id) id = item.city_id; 61 if (item.city_name && !name) name = (item.type || '') + ' ' + item.city_name; 62 if (item.district_id && !id) id = item.district_id; 63 if (item.district_name && !name) name = item.district_name; 64 if (item.subdistrict_id && !id) id = item.subdistrict_id; 65 if (item.subdistrict_name && !name) name = item.subdistrict_name; 66 67 html += '<option value="' + id + '" data-name="' + name + '">' + name + '</option>'; 68 }); 69 el.html(html); 70 } 71 72 // --- AJAX Loaders --- 73 74 function loadProvinces() { 75 $.getJSON(tfRajaOngkir.ajax_url, { action: 'tf_get_ro_provinces' }, function (response) { 76 if (response.success) { 77 populateSelect(selects.province, response.data); 78 } 79 }); 80 } 81 82 function loadCities(provId) { 83 $.getJSON(tfRajaOngkir.ajax_url, { action: 'tf_get_ro_cities', province_id: provId }, function (response) { 84 if (response.success) { 85 populateSelect(selects.city, response.data); 86 } 87 }); 88 } 89 90 function loadDistricts(cityId) { 91 $.getJSON(tfRajaOngkir.ajax_url, { action: 'tf_get_ro_districts', city_id: cityId }, function (response) { 92 if (response.success) { 93 populateSelect(selects.district, response.data); 94 } 95 }); 96 } 97 98 function loadSubdistricts(distId) { 99 $.getJSON(tfRajaOngkir.ajax_url, { action: 'tf_get_ro_subdistricts', district_id: distId }, function (response) { 100 if (response.success) { 101 populateSelect(selects.subdistrict, response.data); 102 } 103 }); 104 } 105 106 // --- Event Listeners --- 107 108 selects.province.on('change', function () { 109 const val = $(this).val(); 110 resetSelect(selects.city, 'Pilih Kota/Kabupaten...'); 111 resetSelect(selects.district, 'Pilih Kecamatan...'); 112 resetSelect(selects.subdistrict, 'Pilih Kelurahan/Desa...'); 113 if (val) loadCities(val);
114 updateHiddenInputs(); 115 }); 116 117 selects.city.on('change', function () { 118 const val = $(this).val(); 119 resetSelect(selects.district, 'Pilih Kecamatan...'); 120 resetSelect(selects.subdistrict, 'Pilih Kelurahan/Desa...'); 121 if (val) loadDistricts(val); 122 inputOrigin.val(''); 123 $ctx.find('.checkout-shipping-render').empty(); 124 updateHiddenInputs(); 125 }); 126 127 selects.district.on('change', function () { 128 const val = $(this).val(); 129 if (val) { 130 inputOrigin.val(val); 131 calculateShipping(val); 132 loadSubdistricts(val); 133 } else { 134 inputOrigin.val(''); 135 $ctx.find('.checkout-shipping-render').empty(); 136 } 137 resetSelect(selects.subdistrict, 'Pilih Kelurahan/Desa...'); 138 updateHiddenInputs(); 139 }); 140 141 selects.subdistrict.on('change', function () { 142 updateHiddenInputs(); 143 }); 144 145 function updateHiddenInputs() { 146 const provTxt = selects.province.find('option:selected').data('name') || ''; 147 const cityTxt = selects.city.find('option:selected').data('name') || ''; 148 const distTxt = selects.district.find('option:selected').data('name') || ''; 149 const subdistTxt = selects.subdistrict.find('option:selected').data('name') || ''; 150 151 let fullAddress = ''; 152 if (subdistTxt) fullAddress += subdistTxt + ', '; 153 if (distTxt) fullAddress += distTxt + ', '; 154 if (cityTxt) fullAddress += cityTxt + ', '; 155 if (provTxt) fullAddress += provTxt; 156 157 inputName.val(fullAddress); 158 inputName.trigger('input'); 159 } 160 161 function calculateShipping(destinationId) { 162 if (typeof tfCourier === 'undefined' || !tfCourier.length) return; 163 164 var weight = 0; 165 $ctx.find('.my-product-weight span').each(function () { 166 weight += Number($(this).text() || 1000); 167 }); 168 169 $ctx.find('ons-modal').show(); 170 $ctx.find('.checkout-shipping-render').html('<ons-list></ons-list>'); 171 $ctx.find('.checkout-shipping .checkout-shipping-val').hide(); 172 173 var courierString = tfCourier.join(':'); 174 175 $.ajax({
176 url: tfSite + '/wp-content/themes/themefood/inc/shipping.php', 177 method: 'GET', 178 data: { 179 origin: tfOrigin, 180 destination: destinationId, 181 weight: weight, 182 courier: courierString, 183 key: tfApiro, 184 price: 'lowest' 185 }, 186 dataType: 'json', 187 success: function (data) { 188 if (data.meta && data.meta.code == 200 && data.data && data.data.length > 0) { 189 $.each(data.data, function (i, item) { 190 var tarifOngkir = item.cost; 191 if (typeof tfDiscongkir !== 'undefined' && tfDiscongkir != 'no') { 192 var subTotalKurir = parseInt($ctx.find('.checkout-subtotal').text().replace(/\D/g, '')); 193 if (subTotalKurir >= +tfMinbelanja) { 194 if (tfDiscongkir == 'free') tarifOngkir = 0; 195 if (tfDiscongkir == 'nominal') { 196 tarifOngkir = tarifOngkir - tfDiscnominal; 197 if (tarifOngkir < 0) tarifOngkir = 0; 198 } 199 } 200 } 201 202 function formatMoney(n) { 203 return tfCurrency + n.toFixed(0).replace(/\B(?=(\d{3})+(?!\d))/g, "."); 204 } 205 206 var serviceDesc = item.name + ' ' + item.service; 207 if (item.description && item.description !== 'Unknown Service') serviceDesc += ' (' + item.description + ')'; 208 if (item.etd) serviceDesc += ' - Estimasi: ' + item.etd; 209 serviceDesc += ' - ' + weight + 'g'; 210 211 var radioId = 'ongkir-' + item.code + '-' + i; 212 213 $ctx.find('.checkout-shipping-render ons-list').append( 214 '<ons-list-item tappable>' + 215 '<label class="left">' + 216 '<ons-radio name="cust-ongkir" ' + 217 'input-id="' + radioId + '" ' + 218 'data-label="' + serviceDesc + ' (' + formatMoney(tarifOngkir) + ')" ' + 219 'value="' + tarifOngkir + '">' + 220 '</ons-radio>' + 221 '</label>' + 222 '<label for="' + radioId + '" class="center">' + 223 '<strong>' + item.name + '</strong><br>' + 224 '<small>' + item.service + 225 (item.etd ? ' - ' + item.etd : '') + 226 ' - ' + weight + 'g' + 227 '</small><br>' + 228 '<strong>' + formatMoney(tarifOngkir) + '</strong>' + 229 '</label>' + 230 '</ons-list-item>' 231 ); 232 }); 233 234 $ctx.find('ons-modal').hide(); 235 // Re-compile if Onsen doesn't auto-enhance 236 if (typeof ons !== 'undefined') { 237 ons.compile($ctx.find('.checkout-shipping-render')[0]); 238 } 239 240 if ($ctx.find('.checkout-shipping-render').length) { 241 $ctx.find('.page__content').animate({ 242 scrollTop: $ctx.find('.checkout-shipping-render').offset().top - 100 243 }, 500); 244 } 245 246 } else { 247 $ctx.find('ons-modal').hide(); 248 ons.notification.alert(data.meta.message || 'Gagal memuat ongkir'); 249 } 250 }, 251 error: function () { 252 $ctx.find('ons-modal').hide(); 253 ons.notification.alert('Gagal menghubungi server ongkir'); 254 } 255 }); 256 } 257 } 258});
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.