';
- // for checkbox
- if( $this.attr('type') == 'checkbox') {
- $this.addClass(defaultOpt.hideCls).wrap(wrapTag).change(function() {
- if( $(this).is(':checked') ) {
- $(this).parent().addClass(defaultOpt.checkedCls);
- }
- else { $(this).parent().removeClass(defaultOpt.checkedCls); }
- });
-
- if( $this.is(':checked') ) {
- $this.parent().addClass(defaultOpt.checkedCls);
- }
- }
- else if( $this.attr('type') == 'radio') {
+(function ($) {
+ var NAMESPACE = 'ezmark';
+ var DELEGATE_CLASS = NAMESPACE + '-del';
+ var defaultOptions = {
+ checkboxClass: NAMESPACE + '-checkbox',
+ radioClass: NAMESPACE + '-radio',
+ checkedClass: NAMESPACE + '-checked',
+ hideClass: NAMESPACE + '-hide',
+ hoverClass: NAMESPACE + '-hover',
+ labelClass: NAMESPACE + '-label',
+ labelCheckboxClass: NAMESPACE + '-label-checkbox',
+ labelRadioClass: NAMESPACE + '-label-radio',
+ disabledClass: NAMESPACE + '-disabled'
+ };
- $this.addClass(defaultOpt.hideCls).wrap(wrapTag).change(function() {
- // radio button may contain groups! - so check for group
- $('input[name="'+$(this).attr('name')+'"]').each(function() {
- if( $(this).is(':checked') ) {
- $(this).parent().addClass(defaultOpt.selectedCls);
- } else {
- $(this).parent().removeClass(defaultOpt.selectedCls);
- }
- });
- });
-
- if( $this.is(':checked') ) {
- $this.parent().addClass(defaultOpt.selectedCls);
- }
- }
+ $.fn.ezMark = function (options) {
+ var defaults = $.extend({}, defaultOptions, options);
+
+ return this.each(function () {
+ var type = this.type;
+
+ if ((type === 'radio' || type === 'checkbox') && !$.data(this.parentNode, NAMESPACE)) {
+ var $this = $(this);
+ var classNames;
+ var isDisabled = !!this.disabled;
+
+ if (type === 'checkbox') {
+ classNames = {
+ input: defaults.checkboxClass,
+ label: defaults.labelCheckboxClass
+ };
+ }
+ else {
+ classNames = {
+ input: defaults.radioClass,
+ label: defaults.labelRadioClass
+ };
+ }
+
+ classNames.disabled = defaults.disabledClass;
+
+ $this.addClass(defaults.hideClass + ' ' + DELEGATE_CLASS)
+ .wrap('
');
+ var $parent = $this.parent();
+
+ if (this.checked) {
+ $parent.addClass(defaults.checkedClass);
+ }
+
+ $('label[for=' + this.id + ']').addClass(defaults.labelClass + ' ' + DELEGATE_CLASS + ' ' + classNames.label + (isDisabled ? ' ' + classNames.disabled : ''));
+ $.data(
+ $parent[0],
+ NAMESPACE,
+ {
+ classNames: {
+ checkedClass: defaults.checkedClass,
+ hoverClass: defaults.hoverClass
+ }
+ }
+ );
+ }
+ });
+ };
+
+ $(function () {
+ $(document.body).delegate('input.' + DELEGATE_CLASS, 'click', function ($e) {
+ var $this = $(this);
+ var type = this.type;
+ var $parent = $this.parent();
+ var parent = $parent[0];
+ var className = $.data(parent, NAMESPACE).classNames.checkedClass;
+
+ if (type === 'checkbox') {
+ $parent[(this.checked ? 'add' : 'remove') + 'Class'](className);
+ }
+ else if (type === 'radio') {
+ $('input[name="' + this.name + '"]').parent().removeClass(className);
+ if (this.checked) {
+ $parent.addClass(className);
+ }
+ }
+ }).delegate('div.' + DELEGATE_CLASS, 'hover', function () {
+ $(this).toggleClass($.data(this, NAMESPACE).classNames.hoverClass);
+ }).delegate('label.' + DELEGATE_CLASS, 'hover', function () {
+ var $divWrapper = $('#' + this.htmlFor).parent();
+ $divWrapper.toggleClass($.data($divWrapper[0], NAMESPACE).classNames.hoverClass);
+ });
});
- }
-})(jQuery);
\ No newline at end of file
+}(jQuery));
\ No newline at end of file