awselect.js 13 KB

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