Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 100 additions & 86 deletions src/angular-googleapi.js
Original file line number Diff line number Diff line change
@@ -1,113 +1,127 @@
angular.module('googleApi', [])
.value('version', '0.1')
(function(){
'use strict';
angular.module('googleApi', [])
.value('version', '0.1')

.service("googleApiBuilder", function($q) {
this.loadClientCallbacks = [];

this.build = function(requestBuilder, responseTransformer) {
return function(args) {
var deferred = $q.defer();
var response;
request = requestBuilder(args);
request.execute(function(resp, raw) {
if(resp.error) {
deferred.reject(resp.error);
} else {
response = responseTransformer ? responseTransformer(resp) : resp;
deferred.resolve(response);
}

});
return deferred.promise;

this.loadClientCallbacks = [];

this.build = function(requestBuilder, responseTransformer) {
return function(args) {
var deferred = $q.defer();
var response;
var request = requestBuilder(args);
request.execute(function(resp, raw) {
if(resp.error) {
deferred.reject(resp.error);
} else {
response = responseTransformer ? responseTransformer(resp) : resp;
deferred.resolve(response);
}
};

this.afterClientLoaded = function(callback) {
this.loadClientCallbacks.push(callback);
};
});
return deferred.promise;

this.runClientLoadedCallbacks = function() {
for(var i=0; i < this.loadClientCallbacks.length; i++) {
this.loadClientCallbacks[i]();
}
};
}
};

this.afterClientLoaded = function(callback) {
this.loadClientCallbacks.push(callback);
};

this.runClientLoadedCallbacks = function() {
for(var i=0; i < this.loadClientCallbacks.length; i++) {
this.loadClientCallbacks[i]();
}
};
})

.provider('googleLogin', function() {

this.configure = function(conf) {
this.config = conf;
};

this.$get = function ($q, googleApiBuilder, $rootScope) {
var config = this.config;
var deferred = $q.defer();
return {
login: function () {
gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: false}, this.handleAuthResult);

return deferred.promise;
},

handleClientLoad: function () {
gapi.auth.init(function () { });
window.setTimeout(checkAuth, 1);
},

checkAuth: function() {
gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: true }, this.handleAuthResult );
},

handleAuthResult: function(authResult) {
if (authResult && !authResult.error) {
var data = {};
$rootScope.$broadcast("google:authenticated", authResult);
googleApiBuilder.runClientLoadedCallbacks();
deferred.resolve(data);
} else {
deferred.reject(authResult.error);
}
},
this.configure = function(conf) {
this.config = conf;
};

this.$get = function ($q, googleApiBuilder, $rootScope) {
var config = this.config;
var deferred = $q.defer();
return {
login: function () {
gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: false}, this.handleAuthResult);

return deferred.promise;
},

handleClientLoad: function () {
gapi.auth.init(function () { });
window.setTimeout(checkAuth, 1);
},

checkAuth: function() {
gapi.auth.authorize({ client_id: config.clientId, scope: config.scopes, immediate: true }, this.handleAuthResult );
},

handleAuthResult: function(authResult) {
if (authResult && !authResult.error) {
var data = {};
$rootScope.$broadcast("google:authenticated", authResult);
googleApiBuilder.runClientLoadedCallbacks();
deferred.resolve(data);
} else {
deferred.reject(authResult.error);
}
};
},
}
};


})

.service("googleCalendar", function(googleApiBuilder, $rootScope) {

var self = this;
var itemExtractor = function(resp) { return resp.items; };

googleApiBuilder.afterClientLoaded(function() {
gapi.client.load('calendar', 'v3', function() {
var self = this;
var itemExtractor = function(resp) { return resp.items; };

self.listEvents = googleApiBuilder.build(gapi.client.calendar.events.list, itemExtractor);
self.listCalendars = googleApiBuilder.build(gapi.client.calendar.calendarList.list, itemExtractor);
self.createEvent = googleApiBuilder.build(gapi.client.calendar.events.insert);
googleApiBuilder.afterClientLoaded(function() {
gapi.client.load('calendar', 'v3', function() {

$rootScope.$broadcast("googleCalendar:loaded")
});
self.listEvents = googleApiBuilder.build(gapi.client.calendar.events.list, itemExtractor);
self.listCalendars = googleApiBuilder.build(gapi.client.calendar.calendarList.list, itemExtractor);
self.createEvent = googleApiBuilder.build(gapi.client.calendar.events.insert);

$rootScope.$broadcast("googleCalendar:loaded")
});

});

})

.service("googlePlus", function(googleApiBuilder, $rootScope) {
.service("googlePlus", function(googleApiBuilder, $rootScope) {

var self = this;
var itemExtractor = function(resp) { return resp.items; };

var self = this;
var itemExtractor = function(resp) { return resp.items; };
googleApiBuilder.afterClientLoaded(function() {
gapi.client.load('plus', 'v1', function() {
self.getPeople = googleApiBuilder.build(gapi.client.plus.people.get);
self.getCurrentUser = function() {
return self.getPeople({userId: "me"});
}
$rootScope.$broadcast("googlePlus:loaded")
});

googleApiBuilder.afterClientLoaded(function() {
gapi.client.load('plus', 'v1', function() {
self.getPeople = googleApiBuilder.build(gapi.client.plus.people.get);
self.getCurrentUser = function() {
return self.getPeople({userId: "me"});
}
$rootScope.$broadcast("googlePlus:loaded")
});
});

});
})
.service("googleDrive", function(googleApiBuilder, $rootScope) {
var self = this;
var itemExtractor = function(resp) { return resp.items; };
googleApiBuilder.afterClientLoaded(function() {
gapi.client.load('drive', 'v2', function() {
self.listFiles = googleApiBuilder.build(gapi.client.drive.files.list);
});
$rootScope.$broadcast("googleDrive:loaded")
});

})
});
})();