├── README.md ├── bower.json ├── controllers.js ├── example.html ├── src └── angular-googleapi.js └── vendor └── angular.js /README.md: -------------------------------------------------------------------------------- 1 | angular-googleapi 2 | ================= 3 | 4 | angular-googleapi makes it easy to use google apis from angular. So far we have support for login and a couple of google calendar methods, but it should be super easy to add others. 5 | 6 | Installation 7 | ------------ 8 | 9 | ```` 10 | bower install angular-googleapi 11 | ```` 12 | 13 | Or just download src/angular-googleapi.js 14 | 15 | Then in your angular module add angular-googleapi as a dependency. 16 | 17 | Usage 18 | ----- 19 | 20 | The module will automatically load and initialize the Google API. 21 | There is no need to manually include a ` 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/angular-googleapi.js: -------------------------------------------------------------------------------- 1 | angular.module('googleApi', []) 2 | .value('version', '0.1') 3 | 4 | .service("googleApiBuilder", function($q) { 5 | this.loadClientCallbacks = []; 6 | 7 | this.build = function(requestBuilder, responseTransformer) { 8 | return function(args) { 9 | var deferred = $q.defer(); 10 | var response; 11 | request = requestBuilder(args); 12 | request.execute(function(resp, raw) { 13 | if(resp.error) { 14 | deferred.reject(resp.error); 15 | } else { 16 | response = responseTransformer ? responseTransformer(resp) : resp; 17 | deferred.resolve(response); 18 | } 19 | 20 | }); 21 | return deferred.promise; 22 | 23 | } 24 | }; 25 | 26 | this.afterClientLoaded = function(callback) { 27 | this.loadClientCallbacks.push(callback); 28 | }; 29 | 30 | this.runClientLoadedCallbacks = function() { 31 | for(var i=0; i < this.loadClientCallbacks.length; i++) { 32 | this.loadClientCallbacks[i](); 33 | } 34 | }; 35 | }) 36 | 37 | .provider('googleLogin', function() { 38 | 39 | this.configure = function(conf) { 40 | this.config = conf; 41 | }; 42 | 43 | this.$get = function ($q, googleApiBuilder, $rootScope) { 44 | var config = this.config; 45 | var deferred = $q.defer(); 46 | var svc = { 47 | login: function () { 48 | gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: false}, this.handleAuthResult); 49 | 50 | return deferred.promise; 51 | }, 52 | 53 | handleAuthResult: function(authResult) { 54 | if (authResult && !authResult.error) { 55 | var data = {}; 56 | $rootScope.$broadcast("google:authenticated", authResult); 57 | googleApiBuilder.runClientLoadedCallbacks(); 58 | deferred.resolve(data); 59 | } else { 60 | deferred.reject(authResult.error); 61 | } 62 | }, 63 | }; 64 | 65 | // load the gapi client, instructing it to invoke a globally-accessible function when finished 66 | window._googleApiLoaded = function() { 67 | gapi.auth.init(function () { 68 | $rootScope.$broadcast("google:ready", {}); 69 | }); 70 | }; 71 | var script = document.createElement('script'); 72 | script.setAttribute("type","text/javascript"); 73 | script.setAttribute("src", "https://apis.google.com/js/client.js?onload=_googleApiLoaded"); 74 | document.getElementsByTagName("head")[0].appendChild(script); 75 | 76 | return svc; 77 | }; 78 | }) 79 | 80 | .service("googleCalendar", function(googleApiBuilder, $rootScope) { 81 | 82 | var self = this; 83 | var itemExtractor = function(resp) { return resp.items; }; 84 | 85 | googleApiBuilder.afterClientLoaded(function() { 86 | gapi.client.load('calendar', 'v3', function() { 87 | 88 | self.listCalendarColors = googleApiBuilder.build(gapi.client.calendar.colors.get); 89 | 90 | self.createEvent = googleApiBuilder.build(gapi.client.calendar.events.quickAdd); 91 | self.deleteEvent = googleApiBuilder.build(gapi.client.calendar.events.delete); 92 | self.updateEvent = googleApiBuilder.build(gapi.client.calendar.events.update); 93 | self.listEvents = googleApiBuilder.build(gapi.client.calendar.events.list, itemExtractor); 94 | 95 | self.createCalendar = googleApiBuilder.build(gapi.client.calendar.calendars.insert); 96 | self.deleteCalendar = googleApiBuilder.build(gapi.client.calendar.calendars.delete); 97 | self.updateCalendar = googleApiBuilder.build(gapi.client.calendar.calendars.update); 98 | self.listCalendars = googleApiBuilder.build(gapi.client.calendar.calendarList.list, itemExtractor); 99 | 100 | $rootScope.$broadcast("googleCalendar:loaded") 101 | }); 102 | 103 | }); 104 | 105 | }) 106 | 107 | .service("googlePlus", function(googleApiBuilder, $rootScope) { 108 | 109 | var self = this; 110 | var itemExtractor = function(resp) { return resp.items; }; 111 | 112 | googleApiBuilder.afterClientLoaded(function() { 113 | gapi.client.load('plus', 'v1', function() { 114 | self.getPeople = googleApiBuilder.build(gapi.client.plus.people.get); 115 | self.getCurrentUser = function() { 116 | return self.getPeople({userId: "me"}); 117 | } 118 | $rootScope.$broadcast("googlePlus:loaded") 119 | }); 120 | 121 | }); 122 | 123 | }) 124 | --------------------------------------------------------------------------------