mirror of
https://github.com/ExactTarget/fuelux.git
synced 2026-01-09 22:47:54 -05:00
226 lines
5.8 KiB
JavaScript
226 lines
5.8 KiB
JavaScript
/* global jQuery:true */
|
|
|
|
/*
|
|
* Fuel UX Checkbox
|
|
* https://github.com/ExactTarget/fuelux
|
|
*
|
|
* Copyright (c) 2014 ExactTarget
|
|
* Licensed under the BSD New license.
|
|
*/
|
|
|
|
// -- BEGIN UMD WRAPPER PREFACE --
|
|
|
|
// For more information on UMD visit:
|
|
// https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
|
|
|
|
(function umdFactory (factory) {
|
|
if (typeof define === 'function' && define.amd) {
|
|
// if AMD loader is available, register as an anonymous module.
|
|
define(['jquery'], factory);
|
|
} else if (typeof exports === 'object') {
|
|
// Node/CommonJS
|
|
module.exports = factory(require('jquery'));
|
|
} else {
|
|
// OR use browser globals if AMD is not present
|
|
factory(jQuery);
|
|
}
|
|
}(function CheckboxWrapper ($) {
|
|
// -- END UMD WRAPPER PREFACE --
|
|
|
|
// -- BEGIN MODULE CODE HERE --
|
|
|
|
var old = $.fn.checkbox;
|
|
|
|
// CHECKBOX CONSTRUCTOR AND PROTOTYPE
|
|
|
|
var logError = function logError (error) {
|
|
if (window && window.console && window.console.error) {
|
|
window.console.error(error);
|
|
}
|
|
};
|
|
|
|
var Checkbox = function Checkbox (element, options) {
|
|
this.options = $.extend({}, $.fn.checkbox.defaults, options);
|
|
var $element = $(element);
|
|
|
|
if (element.tagName.toLowerCase() !== 'label') {
|
|
logError('Checkbox must be initialized on the `label` that wraps the `input` element. See https://github.com/ExactTarget/fuelux/blob/master/reference/markup/checkbox.html for example of proper markup. Call `.checkbox()` on the `<label>` not the `<input>`');
|
|
return;
|
|
}
|
|
|
|
// cache elements
|
|
this.$label = $element;
|
|
this.$chk = this.$label.find('input[type="checkbox"]');
|
|
this.$container = $element.parent('.checkbox'); // the container div
|
|
|
|
if (!this.options.ignoreVisibilityCheck && this.$chk.css('visibility').match(/hidden|collapse/)) {
|
|
logError('For accessibility reasons, in order for tab and space to function on checkbox, checkbox `<input />`\'s `visibility` must not be set to `hidden` or `collapse`. See https://github.com/ExactTarget/fuelux/pull/1996 for more details.');
|
|
}
|
|
|
|
// determine if a toggle container is specified
|
|
var containerSelector = this.$chk.attr('data-toggle');
|
|
this.$toggleContainer = $(containerSelector);
|
|
|
|
// handle internal events
|
|
this.$chk.on('change', $.proxy(this.itemchecked, this));
|
|
|
|
// set default state
|
|
this.setInitialState();
|
|
};
|
|
|
|
Checkbox.prototype = {
|
|
|
|
constructor: Checkbox,
|
|
|
|
setInitialState: function setInitialState () {
|
|
var $chk = this.$chk;
|
|
|
|
// get current state of input
|
|
var checked = $chk.prop('checked');
|
|
var disabled = $chk.prop('disabled');
|
|
|
|
// sync label class with input state
|
|
this.setCheckedState($chk, checked);
|
|
this.setDisabledState($chk, disabled);
|
|
},
|
|
|
|
setCheckedState: function setCheckedState (element, checked) {
|
|
var $chk = element;
|
|
var $lbl = this.$label;
|
|
var $containerToggle = this.$toggleContainer;
|
|
|
|
if (checked) {
|
|
$chk.prop('checked', true);
|
|
$lbl.addClass('checked');
|
|
$containerToggle.removeClass('hide hidden');
|
|
$lbl.trigger('checked.fu.checkbox');
|
|
} else {
|
|
$chk.prop('checked', false);
|
|
$lbl.removeClass('checked');
|
|
$containerToggle.addClass('hidden');
|
|
$lbl.trigger('unchecked.fu.checkbox');
|
|
}
|
|
|
|
$lbl.trigger('changed.fu.checkbox', checked);
|
|
},
|
|
|
|
setDisabledState: function setDisabledState (element, disabled) {
|
|
var $chk = $(element);
|
|
var $lbl = this.$label;
|
|
|
|
if (disabled) {
|
|
$chk.prop('disabled', true);
|
|
$lbl.addClass('disabled');
|
|
$lbl.trigger('disabled.fu.checkbox');
|
|
} else {
|
|
$chk.prop('disabled', false);
|
|
$lbl.removeClass('disabled');
|
|
$lbl.trigger('enabled.fu.checkbox');
|
|
}
|
|
|
|
return $chk;
|
|
},
|
|
|
|
itemchecked: function itemchecked (evt) {
|
|
var $chk = $(evt.target);
|
|
var checked = $chk.prop('checked');
|
|
|
|
this.setCheckedState($chk, checked);
|
|
},
|
|
|
|
toggle: function toggle () {
|
|
var checked = this.isChecked();
|
|
|
|
if (checked) {
|
|
this.uncheck();
|
|
} else {
|
|
this.check();
|
|
}
|
|
},
|
|
|
|
check: function check () {
|
|
this.setCheckedState(this.$chk, true);
|
|
},
|
|
|
|
uncheck: function uncheck () {
|
|
this.setCheckedState(this.$chk, false);
|
|
},
|
|
|
|
isChecked: function isChecked () {
|
|
var checked = this.$chk.prop('checked');
|
|
return checked;
|
|
},
|
|
|
|
enable: function enable () {
|
|
this.setDisabledState(this.$chk, false);
|
|
},
|
|
|
|
disable: function disable () {
|
|
this.setDisabledState(this.$chk, true);
|
|
},
|
|
|
|
destroy: function destroy () {
|
|
this.$label.remove();
|
|
return this.$label[0].outerHTML;
|
|
}
|
|
};
|
|
|
|
Checkbox.prototype.getValue = Checkbox.prototype.isChecked;
|
|
|
|
// CHECKBOX PLUGIN DEFINITION
|
|
|
|
$.fn.checkbox = function checkbox (option) {
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
var methodReturn;
|
|
|
|
var $set = this.each(function applyData () {
|
|
var $this = $(this);
|
|
var data = $this.data('fu.checkbox');
|
|
var options = typeof option === 'object' && option;
|
|
|
|
if (!data) {
|
|
$this.data('fu.checkbox', (data = new Checkbox(this, options)));
|
|
}
|
|
|
|
if (typeof option === 'string') {
|
|
methodReturn = data[option].apply(data, args);
|
|
}
|
|
});
|
|
|
|
return (methodReturn === undefined) ? $set : methodReturn;
|
|
};
|
|
|
|
$.fn.checkbox.defaults = {
|
|
ignoreVisibilityCheck: false
|
|
};
|
|
|
|
$.fn.checkbox.Constructor = Checkbox;
|
|
|
|
$.fn.checkbox.noConflict = function noConflict () {
|
|
$.fn.checkbox = old;
|
|
return this;
|
|
};
|
|
|
|
// DATA-API
|
|
|
|
$(document).on('mouseover.fu.checkbox.data-api', '[data-initialize=checkbox]', function initializeCheckboxes (e) {
|
|
var $control = $(e.target);
|
|
if (!$control.data('fu.checkbox')) {
|
|
$control.checkbox($control.data());
|
|
}
|
|
});
|
|
|
|
// Must be domReady for AMD compatibility
|
|
$(function onReadyInitializeCheckboxes () {
|
|
$('[data-initialize=checkbox]').each(function initializeCheckbox () {
|
|
var $this = $(this);
|
|
if (!$this.data('fu.checkbox')) {
|
|
$this.checkbox($this.data());
|
|
}
|
|
});
|
|
});
|
|
|
|
// -- BEGIN UMD WRAPPER AFTERWORD --
|
|
}));
|
|
// -- END UMD WRAPPER AFTERWORD --
|