awselect.js 8.6 KB

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