aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/i18n/uchar/remotenamefetcher.js
blob: 3524e2837f26ae11e09006237d382c900db80cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Object which fetches Unicode codepoint names from a remote data
 * source. This data source should accept two parameters:
 * <ol>
 * <li>c - the list of codepoints in hexadecimal format
 * <li>p - the name property
 * </ol>
 * and return a JSON object representation of the result.
 * For example, calling this data source with the following URL:
 * http://datasource?c=50,ff,102bd&p=name
 * Should return a JSON object which looks like this:
 * <pre>
 * {"50":{"name":"LATIN CAPITAL LETTER P"},
 * "ff":{"name":"LATIN SMALL LETTER Y WITH DIAERESIS"},
 * "102bd":{"name":"CARIAN LETTER K2"}}
 * </pre>.
 */

goog.provide('goog.i18n.uChar.RemoteNameFetcher');

goog.require('goog.Disposable');
goog.require('goog.Uri');
goog.require('goog.debug.Logger');
goog.require('goog.i18n.uChar');
goog.require('goog.i18n.uChar.NameFetcher');
goog.require('goog.net.XhrIo');
goog.require('goog.structs.Map');



/**
 * Builds the RemoteNameFetcher object. This object retrieves codepoint names
 * from a remote data source.
 *
 * @param {string} dataSourceUri URI to the data source.
 * @constructor
 * @implements {goog.i18n.uChar.NameFetcher}
 * @extends {goog.Disposable}
 */
goog.i18n.uChar.RemoteNameFetcher = function(dataSourceUri) {
  goog.base(this);

  /**
   * XHRIo object for prefetch() asynchronous calls.
   *
   * @type {!goog.net.XhrIo}
   * @private
   */
  this.prefetchXhrIo_ = new goog.net.XhrIo();

  /**
   * XHRIo object for getName() asynchronous calls.
   *
   * @type {!goog.net.XhrIo}
   * @private
   */
  this.getNameXhrIo_ = new goog.net.XhrIo();

  /**
   * URI to the data.
   *
   * @type {string}
   * @private
   */
  this.dataSourceUri_ = dataSourceUri;

  /**
   * A cache of all the collected names from the server.
   *
   * @type {!goog.structs.Map}
   * @private
   */
  this.charNames_ = new goog.structs.Map();
};
goog.inherits(goog.i18n.uChar.RemoteNameFetcher, goog.Disposable);


/**
 * Key to the listener on XHR for prefetch(). Used to clear previous listeners.
 *
 * @type {?number}
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchLastListenerKey_ = 0;


/**
 * Key to the listener on XHR for getName(). Used to clear previous listeners.
 *
 * @type {?number}
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.getNameLastListenerKey_ = 0;


/**
 * A reference to the RemoteNameFetcher logger.
 *
 * @type {!goog.debug.Logger}
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.logger_ =
    goog.debug.Logger.getLogger('goog.i18n.uChar.RemoteNameFetcher');




/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.disposeInternal = function() {
  goog.base(this, 'disposeInternal');
  this.prefetchXhrIo_.dispose();
  this.getNameXhrIo_.dispose();
};


/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetch = function(characters) {
  // Abort the current request if there is one
  if (this.prefetchXhrIo_.isActive()) {
    goog.i18n.uChar.RemoteNameFetcher.logger_.
        info('Aborted previous prefetch() call for new incoming request');
    this.prefetchXhrIo_.abort();
  }
  if (this.prefetchLastListenerKey_ != 0) {
    goog.events.unlistenByKey(this.prefetchLastListenerKey_);
  }

  // Set up new listener
  var preFetchCallback = goog.bind(this.prefetchCallback_, this);
  this.prefetchLastListenerKey_ = goog.events.listenOnce(this.prefetchXhrIo_,
      goog.net.EventType.COMPLETE, preFetchCallback);

  this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.BASE_88,
      characters, this.prefetchXhrIo_);
};


/**
 * Callback on completion of the prefetch operation.
 *
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchCallback_ = function() {
  this.processResponse_(this.prefetchXhrIo_);
};


/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.getName = function(character,
    callback) {
  var codepoint = goog.i18n.uChar.toCharCode(character).toString(16);

  if (this.charNames_.containsKey(codepoint)) {
    var name = /** @type {string} */ (this.charNames_.get(codepoint));
    callback(name);
    return;
  }

  // Abort the current request if there is one
  if (this.getNameXhrIo_.isActive()) {
    goog.i18n.uChar.RemoteNameFetcher.logger_.
        info('Aborted previous getName() call for new incoming request');
    this.getNameXhrIo_.abort();
  }
  if (this.getNameLastListenerKey_ != 0) {
    goog.events.unlistenByKey(this.getNameLastListenerKey_);
  }

  // Set up new listener
  var getNameCallback = goog.bind(this.getNameCallback_, this, codepoint,
      callback);
  this.getNameLastListenerKey_ = goog.events.listenOnce(this.getNameXhrIo_,
      goog.net.EventType.COMPLETE, getNameCallback);

  this.fetch_(goog.i18n.uChar.RemoteNameFetcher.RequestType_.CODEPOINT,
      codepoint, this.getNameXhrIo_);
};


