|
1 | | -/*! Ideal Postcodes jQuery Plugin - v3.0.6 - 2017-11-17 |
| 1 | +/*! Ideal Postcodes jQuery Plugin - v3.0.7 - 2018-03-14 |
2 | 2 | * https://github.com/ideal-postcodes/jquery.postcodes |
3 | | -2017 Ideal Postcodes; Licensed MIT */ |
| 3 | +2018 Ideal Postcodes; Licensed MIT */ |
4 | 4 | (function($) { |
5 | 5 | "use strict"; |
6 | 6 | // Cache for all new instances of the plugin |
|
23 | 23 | line_2: "#line_2", |
24 | 24 | line_3: "#line_3", |
25 | 25 | post_town: "#post_town", |
26 | | - postcode: "#postcode", |
27 | | - postcode_inward: undefined, |
28 | | - postcode_outward: undefined, |
29 | | - udprn: undefined, |
30 | | - dependant_locality: undefined, |
31 | | - double_dependant_locality: undefined, |
32 | | - thoroughfare: undefined, |
33 | | - dependant_thoroughfare: undefined, |
34 | | - building_number: undefined, |
35 | | - building_name: undefined, |
36 | | - sub_building_name: undefined, |
37 | | - po_box: undefined, |
38 | | - department_name: undefined, |
39 | | - organisation_name: undefined, |
40 | | - postcode_type: undefined, |
41 | | - su_organisation_indicator: undefined, |
42 | | - delivery_point_suffix: undefined |
| 26 | + postcode: "#postcode" |
| 27 | + // postcode_inward: undefined, |
| 28 | + // postcode_outward: undefined, |
| 29 | + // udprn: undefined, |
| 30 | + // dependant_locality: undefined, |
| 31 | + // double_dependant_locality: undefined, |
| 32 | + // thoroughfare: undefined, |
| 33 | + // dependant_thoroughfare: undefined, |
| 34 | + // building_number: undefined, |
| 35 | + // building_name: undefined, |
| 36 | + // sub_building_name: undefined, |
| 37 | + // po_box: undefined, |
| 38 | + // department_name: undefined, |
| 39 | + // organisation_name: undefined, |
| 40 | + // postcode_type: undefined, |
| 41 | + // su_organisation_indicator: undefined, |
| 42 | + // delivery_point_suffix: undefined |
43 | 43 | }, |
44 | 44 |
|
45 | 45 | /* |
|
49 | 49 | endpoint: "https://api.ideal-postcodes.co.uk/v1", |
50 | 50 |
|
51 | 51 | // Input Field Configuration |
52 | | - input: undefined, |
53 | | - $input: undefined, |
| 52 | + // input: undefined, |
| 53 | + // $input: undefined, |
54 | 54 | input_label: "Please enter your postcode", |
55 | 55 | placeholder_label: "", |
56 | 56 | input_muted_style: "color:#CBCBCB;", |
57 | 57 | input_class: "", |
58 | 58 | input_id: "idpc_input", |
59 | 59 |
|
60 | 60 | // Button configuration |
61 | | - button: undefined, |
62 | | - $button: undefined, |
| 61 | + // button: undefined, |
| 62 | + // $button: undefined, |
63 | 63 | button_id: "idpc_button", |
64 | 64 | button_label: "Find my Address", |
65 | 65 | button_class: "", |
66 | 66 | button_disabled_message: "Looking up postcode...", |
67 | 67 |
|
68 | 68 | // Dropdown configuration |
69 | | - $dropdown: undefined, |
| 69 | + // $dropdown: undefined, |
70 | 70 | dropdown_id: "idpc_dropdown", |
71 | 71 | dropdown_select_message: "Please select your address", |
72 | 72 | dropdown_class: "", |
73 | | - dropdown_container: undefined, |
| 73 | + // dropdown_container: undefined, |
74 | 74 |
|
75 | 75 | // Error Message Configuration |
76 | | - $error_message: undefined, |
| 76 | + // $error_message: undefined, |
77 | 77 | error_message_id: "idpc_error_message", |
78 | 78 | error_message_invalid_postcode: "Please check your postcode, it seems to be incorrect", |
79 | 79 | error_message_not_found: "Your postcode could not be found. Please type in your address", |
80 | 80 | error_message_address_not_found: "We could not find a match for your address. Please type in your address", |
81 | 81 | error_message_default: "Sorry, we weren't able to get the address you were looking for. Please type your address manually", |
82 | 82 | error_message_class: "", |
83 | | - error_message_container: undefined, |
| 83 | + // error_message_container: undefined, |
84 | 84 |
|
85 | 85 | // Address search fallback - if enabled, postcode searches which fail validation will be forward to the Address search API |
86 | 86 | address_search: false, |
|
125 | 125 | }, |
126 | 126 |
|
127 | 127 | // Register callbacks at specific stages |
128 | | - onLoaded: undefined, // When plugin is initialised |
129 | | - onFailedCheck: undefined, // When key check fails (requires check_key: true) |
130 | | - onSearchCompleted: undefined, // When a lookup succeeds, E.g. Server responds that Postcode is found or doesn't exist |
131 | | - onAddressesRetrieved: undefined, // When a lookup succeeds with a list of addresses |
132 | | - onAddressSelected: undefined, // User has clicked an address in dropdown |
133 | | - onDropdownCreated: undefined, // When the address selection dropdown is inserted to DOM |
134 | | - onDropdownDestroyed: undefined, // When the address selection dropdown is removed (following new search) |
135 | | - onLookupTriggered: undefined, // When user clicks the button to trigger a lookup |
136 | | - shouldLookupTrigger: undefined, // |
137 | | - onSearchError: undefined, // When a request succeeds but the API returns an error code |
| 128 | + // onLoaded: undefined, // When plugin is initialised |
| 129 | + // onFailedCheck: undefined, // When key check fails (requires check_key: true) |
| 130 | + // onSearchCompleted: undefined, // When a lookup succeeds, E.g. Server responds that Postcode is found or doesn't exist |
| 131 | + // onAddressesRetrieved: undefined, // When a lookup succeeds with a list of addresses |
| 132 | + // onAddressSelected: undefined, // User has clicked an address in dropdown |
| 133 | + // onDropdownCreated: undefined, // When the address selection dropdown is inserted to DOM |
| 134 | + // onDropdownDestroyed: undefined, // When the address selection dropdown is removed (following new search) |
| 135 | + // onLookupTriggered: undefined, // When user clicks the button to trigger a lookup |
| 136 | + // shouldLookupTrigger: undefined, // |
| 137 | + // onSearchError: undefined, // When a request succeeds but the API returns an error code |
138 | 138 |
|
139 | 139 | // Tags to be included with search requests |
140 | | - tags: undefined |
| 140 | + // tags: undefined |
141 | 141 | }; |
142 | 142 |
|
143 | 143 | /* |
|
188 | 188 | * |
189 | 189 | * If a selector (this.input) is specified, that input is used |
190 | 190 | * If no selector specified, a new input field is generated and added to context |
191 | | - * |
192 | 191 | */ |
193 | | - |
194 | 192 | AddressFinderController.prototype.setupInputField = function () { |
195 | 193 | var self = this; |
196 | 194 | if ($(this.input).length) { |
|
231 | 229 |
|
232 | 230 | /* |
233 | 231 | * Connects clickable element to the plugin to trigger |
234 | | - * |
235 | 232 | */ |
236 | | - |
237 | 233 | AddressFinderController.prototype.setupLookupButton = function () { |
238 | 234 | var self = this; |
239 | 235 | if ($(self.button).length) { |
|
280 | 276 | /* |
281 | 277 | * Prevents lookup button from being triggered |
282 | 278 | */ |
283 | | - |
284 | 279 | AddressFinderController.prototype.disableLookup = function (message) { |
285 | 280 | // Cancel if custom button |
286 | 281 | if (this.button) { |
|
293 | 288 | /* |
294 | 289 | * Allows lookup button to be triggered |
295 | 290 | */ |
296 | | - |
297 | 291 | AddressFinderController.prototype.enableLookup = function () { |
298 | 292 | // Cancel if custom button |
299 | 293 | if (this.button) { |
|
311 | 305 |
|
312 | 306 | /* |
313 | 307 | * Clears the following fields |
314 | | - * |
315 | 308 | */ |
316 | | - |
317 | 309 | AddressFinderController.prototype.clearAll = function () { |
318 | 310 | this.setDropDown(); |
319 | 311 | this.setErrorMessage(); |
320 | 312 | }; |
321 | 313 |
|
322 | 314 | /* |
323 | 315 | * Removes all elements from DOM |
324 | | - * |
325 | 316 | */ |
326 | | - |
327 | 317 | AddressFinderController.prototype.removeAll = function () { |
328 | 318 | this.$context = null; |
329 | 319 |
|
|
340 | 330 | * - On successful search but no addresses, show error message |
341 | 331 | * - On failed search, show error message |
342 | 332 | */ |
343 | | - |
344 | 333 | AddressFinderController.prototype.executeSearch = function (term) { |
345 | 334 | var self = this; |
346 | 335 | var message; |
|
386 | 375 | /* |
387 | 376 | * Invoke postcode lookup |
388 | 377 | */ |
389 | | - |
390 | 378 | AddressFinderController.prototype.executePostcodeSearch = function (postcode, callback) { |
391 | 379 | var self = this; |
392 | 380 | var options = { |
393 | 381 | query: postcode, |
394 | | - api_key: self.api_key |
| 382 | + api_key: self.api_key, |
| 383 | + endpoint: self.endpoint |
395 | 384 | }; |
396 | 385 |
|
397 | 386 | if (self.tags) { |
|
408 | 397 | /* |
409 | 398 | * Invoke an address search |
410 | 399 | */ |
411 | | - |
412 | 400 | AddressFinderController.prototype.executeAddressSearch = function (query, callback) { |
413 | 401 | var self = this; |
414 | 402 | var options = { |
415 | 403 | query: query, |
416 | | - api_key: self.api_key |
| 404 | + api_key: self.api_key, |
| 405 | + endpoint: self.endpoint |
417 | 406 | }; |
418 | 407 |
|
419 | 408 | if (typeof self.address_search === "object") { |
|
434 | 423 | /* |
435 | 424 | * Caches search result with raw data object |
436 | 425 | */ |
437 | | - |
438 | 426 | AddressFinderController.prototype.cacheSearchResults = function (data) { |
439 | 427 | if (data === null) { |
440 | 428 | return null; |
|
450 | 438 | * |
451 | 439 | * Removes dropdown from DOM if data is undefined |
452 | 440 | */ |
453 | | - |
454 | 441 | AddressFinderController.prototype.setDropDown = function (data) { |
455 | 442 | var self = this; |
456 | 443 |
|
|
528 | 515 | * |
529 | 516 | * Removes error message from DOM if undefined |
530 | 517 | */ |
531 | | - |
532 | 518 | AddressFinderController.prototype.setErrorMessage = function (message) { |
533 | 519 | if (this.$error_message && this.$error_message.length) { |
534 | 520 | this.$error_message.remove(); |
|
561 | 547 | * |
562 | 548 | * Empties output fields if undefined |
563 | 549 | */ |
564 | | - |
565 | 550 | AddressFinderController.prototype.setAddressFields = function (data) { |
566 | 551 | data = data || {}; |
567 | 552 |
|
|
590 | 575 | * - options.query: (string) Postcode to lookup, case and space insensitive |
591 | 576 | * - options.api_key: (string) API Key required |
592 | 577 | * - options.licensee: (string) Licensee key |
| 578 | + * - options.endpoint: (string) API endpoint |
593 | 579 | * - success: (function) Callback invoked upon successful request |
594 | 580 | * - error: (function) Optional callback invoked upon failed HTTP request |
595 | 581 | */ |
596 | | - |
597 | 582 | lookupPostcode: function (o, callback) { |
598 | 583 | var postcode = o.query || o.postcode || ""; |
599 | 584 | var api_key = o.api_key || ""; |
600 | | - var endpoint = defaults.endpoint; |
| 585 | + var endpoint = o.endpoint || defaults.endpoint; |
601 | 586 | var resource = "postcodes"; |
602 | 587 | var url = [endpoint, resource, encodeURI(postcode)].join('/'); |
603 | 588 | var queryString = { |
|
647 | 632 | * - options.api_key: (string) API Key required |
648 | 633 | * - options.licensee: (string) Licensee key |
649 | 634 | * - options.limit: (number) Maximum number of addresses to return (default 10) |
| 635 | + * - options.endpoint: (string) API endpoint |
650 | 636 | * - success: (function) Callback invoked upon successful request |
651 | 637 | * - error: (function) Optional callback invoked upon failed HTTP request |
652 | 638 | */ |
653 | | - |
654 | 639 | lookupAddress: function (o, callback) { |
655 | 640 | var query = o.query || ""; |
656 | 641 | var api_key = o.api_key || ""; |
657 | | - var endpoint = defaults.endpoint; |
| 642 | + var endpoint = o.endpoint || defaults.endpoint; |
658 | 643 | var resource = "addresses"; |
659 | 644 | var url = [endpoint, resource].join('/'); |
660 | 645 | var queryString = { |
|
703 | 688 | * - options: (object) Configuration object for key checking |
704 | 689 | * - options.api_key: (string) API Key to test |
705 | 690 | * - options.licensee: (string) Licensee key |
| 691 | + * - options.endpoint: (string) API endpoint |
706 | 692 | * - success: (function) Callback invoked when key is available |
707 | 693 | * - error: (function) Optional callback invoked when key is not available or HTTP request failed |
708 | 694 | */ |
709 | | - |
710 | 695 | checkKey: function (o, success, error) { |
711 | 696 | var api_key = o.api_key || ""; |
712 | 697 |
|
|
734 | 719 | }; |
735 | 720 | } |
736 | 721 |
|
737 | | - var endpoint = defaults.endpoint; |
| 722 | + var endpoint = o.endpoint || defaults.endpoint; |
738 | 723 | var resource = "keys"; |
739 | 724 | var url = [endpoint, resource, api_key].join('/'); |
740 | 725 | var options = { |
|
817 | 802 | // Check if key is usable if necessary |
818 | 803 | if (options.check_key) { |
819 | 804 | var keyOptions = { api_key: options.api_key }; |
| 805 | + if (options.endpoint) { |
| 806 | + keyOptions.endpoint = options.endpoint; |
| 807 | + } |
820 | 808 | if (options.licensee) { |
821 | 809 | keyOptions.licensee = options.licensee; |
822 | 810 | } |
|
0 commit comments