1/* global common, dawa, messages, modal */ 2/* exported account */ 3 4const account = { 5 6 editAddressCallback: null, 7 editDeliveryAddressCallback: null, 8 changeDeliveryMethodCallback: null, 9 10 /** 11 * Show modal for editing the billing address. 12 * 13 * @param {Function} [callback] 14 */ 15 showEditAddress: function(callback) { 16 if (callback !== undefined) { 17 this.editAddressCallback = callback; 18 } 19 modal.create('edit_address', { 20 url: '/account/address', 21 buttons: ['button.cancel', 'button.save_submit'], 22 reload: true 23 }); 24 }, 25 26 /** 27 * Handle submit of the edit billing address modal. 28 * 29 * @param {HTMLFormElement} form 30 */ 31 submitEditAddress: function(form) { 32 if (window.dawa !== undefined) { 33 if ( ! dawa.validate(form)) { 34 return; 35 } 36 } 37 common.ajaxFormSubmitter({ 38 form: form, 39 success: function(response) { 40 modal.hide(form); 41 if (account.editAddressCallback) { 42 account.editAddressCallback(response); 43 } 44 } 45 }); 46 }, 47 48 /** 49 * Submit a billing address form from a "my account" page. 50 * 51 * @param {HTMLFormElement} form 52 */ 53 submitEditCustomerAddress: function(form) { 54 account.editAddressCallback = function(response) { 55 account.unshippedOrders.handleUnshippedOrders(response.data.changedFields); 56 }; 57 this.submitEditAddress(form); 58 }, 59 60 /** 61 * Initialize loading the city name when the zip code has changed. 62 * 63 * @param {string} [prefix] Prefix of the input IDs 64 */ 65 initCityFromZipCode: function(prefix) { 66 const $zipCode = $('#' + prefix + 'zip_code'); 67 const $city = $('#' + prefix + 'city'); 68 if ($zipCode.length === 0 || $city.length === 0) { 69 return; 70 } 71 72 const $countryCode = $('#' + prefix + 'country_code'); 73 $zipCode.on('change', function() { 74 const zipCode = $zipCode.val(); 75 if (zipCode) { 76 account.loadCityFromZipCode(zipCode, $countryCode.val(), function(cityNames) { 77 if (cityNames.length > 0 && $city.val() === '') { 78 $city.val(cityNames[0]); 79 } 80 }); 81 } 82 }); 83 }, 84 85 /** 86 * AJAX load city name from a zip code. 87 * 88 * @param {string} zipCode 89 * @param {string|null} countryCode 90 * @param {Function} callback Is called with the city as argument 91 */ 92 loadCityFromZipCode: function(zipCode, countryCode, callback) { 93 common.ajaxLoader({ 94 url: '/account/zipCodeToCity', 95 data: { 96 zipCode: zipCode, 97 countryCode: countryCode 98 }, 99 notSuccessFeedbackStyle: false, 100 success: function(response) { 101 callback(response.data.cityNames); 102 } 103 }); 104 }, 105 106 /** 107 * Submit the merge accounts form. 108 * 109 * @param {HTMLFormElement} form 110 */ 111 submitMergeCustomers: function(form) { 112 common.ajaxFormSubmitter({ 113 form: form, 114 success: function(response) { 115 $('#account_merge_customer_form').html(response.content); 116 } 117 }); 118 }, 119 120 /** 121 * Show order details modal. 122 * 123 * @param {number} orderId 124 * @param {Function} [editDeliveryAddressCallback] 125 * @param {Function} [changeDeliveryMethodCallback] 126 */ 127 openOrderDetails: function(orderId, editDeliveryAddressCallback, changeDeliveryMethodCallback) { 128 this.editDeliveryAddressCallback = editDeliveryAddressCallback; 129 this.changeDeliveryMethodCallback = changeDeliveryMethodCallback; 130 modal.create('order_history_modal', { 131 url: '/account/orderHistory/' + orderId, 132 size: 'lg', 133 buttons: ['button.close'], 134 reload: true 135 }); 136 }, 137 138 /** 139 * Hide order history modal and show delivery status for an order. 140 * 141 * @param {number} orderId 142 */ 143 switchToOrderDeliveryStatus: function(orderId) { 144 modal.hide('#order_history_modal', function() { 145 account.showOrderDeliveryStatus(orderId); 146 }); 147 }, 148 149 /** 150 * Show delivery status for an order, before changing delivery address. 151 * 152 * @param {number} orderId 153 */ 154 showOrderDeliveryStatus: function(orderId) { 155 modal.create('order_delivery_status', { 156 url: '/account/orderDeliveryStatus/' + orderId, 157 buttons: ['button.cancel', 'button.ok_submit'], 158 reload: true 159 }); 160 }, 161 162 /** 163 * Hide delivery status modal and show form for editing the delivery address for an order. 164 * @param {number} orderId 165 */ 166 switchToEditDeliveryAddress: function(orderId) { 167 modal.hide('#order_delivery_status', function() { 168 account.showEditDeliveryAddress(orderId); 169 }); 170 }, 171 172 /** 173 * Show form for editing the delivery address for an order. 174 * 175 * @param {number} orderId 176 * @param {Function} [callback] 177 */ 178 showEditDeliveryAddress: function(orderId, callback) { 179 if (callback !== undefined) { 180 this.editDeliveryAddressCallback = callback; 181 } 182 modal.create('edit_delivery_address', { 183 url: '/account/editDeliveryAddress/' + orderId, 184 buttons: ['button.cancel', 'button.save_submit'], 185 reload: true 186 }); 187 }, 188 189 /** 190 * Submit a delivery address for an order. 191 * 192 * @param {HTMLFormElement} form 193 */ 194 submitEditDeliveryAddress: function(form) { 195 if (window.dawa !== undefined) { 196 if ( ! dawa.validate(form)) { 197 return; 198 } 199 } 200 common.ajaxFormSubmitter({ 201 form: form, 202 success: function(response) { 203 modal.hide(form); 204 if (account.editDeliveryAddressCallback) { 205 account.editDeliveryAddressCallback(response); 206 } 207 } 208 }); 209 }, 210 211 /** 212 * Hide order history modal, and show form for changing the freight method of an order. 213 * 214 * @param {number} orderId 215 */ 216 switchToEditDeliveryMethod: function(orderId) { 217 modal.hide('#order_history_modal', function() { 218 account.showEditDeliveryMethod(orderId); 219 }); 220 }, 221 222 /** 223 * Show form for changing the freight method of an order. 224 * 225 * @param {number} orderId 226 */ 227 showEditDeliveryMethod: function(orderId) { 228 modal.create('order_delivery_status', { 229 url: '/account/editDeliveryMethod/' + orderId, 230 buttons: ['button.cancel'], 231 reload: true 232 }); 233 }, 234 235 /** 236 * Change the freight method of an order and show form for changing the delivery address. 237 * 238 * @param {number} orderId 239 * @param {string} methodLabel 240 */ 241 selectDeliveryMethod: function(orderId, methodLabel) { 242 modal.hide('#order_delivery_status', function() { 243 modal.create('order_delivery_address', { 244 url: '/account/editDeliveryAddress/' + orderId + '?method=' + methodLabel, 245 buttons: ['button.cancel', 'button.save_submit'], 246 reload: true 247 }); 248 }); 249 }, 250 251 /** 252 * Change the freight method of an order (without setting the address). 253 * 254 * @param {number} orderId 255 * @param {string} methodLabel 256 */ 257 changeDeliveryMethod: function(orderId, methodLabel) { 258 common.ajaxLoader({ 259 loader: '#order_delivery_status', 260 url: '/account/editDeliveryMethod/' + orderId, 261 data: { 262 method: methodLabel 263 }, 264 method: 'POST', 265 success: function(response) { 266 modal.hide('#order_delivery_status'); 267 if (account.changeDeliveryMethodCallback) { 268 account.changeDeliveryMethodCallback(response); 269 } 270 } 271 }); 272 }, 273 274 /** 275 * Change the DAO service point of an order. 276 * 277 * @param {HTMLFormElement} form 278 */ 279 submitSelectDaoServicePoint: function(form) { 280 common.submitForm(form, { 281 success: function() { 282 window.location.reload(); 283 } 284 }); 285 }, 286 287 /** 288 * Handle submit of the bank information form. 289 * @param {HTMLFormElement} form 290 */ 291 submitBankInformation: function(form) { 292 if (form.reportValidity() === false) { 293 return; 294 } 295 common.ajaxFormSubmitter({ 296 form: form, 297 successFeedbackStyle: false, 298 success: function(response) { 299 if (response.data.messages) { 300 modal.createAlert({ 301 title: response.message, 302 body: response.data.messages 303 }); 304 } 305 else { 306 messages.addSuccess(response.message, 'clean'); 307 } 308 } 309 }); 310 }, 311 312 /** 313 * Trigger registering a new payment card. 314 */ 315 addPaymentCard: function() { 316 common.ajaxLoader({ 317 url: '/payment/registerPaymentCard', 318 method: 'POST' 319 }); 320 }, 321 322 /** 323 * Used for when a customer wants to change the registered payment card. 324 * 325 * @param {number} cardId 326 */ 327 changePaymentCard: function(cardId) { 328 common.ajaxLoader({
329 url: '/payment/changePaymentCard', 330 data: { 331 replaceCardId: cardId 332 }, 333 method: 'POST' 334 }); 335 }, 336 337 /** 338 * Send verify email link mail. 339 */ 340 sendVerifyEmailLink: function() { 341 const $emailVerificationForm = $('#email_verification_form'); 342 common.ajaxFormSubmitter({ 343 loader: $emailVerificationForm.closest('.loader-container'), 344 url: '/account/sendVerifyEmailLink', 345 success: function(response) { 346 $emailVerificationForm.replaceWith(response.content); 347 } 348 }); 349 }, 350 351 /** 352 * Toggle subscribing to a newsletter. 353 * 354 * @param {HTMLInputElement} input The checkbox for the newsletter 355 */ 356 toggleNewsletter: function(input) { 357 const $input = $(input); 358 common.ajaxLoader({ 359 url: '/account/toggleNewsletter', 360 data: { 361 label: $input.val(), 362 state: $input.is(':checked') ? 1 : 0 363 }, 364 method: 'POST' 365 }); 366 }, 367 368 /** 369 * Functions for handling update of delivery address on unshipped orders 370 */ 371 unshippedOrders: { 372 373 /** 374 * Checks if any of the changedAddressProperties should trigger an examination of unshipped orders. 375 * Only if a change is relevant we show a modal for handling/updating the delivery address on unshipped orders. 376 * 377 * @param {string[]} changedFields 378 */ 379 handleUnshippedOrders: function(changedFields) { 380 common.ajaxLoader({ 381 url: '/account/unshippedOrders', 382 data: { 383 changedFields: changedFields 384 }, 385 method: 'get', 386 success: function(response) { 387 if (response.content) { 388 modal.create('unshipped_orders', { 389 title: response.title, 390 body: response.content, 391 buttons: ['button.done'], 392 size: 'lg', 393 reload: true, 394 keyboard: false, 395 callbacks: { 396 loaded: function() { 397 account.unshippedOrders.updateCloseButton(); 398 } 399 } 400 }); 401 } 402 } 403 }); 404 }, 405 406 /** 407 * Toggles display of the delivery address as text or as inputs 408 * 409 * @param {HTMLElement} button 410 * @param {number} orderId 411 */ 412 toggleEditDeliveryAddress: function(button, orderId) { 413 const $button = $(button); 414 const $orderElement = $('#unshipped_orders').find('[data-order_id="' + orderId + '"]'); 415 if ($orderElement.attr('data-is_order_handled') === '1') { 416 $button.hide(); 417 418 return; 419 } 420 const $container = $('#delivery_address_form_container_' + orderId); 421 if ($container.data('loaded')) { 422 $button.hide(); 423 424 return; 425 } 426 common.ajaxLoader({ 427 loader: $container, 428 url: '/account/editDeliveryAddress/' + orderId + '?origin=unshippedOrders', 429 success: function(response) { 430 $container.html(response.content).show(); 431 $container.data('loaded', 1); 432 $button.hide(); 433 } 434 }); 435 }, 436 437 /** 438 * Updates the delivery address on an order 439 * 440 * @param {HTMLFormElement} form 441 */ 442 submitDeliveryAddress: function(form) { 443 if (window.dawa !== undefined) { 444 if ( ! dawa.validate(form)) { 445 return; 446 } 447 } 448 common.ajaxFormSubmitter({ 449 form: form, 450 successFeedbackStyle: 'toast', 451 success: function() { 452 const $orderElement = $(form).closest('[data-order_id]'); 453 account.unshippedOrders.setOrderAsHandled($orderElement); 454 } 455 }); 456 }, 457 458 /** 459 * Hides order detail and shows info that the order has been handled. 460 * 461 * @param {number} orderId 462 */ 463 keepDeliveryAddress: function(orderId) { 464 const $orderElement = $('#unshipped_orders').find('[data-order_id="' + orderId + '"]'); 465 account.unshippedOrders.setOrderAsHandled($orderElement); 466 }, 467 468 /** 469 * Updates the delivery address on the order to the customer's address 470 * 471 * @param {number} orderId 472 */ 473 updateDeliveryToCustomerAddress: function(orderId) { 474 common.ajaxLoader({ 475 url: '/account/editDeliveryAddress/' + orderId, 476 data: { 477 updateToCustomerAddress: true 478 }, 479 method: 'POST', 480 successFeedbackStyle: 'toast', 481 success: function() { 482 const $orderElement = $('#unshipped_orders').find('[data-order_id="' + orderId + '"]'); 483 account.unshippedOrders.setOrderAsHandled($orderElement); 484 } 485 }); 486 }, 487 488 /** 489 * Updates the visibility of a handled order and updates buttons depending on the state og all orders. 490 * 491 * @param {JQuery} $orderElement 492 */ 493 setOrderAsHandled: function($orderElement) { 494 $orderElement.attr('data-is_order_handled', 1); 495 $orderElement.find('[data-state="unhandled"]').hide(); 496 $orderElement.find('[data-state="handled"]').show(); 497 account.unshippedOrders.updateCloseButton(); 498 }, 499 500 /** 501 * Checks if all orders has been handled and updates the close button accordingly 502 */ 503 updateCloseButton: function() { 504 const $container = $('#unshipped_orders'); 505 const unhandledOrders = $container.find('[data-order_id][data-is_order_handled="0"]').length; 506 if (unhandledOrders === 0) {
507 $container.find('.modal-footer button').prop('disabled', false); 508 } 509 else { 510 $container.find('.modal-footer button').prop('disabled', true); 511 } 512 } 513 } 514 515};
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.