You are here

README.txt in Select with Style 7

SELECT WITH STYLE
=================

This module allows you to attractively style select lists, in particular those
that feature parent/child hierarchies, like taxonomies. The module adds CSS
classes that reflect family names and depths in the hierarchy, so you can render
one family differently from another and different from their children. You can
apply colours, font styles or even images, like country flags.

A few basic .css files have been provided to get you started. You can select
these when you configure the widget. E.g. "Plain Jane - blue.css" or "flags.css"
You can add your own CSS, either via your theme's style.css or in a separate
.css file, which you place in a directory you declare on the Select with Style
configuration page.

You can also set the height of select boxes and may use any hierarchy depth
indicator prefix string instead of core's '-'.

All of the above is achieved through two new variants of the classic "Select
list" widget. Both variants are on offer when you select a widget for a field
of type term reference. The new widgets are:
- Select list, styled optgroups
- Select list, styled tree

Once assigned to a field, the same widget will automatically appear in Views
when you add an exposed filter, unless you unticked the associated box on the
module configuration page. Or if you want the widget to appear in Views
ONLY (and not when editing an entity), then you can use the global settings on
the module configuration page to configure Views exposed filter widgets.

Just to make that even clearer, here are the steps for the Select with Style
widget. After you have attached your Select with Style widget to a taxonomy
field under the "Manage fields" tab of the node or other entity type, go to your
Views console and press add in the "Filter criteria" section. Pick the Views
field in question. Do not pick "Content: Has taxonomy term(s)". On the next
panel select "Dropdown" rather than "Autocomplete" and tick the "Show hierarchy
in dropdown" box, if so desired (recommended). Then, after "Apply", on the next
panel tick the box "Expose this filter to visitors...". The exposed filter
should automatically render in the same widget style as used on the content
Edit form, thus creating a consistent look throughout your site. This also works
for filters that are exposed in a block (one of the advanced options on the
right hand side in the Views UI). Note that for an exposed filter to show up in
a block, AJAX must be set to Yes on the View. Be aware that ticking "Allow
multiple selections" on the filter configuration panel results in a multi-select
box, which renders differently from a single-select.

The two widgets are similar in appearance, but different in functionality. In
the styled optgroups parent options become labels, which cannot be selected
(clicked), whereas in the styled tree the parents are selectable options, just
like the children. Another difference between the styled optgroups and the
styled tree is that due to W3C/browser restrictions optgroups only go one level
deep, whereas trees can be nested as deep as your taxonomy dictates.

By the way, the definition of a parent is any item that is not a leaf.

These widgets are lightweight, javascript-free solutions without dependencies.
No libraries are required. There are no module configurations. Just enable the
module and select or tweak your CSS.

CSS classes added for a parent label or option are of this format:

  class="option-parent group-PARENT tid-NUMBER depth-0"

CSS classes added for a child option are of this format:

  class="option-child group-PARENT tid-NUMBER depth-2"

Where PARENT is the name of the parent taxonomy term and NUMBER the id of the
option term.

HOW TO CREATE A COUNTRY & CITY DROP-DOWN WITH FLAG IMAGES?
Create a city vocabulary at Structure >> Taxonomy. Add terms for cities and
countries in any order. Then use the handles to drag cities under their parent
countries and indent them to make them children. Use the official country names,
in the language of your site. With the city vocabulary saved, click the "Manage
fields" tab of a content type. Under the "Add a new field" heading choose the
field type Term Reference and team it up with either the "Select list, styled
tree" or "Select list, styled optgroup" widgets. Press Save and Save again to
arrive at the field Edit tab. Here you can specify additional parameters
pertaining to Select with Style. The important one for this use-case is the
transformation callback. Enter "select_with_style_country_name_to_code" and
Save. The final step is to select the flags.css file from the "Styling file(s)"
select box. That should do it. Works in Firefox, not yet supported by Chrome and
Safari.

Finally, while the main "Select with Style" functionality does not require any
javascript, you may choose to add some additional special effects. See
select_with_style.js


