|
| 1 | +(function($) { |
| 2 | + $.entwine("ss", function($) { |
| 3 | + $(".address_finder p a").entwine({ |
| 4 | + onmatch: function() { |
| 5 | + |
| 6 | + }, |
| 7 | + onclick: function(e) { |
| 8 | + e.preventDefault(); |
| 9 | + $(this).parents('.address_finder[data-api-key]').find('.manual_address').slideToggle(function() { |
| 10 | + $(this).parents('.address_finder[data-api-key]').find('input[name*=ManualAddress]').val( |
| 11 | + $(this).parents('.address_finder[data-api-key]').find('.manual_address').is(":visible") |
| 12 | + ) |
| 13 | + }); |
| 14 | + } |
| 15 | + }) |
| 16 | + $(".address_finder").entwine({ |
| 17 | + onmatch: function(deferred) { |
| 18 | + var widget, |
| 19 | + key = $(this).data('api-key'), |
| 20 | + address = $(this).find('.address_finder_address'), |
| 21 | + input = $(this).find('input').first(), |
| 22 | + manual = $(this).find('.manual_address'), |
| 23 | + toggle = $(this).find('.toggle_manual_address'); |
| 24 | + |
| 25 | + var useManual = manual.find('input[name*=ManualAddress]'), |
| 26 | + field = address.find('input').get(0) |
| 27 | + |
| 28 | + /* update ui with javascript */ |
| 29 | + toggle.show() |
| 30 | + address.show() |
| 31 | + |
| 32 | + if (!useManual.val()) { |
| 33 | + manual.hide() |
| 34 | + } |
| 35 | + |
| 36 | + /* create widget */ |
| 37 | + widget = new AddressFinder.Widget(field, key, "NZ", { |
| 38 | + container: $(this).find('.addressfinder__holder').get(0) |
| 39 | + }); |
| 40 | + |
| 41 | + /* updates manual fields and hidden metadata */ |
| 42 | + widget.on('result:select', function(value, item) { |
| 43 | + /* populate postal line fields */ |
| 44 | + for (var i = 1; i <= 6; i++) { |
| 45 | + manual.find('input[name*=PostalLine' + i + ']').val(item['postal_line_' + i] || '') |
| 46 | + } |
| 47 | + |
| 48 | + manual.find('input[name*=Suburb]').val(item.suburb || '') |
| 49 | + manual.find('input[name*=City]').val(item.city || '') |
| 50 | + manual.find('input[name*=Postcode]').val(item.postcode || '') |
| 51 | + manual.find('input[name*=Longitude]').val(item.x || '') |
| 52 | + manual.find('input[name*=Latitude]').val(item.y || '') |
| 53 | + |
| 54 | + $('body').trigger(jQuery.Event('addressselected')) |
| 55 | + }) |
| 56 | + |
| 57 | + /* on manually changing of the fields then we have to clear x/y */ |
| 58 | + manual.on('keydown', 'input', function(e) { |
| 59 | + manual.find('input[name*=Longitude]').val('') |
| 60 | + manual.find('input[name*=Latitude]').val('') |
| 61 | + }) |
| 62 | + |
| 63 | + /* focusing back on the address dropdown should hide the manual */ |
| 64 | + input.on('focus', function(e) { |
| 65 | + manual.slideUp() |
| 66 | + }) |
| 67 | + } |
| 68 | + }) |
| 69 | +}) |
| 70 | +})(jQuery) |
0 commit comments