Skip to content

Commit

Permalink
Support callback function after file(s) picked.
Browse files Browse the repository at this point in the history
  • Loading branch information
Loic Kartono committed May 17, 2015
1 parent 97d4e09 commit 2e5b541
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
34 changes: 18 additions & 16 deletions dist/google-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

angular.module('lk-google-picker', [])

.provider('lkGoogleSettings', function() {
.provider('lkGoogleSettings', function () {
this.apiKey = null;
this.clientId = null;
this.scopes = ['https://www.googleapis.com/auth/drive'];
Expand All @@ -25,7 +25,7 @@ angular.module('lk-google-picker', [])
* Provider factory $get method
* Return Google Picker API settings
*/
this.$get = ['$window', function($window) {
this.$get = ['$window', function ($window) {
return {
apiKey : this.apiKey,
clientId : this.clientId,
Expand All @@ -40,26 +40,27 @@ angular.module('lk-google-picker', [])
/**
* Set the API config params using a hash
*/
this.configure = function(config) {
this.configure = function (config) {
for (var key in config) {
this[key] = config[key];
}
};
})

.directive('lkGooglePicker', ['lkGoogleSettings', function(lkGoogleSettings) {
.directive('lkGooglePicker', ['lkGoogleSettings', function (lkGoogleSettings) {
return {
restrict: 'A',
scope: {
pickerFiles: '='
pickerFiles: '=',
afterSelect: '&'
},
link: function(scope, element, attrs) {
link: function (scope, element, attrs) {
var accessToken = null;

/**
* Load required modules
*/
function instanciate() {
function instanciate () {
gapi.load('auth', { 'callback': onApiAuthLoad });
gapi.load('picker');
}
Expand All @@ -68,7 +69,7 @@ angular.module('lk-google-picker', [])
* OAuth autorization
* If user is already logged in, then open the Picker modal
*/
function onApiAuthLoad() {
function onApiAuthLoad () {
var authToken = gapi.auth.getToken();

if (authToken) {
Expand All @@ -85,7 +86,7 @@ angular.module('lk-google-picker', [])
/**
* Google API OAuth response
*/
function handleAuthResult(result) {
function handleAuthResult (result) {
if (result && !result.error) {
accessToken = result.access_token;
openDialog();
Expand All @@ -95,7 +96,7 @@ angular.module('lk-google-picker', [])
/**
* Everything is good, open the files picker
*/
function openDialog() {
function openDialog () {
var picker = new google.picker.PickerBuilder()
.setLocale(lkGoogleSettings.locale)
.setDeveloperKey(lkGoogleSettings.apiKey)
Expand All @@ -104,13 +105,13 @@ angular.module('lk-google-picker', [])
.setOrigin(lkGoogleSettings.origin);

if (lkGoogleSettings.features.length > 0) {
angular.forEach(lkGoogleSettings.features, function(feature, key) {
angular.forEach(lkGoogleSettings.features, function (feature, key) {
picker.enableFeature(google.picker.Feature[feature]);
});
}

if (lkGoogleSettings.views.length > 0) {
angular.forEach(lkGoogleSettings.views, function(view, key) {
angular.forEach(lkGoogleSettings.views, function (view, key) {
view = eval('new google.picker.' + view);
picker.addView(view);
});
Expand All @@ -123,12 +124,13 @@ angular.module('lk-google-picker', [])
* Callback invoked when interacting with the Picker
* data: Object returned by the API
*/
function pickerResponse(data) {
function pickerResponse (data) {
if (data.action == google.picker.Action.PICKED) {
gapi.client.load('drive', 'v2', function() {
angular.forEach(data.docs, function(file, index) {
gapi.client.load('drive', 'v2', function () {
angular.forEach(data.docs, function (file, index) {
scope.pickerFiles.push(file);
});
scope.afterSelect();
scope.$apply();
});
}
Expand All @@ -137,7 +139,7 @@ angular.module('lk-google-picker', [])
gapi.load('auth');
gapi.load('picker');

element.bind('click', function(e) {
element.bind('click', function (e) {
instanciate();
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/google-picker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('GooglePickerExample', ['lk-google-picker'])

.config(['lkGoogleSettingsProvider', function(lkGoogleSettingsProvider) {
.config(['lkGoogleSettingsProvider', function (lkGoogleSettingsProvider) {

// Configure the API credentials here
lkGoogleSettingsProvider.configure({
Expand All @@ -9,13 +9,13 @@ angular.module('GooglePickerExample', ['lk-google-picker'])
});
}])

.filter('getExtension', function() {
return function(url) {
.filter('getExtension', function () {
return function (url) {
return url.split('.').pop();
};
})

.controller('ExampleCtrl', ['$scope', 'lkGoogleSettings', function($scope, lkGoogleSettings) {
.controller('ExampleCtrl', ['$scope', 'lkGoogleSettings', function ($scope, lkGoogleSettings) {
$scope.files = [];
$scope.languages = [
{ code: 'en', name: 'English' },
Expand All @@ -25,16 +25,21 @@ angular.module('GooglePickerExample', ['lk-google-picker'])
]

// Check for the current language depending on lkGoogleSettings.locale
$scope.initialize = function() {
angular.forEach($scope.languages, function(language, index) {
$scope.initialize = function () {
angular.forEach($scope.languages, function (language, index) {
if (lkGoogleSettings.locale === language.code) {
$scope.selectedLocale = $scope.languages[index];
}
});
}

// Callback triggered after files selection
$scope.afterSelectCallback = function (files) {
alert("After Select Callback\n" + (files.length) + " file(s) selected");
}

// Define the locale to use
$scope.changeLocale = function(locale) {
$scope.changeLocale = function (locale) {
lkGoogleSettings.locale = locale.code;
}
}]);
2 changes: 1 addition & 1 deletion example/google-picker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</p>

<div>
<a href="javascript:;" lk-google-picker picker-files="files" class="btn btn-2 btn-2c btn-blue">
<a href="javascript:;" lk-google-picker picker-files="files" after-select="afterSelectCallback(files)" class="btn btn-2 btn-2c btn-blue">
<i class="fa fa-inbox"></i>
<span>Pick files</span>
</a>
Expand Down
34 changes: 18 additions & 16 deletions src/google-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

angular.module('lk-google-picker', [])

.provider('lkGoogleSettings', function() {
.provider('lkGoogleSettings', function () {
this.apiKey = null;
this.clientId = null;
this.scopes = ['https://www.googleapis.com/auth/drive'];
Expand All @@ -25,7 +25,7 @@ angular.module('lk-google-picker', [])
* Provider factory $get method
* Return Google Picker API settings
*/
this.$get = ['$window', function($window) {
this.$get = ['$window', function ($window) {
return {
apiKey : this.apiKey,
clientId : this.clientId,
Expand All @@ -40,26 +40,27 @@ angular.module('lk-google-picker', [])
/**
* Set the API config params using a hash
*/
this.configure = function(config) {
this.configure = function (config) {
for (var key in config) {
this[key] = config[key];
}
};
})

.directive('lkGooglePicker', ['lkGoogleSettings', function(lkGoogleSettings) {
.directive('lkGooglePicker', ['lkGoogleSettings', function (lkGoogleSettings) {
return {
restrict: 'A',
scope: {
pickerFiles: '='
pickerFiles: '=',
afterSelect: '&'
},
link: function(scope, element, attrs) {
link: function (scope, element, attrs) {
var accessToken = null;

/**
* Load required modules
*/
function instanciate() {
function instanciate () {
gapi.load('auth', { 'callback': onApiAuthLoad });
gapi.load('picker');
}
Expand All @@ -68,7 +69,7 @@ angular.module('lk-google-picker', [])
* OAuth autorization
* If user is already logged in, then open the Picker modal
*/
function onApiAuthLoad() {
function onApiAuthLoad () {
var authToken = gapi.auth.getToken();

if (authToken) {
Expand All @@ -85,7 +86,7 @@ angular.module('lk-google-picker', [])
/**
* Google API OAuth response
*/
function handleAuthResult(result) {
function handleAuthResult (result) {
if (result && !result.error) {
accessToken = result.access_token;
openDialog();
Expand All @@ -95,7 +96,7 @@ angular.module('lk-google-picker', [])
/**
* Everything is good, open the files picker
*/
function openDialog() {
function openDialog () {
var picker = new google.picker.PickerBuilder()
.setLocale(lkGoogleSettings.locale)
.setDeveloperKey(lkGoogleSettings.apiKey)
Expand All @@ -104,13 +105,13 @@ angular.module('lk-google-picker', [])
.setOrigin(lkGoogleSettings.origin);

if (lkGoogleSettings.features.length > 0) {
angular.forEach(lkGoogleSettings.features, function(feature, key) {
angular.forEach(lkGoogleSettings.features, function (feature, key) {
picker.enableFeature(google.picker.Feature[feature]);
});
}

if (lkGoogleSettings.views.length > 0) {
angular.forEach(lkGoogleSettings.views, function(view, key) {
angular.forEach(lkGoogleSettings.views, function (view, key) {
view = eval('new google.picker.' + view);
picker.addView(view);
});
Expand All @@ -123,12 +124,13 @@ angular.module('lk-google-picker', [])
* Callback invoked when interacting with the Picker
* data: Object returned by the API
*/
function pickerResponse(data) {
function pickerResponse (data) {
if (data.action == google.picker.Action.PICKED) {
gapi.client.load('drive', 'v2', function() {
angular.forEach(data.docs, function(file, index) {
gapi.client.load('drive', 'v2', function () {
angular.forEach(data.docs, function (file, index) {
scope.pickerFiles.push(file);
});
scope.afterSelect();
scope.$apply();
});
}
Expand All @@ -137,7 +139,7 @@ angular.module('lk-google-picker', [])
gapi.load('auth');
gapi.load('picker');

element.bind('click', function(e) {
element.bind('click', function (e) {
instanciate();
});
}
Expand Down

0 comments on commit 2e5b541

Please sign in to comment.