awselect.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. jQuery Awselect
  3. Developed by: Prev Wong
  4. Documentation: https://prevwong.github.io/awesome-select/
  5. Github: https://github.com/prevwong/awesome-select/
  6. **/
  7. awselect_count = 0; // used for generating sequential ID for <select> that does not have ID
  8. (function($) {
  9. $(document).mouseup(function(e) {
  10. var awselect = $(".awselect");
  11. if (!awselect.is(e.target) && awselect.has(e.target).length === 0)
  12. {
  13. deanimate();
  14. }
  15. });
  16. $.fn.awselect = function(options) {
  17. var element = $(this);
  18. var opts = $.extend({}, $.fn.awselect.defaults, options);
  19. element.each(function() {
  20. awselect_count += 1;
  21. build($(this), opts);
  22. });
  23. this.on("click", function() {
  24. animate(getawselectElement($(this)));
  25. });
  26. this.on("change", function() {
  27. setValue(this);
  28. });
  29. console.log(element.attr("id"));
  30. return {
  31. blue: function() {
  32. element.css("color", "blue");
  33. }
  34. };
  35. };
  36. $.fn.awselect.defaults = {
  37. background: "#e5e5e5",
  38. active_background: "#fff",
  39. placeholder_color: "#000",
  40. placeholder_active_color: "#000",
  41. option_color: "#000",
  42. vertical_padding: "15px",
  43. horizontal_padding: "40px"
  44. };
  45. function getawselectElement(select) {
  46. return $('.awselect[data-select="' + select.attr("id") + '"]');
  47. }
  48. function build(element, opts) {
  49. var placeholder = element.attr("data-placeholder");
  50. var id = element.attr("id");
  51. var options = element.children("option");
  52. var selected = false;
  53. var classes = "awselect";
  54. var options_html = "";
  55. var background = opts["background"];
  56. var active_background = opts["active_background"];
  57. var placeholder_color = opts["placeholder_color"];
  58. var placeholder_active_color = opts["placeholder_active_color"];
  59. var option_color = opts["option_color"];
  60. var vertical_padding = opts["vertical_padding"];
  61. var horizontal_padding = opts["horizontal_padding"];
  62. options.each(function() {
  63. if (typeof $(this).attr("selected") !== typeof undefined && $(this).attr("selected") !== false) {
  64. selected = $(this).text();
  65. }
  66. options_html += '<li><a style="padding: 2px '+ horizontal_padding +'">' + $(this).text() + '</a></li>';
  67. });
  68. if (selected !== false) {
  69. classes += " hasValue";
  70. }
  71. if (typeof id !== typeof undefined && id !== false) {
  72. id_html = id;
  73. } else {
  74. id_html = "awselect_" + awselect_count;
  75. $(element).attr("id", id_html);
  76. }
  77. var awselect_html = '<div id="euler_' + id_html + '" data-select="' + id_html + '" class = "' + classes + '"><div style="background:' + active_background + '" class = "bg"></div>';
  78. awselect_html += '<div style="padding:' + vertical_padding + " " + horizontal_padding + '" class = "front_face">';
  79. awselect_html += '<div style="background:' + background + '" class = "bg"></div>';
  80. awselect_html += '<div data-inactive-color="' + placeholder_active_color + '" style="color:' + placeholder_color + '" class = "content">';
  81. if (selected !== false) {
  82. awselect_html += '<span class="current_value">' + selected + "</span>";
  83. }
  84. awselect_html += '<span class = "placeholder">' + placeholder + "</span>";
  85. awselect_html += '<i class = "icon">' + icon(placeholder_color) + "</i>";
  86. awselect_html += "</div>";
  87. awselect_html += "</div>";
  88. awselect_html += '<div style="padding:' + vertical_padding + ' 0;" class = "back_face"><ul style="color:' + option_color + '">';
  89. awselect_html += options_html;
  90. awselect_html += "</ul></div>";
  91. awselect_html += "</div>";
  92. $(awselect_html).insertAfter(element);
  93. //$('#euler_' + id_html).css("height", $('#euler_' + id_html).outerHeight() - $('#euler_' + id_html).find('.back_face').outerHeight() )
  94. element.hide();
  95. }
  96. function animate(element) {
  97. if (element.hasClass("animating") == false) {
  98. element.addClass("animating");
  99. if ($(".awselect.animate").length > 0) {
  100. deanimate($(".awselect").not(element));
  101. var timeout = 600;
  102. } else {
  103. var timeout = 100;
  104. }
  105. setTimeout(function() {
  106. var back_face = element.find(".back_face");
  107. back_face.show();
  108. var bg = element.find("> .bg");
  109. bg.css({
  110. height: element.outerHeight() + back_face.outerHeight()
  111. });
  112. back_face.css({
  113. "margin-top": $(element).outerHeight()
  114. });
  115. element.addClass("placeholder_animate");
  116. setTimeout(function() {
  117. switchPlaceholderColor(element);
  118. if (back_face.outerHeight() == 200) {
  119. back_face.addClass("overflow");
  120. }
  121. element.addClass("placeholder_animate2");
  122. element.addClass("animate");
  123. element.addClass("animate2");
  124. element.removeClass("animating");
  125. }, 100);
  126. }, timeout);
  127. }
  128. }
  129. function deanimate(awselects) {
  130. if (awselects == null) {
  131. var awselect = $(".awselect");
  132. } else {
  133. var awselect = awselects;
  134. }
  135. $(awselect).each(function() {
  136. var element = $(this);
  137. if (element.hasClass("animate")) {
  138. //setTimeout(function() {
  139. element.removeClass("animate2");
  140. //setTimeout(function() {
  141. element.find(".back_face").hide();
  142. element.removeClass("animate");
  143. switchPlaceholderColor(element);
  144. element.children(".bg").css({
  145. height: 0
  146. });
  147. element.removeClass("placeholder_animate2");
  148. setTimeout(function() {
  149. element.removeClass("placeholder_animate");
  150. }, 100);
  151. //}, 0);
  152. //}, 0);
  153. }
  154. });
  155. }
  156. function switchPlaceholderColor(element) {
  157. var placeholder_inactive_color = element.find(".front_face .content").attr("data-inactive-color");
  158. var placeholder_normal_color = element.find(".front_face .content").css("color");
  159. element.find(".front_face .content").attr("data-inactive-color", placeholder_normal_color);
  160. element.find(".front_face .content").css("color", placeholder_inactive_color);
  161. element.find(".front_face .icon svg").css("fill", placeholder_inactive_color);
  162. }
  163. function setValue(select) {
  164. var val = $(select).val();
  165. var euler_awselect = getawselectElement($(select));
  166. var option_value = $(select).children('option[value="' + val + '"]').eq(0);
  167. var callback = $(select).attr("data-callback");
  168. $(euler_awselect).find(".current_value").remove();
  169. $(euler_awselect).find(".front_face .content").prepend('<span class = "current_value">' + option_value.text() + "</span>");
  170. $(euler_awselect).addClass("hasValue");
  171. if (typeof callback !== typeof undefined && callback !== false) {
  172. window[callback](option_value.val());
  173. }
  174. setTimeout(function() {
  175. deanimate();
  176. }, 100);
  177. }
  178. function icon(color) {
  179. return '<svg style="fill:' + color + '" version="1.1" id="Chevron_thin_down" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 20 20" enable-background="new 0 0 20 20" xml:space="preserve"><path d="M17.418,6.109c0.272-0.268,0.709-0.268,0.979,0c0.27,0.268,0.271,0.701,0,0.969l-7.908,7.83c-0.27,0.268-0.707,0.268-0.979,0l-7.908-7.83c-0.27-0.268-0.27-0.701,0-0.969c0.271-0.268,0.709-0.268,0.979,0L10,13.25L17.418,6.109z"/></svg>';
  180. }
  181. function change(elem) {
  182. elem.css("color", "green");
  183. }
  184. })(jQuery);
  185. $(document).ready(function() {
  186. $("body").on("click", ".awselect", function() {
  187. if ($(this).hasClass("animate") == false) {
  188. $("select#" + $(this).attr("id").replace("euler_", "")).trigger("click");
  189. }
  190. });
  191. $("body").on("click", ".awselect ul li a", function() {
  192. var awselect = $(this).parents(".awselect");
  193. var value_index = $(this).parent("li").index();
  194. var id = awselect.attr("data-select");
  195. var select = $("select#" + id);
  196. var option_value = $(select).children("option").eq(value_index);
  197. var callback = $(select).attr("data-callback");
  198. $(select).val(option_value.val());
  199. $(select).trigger("change");
  200. });
  201. });