PageRenderTime 28ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/freemix/exhibit/static/exhibit/js/lib/jquery.themeswitcher.js

https://github.com/pombredanne/freemix
JavaScript | 192 lines | 166 code | 13 blank | 13 comment | 6 complexity | 8c2e580a15827e762c04bb59854e71b7 MD5 | raw file
  1. /* jQuery plugin themeswitcher -- Customized from code at http://jqueryui.com/themeroller/themeswitchertool/
  2. * The target div should be pre-populated with the available themes via the
  3. * theme_list template tag
  4. ---------------------------------------------------------------------*/
  5. $.fn.themeswitcher = function(settings){
  6. var options = jQuery.extend({
  7. loadTheme: null,
  8. initialText: 'Switch Theme',
  9. width: 150,
  10. height: 200,
  11. buttonPreText: 'Theme: ',
  12. closeOnSelect: true,
  13. buttonHeight: 14,
  14. cookieName: 'jquery-ui-theme',
  15. themes: {},
  16. onOpen: function(){},
  17. onClose: function(){},
  18. onSelect: function(){}
  19. }, settings);
  20. var root = $(this);
  21. //markup
  22. var button = $('<a href="#" class="jquery-ui-themeswitcher-trigger"><span class="jquery-ui-themeswitcher-icon"></span><span class="jquery-ui-themeswitcher-title">'+ options.initialText +'</span></a>');
  23. var switcherpane = $('<div class="jquery-ui-themeswitcher"><div id="themeGallery"></div></div>');
  24. var ul = $(this).find("ul");
  25. switcherpane.find("#themeGallery").append(ul);
  26. ul.find("a").click(function() {
  27. updateCSS( $(this).attr('href') );
  28. button.find('.jquery-ui-themeswitcher-title').text( options.buttonPreText + $(this).find("img").attr("title") );
  29. options.onSelect($(this).parent("li").attr("id"));
  30. if(options.closeOnSelect && switcherpane.is(':visible')){ switcherpane.spHide(); }
  31. return false;
  32. });
  33. //button events
  34. button.click(
  35. function(){
  36. if(switcherpane.is(':visible')){ switcherpane.spHide(); }
  37. else{ switcherpane.spShow(); }
  38. return false;
  39. }
  40. );
  41. //menu events (mouseout didn't work...)
  42. switcherpane.hover(
  43. function(){},
  44. function(){if(switcherpane.is(':visible')){$(this).spHide();}}
  45. );
  46. //show/hide panel functions
  47. $.fn.spShow = function(){ $(this).css({top: button.offset().top + options.buttonHeight + 6, left: button.offset().left}).slideDown(50); button.css(button_active); options.onOpen(); };
  48. $.fn.spHide = function(){ $(this).slideUp(50, function(){options.onClose();}); button.css(button_default); };
  49. //function to append a new theme stylesheet with the new style changes
  50. function updateCSS(locStr){
  51. $('link.ui-theme').remove();
  52. var cssLink = $('<link href="'+locStr+'" type="text/css" rel="stylesheet" class="ui-theme" />');
  53. $('head').append(cssLink.clone());
  54. }
  55. /* Inline CSS
  56. ---------------------------------------------------------------------*/
  57. var button_default = {
  58. fontFamily: 'Trebuchet MS, Verdana, sans-serif',
  59. fontSize: '11px',
  60. color: '#666',
  61. background: '#eee url(http://jqueryui.com/themeroller/themeswitchertool/images/buttonbg.png) 50% 50% repeat-x',
  62. border: '1px solid #ccc',
  63. '-moz-border-radius': '6px',
  64. '-webkit-border-radius': '6px',
  65. textDecoration: 'none',
  66. padding: '3px 3px 3px 8px',
  67. width: options.width - 11,//minus must match left and right padding
  68. display: 'block',
  69. height: options.buttonHeight,
  70. outline: '0'
  71. };
  72. var button_hover = {
  73. 'borderColor':'#bbb',
  74. 'background': '#f0f0f0',
  75. cursor: 'pointer',
  76. color: '#444'
  77. };
  78. var button_active = {
  79. color: '#aaa',
  80. background: '#000',
  81. border: '1px solid #ccc',
  82. borderBottom: 0,
  83. '-moz-border-radius-bottomleft': 0,
  84. '-webkit-border-bottom-left-radius': 0,
  85. '-moz-border-radius-bottomright': 0,
  86. '-webkit-border-bottom-right-radius': 0,
  87. outline: '0'
  88. };
  89. //button css
  90. button.css(button_default)
  91. .hover(
  92. function(){
  93. $(this).css(button_hover);
  94. },
  95. function(){
  96. if( !switcherpane.is(':animated') && switcherpane.is(':hidden') ){ $(this).css(button_default); }
  97. }
  98. )
  99. .find('.jquery-ui-themeswitcher-icon').css({
  100. 'float': 'right',
  101. width: '16px',
  102. height: '16px',
  103. background: 'url(http://jqueryui.com/themeroller/themeswitchertool/images/icon_color_arrow.gif) 50% 50% no-repeat'
  104. });
  105. //pane css
  106. switcherpane.css({
  107. position: 'absolute',
  108. 'float': 'left',
  109. fontFamily: 'Trebuchet MS, Verdana, sans-serif',
  110. fontSize: '12px',
  111. background: '#000',
  112. color: '#fff',
  113. padding: '8px 3px 3px',
  114. border: '1px solid #ccc',
  115. '-moz-border-radius-bottomleft': '6px',
  116. '-webkit-border-bottom-left-radius': '6px',
  117. '-moz-border-radius-bottomright': '6px',
  118. '-webkit-border-bottom-right-radius': '6px',
  119. borderTop: 0,
  120. zIndex: 999999,
  121. width: options.width-6//minus must match left and right padding
  122. })
  123. .find('ul').css({
  124. listStyle: 'none',
  125. margin: '0',
  126. padding: '0',
  127. overflow: 'auto',
  128. height: options.height
  129. }).end()
  130. .find('li').hover(
  131. function(){
  132. $(this).css({
  133. 'borderColor':'#555',
  134. 'background': 'url(http://jqueryui.com/themeroller/themeswitchertool/images/menuhoverbg.png) 50% 50% repeat-x',
  135. cursor: 'pointer'
  136. });
  137. },
  138. function(){
  139. $(this).css({
  140. 'borderColor':'#111',
  141. 'background': '#000',
  142. cursor: 'auto'
  143. });
  144. }
  145. ).css({
  146. width: options.width-30,
  147. height: '',
  148. padding: '2px',
  149. margin: '1px',
  150. border: '1px solid #111',
  151. '-moz-border-radius': '4px',
  152. clear: 'left',
  153. 'float': 'left'
  154. }).end()
  155. .find('a').css({
  156. color: '#aaa',
  157. textDecoration: 'none',
  158. 'float': 'left',
  159. width: '100%',
  160. outline: '0'
  161. }).end()
  162. .find('img').css({
  163. 'float': 'left',
  164. border: '1px solid #333',
  165. margin: '0 2px'
  166. }).end()
  167. .find('.themeName').css({
  168. 'float': 'left',
  169. margin: '3px 0'
  170. }).end();
  171. $(this).append(button);
  172. $('body').append(switcherpane);
  173. switcherpane.hide();
  174. if(options.loadTheme ){
  175. var themeName = options.loadTheme;
  176. switcherpane.find('li[id="' + themeName + '"] a').trigger('click');
  177. }
  178. return this;
  179. };