session-desktop/js/views/settings_view.js

302 lines
8.9 KiB
JavaScript
Raw Normal View History

/* global i18n: false */
/* global Whisper: false */
/* global $: false */
/* eslint-disable no-new */
// eslint-disable-next-line func-names
2018-04-27 23:25:04 +02:00
(function() {
'use strict';
2018-04-27 23:25:04 +02:00
window.Whisper = window.Whisper || {};
const { Settings } = window.Signal.Types;
2016-02-19 01:13:53 +01:00
const CheckboxView = Whisper.View.extend({
initialize(options) {
2018-08-10 01:36:41 +02:00
this.name = options.name;
this.setFn = options.setFn;
this.value = options.value;
2018-04-27 23:25:04 +02:00
this.populate();
},
events: {
change: 'change',
},
change(e) {
const value = e.target.checked;
this.setFn(value);
window.log.info(this.name, 'changed to', value);
2018-04-27 23:25:04 +02:00
},
populate() {
this.$('input').prop('checked', !!this.value);
},
});
const MediaPermissionsSettingView = Whisper.View.extend({
initialize(options) {
this.value = options.value;
this.setFn = options.setFn;
this.populate();
},
events: {
change: 'change',
},
change(e) {
this.value = e.target.checked;
this.setFn(this.value);
window.log.info('media-permissions changed to', this.value);
},
populate() {
this.$('input').prop('checked', Boolean(this.value));
2018-04-27 23:25:04 +02:00
},
});
const MessageTTLSettingView = Whisper.View.extend({
initialize(options) {
this.value = options.value;
this.setFn = options.setFn;
this.populate();
},
events: {
change: 'change',
2019-01-14 02:41:25 +01:00
input: 'input',
},
change(e) {
this.value = e.target.value;
this.setFn(this.value);
window.log.info('message-ttl-setting changed to', this.value);
},
2019-01-14 02:41:25 +01:00
input(e) {
this.value = e.target.value;
this.$('label').html(`${this.value} Hours`);
},
populate() {
this.$('input').val(this.value);
this.$('label').html(`${this.value} Hours`);
},
});
const ReadReceiptSettingView = Whisper.View.extend({
initialize(options) {
this.value = options.value;
this.setFn = options.setFn;
this.populate();
},
events: {
change: 'change',
},
change(e) {
this.value = e.target.checked;
this.setFn(this.value);
window.log.info('read-receipt-setting changed to', this.value);
},
populate() {
this.$('input').prop('checked', Boolean(this.value));
},
});
const RadioButtonGroupView = Whisper.View.extend({
initialize(options) {
2018-04-27 23:25:04 +02:00
this.name = options.name;
this.setFn = options.setFn;
this.value = options.value;
2018-04-27 23:25:04 +02:00
this.populate();
},
events: {
change: 'change',
},
change(e) {
const value = this.$(e.target).val();
this.setFn(value);
window.log.info(this.name, 'changed to', value);
2018-04-27 23:25:04 +02:00
},
populate() {
this.$(`#${this.name}-${this.value}`).attr('checked', 'checked');
2018-04-27 23:25:04 +02:00
},
});
Whisper.SettingsView = Whisper.View.extend({
className: 'settings modal expand',
templateName: 'settings',
initialize() {
2018-04-27 23:25:04 +02:00
this.render();
new RadioButtonGroupView({
el: this.$('.notification-settings'),
name: 'notification-setting',
value: window.initialData.notificationSetting,
setFn: window.setNotificationSetting,
2018-04-27 23:25:04 +02:00
});
new RadioButtonGroupView({
el: this.$('.theme-settings'),
name: 'theme-setting',
value: window.initialData.themeSetting,
setFn: theme => {
$(document.body)
.removeClass('dark-theme')
.removeClass('light-theme')
.addClass(`${theme}-theme`);
window.setThemeSetting(theme);
},
2018-04-27 23:25:04 +02:00
});
if (Settings.isAudioNotificationSupported()) {
new CheckboxView({
el: this.$('.audio-notification-setting'),
2018-08-10 01:36:41 +02:00
name: 'audio-notification-setting',
value: window.initialData.audioNotification,
setFn: window.setAudioNotification,
2018-04-27 23:25:04 +02:00
});
}
2018-07-19 03:46:12 +02:00
new CheckboxView({
el: this.$('.spell-check-setting'),
2018-08-10 01:36:41 +02:00
name: 'spell-check-setting',
2018-07-19 03:46:12 +02:00
value: window.initialData.spellCheck,
setFn: window.setSpellCheck,
});
if (Settings.isHideMenuBarSupported()) {
new CheckboxView({
el: this.$('.menu-bar-setting'),
name: 'menu-bar-setting',
value: window.initialData.hideMenuBar,
setFn: window.setHideMenuBar,
});
}
2019-02-08 04:47:42 +01:00
new CheckboxView({
el: this.$('.link-preview-setting'),
name: 'link-preview-setting',
value: window.initialData.linkPreviewSetting,
setFn: window.setLinkPreviewSetting,
});
new MediaPermissionsSettingView({
el: this.$('.media-permissions'),
value: window.initialData.mediaPermissions,
setFn: window.setMediaPermissions,
});
new ReadReceiptSettingView({
el: this.$('.read-receipt-setting'),
value: window.initialData.readReceiptSetting,
setFn: window.setReadReceiptSetting,
});
new MessageTTLSettingView({
el: this.$('.message-ttl-setting'),
value: window.initialData.messageTTL,
setFn: window.setMessageTTL,
});
2018-11-19 02:22:35 +01:00
const blockedNumberView = new Whisper.BlockedNumberView().render();
this.$('.blocked-user-setting').append(blockedNumberView.el);
if (!window.initialData.isPrimary) {
const syncView = new SyncView().render();
2018-04-27 23:25:04 +02:00
this.$('.sync-setting').append(syncView.el);
}
},
events: {
'click .close': 'onClose',
2018-04-27 23:25:04 +02:00
'click .clear-data': 'onClearData',
},
render_attributes() {
2018-04-27 23:25:04 +02:00
return {
deviceNameLabel: i18n('deviceName'),
deviceName: window.initialData.deviceName,
2018-04-27 23:25:04 +02:00
theme: i18n('theme'),
notifications: i18n('notifications'),
notificationSettingsDialog: i18n('notificationSettingsDialog'),
settings: i18n('settings'),
disableNotifications: i18n('disableNotifications'),
nameAndMessage: i18n('nameAndMessage'),
noNameOrMessage: i18n('noNameOrMessage'),
nameOnly: i18n('nameOnly'),
audioNotificationDescription: i18n('audioNotificationDescription'),
isAudioNotificationSupported: Settings.isAudioNotificationSupported(),
isHideMenuBarSupported: Settings.isHideMenuBarSupported(),
themeLight: i18n('themeLight'),
themeDark: i18n('themeDark'),
2018-04-27 23:25:04 +02:00
hideMenuBar: i18n('hideMenuBar'),
clearDataHeader: i18n('clearDataHeader'),
clearDataButton: i18n('clearDataButton'),
clearDataExplanation: i18n('clearDataExplanation'),
permissions: i18n('permissions'),
mediaPermissionsDescription: i18n('mediaPermissionsDescription'),
2019-01-16 04:03:56 +01:00
generalHeader: i18n('general'),
readReceiptSettingDescription: i18n('readReceiptSettingDescription'),
messageTTL: i18n('messageTTL'),
messageTTLSettingDescription: i18n('messageTTLSettingDescription'),
2019-01-14 02:41:25 +01:00
messageTTLSettingWarning: i18n('messageTTLSettingWarning'),
2018-07-19 03:46:12 +02:00
spellCheckHeader: i18n('spellCheck'),
spellCheckDescription: i18n('spellCheckDescription'),
blockedHeader: 'Blocked Users',
linkPreviews: i18n('linkPreviews'),
2019-01-16 04:03:56 +01:00
linkPreviewsDescription: i18n('linkPreviewsDescription'),
linkPreviewsSettingDescription: i18n('linkPreviewsSettingDescription'),
2018-04-27 23:25:04 +02:00
};
},
onClose() {
window.closeSettings();
},
onClearData() {
window.deleteAllData();
window.closeSettings();
},
});
const SyncView = Whisper.View.extend({
2018-04-27 23:25:04 +02:00
templateName: 'syncSettings',
className: 'syncSettings',
events: {
'click .sync': 'sync',
},
initialize() {
this.lastSyncTime = window.initialData.lastSyncTime;
},
enable() {
2018-04-27 23:25:04 +02:00
this.$('.sync').text(i18n('syncNow'));
this.$('.sync').removeAttr('disabled');
},
disable() {
2018-04-27 23:25:04 +02:00
this.$('.sync').attr('disabled', 'disabled');
this.$('.sync').text(i18n('syncing'));
},
onsuccess() {
window.setLastSyncTime(Date.now());
this.lastSyncTime = Date.now();
window.log.info('sync successful');
2018-04-27 23:25:04 +02:00
this.enable();
this.render();
},
ontimeout() {
window.log.error('sync timed out');
2018-04-27 23:25:04 +02:00
this.$('.synced_at').hide();
this.$('.sync_failed').show();
this.enable();
},
async sync() {
2018-04-27 23:25:04 +02:00
this.$('.sync_failed').hide();
if (window.initialData.isPrimary) {
window.log.warn('Tried to sync from device 1');
return;
}
this.disable();
try {
await window.makeSyncRequest();
this.onsuccess();
} catch (error) {
this.ontimeout();
2018-04-27 23:25:04 +02:00
}
},
render_attributes() {
const attrs = {
2018-04-27 23:25:04 +02:00
sync: i18n('sync'),
syncNow: i18n('syncNow'),
syncExplanation: i18n('syncExplanation'),
syncFailed: i18n('syncFailed'),
};
let date = this.lastSyncTime;
2018-04-27 23:25:04 +02:00
if (date) {
date = new Date(date);
attrs.lastSynced = i18n('lastSynced');
attrs.syncDate = date.toLocaleDateString();
attrs.syncTime = date.toLocaleTimeString();
}
return attrs;
},
});
2016-02-19 01:13:53 +01:00
})();