Skip to content

Commit e66629d

Browse files
committed
4.0 CMS form changes
1 parent f55eddb commit e66629d

File tree

6 files changed

+124
-118
lines changed

6 files changed

+124
-118
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# AddressFinder FormField for SilverStripe
22

3-
## Maintainer Contact
4-
* Will Rossiter
3+
## Maintainer Contact
4+
* Will Rossiter
55
<will (at) fullscreen (dot) io>
6-
6+
77
## Requirements
8-
* SilverStripe 3.1 or higher
8+
* SilverStripe 4.0 or higher
99

1010
## Overview
1111

12-
This module provides a custom `AddressFinderField` which implements the
12+
This module provides a custom `AddressFinderField` which implements the
1313
javascript AddressFinder widget ([1](http://addressfinder.co.nz/docs/widget_docs))
1414

15-
To get started, sign up for an account at
16-
[addressfinder.co.nz/plans](http://addressfinder.co.nz/plans) and set your
15+
To get started, sign up for an account at
16+
[addressfinder.co.nz/plans](http://addressfinder.co.nz/plans) and set your
1717
AddressFinder key values via the Config system
1818

1919
*mysite/_config/addressfinder.yml*
2020
```
21-
AddressFinder:
21+
FullscreenInteractive\SilverStripe\AddressFinderField:
2222
api_key: 123
2323
```
2424

@@ -32,7 +32,7 @@ This will provide your form with a single text box that provides an autocomplete
3232
dropdown as well as a toggle for the user to enter a manual address in the event
3333
the API is unaccessible.
3434

35-
The form field provides the saveInto logic for automatically saving into a
35+
The form field provides the saveInto logic for automatically saving into a
3636
DataObject model if defined. The fields that the module will save too (if in the
3737
database) are
3838

code/AddressFinder.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

javascript/addressfinder.js

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,70 @@
1-
;(function ($) {
2-
$(document).ready(function () {
3-
$('.address_finder').each(function (i, elem) {
4-
var widget,
5-
key = $(elem).data('api-key'),
6-
address = $(elem).find('.address_finder_address'),
7-
input = $(elem).find('input').first(),
8-
manual = $(elem).find('.manual_address'),
9-
toggle = $(elem).find('.toggle_manual_address')
1+
;
2+
(function($) {
3+
$(document).ready(function() {
4+
$('.address_finder').each(function(i, elem) {
5+
var widget,
6+
key = $(elem).data('api-key'),
7+
address = $(elem).find('.address_finder_address'),
8+
input = $(elem).find('input').first(),
9+
manual = $(elem).find('.manual_address'),
10+
toggle = $(elem).find('.toggle_manual_address')
1011

11-
var useManual = manual.find('input[name*=ManualAddress]'),
12-
field = address.find('input').get(0)
12+
var useManual = manual.find('input[name*=ManualAddress]'),
13+
field = address.find('input').get(0)
1314

14-
/* update ui with javascript */
15-
toggle.show()
16-
address.show()
15+
/* update ui with javascript */
16+
toggle.show()
17+
address.show()
1718

18-
if (!useManual.val()) {
19-
manual.hide()
20-
}
19+
if (!useManual.val()) {
20+
manual.hide()
21+
}
2122

22-
/* create widget */
23-
widget = new AddressFinder.Widget(field, key, {
24-
show_locations: false
25-
})
23+
/* create widget */
24+
widget = new AddressFinder.Widget(field, key, "NZ", {
25+
container: $(elem).find('.form__field-holder').get(0)
26+
});
2627

27-
/* updates manual fields and hidden metadata */
28-
widget.on('result:select', function (value, item) {
29-
/* populate postal line fields */
30-
for (var i = 1; i <= 6; i++) {
31-
manual.find('input[name*=PostalLine' + i + ']').val(item['postal_line_' + i] || '')
32-
}
28+
/* updates manual fields and hidden metadata */
29+
widget.on('result:select', function(value, item) {
30+
/* populate postal line fields */
31+
for (var i = 1; i <= 6; i++) {
32+
manual.find('input[name*=PostalLine' + i + ']').val(item['postal_line_' + i] || '')
33+
}
3334

34-
manual.find('input[name*=Suburb]').val(item.suburb || '')
35-
manual.find('input[name*=City]').val(item.city || '')
36-
manual.find('input[name*=Postcode]').val(item.postcode || '')
37-
manual.find('input[name*=Longitude]').val(item.x || '')
38-
manual.find('input[name*=Latitude]').val(item.y || '')
35+
manual.find('input[name*=Suburb]').val(item.suburb || '')
36+
manual.find('input[name*=City]').val(item.city || '')
37+
manual.find('input[name*=Postcode]').val(item.postcode || '')
38+
manual.find('input[name*=Longitude]').val(item.x || '')
39+
manual.find('input[name*=Latitude]').val(item.y || '')
3940

40-
$('body').trigger(jQuery.Event('addressselected'))
41-
})
41+
$('body').trigger(jQuery.Event('addressselected'))
42+
})
4243

43-
/* click handler to toggle manual div */
44-
toggle.on('click', function (e) {
45-
e.preventDefault()
44+
/* click handler to toggle manual div */
45+
toggle.on('click', function(e) {
46+
e.preventDefault()
4647

47-
manual.toggle('slow')
48+
manual.toggle('slow')
4849

49-
// if the manual address is visible then add a hidden flag so
50-
if (manual.is(':visible')) {
51-
useManual.val('1')
52-
} else {
53-
useManual.val('0')
54-
}
55-
})
50+
// if the manual address is visible then add a hidden flag so
51+
if (manual.is(':visible')) {
52+
useManual.val('1')
53+
} else {
54+
useManual.val('0')
55+
}
56+
})
5657

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-
})
58+
/* on manually changing of the fields then we have to clear x/y */
59+
manual.on('keydown', 'input', function(e) {
60+
manual.find('input[name*=Longitude]').val('')
61+
manual.find('input[name*=Latitude]').val('')
62+
})
6263

63-
/* focusing back on the address dropdown should hide the manual */
64-
input.on('focus', function (e) {
65-
manual.slideUp()
66-
})
64+
/* focusing back on the address dropdown should hide the manual */
65+
input.on('focus', function(e) {
66+
manual.slideUp()
67+
})
68+
})
6769
})
68-
})
6970
})(jQuery)

code/formfields/AddressFinderField.php renamed to src/AddressFinderField.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?php
22

3+
namespace FullscreenInteractive\SilverStripe;
4+
5+
use SilverStripe\Forms\FieldList;
36
use SilverStripe\Forms\TextField;
7+
use SilverStripe\Forms\HiddenField;
8+
use SilverStripe\Forms\NumericField;
9+
10+
use SilverStripe\ORM\DataObjectInterface;
11+
use SilverStripe\View\Requirements;
12+
use SilverStripe\Core\Config\Config;
413

514
/**
615
* A wrapper for the AddressFinder API.
@@ -74,6 +83,8 @@ public function __construct($name, $title = null, $value = null)
7483
_t("AddressFinderField.POSTCODE", "Postcode")
7584
));
7685

86+
$this->setFieldHolderTemplate('Includes/AddressFinderField_holder');
87+
7788
parent::__construct($name, $title, $value);
7889
}
7990

@@ -89,6 +100,8 @@ public function setReadonly($bool)
89100
foreach ($this->manualFields as $field) {
90101
$field->setReadonly($bool);
91102
}
103+
104+
return $this;
92105
}
93106

94107
/**
@@ -106,6 +119,8 @@ public function setError($message, $messageType)
106119
if ($field) {
107120
$field->setError($message, 'validation');
108121
}
122+
123+
return $this;
109124
}
110125

111126
/**
@@ -125,9 +140,7 @@ public function performReadonlyTransformation()
125140
}
126141

127142
/**
128-
* @param bool $bool
129143
*
130-
* @return void
131144
*/
132145
public function setDisabled($bool)
133146
{
@@ -136,6 +149,8 @@ public function setDisabled($bool)
136149
foreach ($this->manualFields as $field) {
137150
$field->setDisabled($bool);
138151
}
152+
153+
return $this;
139154
}
140155

141156
/**
@@ -145,12 +160,11 @@ public function setDisabled($bool)
145160
*/
146161
public function FieldHolder($properties = array())
147162
{
148-
Requirements::javascript('//www.addressfinder.co.nz/assets/v2/widget.js');
149-
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
163+
Requirements::javascript('//api.addressfinder.io/assets/v3/widget.js');
164+
Requirements::javascript(ADMIN_THIRDPARTY_DIR . '/jquery/jquery.js');
150165
Requirements::javascript('addressfinder/javascript/addressfinder.js');
151166

152167
$properties = array(
153-
'ApiKey' => Config::inst()->get('AddressFinder', 'api_key'),
154168
'ManualAddressFields' => $this->getManualFields(),
155169
'AddressField' => $this->addressField->Field(),
156170
'ManualToggleField' => $this->manualToggle,
@@ -159,6 +173,14 @@ public function FieldHolder($properties = array())
159173
return parent::FieldHolder($properties);
160174
}
161175

176+
/**
177+
*
178+
*/
179+
public function getApiKey()
180+
{
181+
return Config::inst()->get(AddressFinderField::class, 'api_key');
182+
}
183+
162184
/**
163185
* @return FieldList
164186
*/
@@ -209,7 +231,7 @@ public function setValue($value, $record = null)
209231
/**
210232
* @param SilverStripe\ORM\DataObjectInterface
211233
*/
212-
public function saveInto(SilverStripe\ORM\DataObjectInterface $record)
234+
public function saveInto(DataObjectInterface $record)
213235
{
214236
$record->{$this->getName()} = $this->addressField->Value();
215237

templates/AddressFinderField_holder.ss

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<div class="address_finder" data-api-key="$ApiKey" >
2+
<div id="$Name" class="address_finder_address form-group field text<% if $extraClass %> $extraClass<% end_if %>" style="display: none;">
3+
<% if $Title %><label class="form__field-label" for="$ID">$Title</label><% end_if %>
4+
5+
<div class="form__field-holder" style="position: relative">
6+
$AddressField
7+
8+
9+
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
10+
<% if $Description %><p class="form__field-description">$Description</p><% end_if %>
11+
12+
<div class='address_finder_attribution'>
13+
<p><a href='http://addressfinder.co.nz'>AddressFinder</a> provided by <a href='http://www.abletech.co.nz/'>Able Technology</a></p>
14+
</div>
15+
16+
<div class="toggle_manual_address" style="display: none">
17+
<p><a href="#"><% _t('AddressFinderField.ENTERMANUAL', 'Enter your address manually') %></a></p>
18+
</div>
19+
</div>
20+
21+
<% if $RightTitle %><p class="form__field-extra-label" id="extra-label-$ID">$RightTitle</p><% end_if %>
22+
</div>
23+
24+
<div class="manual_address">
25+
$ManualToggleField
26+
27+
<% loop ManualAddressFields %>
28+
$FieldHolder
29+
<% end_loop %>
30+
</div>
31+
</div>

0 commit comments

Comments
 (0)