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

140 lines
4.2 KiB
JavaScript
Raw Normal View History

/* global dropdownSelector */
2016-02-12 23:52:43 +08:00
(function() {
'use strict';
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
}
// 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();
});
}
function initTimeZoneSelector() {
dropdownSelector.init('#time-zone-input-field', {
noEmptyOption: true,
singleSelect: true,
closeOnSelect: true,
selectAppearance: 'simple',
onClose: function() {
$.ajax({
url: $('#time-zone-input-field').data('path-to-update'),
type: 'PUT',
dataType: 'json',
data: { user: { time_zone: dropdownSelector.getValues('#time-zone-input-field') } },
success: function() {
dropdownSelector.highlightSuccess('#time-zone-input-field');
},
error: function() {
dropdownSelector.highlightError('#time-zone-input-field');
}
});
}
});
}
function initDateFormatSelector() {
dropdownSelector.init('#date-format-input-field', {
noEmptyOption: true,
singleSelect: true,
closeOnSelect: true,
selectAppearance: 'simple',
onClose: function() {
$.ajax({
url: $('#date-format-input-field').data('path-to-update'),
type: 'PUT',
dataType: 'json',
data: { user: { date_format: dropdownSelector.getValues('#date-format-input-field') } },
success: function() {
dropdownSelector.highlightSuccess('#date-format-input-field');
},
error: function() {
dropdownSelector.highlightError('#date-format-input-field');
}
});
}
});
}
initTimeZoneSelector();
initDateFormatSelector();
notificationsSettings();
initTogglableSettingsForm();
2016-10-04 21:52:48 +08:00
})();