/
Radio.js
107 lines (93 loc) · 3.06 KB
/
Radio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Toggles the display of related content to a change event from a grouping of radio buttons.
* @class RadioButton
* @constructor
* @extends FormSelect
* @requires ptclass
* @param {HTMLElement} element The HTML element containing this component
* @param {Object} settings Configuration properties for this instance
*/
var Class = require( 'class' ),
FormSelect = require( 'lu/Abstract' ),
RadioButton;
RadioButton = Class.create( FormSelect, ( function () {
// RETURN METHODS OBJECT
return {
/**
* PTClass constructor
* @method initialize
* @public
* @param {Object} $super Pointer to superclass constructor
* @param {Object} $element JQuery object for the element wrapped by the component
* @param {Object} settings Configuration settings
*/
initialize: function ( $super, $element, settings ) {
// PRIVATE INSTANCE PROPERTIES
/**
* Instance of Reveal
* @property Reveal
* @type Object
* @private
*/
var RadioButton = this,
/**
* Default configuration values
* @property defaults
* @type Object
* @private
* @final
*/
defaults = {
on: 'change',
action: 'select',
ariaAttrib: 'aria-controls'
};
// MIX THE DEFAULTS INTO THE SETTINGS VALUES
_.defaults( settings, defaults );
/**
* Determines the ids of any other components to notify
* based on the "aria-controls" attribute.
* @method getNotifyIds
* @private
* @return {Array} The list of element ids.
*/
function getNotifyIds() {
var ids = [];
$( 'input[type="radio"][name="' + $element.attr("name") + '"]', $element.closest("form") ).each( function(index, item) {
var id = $(item).attr(defaults.ariaAttrib);
if ( id ) {
ids.push( "#" + id );
}
});
_.log( 'Radio.getNotifyIds', ids );
return ids;
}
// Get the notify target(s) from the config or from the ARIA-controls attribute
settings.notify = settings.notify || getNotifyIds().join();
// CALL THE PARENT'S CONSTRUCTOR
$super( $element, settings );
// PRIVILEDGED METHODS
/**
* Fires a custom event on the element bound to this instance
* @method triggerAction
* @public
* @return {Void}
*/
RadioButton.triggerAction = function () {
var action = settings.action,
item = $('input:[type="radio"][name="' + $element.attr("name") + '"]:checked', $element.closest("form")).attr(settings.ariaAttrib) || "--";
_.log("Radio.triggerAction", "$element", $element, settings.notify);
RadioButton.trigger(settings.action, item);
};
RadioButton.triggerAction();
RadioButton.on(settings.on, function (event) {
event.stopPropagation();
RadioButton.triggerAction();
});
}
};
}() ));
//Export to CommonJS Loader
if( module && module.exports ) {
module.exports = RadioButton;
}