2019-05-22 13:11:48 +01:00
|
|
|
|
(function(global) {
|
2016-08-07 09:17:49 +01:00
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
2019-05-22 13:11:48 +01:00
|
|
|
|
var Modules = global.GOVUK.Modules;
|
|
|
|
|
|
var Hogan = global.Hogan;
|
|
|
|
|
|
|
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}}
|
2019-07-08 10:42:54 +01:00
|
|
|
|
<input type='button' class='js-done-button js-done-button-block' value='Done' />
|
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
|
|
|
|
|
2019-07-09 10:02:18 +01:00
|
|
|
|
let focusSelected = function(component) {
|
|
|
|
|
|
$('[type=radio]:checked', component).focus();
|
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');
|
2019-07-01 11:48:09 +01:00
|
|
|
|
let mousedownOption = null;
|
|
|
|
|
|
const reset = () => {
|
2019-07-08 10:42:54 +01:00
|
|
|
|
render('initial', {
|
|
|
|
|
|
'categories': categories,
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2019-07-01 11:48:09 +01:00
|
|
|
|
const selectOption = (value) => {
|
|
|
|
|
|
render('chosen', {
|
|
|
|
|
|
'choices': choices.filter(
|
|
|
|
|
|
element => element.value == value
|
|
|
|
|
|
),
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
2019-07-09 10:02:18 +01:00
|
|
|
|
focusSelected(component);
|
2019-07-01 11:48:09 +01:00
|
|
|
|
};
|
|
|
|
|
|
const trackMouseup = (event) => {
|
|
|
|
|
|
const parentNode = event.target.parentNode;
|
|
|
|
|
|
|
|
|
|
|
|
if (parentNode === mousedownOption) {
|
|
|
|
|
|
const value = $('input', parentNode).attr('value');
|
|
|
|
|
|
|
|
|
|
|
|
selectOption(value);
|
|
|
|
|
|
|
|
|
|
|
|
// clear tracking
|
|
|
|
|
|
mousedownOption = null;
|
|
|
|
|
|
$(document).off('mouseup', trackMouseup);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
});
|
2019-07-09 10:02:18 +01:00
|
|
|
|
focusSelected(component);
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
})
|
2019-07-01 11:48:09 +01:00
|
|
|
|
.on('mousedown', '.js-option', function(event) {
|
|
|
|
|
|
mousedownOption = this;
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
2019-07-01 11:48:09 +01:00
|
|
|
|
// mouseup on the same option completes the click action
|
|
|
|
|
|
$(document).on('mouseup', trackMouseup);
|
2016-10-11 14:17:29 +01:00
|
|
|
|
})
|
2019-07-01 11:48:09 +01:00
|
|
|
|
// space and enter, clicked on a radio confirm that option was selected
|
2016-10-11 14:17:29 +01:00
|
|
|
|
.on('keydown', 'input[type=radio]', function(event) {
|
|
|
|
|
|
|
2019-07-01 11:48:09 +01:00
|
|
|
|
// allow keypresses which aren’t enter or space through
|
2016-10-11 14:17:29 +01:00
|
|
|
|
if (event.which !== 13 && event.which !== 32) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
let value = $(this).attr('value');
|
2019-07-01 11:48:09 +01:00
|
|
|
|
selectOption(value);
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
})
|
2019-07-08 10:42:54 +01:00
|
|
|
|
.on('click', '.js-done-button', function(event) {
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
2019-07-08 10:42:54 +01:00
|
|
|
|
let $selection = $('input[type=radio]:checked', this.parentNode);
|
|
|
|
|
|
if ($selection.length) {
|
|
|
|
|
|
|
|
|
|
|
|
render('chosen', {
|
|
|
|
|
|
'choices': choices.filter(
|
|
|
|
|
|
element => element.value == $selection.eq(0).attr('value')
|
|
|
|
|
|
),
|
|
|
|
|
|
'name': name
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-07-09 10:02:18 +01:00
|
|
|
|
focusSelected(component);
|
2016-10-11 14:17:29 +01:00
|
|
|
|
|
2019-07-08 10:42:54 +01:00
|
|
|
|
})
|
|
|
|
|
|
.on('click', '.js-reset-button', function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
reset();
|
2019-07-09 10:02:18 +01:00
|
|
|
|
focusSelected(component);
|
2019-07-08 10:42:54 +01:00
|
|
|
|
|
2016-10-11 14:17:29 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2019-05-22 13:11:48 +01:00
|
|
|
|
})(window);
|