Skip to content

Commit

Permalink
Fix dropdown fields
Browse files Browse the repository at this point in the history
Changed BootstrapDropdownField to avoid conflicts with with
DropdownField and ChosenDropdownfield.

BootstrapDropdownField:
Changed to render with default Dropdownfield.ss template.
Use bootstrap-select JS plugin by adding ExtraClass 'selectpicker'.
Add option bootstrap_select_included to config.yml

ChosenDropdownField
Include/add chosen v1.6.2 library
With bootstrap_included set to false: load silverstripe thirdparty
jquery and /admin/thirdparty/chosen/chosen/chosen.jquery.js
With bootstrap_included set to true: load chosen v1.6.2
(silverstripe bundled version
/admin/thirdparty/chosen/chosen/chosen.jquery.js uses jQuery.browser()
method which removed since jQuery 1.9 – but Bootstrap 3.3.6 and above
requires jQuery 1.9.1+)

caveats:
BootstrapDropdownField can't be used with jquery_included set to false,
as this will include the ss-bundled version which is too low.
To use BootstrapDropdownField you will need to include jQuery 1.9.1+
(Bootstrap 3.3 and above requires 1.9.1+ anyway)
  • Loading branch information
derralf authored and derralf committed Oct 17, 2016
1 parent 05e7829 commit 90111d9
Show file tree
Hide file tree
Showing 79 changed files with 5,409 additions and 71 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BootstrapForm:
bootstrap_included: false
jquery_included: false
bootstrap_form_included: false
bootstrap_select_included: false
inline_fields:
- CheckboxField
- FormAction
Expand Down
49 changes: 19 additions & 30 deletions code/BootstrapDropdownField.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
<?php

/**
* Defines a FormField that uses the bootstrap-select JS plugin for making
* dropdown fields nice.
*
*/
class BootstrapDropdownField extends DropdownField {
protected $optionsList;

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

return $this;
}

public function setOptions($opts) {
$this->optionsList = $opts;

return $this;
}

public function getOptions() {
$options = ArrayList::create();
$selectedValue = $this->Value();
foreach ($this->optionsList as $val => $label) {
$isSelected = $selectedValue == (string) $val;
$options->push(ArrayData::create(array(
'Label' => $label,
'Value' => $val,
'Selected' => $isSelected
)));
/**
* Builds the form field, sets default attributes, and includes JS
*
* @param array $attributes The attributes to include on the formfield
* @return SSViewer
*/
public function FieldHolder($attributes = array ()) {
if(!Config::inst()->get('BootstrapForm', 'bootstrap_select_included')) {
$current_locale = (class_exists('Translatable')) ? Translatable::get_current_locale() : i18n::get_locale();
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/bootstrap-select.min.js");
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/js/i18n/defaults-{$current_locale}.js");
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap-select/css/bootstrap-select.min.css");
}

return $options;
}

public function Field($attributes = array()) {
Requirements::javascript(BOOTSTRAP_FORMS_DIR . "/javascript/bootstrap_forms.js");

return $this->renderWith('BootstrapDropdownField');
$this->addExtraClass('selectpicker');
return parent::FieldHolder($attributes);
}
}
14 changes: 11 additions & 3 deletions code/ChosenDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ public function setSearchThreshold($num) {
* @return SSViewer
*/
public function FieldHolder($attributes = array ()) {
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");

if(!Config::inst()->get('BootstrapForm', 'jquery_included')) {
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
Requirements::javascript(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.jquery.js");
Requirements::css(FRAMEWORK_DIR."/admin/thirdparty/chosen/chosen/chosen.css");
} else {
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.jquery.js");
Requirements::css(BOOTSTRAP_FORMS_DIR."/javascript/chosen/chosen.css");
}

$this->addExtraClass('chosen');
if(!$this->getAttribute('data-search-threshold')) {
$this->setSearchThreshold(self::$default_search_threshold);
}
return parent::FieldHolder($attributes);
}
}
}
293 changes: 293 additions & 0 deletions javascript/bootstrap-select/css/bootstrap-select.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javascript/bootstrap-select/css/bootstrap-select.css.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions javascript/bootstrap-select/css/bootstrap-select.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 90111d9

Please sign in to comment.