Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active January 19, 2016 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavafomin/6b4fae3e6ef5aad0c8f7 to your computer and use it in GitHub Desktop.
Save slavafomin/6b4fae3e6ef5aad0c8f7 to your computer and use it in GitHub Desktop.
Angular.js Directive for Vimeo Player

Angular.js Directive for Vimeo Player

Usage

In your controller

$scope.video = {
  properties: {
    autoplay: true,
    api: true
  },
  events: {
    ready: function () {
      this.api('setVolume', 1.0);
      this.api('getDuration', function (duration) {
        $scope.duration = Math.floor(duration / 60);
      });
    },
    finish: function () {
      $scope.finished = true;
    }
  }
};

In your view

<bs-vimeo-player
    video-id="42615527"
    properties="video.properties"
    events="video.events"
></bs-vimeo-player>
(function (angular, $f) {
'use strict';
angular.module('betsol.directive.vimeoPlayer', [])
.directive('bsVimeoPlayer', function ($timeout) {
return {
restrict: 'E',
templateUrl: 'vimeo-player.html',
scope: {
videoId: '@',
properties: '=',
events: '='
},
link: function ($scope, $directiveElement) {
if (!$f) {
// Froogaloop is not available.
return;
}
if (!$scope.properties.api || !$scope.events) {
// API is not enabled on the player or events are not specified.
return;
}
var frameElement = $directiveElement.find('iframe')[0];
var player = $f(frameElement);
player.addEvent('ready', function () {
var eventArgs = arguments;
if ('function' === typeof $scope.events.ready) {
// Calling "ready" event.
$timeout(function () {
$scope.events.ready.call(player, eventArgs);
});
}
angular.forEach($scope.events, function (eventHandler, eventName) {
if ('ready' == eventName) {
return;
}
if ('function' !== typeof eventHandler) {
return;
}
player.addEvent(eventName, function () {
var eventArgs = arguments;
// Calling custom event.
$timeout(function () {
$scope.events[eventName].call(player, eventArgs);
});
});
});
});
},
controller: function (
$scope,
$sce,
$httpParamSerializer
) {
$scope.frameSrc = $sce.trustAsResourceUrl(
'//player.vimeo.com/video/' + $scope.videoId +
'?' + $httpParamSerializer($scope.properties)
);
}
};
})
;
})(angular, window.Froogaloop || undefined);
<iframe
ng-src="{~ frameSrc ~}"
frameborder="0"
webkitallowfullscreen
mozallowfullscreen
allowfullscreen
></iframe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment