|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 TopCoder Inc., All Rights Reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents the angular route,controller for facebook page |
| 7 | + * |
| 8 | + * @author TCDEVELOPER |
| 9 | + * @version 1.0 |
| 10 | + */ |
| 11 | + |
| 12 | +(function () { |
| 13 | + 'use strict'; |
| 14 | + |
| 15 | + |
| 16 | + var app = angular.module('app'); |
| 17 | + app.requires.push('ngCordovaOauth'); //use ng cordova oauth library for login |
| 18 | + |
| 19 | + |
| 20 | + angular.module('app') |
| 21 | + .config(function ($stateProvider) { |
| 22 | + $stateProvider |
| 23 | + .state('app.facebook', { |
| 24 | + url: '/facebook', |
| 25 | + views: { |
| 26 | + 'menuContent': { |
| 27 | + templateUrl: 'components/facebook/index.html', |
| 28 | + controller: 'FacebookCtrl as fb' |
| 29 | + } |
| 30 | + } |
| 31 | + }) |
| 32 | + .state('app.facebooktimeline', { |
| 33 | + url: '/facebooktimeline', |
| 34 | + views: { |
| 35 | + 'menuContent': { |
| 36 | + templateUrl: 'components/facebook/timeline.html', |
| 37 | + controller: 'FacebookPostCtrl' |
| 38 | + } |
| 39 | + } |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + //facebook config |
| 47 | + .constant('FB_CONFIG', { |
| 48 | + APP_ID: '888218901283961', |
| 49 | + APP_SECRET: '8fc9750c427ea1900c06d9b87246544e', |
| 50 | + API_ENDPOINT:'https://graph.facebook.com/v2.6', |
| 51 | + DEFAULT_MESSAGE :'I just tried the Topcoder Ionic StarterPack!', |
| 52 | + LINK : 'https://github.com/topcoderinc/Topcoder-StarterPack_API', |
| 53 | + IMAGE_URL:'https://www.topcoder.com/wp-content/uploads/2014/01/New-Logo-Stacked-Square-158x154.png' |
| 54 | + }) |
| 55 | + |
| 56 | + |
| 57 | + //defination of controller |
| 58 | + .controller("FacebookCtrl", function ($scope,$state, $http,$window,$timeout,$cordovaOauth,FB_CONFIG) { |
| 59 | + |
| 60 | + /* this controller controls the login flow */ |
| 61 | + |
| 62 | + var vm = this; |
| 63 | + |
| 64 | + /* |
| 65 | + perform the oauth |
| 66 | + */ |
| 67 | + vm.login = function() { |
| 68 | + $cordovaOauth.facebook(FB_CONFIG.APP_ID, ["email", "public_profile", "user_website", "user_location", "user_relationships","user_posts","user_photos","publish_actions"]).then(function(result) { |
| 69 | + $window.localStorage['fb_access_token'] = result.access_token; //store the access token |
| 70 | + $scope.showInfo('Login Success'); |
| 71 | + |
| 72 | + //change the state |
| 73 | + $state.go('app.facebooktimeline'); |
| 74 | + |
| 75 | + }, function(error) { |
| 76 | + $scope.showError('There was problem in login.') |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + //if logged in in start from the timeline page where user can post the data to the timeline |
| 81 | + if($window.localStorage['fb_access_token'] && $window.localStorage['fb_access_token']!=''){ |
| 82 | + $state.go('app.facebooktimeline'); |
| 83 | + } |
| 84 | + |
| 85 | + }) |
| 86 | + |
| 87 | + .controller("FacebookPostCtrl", function ($scope,$state, $http,$window,$ionicModal,FB_CONFIG) { |
| 88 | + |
| 89 | + /* |
| 90 | + THIS controller is used in timeline page contains 2 methods one post to facebook feed/timeline & seconfdmentod logs out from the facebook session |
| 91 | + */ |
| 92 | + |
| 93 | + $scope.post=FB_CONFIG.DEFAULT_MESSAGE; |
| 94 | + console.log(FB_CONFIG.DEFAULT_MESSAGE); |
| 95 | + |
| 96 | + /* |
| 97 | + check if we are logged in else go back to page to ask the user to login */ |
| 98 | + |
| 99 | + if(!$window.localStorage['fb_access_token'] || $window.localStorage['fb_access_token']==''){ |
| 100 | + $state.go('app.facebook'); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /* |
| 105 | + Post the message to facebook |
| 106 | + */ |
| 107 | + $scope.postFeed = function(form) { |
| 108 | + |
| 109 | + if($window.localStorage['fb_access_token'] && $window.localStorage['fb_access_token']!='') { |
| 110 | + $scope.showLoading(); |
| 111 | + |
| 112 | + //construct the post request |
| 113 | + var req = { |
| 114 | + method: 'POST', |
| 115 | + url: FB_CONFIG.API_ENDPOINT+'/me/feed', |
| 116 | + data: {'message':$scope.post,'link':FB_CONFIG.LINK,'picture':FB_CONFIG.IMAGE_URL,'access_token':$window.localStorage['fb_access_token']} |
| 117 | + } |
| 118 | + |
| 119 | + $http(req).then(function(result) { |
| 120 | + |
| 121 | + $scope.showInfo('Message Posted Successfully'); |
| 122 | + |
| 123 | + }).catch($scope.showError) |
| 124 | + .finally(function () { |
| 125 | + $scope.hideLoading(); |
| 126 | + }); |
| 127 | + } else { |
| 128 | + $scope.showError("Oops looks like the sesssion has expired."); |
| 129 | + //clean up the token |
| 130 | + $scope.reset(); |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + /* |
| 135 | + clean the facebook token & so that the user needs to login again |
| 136 | + */ |
| 137 | + $scope.reset=function(){ |
| 138 | + $window.localStorage['fb_access_token']=''; |
| 139 | + $scope.showInfo('Reset Successfull done'); |
| 140 | + |
| 141 | + $state.go('app.facebook'); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + }) |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +})(); |
0 commit comments