-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (73 loc) · 2.13 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(function($routeProvider) {
$routeProvider.when('/connect', {
templateUrl: 'partials/connect.html',
controller: 'connectCtrl'
}).when('/editor', {
templateUrl: 'partials/editor.html',
controller: 'editorCtrl'
}).otherwise({
redirectTo: '/connect'
});
});
demoApp.controller('connectCtrl', function connectCtrl($scope, $rootScope, $location) {
OAuth.initialize("~~~[[YOUR_OAUTH_IO_PUBLIC_KEY]]~~~");
function createDriveFile(title, callback) {
$rootScope.drive.post({
url: "/drive/v2/files",
data: JSON.stringify({
title: title,
mimeType: 'application/vnd.google-apps.document'}),
contentType: 'application/json'
}).done(function(item) {
callback(item);
});
}
function getDriveFile(title, callback) {
$rootScope.drive.get({
url: "/drive/v2/files",
data: {q: "title = '" + title + "'"}
}).done(function(files) {
if (files.items.length)
return callback(files.items[0]);
// create file if it does not exists
createDriveFile(title, callback);
});
}
$scope.connect = function() {
OAuth.popup("google_drive", function(err, res) {
if (err) return alert(err);
$rootScope.drive = res;
getDriveFile("realtime_test", function (file) {
$rootScope.drive_file = file;
$location.path('/editor');
$scope.$apply();
});
});
}
});
demoApp.controller('editorCtrl', function editorCtrl($scope, $rootScope) {
$rootScope.drive.get($rootScope.drive_file.exportLinks['text/plain']).done(function(data) {
$scope.file_content = data;
$scope.$apply();
$scope.$watch("file_content", function() {
$scope.must_save = true;
updateFile();
});
});
function updateFile() {
if ( ! $scope.must_save) return;
if ($scope.saving) return setTimeout(updateFile, 50);
$scope.saving = true;
$scope.must_save = false;
$rootScope.drive.put({
url: "/upload/drive/v2/files/"
+ $rootScope.drive_file.id
+ "?uploadType=media",
data: $scope.file_content
}).always(function() {
$scope.saving = false;
updateFile();
});
}
});