Skip to content

Instantly share code, notes, and snippets.

@EricRohlfs
Last active February 29, 2016 18:08
Show Gist options
  • Save EricRohlfs/07f0ebef326d2bdb6bdd to your computer and use it in GitHub Desktop.
Save EricRohlfs/07f0ebef326d2bdb6bdd to your computer and use it in GitHub Desktop.
(function () {
/**
* Gist by @EricRohlfs for embeding yammer in an AngularJs SPA.
* Assumes you are using open graph and ng-route.
* Steps:
* 1. include the yammer api script as per their documentation
* <script type="text/javascript" src="https://assets.yammer.com/assets/platform_embed.js"></script>
* 2. Add the directives <yammer-embed/> or <yammer-follow/> to your page.
* 3. in your route config add the openGraph property for each route
* .when('/foo', {
templateUrl: 'foo.html',
controller: 'FooCtrl',
controllerAs: 'ctrl',
openGraph: {
title: 'My Foo Page '
}
})
* 4. Include the yammer.js file in your index.html page, just like any other directive.
* 5. Set your network in the getNetwork function.
* 6. You might want to make changes to the getTitle method.
*
*/
angular.module('yammer', []);
angular.module('yammer').service('commonSvc', [ '$document', function ($document) {
var getTitle = function (next) {
var title = " | Process Explorer";
//want to stop the timing errors
if (angular.isUndefined(next.$$route)) {
return title;
}
if (angular.isUndefined(next.$$route.openGraph)) {
return title;
}
if (angular.isDefined(next.$$route.openGraph)) {
title= next.$$route.openGraph.title + " | Process Explorer";
}
return title;
}
var emptyPrevious = function (btnId) {
var content = $document[0].getElementById(btnId);
if (content !== null) {
// ReSharper disable once QualifiedExpressionMaybeNull
content.innerHTML = "";
}
}
var getNetwork = function () {
return 'myWebSite.com';
}
return {
getTitle: getTitle,
emptyPrevious: emptyPrevious,
getNetwork: getNetwork
}
}
]);
angular.module('yammer').directive('yammerFollow', ['commonSvc', function (commonSvc) {
return {
restrict: 'E',
template: '<div id="yammer-follow-btn"></div>',
link: function ($scope) {
var templateId = 'yammer-like-btn';
$scope.$on('$routeChangeSuccess', function () {
commonSvc.emptyPrevious(templateId);
// ReSharper disable once UseOfImplicitGlobalInFunctionScope
yam.connect.actionButton({
container: '#' + templateId,
network: commonSvc.getNetwork(),
action: "follow",
feedType: "open-graph",
feedId: "7238124"
});
});
}
}
}]
);
angular.module('yammer').directive('yammerEmbed', ['$location', 'commonSvc', function ($location, commonSvc) {
return {
restrict: 'E',
template: '<div id="yammer-embedded-feed" style="height: 500px;"></div>',
link: function ($scope) {
var templateId = 'yammer-embedded-feed';
$scope.$on('$routeChangeSuccess', function (event, next) {
commonSvc.emptyPrevious(templateId);
var objectProperties = {
url: $location.absUrl(),
type: 'page',
title: commonSvc.getTitle(next),
image: ""
}
//update with new graph info
// ReSharper disable once UseOfImplicitGlobalInFunctionScope
yam.connect.embedFeed({
container: "#" + templateId,
network: commonSvc.getNetwork(),
feedType: "open-graph",
feedId: "7238124",
objectProperties: objectProperties
});
});
}
}
}]
);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment