-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrate.js
276 lines (239 loc) · 9.09 KB
/
integrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright 2016-2018 Jiří Janoušek <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
(function (Nuvola) {
const DEFAULT_ADDRESS = 'http://localhost/owncloud/index.php/apps/music/'
const ADDRESS = 'app.address'
const ADDRESS_TYPE = 'app.address_type'
const ADDRESS_DEFAULT = 'default'
const ADDRESS_CUSTOM = 'custom'
// Translations
const _ = Nuvola.Translate.gettext
// Create media player component
const player = Nuvola.$object(Nuvola.MediaPlayer)
// Handy aliases
const PlaybackState = Nuvola.PlaybackState
const PlayerAction = Nuvola.PlayerAction
// Create new WebApp prototype
const WebApp = Nuvola.$WebApp()
WebApp._onInitAppRunner = function (emitter) {
Nuvola.WebApp._onInitAppRunner.call(this, emitter)
Nuvola.config.setDefault(ADDRESS_TYPE, ADDRESS_DEFAULT)
Nuvola.config.setDefault(ADDRESS, DEFAULT_ADDRESS)
Nuvola.core.connect('InitializationForm', this)
Nuvola.core.connect('PreferencesForm', this)
}
WebApp._onInitializationForm = function (emitter, values, entries) {
if (!Nuvola.config.hasKey(ADDRESS_TYPE)) {
this.appendPreferences(values, entries)
}
}
WebApp._onPreferencesForm = function (emitter, values, entries) {
this.appendPreferences(values, entries)
}
WebApp.appendPreferences = function (values, entries) {
values[ADDRESS_TYPE] = Nuvola.config.get(ADDRESS_TYPE)
values[ADDRESS] = Nuvola.config.get(ADDRESS)
entries.push(['header', _('ownCloud Music')])
entries.push(['label', _('Specify the address of your ownCloud Music server')])
entries.push(['option', ADDRESS_TYPE, ADDRESS_DEFAULT,
_('Default adress:') + ' ' + DEFAULT_ADDRESS, null, [ADDRESS]])
entries.push(['option', ADDRESS_TYPE, ADDRESS_CUSTOM,
_('Custom address:'), [ADDRESS], null])
entries.push(['string', ADDRESS])
}
WebApp._onHomePageRequest = function (emitter, result) {
result.url = (Nuvola.config.get(ADDRESS_TYPE) === ADDRESS_CUSTOM)
? Nuvola.config.get(ADDRESS)
: DEFAULT_ADDRESS
}
// Initialization routines
WebApp._onInitWebWorker = function (emitter) {
Nuvola.WebApp._onInitWebWorker.call(this, emitter)
const state = document.readyState
if (state === 'interactive' || state === 'complete') {
this._onPageReady()
} else {
document.addEventListener('DOMContentLoaded', this._onPageReady.bind(this))
}
}
// Page is ready for magic
WebApp._onPageReady = function () {
// Connect handler for signal ActionActivated
Nuvola.actions.connect('ActionActivated', this)
// Start update routine
this.albumArt = { url: null, data: null }
this.update()
}
// Extract data from the web page
WebApp.update = function () {
let state = PlaybackState.UNKNOWN
const track = {
title: null,
artist: null,
album: null,
artLocation: null
}
const controls = document.getElementById('controls')
if (controls && controls.classList.contains('started')) {
try {
if (this.getButtonEnabled(1)) {
state = PlaybackState.PAUSED
} else if (this.getButtonEnabled(2)) {
state = PlaybackState.PLAYING
}
track.title = controls.querySelector('.song-info .title').title
track.artist = controls.querySelector('.song-info .artist').title
let albumArt = controls.querySelector('.albumart')
track.album = albumArt.title
albumArt = albumArt.getAttribute('cover')
if (albumArt) {
if (this.albumArt.url === albumArt) {
track.artLocation = this.albumArt.data
} else {
this._downloadAlbumArt(albumArt)
}
} else {
track.artLocation = null
}
} catch (e) {
Nuvola.log('Parsing error: {1}', e)
}
}
const time = this._getTrackTime()
track.length = time ? time[1] : null
this.state = state
player.setPlaybackState(state)
player.setTrack(track)
player.setCanGoPrev(state !== PlaybackState.UNKNOWN)
player.setCanGoNext(state !== PlaybackState.UNKNOWN)
player.setCanPlay(this.getButtonEnabled(1))
player.setCanPause(this.getButtonEnabled(2))
player.setCanSeek(!!time)
player.setTrackPosition(time ? time[0] : null)
const elm = this._getVolumeSlider()
player.updateVolume(elm ? elm.value / 100 || null : null)
player.setCanChangeVolume(!!elm)
const repeat = this._getRepeat()
player.setCanRepeat(repeat !== null)
player.setRepeatState(repeat)
const shuffle = this._getShuffle()
player.setCanShuffle(shuffle !== null)
player.setShuffleState(shuffle)
// Schedule the next update
setTimeout(this.update.bind(this), 500)
}
WebApp._downloadAlbumArt = function (url) {
this.albumArt.url = url
this.albumArt.data = null
Nuvola.exportImageAsBase64(url, (data) => {
if (this.albumArt.url === url) {
this.albumArt.data = data
}
})
}
WebApp.getAbsoluteURL = function (path) {
const port = window.location.port ? window.location.port : (window.location.protocol === 'https:' ? '443' : '80')
return Nuvola.format('{1}//{2}:{3}{4}', window.location.protocol, window.location.hostname, port, path || '/')
}
WebApp.getButton = function (index) {
const buttons = document.querySelectorAll('#controls.started #play-controls img')
return buttons.length ? buttons[index] : null
}
WebApp._getTrackTime = function () {
const elm = document.querySelector('#controls.started .progress-info span')
return elm ? elm.textContent.split('/') : null
}
WebApp._getVolumeSlider = function () {
return document.querySelector('#controls.started .volume-control input.volume-slider')
}
WebApp.getButtonEnabled = function (index) {
const button = this.getButton(index)
return button ? !button.classList.contains('ng-hide') : false
}
WebApp._getRepeatButton = function () {
return document.getElementById('repeat')
}
WebApp._getRepeat = function () {
const button = this._getRepeatButton()
if (!button) {
return null
}
return button.classList.contains('active') ? Nuvola.PlayerRepeat.PLAYLIST : Nuvola.PlayerRepeat.NONE
}
WebApp._setRepeat = function (repeat) {
while (repeat !== Nuvola.PlayerRepeat.TRACK && this._getRepeat() !== repeat) {
Nuvola.clickOnElement(this._getRepeatButton())
}
}
WebApp._getShuffleButton = function () {
return document.getElementById('shuffle')
}
WebApp._getShuffle = function () {
const button = this._getShuffleButton()
return button ? button.classList.contains('active') : null
}
// Handler of playback actions
WebApp._onActionActivated = function (emitter, name, param) {
switch (name) {
case PlayerAction.TOGGLE_PLAY:
case PlayerAction.PLAY:
case PlayerAction.PAUSE:
case PlayerAction.STOP:
Nuvola.clickOnElement(this.getButton(1))
break
case PlayerAction.PREV_SONG:
Nuvola.clickOnElement(this.getButton(0))
break
case PlayerAction.NEXT_SONG:
Nuvola.clickOnElement(this.getButton(3))
break
case PlayerAction.SEEK: {
const elm = document.querySelector('#controls.started .progress-info .seek-bar')
const time = this._getTrackTime()
if (elm && time) {
const total = Nuvola.parseTimeUsec(time[1])
if (param > 0 && param <= total) {
Nuvola.clickOnElement(elm, param / total, 0.5)
}
}
break
}
case PlayerAction.CHANGE_VOLUME: {
const volume = this._getVolumeSlider()
if (volume) {
Nuvola.setInputValueWithEvent(volume, 100 * param)
}
break
}
case PlayerAction.REPEAT:
this._setRepeat(param)
break
case PlayerAction.SHUFFLE:
Nuvola.clickOnElement(this._getShuffleButton())
break
}
}
WebApp.start()
})(this) // function (Nuvola)