/public/assets/pages/scripts/components-dropdowns.js

https://gitlab.com/SFMSP/Hagape · JavaScript · 263 lines · 229 code · 25 blank · 9 comment · 18 complexity · e1e76056a00d137bb31152632692fda3 MD5 · raw file

  1. var ComponentsDropdowns = function () {
  2. var handleSelect2 = function () {
  3. $('#select2_sample1').select2({
  4. placeholder: "Select an option",
  5. allowClear: true
  6. });
  7. $('#select2_sample2').select2({
  8. placeholder: "Select a State",
  9. allowClear: true
  10. });
  11. $("#select2_sample3").select2({
  12. placeholder: "Select...",
  13. allowClear: true,
  14. minimumInputLength: 1,
  15. query: function (query) {
  16. var data = {
  17. results: []
  18. }, i, j, s;
  19. for (i = 1; i < 5; i++) {
  20. s = "";
  21. for (j = 0; j < i; j++) {
  22. s = s + query.term;
  23. }
  24. data.results.push({
  25. id: query.term + i,
  26. text: s
  27. });
  28. }
  29. query.callback(data);
  30. }
  31. });
  32. function format(state) {
  33. if (!state.id) return state.text; // optgroup
  34. return "<img class='flag' src='" + App.getGlobalImgPath() + "flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
  35. }
  36. $("#select2_sample4").select2({
  37. placeholder: "Select a Country",
  38. allowClear: true,
  39. formatResult: format,
  40. formatSelection: format,
  41. escapeMarkup: function (m) {
  42. return m;
  43. }
  44. });
  45. $("#select2_sample5").select2({
  46. tags: ["red", "green", "blue", "yellow", "pink"]
  47. });
  48. function movieFormatResult(movie) {
  49. var markup = "<table class='movie-result'><tr>";
  50. if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
  51. markup += "<td valign='top'><img src='" + movie.posters.thumbnail + "'/></td>";
  52. }
  53. markup += "<td valign='top'><h5>" + movie.title + "</h5>";
  54. if (movie.critics_consensus !== undefined) {
  55. markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
  56. } else if (movie.synopsis !== undefined) {
  57. markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
  58. }
  59. markup += "</td></tr></table>"
  60. return markup;
  61. }
  62. function movieFormatSelection(movie) {
  63. return movie.title;
  64. }
  65. $("#select2_sample6").select2({
  66. placeholder: "Search for a movie",
  67. minimumInputLength: 1,
  68. ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
  69. url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
  70. dataType: 'jsonp',
  71. data: function (term, page) {
  72. return {
  73. q: term, // search term
  74. page_limit: 10,
  75. apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
  76. };
  77. },
  78. results: function (data, page) { // parse the results into the format expected by Select2.
  79. // since we are using custom formatting functions we do not need to alter remote JSON data
  80. return {
  81. results: data.movies
  82. };
  83. }
  84. },
  85. initSelection: function (element, callback) {
  86. // the input tag has a value attribute preloaded that points to a preselected movie's id
  87. // this function resolves that id attribute to an object that select2 can render
  88. // using its formatResult renderer - that way the movie name is shown preselected
  89. var id = $(element).val();
  90. if (id !== "") {
  91. $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
  92. data: {
  93. apikey: "ju6z9mjyajq2djue3gbvv26t"
  94. },
  95. dataType: "jsonp"
  96. }).done(function (data) {
  97. callback(data);
  98. });
  99. }
  100. },
  101. formatResult: movieFormatResult, // omitted for brevity, see the source of this page
  102. formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
  103. dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
  104. escapeMarkup: function (m) {
  105. return m;
  106. } // we do not want to escape markup since we are displaying html in results
  107. });
  108. }
  109. var handleSelect2Modal = function () {
  110. $('#select2_sample_modal_1').select2({
  111. placeholder: "Select an option",
  112. allowClear: true
  113. });
  114. $('#select2_sample_modal_2').select2({
  115. placeholder: "Select a State",
  116. allowClear: true
  117. });
  118. $("#select2_sample_modal_3").select2({
  119. allowClear: true,
  120. minimumInputLength: 1,
  121. query: function (query) {
  122. var data = {
  123. results: []
  124. }, i, j, s;
  125. for (i = 1; i < 5; i++) {
  126. s = "";
  127. for (j = 0; j < i; j++) {
  128. s = s + query.term;
  129. }
  130. data.results.push({
  131. id: query.term + i,
  132. text: s
  133. });
  134. }
  135. query.callback(data);
  136. }
  137. });
  138. function format(state) {
  139. if (!state.id) return state.text; // optgroup
  140. return "<img class='flag' src='" + App.getGlobalImgPath() + "flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
  141. }
  142. $("#select2_sample_modal_4").select2({
  143. allowClear: true,
  144. formatResult: format,
  145. formatSelection: format,
  146. escapeMarkup: function (m) {
  147. return m;
  148. }
  149. });
  150. $("#select2_sample_modal_5").select2({
  151. tags: ["red", "green", "blue", "yellow", "pink"]
  152. });
  153. function movieFormatResult(movie) {
  154. var markup = "<table class='movie-result'><tr>";
  155. if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
  156. markup += "<td valign='top'><img src='" + movie.posters.thumbnail + "'/></td>";
  157. }
  158. markup += "<td valign='top'><h5>" + movie.title + "</h5>";
  159. if (movie.critics_consensus !== undefined) {
  160. markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
  161. } else if (movie.synopsis !== undefined) {
  162. markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
  163. }
  164. markup += "</td></tr></table>"
  165. return markup;
  166. }
  167. function movieFormatSelection(movie) {
  168. return movie.title;
  169. }
  170. $("#select2_sample_modal_6").select2({
  171. placeholder: "Search for a movie",
  172. minimumInputLength: 1,
  173. ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
  174. url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
  175. dataType: 'jsonp',
  176. data: function (term, page) {
  177. return {
  178. q: term, // search term
  179. page_limit: 10,
  180. apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
  181. };
  182. },
  183. results: function (data, page) { // parse the results into the format expected by Select2.
  184. // since we are using custom formatting functions we do not need to alter remote JSON data
  185. return {
  186. results: data.movies
  187. };
  188. }
  189. },
  190. initSelection: function (element, callback) {
  191. // the input tag has a value attribute preloaded that points to a preselected movie's id
  192. // this function resolves that id attribute to an object that select2 can render
  193. // using its formatResult renderer - that way the movie name is shown preselected
  194. var id = $(element).val();
  195. if (id !== "") {
  196. $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
  197. data: {
  198. apikey: "ju6z9mjyajq2djue3gbvv26t"
  199. },
  200. dataType: "jsonp"
  201. }).done(function (data) {
  202. callback(data);
  203. });
  204. }
  205. },
  206. formatResult: movieFormatResult, // omitted for brevity, see the source of this page
  207. formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
  208. dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
  209. escapeMarkup: function (m) {
  210. return m;
  211. } // we do not want to escape markup since we are displaying html in results
  212. });
  213. }
  214. var handleBootstrapSelect = function() {
  215. $('.bs-select').selectpicker({
  216. iconBase: 'fa',
  217. tickIcon: 'fa-check'
  218. });
  219. }
  220. var handleMultiSelect = function () {
  221. $('#my_multi_select1').multiSelect();
  222. $('#my_multi_select2').multiSelect({
  223. selectableOptgroup: true
  224. });
  225. }
  226. return {
  227. //main function to initiate the module
  228. init: function () {
  229. handleSelect2();
  230. handleSelect2Modal();
  231. handleMultiSelect();
  232. handleBootstrapSelect();
  233. }
  234. };
  235. }();
  236. jQuery(document).ready(function() {
  237. ComponentsDropdowns.init();
  238. });