select.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. {% assign id = include.id | default: include.key %}
  2. {% assign value = include.value %}
  3. {% if id %}
  4. {% assign key = include.key | default: 'people' %}
  5. {% assign data = site.data.selects[key] %}
  6. <select type="text" class="form-select{% if include.class %} {{ include.class }}{% endif %}{% if include.state %} is-{{ include.state }}{% endif %}"{% if include.placeholder %} placeholder="{{ include.placeholder }}"{% endif %} id="select-{{ id }}" value="{{ value }}"{% if include.multiple or data.multiple %} multiple{% endif %}>
  7. {% if include.values %}
  8. {% assign values = include.values | split: ',' %}
  9. {% for value in values %}
  10. <option value="{{ value }}">{{ value }}</option>
  11. {% endfor %}
  12. {% else %}
  13. {% assign options = data.options %}
  14. {% if data.data == 'people' %}
  15. {% for person in site.data.people limit: 20 %}
  16. <option value="{{ person.id }}"{% if include.indicator == 'avatar' %} data-custom-properties="{% capture indicator %}{% include ui/avatar.html size='xs' person=person %}{% endcapture %}{{ indicator | strip | escape }}"{% endif %}>{{ person.full_name }}</option>
  17. {% endfor %}
  18. {% elsif data.data == 'optgroup' %}
  19. {% for group in options %}
  20. <optgroup label="{{ group.title }}">
  21. {% for option in group.options %}
  22. <option value="{{ option }}">{{ option }}</option>
  23. {% endfor %}
  24. </optgroup>
  25. {% endfor %}
  26. {% else %}
  27. {% for option in options %}
  28. {% if option.first %}
  29. {% if include.indicator == 'flag' %}
  30. {% capture indicator-html %}
  31. {% assign country = option[1].flag %}
  32. {% include ui/flag.html size="xs" flag=country %}
  33. {% endcapture %}
  34. {% elsif include.indicator == 'label' %}
  35. {% capture indicator-html %}
  36. {% assign label = option[1].label %}
  37. <span class="badge bg-primary-lt">{{ label }}</span>
  38. {% endcapture %}
  39. {% endif %}
  40. <option value="{{ option[0] }}"{% if include.indicator %} data-custom-properties="{{ indicator-html | strip | escape }}"{% endif %}{% if option[1].selected %} selected{% endif %}>{{ option[1].name }}</option>
  41. {% else %}
  42. <option value="{{ option }}">{{ option }}</option>
  43. {% endif %}
  44. {% endfor %}
  45. {% endif %}
  46. {% endif %}
  47. </select>
  48. {% capture script %}
  49. <script>
  50. // @formatter:off
  51. document.addEventListener("DOMContentLoaded", function () {
  52. {% if jekyll.environment == 'development' %}
  53. window.tabler_select = window.tabler_select || {};
  54. {% endif %}
  55. var el;
  56. window.TomSelect && ({% if jekyll.environment == 'development' %}window.tabler_select["select-{{ id }}"] = {% endif %}new TomSelect(el = document.getElementById('select-{{ id }}'), {
  57. copyClassesToDropdown: false,
  58. dropdownParent: 'body',
  59. {% unless include.show-search %}
  60. controlInput: '<input>',
  61. {% endunless %}
  62. render:{
  63. item: function(data,escape) {
  64. if( data.customProperties ){
  65. return '<div><span class="dropdown-item-indicator">' + data.customProperties + '</span>' + escape(data.text) + '</div>';
  66. }
  67. return '<div>' + escape(data.text) + '</div>';
  68. },
  69. option: function(data,escape){
  70. if( data.customProperties ){
  71. return '<div><span class="dropdown-item-indicator">' + data.customProperties + '</span>' + escape(data.text) + '</div>';
  72. }
  73. return '<div>' + escape(data.text) + '</div>';
  74. },
  75. },
  76. }));
  77. });
  78. // @formatter:on
  79. </script>
  80. {% endcapture %}
  81. {% if include.show-scripts %}
  82. {{ script }}
  83. {% else %}
  84. {% capture_global scripts %}
  85. {{ script }}
  86. {% endcapture_global %}
  87. {% endif %}
  88. {% endif %}