2016-08-07 09:17:49 +01:00
|
|
|
|
(function(Modules) {
|
|
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
2016-10-11 14:17:29 +01:00
|
|
|
|
let states = {
|
|
|
|
|
|
'initial': Hogan.compile(`
|
|
|
|
|
|
<div class="radio-select-column">
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<div class="multiple-choice js-multiple-choice">
|
|
|
|
|
|
<input checked="checked" id="{{name}}-0" name="{{name}}" type="radio" value="">
|
|
|
|
|
|
<label class="block-label js-block-label" for="{{name}}-0">Now</label>
|
|
|
|
|
|
</div>
|
2016-10-11 14:17:29 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="radio-select-column">
|
|
|
|
|
|
{{#categories}}
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<input type='button' class='js-category-button' value='{{.}}' />
|
2016-10-11 14:17:29 +01:00
|
|
|
|
{{/categories}}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
`),
|
|
|
|
|
|
'choose': Hogan.compile(`
|
|
|
|
|
|
<div class="radio-select-column">
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<div class="multiple-choice js-multiple-choice js-initial-option">
|
|
|
|
|
|
<input checked="checked" id="{{name}}-0" name="{{name}}" type="radio" value="">
|
|
|
|
|
|
<label for="{{name}}-0">Now</label>
|
|
|
|
|
|
</div>
|
2016-10-11 14:17:29 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="radio-select-column">
|
|
|
|
|
|
{{#choices}}
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<div class="multiple-choice js-multiple-choice js-option">
|
|
|
|
|
|
<input type="radio" value="{{value}}" id="{{id}}" name="{{name}}" />
|
|
|
|
|
|
<label for="{{id}}">{{label}}</label>
|
|
|
|
|
|
</div>
|
2016-10-11 14:17:29 +01:00
|
|
|
|
{{/choices}}
|
2017-04-26 11:28:22 +01:00
|
|
|
|
<input type='button' class='js-reset-button js-reset-button-block' value='Back' />
|
2016-10-11 14:17:29 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
`),
|
|
|
|
|
|
'chosen': Hogan.compile(`
|
|
|
|
|
|
<div class="radio-select-column">
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<div class="multiple-choice js-multiple-choice js-initial-option">
|
|
|
|
|
|
<input id="{{name}}-0" name="{{name}}" type="radio" value="">
|
|
|
|
|
|
<label for="{{name}}-0">Now</label>
|
|
|
|
|
|
</div>
|
2016-10-11 14:17:29 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
<div class="radio-select-column">
|
|
|
|
|
|
{{#choices}}
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<div class="multiple-choice js-multiple-choice">
|
2016-10-11 14:17:29 +01:00
|
|
|
|
<input checked="checked" type="radio" value="{{value}}" id="{{id}}" name="{{name}}" />
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<label for="{{id}}">{{label}}</label>
|
|
|
|
|
|
</div>
|
2016-10-11 14:17:29 +01:00
|
|
|
|
{{/choices}}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="radio-select-column">
|
2016-12-19 10:36:17 +00:00
|
|
|
|
<input type='button' class='category-link js-reset-button' value='Choose a different time' />
|
2016-10-11 14:17:29 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
`)
|
|
|
|
|
|
};
|
2016-08-07 09:17:49 +01:00
|
|
|
|
|
2016-10-11 14:17:29 +01:00
|
|
|
|
let focusSelected = function() {
|
|
|
|
|
|
setTimeout(
|
2016-12-19 10:36:17 +00:00
|
|
|
|
() => $('[type=radio]:checked').next('label').blur().trigger('focus').addClass('selected'),
|
2017-04-26 11:23:39 +01:00
|
|
|
|
50
|
2016-10-11 14:17:29 +01:00
|
|
|
|
);
|
|
|
|
|
|
};
|
2016-08-07 09:17:49 +01:00
|
|
|
|
|
|
|
|
|
|
Modules.RadioSelect = function() {
|
|
|
|
|
|
|
|
|
|
|
|
this.start = function(component) {
|
|
|
|
|
|
|
|
|
|
|
|
let $component = $(component);
|
2016-12-19 10:36:17 +00:00
|
|
|
|
let render = (state, data) => {
|
|
|
|
|
|
$component.html(states[state].render(data));
|
|
|
|
|
|
};
|
2016-10-11 14:17:29 +01:00
|
|
|
|
let choices = $('label', $component).toArray().map(function(element) {
|
|
|
|
|
|
let $element = $(element);
|
|
|
|
|
|
return {
|
|
|
|
|
|
'id': $element.attr('for'),
|
|
|
|
|
|
'label': $.trim($element.text()),
|
2016-12-19 10:36:17 +00:00
|
|
|
|
'value': $element.prev('input').attr('value')
|
2016-10-11 14:17:29 +01:00
|
|
|
|
};
|
2016-08-07 09:17:49 +01:00
|
|
|
|
});
|
2016-10-11 14:17:29 +01:00
|
|
|
|
let categories = $component.data('categories').split(',');
|
|
|
|
|
|
let name = $component.find('input').eq(0).attr('name');
|
|
|
|
|
|
|
|
|
|
|
|
$component
|
|
|
|
|
|
.on('click', '.js-category-button', function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
2016-10-11 17:59:09 +01:00
|
|
|
|
let wordsInDay = $(this).attr('value').split(' ');
|
|
|
|
|
|
let day = wordsInDay[wordsInDay.length - 1].toLowerCase();
|
2016-10-11 14:17:29 +01:00
|
|
|
|
render('choose', {
|
|
|
|
|
|
'choices': choices.filter(
|
2016-10-11 17:59:09 +01:00
|
|
|
|
element => element.label.toLowerCase().indexOf(day) > -1
|
2016-10-11 14:17:29 +01:00
|
|
|
|
),
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
2017-04-26 11:23:39 +01:00
|
|
|
|
focusSelected();
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
.on('click', '.js-option', function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
// stop click being triggered by keyboard events
|
|
|
|
|
|
if (!event.pageX) return true;
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
2016-12-19 10:36:17 +00:00
|
|
|
|
let value = $('input', this).attr('value');
|
2016-10-11 14:17:29 +01:00
|
|
|
|
render('chosen', {
|
|
|
|
|
|
'choices': choices.filter(
|
|
|
|
|
|
element => element.value == value
|
|
|
|
|
|
),
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
|
|
|
|
|
focusSelected();
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
.on('keydown', 'input[type=radio]', function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
// intercept keypresses which aren’t enter or space
|
|
|
|
|
|
if (event.which !== 13 && event.which !== 32) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
let value = $(this).attr('value');
|
|
|
|
|
|
render('chosen', {
|
|
|
|
|
|
'choices': choices.filter(
|
|
|
|
|
|
element => element.value == value
|
|
|
|
|
|
),
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
|
|
|
|
|
focusSelected();
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
.on('click', '.js-reset-button', function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
render('initial', {
|
|
|
|
|
|
'categories': categories,
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
|
|
|
|
|
focusSelected();
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
render('initial', {
|
|
|
|
|
|
'categories': categories,
|
|
|
|
|
|
'name': name
|
2016-08-07 09:17:49 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2016-10-11 18:11:20 +01:00
|
|
|
|
$component.css({'height': 'auto'});
|
|
|
|
|
|
|
2016-08-07 09:17:49 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|