Permalink
Browse files

ENHANCEMENT Using chosen.js for group selection in Member->getCMSFiel…

…ds()
  • Loading branch information...
1 parent 8fa2664 commit 46628721fe72d4473133ae3dd37b536aa53aea16 @chillu chillu committed Mar 2, 2012
Showing with 22 additions and 7 deletions.
  1. +2 −2 admin/javascript/LeftAndMain.js
  2. +9 −5 security/Member.php
  3. +11 −0 templates/forms/CheckboxSetField_Select.ss
View
4 admin/javascript/LeftAndMain.js
@@ -363,9 +363,9 @@ jQuery.noConflict();
* we can fix the height cropping.
*/
- $('.cms .field.dropdown').entwine({
+ $('.cms .field.dropdown, .cms .field.checkboxset').entwine({
@sminnee
sminnee Mar 5, 2012

You seem to be suggesting that every single checkboxset field will get the chosen.js treatment, but that is only the case if the ChecktboxSetField_Select template is used. I'd suggest applying another class, eg, "chosenjs", with the CheckboxSetField_Select template or simialr, and then applying this javascript to that class.

If a random developer chucks a vanilla CheckboxSetField into their getCMSFields, I believe it will break at the moment.

@chillu
chillu Mar 5, 2012

Nah it won't break, because "normal" checkboxsetfields don't contain a <select> element.
It'll just have the class "has-chzn" wrongly applied, but that doesn't have any styling or behaviour effect.
I'll use the custom class in the template as you suggested, cleaner solution. See eab8401.

onmatch: function() {
- $(this).find("select:not(.no-chzn)").chosen();
+ $(this).find("select:not(.no-chzn)").data('placeholder', ' ').chosen();
$(this).addClass("has-chzn");
this._super();
View
14 security/Member.php
@@ -1133,12 +1133,16 @@ public function getCMSFields() {
// Groups relation will get us into logical conflicts because
// Members are displayed within group edit form in SecurityAdmin
$fields->removeByName('Groups');
-
+
if(Permission::check('EDIT_PERMISSIONS')) {
- $groupsField = new TreeMultiselectField('Groups', false, 'Group');
- $fields->findOrMakeTab('Root.Groups', singleton('Group')->i18n_plural_name());
- $fields->addFieldToTab('Root.Groups', $groupsField);
-
+ $groupsMap = DataList::create('Group')->map('ID', 'Breadcrumbs')->toArray();
+ asort($groupsMap);
+ $fields->addFieldToTab('Root.Main',
+ Object::create('CheckboxSetField', 'Groups', singleton('Group')->i18n_plural_name())
+ ->setTemplate('CheckboxSetField_Select')
+ ->setSource($groupsMap)
+ );
+
// Add permission field (readonly to avoid complicated group assignment logic).
// This should only be available for existing records, as new records start
// with no permissions until they have a group assignment anyway.
View
11 templates/forms/CheckboxSetField_Select.ss
@@ -0,0 +1,11 @@
+<%-- Renders a CheckboxField with $multiple=true as a select element which can save into relations.--%>
+<%-- TODO Make relation saving available on ListboxField --%>
+<select id="$ID" class="$extraClass" name="$Name[]" multiple="true">
+ <% if Options.Count %>
+ <% control Options %>
+ <option class="$Class" value="$Value"<% if isChecked %> selected="selected"<% end_if %>>
+ $Title
+ </option>
+ <% end_control %>
+ <% end_if %>
+</select>

0 comments on commit 4662872

Please sign in to comment.