jQuery.style.switcher.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. @author Cameron Manavian
  3. jQuery Style Switcher
  4. The MIT License (MIT)
  5. Copyright (c) 2014 Cameron Manavian
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. **/
  22. (function ($) {
  23. var jStyleSwitcher,
  24. _defaultOptions = {
  25. hasPreview: true,
  26. defaultThemeId: 'jssDefault',
  27. fullPath: 'css/',
  28. cookie: {
  29. expires: 30,
  30. isManagingLoad: true
  31. }
  32. },
  33. // private
  34. _cookieKey = 'jss_selected',
  35. _docCookies = {};
  36. /*\
  37. |*|
  38. |*| :: cookies.js ::
  39. |*|
  40. |*| A complete cookies reader/writer framework with full unicode support.
  41. |*|
  42. |*| revision #1
  43. |*|
  44. |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
  45. |*|
  46. |*| This framework is released under the GNU Public License, version 3 or later.
  47. |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
  48. |*|
  49. |*| Syntaxes:
  50. |*|
  51. |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
  52. |*| * docCookies.getItem(name)
  53. |*| * docCookies.removeItem(name[, path[, domain]])
  54. |*| * docCookies.hasItem(name)
  55. |*| * docCookies.keys()
  56. |*|
  57. \*/
  58. _docCookies = {
  59. getItem: function (sKey) {
  60. if (!sKey) {
  61. return null;
  62. }
  63. return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  64. },
  65. setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
  66. if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
  67. return false;
  68. }
  69. var sExpires = "";
  70. if (vEnd) {
  71. switch (vEnd.constructor) {
  72. case Number:
  73. sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
  74. break;
  75. case String:
  76. sExpires = "; expires=" + vEnd;
  77. break;
  78. case Date:
  79. sExpires = "; expires=" + vEnd.toUTCString();
  80. break;
  81. }
  82. }
  83. document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
  84. return true;
  85. },
  86. removeItem: function (sKey, sPath, sDomain) {
  87. if (!this.hasItem(sKey)) {
  88. return false;
  89. }
  90. document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
  91. return true;
  92. },
  93. hasItem: function (sKey) {
  94. if (!sKey) {
  95. return false;
  96. }
  97. return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  98. },
  99. keys: function () {
  100. var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
  101. for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
  102. aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
  103. }
  104. return aKeys;
  105. }
  106. };
  107. jStyleSwitcher = function ($root, config) {
  108. return this.init($root, config);
  109. };
  110. jStyleSwitcher.prototype = {
  111. /**
  112. * {Object} DOM reference to style option list
  113. */
  114. $root: null,
  115. /**
  116. * {Object} configs for the style switcher
  117. */
  118. config: {},
  119. /**
  120. * {Object} jQuery reference to <link> tag for swapping CSS
  121. */
  122. $themeCss: null,
  123. /**
  124. * {String} default theme page was loaded with
  125. */
  126. defaultTheme: null,
  127. init: function ($root, config) {
  128. this.$root = $root;
  129. this.config = config ? config : {};
  130. this.setDefaultTheme();
  131. if(this.defaultTheme) {
  132. // try cookies
  133. if (this.config.cookie) {
  134. this.checkCookie();
  135. }
  136. // try hover
  137. if (this.config.hasPreview) {
  138. this.addHoverEvents();
  139. }
  140. // finally, add click events
  141. this.addClickEvents();
  142. } else {
  143. this.$root.addClass('jssError error level0');
  144. }
  145. },
  146. setDefaultTheme: function () {
  147. this.$themeCss = $('link[id=' + this.config.defaultThemeId + ']');
  148. if(this.$themeCss.length) {
  149. this.defaultTheme = this.$themeCss.attr('href');
  150. }
  151. },
  152. resetStyle: function() {
  153. this.updateStyle(this.defaultTheme);
  154. },
  155. updateStyle: function(newStyle) {
  156. this.$themeCss.attr('href', newStyle);
  157. },
  158. getFullAssetPath: function(asset) {
  159. return this.config.fullPath + asset;
  160. },
  161. checkCookie: function () {
  162. var styleCookie;
  163. // if using cookies and using JavaScript to load css
  164. if (this.config.cookie && this.config.cookie.isManagingLoad) {
  165. // check if css is set in cookie
  166. styleCookie = _docCookies.getItem(_cookieKey);
  167. if (styleCookie) {
  168. newStyle = this.getFullAssetPath(styleCookie);
  169. // update link tag
  170. this.updateStyle(newStyle);
  171. // update default ref
  172. this.defaultTheme = newStyle;
  173. }
  174. }
  175. },
  176. addHoverEvents: function () {
  177. var self = this;
  178. this.$root.find('a').hover(
  179. function () {
  180. var asset = $(this).data('theme'),
  181. newStyle = self.getFullAssetPath(asset);
  182. // update link tag
  183. self.updateStyle(newStyle);
  184. },
  185. function () {
  186. // reset link tag
  187. self.resetStyle();
  188. }
  189. );
  190. },
  191. addClickEvents: function () {
  192. var self = this;
  193. this.$root.find('a').click(
  194. function () {
  195. var asset = $(this).data('theme'),
  196. newStyle = self.getFullAssetPath(asset);
  197. // update link tag
  198. self.updateStyle(newStyle);
  199. // update default ref
  200. self.defaultTheme = newStyle;
  201. // try to store cookie
  202. if (self.config.cookie) {
  203. _docCookies.setItem(_cookieKey, asset, self.config.cookie.expires, '/');
  204. }
  205. }
  206. );
  207. }
  208. };
  209. $.fn.styleSwitcher = function (options) {
  210. return new jStyleSwitcher(this, $.extend(true, _defaultOptions, options));
  211. };
  212. })(jQuery);