Skip to content

Commit 2ad1bd7

Browse files
committed
cleanup
1 parent 0eac5ef commit 2ad1bd7

File tree

3 files changed

+19
-144
lines changed

3 files changed

+19
-144
lines changed

code/DropdownImageField.php

Lines changed: 10 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,31 @@
11
<?php
22

3-
class DropdownImageField extends FormField {
3+
class DropdownImageField extends DropdownField {
44

5-
/**
6-
* @var boolean $source Associative or numeric array of all dropdown items,
7-
* with array key as the submitted field value, and the array value as a
8-
* natural language description shown in the interface element.
9-
*/
10-
protected $sourceObject;
11-
12-
/**
13-
* @ignore
14-
*/
155
protected $keyField, $labelField, $imageField;
16-
17-
/**
18-
* @var boolean $isSelected Determines if the field was selected
19-
* at the time it was rendered, so if {@link $value} matches on of the array
20-
* values specified in {@link $source}
21-
*/
22-
protected $isSelected;
23-
24-
/**
25-
* @var boolean $hasEmptyDefault Show the first <option> element as
26-
* empty (not having a value), with an optional label defined through
27-
* {@link $emptyString}. By default, the <select> element will be
28-
* rendered with the first option from {@link $source} selected.
29-
*/
30-
protected $hasEmptyDefault = false;
31-
32-
/**
33-
* @var string $emptyString The title shown for an empty default selection,
34-
* e.g. "Select...".
35-
*/
36-
protected $emptyString = '';
37-
38-
/**
39-
* @var array $disabledItems The keys for items that should be disabled (greyed out) in the dropdown
40-
*/
41-
protected $disabledItems = array();
426

43-
public function __construct($name, $title=null, $sourceObject='Group', $keyField = 'ID', $labelField = null, $imageField = 'Image', $value='', $form=null, $emptyString=null) {
44-
$this->setSourceObject($sourceObject);
7+
public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value='', $form=null) {
458

469
$this->keyField = $keyField;
4710
$this->labelField = $labelField;
4811
$this->imageField = $imageField;
49-
50-
if($emptyString === true) {
51-
Deprecation::notice('3.1',
52-
'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument',
53-
Deprecation::SCOPE_GLOBAL);
54-
}
55-
if(is_string($emptyString)) {
56-
Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string emptyString argument.',
57-
Deprecation::SCOPE_GLOBAL);
58-
}
59-
60-
if($emptyString) $this->setHasEmptyDefault(true);
61-
if(is_string($emptyString)) $this->setEmptyString($emptyString);
62-
63-
parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
6412

13+
parent::__construct($name, ($title===null) ? $name : $title, $sourceObject, $value, $form);
14+
6515
$this->addExtraClass('dropdown');
6616
}
6717

6818
public function Field($properties = array()) {
19+
6920
$dirName = basename(dirname(dirname(__FILE__)));;
7021

7122
Requirements::javascript($dirName.'/javascript/Polyfill.js');
7223
Requirements::javascript($dirName.'/javascript/ImageSelect.jquery.js');
7324
Requirements::css($dirName.'/css/ImageSelect.css');
7425

75-
$source = $this->getSourceObject();
26+
$source = $this->getSource();
7627
$options = array();
7728
if($source) {
78-
// SQLMap needs this to add an empty value to the options
7929
if(is_object($source) && $this->emptyString) {
8030
$options[] = new ArrayData(array(
8131
'Value' => '',
@@ -125,102 +75,19 @@ public function Field($properties = array()) {
12575

12676
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
12777

128-
return parent::Field($properties);
129-
}
130-
131-
/**
132-
* Mark certain elements as disabled,
133-
* regardless of the {@link setDisabled()} settings.
134-
*
135-
* @param array $items Collection of array keys, as defined in the $source array
136-
*/
137-
public function setDisabledItems($items){
138-
$this->disabledItems = $items;
139-
return $this;
78+
return FormField::Field($properties);
14079
}
14180

142-
/**
143-
* @return Array
144-
*/
145-
public function getDisabledItems(){
146-
return $this->disabledItems;
147-
}
148-
149-
public function getAttributes() {
150-
return array_merge(
151-
parent::getAttributes(),
152-
array('type' => null, 'value' => null)
153-
);
154-
}
155-
156-
/**
157-
* @return boolean
158-
*/
159-
public function isSelected() {
160-
return $this->isSelected;
161-
}
162-
16381
/**
16482
* Gets the source array including any empty default values.
16583
*
16684
* @return array
16785
*/
16886
public function getSourceObject() {
169-
if(is_array($this->sourceObject) && $this->getHasEmptyDefault()) {
170-
return ArrayList::create()->unshift(array($this->keyField = '', $this->labelField = $this->emptyString, $this->imageField = ''));
87+
if(is_object($this->source) && $this->getHasEmptyDefault()) {
88+
return $this->source->unshift(array($this->keyField = '', $this->labelField = $this->emptyString, $this->imageField = ''));
17189
} else {
172-
return $this->sourceObject;
90+
return $this->source;
17391
}
17492
}
175-
176-
/**
177-
* @param array $source
178-
*/
179-
public function setSourceObject($source) {
180-
$this->sourceObject = $source;
181-
return $this;
182-
}
183-
184-
/**
185-
* @param boolean $bool
186-
*/
187-
public function setHasEmptyDefault($bool) {
188-
$this->hasEmptyDefault = $bool;
189-
return $this;
190-
}
191-
192-
/**
193-
* @return boolean
194-
*/
195-
public function getHasEmptyDefault() {
196-
return $this->hasEmptyDefault;
197-
}
198-
199-
/**
200-
* Set the default selection label, e.g. "select...".
201-
* Defaults to an empty string. Automatically sets
202-
* {@link $hasEmptyDefault} to true.
203-
*
204-
* @param string $str
205-
*/
206-
public function setEmptyString($str) {
207-
$this->setHasEmptyDefault(true);
208-
$this->emptyString = $str;
209-
return $this;
210-
}
211-
212-
/**
213-
* @return string
214-
*/
215-
public function getEmptyString() {
216-
return $this->emptyString;
217-
}
218-
219-
public function performReadonlyTransformation() {
220-
$field = $this->castedCopy('LookupField');
221-
$field->setSourceObject($this->getSourceObject());
222-
$field->setReadonly(true);
223-
224-
return $field;
225-
}
22693
}

javascript/ImageSelect.jquery.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159

160160
if(img_src != undefined){
161161
var template = html_template.replace('{url}',img_src);
162+
// SS uses a modified Chosen.js and after the 'showing_dropdown' event the
163+
// values get immediatelly reset, also removing the images. Solved it with setTimeout.
162164
setTimeout(function(li, template){
163165
$(li).prepend(template.replace('{class_name}','chose-image-list'));
164166
}, 1, li, template);

templates/DropdownImageField.ss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<select $AttributesHTML>
22
<% loop $Options %>
3-
<option data-img-src="$Image.Link" value="$Value.XML"<% if $Selected %> selected="selected"<% end_if %><% if $Disabled %> disabled="disabled"<% end_if %>><% if Title == "--nbsp" %>&nbsp;<% else %>$Title.XML<% end_if %></option>
3+
<option
4+
data-img-src="$Image.URL"
5+
value="$Value.XML"<% if $Selected %>
6+
selected="selected"<% end_if %><% if $Disabled %>
7+
disabled="disabled"<% end_if %>>
8+
<% if $Title = "--nbsp" %>&nbsp;
9+
<% else %>$Title.XML<% end_if %></option>
410
<% end_loop %>
511
</select>

0 commit comments

Comments
 (0)