HTML/CSS DISCLAIMER:
Anno 2014 browser support for select lists is still so-so. Firefox is generally
great, but Chrome and Safari do not honour all of the attributes that are
standard on other HTML elements. This is especially so for single-choice
drop-downs.


INVITE FOR FUTURE EXTENSION:
Someone to create a widget to make general lists other than taxonomies
hierarchical, maybe using indentation to denote an item is a child of the
preceding item:

1|parent1
  11|child_a
  49|child_b
5|parent2
  66|child_c
  ...
...

APPENDIX
While the main "Select with Style" functionality does not require any
javascript, you may choose to add some additional effects. Below are a few
ideas. Put the one you like in select_with_style.js
These behaviors are attached to the entire page. When the document is ready
all of the attach functions below are called, passing in the document context
and the value of Drupal.settings.

(function($) {

  // #1 Put a green border around select lists, text fields and text areas, the
  //    moment an option is clicked or a value entered.
  Drupal.behaviors.select_with_style_color_border_on_select = {
    attach: function(context, settings) {
      $(":input, :checkbox", context).change(function() { // can't seem to style checkboxes
        this.style.border = "2px solid #5ed230";
      });
    }
  }

  // #2 Render select as small box initially. Then expand the select box
  //    to reveal all its options when the box is hovered over.
  Drupal.behaviors.select_with_style_expand_on_hover = {
    attach: function(context, settings) {
      $("select", context).mouseover(function() {
        this.size = this.options.length;
      });
      $("select").mouseout(function() {
        this.size = 1;
      });
    }
  }

})(jQuery);

File

