Guest User

Untitled

a guest
Sep 16th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('multiVideo',[])
  4. .directive('multiVideo', function ($compile,$sce,$rootScope,$interval,$window) {
  5.  
  6. var templateWrapperProgressBar = '<div class="progress-wrapper">'+
  7. '<div class="progress-overlay"></div>'+
  8. '</div>';
  9.  
  10. var templateProgressBar = '<div class="progress-play" ></div>'+
  11. '<div round-progress max="100"'+
  12. 'current="progress" color="#45ccce"'+
  13. 'bgcolor="#eaeaea" radius="70" stroke="5"'+
  14. 'semi="false" rounded="false"'+
  15. 'clockwise="true" responsive="true"'+
  16. 'iterations="100" animation="easeInSine"'+
  17. 'class="pointer" ng-click="clickProgress()">'+
  18. '</div>';
  19.  
  20. var templateAnguVideo = '<div anguvideo ng-model="src"></div>';
  21. var templateClappr = '<clappr src="src"></clappr>';
  22. var templateLightBox = '<angular-master-lightbox images="src">' +
  23. '</angular-master-lightbox>';
  24.  
  25. var switchDirectives = function(val){
  26. var htmlDirective;
  27. var extension = val.substring(val.length - 4);
  28.  
  29. if( extension === ".mp4"){
  30. htmlDirective = templateClappr;
  31. }
  32. else if(extension === ".jpg" || extension === ".png" || extension === ".gif"){
  33. htmlDirective = templateLightBox;
  34. }
  35. else{
  36. htmlDirective = templateAnguVideo;
  37. }
  38.  
  39. return htmlDirective;
  40. }
  41.  
  42. return {
  43. restrict: 'E',
  44. transclude: true,
  45. scope:{
  46. src: '=src',
  47. clickProgress: '&',
  48. watchedMinPercentage: '&',
  49. allowEmmitWatchedMinPercentageEvent: '=',
  50. automaticNextVideo: '=automaticNextVideo'
  51. },
  52. link: function(scope,element,attrs,ctrl,transclude){
  53.  
  54. scope.interval = null;
  55. scope.$watch('src', watchScopeSrc(element,scope));
  56. scope.$on("multiVideo:finishVideo", actionMultiVideoFinish(element,transclude,scope));
  57. var multiVideoFinish = function(){
  58. return $rootScope.$broadcast("multiVideo:finishVideo");
  59. };
  60. var multiVideoWatchedMinPercentage = function(){
  61. if(scope.allowEmmitWatchedMinPercentageEvent){
  62. scope.watchedMinPercentage();
  63. }
  64. };
  65. $window.addEventListener('visibilitychange',callbackVisibilitychange);
  66.  
  67. function callbackVisibilitychange() {
  68. if(document.hidden){
  69. scope.$broadcast("pause");
  70. }else{
  71. scope.$broadcast("play");
  72. }
  73. }
  74. scope.$on("clappr:finishVideo", multiVideoFinish);
  75. scope.$on("anguvideo:finishVideo", multiVideoFinish);
  76. scope.$on("$destroy", function() {
  77. $window.RemoveEventListener('visibilitychange',callbackVisibilitychange);
  78. });
  79. element.$on("$destroy", function() {
  80. $window.RemoveEventListener('visibilitychange',callbackVisibilitychange);
  81. });
  82. scope.$on("clappr:watchedMinPercentage", multiVideoWatchedMinPercentage);
  83. scope.$on("anguvideo:watchedMinPercentage", multiVideoWatchedMinPercentage)
  84. }
  85.  
  86. };
  87.  
  88. function clearIntervalProgressBar(scope){
  89. return function(){
  90. $interval.cancel(scope.interval);
  91. scope.interval = undefined;
  92. scope.progress = 0;
  93.  
  94.  
  95. var mediaElement = angular.element('video')[0];
  96. if(mediaElement){
  97. mediaElement.pause();
  98. mediaElement.src='';
  99. }
  100.  
  101. return false;
  102. }
  103. }
  104.  
  105. function watchScopeSrc(element,scope) {
  106. return function(newVal){
  107. if (!newVal)
  108. return;
  109.  
  110. $(".progress-wrapper").hide();
  111. clearIntervalProgressBar(scope)();
  112. element.html(switchDirectives(newVal)).show();
  113. $compile(element.contents())(scope);
  114. }
  115.  
  116. }
  117.  
  118. function incrementCurrentProgress(scope){
  119. return function(){
  120. scope.progress += 0.5;
  121. if(scope.progress >= 100){
  122. $rootScope.$broadcast("multiVideo:finishProgressbar");
  123. clearIntervalProgressBar(scope)();
  124. return false;
  125. }
  126. }
  127. }
  128.  
  129. function actionMultiVideoFinish(element,transclude,scope) {
  130. return function(){
  131.  
  132. element.html(templateWrapperProgressBar);
  133.  
  134. if(scope.automaticNextVideo){
  135. angular.element(".progress-overlay").append(templateProgressBar);
  136. scope.progress = 0;
  137. scope.interval = $interval(incrementCurrentProgress(scope), 50);
  138. }
  139. angular.element(".progress-overlay").after(transclude());
  140. $compile(element.contents())(scope);
  141. $(".progress-wrapper").show();
  142. }
  143. }
  144.  
  145. });
Advertisement
Add Comment
Please, Sign In to add comment