From e5d73346628c2acc0634f42c72766bea3aa814d5 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 16 Jun 2019 21:45:21 +0200 Subject: [PATCH] New media webplugin WIP --- .../http/static/css/source/common/vars.scss | 1 + .../webpanel/plugins/media/devices.scss | 15 +++++ .../source/webpanel/plugins/media/index.scss | 18 ++++++ .../webpanel/plugins/media/results.scss | 1 + .../source/webpanel/plugins/media/vars.scss | 2 + .../http/static/js/plugins/media/controls.js | 2 +- .../http/static/js/plugins/media/devices.js | 25 ++++++++- .../static/js/plugins/media/handlers/file.js | 26 +++++++-- .../js/plugins/media/handlers/torrent.js | 25 +++++++-- .../js/plugins/media/handlers/youtube.js | 27 +++++++-- .../http/static/js/plugins/media/index.js | 25 ++++++--- .../http/static/js/plugins/media/item.js | 17 ++++++ .../http/static/js/plugins/media/results.js | 55 +++++++++++++++++++ .../http/static/js/plugins/media/search.js | 1 + .../http/templates/plugins/media/devices.html | 8 ++- .../http/templates/plugins/media/index.html | 5 +- .../http/templates/plugins/media/item.html | 6 +- .../http/templates/plugins/media/results.html | 12 +++- .../http/templates/plugins/media/search.html | 5 +- platypush/plugins/media/__init__.py | 6 +- 20 files changed, 250 insertions(+), 32 deletions(-) diff --git a/platypush/backend/http/static/css/source/common/vars.scss b/platypush/backend/http/static/css/source/common/vars.scss index beb900268..ae0c0c901 100644 --- a/platypush/backend/http/static/css/source/common/vars.scss +++ b/platypush/backend/http/static/css/source/common/vars.scss @@ -10,6 +10,7 @@ $default-fg-3: #888888 !default; $default-font-size: 1.5rem !default; $default-shadow: 2px 2px 2px #ccc !default; $default-hover-fg: #35b870 !default; +$default-hover-fg-2: #38cf80 !default; $default-font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif !default; $default-border: 1px solid #e1e4e8 !default; diff --git a/platypush/backend/http/static/css/source/webpanel/plugins/media/devices.scss b/platypush/backend/http/static/css/source/webpanel/plugins/media/devices.scss index de8a1dddc..186c7a83b 100644 --- a/platypush/backend/http/static/css/source/webpanel/plugins/media/devices.scss +++ b/platypush/backend/http/static/css/source/webpanel/plugins/media/devices.scss @@ -4,6 +4,16 @@ button { padding: .5rem; + margin-right: .5rem; + + &.selected { + background: initial; + color: $default-hover-fg; + + &:hover { + color: $default-hover-fg-2; + } + } } .dropdown { @@ -12,6 +22,11 @@ align-items: center; cursor: pointer; + .text { + text-align: left; + margin-left: 2rem; + } + &:hover { background: $hover-bg } diff --git a/platypush/backend/http/static/css/source/webpanel/plugins/media/index.scss b/platypush/backend/http/static/css/source/webpanel/plugins/media/index.scss index b75bfdb91..fb81dc59b 100644 --- a/platypush/backend/http/static/css/source/webpanel/plugins/media/index.scss +++ b/platypush/backend/http/static/css/source/webpanel/plugins/media/index.scss @@ -15,6 +15,24 @@ height: inherit; letter-spacing: .03rem; + .dropdown { + z-index: $devices-dropdown-z-index; + + .item { + display: flex; + align-item: center; + cursor: pointer; + + .text { + margin-left: 1rem; + } + + &:hover { + background: $hover-bg; + } + } + } + input[type=text] { width: 100%; diff --git a/platypush/backend/http/static/css/source/webpanel/plugins/media/results.scss b/platypush/backend/http/static/css/source/webpanel/plugins/media/results.scss index 981a1a742..d72d2edf0 100644 --- a/platypush/backend/http/static/css/source/webpanel/plugins/media/results.scss +++ b/platypush/backend/http/static/css/source/webpanel/plugins/media/results.scss @@ -1,6 +1,7 @@ .media-plugin { .results { @include calc(height, '100% - 16rem'); + position: relative; // For the dropdown menu overflow: auto; .empty { diff --git a/platypush/backend/http/static/css/source/webpanel/plugins/media/vars.scss b/platypush/backend/http/static/css/source/webpanel/plugins/media/vars.scss index 0edcb4003..32d027254 100644 --- a/platypush/backend/http/static/css/source/webpanel/plugins/media/vars.scss +++ b/platypush/backend/http/static/css/source/webpanel/plugins/media/vars.scss @@ -9,3 +9,5 @@ $control-time-color: #666; $empty-results-color: #506050; +$devices-dropdown-z-index: 2; + diff --git a/platypush/backend/http/static/js/plugins/media/controls.js b/platypush/backend/http/static/js/plugins/media/controls.js index b7c7a3080..ce4ae69f2 100644 --- a/platypush/backend/http/static/js/plugins/media/controls.js +++ b/platypush/backend/http/static/js/plugins/media/controls.js @@ -5,7 +5,7 @@ Vue.component('media-controls', { item: { type: Object, default: () => {}, - } + }, }, methods: { diff --git a/platypush/backend/http/static/js/plugins/media/devices.js b/platypush/backend/http/static/js/plugins/media/devices.js index 387a069a4..52afa5d2e 100644 --- a/platypush/backend/http/static/js/plugins/media/devices.js +++ b/platypush/backend/http/static/js/plugins/media/devices.js @@ -2,11 +2,13 @@ Vue.component('media-devices', { template: '#tmpl-media-devices', props: { bus: { type: Object }, + playerPlugin: { type: String }, }, data: function() { return { showDevicesMenu: false, + selectedDevice: {}, }; }, @@ -14,26 +16,47 @@ Vue.component('media-devices', { dropdownItems: function() { var items = [ { - text: 'Local player', + name: this.playerPlugin, + text: this.playerPlugin, + type: 'local', icon: 'desktop', }, { + name: 'browser', text: 'Browser', + type: 'browser', icon: 'laptop', }, ]; + const self = this; + const onClick = (item) => { + return () => { + self.selectDevice(item); + }; + }; + + for (var i=0; i < items.length; i++) { + items[i].click = onClick(items[i]); + } + return items; }, }, methods: { + selectDevice: function(device) { + this.selectedDevice = device; + this.bus.$emit('selected-device', device); + }, + openDevicesMenu: function() { openDropdown(this.$refs.menu); }, }, created: function() { + this.selectDevice(this.dropdownItems.filter(_ => _.type === 'local')[0]); }, }); diff --git a/platypush/backend/http/static/js/plugins/media/handlers/file.js b/platypush/backend/http/static/js/plugins/media/handlers/file.js index 475f8a025..029e84471 100644 --- a/platypush/backend/http/static/js/plugins/media/handlers/file.js +++ b/platypush/backend/http/static/js/plugins/media/handlers/file.js @@ -1,8 +1,26 @@ mediaHandlers.file = { - icon: 'hdd', + iconClass: 'fa fa-hdd', - matchesUrl: function(url) { - return url.startsWith('file:///') || url.startsWith('/'); - }, + actions: [ + { + text: 'Play', + icon: 'play', + action: 'play', + }, + + { + text: 'Download', + icon: 'download', + action: function(item, bus) { + bus.$emit('download', item); + }, + }, + + { + text: 'View info', + icon: 'info', + action: 'info', + }, + ], }; diff --git a/platypush/backend/http/static/js/plugins/media/handlers/torrent.js b/platypush/backend/http/static/js/plugins/media/handlers/torrent.js index 43f9608cd..5787f89f2 100644 --- a/platypush/backend/http/static/js/plugins/media/handlers/torrent.js +++ b/platypush/backend/http/static/js/plugins/media/handlers/torrent.js @@ -1,8 +1,25 @@ mediaHandlers.torrent = { - icon: 'magnet', + iconClass: 'fa fa-magnet', - matchesUrl: function(url) { - return url.startsWith('magnet:?') || url.endsWith('.torrent'); - }, + actions: [ + { + text: 'Play', + icon: 'play', + action: 'play', + }, + + { + text: 'Download', + icon: 'download', + action: function(item) { + }, + }, + + { + text: 'View info', + icon: 'info', + action: 'info', + }, + ], }; diff --git a/platypush/backend/http/static/js/plugins/media/handlers/youtube.js b/platypush/backend/http/static/js/plugins/media/handlers/youtube.js index 820b5e357..bd33328e5 100644 --- a/platypush/backend/http/static/js/plugins/media/handlers/youtube.js +++ b/platypush/backend/http/static/js/plugins/media/handlers/youtube.js @@ -1,10 +1,25 @@ mediaHandlers.youtube = { - icon: 'youtube', + iconClass: 'fab fa-youtube', - matchesUrl: function(url) { - return url.startsWith('https://youtube.com/watch?v=') || - url.startsWith('https://www.youtube.com/watch?v=') || - url.startsWith('https://youtu.be/'); - }, + actions: [ + { + text: 'Play', + icon: 'play', + action: 'play', + }, + + { + text: 'Download', + icon: 'download', + action: function(item) { + }, + }, + + { + text: 'View info', + icon: 'info', + action: 'info', + }, + ], }; diff --git a/platypush/backend/http/static/js/plugins/media/index.js b/platypush/backend/http/static/js/plugins/media/index.js index 2ba800be8..edf3e3dd0 100644 --- a/platypush/backend/http/static/js/plugins/media/index.js +++ b/platypush/backend/http/static/js/plugins/media/index.js @@ -9,8 +9,10 @@ Vue.component('media', { bus: new Vue({}), results: [], currentItem: {}, + selectedDevice: undefined, loading: { results: false, + media: false, }, }; }, @@ -33,22 +35,31 @@ Vue.component('media', { this.loading.results = false; for (var i=0; i < results.length; i++) { - results[i].handler = {}; - - for (const hndl of Object.values(mediaHandlers)) { - if (hndl.matchesUrl(results[i].url)) { - results[i].handler = hndl; - } - } + results[i].handler = mediaHandlers[results[i].type]; } this.results = results; }, + + play: async function(item) { + }, + + info: function(item) { + // TODO + console.log(item); + }, + + selectDevice: function(device) { + this.selectedDevice = device; + }, }, created: function() { this.refresh(); + this.bus.$on('play', this.play); + this.bus.$on('info', this.info); + this.bus.$on('selected-device', this.selectDevice); this.bus.$on('results-loading', this.onResultsLoading); this.bus.$on('results-ready', this.onResultsReady); }, diff --git a/platypush/backend/http/static/js/plugins/media/item.js b/platypush/backend/http/static/js/plugins/media/item.js index 821062824..c4ab9c153 100644 --- a/platypush/backend/http/static/js/plugins/media/item.js +++ b/platypush/backend/http/static/js/plugins/media/item.js @@ -2,10 +2,27 @@ Vue.component('media-item', { template: '#tmpl-media-item', props: { bus: { type: Object }, + + selected: { + type: Boolean, + default: false, + }, + + active: { + type: Boolean, + default: false, + }, + item: { type: Object, default: () => {}, } }, + + methods: { + onClick: function(event) { + this.bus.$emit('result-clicked', this.item); + }, + }, }); diff --git a/platypush/backend/http/static/js/plugins/media/results.js b/platypush/backend/http/static/js/plugins/media/results.js index 6e714a710..74dde21e9 100644 --- a/platypush/backend/http/static/js/plugins/media/results.js +++ b/platypush/backend/http/static/js/plugins/media/results.js @@ -2,6 +2,10 @@ Vue.component('media-results', { template: '#tmpl-media-results', props: { bus: { type: Object }, + searching: { + type: Boolean, + default: false, + }, loading: { type: Boolean, default: false, @@ -12,7 +16,58 @@ Vue.component('media-results', { }, }, + data: function() { + return { + selectedItem: {}, + currentItem: {}, + }; + }, + + computed: { + mediaItemDropdownItems: function() { + if (!Object.keys(this.selectedItem).length) { + return []; + } + + const self = this; + + return this.selectedItem.handler.actions.map(action => { + return { + text: action.text, + icon: action.icon, + click: function() { + if (action.action instanceof Function) { + action.action(self.selectedItem, self.bus); + } else if (typeof(action.action) === 'string') { + self[action.action](self.selectedItem); + } + }, + }; + }); + }, + }, + methods: { + itemClicked: function(item) { + if (this.selectedItem.length && this.selectedItem.url === item.url) { + return; + } + + this.selectedItem = item; + openDropdown(this.$refs.mediaItemDropdown); + }, + + play: function(item) { + this.bus.$emit('play', item); + }, + + info: function(item) { + this.bus.$emit('info', item); + }, + }, + + created: function() { + this.bus.$on('result-clicked', this.itemClicked); }, }); diff --git a/platypush/backend/http/static/js/plugins/media/search.js b/platypush/backend/http/static/js/plugins/media/search.js index 09262bf3f..f3fd82ae9 100644 --- a/platypush/backend/http/static/js/plugins/media/search.js +++ b/platypush/backend/http/static/js/plugins/media/search.js @@ -3,6 +3,7 @@ Vue.component('media-search', { props: { bus: { type: Object }, supportedTypes: { type: Object }, + playerPlugin: { type: String }, }, data: function() { diff --git a/platypush/backend/http/templates/plugins/media/devices.html b/platypush/backend/http/templates/plugins/media/devices.html index 18a39fb87..0638b988e 100644 --- a/platypush/backend/http/templates/plugins/media/devices.html +++ b/platypush/backend/http/templates/plugins/media/devices.html @@ -2,8 +2,12 @@ diff --git a/platypush/backend/http/templates/plugins/media/results.html b/platypush/backend/http/templates/plugins/media/results.html index eb4546083..d46848953 100644 --- a/platypush/backend/http/templates/plugins/media/results.html +++ b/platypush/backend/http/templates/plugins/media/results.html @@ -2,17 +2,25 @@ diff --git a/platypush/backend/http/templates/plugins/media/search.html b/platypush/backend/http/templates/plugins/media/search.html index becb3352f..98ed8192b 100644 --- a/platypush/backend/http/templates/plugins/media/search.html +++ b/platypush/backend/http/templates/plugins/media/search.html @@ -20,7 +20,10 @@
- + +
diff --git a/platypush/plugins/media/__init__.py b/platypush/plugins/media/__init__.py index 3a2736302..1fcb5e050 100644 --- a/platypush/plugins/media/__init__.py +++ b/platypush/plugins/media/__init__.py @@ -176,7 +176,7 @@ class MediaPlugin(Plugin): if self._is_playing_torrent: try: get_plugin('media.webtorrent').quit() - except: + except Exception as e: self.logger.warning('Cannot quit the webtorrent instance: {}'. format(str(e))) @@ -305,9 +305,13 @@ class MediaPlugin(Plugin): format(query, media_type)) flattened_results = [] + for media_type in self._supported_media_types: if media_type in results: + for result in results[media_type]: + result['type'] = media_type flattened_results += results[media_type] + results = flattened_results if results: