Clean up code formatting, add .jsbeautifyrc

Change-Id: Iafcc8084d87d8f2323d97bb4446e031951820d41
This commit is contained in:
Santhosh Thottingal
2016-05-09 16:40:55 +05:30
parent 35cd1b5736
commit 29f28a1f50
2 changed files with 101 additions and 80 deletions

17
.jsbeautifyrc Normal file
View File

@@ -0,0 +1,17 @@
{
"preserve_newlines": true,
"jslint_happy": true,
"keep_array_indentation": true,
"space_before_conditional": true,
"max_preserve_newlines": 10,
"brace_style": "collapse",
"keep_function_indentation": false,
"break_chained_methods": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0,
"space_in_paren": true,
"space_in_square_bracket": true,
"git_happy": true,
"indent_with_tabs": true
}

View File

@@ -22,12 +22,12 @@
* The values for autocompletion is from the options.languages. * The values for autocompletion is from the options.languages.
* The data is in the format of languagecode:languagename. * The data is in the format of languagecode:languagename.
*/ */
(function ( $ ) { ( function ( $ ) {
'use strict'; 'use strict';
var LanguageFilter, delay; var LanguageFilter, delay;
LanguageFilter = function( element, options ) { LanguageFilter = function ( element, options ) {
this.$element = $( element ); this.$element = $( element );
this.options = $.extend( {}, $.fn.languagefilter.defaults, options ); this.options = $.extend( {}, $.fn.languagefilter.defaults, options );
this.$element.addClass( 'languagefilter' ); this.$element.addClass( 'languagefilter' );
@@ -39,17 +39,17 @@
this.listen(); this.listen();
}; };
delay = ( function() { delay = ( function () {
var timer = 0; var timer = 0;
return function( callback, milliseconds ) { return function ( callback, milliseconds ) {
clearTimeout( timer ); clearTimeout( timer );
timer = setTimeout( callback, milliseconds ); timer = setTimeout( callback, milliseconds );
}; };
} () ); }() );
LanguageFilter.prototype = { LanguageFilter.prototype = {
listen: function() { listen: function () {
this.$element.on( 'keypress', $.proxy( this.keyup, this ) ) this.$element.on( 'keypress', $.proxy( this.keyup, this ) )
.on( 'keyup', $.proxy( this.keyup, this ) ); .on( 'keyup', $.proxy( this.keyup, this ) );
@@ -58,68 +58,68 @@
} }
if ( this.$clear.length ) { if ( this.$clear.length ) {
this.$clear.on( 'click' , $.proxy( this.clear, this ) ); this.$clear.on( 'click', $.proxy( this.clear, this ) );
} }
this.toggleClear(); this.toggleClear();
}, },
keyup: function( e ) { keyup: function ( e ) {
var suggestion, query, languageFilter; var suggestion, query, languageFilter;
switch( e.keyCode ) { switch ( e.keyCode ) {
case 9: // Tab -> Autocomplete case 9: // Tab -> Autocomplete
suggestion = this.$suggestion.val(); suggestion = this.$suggestion.val();
if ( suggestion && suggestion !== this.$element.val() ) { if ( suggestion && suggestion !== this.$element.val() ) {
this.$element.val( suggestion ); this.$element.val( suggestion );
e.preventDefault();
e.stopPropagation();
}
break;
case 13: // Enter
if ( !this.options.onSelect ) {
break;
}
// Avoid bubbling this 'enter' to background page elements
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
}
query = $.trim( this.$element.val() ).toLowerCase(); break;
case 13: // Enter
if ( this.selectedLanguage ) { if ( !this.options.onSelect ) {
// this.selectLanguage will be populated from a matching search
this.options.onSelect( this.selectedLanguage );
} else if ( this.options.languages[query] ) {
// Search is yet to happen (in timeout delay),
// but we have a matching language code.
this.options.onSelect( query );
}
break; break;
default: }
languageFilter = this;
if ( e.which < 32 && // Avoid bubbling this 'enter' to background page elements
e.which !== 8 // Backspace e.preventDefault();
) { e.stopPropagation();
// ignore any ASCII control characters
break; query = $.trim( this.$element.val() ).toLowerCase();
if ( this.selectedLanguage ) {
// this.selectLanguage will be populated from a matching search
this.options.onSelect( this.selectedLanguage );
} else if ( this.options.languages[ query ] ) {
// Search is yet to happen (in timeout delay),
// but we have a matching language code.
this.options.onSelect( query );
}
break;
default:
languageFilter = this;
if ( e.which < 32 &&
e.which !== 8 // Backspace
) {
// ignore any ASCII control characters
break;
}
this.selectedLanguage = null;
delay( function () {
if ( !languageFilter.$element.val() ) {
languageFilter.clear();
} else {
languageFilter.options.$target.empty();
languageFilter.search();
} }
}, 300 );
this.selectedLanguage = null; this.toggleClear();
delay( function() {
if ( !languageFilter.$element.val() ) {
languageFilter.clear();
} else {
languageFilter.options.$target.empty();
languageFilter.search();
}
}, 300 );
this.toggleClear();
} }
}, },
@@ -127,7 +127,7 @@
* Clears the current search removing * Clears the current search removing
* clear buttons and suggestions. * clear buttons and suggestions.
*/ */
deactivate: function() { deactivate: function () {
this.$element.val( '' ); this.$element.val( '' );
if ( !$.fn.uls.Constructor.prototype.isMobile() ) { if ( !$.fn.uls.Constructor.prototype.isMobile() ) {
@@ -141,7 +141,7 @@
/** /**
* Clears the search and shows all languages * Clears the search and shows all languages
*/ */
clear: function() { clear: function () {
this.deactivate(); this.deactivate();
this.$element.trigger( 'searchclear.uls' ); this.$element.trigger( 'searchclear.uls' );
}, },
@@ -150,7 +150,7 @@
* Toggles the visibility of clear icon depending * Toggles the visibility of clear icon depending
* on whether there is anything to clear. * on whether there is anything to clear.
*/ */
toggleClear: function() { toggleClear: function () {
if ( !this.$clear.length ) { if ( !this.$clear.length ) {
return; return;
} }
@@ -186,18 +186,20 @@
} }
// Also do a search by search API // Also do a search by search API
if( !this.resultCount && this.options.searchAPI && query ) { if ( !this.resultCount && this.options.searchAPI && query ) {
this.searchAPI( query ); this.searchAPI( query );
} else { } else {
this.resultHandler( query ); this.resultHandler( query );
} }
}, },
searchAPI: function( query ) { searchAPI: function ( query ) {
var languageFilter = this; var languageFilter = this;
$.get( languageFilter.options.searchAPI, { search: query }, function( result ) { $.get( languageFilter.options.searchAPI, {
$.each( result.languagesearch, function( code, name ) { search: query
}, function ( result ) {
$.each( result.languagesearch, function ( code, name ) {
if ( languageFilter.resultCount === 0 ) { if ( languageFilter.resultCount === 0 ) {
// Autofill the first result. // Autofill the first result.
languageFilter.autofill( code, name ); languageFilter.autofill( code, name );
@@ -219,16 +221,18 @@
* Based on search result triggers resultsfound or noresults events * Based on search result triggers resultsfound or noresults events
* @param query string * @param query string
*/ */
resultHandler: function( query ) { resultHandler: function ( query ) {
if ( this.resultCount === 0 ) { if ( this.resultCount === 0 ) {
this.$suggestion.val( '' ); this.$suggestion.val( '' );
this.$element.trigger( 'noresults.uls', query ); this.$element.trigger( 'noresults.uls', query );
} else { } else {
this.$element.trigger( 'resultsfound.uls', [query, this.resultCount] ); this.$element.trigger( 'resultsfound.uls', [ query, this.resultCount ] );
} }
}, },
autofill: function( langCode, languageName ) { autofill: function ( langCode, languageName ) {
var autonym, userInput, suggestion;
if ( !this.$suggestion.length ) { if ( !this.$suggestion.length ) {
return; return;
} }
@@ -239,15 +243,14 @@
} }
this.selectedLanguage = langCode; this.selectedLanguage = langCode;
languageName = languageName || this.options.languages[langCode]; languageName = languageName || this.options.languages[ langCode ];
if ( !languageName ) { if ( !languageName ) {
return; return;
} }
var autonym, userInput = this.$element.val();
userInput = this.$element.val(), suggestion = userInput + languageName.substring( userInput.length, languageName.length );
suggestion = userInput + languageName.substring( userInput.length, languageName.length );
if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) { if ( suggestion.toLowerCase() !== languageName.toLowerCase() ) {
// see if it was autonym match // see if it was autonym match
@@ -268,7 +271,7 @@
this.$suggestion.val( suggestion ); this.$suggestion.val( suggestion );
}, },
render: function( langCode ) { render: function ( langCode ) {
var $target = this.options.$target; var $target = this.options.$target;
if ( !$target ) { if ( !$target ) {
@@ -278,7 +281,7 @@
return $target.append( langCode ); return $target.append( langCode );
}, },
escapeRegex: function( value ) { escapeRegex: function ( value ) {
return value.replace( /[\-\[\]{}()*+?.,\\\^$\|#\s]/g, '\\$&' ); return value.replace( /[\-\[\]{}()*+?.,\\\^$\|#\s]/g, '\\$&' );
}, },
@@ -290,10 +293,10 @@
* c) ISO 639 code match with search string. * c) ISO 639 code match with search string.
* d) ISO 15924 code for the script match the search string. * d) ISO 15924 code for the script match the search string.
*/ */
filter: function( langCode, searchTerm ) { filter: function ( langCode, searchTerm ) {
// FIXME script is ISO 15924 code. We might need actual name of script. // FIXME script is ISO 15924 code. We might need actual name of script.
var matcher = new RegExp( '^' + this.escapeRegex( searchTerm ), 'i' ), var matcher = new RegExp( '^' + this.escapeRegex( searchTerm ), 'i' ),
languageName = this.options.languages[langCode]; languageName = this.options.languages[ langCode ];
return matcher.test( languageName ) || return matcher.test( languageName ) ||
matcher.test( $.uls.data.getAutonym( langCode ) ) || matcher.test( $.uls.data.getAutonym( langCode ) ) ||
@@ -306,15 +309,15 @@
if ( !isSupported ) { if ( !isSupported ) {
this.$element.setAttribute( eventName, 'return;' ); this.$element.setAttribute( eventName, 'return;' );
isSupported = typeof this.$element[eventName] === 'function'; isSupported = typeof this.$element[ eventName ] === 'function';
} }
return isSupported; return isSupported;
} }
}; };
$.fn.languagefilter = function( option ) { $.fn.languagefilter = function ( option ) {
return this.each( function() { return this.each( function () {
var $this = $( this ), var $this = $( this ),
data = $this.data( 'languagefilter' ), data = $this.data( 'languagefilter' ),
options = typeof option === 'object' && option; options = typeof option === 'object' && option;
@@ -324,7 +327,7 @@
} }
if ( typeof option === 'string' ) { if ( typeof option === 'string' ) {
data[option](); data[ option ]();
} }
} ); } );
}; };
@@ -340,13 +343,14 @@
/** /**
* Check if a prefix is visually prefix of a string * Check if a prefix is visually prefix of a string
* @param prefix string *
* @param string string * @param {string} prefix
* @param {string} string
*/ */
function isVisualPrefix( prefix, string ) { function isVisualPrefix( prefix, string ) {
// Pre-base vowel signs of Indic languages. A vowel sign is called pre-base if // Pre-base vowel signs of Indic languages. A vowel sign is called pre-base if
// consonant + vowel becomes [vowel][consonant] when rendered. Eg: ക + െ => കെ // consonant + vowel becomes [vowel][consonant] when rendered. Eg: ക + െ => കെ
var prebases = 'െേൈൊോൌெேைொோௌେୈୋୌિਿिিেৈোৌෙේෛොෝෞ'; var prebases = 'െേൈൊോൌெேைொோௌେୈୋୌિਿिিেৈোৌෙේෛොෝෞ';
return prebases.indexOf( string[prefix.length] ) <= 0; return prebases.indexOf( string[ prefix.length ] ) <= 0;
} }
} ( jQuery ) ); }( jQuery ) );