select_with_style/README.txt
View source
  1. SELECT WITH STYLE
  2. =================
  3. This module allows you to attractively style select lists, in particular those
  4. that feature parent/child hierarchies, like taxonomies. The module adds CSS
  5. classes that reflect family names and depths in the hierarchy, so you can render
  6. one family differently from another and different from their children. You can
  7. apply colours, font styles or even images, like country flags.
  8. A few basic .css files have been provided to get you started. You can select
  9. these when you configure the widget. E.g. "Plain Jane - blue.css" or "flags.css"
  10. You can add your own CSS, either via your theme's style.css or in a separate
  11. .css file, which you place in a directory you declare on the Select with Style
  12. configuration page.
  13. You can also set the height of select boxes and may use any hierarchy depth
  14. indicator prefix string instead of core's '-'.
  15. All of the above is achieved through two new variants of the classic "Select
  16. list" widget. Both variants are on offer when you select a widget for a field
  17. of type term reference. The new widgets are:
  18. - Select list, styled optgroups
  19. - Select list, styled tree
  20. Once assigned to a field, the same widget will automatically appear in Views
  21. when you add an exposed filter, unless you unticked the associated box on the
  22. module configuration page. Or if you want the widget to appear in Views
  23. ONLY (and not when editing an entity), then you can use the global settings on
  24. the module configuration page to configure Views exposed filter widgets.
  25. Just to make that even clearer, here are the steps for the Select with Style
  26. widget. After you have attached your Select with Style widget to a taxonomy
  27. field under the "Manage fields" tab of the node or other entity type, go to your
  28. Views console and press add in the "Filter criteria" section. Pick the Views
  29. field in question. Do not pick "Content: Has taxonomy term(s)". On the next
  30. panel select "Dropdown" rather than "Autocomplete" and tick the "Show hierarchy
  31. in dropdown" box, if so desired (recommended). Then, after "Apply", on the next
  32. panel tick the box "Expose this filter to visitors...". The exposed filter
  33. should automatically render in the same widget style as used on the content
  34. Edit form, thus creating a consistent look throughout your site. This also works
  35. for filters that are exposed in a block (one of the advanced options on the
  36. right hand side in the Views UI). Note that for an exposed filter to show up in
  37. a block, AJAX must be set to Yes on the View. Be aware that ticking "Allow
  38. multiple selections" on the filter configuration panel results in a multi-select
  39. box, which renders differently from a single-select.
  40. The two widgets are similar in appearance, but different in functionality. In
  41. the styled optgroups parent options become labels, which cannot be selected
  42. (clicked), whereas in the styled tree the parents are selectable options, just
  43. like the children. Another difference between the styled optgroups and the
  44. styled tree is that due to W3C/browser restrictions optgroups only go one level
  45. deep, whereas trees can be nested as deep as your taxonomy dictates.
  46. By the way, the definition of a parent is any item that is not a leaf.
  47. These widgets are lightweight, javascript-free solutions without dependencies.
  48. No libraries are required. There are no module configurations. Just enable the
  49. module and select or tweak your CSS.
  50. CSS classes added for a parent label or option are of this format:
  51. class="option-parent group-PARENT tid-NUMBER depth-0"
  52. CSS classes added for a child option are of this format:
  53. class="option-child group-PARENT tid-NUMBER depth-2"
  54. Where PARENT is the name of the parent taxonomy term and NUMBER the id of the
  55. option term.
  56. HOW TO CREATE A COUNTRY & CITY DROP-DOWN WITH FLAG IMAGES?
  57. Create a city vocabulary at Structure >> Taxonomy. Add terms for cities and
  58. countries in any order. Then use the handles to drag cities under their parent
  59. countries and indent them to make them children. Use the official country names,
  60. in the language of your site. With the city vocabulary saved, click the "Manage
  61. fields" tab of a content type. Under the "Add a new field" heading choose the
  62. field type Term Reference and team it up with either the "Select list, styled
  63. tree" or "Select list, styled optgroup" widgets. Press Save and Save again to
  64. arrive at the field Edit tab. Here you can specify additional parameters
  65. pertaining to Select with Style. The important one for this use-case is the
  66. transformation callback. Enter "select_with_style_country_name_to_code" and
  67. Save. The final step is to select the flags.css file from the "Styling file(s)"
  68. select box. That should do it. Works in Firefox, not yet supported by Chrome and
  69. Safari.
  70. Finally, while the main "Select with Style" functionality does not require any
  71. javascript, you may choose to add some additional special effects. See
  72. select_with_style.js
  73. HTML/CSS DISCLAIMER:
  74. Anno 2014 browser support for select lists is still so-so. Firefox is generally
  75. great, but Chrome and Safari do not honour all of the attributes that are
  76. standard on other HTML elements. This is especially so for single-choice
  77. drop-downs.
  78. INVITE FOR FUTURE EXTENSION:
  79. Someone to create a widget to make general lists other than taxonomies
  80. hierarchical, maybe using indentation to denote an item is a child of the
  81. preceding item:
  82. 1|parent1
  83. 11|child_a
  84. 49|child_b
  85. 5|parent2
  86. 66|child_c
  87. ...
  88. ...
  89. APPENDIX
  90. While the main "Select with Style" functionality does not require any
  91. javascript, you may choose to add some additional effects. Below are a few
  92. ideas. Put the one you like in select_with_style.js
  93. These behaviors are attached to the entire page. When the document is ready
  94. all of the attach functions below are called, passing in the document context
  95. and the value of Drupal.settings.
  96. (function($) {
  97. // #1 Put a green border around select lists, text fields and text areas, the
  98. // moment an option is clicked or a value entered.
  99. Drupal.behaviors.select_with_style_color_border_on_select = {
  100. attach: function(context, settings) {
  101. $(":input, :checkbox", context).change(function() { // can't seem to style checkboxes
  102. this.style.border = "2px solid #5ed230";
  103. });
  104. }
  105. }
  106. // #2 Render select as small box initially. Then expand the select box
  107. // to reveal all its options when the box is hovered over.
  108. Drupal.behaviors.select_with_style_expand_on_hover = {
  109. attach: function(context, settings) {
  110. $("select", context).mouseover(function() {
  111. this.size = this.options.length;
  112. });
  113. $("select").mouseout(function() {
  114. this.size = 1;
  115. });
  116. }
  117. }
  118. })(jQuery);