jellyfin-web/dashboard-ui/scripts/notificationsetting.js

191 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-09-09 19:41:29 -07:00
define(['jQuery', 'emby-checkbox'], function ($) {
2014-04-26 20:42:05 -07:00
2014-07-02 11:34:08 -07:00
var notificationsConfigurationKey = "notifications";
2015-06-07 21:47:19 -07:00
2014-04-27 10:54:43 -07:00
function fillItems(elem, items, cssClass, idPrefix, currentList, isEnabledList) {
2014-04-26 20:42:05 -07:00
2016-09-09 19:41:29 -07:00
var html = '<div class="checkboxList paperList" style="padding: .5em 1em;">';
2014-04-26 20:42:05 -07:00
html += items.map(function (u) {
2014-04-27 10:54:43 -07:00
var isChecked = isEnabledList ? currentList.indexOf(u.Id) != -1 : currentList.indexOf(u.Id) == -1;
var checkedHtml = isChecked ? ' checked="checked"' : '';
2014-04-26 20:42:05 -07:00
2016-09-09 19:41:29 -07:00
return '<label><input is="emby-checkbox" class="' + cssClass + '" type="checkbox" data-itemid="' + u.Id + '"' + checkedHtml + '/><span>' + u.Name + '</span></label>';
2014-04-26 20:42:05 -07:00
}).join('');
html += '</div>';
elem.html(html).trigger('create');
}
function reload(page) {
var type = getParameterByName('type');
var promise1 = ApiClient.getUsers();
2014-07-02 11:34:08 -07:00
var promise2 = ApiClient.getNamedConfiguration(notificationsConfigurationKey);
2014-07-01 22:16:59 -07:00
var promise3 = ApiClient.getJSON(ApiClient.getUrl("Notifications/Types"));
var promise4 = ApiClient.getJSON(ApiClient.getUrl("Notifications/Services"));
2014-04-26 20:42:05 -07:00
2015-12-14 08:43:03 -07:00
Promise.all([promise1, promise2, promise3, promise4]).then(function (responses) {
2014-04-26 20:42:05 -07:00
2015-12-14 08:43:03 -07:00
var users = responses[0];
var notificationOptions = responses[1];
var types = responses[2];
var services = responses[3];
2014-04-26 20:42:05 -07:00
2014-07-02 11:34:08 -07:00
var notificationConfig = notificationOptions.Options.filter(function (n) {
2014-04-26 20:42:05 -07:00
return n.Type == type;
})[0];
var typeInfo = types.filter(function (n) {
return n.Type == type;
})[0] || {};
if (typeInfo.IsBasedOnUserEvent) {
$('.monitorUsers', page).show();
} else {
$('.monitorUsers', page).hide();
}
if (typeInfo.Variables.length) {
$('.tokenHelp', page).show();
$('.tokenList', page).html(typeInfo.Variables.map(function (v) {
return '{' + v + '}';
}).join(', '));
} else {
$('.tokenHelp', page).hide();
}
$('.notificationType', page).html(typeInfo.Name || 'Unknown Notification');
if (!notificationConfig) {
notificationConfig = {
DisabledMonitorUsers: [],
2014-04-27 10:54:43 -07:00
SendToUsers: [],
DisabledServices: [],
SendToUserMode: 'Admins'
2014-04-26 20:42:05 -07:00
};
}
fillItems($('.monitorUsersList', page), users, 'chkMonitor', 'chkMonitor', notificationConfig.DisabledMonitorUsers);
2014-04-27 10:54:43 -07:00
fillItems($('.sendToUsersList', page), users, 'chkSendTo', 'chkSendTo', notificationConfig.SendToUsers, true);
2014-04-26 20:42:05 -07:00
fillItems($('.servicesList', page), services, 'chkService', 'chkService', notificationConfig.DisabledServices);
2016-04-14 19:58:51 -07:00
$('#chkEnabled', page).checked(notificationConfig.Enabled || false);
2014-04-26 20:42:05 -07:00
$('#txtTitle', page).val(notificationConfig.Title || typeInfo.DefaultTitle);
2015-09-03 10:01:51 -07:00
$('#selectUsers', page).val(notificationConfig.SendToUserMode).trigger('change');
2014-04-27 10:54:43 -07:00
2014-04-26 20:42:05 -07:00
});
}
function save(page) {
var type = getParameterByName('type');
2014-07-02 11:34:08 -07:00
var promise1 = ApiClient.getNamedConfiguration(notificationsConfigurationKey);
2014-07-01 22:16:59 -07:00
var promise2 = ApiClient.getJSON(ApiClient.getUrl("Notifications/Types"));
2014-04-26 20:42:05 -07:00
2015-12-14 08:43:03 -07:00
Promise.all([promise1, promise2]).then(function (responses) {
2014-04-26 20:42:05 -07:00
2015-12-14 08:43:03 -07:00
var notificationOptions = responses[0];
var types = responses[1];
2014-04-26 20:42:05 -07:00
2014-07-02 11:34:08 -07:00
var notificationConfig = notificationOptions.Options.filter(function (n) {
2014-04-26 20:42:05 -07:00
return n.Type == type;
})[0];
if (!notificationConfig) {
notificationConfig = {
Type: type
};
notificationOptions.Options.push(notificationConfig);
}
var typeInfo = types.filter(function (n) {
return n.Type == type;
})[0] || {};
notificationConfig.Enabled = $('#chkEnabled', page).checked();
notificationConfig.Title = $('#txtTitle', page).val();
2014-04-27 10:54:43 -07:00
notificationConfig.SendToUserMode = $('#selectUsers', page).val();
2014-04-26 20:42:05 -07:00
// Don't persist if it's just the default
if (notificationConfig.Title == typeInfo.DefaultTitle) {
notificationConfig.Title = null;
}
2016-04-14 19:58:51 -07:00
notificationConfig.DisabledMonitorUsers = $('.chkMonitor', page).get().filter(function (c) {
return !c.checked;
}).map(function (c) {
2014-04-26 20:42:05 -07:00
return c.getAttribute('data-itemid');
});
2016-04-14 19:58:51 -07:00
notificationConfig.SendToUsers = $('.chkSendTo', page).get().filter(function (c) {
return c.checked;
}).map(function (c) {
2014-04-26 20:42:05 -07:00
return c.getAttribute('data-itemid');
});
2016-04-14 19:58:51 -07:00
notificationConfig.DisabledServices = $('.chkService', page).get().filter(function (c) {
return !c.checked;
}).map(function (c) {
2014-04-26 20:42:05 -07:00
return c.getAttribute('data-itemid');
});
2015-12-14 08:43:03 -07:00
ApiClient.updateNamedConfiguration(notificationsConfigurationKey, notificationOptions).then(function (r) {
2014-04-26 20:42:05 -07:00
Dashboard.navigate('notificationsettings.html');
});
});
}
2015-06-07 21:47:19 -07:00
function onSubmit() {
var page = $(this).parents('.page');
save(page);
return false;
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#notificationSettingPage", function () {
2014-04-27 10:54:43 -07:00
var page = this;
$('#selectUsers', page).on('change', function () {
if (this.value == 'Custom') {
$('.selectCustomUsers', page).show();
} else {
$('.selectCustomUsers', page).hide();
}
});
2015-06-07 21:47:19 -07:00
$('.notificationSettingForm').off('submit', onSubmit).on('submit', onSubmit);
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#notificationSettingPage", function () {
2014-04-26 20:42:05 -07:00
var page = this;
reload(page);
});
});