Skip to content

Commit 524b7e2

Browse files
author
Aaron Carlino
authored
Merge pull request #51 from derralf/dropdown-bugfix
Fix dropdown fields
2 parents 05e7829 + 90111d9 commit 524b7e2

File tree

79 files changed

+5409
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+5409
-71
lines changed

_config/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ BootstrapForm:
44
bootstrap_included: false
55
jquery_included: false
66
bootstrap_form_included: false
7+
bootstrap_select_included: false
78
inline_fields:
89
- CheckboxField
910
- FormAction

code/BootstrapDropdownField.php

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

3+
/**
4+
* Defines a FormField that uses the bootstrap-select JS plugin for making
5+
* dropdown fields nice.
6+
*
7+
*/
38
class BootstrapDropdownField extends DropdownField {
4-
protected $optionsList;
59

6-
public function __construct($name, $title = NULL, $options = array(), $value = NULL) {
7-
parent::__construct($name, $title,$options, $value);
8-
$this->optionsList = $options;
910

10-
return $this;
11-
}
12-
13-
public function setOptions($opts) {
14-
$this->optionsList = $opts;
15-
16-
return $this;
17-
}
1811

19-
public function getOptions() {
20-
$options = ArrayList::create();
21-
$selectedValue = $this->Value();
22-
foreach ($this->optionsList as $val => $label) {
23-
$isSelected = $selectedValue == (string) $val;
24-
$options->push(ArrayData::create(array(
25-
'Label' => $label,
26-
'Value' => $val,
27-
'Selected' => $isSelected
28-
)));
12+
/**
13+
* Builds the form field, sets default attributes, and includes JS
14+
*
15+
* @param array $attributes The attributes to include on the formfield
16+
* @return SSViewer
17+
*/
18+
public function FieldHolder($attributes = array ()) {
19+
if(!Config::inst()->get('BootstrapForm', 'bootstrap_select_included')) {
20+
$current_locale = (class_exists('Translatable')) ? Translatable::get_current_locale() : i18n::get_locale();
21+
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/bootstrap-select.min.js");
22+
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/i18n/defaults-{$current_locale}.js");
23+
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/css/bootstrap-select.min.css");
2924
}
30-
31-
return $options;
32-
}
33-
34-
public function Field($attributes = array()) {
35-
Requirements::javascript(BOOTSTRAP_FORMS_DIR . "/javascript/bootstrap_forms.js");
36-
37-
return $this->renderWith('BootstrapDropdownField');
25+
$this->addExtraClass('selectpicker');
26+
return parent::FieldHolder($attributes);
3827
}
3928
}

code/ChosenDropdownField.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ public function setSearchThreshold($num) {
3838
* @return SSViewer
3939
*/
4040
public function FieldHolder($attributes = array ()) {
41-
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
42-
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");
41+
42+
if(!Config::inst()->get('BootstrapForm', 'jquery_included')) {
43+
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
44+
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
45+
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");
46+
} else {
47+
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.jquery.js");
48+
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.css");
49+
}
50+
4351
$this->addExtraClass('chosen');
4452
if(!$this->getAttribute('data-search-threshold')) {
4553
$this->setSearchThreshold(self::$default_search_threshold);
4654
}
4755
return parent::FieldHolder($attributes);
4856
}
49-
}
57+
}

javascript/bootstrap-select/css/bootstrap-select.css

Lines changed: 293 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/bootstrap-select/css/bootstrap-select.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/bootstrap-select/css/bootstrap-select.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)