This repository has been archived by the owner on Jan 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathapp.js
142 lines (126 loc) · 3.83 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var CONFIG = {
clientId: '502747173299.apps.googleusercontent.com',
scopes: [
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.install'
]
};
var app = {};
app.module = angular.module('todos', []);
/**
* A simple type for todo items.
* @constructor
*/
app.Todo = function () {
};
/**
* Initializer for constructing via the realtime API
*
* @param title
*/
app.Todo.prototype.initialize = function (title) {
var model = gapi.drive.realtime.custom.getModel(this);
this.title = model.createString(title);
this.completed = false;
this.setup();
};
/**
* Adds a "text" property to collaborative strings for ng-model compatibility
* after a model is created or loaded.
*/
app.Todo.prototype.setup = function() {
Object.defineProperty(this.title, 'text', {
set: this.title.setText,
get: this.title.getText
});
};
/**
* Loads the document. Used to inject the collaborative document
* into the main controller.
*
* @param $route
* @param storage
* @returns {*}
*/
app.loadFile = function ($route, storage) {
var id = $route.current.params.fileId;
var userId = $route.current.params.user;
return storage.requireAuth(true, userId).then(function () {
return storage.getDocument(id);
});
};
app.loadFile.$inject = ['$route', 'storage'];
/**
* Initialize our application routes
*/
app.module.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/todos/:fileId/:filter', {
templateUrl: 'views/main.html',
controller: 'MainCtrl',
resolve: {
realtimeDocument: app.loadFile
}
})
.when('/create', {
templateUrl: 'views/loading.html',
controller: 'CreateCtrl'
})
.when('/install', {
templateUrl: 'views/install.html',
controller: 'InstallCtrl'
})
.otherwise({
redirectTo: '/install'
});
}]
);
app.module.value('config', CONFIG);
/**
* Set up handlers for various authorization issues that may arise if the access token
* is revoked or expired.
*/
app.module.run(['$rootScope', '$location', 'storage', function ($rootScope, $location, storage) {
// Error loading the document, likely due revoked access. Redirect back to home/install page
$rootScope.$on('$routeChangeError', function () {
$location.url('/install?target=' + encodeURIComponent($location.url()));
});
// Token expired, refresh
$rootScope.$on('todos.token_refresh_required', function () {
storage.requireAuth(true).then(function () {
// no-op
}, function () {
$location.url('/install?target=' + encodeURIComponent($location.url()));
});
});
}]);
/**
* Bootstrap the app
*/
gapi.load('auth:client:drive-share:drive-realtime', function () {
gapi.auth.init();
// Register our Todo class
app.Todo.prototype.title = gapi.drive.realtime.custom.collaborativeField('title');
app.Todo.prototype.completed = gapi.drive.realtime.custom.collaborativeField('completed');
gapi.drive.realtime.custom.registerType(app.Todo, 'todo');
gapi.drive.realtime.custom.setInitializer(app.Todo, app.Todo.prototype.initialize);
gapi.drive.realtime.custom.setOnLoaded(app.Todo, app.Todo.prototype.setup);
$(document).ready(function () {
angular.bootstrap(document, ['todos']);
});
});