-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRevitServer.js
539 lines (454 loc) · 21.4 KB
/
RevitServer.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
var SERVICE_URL = "http://localhost/RevitServerAdminRESTService/AdminRESTService.svc/";
var RevitServer = {
//Revit Server API requires certain headers each time
//we'll store these here so we dont ahve to re-make them.
getCommonHeaders: function () {
var headers = {
'User-Name': 'HtmlUser',
'User-Machine-Name': 'localhost',
'Operation-GUID': Utils.getGuid() //GUID is used to identify each request.
};
return headers;
},
/*
* Gets the general server info
*/
getServerInfo: function () {
//ajax call from Jquery
$.ajax({
url: SERVICE_URL + "serverProperties",
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
var serverNameText = data.CentralServerName;
if (data.IsCentralServer)
serverNameText += ' (Central Server)';
var branchHtml = "<li class='closed' id='serverNode'>";
branchHtml += "<div class='hitareaexpandable-hitarea lastExpandable-hitarea'></div>";
branchHtml += "<span class='folder selectable' style='background: url(images/ServerIcon.PNG) 0 0 no-repeat;'>"
+ serverNameText + "</span>";
branchHtml += "<span style='display:none' class='fullPath'> </span>";
branchHtml += "<ul></ul></li>";
var branches = $(branchHtml).appendTo("#rootNode");
$("#Folders1").treeview(
{ add: branches }
);
//theres a bug with treeview plugin that makes the +/- box not work
// here is my hack to get it working
$("#Folders1").find("div.hitarea").click($("#Folders1").find("span").click);
//get the rest of the folders
//start on folder " " which is the shortcut to get the base folders
RevitServer.getFolders(" ", "#serverNode");
},
error: function (data) {
alert('error' + data);
}
});
},
/*
* Gets all the folders recursively starting at the currentPath
*/
getFolders: function (currentPath, parent) {
// first, get the lock status of the descendants, then on the callback, get the folder info
// this is so we can indicate that the folder is locked
$.ajax({
url: SERVICE_URL + currentPath + '/descendent/locks',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (lockData) {
//now get the actual folder info
$.ajax({
url: SERVICE_URL + currentPath + '/contents',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
//add all the subfolders
for (var i = 0; i < data.Folders.length; i++) {
//this line looks funny.. but /.../gi is a global string search, and \\ is how we write a backslash
// we are replacing the backslash character with a pipe character | which is how the API deals with subfolders
var path = data.Path.replace(/\\/gi, '|');
if (path == " ")
path = "";
else
path = path + "|";
//we are storign the path to this folder so we can append to it
var pathid = data.Path.replace(/\\/gi, '').replace(' ', '') + data.Folders[i].Name;
var lockedClass = '';
//check if its locked or not
if (lockData.Items) {
var folderPathToCheck = data.Path + '\\' + data.Folders[i].Name;
if ($.inArray(folderPathToCheck, lockData.Items) >= 0) {
//yes, there is a lock on it
lockedClass = 'locked';
}
}
//construct the html
var branchHtml = "<li class='closed' id='" + pathid + "'>";
branchHtml += "<div class='hitarea closed-hitarea expandable-hitarea lastExpandable-hitarea'></div>";
branchHtml += "<span class='folder selectable " + lockedClass + "'>" + data.Folders[i].Name + "</span>";
branchHtml += "<span style='display:none' class='fullPath'>" + path + data.Folders[i].Name + "</span>";
branchHtml += "<ul></ul></li>";
//append the new branch to the others and update treeview
var branches = $(branchHtml).appendTo(parent + "> ul");
$("#Folders1").treeview(
{ add: branches }
);
//theres a bug with treeview plugin that makes the +/- box not work
// here is my hack to get it working
$("#Folders1").find("div.hitarea").click($("#Folders1").find("span").click);
//recursively call the get folders, but this time call it with the sub folder.
RevitServer.getFolders(path + data.Folders[i].Name, "#" + pathid);
}
//add all the models in this folder
for (var x = 0; x < data.Models.length; x++) {
var lockedClass = '';
//check if its locked or not
if (lockData.Items) {
var folderPathToCheck = data.Path + '\\' + data.Models[x].Name;
if ($.inArray(folderPathToCheck, lockData.Items) >= 0) {
//yes, there is a lock on it
lockedClass = 'locked';
}
}
//construct the html
var branchHtml = "<li>";
branchHtml += '<span class="file selectable ' + lockedClass + '">' + data.Models[x].Name + "</span>";
branchHtml += "<span style='display:none' class='fullPath'>" + path + '|' + data.Models[x].Name + "</span></li>";
var branches = $(branchHtml).appendTo(parent + "> ul");
$("#Folders1").treeview(
{ add: branches }
);
}
$('.selectable:not(.setup)').click(RevitServer.selectedFolder).addClass('setup');
}
});
}
});
},
/*
* Event for when a folder is clicked
* Sets up the display on the right
*/
selectedFolder: function (event) {
//highlight the current selection
$('.currentSelection').removeClass('currentSelection');
$(this).addClass('currentSelection');
//clear details pane
$('#details').html(' ');
$('#modelhistory').html(' ');
$('#picture').html(' ');
//setup lock icon
if ($(this).hasClass('locked')) {
$('#LockButton').attr("src", "Images/6UnlockLock.png");
} else {
$('#LockButton').attr("src", "Images/6Lock.png");
}
//show details for current selection
//check for folder
if ($(this).hasClass('folder')) {
//find its path
var path = $(this).siblings('.fullPath').html();
var fileName = $(this).html();
//get the full info on the directory
$.ajax({
url: SERVICE_URL + path + '/DirectoryInfo',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
var updatedDateString = data.DateModified.replace('/Date(', '').replace(')/', '');
var updatedDate = new Date();
updatedDate.setTime(updatedDateString);
var createdDateString = data.DateCreated.replace('/Date(', '').replace(')/', '');
var createdDate = new Date();
createdDate.setTime(createdDateString);
//construct html to display on right
var html = "<h1>" + fileName + "</h1>";
html += "Last updated: " + updatedDate.toString("d-MMM-yyyy HH:mm") + "<br />";
html += "Date Created: " + createdDate.toString("d-MMM-yyyy HH:mm") + "<br />";
html += "Number of models: " + data.ModelCount + "<br />";
html += "Number of folders: " + data.FolderCount + "<br />";
html += "Size: " + (data.Size / 1024).toFixed(2) + "KB <br />";
$('#details').html(html);
}
});
}
//check if its a file
if ($(this).hasClass('file')) {
//find its path
var path = $(this).siblings('.fullPath').html();
var fileName = $(this).html();
//unfortunately getting the thumbnail doesn't work at present
//left here for future action
// //get the thumbnail
// $.ajax({
// url: SERVICE_URL + path + '/thumbnail?width=64&height=64',
// headers: RevitServer.getCommonHeaders(),
// success: function (data) {
// var encodedData = Base64.encode(data);
// // $('#picture').html('<img src="data:image/png;base64,' + encodedData + '" >');
// //doesn't seem to work for some reason.
// }
// });
//this isn't clear via the API, but you can get the info for a file the same way you do a directory
$.ajax({
url: SERVICE_URL + path + '/DirectoryInfo',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
var updatedDateString = data.DateModified.replace('/Date(', '').replace(')/', '');
var updatedDate = new Date();
updatedDate.setTime(updatedDateString);
var createdDateString = data.DateCreated.replace('/Date(', '').replace(')/', '');
var createdDate = new Date();
createdDate.setTime(createdDateString);
//construct html to display on right
var html = "<h1>" + fileName + "</h1>";
html += "Last updated: " + updatedDate.toString("d-MMM-yyyy HH:mm") + "<br />";
html += "Date Created: " + createdDate.toString("d-MMM-yyyy HH:mm") + "<br />";
html += "Size: " + (data.Size / 1024).toFixed(2) + "KB <br />";
$('#details').html(html);
}
});
// also get the history
$.ajax({
url: SERVICE_URL + path + '/history',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
var table = "<br /><h1>Submission history</h1><table id='historyTable'>";
//headings
table += "<thead><tr>";
table += "<th>Version</th>";
table += "<th>User</th>";
table += "<th>Date</th>";
table += "<th>Comment</th>";
table += "<th>Model Size</th>";
table += "<th>Support Files</th>";
table += "</tr></thead><tbody>";
for (var currentItem = 0; currentItem < data.Items.length; currentItem++) {
var item = data.Items[currentItem];
var dateString = item.Date.replace('/Date(', '').replace(')/', '');
var date = new Date();
date.setTime(dateString);
table += "<tr>";
//construct html tto display on right
table += "<td>" + item.VersionNumber + "</td>";
table += "<td>" + item.User + "</td>";
table += "<td>" + date.toString("d-MMM-yyyy HH:mm") + "</td>";
table += "<td>" + item.Comment + "</td>";
table += "<td>" + (item.ModelSize / 1024).toFixed(2) + "KB</td>";
table += "<td>" + (item.SupportSize / 1024).toFixed(2) + "KB</td>";
table += "</tr>";
}
table += "</tbody></table>";
$('#modelhistory').html(table);
}
});
}
},
//Event handlers for all of the buttons
Operations: {
/*
* Creates a new folder
*/
createNew: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
var fileName = window.prompt("Enter a name for the folder", "");
$.ajax({
url: SERVICE_URL + currentPath + '|' + fileName,
type: 'PUT',
headers: RevitServer.getCommonHeaders(),
success: function (data) {
alert('created');
}
});
},
/*
* Deletes the selected item
*/
remove: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
if (confirm("Are you sure you want to delete this?")) {
$.ajax({
url: SERVICE_URL + currentPath + '?newObjectName=',
type: 'DELETE',
cache: false,
headers: RevitServer.getCommonHeaders(),
success: function (data) {
alert(currentPath + ' was deleted');
$('.currentSelection').parent().remove();
}
});
}
},
/*
* Cuts the selected item
*/
cut: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
RevitServer.Operations.ClipBoard = currentPath;
RevitServer.Operations.ClipBoardItem = $('.currentSelection').parent();
RevitServer.Operations.ClipBoardIsCut = true;
$('#PasteButton').attr("src", "Images/5PasteReady.png");
},
/*
* Copies selected item
*/
copy: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
RevitServer.Operations.ClipBoard = currentPath;
RevitServer.Operations.ClipBoardItem = $('.currentSelection').parent();
RevitServer.Operations.ClipBoardIsCut = false;
$('#PasteButton').attr("src", "Images/5PasteReady.png");
},
/*
* Pastes what is on the clipboard
*/
paste: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
if (!RevitServer.Operations.ClipBoard) {
alert("You must cut/copy first");
return;
}
var pasteAction = 'Copy';
if (RevitServer.Operations.ClipBoardIsCut) {
pasteAction = 'Move';
}
if (RevitServer.Operations.ClipBoardIsCut) {
if (confirm("Are you sure you want to move " + RevitServer.Operations.ClipBoard + " into " + currentPath)) {
$.ajax({
url: SERVICE_URL + currentPath + '/descendent?sourceObjectPath=' + RevitServer.Operations.ClipBoard
+ '&pasteAction=' + pasteAction + '&duplicateOption=CopyIncrement'
,
type: 'post',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
//recreate the tree, this could be done dynamically but for now, we're just wiping it.
$("#leftContent").html(RevitServer.LeftContent);
RevitServer.initTree();
}
});
}
}
else {
alert("pasting " + RevitServer.Operations.ClipBoard + " into " + currentPath);
$.ajax({
url: SERVICE_URL + currentPath + '/descendent?sourceObjectPath=' + RevitServer.Operations.ClipBoard
+ '&pasteAction=' + pasteAction + '&duplicateOption=CopyIncrement'
,
type: 'post',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
//recreate the tree, this could be done dynamically but for now, we're just wiping it.
$("#leftContent").html(RevitServer.LeftContent);
RevitServer.initTree();
}
});
}
RevitServer.Operations.ClipBoard = null;
RevitServer.Operations.ClipBoardItem = null;
RevitServer.Operations.ClipBoardIsCut = false;
$('#PasteButton').attr("src", "Images/5Paste.png");
},
/*
* Toggles the lock on the selected item
*/
toggleLock: function () {
var currentPath = Utils.getSelectedPath();
if (!currentPath)
return;
var selected = $('.currentSelection');
//check if its currently locked
if ($(selected).hasClass('locked')) {
//it is, unlock it
if (confirm("Are you sure you want to unlock this?")) {
$.ajax({
url: SERVICE_URL + currentPath + '/lock?objectMustExist=false',
type: 'DELETE',
headers: RevitServer.getCommonHeaders(),
cache: false,
success: function (data) {
$('.currentSelection').removeClass('locked');
//change the icon
$('#LockButton').attr("src", "Images/6Lock.png");
}
});
}
} else {
//it isnt, lock it
if (confirm("Are you sure you want to lock this?")) {
$.ajax({
url: SERVICE_URL + currentPath + '/lock',
type: 'PUT',
headers: RevitServer.getCommonHeaders(),
success: function (data) {
$('.currentSelection').addClass('locked');
//change the icon
$('#LockButton').attr("src", "Images/6UnlockLock.png");
}
});
}
}
}
},
/*
* Sets up the tree. Stores HTML in a variable so it can be recreated later
*/
initTree: function () {
//save the left content before the treeview so we can rest
RevitServer.LeftContent = $("#leftContent").html();
//setup treeview
$("#Folders1").treeview();
//get the server info which will trigger the rest recursively
RevitServer.getServerInfo();
},
/*
* Initialisation functions
*/
init: function () {
RevitServer.initTree();
//add handlers to the buttons
$('#CreateNewButton').click(RevitServer.Operations.createNew);
$('#DeleteButton').click(RevitServer.Operations.remove);
$('#CutButton').click(RevitServer.Operations.cut);
$('#CopyButton').click(RevitServer.Operations.copy);
$('#PasteButton').click(RevitServer.Operations.paste);
$('#LockButton').click(RevitServer.Operations.toggleLock);
}
};
var Utils = {
//taken from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
getGuid: function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
},
getSelectedPath: function () {
var selected = $('.currentSelection');
//find its path
var path = $(selected).siblings('.fullPath').html();
var fileName = $(selected).html();
if (!fileName) {
alert('Please select a file/folder first');
return null;
}
return path;
}
};
//entry point
$(document).ready(function () {
RevitServer.init();
});