diff --git a/src/angular-googleapi.js b/src/angular-googleapi.js index 4852c2f..33524bf 100644 --- a/src/angular-googleapi.js +++ b/src/angular-googleapi.js @@ -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") + }); - }) + }); +})(); \ No newline at end of file