From 32e3d204e29570393c7714834e51b3b036782635 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 29 Dec 2017 00:26:10 +0400 Subject: [PATCH] Improved gif playback --- app/js/directives.js | 95 +++++++++++++++++++++++------- app/less/app.less | 8 +++ app/partials/desktop/full_gif.html | 4 +- app/partials/mobile/full_gif.html | 4 +- 4 files changed, 86 insertions(+), 25 deletions(-) diff --git a/app/js/directives.js b/app/js/directives.js index 430541c3..9b2e6930 100755 --- a/app/js/directives.js +++ b/app/js/directives.js @@ -2275,6 +2275,10 @@ angular.module('myApp.directives', ['myApp.filters']) }) .directive('myLoadGif', function (AppDocsManager, $timeout) { + + var currentPlayer = false + var currentPlayerScope = false + return { link: link, templateUrl: templateUrl('full_gif'), @@ -2283,23 +2287,51 @@ angular.module('myApp.directives', ['myApp.filters']) } } + function checkPlayer(newPlayer, newScope) { + if (currentPlayer === newPlayer) { + return false + } + if (currentPlayer) { + currentPlayer.pause() + currentPlayer.currentTime = 0 + currentPlayerScope.isActive = false + } + currentPlayer = newPlayer + currentPlayerScope = newScope + } + + function toggleVideoPlayer ($scope, element) { + var video = $('video', element)[0] + if (video) { + if (!$scope.isActive) { + video.pause() + video.currentTime = 0 + } else { + checkPlayer(video, $scope) + + var promise = video.play() + if (promise && promise.then) { + promise.then(function () { + $scope.needClick = false + }, function () { + $scope.needClick = true + }) + } + } + return video + } + return false + } + function link ($scope, element, attrs) { var imgWrap = $('.img_gif_image_wrap', element) imgWrap.css({width: $scope.document.thumb.width, height: $scope.document.thumb.height}) var downloadPromise = false + var peerChanged = false $scope.isActive = false - // Demo - // $scope.document.progress = {enabled: true, percent: 30} - // $timeout(function () { - // $scope.document.progress.percent = 60 - // }, 3000) - // $timeout(function () { - // $scope.document.progress.percent = 100 - // }, 10000) - $scope.toggle = function (e) { if (e && checkClick(e, true)) { AppDocsManager.saveDocFile($scope.document.id) @@ -2307,19 +2339,15 @@ angular.module('myApp.directives', ['myApp.filters']) } if ($scope.document.url) { + if ($scope.needClick) { + if (toggleVideoPlayer($scope, element)) { + return; + } + } + $scope.isActive = !$scope.isActive onContentLoaded(function () { - $scope.isActive = !$scope.isActive $scope.$emit('ui_height') - - var video = $('video', element)[0] - if (video) { - if (!$scope.isActive) { - video.pause() - video.currentTime = 0 - } else { - video.play() - } - } + toggleVideoPlayer($scope, element) }) return } @@ -2330,14 +2358,39 @@ angular.module('myApp.directives', ['myApp.filters']) return } + peerChanged = false downloadPromise = AppDocsManager.downloadDoc($scope.document.id) downloadPromise.then(function () { $timeout(function () { - $scope.isActive = true + if (!peerChanged) { + $scope.isActive = true + } + var video = toggleVideoPlayer($scope, element) + if (video) { + $(video).on('ended', function () { + if ($scope.isActive) { + $scope.toggle() + } + }) + } }, 200) }) } + + $scope.$on('ui_history_change', function () { + if ($scope.isActive) { + $scope.toggle() + } + peerChanged = true + }) + + $scope.$on('$destroy', function () { + if (downloadPromise) { + downloadPromise.cancel() + downloadPromise = false + } + }) } }) diff --git a/app/less/app.less b/app/less/app.less index f85a70a9..37c9bb8b 100644 --- a/app/less/app.less +++ b/app/less/app.less @@ -1756,6 +1756,10 @@ div.im_message_video_thumb { .im_message_gif_wrap { position: relative; } +.img_gif_with_progress_wrap { + display: inline-block; + line-height: 0; +} .im_message_gif_btn { color: #fff; font-style: normal; @@ -4200,6 +4204,10 @@ h5 { /* Round documents */ +.img_round_with_progress_wrap { + display: inline-block; + line-height: 0; +} .img_round_image_wrap { position: relative; overflow: hidden; diff --git a/app/partials/desktop/full_gif.html b/app/partials/desktop/full_gif.html index d563bd5d..0c24eb24 100644 --- a/app/partials/desktop/full_gif.html +++ b/app/partials/desktop/full_gif.html @@ -2,7 +2,7 @@
-
+
@@ -14,7 +14,7 @@
-