autocomplete-sources-debug.js revision 76ca635d61eb3f9fb7c9d788a44fa8b1690aa138
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Mixes support for JSONP and YQL result sources into AutoCompleteBase.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @module autocomplete
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @submodule autocomplete-sources
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass// Add prototype properties and methods to AutoCompleteBase.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Regular expression used to determine whether a String source is a YQL
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @property _YQL_SOURCE_REGEX
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type RegExp
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Runs before AutoCompleteBase's <code>_createObjectSource()</code> method
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * and augments it to support additional object-based source types.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _beforeCreateObjectSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} source
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // If the object is a <select> node, use the options as the result
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass source.get('nodeName').toLowerCase() === 'select') {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // If the object is a JSONPRequest instance, try to use it as a JSONP
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (Y.JSONPRequest && source instanceof Y.JSONPRequest) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Fall back to a basic object source.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Creates a DataSource-like object that uses <code>Y.io</code> as a source.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * See the <code>source</code> attribute for more details.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _createIOSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} source URL.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Object} DataSource-like object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Private internal _sendRequest method that will be assigned to
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // ioSource.sendRequest once io-base and json-parse are available.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Return immediately on a cached response.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Cancel any outstanding requests.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass ioRequest = Y.io(that._getXHRUrl(source, query), {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass } catch (ex) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Keep track of the most recent request in case there are multiple
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // requests while we're waiting for the IO module to load. Only the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // most recent request will be sent.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (loading) { return; }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Lazy-load the io-base and json-parse modules if necessary,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // then overwrite the sendRequest method to bypass this check in
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // the future.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Creates a DataSource-like object that uses the specified JSONPRequest
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * instance as a source. See the <code>source</code> attribute for more
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _createJSONPSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {JSONPRequest|String} source URL string or JSONPRequest instance.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Object} DataSource-like object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Hack alert: JSONPRequest currently doesn't support
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // per-request callbacks, so we're reaching into the protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // _config object to make it happen.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // This limitation is mentioned in the following JSONP
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // enhancement ticket:
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // http://yuilibrary.com/projects/yui3/ticket/2529371
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Keep track of the most recent request in case there are multiple
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // requests while we're waiting for the JSONP module to load. Only
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // the most recent request will be sent.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (loading) { return; }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Lazy-load the JSONP module if necessary, then overwrite the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // sendRequest method to bypass this check in the future.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Turn the source into a JSONPRequest instance if it isn't
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // one already.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Creates a DataSource-like object that uses the specified <select>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * node as a source.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _createSelectSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Node} source YUI Node instance wrapping a <select> node.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Object} DataSource-like object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Creates a DataSource-like object that calls the specified URL or
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * executes the specified YQL query for results. If the string starts
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * with "select ", "use ", or "set " (case-insensitive), it's assumed to be
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * a YQL query; otherwise, it's assumed to be a URL (which may be absolute
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * or relative). URLs containing a "{callback}" placeholder are assumed to
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * be JSONP URLs; all others will use XHR. See the <code>source</code>
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * attribute for more details.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _createStringSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} source URL or YQL query.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Object} DataSource-like object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Looks like a YQL query.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Contains a {callback} param and isn't a YQL query, so it must be
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Not a YQL query or JSONP, so we'll assume it's an XHR URL.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Creates a DataSource-like object that uses the specified YQL query string
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * to create a YQL-based source. See the <code>source</code> attribute for
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * details. If no <code>resultListLocator</code> is defined, this method
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * will set a best-guess locator that might work for many typical YQL
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _createYQLSource
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} source YQL query.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Object} DataSource-like object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.set(RESULT_LIST_LOCATOR, this._defaultYQLLocator);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Only create a new YQLRequest instance if this is the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // first request. For subsequent requests, we'll reuse the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // original instance.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass allowCache: false // temp workaround until JSONP has per-URL callback proxies
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Keep track of the most recent request in case there are multiple
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // requests while we're waiting for the YQL module to load. Only the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // most recent request will be sent.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Lazy-load the YQL module if necessary, then overwrite the
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // sendRequest method to bypass this check in the future.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Default resultListLocator used when a string-based YQL source is set and
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * the implementer hasn't already specified one.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _defaultYQLLocator
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {Object} response YQL response object.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {Array}
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var results = response && response.query && response.query.results,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // If there's only a single value on YQL's results object, that
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // value almost certainly contains the array of results we want. If
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // there are 0 or 2+ values, then the values themselves are most
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // likely the results we want.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass results = values.length === 1 ? values[0] : values;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Returns a formatted XHR URL based on the specified base <i>url</i>,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * <i>query</i>, and the current <i>requestTemplate</i> if any.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _getXHRUrl
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} url Base URL.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} query AutoComplete query.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {String} Formatted URL.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * URL formatter passed to <code>JSONPRequest</code> instances.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _jsonpFormatter
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} url
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} proxy
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param {String} query
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @return {String} Formatted URL
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @protected
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass// Add attributes to AutoCompleteBase.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * YQL environment file URL to load when the <code>source</code> is set to
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * a YQL query. Set this to <code>null</code> to use the default Open Data
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Tables environment file (http://datatables.org/alltables.env).
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @attribute yqlEnv
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @default null
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * URL protocol to use when the <code>source</code> is set to a YQL query.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @attribute yqlProtocol
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @type String
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @default 'http'
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass// Tell AutoCompleteBase about the new source types it can now support.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass object: '_beforeCreateObjectSource', // Run our version before the base version.
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['autocomplete-base'], optional:['io-base', 'json-parse', 'jsonp', 'yql']});