awselect.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. var awselect_count = 0; // used for generating sequential ID for <select> that does not have ID
  8. var mobile_width = 800;
  9. (function($) {
  10. $(document).mouseup(function(e) {
  11. var awselect = $(".awselect");
  12. if (!awselect.is(e.target) && awselect.has(e.target).length === 0)
  13. {
  14. deanimate();
  15. }
  16. });
  17. $.fn.awselect = function(options) {
  18. var element = $(this);
  19. var opts = $.extend({}, $.fn.awselect.defaults, options);
  20. element.each(function() {
  21. awselect_count += 1;
  22. build($(this), opts);
  23. });
  24. this.on("aw:animate", function() {
  25. animate(getawselectElement($(this)));
  26. });
  27. this.on("change", function() {
  28. setValue(this);
  29. });
  30. this.on("aw:deanimate", function() {
  31. deanimate(getawselectElement($(this)))
  32. });
  33. console.log(element.attr("id"));
  34. return {
  35. blue: function() {
  36. element.css("color", "blue");
  37. }
  38. };
  39. };
  40. $.fn.awselect.defaults = {
  41. background: "#e5e5e5",
  42. active_background: "#fff",
  43. placeholder_color: "#000",
  44. placeholder_active_color: "#000",
  45. option_color: "#000",
  46. vertical_padding: "15px",
  47. horizontal_padding: "40px"
  48. };
  49. function getawselectElement(select) {
  50. return $('.awselect[data-select="' + select.attr("id") + '"]');
  51. }
  52. function build(element, opts) {
  53. var placeholder = element.attr("data-placeholder");
  54. var id = element.attr("id");
  55. var options = element.children("option");
  56. var selected = false;
  57. var classes = "awselect";
  58. var options_html = "";
  59. var background = opts["background"];
  60. var active_background = opts["active_background"];
  61. var placeholder_color = opts["placeholder_color"];
  62. var placeholder_active_color = opts["placeholder_active_color"];
  63. var option_color = opts["option_color"];
  64. var vertical_padding = opts["vertical_padding"];
  65. var horizontal_padding = opts["horizontal_padding"];
  66. options.each(function() {
  67. if (typeof $(this).attr("selected") !== typeof undefined && $(this).attr("selected") !== false) {
  68. selected = $(this).text();
  69. }
  70. options_html += '<li><a style="padding: 2px '+ horizontal_padding +'">' + $(this).text() + '</a></li>';
  71. });
  72. if (selected !== false) {
  73. classes += " hasValue";
  74. }
  75. if (typeof id !== typeof undefined && id !== false) {
  76. id_html = id;
  77. } else {
  78. id_html = "awselect_" + awselect_count;
  79. $(element).attr("id", id_html);
  80. }
  81. var awselect_html = '<div id="awselect_' + id_html + '" data-select="' + id_html + '" class = "' + classes + '"><div style="background:' + active_background + '" class = "bg"></div>';
  82. awselect_html += '<div style="padding:' + vertical_padding + " " + horizontal_padding + '" class = "front_face">';
  83. awselect_html += '<div style="background:' + background + '" class = "bg"></div>';
  84. awselect_html += '<div data-inactive-color="' + placeholder_active_color + '" style="color:' + placeholder_color + '" class = "content">';
  85. if (selected !== false) {
  86. awselect_html += '<span class="current_value">' + selected + "</span>";
  87. }
  88. awselect_html += '<span class = "placeholder">' + placeholder + "</span>";
  89. awselect_html += '<i class = "icon">' + icon(placeholder_color) + "</i>";
  90. awselect_html += "</div>";
  91. awselect_html += "</div>";
  92. awselect_html += '<div style="padding:' + vertical_padding + ' 0;" class = "back_face"><ul style="color:' + option_color + '">';
  93. awselect_html += options_html;
  94. awselect_html += "</ul></div>";
  95. awselect_html += "</div>";
  96. $(awselect_html).insertAfter(element);
  97. element.hide();
  98. }
  99. function animate(element) {
  100. if (element.hasClass("animating") == false) {
  101. element.addClass("animating");
  102. if ($(".awselect.animate").length > 0) {
  103. deanimate($(".awselect").not(element));
  104. var timeout = 600;
  105. } else {
  106. var timeout = 100;
  107. }
  108. if ($(window).width() < mobile_width ) {
  109. mobile_animate(element);
  110. timeout += 200
  111. }
  112. setTimeout(function() {
  113. var back_face = element.find(".back_face");
  114. back_face.show();
  115. var bg = element.find("> .bg");
  116. bg.css({
  117. height: element.outerHeight() + back_face.outerHeight()
  118. });
  119. back_face.css({
  120. "margin-top": $(element).outerHeight()
  121. });
  122. if ( $(window).width() < mobile_width ) {
  123. element.css({
  124. "top": parseInt(element.css('top')) - back_face.height()
  125. })
  126. }
  127. element.addClass("placeholder_animate");
  128. setTimeout(function() {
  129. switchPlaceholderColor(element);
  130. setTimeout(function(){
  131. if (back_face.outerHeight() == 200) {
  132. back_face.addClass("overflow");
  133. }
  134. }, 200);
  135. element.addClass("placeholder_animate2");
  136. element.addClass("animate");
  137. element.addClass("animate2");
  138. element.removeClass("animating");
  139. }, 100);
  140. }, timeout);
  141. }
  142. }
  143. function mobile_animate(element) {
  144. $(".awselect_bg").remove()
  145. $('body').prepend('<div class = "awselect_bg"></div>')
  146. setTimeout(function(){
  147. $('.awselect_bg').addClass('animate')
  148. }, 100)
  149. var current_width = element.outerWidth()
  150. var current_height = element.outerHeight()
  151. var current_left = element.offset().left
  152. var current_top = element.offset().top - $(window).scrollTop()
  153. element.attr('data-o-width', current_width)
  154. element.attr('data-o-left', current_left)
  155. element.attr('data-o-top', current_top)
  156. element.addClass('transition_paused').css({
  157. "width" : current_width,
  158. "z-index": "9999"
  159. })
  160. setTimeout(function(){
  161. $('<div class = "awselect_placebo" style="position:relative; width:'+ current_width +'px; height:'+ current_height +'px; float:left;ß"></div>').insertAfter(element)
  162. element.css({
  163. "position": "fixed",
  164. "top" : current_top,
  165. "left": current_left
  166. })
  167. element.removeClass('transition_paused')
  168. setTimeout(function(){
  169. element.css('width', $(window).outerWidth() - 40)
  170. element.css({
  171. "top" : $(window).outerHeight() / 2 + element.outerHeight() / 2,
  172. "left" : 20
  173. })
  174. setTimeout(function(){
  175. animate(element)
  176. }, 100)
  177. }, 100)
  178. }, 50)
  179. }
  180. function deanimate(awselects) {
  181. if (awselects == null) {
  182. var awselect = $(".awselect");
  183. } else {
  184. var awselect = awselects;
  185. }
  186. $(awselect).each(function() {
  187. var element = $(this);
  188. if (element.hasClass("animate")) {
  189. setTimeout(function() {
  190. }, 300);
  191. element.removeClass("animate2");
  192. element.find(".back_face").hide();
  193. element.find('.back_face').removeClass('overflow')
  194. element.removeClass("animate");
  195. switchPlaceholderColor(element);
  196. element.children(".bg").css({
  197. height: 0
  198. });
  199. element.removeClass("placeholder_animate2");
  200. setTimeout(function() {
  201. mobile_deanimate(element)
  202. element.removeClass("placeholder_animate");
  203. }, 100);
  204. }
  205. });
  206. }
  207. function mobile_deanimate(element){
  208. if ( element.siblings('.awselect_placebo').length > 0 ) {
  209. setTimeout(function(){
  210. var original_width = element.attr('data-o-width')
  211. var original_left = element.attr('data-o-left')
  212. var original_top = element.attr('data-o-top')
  213. element.css({
  214. "width" : original_width,
  215. "left" : original_left + "px",
  216. "top" : original_top + "px"
  217. })
  218. $('.awselect_bg').removeClass('animate')
  219. setTimeout(function(){
  220. $('.awselect_placebo').remove()
  221. setTimeout(function(){
  222. $('.awselect_bg').removeClass('animate').remove()
  223. }, 200);
  224. element.attr('style', '')
  225. }, 200)
  226. }, 100)
  227. }
  228. }
  229. function switchPlaceholderColor(element) {
  230. var placeholder_inactive_color = element.find(".front_face .content").attr("data-inactive-color");
  231. var placeholder_normal_color = element.find(".front_face .content").css("color");
  232. element.find(".front_face .content").attr("data-inactive-color", placeholder_normal_color);
  233. element.find(".front_face .content").css("color", placeholder_inactive_color);
  234. element.find(".front_face .icon svg").css("fill", placeholder_inactive_color);
  235. }
  236. function setValue(select) {
  237. var val = $(select).val();
  238. var awselect = getawselectElement($(select));
  239. var option_value = $(select).children('option[value="' + val + '"]').eq(0);
  240. var callback = $(select).attr("data-callback");
  241. $(awselect).find(".current_value").remove();
  242. $(awselect).find(".front_face .content").prepend('<span class = "current_value">' + option_value.text() + "</span>");
  243. $(awselect).addClass("hasValue");
  244. if (typeof callback !== typeof undefined && callback !== false) {
  245. window[callback](option_value.val());
  246. }
  247. setTimeout(function() {
  248. deanimate();
  249. }, 100);
  250. }
  251. function icon(color) {
  252. 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>';
  253. }
  254. function change(elem) {
  255. elem.css("color", "green");
  256. }
  257. })(jQuery);
  258. $(document).ready(function() {
  259. $("body").on("click", ".awselect .front_face", function() {
  260. var dropdown = $(this).parent('.awselect');
  261. if ( dropdown.hasClass("animate") == false) {
  262. $("select#" + dropdown.attr("id").replace("awselect_", "")).trigger("aw:animate");
  263. } else {
  264. $("select#" + dropdown.attr("id").replace("awselect_", "")).trigger("aw:deanimate");
  265. }
  266. });
  267. $("body").on("click", ".awselect ul li a", function() {
  268. var awselect = $(this).parents(".awselect");
  269. var value_index = $(this).parent("li").index();
  270. var id = awselect.attr("data-select");
  271. var select = $("select#" + id);
  272. var option_value = $(select).children("option").eq(value_index);
  273. var callback = $(select).attr("data-callback");
  274. $(select).val(option_value.val());
  275. $(select).trigger("change");
  276. });
  277. });