-
Notifications
You must be signed in to change notification settings - Fork 3
/
mfiles.js
480 lines (453 loc) · 11.9 KB
/
mfiles.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
var MFiles = (function(){
/**
* defined constants
*/
var __host = null,
__vault = null,
__token = null,
__storeToken = true,
__noTokenIsSetError = "The token is null. Cannot send request to server.",
__tokenName = 'mfiles_api_token';
/**
* The main IDs of the M-Files
* @type object
var __main_IDs = {
"objects": {
"document":0,
"documentCollection":9
},
"properties": {
"nameOrTitle": 0,
"class": 100,
"created": 20,
"lastModified": 21
},
"classes": {
"unclassifiedDocument": 0,
"report": -101,
"assignment": -100
}
};
*/
/**
* returns a list of all objects on the server
* @param int type filter by type
* @return array
*/
function getObjectTypes(type = false){
if (__token) {
return $http(__host+'/objects'+(type != false ? '/'+type : ''))
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* Download file helper - It creates a link that clicks itself
* @param int objectID The ID of the M-Files object
* @param int fileID The ID of the file inside the M-Files object
*/
function downloadFile(objectID, fileID){
// Construct the a element
return function(){
var link = document.createElement("a");
link.target = "_blank";
// Construct the uri
link.href = __host + '/objects/0/'+objectID+'/latest/files/'+fileID+'/content?auth='+__token;
document.body.appendChild(link);
link.click();
// Cleanup the DOM
document.body.removeChild(link);
delete link;
}
}
/**
* Searches documents
* @param object params Search params
* @param string listId The ID# of the UL element in which the list will be generated
* @param function callback Callback function
* @param function errCallback Error callback function
* @return array The search result
*/
function searchDocuments(params, listId, callback = false, errCallback = false){
if (__token) {
var queryString = '';
var argCount = 0;
// check if object is empty
if ( !(Object.keys(params).length === 0 && params.constructor === Object) ) {
for(var index in params) {
if (argCount == 0){
queryString += '?';
argCount++;
} else {
queryString += '&';
}
if (index == 'q'){
queryString += 'q='+params[index];
} else {
queryString += 'p'+index+'='+params[index];
}
}
}
$http(__host+'/objects/0'+queryString)
.get()
.then(function(data){
// string to JSON
data = JSON.parse(data);
// adds a download function to each file
data = createDownloadLink(data);
// generate the list with the links for the file download
generateHtmlLinks(data, listId);
if (callback != false){
callback(data);
}
},
function(data){
if (errCallback != false){
errCallback(data);
}
});
} else {
throw __noTokenIsSetError;
}
}
/**
* Append to each file a function to download itself
* @param array - Array with M-Files objects and files
* @return array - Array with M-Files object and files with download function
*/
function createDownloadLink(data) {
data.Items.forEach(function(item){
item.Files.forEach(function(file){
file.download = downloadFile(item.ObjVer.ID, file.ID);
});
});
return data;
}
function generateHtmlLinks(data, id){
data.Items.forEach(function(item){
item.Files.forEach(function(file){
var ul = document.getElementById(id);
var li = document.createElement("li");
var a = document.createElement("a");
a.onclick = file.download;
a.href = "";
a.innerHTML = file.EscapedName;
li.appendChild(a);
ul.appendChild(li);
});
});
}
/**
* Retrieve all documents belonging to a specific class
* @param int classID - ID of the specific class
* @return promise
*/
function getDocumentsByClass(classID) {
if (__token) {
return $http(__host+'/objects/0?p100='+classID)
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* Retrieve all documents collections belonging to a specific class
* @param int classID - ID of the specific class
* @return promise
*/
function getDocumentCollectionsByClass(classID) {
if (__token) {
return $http(__host+'/objects/9?p100='+classID)
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* returns the most recenly accessed objects by the current user
* @return array
*/
function getRecentlyAccessed(){
if (__token) {
return $http(__host+'/recentlyaccessedbyme')
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* Returns a list of all classes from the server
* @param int objectID - filters the classes list by object
* @param boolean byGroup - returns a list of classes grouped by parent
* @return array of objects containing the description of the classes
*/
function getClasses(objectID = false, byGroup = false){
if (__token) {
return $http(__host+'/structure/classes'+(objectID != false ? '/'+objectID : '')+(byGroup != false ? '/'+byGroup : ''))
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* getFavorites
* get user favorite objects
*/
function getFavorites(){
if (__token) {
return $http(__host+'/structure/classes'+(objectID != false ? '/'+objectID : '')+(byGroup != false ? '/'+byGroup : ''))
.get();
} else {
throw __noTokenIsSetError;
}
}
/**
* storeToken - a function to toggle whether to store token in storage or not.
* @param boolean value
*/
function storeToken(value){
if (typeof value === 'boolean' ) {
__storeToken = value;
} else {
throw "The parameter must be of type boolean.";
}
}
/**
* deletes the token from local storage
*/
function removeToken(){
__token = null;
localStorage.removeItem(__tokenName);
}
/**
* checks whether the current user is logged in or not
* @return {Boolean} [description]
*/
function isLoggedIn(){
if (__token != null){
http(__host+'/session')
.get()
.then(
function(data){
try {
var result = JSON.parse(data)
} catch (e) {
throw "Error occured while decoding JSON response. (isLoggedIn)";
}
if(result.IsLoggedToVault && result.IsLoggedToApplication){
return true;
} else {
return false;
}
}
);
}
return false;
}
/**
* sets the token to javascript and to local storage if __storeToken is true
* @param string token - the authentication token sent by X-Authentication header to M-Files
*/
function setToken(token) {
try {
token_parsed = JSON.parse(token);
} catch (e) {
throw "Error occured while decoding JSON response. (setToken)";
}
__token = token_parsed.Value;
if ( __storeToken ) {
storeToken(__token);
}
}
/**
* Returns the M-Files API token
* @return string M-Files API token
*/
function getToken() {
return __token;
}
/**
* Sets host server
* @param string host Ex: 127.0.0.1, localhost etc.
*/
function setHost(host){
__host = host;
}
/**
* Sets vault for the user to login into
* @param string vault Ex: {43D6F4FE-7BF4-4D4E-9717-C298BCC81A6B}
*/
function setVault(vault){
__vault = vault;
}
/**
* store token in browser storage
* @param string token Token used to authenticate into M-Files server using REST service
*/
function storeToken(token){
removeToken(); // make sure there is no other token by removing it
localStorage.setItem(__tokenName, token);
}
/**
* Authenticate user and retrieve token from M-Files server
* @param object credentials - It must be of form: {"Username":"user", "Password":"pass"}
* @param Accepts callback that is injected in then function in order to process the response
* @param Accepts error callback that is injected in then function in order to handle the error
*/
function auth(credentials, callback = false, errCallback = false){
// remove token from local storage if any
removeToken();
// set current vault
credentials.VaultGuid = __vault;
// authenticate
$http(__host+'/server/authenticationtokens')
.post(credentials)
.then(function(data){
setToken(data);
if (callback != false){
callback(data);
}
}, function(data){
if (errCallback != false){
errCallback(data);
}
});
}
// logout
/**
* logout the user from M-Files vault
* @param function callback - The success callback
* @param function errCallback - Error callback
*/
function logout(callback = false, errCallback = false){
$http(__host+'/session')
.delete()
.then(function(data){
removeToken();
if (callback != false){
callback(data);
}
}, function(data){
if (errCallback != false){
errCallback(data);
}
});
}
/**
* A custom JavaScript function that is used to interogate the M-Files REST API
* @param string url - The URL
* @return promise - It returns a promise that is later used to interogate the API
*/
function $http(url){
var core = {
// Method that performs the ajax request
ajax: function (method, url, args) {
var promise = new Promise( function (resolve, reject) {
var client = new XMLHttpRequest();
var uri = url;
var params = {};
// if the method is GET, then put the paramenters in the URL
if (args && (method === 'GET')) {
uri += '?';
var argcount = 0;
for (var key in args) {
if (args.hasOwnProperty(key)) {
if (argcount++) {
uri += '&';
}
uri += encodeURIComponent(key) + '=' + encodeURIComponent(args[key]);
}
}
// if the method is POST, PUT or PATCH then send the parameters as form data
} else if (args && (method === 'POST' || method === 'PUT' || method === 'PATCH')){
for (var key in args) {
if (args.hasOwnProperty(key)) {
params[key] = args[key];
}
}
}
client.open(method, uri);
// if the token is not null then send also the X-Authentication header
if (__token != null) {
client.setRequestHeader("X-Authentication", __token);
}
client.send(JSON.stringify(params));
client.onload = function () {
if (this.status >= 200 && this.status < 300) {
// Performs the function "resolve" when this.status is equal to 2xx
resolve(this.response);
} else {
// Performs the function "reject" when this.status is different than 2xx
reject(this.statusText);
}
};
client.onerror = function () {
reject(this.statusText);
};
});
return promise;
}
};
// Adapter pattern
// Returns all methods
return {
'get': function(args) {
return core.ajax('GET', url, args);
},
'post': function(args) {
return core.ajax('POST', url, args);
},
'put': function(args) {
return core.ajax('PUT', url, args);
},
'delete': function(args) {
return core.ajax('DELETE', url, args);
}
};
};
// console.log function shorcut
function log(msg){
console.log(msg);
}
/**
* This loads then this JavaScript file is loaded into the document
*/
function init(){
// get token from storage if any and save it into JavaScript
__token = localStorage.getItem(__tokenName) || null;
}
/**
* Run the init function
*/
init();
/**
* Returns the functions and the variables organized in categories
* Config - Settings to current JS instance
* User - User related functions
* Vault - Vault related functions
*/
return {
config:{
__tokenName: __tokenName,
getToken: getToken,
storeToken: storeToken,
setHost: setHost,
setVault: setVault,
generateHtmlLinks: generateHtmlLinks,
},
user: {
auth: auth,
logout: logout,
isLoggedIn: isLoggedIn
},
vault: {
getObjectTypes: getObjectTypes,
getClasses: getClasses,
getRecentlyAccessed: getRecentlyAccessed,
getDocumentsByClass: getDocumentsByClass,
getDocumentCollectionsByClass: getDocumentCollectionsByClass,
searchDocuments: searchDocuments,
}
}
})();