dropdown.js 7.3 KB

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