dropdown.js 6.8 KB

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