· 7 years ago · Mar 25, 2018, 05:58 PM
1 <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
2 <script>
3 var developerKey = 'AIzaSyBK5HYEjLpMNiP6FMHfq8XOPu8sqjTEaXg';
4 // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
5 var clientId = "120928419966-e2b1nkuip7piq9puq96esp5imeqjmfus.apps.googleusercontent.com"
6
7 // Replace with your own App ID. (Its the first number in your Client ID)
8 var appId = "120928419966";
9
10 // Scope to use to access user's Drive items.
11 var scope = ['https://www.googleapis.com/auth/drive'];
12
13 var pickerApiLoaded = false;
14 var oauthToken;
15
16 // Use the Google API Loader script to load the google.picker script.
17 function loadPicker() {
18 gapi.load('auth', {'callback': onAuthApiLoad});
19 gapi.load('picker', {'callback': onPickerApiLoad});
20 }
21
22 function onAuthApiLoad() {
23 window.gapi.auth.authorize(
24 {
25 'client_id': clientId,
26 'scope': scope,
27 'immediate': false
28 },
29 handleAuthResult);
30 }
31
32 function onPickerApiLoad() {
33 pickerApiLoaded = true;
34 createPicker();
35 }
36
37 function handleAuthResult(authResult) {
38 if (authResult && !authResult.error) {
39 oauthToken = authResult.access_token;
40 createPicker();
41 }
42 }
43
44 // Create and render a Picker object for searching images.
45 function createPicker() {
46 if (pickerApiLoaded && oauthToken) {
47 var view = new google.picker.View(google.picker.ViewId.DOCS);
48 view.setMimeTypes("image/png,image/jpeg,image/jpg,application/pdf,application/vnd.google-apps.document,application/vnd.google-apps.spreadsheet,application/vnd.google-apps.presentation,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation");
49
50 //view.setMimeTypes("application/pdf");
51 var picker = new google.picker.PickerBuilder()
52 .enableFeature(google.picker.Feature.NAV_HIDDEN)
53 .enableFeature(google.picker.Feature.MULTISELECT_DISABLED)
54 .setAppId(appId)
55 .setOAuthToken(oauthToken)
56 .addView(view)
57 .setDeveloperKey(developerKey)
58 .addView(new google.picker.DocsUploadView())
59 .setCallback(pickerCallback)
60 .build();
61 picker.setVisible(true);
62 }
63 }
64
65 // A simple callback implementation.
66 function pickerCallback(data) {
67 if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
68 var doc = data[google.picker.Response.DOCUMENTS][0];
69 pointer_file++;
70
71 $('#files_attachements_publication').val(parseFloat($('#files_attachements_publication').val())+1);
72
73
74 $('#container_attachments_publications').append('<div title="'+doc[google.picker.Document.NAME]+'" id="file_attachment'+pointer_file+'" style="margin-bottom:5px; width:500px; float:left; border: 1px solid #e1e1e1; background-color: #f8f8f8; padding:5px 10px 5px 8px;"><img style="float:left;" src="' + doc[google.picker.Document.ICON_URL] + '"/><div style="margin-left: 5px; float:left; width:250px; overflow: hidden; white-space:nowrap; text-overflow: ellipsis; word-break:break-strict; ">'+doc[google.picker.Document.NAME]+'</div><input id="icon_url'+pointer_file+'" type="hidden" value=' + +doc[google.picker.Document.ICON_URL] + '/><div class="button delete_file_gd_container_attachments" style="min-width:0px; float:right;" tag="'+doc[google.picker.Document.NAME]+'" alt="file_attachment'+pointer_file+'" title="Quitar archivo"><i class="material-icons md-18 md-dark">delete</i></div><div class="button open_preview_file_gd" style="min-width:0px; float:right; margin-right:5px;" title="Vista previa del archivo" alt="'+doc[google.picker.Document.ID]+'" tag="'+doc[google.picker.Document.URL]+'"><i class="material-icons md-18 md-dark">visibility</i></div></div>');
75
76 $('.open_preview_file_gd').off();
77 $('.open_preview_file_gd').on('click',function(){
78 window.open($(this).attr('tag'));
79 });
80
81
82 $('.delete_file_gd_container_attachments').off();
83 $('.delete_file_gd_container_attachments').on('click',function(){
84 pf = $(this).attr('alt')
85 dlgbox(1,175,380,'#teachers','Eliminar archivo','','dlgbox');
86
87 $('#bodydlg').html('<div class="data12" style=" font-weight:bold; margin-top: 15px; padding: 0 10px 0 10px;">¿Seguro de eliminar el archivo a adjuntar "'+$(this).attr('tag')+'"?</div>');
88
89 $('#buttonsdlg').html('<div id="dlg_yes_file_attachment" class="c_button">Si</div><div id="dlg_no_delete_file_attachment" class="c_button">No</div>');
90
91 $('#dlg_yes_file_attachment').click(function(e) {
92 $('#'+pf).remove();
93 $('#files_attachements_publication').val(parseFloat($('#files_attachements_publication').val())-1);
94 closedlgbox();
95 });
96
97
98 $('#dlg_no_delete_file_attachment').click(function(e) {
99 closedlgbox();
100 });
101 });
102
103 }
104
105
106 }
107 </script>