| Conditions | 1 |
| Paths | 2 |
| Total Lines | 113 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** global: GLSR, grecaptcha */ |
||
| 2 | ;(function() { |
||
| 3 | |||
| 4 | 'use strict'; |
||
| 5 | |||
| 6 | GLSR.Recaptcha = function( form ) { // Form object |
||
| 7 | this.Form = form; |
||
| 8 | }; |
||
| 9 | |||
| 10 | GLSR.Recaptcha.prototype = { |
||
| 11 | config: { |
||
| 12 | recaptchaSelector: '.glsr-recaptcha-holder', |
||
| 13 | }, |
||
| 14 | |||
| 15 | /** @return void */ |
||
| 16 | addListeners_: function() { |
||
| 17 | var overlayEl = this.getOverlay_(); |
||
| 18 | if( overlayEl === -1 )return; |
||
| 19 | overlayEl.addEventListener( 'click', this.Form.enableButton_ ); |
||
| 20 | window.addEventListener( 'keyup', this.onKeyup_.bind( this, overlayEl )); |
||
| 21 | }, |
||
| 22 | |||
| 23 | /** @return void */ |
||
| 24 | execute_: function() { |
||
| 25 | var recaptchaId = this.getId_(); |
||
| 26 | if( recaptchaId !== -1 ) { |
||
| 27 | grecaptcha.execute( recaptchaId ); |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | // recaptcha ID not found so pass through an error |
||
| 31 | this.Form.submitForm_( false ); |
||
| 32 | }, |
||
| 33 | |||
| 34 | /** @return string|int (-1) */ |
||
| 35 | getId_: function() { |
||
| 36 | return this.search_( function( value, id ) { |
||
| 37 | if( Object.prototype.toString.call( value ) !== '[object HTMLDivElement]' )return; |
||
| 38 | if( value.closest( 'form' ) === this.Form.form ) { |
||
| 39 | return id; |
||
| 40 | } |
||
| 41 | }.bind( this )); |
||
| 42 | }, |
||
| 43 | |||
| 44 | /** @return HTMLDivElement|int (-1) */ |
||
| 45 | getOverlay_: function() { |
||
| 46 | return this.search_( function( value ) { |
||
| 47 | if( Object.prototype.toString.call( value ) !== '[object Object]' )return; |
||
| 48 | for( var obj in value) { |
||
| 49 | if( !value.hasOwnProperty( obj ) || Object.prototype.toString.call( value[obj] ) !== '[object HTMLDivElement]' )continue; |
||
| 50 | if( value[obj].className === '' ) { |
||
| 51 | return value[obj].firstChild; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | return false; |
||
| 55 | }.bind( this )); |
||
|
|
|||
| 56 | }, |
||
| 57 | |||
| 58 | /** @return void */ |
||
| 59 | onKeyup_: function( ev ) { // KeyboardEvent |
||
| 60 | if( ev.keyCode !== 27 )return; |
||
| 61 | this.Form.enableButton_(); |
||
| 62 | this.removeListeners_( ev.target ); |
||
| 63 | }, |
||
| 64 | |||
| 65 | /** @return void */ |
||
| 66 | removeListeners_: function( overlayEl ) { // HTMLDivElement |
||
| 67 | overlayEl.removeEventListener( 'click', this.Form.enableButton_ ); |
||
| 68 | window.removeEventListener( 'keyup', this.onKeyup_ ); |
||
| 69 | }, |
||
| 70 | |||
| 71 | /** @return void */ |
||
| 72 | render_: function() { |
||
| 73 | this.Form.form.onsubmit = null; |
||
| 74 | var recaptchaEl = this.Form.form.querySelector( this.config.recaptchaSelector ); |
||
| 75 | if( !recaptchaEl )return; |
||
| 76 | recaptchaEl.innerHTML = ''; |
||
| 77 | var id = grecaptcha.render( recaptchaEl, { |
||
| 78 | callback: this.submitForm_.bind( this ), |
||
| 79 | 'expired-callback': function() { |
||
| 80 | grecaptcha.reset( id ); |
||
| 81 | }, |
||
| 82 | }, true ); |
||
| 83 | }, |
||
| 84 | |||
| 85 | /** @return void */ |
||
| 86 | reset_: function() { |
||
| 87 | var recaptchaId = this.getId_(); |
||
| 88 | if( recaptchaId !== -1 ) { |
||
| 89 | grecaptcha.reset( recaptchaId ); |
||
| 90 | } |
||
| 91 | }, |
||
| 92 | |||
| 93 | /** @return mixed|int (-1) */ |
||
| 94 | search_: function( callback ) { // function |
||
| 95 | var result = -1; |
||
| 96 | if( window.hasOwnProperty( '___grecaptcha_cfg' )) { |
||
| 97 | var clients = window.___grecaptcha_cfg.clients; |
||
| 98 | var i, key; |
||
| 99 | for( i in clients ) { |
||
| 100 | for( key in clients[i] ) { |
||
| 101 | if( !( result = callback( clients[i][key], i )))continue; |
||
| 102 | return result; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | return result; |
||
| 107 | }, |
||
| 108 | |||
| 109 | /** @return void */ |
||
| 110 | submitForm_: function( token ) { // string |
||
| 111 | this.Form.submitForm_( token ); |
||
| 112 | }, |
||
| 113 | }; |
||
| 114 | })(); |
||
| 115 |