Sha256: 45df9b87c9ecb5fcd71d32f45dd46bc42a2d04633d51334159c26c967ae9de73

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

/**
* Gumby Checkbox
*/
!function() {

	'use strict';

	function Checkbox($el) {

		this.$el = $el;
		var scope = this;

		// listen for click event and custom gumby check/uncheck events
		this.$el.on(Gumby.click, function(e) {
			// prevent propagation
			e.stopImmediatePropagation();

			// prevent checkbox checking, we'll do that manually
			e.preventDefault();

			// check/uncheck
			if(scope.$el.hasClass('checked')) {
				scope.update(false);
			} else {
				scope.update(true);
			}
		}).on('gumby.check', function() {
			scope.update(true);
		}).on('gumby.uncheck', function() {
			scope.update(false);
		});

		// update any .checked checkboxes on load
		if(scope.$el.hasClass('checked')) {
			scope.update(true);
		}
	}

	// update checkbox, check equals true/false to sepcify check/uncheck
	Checkbox.prototype.update = function(check) {

		var $input = this.$el.find('input'),
			$span = this.$el.find('span');

		// check checkbox - check input, add checked class, append <i>
		if(check) {

			$span.append('<i class="icon-check" />');

			$input.prop('checked', true).end()
				.addClass('checked')
				.trigger('gumby.onCheck').trigger('gumby.onChange');

		// uncheck checkbox - uncheck input, remove checked class, remove <i>
		} else {
			$input.prop('checked', false).end()
				.find('i').remove().end()
				.removeClass('checked').trigger('gumby.onUncheck').trigger('gumby.onChange');
		}
	};

	// add initialisation
	Gumby.addInitalisation('checkboxes', function() {
		$('.checkbox').each(function() {
			var $this = $(this);
			// this element has already been initialized
			if($this.data('isCheckbox')) {
				return true;
			}
			// mark element as initialized
			$this.data('isCheckbox', true);
			new Checkbox($this);
		});
	});

	// register UI module
	Gumby.UIModule({
		module: 'checkbox',
		events: ['onCheck', 'onUncheck', 'onChange', 'check', 'uncheck'],
		init: function() {
			Gumby.initialize('checkboxes');
		}
	});
}();

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gumby-0.0.4 vendor/assets/javascripts/ui/gumby.checkbox.js
gumby-0.0.3 vendor/assets/javascripts/ui/gumby.checkbox.js
gumby-0.0.2 vendor/assets/javascripts/ui/gumby.checkbox.js
gumby-0.0.1 vendor/assets/javascripts/ui/gumby.checkbox.js