Skip to content

Commit ccf066c

Browse files
committed
CMS support
1 parent 54ca3db commit ccf066c

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

_config/.gitkeep

Whitespace-only changes.

_config/addressfinder.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SilverStripe\Admin\LeftAndMain:
2+
extra_requirements_javascript:
3+
- 'addressfinder/javascript/addressfinder_ent.js'

javascript/addressfinder_ent.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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)

src/AddressFinderField.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use SilverStripe\Forms\TextField;
77
use SilverStripe\Forms\HiddenField;
88
use SilverStripe\Forms\NumericField;
9-
9+
use SilverStripe\Control\Controller;
1010
use SilverStripe\ORM\DataObjectInterface;
1111
use SilverStripe\View\Requirements;
1212
use SilverStripe\Core\Config\Config;
@@ -162,7 +162,12 @@ public function FieldHolder($properties = array())
162162
{
163163
Requirements::javascript('//api.addressfinder.io/assets/v3/widget.js');
164164
Requirements::javascript(ADMIN_THIRDPARTY_DIR . '/jquery/jquery.js');
165-
Requirements::javascript('addressfinder/javascript/addressfinder.js');
165+
166+
if(Controller::curr()->hasMethod('ShowSwitchView')) {
167+
// leftandmain check. If admin then use entwine.
168+
} else {
169+
Requirements::javascript('addressfinder/javascript/addressfinder.js');
170+
}
166171

167172
$properties = array(
168173
'ManualAddressFields' => $this->getManualFields(),

0 commit comments

Comments
 (0)