/**
 * Callback on completion of the getName operation.
 *
 * @param {string} codepoint The codepoint in hexadecimal format.
 * @param {function(?string)} callback The callback function called when the
 *     name retrieval is complete, contains a single string parameter with the
 *     codepoint name, this parameter will be null if the character name is not
 *     defined.
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.getNameCallback_ = function(
    codepoint, callback) {
  this.processResponse_(this.getNameXhrIo_);
  var name = /** @type {?string} */ (this.charNames_.get(codepoint, null));
  callback(name);
};


/**
 * Process the response received from the server and store results in the cache.
 *
 * @param {!goog.net.XhrIo} xhrIo The XhrIo object used to make the request.
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.processResponse_ = function(xhrIo) {
  if (!xhrIo.isSuccess()) {
    goog.i18n.uChar.RemoteNameFetcher.logger_.severe(
        'Problem with data source: ' + xhrIo.getLastError());
    return;
  }
  var result = xhrIo.getResponseJson();
  for (var codepoint in result) {
    if (result[codepoint].hasOwnProperty('name')) {
      this.charNames_.set(codepoint, result[codepoint]['name']);
    }
  }
};


/**
 * Enum for the different request types.
 *
 * @enum {string}
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.RequestType_ = {

  /**
   * Request type that uses a base 88 string containing a set of codepoints to
   * be fetched from the server (see goog.i18n.charpickerdata for more
   * information on b88).
   */
  BASE_88: 'b88',

  /**
   * Request type that uses a a string of comma separated codepoint values.
   */
  CODEPOINT: 'c'
};


/**
 * Fetches a set of codepoint names from the data source.
 *
 * @param {!goog.i18n.uChar.RemoteNameFetcher.RequestType_} requestType The
 *     request type of the operation. This parameter specifies how the server is
 *     called to fetch a particular set of codepoints.
 * @param {string} requestInput The input to the request, this is the value that
 *     is passed onto the server to complete the request.
 * @param {!goog.net.XhrIo} xhrIo The XHRIo object to execute the server call.
 * @private
 */
goog.i18n.uChar.RemoteNameFetcher.prototype.fetch_ = function(requestType,
    requestInput, xhrIo) {
  var url = new goog.Uri(this.dataSourceUri_);
  url.setParameterValue(requestType, requestInput);
  url.setParameterValue('p', 'name');
  goog.i18n.uChar.RemoteNameFetcher.logger_.info('Request: ' +
      url.toString());
  xhrIo.send(url);
};


/** @override */
goog.i18n.uChar.RemoteNameFetcher.prototype.isNameAvailable = function(
    character) {
  return true;
};