dropdown.js 7.2 KB

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