forked from directus/v8-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
308 lines (252 loc) · 8.17 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
define(function(require, exports, module) {
"use strict";
// Load dependent Modules
var Handlebars = require('handlebars'),
typetools = require("typetools");
// Globally load Handlebars helpers
require('helpers');
// Load layout manager so it can be configured
require('plugins/backbone.layoutmanager');
// Load Backbone Model Track it plugin
// Track Model changes
require('plugins/backbone.trackit');
// Globally load Bootstrap plugins
require('plugins/bootstrap-dropdown');
require('plugins/typeahead');
var app = {
progressView: undefined,
alertViews: [],
lockScreen: function() {
this.noScroll = true;
},
unlockScreen: function() {
this.noScroll = false;
},
//bInactive true if logged out because inactive
logOut: function(bInactive) {
//if binactive pass url parameter
if(bInactive) {
window.location.href = app.API_URL + "auth/logout/inactive";
} else {
window.location.href = app.API_URL + "auth/logout";
}
},
// @TODO: remove this
//logErrorToServer: function(type, message, details) {
// var user = app.users.getCurrentUser(), email = 'n/a';
//
// if (user) {
// email = user.get('email');
// }
//
// var data = {
// type: type,
// message: message,
// details: details,
// page: location.href,
// user_email: email
// };
//
// $.post(app.API_URL + 'exception', JSON.stringify(data))
// .done(function(response) {
// console.log(response.response);
// })
// .error(function(obj) {
// console.log('FAILED TO LOG ERROR'+obj.responseText);
// });
//},
// http://stackoverflow.com/a/1830844
isNumber: function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
},
evaluateExpression: function(a, operator, b) {
switch (operator) {
case '==':
return (a == b);
case '===':
return (a === b);
}
},
getCurrentGroup: function() {
var user = app.users.getCurrentUser();
return user.get('group');
},
getBookmarks: function() {
return app.bookmarks;
},
deepClone: function(data) {
return JSON.parse(JSON.stringify(data));
},
affix: function() {
var sidebarOffset = $('#sidebar').offset();
var navbarHeight = $('.navbar').height();
var stickyHeight = parseInt(sidebarOffset.top,10) - parseInt(navbarHeight,10) - 20;
var stuck = false;
$(window).scroll(function(e){
var scrollTop = $(window).scrollTop();
if(!stuck && scrollTop >= stickyHeight){
//console.log("stuck");
stuck = true;
$("#sidebar").addClass('affix-sidebar');
} else if(stuck && scrollTop < stickyHeight){
//console.log("unstuck");
stuck = false;
$("#sidebar").removeClass('affix-sidebar');
}
});
}
};
app.sendFiles = function(files, callback, progressCallback) {
var formData = new FormData();
var success = function(responseData) {
callback.apply(this, [responseData]);
};
if (files instanceof File) files = [files];
_.each(files, function(file, i) {
formData.append('file'+i, file);
});
//app.trigger('progress', 'Uploading');
$.ajax({
url: app.API_URL + 'upload',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
success: success,
error: function() {
app.trigger('alert','upload failed', arguments);
callback.apply(this, []);
},
xhr: function() { // custom xhr
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload && progressCallback){ // check if upload property exists
myXhr.upload.addEventListener('progress',progressCallback, false); // for handling the progress of the upload
}
return myXhr;
},
});
};
app.sendLink = function(link, callback) {
var success = function(responseData) {
callback.apply(this, [responseData]);
};
$.ajax({
url: app.API_URL + 'upload/link',
type: 'POST',
data: {
'link': link
},
success: success,
error: function(err1, err2, err3) {
app.trigger('alert','upload failed', arguments);
//console.log('ERRRRRROOOORRR!!', err1, err2, err3);
}
});
};
// TODO: Move to a Directus backbone model
// change status or delete item
app.changeItemStatus = function(model, value, options) {
var hasStatusColumn = model.has(app.statusMapping.status_name);
var goingToDelete = value == app.statusMapping.deleted_num;
if (goingToDelete && !hasStatusColumn) {
// https://github.com/RNGR/Directus/issues/960
// Pass {wait: true} if you'd like to wait for the server to respond
// before removing the model from the collection.
model.destroy(options);
} else {
var attributes = {};
attributes[app.statusMapping.status_name] = value;
model.save(attributes, options);
}
};
app.changeCollectionStatus = function(collection, value, options) {
var model = collection.at(0);
var hasStatusColumn = model.has(app.statusMapping.status_name);
var goingToDelete = value == app.statusMapping.deleted_num;
if (goingToDelete && !hasStatusColumn) {
collection.destroyAll(options);
} else {
collection.saveAll(options);
}
};
// check if string has this format "D, d M Y H:i:s"
app.isStringADate = function(date) {
return (typeof date === "string") ? !!date.match(/^([a-zA-Z]{3})\, ([0-9]{2}) ([a-zA-Z]{3}) ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/) : false;
};
// Agnus Croll:
// http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
Object.toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
//Give forms the ability to serialize to objects
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
// Localize or create a new JavaScript Template object.
var JST = window.JST = window.JST || {};
// Configure LayoutManager with Backbone Boilerplate defaults.
Backbone.Layout.configure({
// Allow LayoutManager to augment Backbone.View.prototype.
manage: true,
prefix: "app/templates/",
fetchTemplate: function(path) {
// Concatenate the file extension.
// If template is not a path but instead Handlebars.Compile
if (typeof path === 'function') {
return path;
}
path = path + ".html";
// If cached, use the compiled template.
if (JST[path]) {
return JST[path];
}
// Put fetch into `async-mode`.
var done = this.async();
// Seek out the template asynchronously.
// ASYNC is causing render-order trouble, use sync now since it will be compiled anyway
//$.get(app.root + path, function(contents) {
// done(JST[path] = Handlebars.compile(contents));
//});
$.ajax({
url: app.root + path,
global: false,
async: false,
success: function(contents) {
done(JST[path] = Handlebars.compile(contents));
}
});
}
});
window.app = app;
// Mix Backbone.Events, modules, typetools, and layout management into the app object.
app = _.extend(app, {
// Create a custom object with a nested Views object.
module: function(additionalProps) {
return _.extend({ Views: {} }, additionalProps);
},
// Helper for using layouts.
useLayout: function(options) {
// Create a new Layout with options.
var layout = new Backbone.Layout(_.extend({
el: "body"
}, options));
// Cache the refererence.
this.layout = layout;
return this.layout;
}
}, Backbone.Events, typetools);
module.exports = app;
});