1/** 2* Gumby RadioBtn 3*/ 4!function($) { 5 6 'use strict'; 7 8 function RadioBtn($el) { 9 10 Gumby.debug('Initializing Radio Button', $el); 11 12 this.$el = $el; 13 this.$input = this.$el.find('input[type=radio]'); 14 15 var scope = this; 16 17 // listen for click event and custom gumby check event 18 this.$el.on(Gumby.click, function(e) { 19 // prevent radio button checking, we'll do that manually 20 e.preventDefault(); 21 22 // do nothing if radio is disabled 23 if (scope.$input.is('[disabled]')) { 24 return; 25 } 26 27 // check radio button 28 scope.update(); 29 }).on('gumby.check', function() { 30 Gumby.debug('Check event triggered', scope.$el); 31 scope.update(); 32 }); 33 34 // update any prechecked on load 35 if(this.$input.prop('checked') || this.$el.hasClass('checked')) { 36 scope.update(true); 37 } 38 } 39 40 // check radio button, uncheck all others in name group 41 RadioBtn.prototype.update = function() { 42 43 // already checked so no need to update 44 if(this.$el.hasClass('checked') && this.$input.prop('checked') && this.$el.find('i.icon-dot').length) { 45 return; 46 } 47 48 Gumby.debug('Updating Radio Button group', this.$el); 49 50 var $span = this.$el.find('span'), 51 // the group of radio buttons 52 group = 'input[name="'+this.$input.attr('name')+'"]'; 53 54 // uncheck radio buttons in same group - uncheck input, remove checked class, remove <i> 55 $('.radio').has(group).removeClass('checked') 56 .find('input').prop('checked', false).end() 57 .find('i').remove(); 58 59 // check this radio button - check input, add checked class, append <i> 60 this.$input.prop('checked', true); 61 $span.append('<i class="icon-dot" />'); 62 63 Gumby.debug('Triggering onCheck event', this.$el); 64 65 this.$el.addClass('checked').trigger('gumby.onCheck'); 66 }; 67 68 // add initialisation 69 Gumby.addInitalisation('radiobtn', function() { 70 $('.radio').each(function() { 71 var $this = $(this); 72 // this element has already been initialized 73 if($this.data('isRadioBtn')) { 74 return true; 75 } 76 // mark element as initialized 77 $this.data('isRadioBtn', true); 78 new RadioBtn($this); 79 }); 80 }); 81 82 // register UI module 83 Gumby.UIModule({ 84 module: 'radiobtn', 85 events: ['onCheck', 'check'], 86 init: function() { 87 Gumby.initialize('radiobtn'); 88 } 89 }); 90}(jQuery); 91