autocomplete-sources-debug.js revision 753ef90e92d428310b4a07006e8850639c6faaad
13fdd42f1fc3e519650037a920e6a54c24973866vboxsyncYUI.add('autocomplete-sources', function(Y) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync/**
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * Mixes support for JSONP and YQL result sources into AutoCompleteBase.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync *
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * @module autocomplete
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * @submodule autocomplete-sources
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync */
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsyncvar ACBase = Y.AutoCompleteBase,
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync Lang = Y.Lang,
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync _SOURCE_SUCCESS = '_sourceSuccess',
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync
565c6b62913edd14704b060e3172bba10b1fe12evboxsync MAX_RESULTS = 'maxResults',
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync REQUEST_TEMPLATE = 'requestTemplate',
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync RESULT_LIST_LOCATOR = 'resultListLocator';
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync// Add prototype properties and methods to AutoCompleteBase.
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsyncY.mix(ACBase.prototype, {
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync /**
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync * Regular expression used to determine whether a String source is a YQL
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync * query.
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync *
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync * @property _YQL_SOURCE_REGEX
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync * @type RegExp
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * @protected
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * @for AutoCompleteBase
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync */
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync _YQL_SOURCE_REGEX: /^(?:select|set|use)\s+/i,
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync /**
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * Runs before AutoCompleteBase's <code>_createObjectSource()</code> method
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * and augments it to support additional object-based source types.
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync *
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * @method _beforeCreateObjectSource
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * @param {String} source
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync * @protected
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync * @for AutoCompleteBase
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync */
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync _beforeCreateObjectSource: function (source) {
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync // If the object is a <select> node, use the options as the result
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync // source.
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync if (source instanceof Y.Node &&
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync source.get('nodeName').toLowerCase() === 'select') {
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync return this._createSelectSource(source);
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync }
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync // If the object is a JSONPRequest instance, try to use it as a JSONP
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync // source.
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync if (Y.JSONPRequest && source instanceof Y.JSONPRequest) {
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync return this._createJSONPSource(source);
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync }
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync
3dc9141e9dc45634498a9b543477bec0bcf5dab8vboxsync // Fall back to a basic object source.
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync return this._createObjectSource(source);
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync },
66cd15f94910b1afc64c42375d40e01d5d33ad2fvboxsync
fa8e26644bdce3fe8590a2fd82433dc8bdf55414vboxsync /**
80096fb54d147045204dcc7e3ff273158de03f81vboxsync * Creates a DataSource-like object that uses <code>Y.io</code> as a source.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * See the <code>source</code> attribute for more details.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync *
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync * @method _createIOSource
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync * @param {String} source URL.
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync * @return {Object} DataSource-like object.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @protected
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @for AutoCompleteBase
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync */
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync _createIOSource: function (source) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync var cache = {},
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync ioSource = {type: 'io'},
7ca4727ba2f9e3703771a308b11cd551f3fca4fdvboxsync that = this,
7ca4727ba2f9e3703771a308b11cd551f3fca4fdvboxsync ioRequest, lastRequest, loading;
2043260aa6abeee8eb8cf0309cebb97ede45851evboxsync
2043260aa6abeee8eb8cf0309cebb97ede45851evboxsync // Private internal _sendRequest method that will be assigned to
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync // ioSource.sendRequest once io-base and json-parse are available.
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync function _sendRequest(request) {
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync var cacheKey = request.request,
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync query = request.query;
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync
2043260aa6abeee8eb8cf0309cebb97ede45851evboxsync // Return immediately on a cached response.
e7aa635d70dd0cdf083287ec896fe8cca2775466vboxsync if (cache[cacheKey]) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync that[_SOURCE_SUCCESS](cache[cacheKey], request);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync return;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // Cancel any outstanding requests.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync if (ioRequest && ioRequest.isInProgress()) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync ioRequest.abort();
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync }
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
6ba8963f8a097b33938cd6fbcd6b63faf6d26889vboxsync ioRequest = Y.io(that._getXHRUrl(source, query), {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync on: {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync success: function (tid, response) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync var data;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync try {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync data = Y.JSON.parse(response.responseText);
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync } catch (ex) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync Y.error('JSON parse error', ex);
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync }
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync if (data) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync cache[cacheKey] = data;
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync that[_SOURCE_SUCCESS](data, request);
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync }
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync }
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync });
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync }
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync ioSource.sendRequest = function (request) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // Keep track of the most recent request in case there are multiple
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // requests while we're waiting for the IO module to load. Only the
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // most recent request will be sent.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync lastRequest = request;
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync if (loading) { return; }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync loading = true;
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // Lazy-load the io-base and json-parse modules if necessary,
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // then overwrite the sendRequest method to bypass this check in
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync // the future.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync Y.use('io-base', 'json-parse', function () {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync ioSource.sendRequest = _sendRequest;
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync _sendRequest(lastRequest);
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync });
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync };
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync return ioSource;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync },
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync /**
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync * Creates a DataSource-like object that uses the specified JSONPRequest
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync * instance as a source. See the <code>source</code> attribute for more
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync * details.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync *
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @method _createJSONPSource
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync * @param {JSONPRequest|String} source URL string or JSONPRequest instance.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync * @return {Object} DataSource-like object.
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync * @protected
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync * @for AutoCompleteBase
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync */
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync _createJSONPSource: function (source) {
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync var cache = {},
e4d40e67c9cbfecb0b2ed48eec081648ba3a14bavboxsync jsonpSource = {type: 'jsonp'},
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync that = this,
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync lastRequest, loading;
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync
df409c2a6d13641b0586a36103d015cc8d76da53vboxsync function _sendRequest(request) {
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync var cacheKey = request.request,
9868c07279330f31ed91aa986bec707b7e8635a3vboxsync query = request.query;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync if (cache[cacheKey]) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync that[_SOURCE_SUCCESS](cache[cacheKey], request);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync return;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // Hack alert: JSONPRequest currently doesn't support
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // per-request callbacks, so we're reaching into the protected
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // _config object to make it happen.
b1ae8b1c40ec6bb82e7984b72712df5661332e2cvboxsync //
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // This limitation is mentioned in the following JSONP
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // enhancement ticket:
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync //
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // http://yuilibrary.com/projects/yui3/ticket/2529371
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync source._config.on.success = function (data) {
6e9e03fceb2e76b576905c4d3faa55ca31555956vboxsync cache[cacheKey] = data;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync that[_SOURCE_SUCCESS](data, request);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync };
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync source.send(query);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync jsonpSource.sendRequest = function (request) {
a4ab203fe0f52099fa53f52758e83d06f470ac04vboxsync // Keep track of the most recent request in case there are multiple
74959ba85f2898a152aed46b41cfe908638f2420vboxsync // requests while we're waiting for the JSONP module to load. Only
74959ba85f2898a152aed46b41cfe908638f2420vboxsync // the most recent request will be sent.
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync lastRequest = request;
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync if (loading) { return; }
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync
d2c66e0925a76b3636d86b2afb2487fd6f956aebvboxsync loading = true;
e62e26b69d90d1f29ad146c68606d95cf47340a4vboxsync
2043260aa6abeee8eb8cf0309cebb97ede45851evboxsync // Lazy-load the JSONP module if necessary, then overwrite the
2043260aa6abeee8eb8cf0309cebb97ede45851evboxsync // sendRequest method to bypass this check in the future.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync Y.use('jsonp', function () {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // Turn the source into a JSONPRequest instance if it isn't
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // one already.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync if (!(source instanceof Y.JSONPRequest)) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync source = new Y.JSONPRequest(source, {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync format: Y.bind(that._jsonpFormatter, that)
f1630a6818621238c9c2338394b44995e318e108vboxsync });
f1630a6818621238c9c2338394b44995e318e108vboxsync }
f1630a6818621238c9c2338394b44995e318e108vboxsync
f1630a6818621238c9c2338394b44995e318e108vboxsync jsonpSource.sendRequest = _sendRequest;
0fd850701c4b07dd4cc9074734b8d98f6184055cvboxsync _sendRequest(lastRequest);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync });
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync };
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync return jsonpSource;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync },
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync /**
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * Creates a DataSource-like object that uses the specified &lt;select&gt;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * node as a source.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync *
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @method _createSelectSource
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @param {Node} source YUI Node instance wrapping a &lt;select&gt; node.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @return {Object} DataSource-like object.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @protected
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @for AutoCompleteBase
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync */
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync _createSelectSource: function (source) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync var that = this;
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
6a4586a1a79ef87a9b97119ae350009a258fe924vboxsync return {
6a4586a1a79ef87a9b97119ae350009a258fe924vboxsync type: 'select',
b1fd0840de05fc2e189d4ea55ba33c2d6b09a4d8vboxsync sendRequest: function (request) {
b1fd0840de05fc2e189d4ea55ba33c2d6b09a4d8vboxsync var options = [];
b1fd0840de05fc2e189d4ea55ba33c2d6b09a4d8vboxsync
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync source.get('options').each(function (option) {
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync options.push({
6a4586a1a79ef87a9b97119ae350009a258fe924vboxsync html : option.get('innerHTML'),
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync index : option.get('index'),
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync node : option,
b1fd0840de05fc2e189d4ea55ba33c2d6b09a4d8vboxsync selected: option.get('selected'),
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync text : option.get('text'),
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync value : option.get('value')
6a4586a1a79ef87a9b97119ae350009a258fe924vboxsync });
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync });
7a006aff4af0ac1eaed18e272ecc9aeeaf41e4efvboxsync
b1fd0840de05fc2e189d4ea55ba33c2d6b09a4d8vboxsync that[_SOURCE_SUCCESS](options, request);
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync }
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync };
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync },
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync /**
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * Creates a DataSource-like object that calls the specified URL or
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * executes the specified YQL query for results. If the string starts
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * with "select ", "use ", or "set " (case-insensitive), it's assumed to be
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * a YQL query; otherwise, it's assumed to be a URL (which may be absolute
b1ae8b1c40ec6bb82e7984b72712df5661332e2cvboxsync * or relative). URLs containing a "{callback}" placeholder are assumed to
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * be JSONP URLs; all others will use XHR. See the <code>source</code>
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * attribute for more details.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync *
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @method _createStringSource
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @param {String} source URL or YQL query.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @return {Object} DataSource-like object.
6831f283dbc5c27bde8a8f8bea179b84a5741697vboxsync * @protected
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync * @for AutoCompleteBase
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync */
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync _createStringSource: function (source) {
01ea175f55740168d1dd0af3277d3b86a30b4f91vboxsync if (this._YQL_SOURCE_REGEX.test(source)) {
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync // Looks like a YQL query.
13fdd42f1fc3e519650037a920e6a54c24973866vboxsync return this._createYQLSource(source);
80096fb54d147045204dcc7e3ff273158de03f81vboxsync } else if (source.indexOf('{callback}') !== -1) {
// Contains a {callback} param and isn't a YQL query, so it must be
// JSONP.
return this._createJSONPSource(source);
} else {
// Not a YQL query or JSONP, so we'll assume it's an XHR URL.
return this._createIOSource(source);
}
},
/**
* Creates a DataSource-like object that uses the specified YQL query string
* to create a YQL-based source. See the <code>source</code> attribute for
* details. If no <code>resultListLocator</code> is defined, this method
* will set a best-guess locator that might work for many typical YQL
* queries.
*
* @method _createYQLSource
* @param {String} source YQL query.
* @return {Object} DataSource-like object.
* @protected
* @for AutoCompleteBase
*/
_createYQLSource: function (source) {
var cache = {},
yqlSource = {type: 'yql'},
that = this,
lastRequest, loading, yqlRequest;
if (!this.get(RESULT_LIST_LOCATOR)) {
this.set(RESULT_LIST_LOCATOR, this._defaultYQLLocator);
}
function _sendRequest(request) {
var cacheKey = request.request,
query = request.query,
callback, env, maxResults, opts, yqlQuery;
if (cache[cacheKey]) {
that[_SOURCE_SUCCESS](cache[cacheKey], request);
return;
}
callback = function (data) {
cache[cacheKey] = data;
that[_SOURCE_SUCCESS](data, request);
};
env = that.get('yqlEnv');
maxResults = that.get(MAX_RESULTS);
opts = {proto: that.get('yqlProtocol')};
yqlQuery = Lang.sub(source, {
maxResults: maxResults > 0 ? maxResults : 1000,
query : query
});
// Only create a new YQLRequest instance if this is the
// first request. For subsequent requests, we'll reuse the
// original instance.
if (yqlRequest) {
yqlRequest._callback = callback;
yqlRequest._opts = opts;
yqlRequest._params.q = yqlQuery;
if (env) {
yqlRequest._params.env = env;
}
} else {
yqlRequest = new Y.YQLRequest(yqlQuery, {
on: {success: callback},
allowCache: false // temp workaround until JSONP has per-URL callback proxies
}, env ? {env: env} : null, opts);
}
yqlRequest.send();
}
yqlSource.sendRequest = function (request) {
// Keep track of the most recent request in case there are multiple
// requests while we're waiting for the YQL module to load. Only the
// most recent request will be sent.
lastRequest = request;
if (!loading) {
// Lazy-load the YQL module if necessary, then overwrite the
// sendRequest method to bypass this check in the future.
loading = true;
Y.use('yql', function () {
yqlSource.sendRequest = _sendRequest;
_sendRequest(lastRequest);
});
}
};
return yqlSource;
},
/**
* Default resultListLocator used when a string-based YQL source is set and
* the implementer hasn't already specified one.
*
* @method _defaultYQLLocator
* @param {Object} response YQL response object.
* @return {Array}
* @protected
* @for AutoCompleteBase
*/
_defaultYQLLocator: function (response) {
var results = response && response.query && response.query.results,
values;
if (results && Lang.isObject(results)) {
// If there's only a single value on YQL's results object, that
// value almost certainly contains the array of results we want. If
// there are 0 or 2+ values, then the values themselves are most
// likely the results we want.
values = Y.Object.values(results) || [];
results = values.length === 1 ? values[0] : values;
if (!Lang.isArray(results)) {
results = [results];
}
} else {
results = [];
}
return results;
},
/**
* Returns a formatted XHR URL based on the specified base <i>url</i>,
* <i>query</i>, and the current <i>requestTemplate</i> if any.
*
* @method _getXHRUrl
* @param {String} url Base URL.
* @param {String} query AutoComplete query.
* @return {String} Formatted URL.
* @protected
* @for AutoCompleteBase
*/
_getXHRUrl: function (url, query) {
var maxResults = this.get(MAX_RESULTS),
requestTemplate = this.get(REQUEST_TEMPLATE);
if (requestTemplate) {
url += requestTemplate(query);
}
return Lang.sub(url, {
maxResults: maxResults > 0 ? maxResults : 1000,
query : encodeURIComponent(query)
});
},
/**
* URL formatter passed to <code>JSONPRequest</code> instances.
*
* @method _jsonpFormatter
* @param {String} url
* @param {String} proxy
* @param {String} query
* @return {String} Formatted URL
* @protected
* @for AutoCompleteBase
*/
_jsonpFormatter: function (url, proxy, query) {
var maxResults = this.get(MAX_RESULTS),
requestTemplate = this.get(REQUEST_TEMPLATE);
if (requestTemplate) {
url += requestTemplate(query);
}
return Lang.sub(url, {
callback : proxy,
maxResults: maxResults > 0 ? maxResults : 1000,
query : encodeURIComponent(query)
});
}
});
// Add attributes to AutoCompleteBase.
Y.mix(ACBase.ATTRS, {
/**
* YQL environment file URL to load when the <code>source</code> is set to
* a YQL query. Set this to <code>null</code> to use the default Open Data
* Tables environment file (http://datatables.org/alltables.env).
*
* @attribute yqlEnv
* @type String
* @default null
* @for AutoCompleteBase
*/
yqlEnv: {
value: null
},
/**
* URL protocol to use when the <code>source</code> is set to a YQL query.
*
* @attribute yqlProtocol
* @type String
* @default 'http'
* @for AutoCompleteBase
*/
yqlProtocol: {
value: 'http'
}
});
// Tell AutoCompleteBase about the new source types it can now support.
Y.mix(ACBase.SOURCE_TYPES, {
io : '_createIOSource',
jsonp : '_createJSONPSource',
object: '_beforeCreateObjectSource', // Run our version before the base version.
select: '_createSelectSource',
string: '_createStringSource',
yql : '_createYQLSource'
}, true);
}, '@VERSION@' ,{optional:['io-base', 'json-parse', 'jsonp', 'yql'], requires:['autocomplete-base']});