Skip to content

Commit

Permalink
JS: Use the class syntax on a couple of embedded player modules
Browse files Browse the repository at this point in the history
The modules are Playlist and PlaylistFileService. Also the EmbeddedPlayer
module could have been transformed but that would have required a bit more
effort.
  • Loading branch information
paulijar committed Jun 11, 2023
1 parent 56c845d commit 4dc4008
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
14 changes: 7 additions & 7 deletions js/embedded/main.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 2017 - 2022
* @copyright Pauli Järvinen 2017 - 2023
*/

import playIconPath from '../../img/play-big.svg';
Expand All @@ -28,7 +28,7 @@ function initEmbeddedPlayer() {
let mShareToken = $('#sharingToken').val(); // undefined when not on share page

let mPlayer = new OCA.Music.EmbeddedPlayer(onClose, onNext, onPrev, onMenuOpen, onShowList, onImportList, onImportRadio);
let mPlaylist = new OCA.Music.Playlist();
let mPlaylist = null;

const mAudioMimes = _.filter([
'audio/aac',
Expand Down Expand Up @@ -137,11 +137,11 @@ function initEmbeddedPlayer() {
}

function onImportList() {
doImportFromFile(OCA.Music.playlistFileService.importPlaylist);
doImportFromFile(OCA.Music.PlaylistFileService.importPlaylist);
}

function onImportRadio() {
doImportFromFile(OCA.Music.playlistFileService.importRadio);
doImportFromFile(OCA.Music.PlaylistFileService.importRadio);
}

function doImportFromFile(serviceImportFunc) {
Expand Down Expand Up @@ -270,7 +270,7 @@ function initEmbeddedPlayer() {
mPlayingListFile = false;

mPlayer.show();
mPlaylist.init(mFileList.files, mAudioMimes, mCurrentFile.id);
mPlaylist = new OCA.Music.Playlist(mFileList.files, mAudioMimes, mCurrentFile.id);
mPlayer.setNextAndPrevEnabled(mPlaylist.length() > 1);
jumpToPlaylistFile(mPlaylist.currentFile());
}
Expand All @@ -283,7 +283,7 @@ function initEmbeddedPlayer() {
let onPlaylistLoaded = function(data) {
if (data.files.length > 0) {
mPlayer.show(mCurrentFile.name);
mPlaylist.init(data.files, mAudioMimes, data.files[0].id);
mPlaylist = new OCA.Music.Playlist(data.files, mAudioMimes, data.files[0].id);
mPlayer.setNextAndPrevEnabled(mPlaylist.length() > 1);
jumpToPlaylistFile(mPlaylist.currentFile());
}
Expand Down Expand Up @@ -313,7 +313,7 @@ function initEmbeddedPlayer() {
OC.Notification.showTemporary(t('music', 'Error reading playlist file'));
mFileList.showFileBusyState($file, false);
};
OCA.Music.playlistFileService.readFile(mCurrentFile.id, onPlaylistLoaded, onError, mShareToken);
OCA.Music.PlaylistFileService.readFile(mCurrentFile.id, onPlaylistLoaded, onError, mShareToken);
}

/**
Expand Down
76 changes: 38 additions & 38 deletions js/embedded/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,69 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2018 - 2020
* @copyright Pauli Järvinen 2018 - 2023
*/

OCA.Music = OCA.Music || {};

OCA.Music.Playlist = function() {
OCA.Music.Playlist = class {

let mFiles = null;
let mCurrentIndex = null;
#files = null;
#currentIndex = null;

function jumpToOffset(offset) {
if (!mFiles || mFiles.length <= 1) {
#jumpToOffset(offset) {
if (!this.#files || this.#files.length <= 1) {
return null;
} else {
mCurrentIndex = (mCurrentIndex + mFiles.length + offset) % mFiles.length;
return mFiles[mCurrentIndex];
this.#currentIndex = (this.#currentIndex + this.#files.length + offset) % this.#files.length;
return this.#files[this.#currentIndex];
}
}

this.init = function(folderFiles, supportedMimes, firstFileId) {
mFiles = _.filter(folderFiles, function(file) {
constructor(folderFiles, supportedMimes, firstFileId) {
this.#files = _.filter(folderFiles, function(file) {
// external URLs do not have a valid MIME type set, attempt to play them regardless
return file.mimetype === null || _.includes(supportedMimes, file.mimetype);
});
mCurrentIndex = _.findIndex(mFiles, function(file) {
this.#currentIndex = _.findIndex(this.#files, function(file) {
// types int/string depend on the cloud version, don't use ===
return file.id == firstFileId;
});
};
}

this.next = function() {
return jumpToOffset(+1);
};
next() {
return this.#jumpToOffset(+1);
}

this.prev = function() {
return jumpToOffset(-1);
};
prev() {
return this.#jumpToOffset(-1);
}

this.jumpToIndex = function(index) {
if (index < mFiles.length) {
mCurrentIndex = index;
jumpToIndex(index) {
if (index < this.#files.length) {
this.#currentIndex = index;
}
return this.currentFile();
};
}

this.reset = function() {
mFiles = null;
mCurrentIndex = null;
};
reset() {
this.#files = null;
this.#currentIndex = null;
}

this.length = function() {
return mFiles ? mFiles.length : 0;
};
length() {
return this.#files ? this.#files.length : 0;
}

this.currentFile = function() {
return mFiles ? mFiles[mCurrentIndex] : null;
};
currentFile() {
return this.#files ? this.#files[this.#currentIndex] : null;
}

this.currentIndex = function() {
return mCurrentIndex;
};
currentIndex() {
return this.#currentIndex;
}

this.files = function() {
return mFiles;
};
files() {
return this.#files;
}
};
33 changes: 16 additions & 17 deletions js/embedded/playlistfileservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
* 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
*/

OCA.Music = OCA.Music || {};

OCA.Music.PlaylistFileService = function() {
/** @namespace */
OCA.Music.PlaylistFileService = class {

let mFileId = null;
let mData = null;
static #fileId = null;
static #data = null;

this.readFile = function(fileId, onSuccess, onFail, shareToken /*optional*/) {
static readFile(fileId, onSuccess, onFail, shareToken /*optional*/) {

if (fileId == mFileId && mData !== null) {
onSuccess(mData);
if (fileId == this.#fileId && this.#data !== null) {
onSuccess(this.#data);
}
else {
let url = null;
Expand All @@ -30,15 +31,15 @@ OCA.Music.PlaylistFileService = function() {
url = OC.generateUrl('apps/music/api/playlists/file/{fileId}', {'fileId': fileId});
}

$.get(url, function(data) {
mFileId = fileId;
mData = data;
$.get(url, (data) => {
this.#fileId = fileId;
this.#data = data;
onSuccess(data);
}).fail(onFail);
}
};
}

this.importPlaylist = function(file, onDone) {
static importPlaylist(file, onDone) {
let name = OCA.Music.Utils.dropFileExtension(file.name);
let path = OCA.Music.Utils.joinPath(file.path, file.name);

Expand Down Expand Up @@ -66,9 +67,9 @@ OCA.Music.PlaylistFileService = function() {
OC.Notification.showTemporary(t('music', 'Failed to create a new playlist'));
onDone(false);
});
};
}

this.importRadio = function(file, onDone) {
static importRadio(file, onDone) {
let path = OCA.Music.Utils.joinPath(file.path, file.name);

let url = OC.generateUrl('apps/music/api/radio/import');
Expand All @@ -85,8 +86,6 @@ OCA.Music.PlaylistFileService = function() {
t('music', 'Failed to import the playlist file {file}', { file: file.name }));
onDone(false);
});
};
}

};

OCA.Music.playlistFileService = new OCA.Music.PlaylistFileService();
2 changes: 1 addition & 1 deletion js/embedded/playlisttabview.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ OCA.Music.initPlaylistTabView = function(playlistMimes) {
container.append($(document.createElement('p')).text(t('music', 'Error reading playlist file')));
};

OCA.Music.playlistFileService.readFile(fileInfo.id, onPlaylistLoaded, onError);
OCA.Music.PlaylistFileService.readFile(fileInfo.id, onPlaylistLoaded, onError);
}
},

Expand Down

0 comments on commit 4dc4008

Please sign in to comment.