radiocheck.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* ============================================================
  2. * flatui-radiocheck v0.1.0
  3. * ============================================================ */
  4. +function (global, $) {
  5. 'use strict';
  6. var Radiocheck = function (element, options) {
  7. this.init('radiocheck', element, options);
  8. };
  9. Radiocheck.DEFAULTS = {
  10. checkboxClass: 'custom-checkbox',
  11. radioClass: 'custom-radio',
  12. checkboxTemplate: '<span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>',
  13. radioTemplate: '<span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>'
  14. };
  15. Radiocheck.prototype.init = function (type, element, options) {
  16. this.$element = $(element);
  17. this.options = $.extend({}, Radiocheck.DEFAULTS, this.$element.data(), options);
  18. if (this.$element.attr('type') == 'checkbox') {
  19. this.$element.addClass(this.options.checkboxClass);
  20. this.$element.after(this.options.checkboxTemplate);
  21. } else if (this.$element.attr('type') == 'radio') {
  22. this.$element.addClass(this.options.radioClass);
  23. this.$element.after(this.options.radioTemplate);
  24. }
  25. };
  26. Radiocheck.prototype.check = function () {
  27. this.$element.prop('checked', true);
  28. this.$element.trigger('change.radiocheck').trigger('checked.radiocheck');
  29. },
  30. Radiocheck.prototype.uncheck = function () {
  31. this.$element.prop('checked', false);
  32. this.$element.trigger('change.radiocheck').trigger('unchecked.radiocheck');
  33. },
  34. Radiocheck.prototype.toggle = function () {
  35. this.$element.prop('checked', function (i, value) {
  36. return !value;
  37. });
  38. this.$element.trigger('change.radiocheck').trigger('toggled.radiocheck');
  39. },
  40. Radiocheck.prototype.indeterminate = function () {
  41. this.$element.prop('indeterminate', true);
  42. this.$element.trigger('change.radiocheck').trigger('indeterminated.radiocheck');
  43. },
  44. Radiocheck.prototype.determinate = function () {
  45. this.$element.prop('indeterminate', false);
  46. this.$element.trigger('change.radiocheck').trigger('determinated.radiocheck');
  47. },
  48. Radiocheck.prototype.disable = function () {
  49. this.$element.prop('disabled', true);
  50. this.$element.trigger('change.radiocheck').trigger('disabled.radiocheck');
  51. },
  52. Radiocheck.prototype.enable = function () {
  53. this.$element.prop('disabled', false);
  54. this.$element.trigger('change.radiocheck').trigger('enabled.radiocheck');
  55. },
  56. Radiocheck.prototype.destroy = function () {
  57. this.$element.removeData().removeClass(this.options.checkboxClass + ' ' + this.options.radioClass).next('.icons').remove();
  58. this.$element.trigger('destroyed.radiocheck');
  59. };
  60. // RADIOCHECK PLUGIN DEFINITION
  61. // ============================
  62. function Plugin(option) {
  63. return this.each(function () {
  64. var $this = $(this);
  65. var data = $this.data('radiocheck');
  66. var options = typeof option == 'object' && option;
  67. if (!data && option == 'destroy') { return; }
  68. if (!data) {
  69. $this.data('radiocheck', (data = new Radiocheck(this, options)));
  70. }
  71. if (typeof option == 'string') {
  72. data[option]();
  73. }
  74. // Adding 'nohover' class for mobile devices
  75. var mobile = /mobile|tablet|phone|ip(ad|od)|android|silk|webos/i.test(global.navigator.userAgent);
  76. if (mobile === true) {
  77. $this.parent().hover(function () {
  78. $this.addClass('nohover');
  79. }, function () {
  80. $this.removeClass('nohover');
  81. });
  82. }
  83. });
  84. }
  85. var old = $.fn.radiocheck;
  86. $.fn.radiocheck = Plugin;
  87. $.fn.radiocheck.Constructor = Radiocheck;
  88. // RADIOCHECK NO CONFLICT
  89. // ======================
  90. $.fn.radiocheck.noConflict = function () {
  91. $.fn.radiocheck = old;
  92. return this;
  93. };
  94. }(this, jQuery);