flatui-checkbox.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* =============================================================
  2. * flatui-checkbox v0.0.3
  3. * ============================================================ */
  4. !function ($) {
  5. /* CHECKBOX PUBLIC CLASS DEFINITION
  6. * ============================== */
  7. var Checkbox = function (element, options) {
  8. this.init(element, options);
  9. }
  10. Checkbox.prototype = {
  11. constructor: Checkbox
  12. , init: function (element, options) {
  13. var $el = this.$element = $(element)
  14. this.options = $.extend({}, $.fn.checkbox.defaults, options);
  15. $el.before(this.options.template);
  16. this.setState();
  17. }
  18. , setState: function () {
  19. var $el = this.$element
  20. , $parent = $el.closest('.checkbox');
  21. $el.prop('disabled') && $parent.addClass('disabled');
  22. $el.prop('checked') && $parent.addClass('checked');
  23. }
  24. , toggle: function () {
  25. var ch = 'checked'
  26. , $el = this.$element
  27. , $parent = $el.closest('.checkbox')
  28. , checked = $el.prop(ch)
  29. , e = $.Event('toggle')
  30. if ($el.prop('disabled') == false) {
  31. $parent.toggleClass(ch) && checked ? $el.removeAttr(ch) : $el.prop(ch, ch);
  32. $el.trigger(e).trigger('change');
  33. }
  34. }
  35. , setCheck: function (option) {
  36. var d = 'disabled'
  37. , ch = 'checked'
  38. , $el = this.$element
  39. , $parent = $el.closest('.checkbox')
  40. , checkAction = option == 'check' ? true : false
  41. , e = $.Event(option)
  42. $parent[checkAction ? 'addClass' : 'removeClass' ](ch) && checkAction ? $el.prop(ch, ch) : $el.removeAttr(ch);
  43. $el.trigger(e).trigger('change');
  44. }
  45. }
  46. /* CHECKBOX PLUGIN DEFINITION
  47. * ======================== */
  48. var old = $.fn.checkbox
  49. $.fn.checkbox = function (option) {
  50. return this.each(function () {
  51. var $this = $(this)
  52. , data = $this.data('checkbox')
  53. , options = $.extend({}, $.fn.checkbox.defaults, $this.data(), typeof option == 'object' && option);
  54. if (!data) $this.data('checkbox', (data = new Checkbox(this, options)));
  55. if (option == 'toggle') data.toggle()
  56. if (option == 'check' || option == 'uncheck') data.setCheck(option)
  57. else if (option) data.setState();
  58. });
  59. }
  60. $.fn.checkbox.defaults = {
  61. template: '<span class="icons"><span class="first-icon fui-checkbox-unchecked"></span><span class="second-icon fui-checkbox-checked"></span></span>'
  62. }
  63. /* CHECKBOX NO CONFLICT
  64. * ================== */
  65. $.fn.checkbox.noConflict = function () {
  66. $.fn.checkbox = old;
  67. return this;
  68. }
  69. /* CHECKBOX DATA-API
  70. * =============== */
  71. $(document).on('click.checkbox.data-api', '[data-toggle^=checkbox], .checkbox', function (e) {
  72. var $checkbox = $(e.target);
  73. if (e.target.tagName != "A") {
  74. e && e.preventDefault() && e.stopPropagation();
  75. if (!$checkbox.hasClass('checkbox')) $checkbox = $checkbox.closest('.checkbox');
  76. $checkbox.find(':checkbox').checkbox('toggle');
  77. }
  78. });
  79. $(function () {
  80. $('[data-toggle="checkbox"]').each(function () {
  81. var $checkbox = $(this);
  82. $checkbox.checkbox();
  83. });
  84. });
  85. }(window.jQuery);