Skip to content

Commit

Permalink
JS: Use the function argument default values where appropriate
Browse files Browse the repository at this point in the history
Previously, we we simulating this using optional arguments and applying
default values within the function body. ES6 provides this more natural and
concise syntax for the same.
  • Loading branch information
paulijar committed Jun 11, 2023
1 parent 12a7a4c commit 28c15f9
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 86 deletions.
21 changes: 6 additions & 15 deletions js/app/controllers/maincontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function ($rootScope, $scope, $timeout, $window, $document, ArtistFactory,
});
}

$scope.startScanning = function(fileIds /*optional*/) {
$scope.startScanning = function(fileIds = null) {
if (fileIds) {
filesToScan = fileIds;
previouslyScannedCount = 0;
Expand Down Expand Up @@ -356,14 +356,11 @@ function ($rootScope, $scope, $timeout, $window, $document, ArtistFactory,
return offset;
}

$scope.scrollToItem = function(itemId, animationTime /* optional */) {
$scope.scrollToItem = function(itemId, animationTime = 500) {
if (itemId) {
let container = OCA.Music.Utils.getScrollContainer();
let element = $('#' + itemId);
if (container && element) {
if (animationTime === undefined) {
animationTime = 500;
}
container.scrollToElement(element, scrollOffset(), animationTime);
}
}
Expand All @@ -376,12 +373,12 @@ function ($rootScope, $scope, $timeout, $window, $document, ArtistFactory,
// Navigate to a view selected from the navigation bar
let navigationDestination = null;
let afterNavigationCallback = null;
$scope.navigateTo = function(destination, callback /*optional*/) {
$scope.navigateTo = function(destination, callback = null) {
let curView = $rootScope.currentView;
if (curView != destination) {
$rootScope.currentView = null;
navigationDestination = destination;
afterNavigationCallback = callback || null;
afterNavigationCallback = callback;
$rootScope.loading = true;
// Deactivate the current view. The view emits 'viewDeactivated' once that is done.
// In the abnormal special case of no active view, activate the new view immediately.
Expand All @@ -402,10 +399,7 @@ function ($rootScope, $scope, $timeout, $window, $document, ArtistFactory,

// Compact/normal layout of the Albums view
$scope.albumsCompactLayout = (localStorage.getItem('oc_music_albums_compact') === 'true');
$scope.toggleAlbumsCompactLayout = function(useCompact /*optional, invert current value if omitted */) {
if (typeof useCompact === 'undefined') {
useCompact = !$scope.albumsCompactLayout;
}
$scope.toggleAlbumsCompactLayout = function(useCompact = !$scope.albumsCompactLayout) {
$scope.albumsCompactLayout = useCompact;
$('#albums').toggleClass('compact', useCompact);
$rootScope.$emit('albumsLayoutChanged');
Expand All @@ -418,10 +412,7 @@ function ($rootScope, $scope, $timeout, $window, $document, ArtistFactory,

// Flat/tree layout of the Folders view
$scope.foldersFlatLayout = (localStorage.getItem('oc_music_folders_flat') === 'true');
$scope.toggleFoldersFlatLayout = function(useFlat /*optional, invert current value if omitted */) {
if (typeof useFlat === 'undefined') {
useFlat = !$scope.foldersFlatLayout;
}
$scope.toggleFoldersFlatLayout = function(useFlat = !$scope.foldersFlatLayout) {
$scope.foldersFlatLayout = useFlat;
$rootScope.$emit('foldersLayoutChanged');

Expand Down
15 changes: 7 additions & 8 deletions js/app/controllers/playercontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Morris Jobke 2013
* @copyright Pauli Järvinen 2017 - 2022
* @copyright Pauli Järvinen 2017 - 2023
*/

import radioIconPath from '../../../img/radio-file.svg';
Expand Down Expand Up @@ -208,8 +208,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula
}
}

function getRadioTitle(radioTrack, failCounter /*optional, internal*/) {
failCounter = failCounter || 0;
function getRadioTitle(radioTrack, failCounter = 0 /*internal*/) {
abortRadioTitleFetch = $q.defer();
const config = {timeout: abortRadioTitleFetch.promise};
const metaType = radioTrack.metadata?.type; // request the same metadata type as previously got (if any)
Expand Down Expand Up @@ -259,7 +258,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula
return null;
}

function setCurrentTrack(playlistEntry, startOffset /*optional*/, gapless /*optional*/) {
function setCurrentTrack(playlistEntry, startOffset = 0, gapless = false) {
let track = playlistEntry ? playlistEntry.track : null;

if (track !== null) {
Expand All @@ -276,7 +275,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula
$scope.shiftHeldDown = false;
}

function playCurrentTrack(startOffset /*optional*/) {
function playCurrentTrack(startOffset = 0) {
// the playback may have been stopped and currentTrack vanished during the debounce time
if ($scope.currentTrack) {
if ($scope.currentTrack.type === 'radio') {
Expand Down Expand Up @@ -318,7 +317,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula
*/
let debouncedPlayCurrentTrack = _.debounce(playCurrentTrack, 300);

function playTrack(track, startOffset /*optional*/, gapless /*optional*/) {
function playTrack(track, startOffset = 0, gapless = false) {
$scope.currentTrack = track;

// Don't indicate support for seeking before we actually know its status for the new track.
Expand Down Expand Up @@ -509,7 +508,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula
$timeout(() => $scope.playPauseContextMenuVisible = false);
});

$scope.next = function(startOffset /*optional*/, gapless /*optional*/) {
$scope.next = function(startOffset = 0, gapless = false) {
let entry = playlistService.jumpToNextTrack();

// For ordinary tracks, skip the tracks with unsupported MIME types.
Expand Down Expand Up @@ -613,7 +612,7 @@ function ($scope, $rootScope, playlistService, Audio, gettextCatalog, Restangula

$scope.seekForward = $scope.player.seekForward;

playlistService.subscribe('play', function(_event, _playingView /*optional, ignored*/, startOffset /*optional*/) {
playlistService.subscribe('play', function(_event, _playingView = null, startOffset = 0) {
$scope.next(startOffset); /* fetch track and start playing*/
});

Expand Down
4 changes: 2 additions & 2 deletions js/app/controllers/views/alltracksviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2018 - 2022
* @copyright Pauli Järvinen 2018 - 2023
*/


Expand Down Expand Up @@ -35,7 +35,7 @@ angular.module('Music').controller('AllTracksViewController', [
_.each(_unsubFuncs, function(func) { func(); });
});

function play(startIndex /*optional*/) {
function play(startIndex = 0) {
playlistService.setPlaylist('alltracks', _tracks, startIndex);
playlistService.publish('play');
}
Expand Down
6 changes: 3 additions & 3 deletions js/app/controllers/views/foldersviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2019 - 2022
* @copyright Pauli Järvinen 2019 - 2023
*/


Expand All @@ -30,10 +30,10 @@ angular.module('Music').controller('FoldersViewController', [
unsubFuncs.push( $rootScope.$on(event, handler) );
}

function playPlaylist(listId, tracks, startFromTrackId /*optional*/) {
function playPlaylist(listId, tracks, startFromTrackId = undefined) {
let startIndex = null;
if (startFromTrackId !== undefined) {
startIndex = _.findIndex(tracks, function(i) {return i.track.id == startFromTrackId;});
startIndex = _.findIndex(tracks, (i) => i.track.id == startFromTrackId);
}
playlistService.setPlaylist(listId, tracks, startIndex);
playlistService.publish('play');
Expand Down
6 changes: 3 additions & 3 deletions js/app/controllers/views/genresviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2020, 2021
* @copyright Pauli Järvinen 2020 - 2023
*/


Expand Down Expand Up @@ -34,10 +34,10 @@ angular.module('Music').controller('GenresViewController', [
$scope.$parent.filesWithUnscannedGenre = null;
};

function playPlaylist(listId, tracks, startFromTrackId /*optional*/) {
function playPlaylist(listId, tracks, startFromTrackId = undefined) {
let startIndex = null;
if (startFromTrackId !== undefined) {
startIndex = _.findIndex(tracks, function(i) {return i.track.id == startFromTrackId;});
startIndex = _.findIndex(tracks, (i) => i.track.id == startFromTrackId);
}
playlistService.setPlaylist(listId, tracks, startIndex);
playlistService.publish('play');
Expand Down
4 changes: 2 additions & 2 deletions js/app/controllers/views/playlistviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Morris Jobke 2013
* @copyright Pauli Järvinen 2017 - 2021
* @copyright Pauli Järvinen 2017 - 2023
*/


Expand Down Expand Up @@ -58,7 +58,7 @@ angular.module('Music').controller('PlaylistViewController', [
});
};

function play(startIndex /*optional*/) {
function play(startIndex = 0) {
let id = 'playlist-' + $scope.playlist.id;
playlistService.setPlaylist(id, $scope.tracks, startIndex);
playlistService.publish('play');
Expand Down
6 changes: 3 additions & 3 deletions js/app/controllers/views/podcastsviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2021, 2022
* @copyright Pauli Järvinen 2021 - 2023
*/

angular.module('Music').controller('PodcastsViewController', [
Expand Down Expand Up @@ -122,14 +122,14 @@ angular.module('Music').controller('PodcastsViewController', [
updateHighlight(playlistId);
});

subscribe('scrollToPodcastEpisode', function(_event, episodeId, animationTime /* optional */) {
subscribe('scrollToPodcastEpisode', function(_event, episodeId, animationTime = 500) {
let episode = libraryService.getPodcastEpisode(episodeId);
if (episode) {
$scope.$parent.scrollToItem('podcast-channel-' + episode.channel.id, animationTime);
}
});

subscribe('scrollToPodcastChannel', function(_event, channelId, animationTime /* optional */) {
subscribe('scrollToPodcastChannel', function(_event, channelId, animationTime = 500) {
$scope.$parent.scrollToItem('podcast-channel-' + channelId, animationTime);
});

Expand Down
4 changes: 2 additions & 2 deletions js/app/controllers/views/radioviewcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2020, 2021
* @copyright Pauli Järvinen 2020 - 2023
*/


Expand Down Expand Up @@ -75,7 +75,7 @@ angular.module('Music').controller('RadioViewController', [
);
}

function play(startIndex /*optional*/) {
function play(startIndex = 0) {
playlistService.setPlaylist('radio', $scope.stations, startIndex);
playlistService.publish('play');
}
Expand Down
6 changes: 3 additions & 3 deletions js/app/directives/albumart.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright 2013 Morris Jobke
* @copyright 2016 - 2022 Pauli Järvinen
* @copyright 2016 - 2023 Pauli Järvinen
*
*/

Expand All @@ -21,12 +21,12 @@ angular.module('Music').directive('albumart', [function() {
element.css('background-image', 'url(' + imageUrl + ')');
}

function setPlaceholder(element, text, seed /*optional*/) {
function setPlaceholder(element, text, seed = null) {
if (text) {
// remove background image
element.css('background-image', '');
// add placeholder stuff
element.imageplaceholder(seed || text, text);
element.imageplaceholder(seed ?? text, text);
// remove inlined size-related style properties set by imageplaceholder() to allow
// dynamic changing between mobile and desktop styles when window size changes
element.css('line-height', '');
Expand Down
8 changes: 4 additions & 4 deletions js/app/directives/inviewobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright 2019 - 2022 Pauli Järvinen
* @copyright 2019 - 2023 Pauli Järvinen
*
*/

Expand Down Expand Up @@ -117,7 +117,7 @@ function($rootScope, $timeout, inViewService) {
/**
* Initial setup of the in-view-port statuses of the available instances
*/
function initInViewRange(skipDelays/*optional*/) {
function initInViewRange(skipDelays = false) {
let length = _instances.length;
let i;

Expand Down Expand Up @@ -204,7 +204,7 @@ function($rootScope, $timeout, inViewService) {
/**
* Update in-view-port status of the given instance
*/
function updateInViewStatus(inst, skipDelays/*optional*/) {
function updateInViewStatus(inst, skipDelays = false) {
skipDelays = skipDelays || false;

let wasInViewPort = inst.inViewPort;
Expand Down Expand Up @@ -285,7 +285,7 @@ function($rootScope, $timeout, inViewService) {
invalidateInViewRange();
}

function updateStatusForAll(skipDelays/*optional*/) {
function updateStatusForAll(skipDelays = false) {
_(_instances).each(function(inst) {
updateInViewStatus(inst, skipDelays);
});
Expand Down
4 changes: 2 additions & 2 deletions js/app/directives/tracklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Moritz Meißelbach <moritz@meisselba.ch>
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright 2017 Moritz Meißelbach
* @copyright 2018 - 2021 Pauli Järvinen
* @copyright 2018 - 2023 Pauli Järvinen
*
*/

Expand Down Expand Up @@ -151,7 +151,7 @@ function ($rootScope, $interpolate, gettextCatalog) {
* @param string className (optional)
* @returns {HTMLLIElement}
*/
function getTrackNode(track, index, className) {
function getTrackNode(track, index, className = null) {
let listItem = document.createElement('li');

let listItemContent = document.createElement('div');
Expand Down
7 changes: 2 additions & 5 deletions js/app/services/inviewservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright 2019 Pauli Järvinen
* @copyright 2019 - 2023 Pauli Järvinen
*
*/

Expand Down Expand Up @@ -38,11 +38,8 @@ angular.module('Music').service('inViewService', ['$rootScope', function($rootSc
* @param int topMargin Optional top extension in pixels (use negative value for reduction)
* @param int bottomMargin Optional bottom extension in pixels (use negative value for reduction)
*/
isElementInViewPort: function(el, topMargin/*optional*/, bottomMargin/*optional*/) {
isElementInViewPort: function(el, topMargin = 0, bottomMargin = 0) {
if (el) {
topMargin = topMargin || 0;
bottomMargin = bottomMargin || 0;

if (dirty) {
updateHeights();
}
Expand Down
Loading

0 comments on commit 28c15f9

Please sign in to comment.