Brak opisu

jquery.style.switcher.js 6.5KB

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