scinote-web/app/assets/javascripts/users/settings/account/preferences/index.js

167 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-10-04 21:52:48 +08:00
(function(){
'use strict';
/**
* Toggle the view/edit form visibility.
* @param form - The jQuery form selector.
* @param edit - True to set form to edit mode;
* false to set form to view mode.
*/
function toggleFormVisibility(form, edit) {
if (edit) {
2018-08-08 17:43:18 +08:00
form.find('.selectpicker').selectpicker();
2016-10-04 21:52:48 +08:00
form.find("[data-part='view']").hide();
form.find("[data-part='edit']").show();
form.find("[data-part='edit'] input:not([type='file']):not([type='submit']):first").focus();
} else {
form.find("[data-part='view']").show();
form.find("[data-part='edit'] input").blur();
form.find("[data-part='edit']").hide();
// Clear all errors on the parent form
form.clearFormErrors();
// Clear any neccesary fields
form.find("input[data-role='clear']").val("");
// Copy field data
var val = form.find("input[data-role='src']").val();
form.find("input[data-role='edit']").val(val);
}
2016-02-12 23:52:43 +08:00
}
2016-10-04 21:52:48 +08:00
var forms = $("form[data-for]");
2016-02-12 23:52:43 +08:00
2016-10-04 21:52:48 +08:00
// Add "edit form" listeners
forms
.find("[data-action='edit']").click(function() {
var form = $(this).closest("form");
2016-02-12 23:52:43 +08:00
2016-10-04 21:52:48 +08:00
// First, hide all form edits
_.each(forms, function(form) {
toggleFormVisibility($(form), false);
});
// Then, edit the current form
toggleFormVisibility(form, true);
2016-02-12 23:52:43 +08:00
});
2016-10-04 21:52:48 +08:00
// Add "cancel form" listeners
forms
.find("[data-action='cancel']").click(function() {
var form = $(this).closest("form");
2016-07-21 19:11:15 +08:00
2016-10-04 21:52:48 +08:00
// Hide the edit portion of the form
toggleFormVisibility(form, false);
2016-07-21 19:11:15 +08:00
});
2016-10-04 21:52:48 +08:00
// Add form submit listeners
forms
.on("ajax:success", function(ev, data, status) {
// Simply reload the page
location.reload();
})
.on("ajax:error", function(ev, data, status) {
// Render form errors
$(this).renderFormErrors("user", data.responseJSON);
2016-07-21 19:11:15 +08:00
});
2016-10-04 21:52:48 +08:00
notificationsSettings();
tooltipSettings();
initTogglableSettingsForm();
2016-10-04 21:52:48 +08:00
// Setup notification checkbox buttons
function notificationsSettings() {
var notification_settings = [ "recent_notification",
"assignments_notification" ]
for (var i = 0; i < notification_settings.length; i++ ) {
var setting = $('[name="' + notification_settings[i] + '"]');
var dependant = $('[name="' + notification_settings[i] + '_email"]');
2018-05-18 22:24:24 +08:00
dependant.checkboxpicker({ onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle' });
setting
.checkboxpicker({
2018-05-18 22:24:24 +08:00
onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle'
}).change(function() {
if ( $(this).prop('checked') ) {
enableDependant($('[name="' + $(this).attr('name') + '_email"]'));
} else {
disableDependant($('[name="' + $(this).attr('name') + '_email"]'));
}
});
if ( setting.attr('value') === 'true' ) {
setting.prop('checked', true);
} else {
setting.prop('checked', false);
disableDependant(dependant);
}
setEmailSwitch(dependant);
}
2016-10-04 21:52:48 +08:00
function setEmailSwitch(setting) {
setting
.checkboxpicker({
2018-05-18 22:24:24 +08:00
onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle'
});
if ( setting.attr('value') === 'true' ) {
setting.prop('checked', true);
enableDependant(setting);
} else {
setting.prop('checked', false);
}
2016-10-04 21:52:48 +08:00
}
function disableDependant(dependant) {
dependant.checkboxpicker().prop('disabled', true);
dependant.checkboxpicker().prop('checked', false);
}
2016-10-04 21:52:48 +08:00
function enableDependant(dependant) {
dependant.checkboxpicker().prop('disabled', false);
2016-10-04 21:52:48 +08:00
}
// Initialize system messages
var system_message_notification = $('[name="system_message_notification"]');
system_message_notification
.checkboxpicker({
2018-05-18 22:24:24 +08:00
onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle'
});
system_message_notification.prop('checked', true);
system_message_notification.prop('disabled', true);
// Initialize system messages email
var system_message_notification_mail = $('[name="system_message_notification_email"]');
system_message_notification_mail
.checkboxpicker({
2018-05-18 22:24:24 +08:00
onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle'
});
system_message_notification_mail.prop(
'checked',
system_message_notification_mail.attr('value') === 'true'
);
2016-10-04 21:52:48 +08:00
}
// Initialize tooltips settings form
function tooltipSettings() {
var toggleInput = $('[name="tooltips_enabled"]');
toggleInput
.checkboxpicker({ onActiveCls: 'btn-toggle', offActiveCls: 'btn-toggle' });
if (toggleInput.attr('value') === 'true') {
toggleInput.prop('checked', true);
} else {
toggleInput.prop('checked', false);
}
}
2016-10-04 21:52:48 +08:00
// triggers submit action when the user clicks
function initTogglableSettingsForm() {
$('#togglable-settings-panel')
2016-10-04 21:52:48 +08:00
.find('.btn-group')
.on('click', function() {
$(this).submit();
});
}
})();