bjui-theme.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*!
  2. * B-JUI v1.2 (http://b-jui.com)
  3. * Git@OSC (http://git.oschina.net/xknaan/B-JUI)
  4. * Copyright 2014 K'naan (xknaan@163.com).
  5. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  6. */
  7. /* ========================================================================
  8. * B-JUI: bjui-theme.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-theme.js
  11. * ========================================================================
  12. * Copyright 2014 K'naan.
  13. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  14. * ======================================================================== */
  15. +function ($) {
  16. 'use strict';
  17. // THEME GLOBAL ELEMENTS
  18. // ======================
  19. var $themeLink, $themeLis
  20. $(function() {
  21. var INIT_THEME = function() {
  22. $themeLink = $('#bjui-link-theme')
  23. $themeLis = $('#bjui-themes')
  24. if ($.cookie) {
  25. var themeName = $.cookie('bjui_theme') || 'orange'
  26. var $li = $themeLis.find('a.theme_'+ themeName)
  27. $li.theme({})
  28. }
  29. }
  30. INIT_THEME()
  31. })
  32. // THEME CLASS DEFINITION
  33. // ======================
  34. var Theme = function(element, options) {
  35. this.$element = $(element)
  36. this.options = options
  37. }
  38. Theme.DEFAULTS = {
  39. theme: 'orange'
  40. }
  41. Theme.prototype.init = function() {
  42. if (!$themeLink.length) return
  43. var themeHref = $themeLink.attr('href')
  44. themeHref = themeHref.substring(0, themeHref.lastIndexOf('/'))
  45. themeHref = themeHref.substring(0, themeHref.lastIndexOf('/'))
  46. themeHref += '/'+ this.options.theme +'/core.css'
  47. $themeLink.attr('href', themeHref)
  48. var $themeA = this.$element.closest('ul').prev()
  49. var classA = $themeA.attr('class')
  50. classA = classA.replace(/(theme[\s][a-z]*)/g, '')
  51. $themeA.removeClass().addClass(classA).addClass('theme').addClass(this.options.theme)
  52. $themeLis.find('li').removeClass('active')
  53. this.$element.parent().addClass('active')
  54. this.cookie()
  55. }
  56. Theme.prototype.setTheme = function(themeName) {
  57. $themeLis.find('a.theme_'+ themeName).trigger('click')
  58. }
  59. Theme.prototype.cookie = function() {
  60. var theme = this.options.theme
  61. if ($.cookie) $.cookie('bjui_theme', theme, { path: '/', expires: 30 });
  62. }
  63. // THEME PLUGIN DEFINITION
  64. // =======================
  65. function Plugin(option) {
  66. var args = arguments
  67. var property = option
  68. return this.each(function () {
  69. var $this = $(this)
  70. var options = $.extend({}, Theme.DEFAULTS, $this.data(), typeof option == 'object' && option)
  71. var data = $this.data('bjui.theme')
  72. if (!data) $this.data('bjui.theme', (data = new Theme(this, options)))
  73. if (typeof property == 'string' && $.isFunction(data[property])) {
  74. [].shift.apply(args)
  75. if (!args) data[property]()
  76. else data[property].apply(data, args)
  77. } else {
  78. data.init()
  79. }
  80. })
  81. }
  82. var old = $.fn.theme
  83. $.fn.theme = Plugin
  84. $.fn.theme.Constructor = Theme
  85. // THEME NO CONFLICT
  86. // =================
  87. $.fn.theme.noConflict = function () {
  88. $.fn.theme = old
  89. return this
  90. }
  91. // THEME DATA-API
  92. // ==============
  93. $(document).on('click.bjui.theme.data-api', '[data-toggle="theme"]', function(e) {
  94. Plugin.call($(this))
  95. e.preventDefault()
  96. })
  97. }(jQuery);