jellyfish-web/src/controllers/dashboard/notifications/notification.js

120 lines
5.3 KiB
JavaScript
Raw Normal View History

define(['jQuery', 'emby-checkbox'], function ($) {
2020-05-04 12:44:12 +02:00
'use strict';
2018-10-23 01:05:09 +03:00
function fillItems(elem, items, cssClass, idPrefix, currentList, isEnabledList) {
var html = '<div class="checkboxList paperList" style="padding: .5em 1em;">';
html += items.map(function (u) {
var isChecked = isEnabledList ? currentList.indexOf(u.Id) != -1 : currentList.indexOf(u.Id) == -1;
2020-05-04 12:44:12 +02:00
var checkedHtml = isChecked ? ' checked="checked"' : '';
return '<label><input is="emby-checkbox" class="' + cssClass + '" type="checkbox" data-itemid="' + u.Id + '"' + checkedHtml + '/><span>' + u.Name + '</span></label>';
}).join('');
html += '</div>';
elem.html(html).trigger('create');
2018-10-23 01:05:09 +03:00
}
function reload(page) {
2020-05-04 12:44:12 +02:00
var type = getParameterByName('type');
var promise1 = ApiClient.getUsers();
var promise2 = ApiClient.getNamedConfiguration(notificationsConfigurationKey);
2020-05-04 12:44:12 +02:00
var promise3 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Types'));
var promise4 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Services'));
Promise.all([promise1, promise2, promise3, promise4]).then(function (responses) {
var users = responses[0];
var notificationOptions = responses[1];
var types = responses[2];
var services = responses[3];
var notificationConfig = notificationOptions.Options.filter(function (n) {
return n.Type == type;
})[0];
var typeInfo = types.filter(function (n) {
return n.Type == type;
})[0] || {};
if (typeInfo.IsBasedOnUserEvent) {
2020-05-04 12:44:12 +02:00
$('.monitorUsers', page).show();
} else {
2020-05-04 12:44:12 +02:00
$('.monitorUsers', page).hide();
}
2020-05-04 12:44:12 +02:00
$('.notificationType', page).html(typeInfo.Name || 'Unknown Notification');
if (!notificationConfig) {
notificationConfig = {
DisabledMonitorUsers: [],
SendToUsers: [],
DisabledServices: [],
2020-05-04 12:44:12 +02:00
SendToUserMode: 'Admins'
};
}
2020-05-04 12:44:12 +02:00
fillItems($('.monitorUsersList', page), users, 'chkMonitor', 'chkMonitor', notificationConfig.DisabledMonitorUsers);
fillItems($('.sendToUsersList', page), users, 'chkSendTo', 'chkSendTo', notificationConfig.SendToUsers, true);
fillItems($('.servicesList', page), services, 'chkService', 'chkService', notificationConfig.DisabledServices);
$('#chkEnabled', page).checked = notificationConfig.Enabled || false;
2020-05-04 12:44:12 +02:00
$('#selectUsers', page).val(notificationConfig.SendToUserMode).trigger('change');
});
2018-10-23 01:05:09 +03:00
}
function save(page) {
2020-05-04 12:44:12 +02:00
var type = getParameterByName('type');
var promise1 = ApiClient.getNamedConfiguration(notificationsConfigurationKey);
2020-05-14 23:34:23 +02:00
// TODO: Check if this promise is really needed, as it's unused.
2020-05-04 12:44:12 +02:00
var promise2 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Types'));
Promise.all([promise1, promise2]).then(function (responses) {
var notificationOptions = responses[0];
var notificationConfig = notificationOptions.Options.filter(function (n) {
return n.Type == type;
2018-10-23 01:05:09 +03:00
})[0];
if (!notificationConfig) {
notificationConfig = {
Type: type
};
notificationOptions.Options.push(notificationConfig);
}
notificationConfig.Enabled = $('#chkEnabled', page).checked;
2020-05-04 12:44:12 +02:00
notificationConfig.SendToUserMode = $('#selectUsers', page).val();
notificationConfig.DisabledMonitorUsers = $('.chkMonitor', page).get().filter(function (c) {
return !c.checked;
}).map(function (c) {
2020-05-04 12:44:12 +02:00
return c.getAttribute('data-itemid');
});
2020-05-04 12:44:12 +02:00
notificationConfig.SendToUsers = $('.chkSendTo', page).get().filter(function (c) {
return c.checked;
}).map(function (c) {
2020-05-04 12:44:12 +02:00
return c.getAttribute('data-itemid');
});
2020-05-04 12:44:12 +02:00
notificationConfig.DisabledServices = $('.chkService', page).get().filter(function (c) {
return !c.checked;
}).map(function (c) {
2020-05-04 12:44:12 +02:00
return c.getAttribute('data-itemid');
});
ApiClient.updateNamedConfiguration(notificationsConfigurationKey, notificationOptions).then(function (r) {
Dashboard.processServerConfigurationUpdateResult();
2020-05-04 12:44:12 +02:00
Dashboard.navigate('notificationsettings.html');
});
});
2018-10-23 01:05:09 +03:00
}
function onSubmit() {
2020-05-04 12:44:12 +02:00
save($(this).parents('.page'));
return false;
2018-10-23 01:05:09 +03:00
}
2020-05-04 12:44:12 +02:00
var notificationsConfigurationKey = 'notifications';
$(document).on('pageinit', '#notificationSettingPage', function () {
2018-10-23 01:05:09 +03:00
var page = this;
2020-05-04 12:44:12 +02:00
$('#selectUsers', page).on('change', function () {
if ('Custom' == this.value) {
$('.selectCustomUsers', page).show();
} else {
2020-05-04 12:44:12 +02:00
$('.selectCustomUsers', page).hide();
}
});
2020-05-04 12:44:12 +02:00
$('.notificationSettingForm').off('submit', onSubmit).on('submit', onSubmit);
}).on('pageshow', '#notificationSettingPage', function () {
reload(this);
});
});