-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnominatim.js
120 lines (104 loc) · 4.5 KB
/
nominatim.js
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
/*
* Copyright (C) 2011 Klokan Technologies GmbH ([email protected])
*
* The JavaScript code in this page is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* USE OF THIS CODE OR ANY PART OF IT IN A NONFREE SOFTWARE IS NOT ALLOWED
* WITHOUT PRIOR WRITTEN PERMISSION FROM KLOKAN TECHNOLOGIES GMBH.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
*/
/**
* @fileoverview A class to create a Nominatim autocomplete that will match
* from an array of data provided via JSONP. The server returns a complex data
* structure that is used with client-side javascript functions to render the
* results.
*
* More info:
* http://wiki.openstreetmap.org/wiki/Nominatim
* http://open.mapquestapi.com/nominatim/
*
* @author [email protected] (Petr Pridal)
*/
goog.provide('goog.ui.AutoComplete.Nominatim');
goog.require('goog.ui.AutoComplete');
goog.require('goog.ui.AutoComplete.InputHandler');
goog.require('goog.ui.AutoComplete.NominatimMatcher');
goog.require('goog.ui.AutoComplete.Renderer');
/**
* Factory class to create a rich autocomplete widget that autocompletes an
* inputbox or textarea from data provided via ajax. The server returns a
* complex data structure that is used with client-side javascript functions to
* render the results.
* @param {!Element} input Input element or text area.
* @param {string=} opt_url The Uri of the Nominatim service.
* @param {Object=} opt_payload Extra parameters for the Jsonp request.
* @constructor
* @extends {goog.ui.AutoComplete}
*/
goog.ui.AutoComplete.Nominatim = function(input, opt_url, opt_payload) {
// Create a custom renderer that renders rich rows returned from server.
var customRenderer = {};
customRenderer.renderRow = function(row, token, node) {
node.innerHTML = row.data['display_name'] + ' (' + row.data['type'] + ')';
/* render:
goog.dom.appendChild(node, goog.dom.createTextNode(
row.data['display_name']));
goog.dom.appendChild(node, goog.dom.createDom("span", "ac-type",
goog.dom.createTextNode(row.data['type'])));
*/
};
/**
* A standard renderer that uses a custom row renderer to display the
* rich rows generated by this autocomplete widget.
* @type {!goog.ui.AutoComplete.Renderer}
*/
var renderer = new goog.ui.AutoComplete.Renderer(null, customRenderer);
/**
* A remote matcher that parses rich results returned via JSONP from a server.
* @type {!goog.ui.AutoComplete.NominatimMatcher}
* @private
*/
this.matcher_ = new goog.ui.AutoComplete.NominatimMatcher(
opt_url, opt_payload);
/**
* An input handler that calls select on a row when it is selected.
* @type {!goog.ui.AutoComplete.InputHandler}
*/
var inputhandler = new goog.ui.AutoComplete.InputHandler(null, null, false);
inputhandler.setThrottleTime(300);
inputhandler.setUpdateDuringTyping(false);
inputhandler.attachAutoComplete(this);
inputhandler.attachInputs(input);
// Create the widget and connect it to the input handler.
goog.base(this, this.matcher_, renderer, inputhandler);
this.addEventListener(goog.ui.AutoComplete.EventType.UPDATE, function(e) {
input.value = e.row['display_name'];
});
};
goog.inherits(goog.ui.AutoComplete.Nominatim, goog.ui.AutoComplete);
/**
* Calls matchHandler on a set of matching rows retrieved from server.
* @param {string} token The text that should be matched; passed to the server
* as the 'token' query param.
* @param {number} maxMatches The maximum number of matches requested from the
* server; passed as the 'max_matches' query param. The server is
* responsible for limiting the number of matches that are returned.
* @param {Function} matchHandler Callback to execute on the result after
* matching.
*/
goog.ui.AutoComplete.Nominatim.prototype.search =
function(token, maxMatches, matchHandler) {
this.matcher_.requestMatchingRows(token, maxMatches, matchHandler);
};