4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan GroveMixes support for JSONP and YQL result sources into AutoCompleteBase.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove@module autocomplete
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove@submodule autocomplete-sources
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass// Add prototype properties and methods to AutoCompleteBase.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Regular expression used to determine whether a String source is a YQL query.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @property _YQL_SOURCE_REGEX
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @type RegExp
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Runs before AutoCompleteBase's `_createObjectSource()` method and augments
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove it to support additional object-based source types.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _beforeCreateObjectSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} source
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @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.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Creates a DataSource-like object that uses `Y.io` as a source. See the
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove `source` attribute for more details.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _createIOSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} source URL.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Object} DataSource-like object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @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.
6506cffe711cef12e98d0096a15b9de5ca07931bRyan Grove that[_SOURCE_SUCCESS](that._cache[cacheKey], request);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass // Cancel any outstanding requests.
20ec7ff76cf1e295267fb55bb238bd8894b08188Ryan Grove ioRequest = Y.io(that._getXHRUrl(source, request), {
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.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Creates a DataSource-like object that uses the specified JSONPRequest
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove instance as a source. See the `source` attribute for more details.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _createJSONPSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {JSONPRequest|String} source URL string or JSONPRequest instance.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Object} DataSource-like object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
6506cffe711cef12e98d0096a15b9de5ca07931bRyan Grove that[_SOURCE_SUCCESS](that._cache[cacheKey], request);
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.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Creates a DataSource-like object that uses the specified `<select>` node as
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _createSelectSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {Node} source YUI Node instance wrapping a `<select>` node.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Object} DataSource-like object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Creates a DataSource-like object that calls the specified URL or executes
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove the specified YQL query for results. If the string starts with "select ",
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove "use ", or "set " (case-insensitive), it's assumed to be a YQL query;
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove otherwise, it's assumed to be a URL (which may be absolute or relative).
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove URLs containing a "{callback}" placeholder are assumed to be JSONP URLs; all
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove others will use XHR. See the `source` attribute for more details.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _createStringSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} source URL or YQL query.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Object} DataSource-like object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @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.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Creates a DataSource-like object that uses the specified YQL query string to
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove create a YQL-based source. See the `source` attribute for details. If no
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove `resultListLocator` is defined, this method will set a best-guess locator
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove that might work for many typical YQL queries.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _createYQLSource
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} source YQL query.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Object} DataSource-like object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
6506cffe711cef12e98d0096a15b9de5ca07931bRyan Grove that.set(RESULT_LIST_LOCATOR, that._defaultYQLLocator);
6506cffe711cef12e98d0096a15b9de5ca07931bRyan Grove that[_SOURCE_SUCCESS](that._cache[yqlQuery], request);
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.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Default resultListLocator used when a string-based YQL source is set and the
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove implementer hasn't already specified one.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _defaultYQLLocator
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {Object} response YQL response object.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {Array}
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @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;
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Returns a formatted XHR URL based on the specified base _url_, _query_, and
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove the current _requestTemplate_ if any.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _getXHRUrl
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} url Base URL.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {Object} request Request object containing `query` and `request`
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {String} Formatted URL.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
20ec7ff76cf1e295267fb55bb238bd8894b08188Ryan Grove // Append the request template to the URL.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove URL formatter passed to `JSONPRequest` instances.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @method _jsonpFormatter
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} url
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} proxy
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @param {String} query
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @return {String} Formatted URL
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass// Add attributes to AutoCompleteBase.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove YQL environment file URL to load when the `source` is set to a YQL query.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove Set this to `null` to use the default Open Data Tables environment file
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @attribute yqlEnv
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @type String
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @default null
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @for AutoCompleteBase
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove URL protocol to use when the `source` is set to a YQL query.
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @attribute yqlProtocol
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @type String
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @default 'http'
4bc090583f2a5d4fee80ca7a79a993d4e5ac14ecRyan Grove @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.
c0486d6f93858a5de5737e65f736d40e4b0feed5Ryan Grove}, '@VERSION@' ,{requires:['autocomplete-base'], optional:['io-base', 'json-parse', 'jsonp', 'yql']});