themeswitchertool.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* jQuery plugin themeswitcher
  2. ---------------------------------------------------------------------*/
  3. $.fn.themeswitcher = function( settings ) {
  4. var options = $.extend({
  5. loadTheme: null,
  6. initialText: 'Choose Theme',
  7. width: 150,
  8. height: 200,
  9. buttonPreText: 'Theme: ',
  10. closeOnSelect: true,
  11. buttonHeight: 14,
  12. cookieName: 'jquery-ui-theme',
  13. onOpen: function(){},
  14. onClose: function(){},
  15. onSelect: function(){}
  16. }, settings );
  17. // markup
  18. var button = $([
  19. '<a href="#" class="jquery-ui-themeswitcher-trigger">'
  20. , '<span class="jquery-ui-themeswitcher-icon"></span>'
  21. , '<span class="jquery-ui-themeswitcher-title">'
  22. , options.initialText
  23. , '</span>'
  24. , '</a>'
  25. ].join('') );
  26. // TODO: download all the preview images and host locally
  27. var switcherpane = $([
  28. '<div class="jquery-ui-themeswitcher"><div id="themeGallery"><ul>'
  29. , '<li><a href="/lib/css/themes/ui-lightness/jquery-ui.css">'
  30. , '<img src="/lib/css/themeswitcher/theme_30_ui_light.png" alt="UI Lightness" title="UI Lightness" />'
  31. , '<span class="themeName">UI lightness</span>'
  32. , '</a></li>'
  33. , '<li><a href="/lib/css/themes/ui-darkness/jquery-ui.css">'
  34. , '<img src="/lib/css/themeswitcher/theme_30_ui_dark.png" alt="UI Darkness" title="UI Darkness" />'
  35. , '<span class="themeName">UI darkness</span>'
  36. , '</a></li>'
  37. , '<li><a href="/lib/css/themes/smoothness/jquery-ui.css">'
  38. , '<img src="/lib/css/themeswitcher/theme_30_smoothness.png" alt="Smoothness" title="Smoothness" />'
  39. , '<span class="themeName">Smoothness</span>'
  40. , '</a></li>'
  41. , '<li><a href="/lib/css/themes/start/jquery-ui.css">'
  42. , '<img src="/lib/css/themeswitcher/theme_30_start_menu.png" alt="Start" title="Start" />'
  43. , '<span class="themeName">Start</span>'
  44. , '</a></li>'
  45. , '<li><a href="/lib/css/themes/redmond/jquery-ui.css">'
  46. , '<img src="/lib/css/themeswitcher/theme_30_windoze.png" alt="Redmond" title="Redmond" />'
  47. , '<span class="themeName">Redmond</span>'
  48. , '</a></li>'
  49. , '<li><a href="/lib/css/themes/sunny/jquery-ui.css">'
  50. , '<img src="/lib/css/themeswitcher/theme_30_sunny.png" alt="Sunny" title="Sunny" />'
  51. , '<span class="themeName">Sunny</span>'
  52. , '</a></li>'
  53. , '<li><a href="/lib/css/themes/overcast/jquery-ui.css">'
  54. , '<img src="/lib/css/themeswitcher/theme_30_overcast.png" alt="Overcast" title="Overcast" />'
  55. , '<span class="themeName">Overcast</span>'
  56. , '</a></li>'
  57. , '<li><a href="/lib/css/themes/le-frog/jquery-ui.css">'
  58. , '<img src="/lib/css/themeswitcher/theme_30_le_frog.png" alt="Le Frog" title="Le Frog" />'
  59. , '<span class="themeName">Le Frog</span>'
  60. , '</a></li>'
  61. , '<li><a href="/lib/css/themes/flick/jquery-ui.css">'
  62. , '<img src="/lib/css/themeswitcher/theme_30_flick.png" alt="Flick" title="Flick" />'
  63. , '<span class="themeName">Flick</span>'
  64. , '</a></li>'
  65. , '<li><a href="/lib/css/themes/pepper-grinder/jquery-ui.css">'
  66. , '<img src="/lib/css/themeswitcher/theme_30_pepper_grinder.png" alt="Pepper Grinder" title="Pepper Grinder" />'
  67. , '<span class="themeName">Pepper Grinder</span>'
  68. , '</a></li>'
  69. , '<li><a href="/lib/css/themes/eggplant/jquery-ui.css">'
  70. , '<img src="/lib/css/themeswitcher/theme_30_eggplant.png" alt="Eggplant" title="Eggplant" />'
  71. , '<span class="themeName">Eggplant</span>'
  72. , '</a></li>'
  73. , '<li><a href="/lib/css/themes/dark-hive/jquery-ui.css">'
  74. , '<img src="/lib/css/themeswitcher/theme_30_dark_hive.png" alt="Dark Hive" title="Dark Hive" />'
  75. , '<span class="themeName">Dark Hive</span>'
  76. , '</a></li>'
  77. , '<li><a href="/lib/css/themes/cupertino/jquery-ui.css">'
  78. , '<img src="/lib/css/themeswitcher/theme_30_cupertino.png" alt="Cupertino" title="Cupertino" />'
  79. , '<span class="themeName">Cupertino</span>'
  80. , '</a></li>'
  81. , '<li><a href="/lib/css/themes/south-street/jquery-ui.css">'
  82. , '<img src="/lib/css/themeswitcher/theme_30_south_street.png" alt="South St" title="South St" />'
  83. , '<span class="themeName">South Street</span>'
  84. , '</a></li>'
  85. , '<li><a href="/lib/css/themes/blitzer/jquery-ui.css">'
  86. , '<img src="/lib/css/themeswitcher/theme_30_blitzer.png" alt="Blitzer" title="Blitzer" />'
  87. , '<span class="themeName">Blitzer</span>'
  88. , '</a></li>'
  89. , '<li><a href="/lib/css/themes/humanity/jquery-ui.css">'
  90. , '<img src="/lib/css/themeswitcher/theme_30_humanity.png" alt="Humanity" title="Humanity" />'
  91. , '<span class="themeName">Humanity</span>'
  92. , '</a></li>'
  93. , '<li><a href="/lib/css/themes/hot-sneaks/jquery-ui.css">'
  94. , '<img src="/lib/css/themeswitcher/theme_30_hot_sneaks.png" alt="Hot Sneaks" title="Hot Sneaks" />'
  95. , '<span class="themeName">Hot sneaks</span>'
  96. , '</a></li>'
  97. , '<li><a href="/lib/css/themes/excite-bike/jquery-ui.css">'
  98. , '<img src="/lib/css/themeswitcher/theme_30_excite_bike.png" alt="Excite Bike" title="Excite Bike" />'
  99. , '<span class="themeName">Excite Bike</span>'
  100. , '</a></li>'
  101. , '<li><a href="/lib/css/themes/vader/jquery-ui.css">'
  102. , '<img src="/lib/css/themeswitcher/theme_30_black_matte.png" alt="Vader" title="Vader" />'
  103. , '<span class="themeName">Vader</span>'
  104. , '</a></li>'
  105. , '<li><a href="/lib/css/themes/dot-luv/jquery-ui.css">'
  106. , '<img src="/lib/css/themeswitcher/theme_30_dot_luv.png" alt="Dot Luv" title="Dot Luv" />'
  107. , '<span class="themeName">Dot Luv</span>'
  108. , '</a></li>'
  109. , '<li><a href="/lib/css/themes/mint-choc/jquery-ui.css">'
  110. , '<img src="/lib/css/themeswitcher/theme_30_mint_choco.png" alt="Mint Choc" title="Mint Choc" />'
  111. , '<span class="themeName">Mint Choc</span>'
  112. , '</a></li>'
  113. , '<li><a href="/lib/css/themes/black-tie/jquery-ui.css">'
  114. , '<img src="/lib/css/themeswitcher/theme_30_black_tie.png" alt="Black Tie" title="Black Tie" />'
  115. , '<span class="themeName">Black Tie</span>'
  116. , '</a></li>'
  117. , '<li><a href="/lib/css/themes/trontastic/jquery-ui.css">'
  118. , '<img src="/lib/css/themeswitcher/theme_30_trontastic.png" alt="Trontastic" title="Trontastic" />'
  119. , '<span class="themeName">Trontastic</span>'
  120. , '</a></li>'
  121. , '<li><a href="/lib/css/themes/swanky-purse/jquery-ui.css">'
  122. , '<img src="/lib/css/themeswitcher/theme_30_swanky_purse.png" alt="Swanky Purse" title="Swanky Purse" />'
  123. , '<span class="themeName">Swanky Purse</span>'
  124. , '</a></li>'
  125. , '</ul></div></div>'
  126. ].join('') ).find('div').removeAttr('id');
  127. //button events
  128. button.click(function(){
  129. if ( switcherpane.is(':visible') )
  130. switcherpane.spHide();
  131. else
  132. switcherpane.spShow();
  133. return false;
  134. });
  135. //menu events (mouseout didn't work...)
  136. switcherpane.hover(
  137. $.noop,
  138. function(){ if(switcherpane.is(':visible') ){ $(this).spHide(); } }
  139. );
  140. //show/hide panel functions
  141. $.fn.spShow = function(){
  142. var offset = button.offset();
  143. $(this)
  144. .css({
  145. top: offset.top + options.buttonHeight + 6
  146. , left: offset.left
  147. })
  148. .slideDown(50);
  149. button.css( button_active );
  150. options.onOpen();
  151. }
  152. $.fn.spHide = function(){
  153. $(this).slideUp( 50, function(){ options.onClose(); });
  154. button.css( button_default );
  155. }
  156. /* Theme Loading
  157. ---------------------------------------------------------------------*/
  158. switcherpane.find('a').click(function(){
  159. updateCSS( $(this).attr('href') );
  160. var themeName = $(this).find('span').text();
  161. button.find('.jquery-ui-themeswitcher-title').text( options.buttonPreText + themeName );
  162. $.cookie( options.cookieName, themeName );
  163. options.onSelect();
  164. if (options.closeOnSelect && switcherpane.is(':visible')){ switcherpane.spHide(); }
  165. return false;
  166. });
  167. //function to append a new theme stylesheet with the new style changes
  168. function updateCSS(locStr){
  169. var cssLink = $('<link href="'+locStr+'" type="text/css" rel="Stylesheet" class="ui-theme" />');
  170. $("head").append(cssLink);
  171. if ( $("link.ui-theme").size() > 3 )
  172. $("link.ui-theme:first").remove();
  173. }
  174. /* Inline CSS
  175. ---------------------------------------------------------------------*/
  176. var button_default = {
  177. fontFamily: 'Trebuchet MS, Verdana, sans-serif',
  178. fontSize: '11px',
  179. color: '#666',
  180. background: '#eee url(/lib/css/themeswitcher/buttonbg.png) 50% 50% repeat-x',
  181. border: '1px solid #ccc',
  182. '-moz-border-radius': '6px',
  183. '-webkit-border-radius': '6px',
  184. textDecoration: 'none',
  185. padding: '3px 3px 3px 8px',
  186. width: options.width - 11,//minus must match left and right padding
  187. display: 'block',
  188. height: options.buttonHeight,
  189. outline: '0'
  190. };
  191. var button_hover = {
  192. 'borderColor':'#bbb',
  193. 'background': '#f0f0f0',
  194. cursor: 'pointer',
  195. color: '#444'
  196. };
  197. var button_active = {
  198. color: '#aaa',
  199. background: '#000',
  200. border: '1px solid #ccc',
  201. borderBottom: 0,
  202. '-moz-border-radius-bottomleft': 0,
  203. '-webkit-border-bottom-left-radius': 0,
  204. '-moz-border-radius-bottomright': 0,
  205. '-webkit-border-bottom-right-radius': 0,
  206. outline: '0'
  207. };
  208. //button css
  209. button.css(button_default)
  210. .hover(
  211. function(){
  212. $(this).css(button_hover);
  213. },
  214. function(){
  215. if( !switcherpane.is(':animated') && switcherpane.is(':hidden') ){ $(this).css(button_default); }
  216. }
  217. )
  218. .find('.jquery-ui-themeswitcher-icon').css({
  219. float: 'right',
  220. width: '16px',
  221. height: '16px',
  222. background: 'url(/lib/css/themeswitcher/icon_color_arrow.gif) 50% 50% no-repeat'
  223. });
  224. //pane css
  225. switcherpane
  226. .css({
  227. position: 'absolute',
  228. float: 'left',
  229. fontFamily: 'Trebuchet MS, Verdana, sans-serif',
  230. fontSize: '12px',
  231. background: '#000',
  232. color: '#fff',
  233. padding: '8px 3px 3px',
  234. border: '1px solid #ccc',
  235. '-moz-border-radius-bottomleft': '6px',
  236. '-webkit-border-bottom-left-radius': '6px',
  237. '-moz-border-radius-bottomright': '6px',
  238. '-webkit-border-bottom-right-radius': '6px',
  239. borderTop: 0,
  240. zIndex: 999999,
  241. width: options.width-6//minus must match left and right padding
  242. })
  243. .find('ul').css({
  244. listStyle: 'none',
  245. margin: '0',
  246. padding: '0',
  247. overflow: 'auto',
  248. overflowX: 'hidden', // NEW
  249. height: options.height
  250. })
  251. .end()
  252. .find('li')
  253. .hover(
  254. function(){
  255. $(this).css({
  256. 'borderColor':'#555',
  257. 'background': 'url(/lib/css/themeswitcher/menuhoverbg.png) 50% 50% repeat-x',
  258. cursor: 'pointer'
  259. });
  260. },
  261. function(){
  262. $(this).css({
  263. 'borderColor':'#111',
  264. 'background': '#000',
  265. cursor: 'auto'
  266. });
  267. }
  268. )
  269. .css({
  270. width: options.width-30,
  271. height: '',
  272. padding: '2px',
  273. margin: '1px',
  274. border: '1px solid #111',
  275. '-moz-border-radius': '4px',
  276. clear: 'left',
  277. float: 'left'
  278. })
  279. .end()
  280. .find('a').css({
  281. color: '#aaa',
  282. textDecoration: 'none',
  283. float: 'left',
  284. width: '100%',
  285. outline: '0'
  286. })
  287. .end()
  288. .find('img').css({
  289. float: 'left',
  290. border: '1px solid #333',
  291. margin: '0 2px'
  292. })
  293. .end()
  294. .find('.themeName').css({
  295. float: 'left',
  296. margin: '3px 0'
  297. })
  298. .end()
  299. ;
  300. $(this).append( button );
  301. $('body').append( switcherpane );
  302. switcherpane.hide();
  303. if ( $.cookie(options.cookieName) || options.loadTheme ) {
  304. var themeName = $.cookie(options.cookieName) || options.loadTheme;
  305. switcherpane.find('a:contains('+ themeName +')').trigger('click');
  306. }
  307. return this;
  308. };
  309. /**
  310. * Cookie plugin
  311. *
  312. * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  313. * Dual licensed under the MIT and GPL licenses:
  314. * http://www.opensource.org/licenses/mit-license.php
  315. * http://www.gnu.org/licenses/gpl.html
  316. */
  317. $.cookie = $.cookie || function(name, value, options) {
  318. if (typeof value != 'undefined') { // name and value given, set cookie
  319. options = options || {};
  320. if (value === null) {
  321. value = '';
  322. options.expires = -1;
  323. }
  324. var expires = '';
  325. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  326. var date;
  327. if (typeof options.expires == 'number') {
  328. date = new Date();
  329. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  330. } else {
  331. date = options.expires;
  332. }
  333. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  334. }
  335. // CAUTION: Needed to parenthesize options.path and options.domain
  336. // in the following expressions, otherwise they evaluate to undefined
  337. // in the packed version for some reason...
  338. var path = options.path ? '; path=' + (options.path) : '';
  339. var domain = options.domain ? '; domain=' + (options.domain) : '';
  340. var secure = options.secure ? '; secure' : '';
  341. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  342. } else { // only name given, get cookie
  343. var cookieValue = null;
  344. if (document.cookie && document.cookie != '') {
  345. var cookies = document.cookie.split(';');
  346. for (var i = 0; i < cookies.length; i++) {
  347. var cookie = jQuery.trim(cookies[i]);
  348. // Does this cookie string begin with the name we want?
  349. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  350. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  351. break;
  352. }
  353. }
  354. }
  355. return cookieValue;
  356. }
  357. };
  358. /**
  359. * addThemeSwitcher
  360. *
  361. * Add themeswitcher tool inside a generated, positioned placeholder
  362. *
  363. * Dependancies: /lib/js/themeswitchertool.js
  364. */
  365. function addThemeSwitcher ( container, position ) {
  366. var pos = { top: '10px', right: '10px', zIndex: 10 };
  367. $('<div id="themeContainer" style="position: absolute; overflow-x: hidden;"></div>')
  368. .css( $.extend( pos, position ) )
  369. .appendTo( container || 'body' )
  370. .themeswitcher()
  371. ;
  372. };
  373. /**
  374. * removeUITheme
  375. *
  376. * Remove the cookie set by the UI Themeswitcher to reset a page to default styles
  377. */
  378. function removeUITheme ( cookieName, removeCookie ) {
  379. $('link.ui-theme').remove();
  380. $('.jquery-ui-themeswitcher-title').text( 'Choose Theme' );
  381. if (removeCookie !== false)
  382. $.cookie( cookieName || 'jquery-ui-theme', null );
  383. };