1var Router = Backbone.Router.extend({ 2 3 routes: { 4 "create": "create", 5 "forgot": "forgot", 6 "reset/:t1/:t2/:t3": "reset", 7 "": "login" 8 } 9 10}); 11 12var ConventionalLoginView = Backbone.View.extend({ 13 14 events: { 15 'click .goto-screen' : 'gotoScreen', 16 'submit form' : 'submitForm', 17 'click #close': 'returnToMain' 18 }, 19 20 // To start, set view to point to all of settings screen, cache groups 21 initialize: function() { 22 this.setElement($('#content')[0]); 23 this.$screens = this.$('.screen'); 24 this.$currentScreen = this.$screens.filter(':visible').eq(0); 25 this.router = new Router(); 26 this.router.on('route', _.bind(this.gotoScreen, this)); 27 this.email = ''; 28 Backbone.history.start({pushState: false, root: "/provider/conventional_login/"}); 29 }, 30 31 // Switch to screen specificied in the element target-screen 32 gotoScreen: function(screenName, params) { 33 34 var $targetScreen = this.$screens.filter('#screen-' + screenName).eq(0); 35 var emailSelector = 'input[name="email"]'; 36 37 // Reset form of the current screen 38 var $currentForm = this.$currentScreen.find('form'); 39 if ($currentForm.length > 0) { 40 this.email = $currentForm.find(emailSelector).val(); 41 $currentForm.eq(0)[0].reset(); 42 } 43 44 // Hide all screens, show the target one 45 this.$screens.hide(); 46 $targetScreen.show(); 47 48 // Transfer over email if it was filled in on previous forms 49 $targetScreen.find(emailSelector).val(this.email); 50 51 // Highlight first form field 52 $targetScreen.find('input:visible').eq(0).focus(); 53 54 // Reset screen also needs to fill in some info 55 if (screenName == 'reset') { 56 $targetScreen.find('input[name="t1"]').val(params[0]); 57 $targetScreen.find('input[name="t2"]').val(params[1]); 58 $targetScreen.find('input[name="t3"]').val(params[2]); 59 } 60 61 this.$currentScreen = $targetScreen; 62 this.errorMessage('hide'); 63 $(window).resize(); 64 }, 65 66 67 /** 68 * Show all is good and redirect parent to main page. 69 */ 70 loginSuccess: function() { 71 this.gotoScreen('success'); 72 this.returnToMain(); 73 }, 74 75 returnToMain: function() { 76 // Lifted from the provider module's client_close_and_refresh_parent.php 77 setTimeout(function() { 78 // var opener = window.opener; 79// 80 // // Workaround for Chrome when site installed as app: http://code.google.com/p/chromium/issues/detail?id=65953 81 // if (opener == null) 82 // opener = window.open("", "wtb-main-window"); 83// 84 // // If opener is still null, assume it's in the current window 85 // if (opener == null) { 86 // IE doesn't have .origin 87 if (!window.location.origin) { 88 window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); 89 } 90 window.location.href = window.location.origin; 91 // } else { 92 // opener.location.reload(true); 93 // window.close() 94 // } 95 }, 500); 96 }, 97 98 forgotSuccess: function() { 99 this.gotoScreen('reminded'); 100 }, 101 102 /** 103 * Shows a specific error message on the $currentScreen. Hides on all screens. 104 */ 105 errorMessage: function(visibility, action, message) { 106 this.$('.error-msg').remove(); 107 if (visibility == 'show') { 108 var $form = this.$currentScreen.find('form'); 109 var $msg = $('<div class="error-msg">' + message + '</div>'); 110 setTimeout(()=>{$form.prepend( $msg )}, 0); // this allows the previously removed error message to be removed before adding the new one 111 } 112 $(window).resize(); 113 }, 114 115 showVerificationField: function() { 116 $vcodeField = this.$currentScreen.find('[name="vcode"]'); 117 if ($vcodeField.length == 0) { 118 $vcodeField = $('<input type="text" name="vcode" placeholder="Verification Code" />'); 119 this.$currentScreen.find('[name="email"]').eq(0).after($vcodeField); 120 $vcodeField.focus(); 121 } 122 123 }, 124 125 /** 126 * Submit forms via a POST method and handles responses. 127 */ 128 submitForm: function(e) { 129 130 e.preventDefault(); 131 132 var $form = $(e.target); 133 var $submit = $form.find('[type="submit"]'); 134 var submitValue = $submit.val(); 135 var data = $form.serialize(); 136 var that = this; 137 138 $submit.attr('disabled', 1).val('Procesing...'); 139 140 // Once we start processing, hide any error messages that are showing. 141 // This type of 'hiding' keeps the vertical space of the error message. 142 // This way the 'Processing...' button doesn't jump around when the error message is removed. 143 var $errorMsg = this.$('.error-msg'); 144 if ($errorMsg.length > 0) { 145 $errorMsg.addClass('hidden'); 146 } 147 148 $.ajax({
149 type: 'post', 150 timeout: 10000, 151 dataType: 'json', 152 url: window.location.href, 153 data: data, 154 complete: function () { 155 $submit.removeAttr('disabled').val(submitValue); 156 }, 157 success: function (data, textStatus) { 158 if (typeof data == 'object') { 159 if (data.success == 1) { 160 that.errorMessage('hide'); 161 switch (data.action) { 162 case 'login': that.loginSuccess(); break; 163 case 'create': that.loginSuccess(); break; 164 case 'reset1': that.forgotSuccess(); break; 165 case 'reset2': that.loginSuccess(); break; 166 default: console.error('Unknown login action: ' + data.action); 167 } 168 } else { 169 // result=2 means - verification field is needed 170 if (data.action == 'create' && data.result == '2') { 171 that.showVerificationField(); 172 } 173 // any error for 'create' action means - refresh captcha 174 else if (data.action == 'create') { 175 const img = document.querySelector('#captcha-container>img'); 176 const cpt = document.querySelector('#captcha-container>input'); 177 const url = img.src.split('?')[0]; 178 img.src = url + '?' + Math.random(); 179 cpt.value = ''; 180 } 181 that.errorMessage('show', data.action, data.message); 182 } 183 } else { 184 that.errorMessage('show', null, 'Invalid response from the server.'); 185 } 186 }, 187 error: function () { 188 that.errorMessage('show', null, 'Cannot talk to the server.'); 189 } 190 }); 191 } 192 193});
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.