From ce31c64f840c4b70ad43b278b477a65899177e0e Mon Sep 17 00:00:00 2001 From: "James D. Forrester" Date: Fri, 25 Mar 2016 11:24:43 -0700 Subject: [PATCH] jQuery.IME: Bump to master (b6102fb) New changes: 9316542 Remove trailing whitespace and superfluous newlines 1db02ed Improve language and method selection API 22ce72a Merge pull request #434 from divec/api 965c94e Update README and example b6102fb Remove the unused showSelector property of defaultInputMethod Change-Id: Icc78795fe1ece63b29ea3863ba1902519c818841 --- lib/jquery.ime/jquery.ime.js | 170 ++++++++++++++++++++++++++--------- 1 file changed, 129 insertions(+), 41 deletions(-) diff --git a/lib/jquery.ime/jquery.ime.js b/lib/jquery.ime/jquery.ime.js index 8173af88..505674dd 100644 --- a/lib/jquery.ime/jquery.ime.js +++ b/lib/jquery.ime/jquery.ime.js @@ -1,4 +1,4 @@ -/*! jquery.ime - v0.1.0+20160129 +/*! jquery.ime - v0.1.0+20160325 * https://github.com/wikimedia/jquery.ime * Copyright (c) 2016 Santhosh Thottingal; Licensed GPL, MIT */ ( function ( $ ) { @@ -51,12 +51,20 @@ // This needs to be delayed here since extending language list happens at DOM ready $.ime.defaults.languages = arrayKeys( $.ime.languages ); this.options = $.extend( {}, $.ime.defaults, options ); + if ( this.options.imePath ) { + // Set the global IME path from the one specified to the instance + // TODO: remove this functionality and force clients to set the global + // IME path + $.ime.path = this.options.imePath; + } this.active = false; this.shifted = false; this.inputmethod = null; this.language = null; this.context = ''; - this.selector = this.$element.imeselector( this.options ); + if ( this.options.showSelector ) { + this.selector = this.$element.imeselector( this.options ); + } this.listen(); } @@ -75,6 +83,61 @@ this.$element.on( 'disable.ime', $.proxy( this.disable, this ) ); }, + /** + * Return a list of available language codes + * + * @return {string[]} Available language codes + */ + getLanguageCodes: function () { + return $.ime.defaults.languages; + }, + + /** + * Return the autonym for an available language code + * + * @param {string} languageCode The language code + * @return {string} The autonym + */ + getAutonym: function ( languageCode ) { + return $.ime.languages[ languageCode ].autonym; + }, + + /** + * Return a list of available input method ids for a language + * + * @param {string} languageCode An available language code + * @return {string[]} Available input method ids for that language + */ + getInputMethodIds: function ( languageCode ) { + return $.ime.languages[ languageCode ].inputmethods; + }, + + /** + * Return the name of an input method + * + * @param {string} inputMethodId The id of an input method + * @return {string} The input method's name + * @see IME#load + */ + getInputMethodName: function ( inputMethodId ) { + return $.ime.sources[ inputMethodId ].name; + }, + + /** + * Return a list of input method info { id: ..., name: ... } for a language. + * + * @param {string} languageCode An available language code + * @return {Object[]} Info object for each available input method + */ + getInputMethods: function ( languageCode ) { + return this.getInputMethodIds( languageCode ).map( function ( inputMethodId ) { + return { + id: inputMethodId, + name: $.ime.sources[ inputMethodId ].name + }; + } ); + }, + /** * Transliterate a given string input based on context and input method definition. * If there are no matching rules defined, returns the original string. @@ -273,15 +336,18 @@ /** * Set the current input method * @param {string} inputmethodId + * @fires imeLanguageChange */ setIM: function ( inputmethodId ) { this.inputmethod = $.ime.inputmethods[inputmethodId]; $.ime.preferences.setIM( inputmethodId ); + this.$element.trigger( 'imeMethodChange' ); }, /** * Set the current Language * @param {string} languageCode + * @fires imeLanguageChange * @returns {Boolean} */ setLanguage: function ( languageCode ) { @@ -293,6 +359,7 @@ this.language = languageCode; $.ime.preferences.setLanguage( languageCode ); + this.$element.trigger( 'imeLanguageChange' ); return true; }, @@ -310,43 +377,8 @@ * @return {jQuery.Promise} */ load: function ( inputmethodId ) { - var ime = this, - deferred = $.Deferred(), - dependency; - - if ( $.ime.inputmethods[inputmethodId] ) { - return deferred.resolve(); - } - - // Validate the input method id. - if ( !$.ime.sources[inputmethodId] ) { - return deferred.reject(); - } - - dependency = $.ime.sources[inputmethodId].depends; - if ( dependency && !$.ime.inputmethods[dependency] ) { - ime.load( dependency ).done( function () { - ime.load( inputmethodId ).done( function () { - deferred.resolve(); - } ); - } ); - - return deferred; - } - - debug( 'Loading ' + inputmethodId ); - deferred = $.ajax( { - url: ime.options.imePath + $.ime.sources[inputmethodId].source, - dataType: 'script', - cache: true - } ).done( function () { - debug( inputmethodId + ' loaded' ); - } ).fail( function ( jqxhr, settings, exception ) { - debug( 'Error in loading inputmethod ' + inputmethodId + ' Exception: ' + exception ); - } ); - - return deferred.promise(); - }, + return $.ime.load( inputmethodId ); + } }; /** @@ -746,6 +778,10 @@ $.ime.preferences = {}; $.ime.languages = {}; + /** + * @property {string} Relative/absolute path for the rules folder of jquery.ime + */ + $.ime.path = '../'; $.ime.textEntryFactory = TextEntryFactory.static.singleton; $.ime.TextEntry = TextEntry; $.ime.inheritClass = inheritClass; @@ -755,15 +791,67 @@ maxKeyLength: 1 }; + /** + * load an input method by given id + * @param {string} inputmethodId + * @return {jQuery.Promise} + */ + $.ime.load = function ( inputmethodId ) { + var dependency, + deferred = $.Deferred(); + + if ( $.ime.inputmethods[inputmethodId] ) { + return deferred.resolve(); + } + + // Validate the input method id. + if ( !$.ime.sources[inputmethodId] ) { + return deferred.reject(); + } + + dependency = $.ime.sources[inputmethodId].depends; + if ( dependency && !$.ime.inputmethods[dependency] ) { + $.ime.load( dependency ).done( function () { + $.ime.load( inputmethodId ).done( function () { + deferred.resolve(); + } ); + } ); + + return deferred; + } + + debug( 'Loading ' + inputmethodId ); + deferred = $.ajax( { + url: $.ime.path + $.ime.sources[inputmethodId].source, + dataType: 'script', + cache: true + } ).done( function () { + debug( inputmethodId + ' loaded' ); + } ).fail( function ( jqxhr, settings, exception ) { + debug( 'Error in loading inputmethod ' + inputmethodId + ' Exception: ' + exception ); + } ); + + return deferred.promise(); + }; + $.ime.register = function ( inputMethod ) { $.ime.inputmethods[inputMethod.id] = $.extend( {}, defaultInputMethod, inputMethod ); }; + /** + * Set the relative/absolute path to rules/ (for loading input methods) + * + * @param {string} path The relative/absolute path in which rules/ lies + */ + $.ime.setPath = function ( path ) { + $.ime.path = path; + }; + // default options $.ime.defaults = { - imePath: '../', // Relative/Absolute path for the rules folder of jquery.ime languages: [], // Languages to be used- by default all languages - helpHandler: null // Called for each ime option in the menu + helpHandler: null, // Called for each ime option in the menu + showSelector: true }; /**