jquery.jqtransform.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. *
  3. * jqTransform
  4. * by mathieu vilaplana mvilaplana@dfc-e.com
  5. * Designer ghyslain armand garmand@dfc-e.com
  6. *
  7. *
  8. * Version 1.0 25.09.08
  9. * Version 1.1 06.08.09
  10. * Add event click on Checkbox and Radio
  11. * Auto calculate the size of a select element
  12. * Can now, disabled the elements
  13. * Correct bug in ff if click on select (overflow=hidden)
  14. * No need any more preloading !!
  15. *
  16. ******************************************** */
  17. (function($){
  18. var defaultOptions = {preloadImg:true};
  19. var jqTransformImgPreloaded = false;
  20. /***************************
  21. Labels
  22. ***************************/
  23. var jqTransformGetLabel = function(objfield){
  24. var selfForm = $(objfield.get(0).form);
  25. var oLabel = objfield.next();
  26. if(!oLabel.is('label')) {
  27. oLabel = objfield.prev();
  28. if(oLabel.is('label')){
  29. var inputname = objfield.attr('id');
  30. if(inputname){
  31. oLabel = selfForm.find('label[for="'+inputname+'"]');
  32. }
  33. }
  34. }
  35. if(oLabel.is('label')){return oLabel.css('cursor','pointer');}
  36. return false;
  37. };
  38. /* Hide all open selects */
  39. var jqTransformHideSelect = function(oTarget){
  40. var ulVisible = $('.jq_sel ul:visible');
  41. ulVisible.each(function(){
  42. var oSelect = $(this).parents(".jq_sel:first").find("select").get(0);
  43. //do not hide if click on the label object associated to the select
  44. if( !(oTarget && oSelect.oLabel && oSelect.oLabel.get(0) == oTarget.get(0)) ){$(this).hide();$(this).parent().css({zIndex: '20'});}
  45. });
  46. };
  47. /* Check for an external click */
  48. var jqTransformCheckExternalClick = function(event) {
  49. if ($(event.target).parents('.jq_sel').length === 0) { jqTransformHideSelect($(event.target));};
  50. };
  51. /* Apply document listener */
  52. var jqTransformAddDocumentListener = function (){
  53. $(document).mousedown(jqTransformCheckExternalClick);
  54. };
  55. /* Add a new handler for the reset action */
  56. var jqTransformReset = function(f){
  57. var sel;
  58. $('.jq_sel select', f).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; $('ul', $(this).parent()).each(function(){$('a:eq('+ sel +')', this).click();});});
  59. };
  60. /***************************
  61. Select
  62. ***************************/
  63. $.fn.jqTransSelect = function(){
  64. return this.each(function(index){
  65. var $select = $(this);
  66. if($select.hasClass('jq_sel_hide')) {return;}
  67. if($select.attr('multiple')) {return;}
  68. var oLabel = jqTransformGetLabel($select);
  69. /* First thing we do is Wrap it */
  70. var $wrapper = $select
  71. .addClass('jq_sel_hide')
  72. .wrap('<div class="jq_sel"></div>')
  73. .parent()
  74. .css({zIndex: 20-index})
  75. ;
  76. /* Now add the html for the select */
  77. $wrapper.prepend('<div><span></span><a href="#" class="jqTransformSelectOpen"></a></div><ul></ul>');
  78. var $ul = $('ul', $wrapper).hide();
  79. /* Now we add the options */
  80. $('option', this).each(function(i){
  81. var oLi = $('<li><a href="#" index="'+ i +'">'+ $(this).html() +'</a></li>');
  82. $ul.append(oLi);
  83. });
  84. /* Add click handler to the a */
  85. $ul.find('a').click(function(){
  86. $('a.selected', $wrapper).removeClass('selected');
  87. $(this).addClass('selected');
  88. /* Fire the onchange event */
  89. if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) { $select[0].selectedIndex = $(this).attr('index'); $select[0].onchange(); }
  90. $select[0].selectedIndex = $(this).attr('index');
  91. $('span:eq(0)', $wrapper).html($(this).html());
  92. $ul.hide();
  93. $wrapper.css({zIndex: '20'})
  94. return false;
  95. });
  96. /* Set the default */
  97. $('a:eq('+ this.selectedIndex +')', $ul).click();
  98. $('span:first', $wrapper).click(function(){$("a.jqTransformSelectOpen",$wrapper).trigger('click');});
  99. oLabel && oLabel.click(function(){$("a.jqTransformSelectOpen",$wrapper).trigger('click');});
  100. this.oLabel = oLabel;
  101. /* Apply the click handler to the Open */
  102. var oLinkOpen = $('a.jqTransformSelectOpen', $wrapper)
  103. .click(function(){
  104. //Check if box is already open to still allow toggle, but close all other selects
  105. if( $ul.css('display') == 'none' ) {jqTransformHideSelect();$wrapper.css({zIndex: '50'});}
  106. if($select.attr('disabled')){
  107. return false;
  108. $wrapper.css({zIndex: '20'});
  109. }
  110. $ul.slideToggle('fast', function(){
  111. var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
  112. $ul.animate({scrollTop: offSet});
  113. if( $ul.css('display') == 'none' ) {$wrapper.css({zIndex: '20'});}
  114. });
  115. return false;
  116. })
  117. ;
  118. // Set the new width
  119. var iSelectWidth = $select.outerWidth();
  120. var oSpan = $('span:first',$wrapper);
  121. var newWidth = (iSelectWidth > oSpan.innerWidth())?iSelectWidth+oLinkOpen.outerWidth():$wrapper.width();
  122. $wrapper.parents(".comm_sel").css('width',newWidth);
  123. //$ul.css('width',newWidth);
  124. //$ul.css('width',newWidth-2);
  125. // Calculate the height if necessary, less elements that the default height
  126. //show the ul to calculate the block, if ul is not displayed li height value is 0
  127. $ul.css({display:'block',visibility:'hidden'});
  128. var iSelectHeight = ($('li',$ul).length)*($('li:first',$ul).height());//+1 else bug ff
  129. (iSelectHeight < $ul.height()) && $ul.css({height:iSelectHeight,'overflow':'hidden'});//hidden else bug with ff
  130. $ul.css({display:'none',visibility:'visible'});
  131. });
  132. };
  133. $.fn.jqTransform = function(options){
  134. var opt = $.extend({},defaultOptions,options);
  135. /* each form */
  136. return this.each(function(){
  137. var selfForm = $(this);
  138. if(selfForm.hasClass('jqtransformdone')) {return;}
  139. selfForm.addClass('jqtransformdone');
  140. if( $('select', this).jqTransSelect().length > 0 ){jqTransformAddDocumentListener();}
  141. selfForm.bind('reset',function(){var action = function(){jqTransformReset(this);}; window.setTimeout(action, 10);});
  142. }); /* End Form each */
  143. };/* End the Plugin */
  144. $.fn.jqTransSelectRefresh = function(){
  145. return this.each(function(index){
  146. var $select = $(this);
  147. var i=$select.parent().find('div,ul').remove().css('zIndex');
  148. $select.unwrap().removeClass('jq_sel_hide').jqTransSelect();
  149. $select.parent().css('zIndex', i);
  150. });
  151. };
  152. })(jQuery);