Debian Codegraph @

closure-library/closure/goog/i18n/uchar/remotenamefetcher.js

raw
1 // Copyright 2012 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Object which fetches Unicode codepoint names from a remote data
17 * source. This data source should accept two parameters:
18 * <ol>
19 * <li>c - the list of codepoints in hexadecimal format
20 * <li>p - the name property
21 * </ol>
22 * and return a JSON object representation of the result.
23 * For example, calling this data source with the following URL:
24 * http://datasource?c=50,ff,102bd&p=name
25 * Should return a JSON object which looks like this:
26 * <pre>
27 * {"50":{"name":"LATIN CAPITAL LETTER P"},
28 * "ff":{"name":"LATIN SMALL LETTER Y WITH DIAERESIS"},
29 * "102bd":{"name":"CARIAN LETTER K2"}}
30 * </pre>.
31 */
32
33 goog.provide('goog.i18n.uChar.RemoteNameFetcher');
34
35 goog.require('goog.Disposable');
36 goog.require('goog.Uri');
37 goog.require('goog.events');
38 goog.require('goog.i18n.uChar');
39 goog.require('goog.i18n.uChar.NameFetcher');
40 goog.require('goog.log');
41 goog.require('goog.net.EventType');
42 goog.require('goog.net.XhrIo');
43 goog.require('goog.structs.Map');
44
45
46
47 /**
48 * Builds the RemoteNameFetcher object. This object retrieves codepoint names
49 * from a remote data source.
50 *
51 * @param {string} dataSourceUri URI to the data source.
52 * @constructor
53 * @implements {goog.i18n.uChar.NameFetcher}
54 * @extends {goog.Disposable}
55 * @final
56 */
57 goog.i18n.uChar.RemoteNameFetcher = function(dataSourceUri) {
58 goog.i18n.uChar.RemoteNameFetcher.base(this, 'constructor');
59
60 /**
61 * XHRIo object for prefetch() asynchronous calls.
62 *
63 * @type {!goog.net.XhrIo}
64 * @private
65 */
66 this.prefetchXhrIo_ = new goog.net.XhrIo();
67
68 /**
69 * XHRIo object for getName() asynchronous calls.
70 *
71 * @type {!goog.net.XhrIo}
72 * @private
73 */
74 this.getNameXhrIo_ = new goog.net.XhrIo();
75
76 /**
77 * URI to the data.
78 *
79 * @type {string}
80 * @private
81 */
82 this.dataSourceUri_ = dataSourceUri;
83
84 /**
85 * A cache of all the collected names from the server.
86 *
87 * @type {!goog.structs.Map}
88 * @private
89 */
90 this.charNames_ = new goog.structs.Map();
91 };
92 goog.inherits(goog.i18n.uChar.RemoteNameFetcher, goog.Disposable);
93
94
95 /**
96 * Key to the listener on XHR for prefetch(). Used to clear previous listeners.
97 *
98 * @type {goog.events.Key}
99 * @private
100 */
101 goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchLastListenerKey_;
102
103
104 /**
105 * Key to the listener on XHR for getName(). Used to clear previous listeners.
106 *
107 * @type {goog.events.Key}
108 * @private
109 */
110 goog.i18n.uChar.RemoteNameFetcher.prototype.getNameLastListenerKey_;
111
112
113 /**
114 * A reference to the RemoteNameFetcher logger.
115 *
116 * @type {goog.log.Logger}
117 * @private
118 */
119 goog.i18n.uChar.RemoteNameFetcher.logger_ =
120 goog.log.getLogger('goog.i18n.uChar.RemoteNameFetcher');
121
122
123
124
125 /** @override */
126 goog.i18n.uChar.RemoteNameFetcher.prototype.disposeInternal = function() {
127 goog.i18n.uChar.RemoteNameFetcher.base(this, 'disposeInternal');
128 this.prefetchXhrIo_.dispose();
129 this.getNameXhrIo_.dispose();
130 };
131
132
133 /** @override */
134 goog.i18n.uChar.RemoteNameFetcher.prototype.prefetch = function(characters) {
135 // Abort the current request if there is one
136 if (this.prefetchXhrIo_.isActive()) {
137 goog.log.info(
138 goog.i18n.uChar.RemoteNameFetcher.logger_,
139 'Aborted previous prefetch() call for new incoming request');
140 this.prefetchXhrIo_.abort();
141 }
142 if (this.prefetchLastListenerKey_) {
143 goog.events.unlistenByKey(this.prefetchLastListenerKey_);
144 }
145
146 // Set up new listener
147 var preFetchCallback = goog.bind(this.prefetchCallback_, this);
148 this.prefetchLastListenerKey_ = goog.events.listenOnce(
149 this.prefetchXhrIo_, goog.net.EventType.COMPLETE, preFetchCallback);
150
151 this.fetch_(
152 goog.i18n.uChar.RemoteNameFetcher.RequestType_.BASE_88, characters,
153 this.prefetchXhrIo_);
154 };
155
156
157 /**
158 * Callback on completion of the prefetch operation.
159 *
160 * @private
161 */
162 goog.i18n.uChar.RemoteNameFetcher.prototype.prefetchCallback_ = function() {
163 this.processResponse_(this.prefetchXhrIo_);
164 };
165
166
167 /** @override */
168 goog.i18n.uChar.RemoteNameFetcher.prototype.getName = function(
169 character, callback) {
170 var codepoint = goog.i18n.uChar.toCharCode(character).toString(16);
171
172 if (this.charNames_.containsKey(codepoint)) {
173 var name = /** @type {string} */ (this.charNames_.get(codepoint));
174 callback(name);
175 return;
176 }
177
178 // Abort the current request if there is one
179 if (this.getNameXhrIo_.isActive()) {
180 goog.log.info(
181 goog.i18n.uChar.RemoteNameFetcher.logger_,
182 'Aborted previous getName() call for new incoming request');
183 this.getNameXhrIo_.abort();
184 }
185 if (this.getNameLastListenerKey_) {
186 goog.events.unlistenByKey(this.getNameLastListenerKey_);
187 }
188
189 // Set up new listener
190 var getNameCallback =
191 goog.bind(this.getNameCallback_, this, codepoint, callback);
192 this.getNameLastListenerKey_ = goog.events.listenOnce(
193 this.getNameXhrIo_, goog.net.EventType.COMPLETE, getNameCallback);
194
195 this.fetch_(
196 goog.i18n.uChar.RemoteNameFetcher.RequestType_.CODEPOINT, codepoint,
197 this.getNameXhrIo_);
198 };
199
200
201 /**
202 * Callback on completion of the getName operation.
203 *
204 * @param {string} codepoint The codepoint in hexadecimal format.
205 * @param {function(?string)} callback The callback function called when the
206 * name retrieval is complete, contains a single string parameter with the
207 * codepoint name, this parameter will be null if the character name is not
208 * defined.
209 * @private
210 */
211 goog.i18n.uChar.RemoteNameFetcher.prototype.getNameCallback_ = function(
212 codepoint, callback) {
213 this.processResponse_(this.getNameXhrIo_);
214 var name = /** @type {?string} */ (this.charNames_.get(codepoint, null));
215 callback(name);
216 };
217
218
219 /**
220 * Process the response received from the server and store results in the cache.
221 *
222 * @param {!goog.net.XhrIo} xhrIo The XhrIo object used to make the request.
223 * @private
224 */
225 goog.i18n.uChar.RemoteNameFetcher.prototype.processResponse_ = function(xhrIo) {
226 if (!xhrIo.isSuccess()) {
227 goog.log.error(
228 goog.i18n.uChar.RemoteNameFetcher.logger_,
229 'Problem with data source: ' + xhrIo.getLastError());
230 return;
231 }
232 var result = xhrIo.getResponseJson();
233 for (var codepoint in result) {
234 if (result[codepoint].hasOwnProperty('name')) {
235 this.charNames_.set(codepoint, result[codepoint]['name']);
236 }
237 }
238 };
239
240
241 /**
242 * Enum for the different request types.
243 *
244 * @enum {string}
245 * @private
246 */
247 goog.i18n.uChar.RemoteNameFetcher.RequestType_ = {
248
249 /**
250 * Request type that uses a base 88 string containing a set of codepoints to
251 * be fetched from the server (see goog.i18n.charpickerdata for more
252 * information on b88).
253 */
254 BASE_88: 'b88',
255
256 /**
257 * Request type that uses a a string of comma separated codepoint values.
258 */
259 CODEPOINT: 'c'
260 };
261
262
263 /**
264 * Fetches a set of codepoint names from the data source.
265 *
266 * @param {!goog.i18n.uChar.RemoteNameFetcher.RequestType_} requestType The
267 * request type of the operation. This parameter specifies how the server is
268 * called to fetch a particular set of codepoints.
269 * @param {string} requestInput The input to the request, this is the value that
270 * is passed onto the server to complete the request.
271 * @param {!goog.net.XhrIo} xhrIo The XHRIo object to execute the server call.
272 * @private
273 */
274 goog.i18n.uChar.RemoteNameFetcher.prototype.fetch_ = function(
275 requestType, requestInput, xhrIo) {
276 var url = new goog.Uri(this.dataSourceUri_);
277 url.setParameterValue(requestType, requestInput);
278 url.setParameterValue('p', 'name');
279 goog.log.info(
280 goog.i18n.uChar.RemoteNameFetcher.logger_, 'Request: ' + url.toString());
281 xhrIo.send(url);
282 };
283
284
285 /** @override */
286 goog.i18n.uChar.RemoteNameFetcher.prototype.isNameAvailable = function(
287 character) {
288 return true;
289 };