components-select2.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var ComponentsSelect2 = function() {
  2. var handleDemo = function() {
  3. // Set the "bootstrap" theme as the default theme for all Select2
  4. // widgets.
  5. //
  6. // @see https://github.com/select2/select2/issues/2927
  7. $.fn.select2.defaults.set("theme", "bootstrap");
  8. var placeholder = "Select a State";
  9. $(".select2, .select2-multiple").select2({
  10. placeholder: placeholder,
  11. width: null
  12. });
  13. $(".select2-allow-clear").select2({
  14. allowClear: true,
  15. placeholder: placeholder,
  16. width: null
  17. });
  18. // @see https://select2.github.io/examples.html#data-ajax
  19. function formatRepo(repo) {
  20. if (repo.loading) return repo.text;
  21. var markup = "<div class='select2-result-repository clearfix'>" +
  22. "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
  23. "<div class='select2-result-repository__meta'>" +
  24. "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
  25. if (repo.description) {
  26. markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
  27. }
  28. markup += "<div class='select2-result-repository__statistics'>" +
  29. "<div class='select2-result-repository__forks'><span class='glyphicon glyphicon-flash'></span> " + repo.forks_count + " Forks</div>" +
  30. "<div class='select2-result-repository__stargazers'><span class='glyphicon glyphicon-star'></span> " + repo.stargazers_count + " Stars</div>" +
  31. "<div class='select2-result-repository__watchers'><span class='glyphicon glyphicon-eye-open'></span> " + repo.watchers_count + " Watchers</div>" +
  32. "</div>" +
  33. "</div></div>";
  34. return markup;
  35. }
  36. function formatRepoSelection(repo) {
  37. return repo.full_name || repo.text;
  38. }
  39. $(".js-data-example-ajax").select2({
  40. width: "off",
  41. ajax: {
  42. url: "https://api.github.com/search/repositories",
  43. dataType: 'json',
  44. delay: 250,
  45. data: function(params) {
  46. return {
  47. q: params.term, // search term
  48. page: params.page
  49. };
  50. },
  51. processResults: function(data, page) {
  52. // parse the results into the format expected by Select2.
  53. // since we are using custom formatting functions we do not need to
  54. // alter the remote JSON data
  55. return {
  56. results: data.items
  57. };
  58. },
  59. cache: true
  60. },
  61. escapeMarkup: function(markup) {
  62. return markup;
  63. }, // let our custom formatter work
  64. minimumInputLength: 1,
  65. templateResult: formatRepo,
  66. templateSelection: formatRepoSelection
  67. });
  68. $("button[data-select2-open]").click(function() {
  69. $("#" + $(this).data("select2-open")).select2("open");
  70. });
  71. $(":checkbox").on("click", function() {
  72. $(this).parent().nextAll("select").prop("disabled", !this.checked);
  73. });
  74. // copy Bootstrap validation states to Select2 dropdown
  75. //
  76. // add .has-waring, .has-error, .has-succes to the Select2 dropdown
  77. // (was #select2-drop in Select2 v3.x, in Select2 v4 can be selected via
  78. // body > .select2-container) if _any_ of the opened Select2's parents
  79. // has one of these forementioned classes (YUCK! ;-))
  80. $(".select2, .select2-multiple, .select2-allow-clear, .js-data-example-ajax").on("select2:open", function() {
  81. if ($(this).parents("[class*='has-']").length) {
  82. var classNames = $(this).parents("[class*='has-']")[0].className.split(/\s+/);
  83. for (var i = 0; i < classNames.length; ++i) {
  84. if (classNames[i].match("has-")) {
  85. $("body > .select2-container").addClass(classNames[i]);
  86. }
  87. }
  88. }
  89. });
  90. $(".js-btn-set-scaling-classes").on("click", function() {
  91. $("#select2-multiple-input-sm, #select2-single-input-sm").next(".select2-container--bootstrap").addClass("input-sm");
  92. $("#select2-multiple-input-lg, #select2-single-input-lg").next(".select2-container--bootstrap").addClass("input-lg");
  93. $(this).removeClass("btn-primary btn-outline").prop("disabled", true);
  94. });
  95. }
  96. return {
  97. //main function to initiate the module
  98. init: function() {
  99. handleDemo();
  100. }
  101. };
  102. }();
  103. if (App.isAngularJsApp() === false) {
  104. jQuery(document).ready(function() {
  105. ComponentsSelect2.init();
  106. });
  107. }