2018-11-28 17:40:54 +00:00
|
|
|
(function(Modules) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
Modules.TemplateFolderForm = function() {
|
|
|
|
|
|
|
|
|
|
this.start = function(templateFolderForm) {
|
|
|
|
|
this.$form = $(templateFolderForm);
|
|
|
|
|
|
remove the unknown button in js
When you hit enter while an input in a form is in focus, your browser
finds the first button in the form, and carries out that action. So,
for non-js users, we added a hidden submit button with a value of
"unknown" to reflect that we don't know the intention of the user.
However, with JS enabled, this ambiguity doesn't exist - there's only
submit button and forms to fill in at a time, and non-visible fields
aren't even submitted at all. We can remove the unknown button,
supporting enter as submit properly. If the user is on one of the grey
button states, with no submit, it'll press the first button, and go to
the new template / move to existing folder dialog. That's fine enough.
2018-12-05 13:48:07 +00:00
|
|
|
// remove the hidden unknown button - if you've got JS enabled then the action you want to do is implied by
|
|
|
|
|
// which field is visible.
|
|
|
|
|
this.$form.find('button[value=unknown]').remove();
|
|
|
|
|
|
2018-11-28 17:40:54 +00:00
|
|
|
this.$stickyBottom = this.$form.find('#sticky_template_forms');
|
|
|
|
|
|
|
|
|
|
this.$stickyBottom.append(this.nothingSelectedButtons);
|
|
|
|
|
this.$stickyBottom.append(this.itemsSelectedButtons);
|
|
|
|
|
|
|
|
|
|
// all the diff states that we want to show or hide
|
2018-12-04 14:48:39 +00:00
|
|
|
this.states = [
|
|
|
|
|
{key: 'nothing-selected-buttons', $el: this.$form.find('#nothing_selected'), cancellable: false},
|
|
|
|
|
{key: 'items-selected-buttons', $el: this.$form.find('#items_selected'), cancellable: false},
|
|
|
|
|
{key: 'move-to-existing-folder', $el: this.$form.find('#move_to_folder_radios'), cancellable: true},
|
|
|
|
|
{key: 'move-to-new-folder', $el: this.$form.find('#move_to_new_folder_form'), cancellable: true},
|
|
|
|
|
{key: 'add-new-folder', $el: this.$form.find('#add_new_folder_form'), cancellable: true},
|
|
|
|
|
{key: 'add-new-template', $el: this.$form.find('#add_new_template_form'), cancellable: true}
|
|
|
|
|
];
|
2018-11-29 17:39:58 +00:00
|
|
|
|
2018-12-10 16:53:12 +00:00
|
|
|
// cancel/clear buttons only relevant if JS enabled, so
|
2018-12-04 14:48:39 +00:00
|
|
|
this.states.filter(state => state.cancellable).forEach((x) => this.addCancelButton(x));
|
2018-12-10 16:53:12 +00:00
|
|
|
this.states.filter(state => state.key === 'items-selected-buttons').forEach(x => this.addClearButton(x));
|
2018-11-30 12:02:51 +00:00
|
|
|
|
2018-11-29 16:20:44 +00:00
|
|
|
// first off show the new template / new folder buttons
|
2018-12-05 12:01:02 +00:00
|
|
|
this.currentState = this.$form.data('prev-state') || 'unknown';
|
|
|
|
|
if (this.currentState === 'unknown') {
|
|
|
|
|
this.selectActionButtons();
|
2018-12-19 15:24:48 +00:00
|
|
|
} else {
|
|
|
|
|
this.render();
|
2018-12-05 12:01:02 +00:00
|
|
|
}
|
2018-11-29 16:20:44 +00:00
|
|
|
|
2018-11-29 17:39:58 +00:00
|
|
|
this.$form.on('click', 'button.button-secondary', (event) => this.actionButtonClicked(event));
|
2018-11-29 16:20:44 +00:00
|
|
|
this.$form.on('change', 'input[type=checkbox]', () => this.templateFolderCheckboxChanged());
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-04 14:48:39 +00:00
|
|
|
this.addCancelButton = function(state) {
|
2018-12-10 16:53:12 +00:00
|
|
|
let $cancel = this.makeButton('Cancel', () => {
|
|
|
|
|
|
|
|
|
|
// clear existing data
|
|
|
|
|
state.$el.find('input:radio').prop('checked', false);
|
|
|
|
|
state.$el.find('input:text').val('');
|
|
|
|
|
|
|
|
|
|
// go back to action buttons
|
|
|
|
|
this.selectActionButtons();
|
|
|
|
|
});
|
2018-11-30 12:02:51 +00:00
|
|
|
|
2018-12-10 17:07:30 +00:00
|
|
|
state.$el.find('[type=submit]').after($cancel);
|
2018-11-30 12:02:51 +00:00
|
|
|
};
|
|
|
|
|
|
2018-12-10 16:53:12 +00:00
|
|
|
this.addClearButton = function(state) {
|
|
|
|
|
|
|
|
|
|
let $clear = this.makeButton('Clear', () => {
|
|
|
|
|
|
|
|
|
|
// uncheck all templates and folders
|
|
|
|
|
this.$form.find('input:checkbox').prop('checked', false);
|
|
|
|
|
|
|
|
|
|
// go back to action buttons
|
|
|
|
|
this.selectActionButtons();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
state.$el.find('.template-list-selected-counter').append($clear);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.makeButton = (text, fn) => $('<a></a>')
|
|
|
|
|
.html(text)
|
2018-12-10 17:07:30 +00:00
|
|
|
.addClass('js-cancel')
|
2018-12-10 16:53:12 +00:00
|
|
|
.attr('tabindex', '0')
|
|
|
|
|
.on('click keydown', event => {
|
2018-12-10 17:32:36 +00:00
|
|
|
// space, enter or no keyCode (must be mouse input)
|
|
|
|
|
if ([13, 32, undefined].indexOf(event.keyCode) > -1) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
fn();
|
|
|
|
|
}
|
2018-12-10 16:53:12 +00:00
|
|
|
});
|
|
|
|
|
|
2018-12-05 12:01:02 +00:00
|
|
|
this.selectActionButtons = function () {
|
|
|
|
|
// If we want to show one of the grey choose actions state, we can pretend we're in the choose actions state,
|
|
|
|
|
// and then pretend a checkbox was clicked to work out whether to show zero or non-zero options.
|
|
|
|
|
// This calls a render at the end
|
|
|
|
|
this.currentState = 'nothing-selected-buttons';
|
|
|
|
|
this.templateFolderCheckboxChanged();
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-29 17:39:58 +00:00
|
|
|
this.actionButtonClicked = function(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.currentState = $(event.currentTarget).val();
|
|
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.templateFolderCheckboxChanged = function() {
|
2018-11-29 16:20:44 +00:00
|
|
|
let numSelected = this.countSelectedCheckboxes();
|
|
|
|
|
|
2018-12-04 14:47:05 +00:00
|
|
|
if (this.currentState === 'nothing-selected-buttons' && numSelected !== 0) {
|
2018-11-30 16:29:00 +00:00
|
|
|
// user has just selected first item
|
2018-12-04 14:47:05 +00:00
|
|
|
this.currentState = 'items-selected-buttons';
|
|
|
|
|
} else if (this.currentState === 'items-selected-buttons' && numSelected === 0) {
|
2018-11-30 16:29:00 +00:00
|
|
|
// user has just deselected last item
|
2018-12-04 14:47:05 +00:00
|
|
|
this.currentState = 'nothing-selected-buttons';
|
2018-11-29 16:20:44 +00:00
|
|
|
}
|
2018-11-28 17:40:54 +00:00
|
|
|
|
|
|
|
|
this.render();
|
2018-12-10 16:53:12 +00:00
|
|
|
|
|
|
|
|
$('.template-list-selected-counter-count').html(numSelected);
|
|
|
|
|
|
2019-01-03 10:50:05 +00:00
|
|
|
$('.template-list-selected-counter').toggle(this.hasCheckboxes());
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.hasCheckboxes = function() {
|
|
|
|
|
return !!this.$form.find('input:checkbox').length;
|
2018-11-28 17:40:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.countSelectedCheckboxes = function() {
|
2018-11-30 16:29:00 +00:00
|
|
|
return this.$form.find('input:checkbox:checked').length;
|
2018-11-28 17:40:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.render = function() {
|
2018-11-29 17:39:58 +00:00
|
|
|
// detach everything, unless they are the currentState
|
2018-12-04 14:48:39 +00:00
|
|
|
this.states.forEach(
|
|
|
|
|
state => (state.key === this.currentState ? this.$stickyBottom.append(state.$el) : state.$el.detach())
|
2018-11-29 17:39:58 +00:00
|
|
|
);
|
2018-12-13 14:25:22 +00:00
|
|
|
|
|
|
|
|
// make sticky JS recalculate its cache of the element's position
|
|
|
|
|
if ('stickAtBottomWhenScrolling' in GOVUK) {
|
|
|
|
|
GOVUK.stickAtBottomWhenScrolling.recalculate();
|
|
|
|
|
}
|
2018-11-28 17:40:54 +00:00
|
|
|
};
|
|
|
|
|
|
2018-11-30 16:29:00 +00:00
|
|
|
this.nothingSelectedButtons = `
|
|
|
|
|
<div id="nothing_selected">
|
2018-12-04 14:47:05 +00:00
|
|
|
<button class="button-secondary" value="add-new-template">New template</button>
|
|
|
|
|
<button class="button-secondary" value="add-new-folder">New folder</button>
|
2018-12-10 16:53:12 +00:00
|
|
|
<div class="template-list-selected-counter">
|
|
|
|
|
Nothing selected
|
|
|
|
|
</div>
|
2018-11-30 16:29:00 +00:00
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
this.itemsSelectedButtons = `
|
|
|
|
|
<div id="items_selected">
|
2018-12-04 14:47:05 +00:00
|
|
|
<button class="button-secondary" value="move-to-existing-folder">Move</button>
|
|
|
|
|
<button class="button-secondary" value="move-to-new-folder">Add to a new folder</button>
|
2018-12-10 16:53:12 +00:00
|
|
|
<div class="template-list-selected-counter">
|
2018-12-10 17:07:30 +00:00
|
|
|
<span class="template-list-selected-counter-count">1</span> selected
|
2018-12-10 16:53:12 +00:00
|
|
|
</div>
|
2018-11-30 16:29:00 +00:00
|
|
|
</div>
|
|
|
|
|
`;
|
2018-11-28 17:40:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|