From 81731f4a8ed10963c962ed4ca6ad9b3d9cde2c92 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 26 Jul 2020 10:37:04 +0100 Subject: [PATCH] Migration of notification and notifications to ES6 modules --- package.json | 2 + .../dashboard/notifications/notification.js | 221 +++++++++--------- .../dashboard/notifications/notifications.js | 108 ++++----- 3 files changed, 167 insertions(+), 164 deletions(-) diff --git a/package.json b/package.json index dbedd2922e..088e820a85 100644 --- a/package.json +++ b/package.json @@ -177,6 +177,8 @@ "src/controllers/dashboard/metadataImages.js", "src/controllers/dashboard/metadatanfo.js", "src/controllers/dashboard/networking.js", + "src/controllers/dashboard/notifications/notification.js", + "src/controllers/dashboard/notifications/notifications.js", "src/controllers/dashboard/playback.js", "src/controllers/dashboard/plugins/repositories.js", "src/controllers/dashboard/scheduledtasks/scheduledtask.js", diff --git a/src/controllers/dashboard/notifications/notification.js b/src/controllers/dashboard/notifications/notification.js index af9301c13f..013e505f36 100644 --- a/src/controllers/dashboard/notifications/notification.js +++ b/src/controllers/dashboard/notifications/notification.js @@ -1,119 +1,118 @@ -define(['jQuery', 'emby-checkbox'], function ($) { - 'use strict'; +import $ from 'jQuery'; +import 'emby-checkbox'; - function fillItems(elem, items, cssClass, idPrefix, currentList, isEnabledList) { - var html = '
'; - html += items.map(function (u) { - var isChecked = isEnabledList ? currentList.indexOf(u.Id) != -1 : currentList.indexOf(u.Id) == -1; - var checkedHtml = isChecked ? ' checked="checked"' : ''; - return ''; - }).join(''); - html += '
'; - elem.html(html).trigger('create'); - } +function fillItems(elem, items, cssClass, idPrefix, currentList, isEnabledList) { + let html = '
'; + html += items.map(function (u) { + const isChecked = isEnabledList ? currentList.indexOf(u.Id) != -1 : currentList.indexOf(u.Id) == -1; + const checkedHtml = isChecked ? ' checked="checked"' : ''; + return ''; + }).join(''); + html += '
'; + elem.html(html).trigger('create'); +} - function reload(page) { - var type = getParameterByName('type'); - var promise1 = ApiClient.getUsers(); - var promise2 = ApiClient.getNamedConfiguration(notificationsConfigurationKey); - 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] || {}; +function reload(page) { + const type = getParameterByName('type'); + const promise1 = ApiClient.getUsers(); + const promise2 = ApiClient.getNamedConfiguration(notificationsConfigurationKey); + const promise3 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Types')); + const promise4 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Services')); + Promise.all([promise1, promise2, promise3, promise4]).then(function (responses) { + const users = responses[0]; + const notificationOptions = responses[1]; + const types = responses[2]; + const services = responses[3]; + let notificationConfig = notificationOptions.Options.filter(function (n) { + return n.Type == type; + })[0]; + const typeInfo = types.filter(function (n) { + return n.Type == type; + })[0] || {}; - if (typeInfo.IsBasedOnUserEvent) { - $('.monitorUsers', page).show(); - } else { - $('.monitorUsers', page).hide(); - } + if (typeInfo.IsBasedOnUserEvent) { + $('.monitorUsers', page).show(); + } else { + $('.monitorUsers', page).hide(); + } - $('.notificationType', page).html(typeInfo.Name || 'Unknown Notification'); + $('.notificationType', page).html(typeInfo.Name || 'Unknown Notification'); - if (!notificationConfig) { - notificationConfig = { - DisabledMonitorUsers: [], - SendToUsers: [], - DisabledServices: [], - SendToUserMode: 'Admins' - }; - } + if (!notificationConfig) { + notificationConfig = { + DisabledMonitorUsers: [], + SendToUsers: [], + DisabledServices: [], + SendToUserMode: 'Admins' + }; + } - 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).prop('checked', notificationConfig.Enabled || false); - $('#selectUsers', page).val(notificationConfig.SendToUserMode).trigger('change'); - }); - } - - function save(page) { - var type = getParameterByName('type'); - var promise1 = ApiClient.getNamedConfiguration(notificationsConfigurationKey); - // TODO: Check if this promise is really needed, as it's unused. - 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; - })[0]; - - if (!notificationConfig) { - notificationConfig = { - Type: type - }; - notificationOptions.Options.push(notificationConfig); - } - - notificationConfig.Enabled = $('#chkEnabled', page).is(':checked'); - notificationConfig.SendToUserMode = $('#selectUsers', page).val(); - notificationConfig.DisabledMonitorUsers = $('.chkMonitor', page).get().filter(function (c) { - return !c.checked; - }).map(function (c) { - return c.getAttribute('data-itemid'); - }); - notificationConfig.SendToUsers = $('.chkSendTo', page).get().filter(function (c) { - return c.checked; - }).map(function (c) { - return c.getAttribute('data-itemid'); - }); - notificationConfig.DisabledServices = $('.chkService', page).get().filter(function (c) { - return !c.checked; - }).map(function (c) { - return c.getAttribute('data-itemid'); - }); - ApiClient.updateNamedConfiguration(notificationsConfigurationKey, notificationOptions).then(function (r) { - Dashboard.processServerConfigurationUpdateResult(); - Dashboard.navigate('notificationsettings.html'); - }); - }); - } - - function onSubmit() { - save($(this).parents('.page')); - return false; - } - - var notificationsConfigurationKey = 'notifications'; - $(document).on('pageinit', '#notificationSettingPage', function () { - var page = this; - $('#selectUsers', page).on('change', function () { - if ('Custom' == this.value) { - $('.selectCustomUsers', page).show(); - } else { - $('.selectCustomUsers', page).hide(); - } - }); - $('.notificationSettingForm').off('submit', onSubmit).on('submit', onSubmit); - }).on('pageshow', '#notificationSettingPage', function () { - reload(this); + 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).prop('checked', notificationConfig.Enabled || false); + $('#selectUsers', page).val(notificationConfig.SendToUserMode).trigger('change'); }); +} + +function save(page) { + const type = getParameterByName('type'); + const promise1 = ApiClient.getNamedConfiguration(notificationsConfigurationKey); + // TODO: Check if this promise is really needed, as it's unused. + const promise2 = ApiClient.getJSON(ApiClient.getUrl('Notifications/Types')); + Promise.all([promise1, promise2]).then(function (responses) { + const notificationOptions = responses[0]; + let notificationConfig = notificationOptions.Options.filter(function (n) { + return n.Type == type; + })[0]; + + if (!notificationConfig) { + notificationConfig = { + Type: type + }; + notificationOptions.Options.push(notificationConfig); + } + + notificationConfig.Enabled = $('#chkEnabled', page).is(':checked'); + notificationConfig.SendToUserMode = $('#selectUsers', page).val(); + notificationConfig.DisabledMonitorUsers = $('.chkMonitor', page).get().filter(function (c) { + return !c.checked; + }).map(function (c) { + return c.getAttribute('data-itemid'); + }); + notificationConfig.SendToUsers = $('.chkSendTo', page).get().filter(function (c) { + return c.checked; + }).map(function (c) { + return c.getAttribute('data-itemid'); + }); + notificationConfig.DisabledServices = $('.chkService', page).get().filter(function (c) { + return !c.checked; + }).map(function (c) { + return c.getAttribute('data-itemid'); + }); + ApiClient.updateNamedConfiguration(notificationsConfigurationKey, notificationOptions).then(function (r) { + Dashboard.processServerConfigurationUpdateResult(); + Dashboard.navigate('notificationsettings.html'); + }); + }); +} + +function onSubmit() { + save($(this).parents('.page')); + return false; +} + +const notificationsConfigurationKey = 'notifications'; +$(document).on('pageinit', '#notificationSettingPage', function () { + const page = this; + $('#selectUsers', page).on('change', function () { + if ('Custom' == this.value) { + $('.selectCustomUsers', page).show(); + } else { + $('.selectCustomUsers', page).hide(); + } + }); + $('.notificationSettingForm').off('submit', onSubmit).on('submit', onSubmit); +}).on('pageshow', '#notificationSettingPage', function () { + reload(this); }); diff --git a/src/controllers/dashboard/notifications/notifications.js b/src/controllers/dashboard/notifications/notifications.js index 4e049bc106..d12a5f2e61 100644 --- a/src/controllers/dashboard/notifications/notifications.js +++ b/src/controllers/dashboard/notifications/notifications.js @@ -1,60 +1,62 @@ -define(['loading', 'libraryMenu', 'globalize', 'listViewStyle', 'emby-button'], function(loading, libraryMenu, globalize) { - 'use strict'; +import loading from 'loading'; +import libraryMenu from 'libraryMenu'; +import globalize from 'globalize'; +import 'listViewStyle'; +import 'emby-button'; - function reload(page) { - loading.show(); - ApiClient.getJSON(ApiClient.getUrl('Notifications/Types')).then(function(list) { - var html = ''; - var lastCategory = ''; - var showHelp = true; - html += list.map(function(notification) { - var itemHtml = ''; - if (notification.Category !== lastCategory) { - lastCategory = notification.Category; - if (lastCategory) { - itemHtml += ''; - itemHtml += ''; - } - itemHtml += '
'; - itemHtml += '
'; - itemHtml += '

'; - itemHtml += notification.Category; - itemHtml += '

'; - if (showHelp) { - showHelp = false; - itemHtml += ''; - itemHtml += globalize.translate('Help'); - itemHtml += ''; - } +function reload(page) { + loading.show(); + ApiClient.getJSON(ApiClient.getUrl('Notifications/Types')).then(function (list) { + let html = ''; + let lastCategory = ''; + let showHelp = true; + html += list.map(function (notification) { + let itemHtml = ''; + if (notification.Category !== lastCategory) { + lastCategory = notification.Category; + if (lastCategory) { + itemHtml += '
'; itemHtml += '
'; - itemHtml += '
'; } - itemHtml += ''; - if (notification.Enabled) { - itemHtml += ''; - } else { - itemHtml += ''; + itemHtml += '
'; + itemHtml += '
'; + itemHtml += '

'; + itemHtml += notification.Category; + itemHtml += '

'; + if (showHelp) { + showHelp = false; + itemHtml += '
'; + itemHtml += globalize.translate('Help'); + itemHtml += ''; } - itemHtml += '
'; - itemHtml += '
' + notification.Name + '
'; itemHtml += '
'; - itemHtml += ''; - itemHtml += ''; - return itemHtml; - }).join(''); - - if (list.length) { - html += '
'; - html += '
'; + itemHtml += '
'; } - page.querySelector('.notificationList').innerHTML = html; - loading.hide(); - }); - } + itemHtml += ''; + if (notification.Enabled) { + itemHtml += ''; + } else { + itemHtml += ''; + } + itemHtml += '
'; + itemHtml += '
' + notification.Name + '
'; + itemHtml += '
'; + itemHtml += ''; + itemHtml += '
'; + return itemHtml; + }).join(''); - return function(view, params) { - view.addEventListener('viewshow', function() { - reload(view); - }); - }; -}); + if (list.length) { + html += '
'; + html += '
'; + } + page.querySelector('.notificationList').innerHTML = html; + loading.hide(); + }); +} + +export default function (view, params) { + view.addEventListener('viewshow', function () { + reload(view); + }); +}