Skip to content

I added new feature #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Empty file modified .editorconfig
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .travis.yml
100644 → 100755
Empty file.
Empty file modified CHANGELOG.md
100644 → 100755
Empty file.
48 changes: 47 additions & 1 deletion README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Download [https://github.com/softmonkeyjapan/angular-google-picker/archive/0.2.2
5. Add the directive to your HTML element

```html
<a href="javascript:;" lk-google-picker on-picked="onPicked(docs)" on-loaded="onLoaded()" on-cancel="onCancel()">Open my Google Drive</a>
<a href="javascript:;" lk-google-picker on-authenticated="onAuthenticated(result)" on-picked="onPicked(docs)" on-loaded="onLoaded()" on-cancel="onCancel()" acces-token="tokenFromTheServer" open-dialog-from="both" open-dialog="openIt" >Open my Google Drive</a>
```

6. That's it, you're done!
Expand Down Expand Up @@ -187,6 +187,25 @@ Please refer to [https://developers.google.com/picker/docs/reference](https://de

The directive provide you 3 callbacks that you can use in order to work with the Picker.

### `onAuthenticated`

This callback is triggered after you select files and click on the `select` button from the Picker.

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

.controller('ExampleCtrl', ['$scope', function ($scope) {
$scope.onAuthenticated = function (result) {
console.log(result.access_token);
}
}]);
```

```html
<a href="javascript:;" lk-google-picker on-picked="onPicked">Open my Google Drive</a>
```


### `onLoaded`

This callback is triggered after the picker has been initialized and shown on the page.
Expand Down Expand Up @@ -241,6 +260,33 @@ angular.module('myApp', ['lk-google-picker'])
<a href="javascript:;" lk-google-picker on-cancel="onCancel">Open my Google Drive</a>
```

## Additional parametars

### `accessToken`
This property gives you chance to use externally generated access token.

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

.controller('ExampleCtrl', ['$scope', function ($scope) {
$scope.setToken = function (token) {
scope.tokenFromTheServer = token;
}
}]);
```
### `openDialogFrom`
This property gives you chance to specify from where you want to open the picker.
Options are:
both - opens on click or when openDialog value is true
click - opens only on click
variable - opens only when openDialog value is true

Default option is click!

### `openDialog`
You can use this, to controll the picker


## Demo

The demo version available at [http://softmonkeyjapan.github.io/angular-google-picker/](http://softmonkeyjapan.github.io/angular-google-picker/) can be found in the `example` folder.
Expand Down
Empty file modified bower.json
100644 → 100755
Empty file.
277 changes: 152 additions & 125 deletions dist/google-picker.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,144 +8,171 @@
* License: MIT
*/
(function () {
angular.module('lk-google-picker', [])
angular.module('lk-google-picker', [])

.provider('lkGoogleSettings', function () {
this.apiKey = null;
this.clientId = null;
this.scopes = ['https://www.googleapis.com/auth/drive'];
this.features = ['MULTISELECT_ENABLED'];
this.views = [
'DocsView().setIncludeFolders(true)',
'DocsUploadView().setIncludeFolders(true)'
];
this.locale = 'en'; // Default to English
.provider('lkGoogleSettings', function () {
this.apiKey = null;
this.clientId = null;
this.scopes = ['https://www.googleapis.com/auth/drive'];
this.features = ['MULTISELECT_ENABLED'];
this.views = [
'DocsView().setIncludeFolders(true)',
'DocsUploadView().setIncludeFolders(true)'
];
this.locale = 'en'; // Default to English

/**
* Provider factory $get method
* Return Google Picker API settings
*/
this.$get = ['$window', function ($window) {
return {
apiKey : this.apiKey,
clientId : this.clientId,
scopes : this.scopes,
features : this.features,
views : this.views,
locale : this.locale,
origin : this.origin || $window.location.protocol + '//' + $window.location.host
}
}];
/**
* Provider factory $get method
* Return Google Picker API settings
*/
this.$get = ['$window', function ($window) {
return {
apiKey : this.apiKey,
clientId : this.clientId,
scopes : this.scopes,
features : this.features,
views : this.views,
locale : this.locale,
origin : this.origin || $window.location.protocol + '//' + $window.location.host
}
}];

/**
* Set the API config params using a hash
*/
this.configure = function (config) {
for (var key in config) {
this[key] = config[key];
}
};
})
/**
* Set the API config params using a hash
*/
this.configure = function (config) {
for (var key in config) {
this[key] = config[key];
}
};
})

.directive('lkGooglePicker', ['lkGoogleSettings', function (lkGoogleSettings) {
return {
restrict: 'A',
scope: {
onLoaded: '&',
onCancel: '&',
onPicked: '&'
},
link: function (scope, element, attrs) {
var accessToken = null;
.directive('lkGooglePicker', ['lkGoogleSettings', function (lkGoogleSettings) {
return {
restrict: 'A',
scope: {
onLoaded: '&',
onAuthenticated: '&',
onCancel: '&',
onPicked: '&',
accesToken: '=',
openDialog: '=',
openDialogFrom: '@',
},
link: function (scope, element, attrs) {
var accessToken = null;

/**
* Load required modules
*/
function instanciate () {
gapi.load('auth', { 'callback': onApiAuthLoad });
gapi.load('picker');
}
/**
* Load required modules
*/
function instantiate () {
if(scope.accesToken){
accessToken = scope.accesToken;
gapi.auth.setToken({
access_token: accessToken,
});
}
gapi.load('auth', { 'callback': onApiAuthLoad });
gapi.load('picker');
}

/**
* OAuth autorization
* If user is already logged in, then open the Picker modal
*/
function onApiAuthLoad () {
var authToken = gapi.auth.getToken();
/**
* OAuth autorization
* If user is already logged in, then open the Picker modal
*/
function onApiAuthLoad () {
var authToken = gapi.auth.getToken();
if (authToken) {
handleAuthResult(authToken);
}else{
gapi.auth.authorize({
'client_id' : lkGoogleSettings.clientId,
'scope' : lkGoogleSettings.scopes,
'immediate' : false
}, handleAuthResult);
}
}

if (authToken) {
handleAuthResult(authToken);
} else {
gapi.auth.authorize({
'client_id' : lkGoogleSettings.clientId,
'scope' : lkGoogleSettings.scopes,
'immediate' : false
}, handleAuthResult);
}
}
/**
* Google API OAuth response
*/
function handleAuthResult (result) {
if (result && !result.error) {
if(!accessToken){
accessToken = result.access_token;
if(scope.onAuthenticated)
scope.onAuthenticated({result : result});
}
openDialog();
}
}

/**
* Google API OAuth response
*/
function handleAuthResult (result) {
if (result && !result.error) {
accessToken = result.access_token;
openDialog();
}
}
/**
* Everything is good, open the files picker
*/
function openDialog () {
try{
var picker = new google.picker.PickerBuilder()
.setLocale(lkGoogleSettings.locale)
.setOAuthToken(accessToken)
.setCallback(pickerResponse)
.setOrigin(lkGoogleSettings.origin);
}catch(err){
setTimeout(openDialog, 100);
return;
}

/**
* Everything is good, open the files picker
*/
function openDialog () {
var picker = new google.picker.PickerBuilder()
.setLocale(lkGoogleSettings.locale)
.setOAuthToken(accessToken)
.setCallback(pickerResponse)
.setOrigin(lkGoogleSettings.origin);
if (lkGoogleSettings.features.length > 0) {
angular.forEach(lkGoogleSettings.features, function (feature, key) {
picker.enableFeature(google.picker.Feature[feature]);
});
}

if (lkGoogleSettings.features.length > 0) {
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) {
view = eval('new google.picker.' + view);
picker.addView(view);
});
}

if (lkGoogleSettings.views.length > 0) {
angular.forEach(lkGoogleSettings.views, function (view, key) {
view = eval('new google.picker.' + view);
picker.addView(view);
});
}
picker.build().setVisible(true);
}

picker.build().setVisible(true);
}
/**
* Callback invoked when interacting with the Picker
* data: Object returned by the API
*/
function pickerResponse (data) {
gapi.client.load('drive', 'v2', function () {
if (data.action == google.picker.Action.LOADED && scope.onLoaded) {
(scope.onLoaded || angular.noop)();
}
if (data.action == google.picker.Action.CANCEL && scope.onCancel) {
(scope.onCancel || angular.noop)();
}
if (data.action == google.picker.Action.PICKED && scope.onPicked) {
(scope.onPicked || angular.noop)({docs: data.docs});
}
scope.$apply();
});
}

/**
* Callback invoked when interacting with the Picker
* data: Object returned by the API
*/
function pickerResponse (data) {
gapi.client.load('drive', 'v2', function () {
if (data.action == google.picker.Action.LOADED && scope.onLoaded) {
(scope.onLoaded || angular.noop)();
}
if (data.action == google.picker.Action.CANCEL && scope.onCancel) {
(scope.onCancel || angular.noop)();
}
if (data.action == google.picker.Action.PICKED && scope.onPicked) {
(scope.onPicked || angular.noop)({docs: data.docs});
}
scope.$apply();
});
}
if(!scope.openDialogFrom || scope.openDialogFrom == 'both' || scope.openDialogFrom == 'click'){
element.bind('click', function (e) {
/* dynamically load dependencies only on click */
instantiate();
});
}

gapi.load('auth');
gapi.load('picker');
if(scope.openDialogFrom == 'both' || scope.openDialogFrom == 'variable'){
scope.$watch('openDialog', function(value){
if(value == true){
instantiate();
}
});
}

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

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

Empty file modified example/app.js
100644 → 100755
Empty file.
Loading