Source: home.js

  1. "use strict";
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. // myMPD (c) 2018-2025 Juergen Mang <mail@jcgames.de>
  4. // https://github.com/jcorporation/mympd
  5. /** @module home_js */
  6. /**
  7. * Adds a link to a view to the homescreen
  8. * @returns {void}
  9. */
  10. //eslint-disable-next-line no-unused-vars
  11. function addViewToHome() {
  12. _addHomeIcon('appGoto', '', 'preview', '', [app.current.card, app.current.tab, app.current.view,
  13. app.current.offset, app.current.limit, app.current.filter, app.current.sort, app.current.tag, app.current.search]);
  14. }
  15. /**
  16. * Adds a modal shortcut to the homescreen
  17. * @returns {void}
  18. */
  19. //eslint-disable-next-line no-unused-vars
  20. function addOpenModalToHome() {
  21. _addHomeIcon('openModal', '', 'web_asset', '', []);
  22. }
  23. /**
  24. * Adds a external link to the homescreen
  25. * @returns {void}
  26. */
  27. //eslint-disable-next-line no-unused-vars
  28. function addExternalLinkToHome() {
  29. _addHomeIcon('openExternalLink', '', 'link', '', []);
  30. }
  31. /**
  32. * Adds a script to the homescreen
  33. * @param {string} name name for the home icon
  34. * @param {object} script script object
  35. * @returns {void}
  36. */
  37. //eslint-disable-next-line no-unused-vars
  38. function addScriptToHome(name, script) {
  39. const options = [script.script, script.arguments.join(',')];
  40. _addHomeIcon('execScriptFromOptions', name, 'code', '', options);
  41. }
  42. /**
  43. * Adds a playlist to the homescreen
  44. * @param {string} uri uri of the playlist
  45. * @param {string} type one of plist, smartpls
  46. * @param {string} name name for the home icon
  47. * @returns {void}
  48. */
  49. //eslint-disable-next-line no-unused-vars
  50. function addPlistToHome(uri, type, name) {
  51. _addHomeIcon('replaceQueue', name, 'list', '', [type, uri]);
  52. }
  53. /**
  54. * Adds a webradio favorite to the homescreen
  55. * @param {string} uri uri of the webradio favorite
  56. * @param {string} type must be webradio
  57. * @param {string} name name for the home icon
  58. * @param {string} image image for the home icon
  59. * @returns {void}
  60. */
  61. //eslint-disable-next-line no-unused-vars
  62. function addRadioFavoriteToHome(uri, type, name, image) {
  63. _addHomeIcon('replaceQueue', name, '', image, [type, uri]);
  64. }
  65. /**
  66. * Adds a webradioDB entry to the homescreen
  67. * @param {string} uri uri of the stream
  68. * @param {string} type must be stream
  69. * @param {string} name name for the home icon
  70. * @param {string} image image for the home icon
  71. * @returns {void}
  72. */
  73. //eslint-disable-next-line no-unused-vars
  74. function addWebRadiodbToHome(uri, type, name, image) {
  75. _addHomeIcon('replaceQueue', name, '', image, [type, uri]);
  76. }
  77. /**
  78. * Adds a directory to the homescreen
  79. * @param {string} uri directory uri
  80. * @param {string} name name for the home icon
  81. * @returns {void}
  82. */
  83. //eslint-disable-next-line no-unused-vars
  84. function addDirToHome(uri, name) {
  85. if(uri === undefined) {
  86. uri = app.current.filter;
  87. name = basename(app.current.filter, false);
  88. }
  89. _addHomeIcon('replaceQueue', name, 'folder_open', '', ['dir', uri]);
  90. }
  91. /**
  92. * Adds the current directory search to the homescreen
  93. * @returns {void}
  94. */
  95. //eslint-disable-next-line no-unused-vars
  96. function addSearchDirToHome() {
  97. if (app.current.search === '') {
  98. // Search is empty, add a dir to home screen
  99. return addDirToHome(undefined, undefined);
  100. }
  101. // Add a saved search to home screen
  102. const expression = createBaseSearchExpression(app.current.filter, app.current.search);
  103. const name = basename(app.current.filter, false) + ' (' + app.current.search + ')';
  104. _addHomeIcon('replaceQueue', name, 'saved_search', '', ['search', expression]);
  105. }
  106. /**
  107. * Adds a song or stream to the homescreen
  108. * @param {string} uri song or stream uri
  109. * @param {string} type one of song, stream
  110. * @param {string} name name for the home icon
  111. * @returns {void}
  112. */
  113. //eslint-disable-next-line no-unused-vars
  114. function addSongToHome(uri, type, name) {
  115. const ligature = type === 'song'
  116. ? 'music_note'
  117. : 'stream';
  118. _addHomeIcon('replaceQueue', name, ligature, '', [type, uri]);
  119. }
  120. /**
  121. * Adds the current search to the homescreen
  122. * @returns {void}
  123. */
  124. //eslint-disable-next-line no-unused-vars
  125. function addSearchToHome() {
  126. _addHomeIcon('replaceQueue', tn('Current search'), 'saved_search', '', ['search', app.current.search, app.current.sort.tag, app.current.sort.desc]);
  127. }
  128. /**
  129. * Adds an album to the homescreen
  130. * @param {string} albumId the albumid
  131. * @param {string} name name for the home icon
  132. * @param {string} image image for the home icon
  133. * @returns {void}
  134. */
  135. //eslint-disable-next-line no-unused-vars
  136. function addAlbumToHome(albumId, name, image) {
  137. if (image === '') {
  138. _addHomeIcon('replaceQueue', name, 'album', '', ['album', albumId]);
  139. }
  140. else {
  141. _addHomeIcon('replaceQueue', name, '', image, ['album', albumId]);
  142. }
  143. }
  144. /**
  145. * Adds a new stream to the homescreen
  146. * @returns {void}
  147. */
  148. //eslint-disable-next-line no-unused-vars
  149. function addStreamToHome() {
  150. const action = getRadioBoxValueId('modalPlaylistAddToPos') + 'Queue';
  151. const uri = elGetById('modalPlaylistAddToUrisInput').value;
  152. _addHomeIcon(action, '', 'stream', '', ['stream', uri]);
  153. }
  154. /**
  155. * Duplicates a home icon
  156. * @param {number} pos home icon position
  157. * @returns {void}
  158. */
  159. //eslint-disable-next-line no-unused-vars
  160. function duplicateHomeIcon(pos) {
  161. _editHomeIcon(pos, false, "Duplicate home icon");
  162. }
  163. /**
  164. * Opens the edit home icon dialog
  165. * @param {number} pos home icon position
  166. * @returns {void}
  167. */
  168. //eslint-disable-next-line no-unused-vars
  169. function editHomeIcon(pos) {
  170. _editHomeIcon(pos, true, "Edit home icon");
  171. }
  172. /**
  173. * Deletes a home icon
  174. * @param {number} pos home icon position
  175. * @returns {void}
  176. */
  177. //eslint-disable-next-line no-unused-vars
  178. function deleteHomeIcon(pos) {
  179. sendAPI("MYMPD_API_HOME_ICON_RM", {"pos": pos}, null, false);
  180. }
  181. /**
  182. * Executes the home icon action
  183. * @param {number} pos home icon position
  184. * @returns {void}
  185. */
  186. //eslint-disable-next-line no-unused-vars
  187. function executeHomeIcon(pos) {
  188. const el = elGetById('HomeList').children[pos].firstChild;
  189. parseCmd(null, getData(el, 'href'));
  190. }
  191. /**
  192. * Executes the home icon action
  193. * @param {number} pos home icon position
  194. * @returns {void}
  195. */
  196. //eslint-disable-next-line no-unused-vars
  197. function refreshHomeWidget(pos) {
  198. const el = elGetById('HomeList').children[pos].firstChild;
  199. updateHomeWidget(el);
  200. }
  201. /**
  202. * Opens the link in a new window
  203. * @param {string} link uri to open
  204. * @returns {void}
  205. */
  206. //eslint-disable-next-line no-unused-vars
  207. function openExternalLink(link) {
  208. window.open(link);
  209. }
  210. /**
  211. * Goto handler for home icons
  212. * @param {string} type one of dir, search, album, plist, smartpls
  213. * @param {Array} options type = search: search expression, sort, sortdec
  214. * type = album: albumId,
  215. * else uri of directory or playlist
  216. * @returns {void}
  217. */
  218. //eslint-disable-next-line no-unused-vars
  219. function homeIconGoto(type, options) {
  220. switch(type) {
  221. case 'dir':
  222. gotoFilesystem(options[0], type);
  223. break;
  224. case 'search':
  225. elGetById('SearchSearchStr').value = '';
  226. appGoto('Search', undefined, undefined, 0, undefined, 'any', {'tag': options[1], 'desc': options[2]}, '', options[0]);
  227. break;
  228. case 'album':
  229. //uri = AlbumId
  230. gotoAlbum(options[0]);
  231. break;
  232. case 'plist':
  233. case 'smartpls':
  234. gotoPlaylist(options[0]);
  235. break;
  236. default:
  237. logError('Invalid type: ' + type);
  238. }
  239. }