1// async user processing code 2 3// fix older IE barfing on console.log code 4if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 5 6function User() { 7 // properties 8 this.user = {}; 9 this.addresses = []; 10 11 // methods 12 13 this.loadUser = function(callback) { 14 console.log("loading User"); 15 AwsWafIntegration.fetch('/SecureUser.json', { 16 credentials: 'same-origin' 17 }) 18 .then(function(response) { return response.json(); }) 19 .then(function(data) { 20 if (data.user) { 21 user.user = data.user; 22 $("#cart_login_link").html("Hi, " + data.user.first_name + " " + data.user.last_name + "!" ); 23 if (callback) { 24 callback(); 25 } 26 console.log("Loaded user"); 27 } 28 }) 29 .catch(function(error) { 30 // could not load user data - eat error and proceed 31 }); 32 return false; 33 }; 34 35 this.showLoginForm = function(existing_data,cb) { 36 var tmpl = $("#login_popup_template").html(); 37 var template = Handlebars.compile(tmpl); 38 var data; 39 if (existing_data) { 40 data = existing_data; 41 } else { 42 data = { 43 username: user.user ? user.user.username : '' 44 }; 45 } 46 var text = template(data); 47 //console.log(text); 48 $("#user_dialog").html(text).dialog({modal: true}); 49 $("#login_popup").submit( function(e){ 50 console.log("login form submitted"); 51 if(cb){user.doLogin(cb);}else{user.doLogin();} 52 return false; 53 }); 54 }; 55 56 this.doLogin = function(cb) { 57 var username = $('#login_popup input[name="username"]').val(); 58 var password = $('#login_popup input[name="password"]').val(); 59 if (username && password) { 60 $.ajax({ 61 url: '/SecureLoginSubmit.json', 62 dataType: 'jsonp', 63 data: { 64 'username': username, 65 'password': password 66 }, 67 success: function(data) { 68 var results = data; 69 if (data.success == 1) { 70 $("#user_dialog").dialog("close"); 71 user.loadUser(function(){ 72 user.showUserHome(); 73 if(cb){cb();} 74 }); 75 //user.showUserHome(); 76 // reload cart after logging in 77 if (cart) { 78 cart.get_cart(); 79 } 80 } else { 81 console.log("error in logging in - " + data.error); 82 $("#login_message_div").html("ERROR: " + data.error); 83 } 84 85 }, 86 error: function(jqXHR, textStatus, errorThrown) { 87 console.log("Error in doing login: " + errorThrown); 88 $("#login_message_div").html("Problem with reaching the server - please try again"); 89 } 90 }); 91 } else { 92 $("#login_message_div").html("Please provide a username and password to log in"); 93 } 94 95 }; 96 97 this.doLogout = function() { 98 $.ajax({ 99 url: '/SecureLogout.json', 100 dataType: 'jsonp', 101 success: function(data, textStatus, jqXHR) { 102 if (data.success) { 103 user.user = {}; 104 $("#user_dialog").dialog("close"); 105 user.loadUser(); 106 if (cart) { 107 cart.get_cart(); 108 } 109 $("#cart_login_link").html("You are now logged out."); 110 } else { 111 console.log("failed logout"); 112 } 113 }, 114 error: function(jqXHR, textStatus, errorThrown) { 115 console.log("logout error - " + errorThrown); 116 } 117 }); 118 }; 119 120 121 this.showUserWishlist = function() { 122 if (user.user && user.user.hasOwnProperty("email_address")) { 123 window.location = "/SecureUserWishlist"; 124 }else{ 125 user.showLoginForm(); 126 } 127 }; 128 this.addToWishlist = function(item_id) { 129 console.log("Adding"); 130 if (user.user && user.user.hasOwnProperty("email_address")) { 131 console.log("USER"); 132 $.ajax({ 133 url: '/SecureWishlist.json', 134 dataType: 'json', 135 data: { 136 item_id: item_id, 137 username: user.user.email_address 138 }, 139 success: function(data) { 140 $("#wishlistButton").attr("onclick", ""); 141 $("#wishlistButton").attr("href", "/SecureUserWishlist").html("View List"); 142 }, 143 error: function(jqXHR, textStatus, errorThrown) { 144 } 145 }); 146 }else{ 147 console.log("USER"); 148 user.showLoginForm(undefined, function(){ 149 user.addToWishlist(upc); 150 console.log("adding "+upc); 151 }); 152 } 153 154 }; 155 this.showUserHome = function(override) { 156 if (override || (user.user && user.user.hasOwnProperty("email_address"))) { 157 var tmpl = $("#user_home_template").html(); 158 var template = Handlebars.compile(tmpl); 159 var data = { 160 'user': user.user 161 }; 162 var text = template(data); 163 $("#user_dialog").html(text).dialog({modal: true}); 164 } else { 165 user.showLoginForm(); 166 } 167 } 168 169 this.showUser = function() { 170 if ($.isEmptyObject(user.user)) { 171 // user create 172 console.log("user create"); 173 var tmpl = $("#user_edit_template").html(); 174 var template = Handlebars.compile(tmpl); 175 var data = { 176 'submit_button_text': 'Submit and enter your address' 177 }; 178 var text = template(data); 179 $("#user_dialog").html(text).dialog({modal: true}); 180 $("#user_form").submit( function(e){ 181 console.log("user edit form submitted"); 182 user.submitUser(); 183 return false;
184 }); 185 } else { 186 187 if (user.user.fully_logged_in) { 188 var tmpl = $("#user_edit_template").html(); 189 var template = Handlebars.compile(tmpl); 190 var data = { 191 'user': user.user, 192 'submit_button_text': 'Submit changes' 193 }; 194 var text = template(data); 195 $("#user_dialog").html(text).dialog({modal: true}); 196 $("#user_form").submit( function(e){ 197 console.log("user edit form submitted"); 198 user.submitUser(); 199 return false; 200 }); 201 } else { 202 user.showLoginForm(); 203 } 204 205 } 206 $('input[name=email]').change(function(){user.checkUser()}); 207 } 208 209 this.checkUser = function() { 210 var email = $('input[name=email]').val(); 211 if (user.user && user.user.fully_logged_in) { 212 // existing customer, let them pass 213 } else { 214 if (email) { 215 $.ajax({ 216 url: '/SecureCartUserCheck.json', 217 dataType: 'jsonp', 218 data: { 219 'email': email 220 }, 221 success: function(data) { 222 if (data.force_login) { 223 console.log("existing user, not logged in"); 224 var prepopulated_data = { 225 'username': email 226 }; 227 user.showLoginForm(prepopulated_data); 228 229 } else if (data.incomplete_user) { 230 console.log("mailing list user"); 231 user.showPasswordRecovery(email); 232 233 } else { 234 // new customer, let them pass 235 } 236 237 }, 238 error: function(jqXHR, textStatus, errorThrown) { 239 console.log("Error in doing email check: " + errorThrown); 240 } 241 }); 242 } 243 } 244 return false; 245 } 246 247 this.submitUser = function() { 248 var user_id = $('#user_form input[name="user_id"]').val(); 249 var email = $('#user_form input[name="email"]').val(); 250 var first_name = $('#user_form input[name="first_name"]').val(); 251 var last_name = $('#user_form input[name="last_name"]').val(); 252 var password = $('#user_form input[name="password"]').val(); 253 var password_verify = $('#user_form input[name="password_verify"]').val(); 254 var subscribe = $('#user_form input[name="subscribe"]:checked').val(); 255 var submit_form = 1; 256 if (!user_id || ( user.user && user.user.fully_logged_in ) ) { 257 var message = ""; 258 259 if (! (email && first_name && last_name)) { 260 submit_form = 0; 261 message = "We‘re sorry, you must finish filling out the form."; 262 } 263 264 if ( (password || password_verify) && (password != password_verify)) { 265 submit_form = 0; 266 message = "Sorry, passwords need to match"; 267 } 268 269 if (submit_form) { 270 $.ajax({ 271 url: '/SecureUserSubmit.json', 272 dataType: 'jsonp', 273 data: { 274 'user_id': user_id, 275 'email': email, 276 'first_name': first_name, 277 'last_name': last_name, 278 'password': password, 279 'password_verify': password_verify, 280 'subscribe': subscribe 281 }, 282 success: function(data) { 283 var results = data; 284 if (data.success == 1) { 285 console.log("user data updated"); 286 if (user_id) { 287 // user edited 288 user.loadUser(); 289 user.showUserHome(1); 290 } else { 291 // new user needs an address 292 var callback = function(){user.showAddress("new");}; 293 user.loadUser(callback); 294 } 295 $("#home_message_area").html("User updates applied"); 296 } else { 297 console.log("error in editing user"); 298 var error_message = data.errors.join("<br />"); 299 $("#login_message_div").html("Errors in editing user:<br /> " + error_message); 300 } 301 302 }, 303 error: function(jqXHR, textStatus, errorThrown) { 304 console.log("Error in doing login: " + errorThrown); 305 $("#login_message_div").html("Error reaching server - please resubmit"); 306 } 307 }); 308 } else { 309 $("#login_message_div").html(message); 310 } 311 } else { 312 user.showLoginForm(); 313 } 314 }; 315 316 this.loadAddressesForCheckout = function() { 317 $.ajax({ 318 url: '/SecureAddresses.json', 319 dataType: 'jsonp', 320 success: function(data, textStatus, jqXHR) { 321 if (data.addresses) { 322 user.addresses = data.addresses; 323 cart.render_cart_address_form(); 324 } 325 }, 326 error: function(jqXHR, textStatus, errorThrown) { 327 // could not load user data - eat error and proceed 328 console.log("Error retrieving addresses"); 329 } 330 }); 331 } 332 333 this.showAddresses = function() { 334 if (user.user && user.user.fully_logged_in ) { 335 $.ajax({ 336 url: '/SecureAddresses.json', 337 dataType: 'jsonp', 338 success: function(data, textStatus, jqXHR) { 339 if (data.addresses) { 340 user.addresses = data.addresses; 341 } 342 var tmpl = $("#addresses_show_template").html(); 343 var template = Handlebars.compile(tmpl); 344 var data = { 345 addresses: user.addresses 346 }; 347 var text = template(data); 348 $("#user_dialog").html(text).dialog({modal: true, 349 closeText: "X", // doesn't work...? 350 closeOnEscape: true, 351 dialogClass: "no-close-button", 352 buttons: [{ 353 text: "close", 354 click: function() { 355 $( this ).dialog( "close" ); 356 //var form = $("#address_edit_form"); 357 //if(form){ 358 // form.submit(); 359 // setTimeout(function(){$(this).dialog("close");}, 0); 360 //}else{ 361 //$( this ).dialog( "close" ); 362 //} 363 } 364 }]}); 365 366 }, 367 error: function(jqXHR, textStatus, errorThrown) { 368 // could not load user data - eat error and proceed 369 } 370 }); 371 } else { 372 user.showLoginForm(); 373 } 374 }; 375 376 this.showAddress = function(id) { 377 if ( user.user && user.user.fully_logged_in ){ 378 var tmpl = $("#address_edit_template").html(); 379 var template = Handlebars.compile(tmpl); 380 var select_state = ""; 381 var select_country = ""; 382 var data = { 383 address : { id: 'new' } 384 }; 385 if (id) { 386 var address; 387 for (count in user.addresses) { 388 var test_address = user.addresses[count]; 389 console.log("testing address " + count + " against " + id + " == " + test_address.id); 390 if (test_address.id == id) { 391 address = test_address; 392 break; 393 } 394 } 395 if (address) { 396 data = { 397 address : address 398 }; 399 select_state = address.state || ""; 400 select_country = address.country || ""; 401 } else { 402 console.log("no address ID - creating new one"); 403 data = { 404 address: { 405 first_name: user.user.first_name, 406 last_name: user.user.last_name, 407 country: "US" 408 } 409 }; 410 } 411 } 412 var text = template(data); 413 $("#user_dialog").html(text).dialog({modal: true, position: 'center'}); 414 if (select_state && select_state != ""){ 415 $("select[name='state']").val(select_state); 416 } 417 if (select_country && select_country != ""){ 418 $("select[name='country']").val(select_country); 419 } 420 var target = $("#address_edit_form select[name='state']").parent(); 421 var us_vals = target.html(); 422 var v = select_state || ""; 423 var other_vals = "<input class='textbox' name='state' type='text' value='"+v+"' size='10' />"; 424 if(select_country && select_country != "US"){ 425 target.html(other_vals); 426 target.find("input").val(select_state); 427 } 428 $("#address_edit_form select[name='country']").change(function(){ 429 if($(this).val() == 'US'){ 430 target.html(us_vals); 431 }else{ 432 target.html(other_vals); 433 } 434 }); 435 $("#address_edit_form").submit( function(e){ 436 console.log("address edit form submitted"); 437 user.submitAddress(); 438 return false;
439 }); 440 } else { 441 user.showLoginForm(); 442 } 443 }; 444 445 this.submitAddress = function() { 446 if (user.user && user.user.fully_logged_in ) { 447 var address_id = $('#address_edit_form input[name="id"]').val(); 448 var title = $('#address_edit_form input[name="title"]').val(); 449 var first_name = $('#address_edit_form input[name="first_name"]').val(); 450 var last_name = $('#address_edit_form input[name="last_name"]').val(); 451 var street_address = $('#address_edit_form input[name="street_address"]').val(); 452 var city = $('#address_edit_form input[name="city"]').val(); 453 var state = $('#address_edit_form [name="state"]').val(); 454 var zip_code = $('#address_edit_form input[name="zip_code"]').val(); 455 var country = $('#address_edit_form select[name="country"]').val(); 456 var phone_number = $('#address_edit_form input[name="phone_number"]').val(); 457 var mobile_phone_number = $('#address_edit_form input[name="mobile_phone_number"]').val(); 458 var submit_form = 1; 459 460 if (! (street_address && city && phone_number)) { 461 submit_form = 0; 462 } 463 464 if (submit_form) { 465 $.ajax({ 466 url: '/SecureAddressSubmit.json', 467 dataType: 'jsonp', 468 data: { 469 'address_id': address_id, 470 'address_title': title, 471 'first_name': first_name, 472 'last_name': last_name, 473 'street_address': street_address, 474 'city': city, 475 'state': state, 476 'zip_code': zip_code, 477 'country': country, 478 'phone_number': phone_number, 479 'mobile_phone_number': mobile_phone_number 480 }, 481 success: function(data) { 482 var results = data; 483 if (data.success == 1) { 484 console.log("user data updated"); 485 user.showAddresses(); 486 $("#home_message_area").html("Address updates applied"); 487 user.loadUser(); 488 if (window.location.pathname == '/SecureCartCheckout') { 489 $("#user_dialog").dialog("close"); 490 user.loadAddressesForCheckout(); 491 } 492 } else { 493 console.log("error in editing user"); 494 var error_message = data.errors.join("<br />"); 495 $("#address_message_div").html("Errors in editing address:<br /> " + error_message); 496 } 497 498 }, 499 error: function(jqXHR, textStatus, errorThrown) { 500 console.log("Error in doing login: " + errorThrown); 501 $("#address_message_div").html("Error reaching server - please resubmit"); 502 } 503 }); 504 } else { 505 console.log("form incomplete"); 506 $("#address_message_div").html("We‘re sorry, you must finish filling out the form."); 507 } 508 } else { 509 user.showLoginForm(); 510 } 511 }; 512 513 this.deleteAddress = function(address_id) { 514 if (user.user && user.user.fully_logged_in ) { 515 if (address_id) { 516 $.ajax({ 517 url: '/SecureAddressSubmit.json', 518 dataType: 'jsonp', 519 data: { 520 'address_id': address_id, 521 'delete': 1 522 }, 523 success: function(data) { 524 var results = data; 525 if (data.success == 1) { 526 console.log("address deleted"); 527 user.showAddresses(); 528 $("#home_message_area").html("Address deleted"); 529 user.loadUser(); 530 if (window.location.pathname == '/SecureCartCheckout') { 531 $("#user_dialog").dialog("close"); 532 cart.loadAddressesForCheckout(); 533 } 534 } else { 535 console.log("error in deleting address"); 536 var error_message = data.errors.join("<br />"); 537 $("#address_message_div").html("Errors in deleting address:<br /> " + error_message); 538 } 539 540 }, 541 error: function(jqXHR, textStatus, errorThrown) { 542 console.log("Error in doing delete: " + errorThrown); 543 $("#address_message_div").html("Error reaching server - please resubmit"); 544 } 545 }); 546 } 547 } else { 548 user.showLoginForm(); 549 } 550 }; 551 552 this.showOrders = function() { 553 if (user.user && user.user.fully_logged_in ) { 554 alert("not implemented"); 555 } else { 556 user.showLoginForm(); 557 } 558 }; 559 560 this.showOrder = function(id) { 561 if (user.user && user.user.fully_logged_in ) { 562 alert("not implemented"); 563 } else { 564 user.showLoginForm(); 565 } 566 }; 567 568 this.showPasswordRecovery = function(email) { 569 var tmpl = $("#password_recovery_template").html(); 570 var template = Handlebars.compile(tmpl); 571 var data = { 572 email: email 573 }; 574 var text = template(data); 575 $("#user_dialog").html(text).dialog({modal: true}); 576 $("#password_recovery_popup").submit( function(e){ 577 console.log("password recovery form submitted"); 578 user.doPasswordRecovery(); 579 return false;
580 }); 581 }; 582 583 this.doPasswordRecovery = function() { 584 var email = $('#password_recovery_popup input[name="email"]').val(); 585 if (email) { 586 $.ajax({ 587 url: '/RecoverPasswordSubmit.json', 588 dataType: 'jsonp', 589 data: { 590 'email_address': email 591 }, 592 success: function(data) { 593 var results = data; 594 if (data.success == 1) { 595 $("#password_recovery_msg").html("Your password recovery email has been sent."); 596 } else { 597 console.log("error in sending password recovery"); 598 $("#password_recovery_msg").html("There was a problem sending your password recovery email. Please try again later."); 599 } 600 601 }, 602 error: function(jqXHR, textStatus, errorThrown) { 603 console.log("Error in doing login: " + errorThrown); 604 $("#password_recovery_msg").html("There was a problem sending your password recovery email - " + errorThrown); 605 } 606 }); 607 } else { 608 $("#password_recovery_msg").html("Please provide your email address so we can send you a password reset email."); 609 } 610 return false; 611 }; 612 613 this.cartSplash = function(prepopulated_data) { 614 var tmpl = $("#cart_splash_template").html(); 615 var template = Handlebars.compile(tmpl); 616 if ( user.user && user.user.fully_logged_in ) { 617 // good login - pass them along to the cart 618 window.location.href = "/SecureCartCheckout"; 619 } else { 620 var data; 621 if (prepopulated_data) { 622 data = prepopulated_data; 623 } else if ($.isEmptyObject(user.user)) { 624 data = {}; 625 } else { 626 data = { 627 'half_logged_in': 1, 628 'username': user.user.email 629 }; 630 } 631 var text = template(data); 632 $("#user_dialog").html(text).dialog({modal: true}); 633 $("#cart_splash_popup").submit( function(e){ 634 console.log("cart splash form submitted"); 635 user.cartSplashSubmit(); 636 return false; 637 }); 638 } 639 }; 640 641 this.cartSplashSubmit = function() { 642 var username = $('#cart_splash_popup input[name="username"]').val(); 643 var password = $('#cart_splash_popup input[name="password"]').val(); 644 var anon_ok = $('#cart_splash_popup input[name="anon_ok"]').val(); 645 646 if (anon_ok) { 647 // anon checkout - just pass them along 648 window.location.href = "/SecureCartCheckout?anon_ok=1"; 649 } else { 650 if (username && password) { 651 $.ajax({ 652 url: '/SecureLoginSubmit.json', 653 dataType: 'jsonp', 654 data: { 655 'username': username, 656 'password': password 657 }, 658 success: function(data) { 659 var results = data; 660 if (data.success == 1) { 661 $("#user_dialog").dialog("close"); 662 // send them on their way to the checkout 663 var callback = function(){window.location.href="/SecureCartCheckout";}; 664 user.loadUser(callback); 665 } else { 666 console.log("error in logging in - " + data.error); 667 $("#login_message_div").html("ERROR: " + data.error); 668 } 669 670 }, 671 error: function(jqXHR, textStatus, errorThrown) { 672 console.log("Error in doing login: " + errorThrown); 673 $("#login_message_div").html("Problem with reaching the server - please try again"); 674 } 675 }); 676 } else { 677 $("#login_message_div").html("Please provide a username and password to log in"); 678 } 679 } 680 return false; 681 }; 682 683 return this; 684 685}
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.