/
extensionloader.js
192 lines (182 loc) · 8.64 KB
/
extensionloader.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
$(document).ready(function() {
loadJSON(init);
function init(config){
var Extensions = config.Extensions;
var loaderconfig = {"initialload":false}
Extensions.forEach(element => {
let path = "extensions/"+element.name+"/contents/";
element.filestoload.cssfiles.forEach(ele => {loadjscssfile((path+ele), 'css')});
element.filestoload.jsfiles.forEach(ele => {loadjscssfile((path+ele), 'js')});
});
document.addEventListener('loadextension',function(e){
loaderconfig.Viewer = e.detail.viewer;loaderconfig.Viewer.loadExtension(e.detail.extension);
e.detail.viewer.loadExtension(e.detail.extension);
})
document.addEventListener('unloadextension',function(e){
e.detail.viewer.unloadExtension(e.detail.extension);
})
document.addEventListener('viewerinstance',function(e){
loaderconfig.Viewer = e.detail.viewer;
if (!loaderconfig.initialload) {
loadStartupExtensions();
loaderconfig.initialload = true;
}
document.getElementById(config.ListConfig.ListId).style.display = 'block';
if(config.InbuiltExtensionsConfig && config.InbuiltExtensionsConfig.CreateList === "true") ListInbuiltExtensions();
if(config.ListConfig && config.ListConfig.CreateList === "true") CreateList();
});
function loadStartupExtensions(){
Extensions.forEach(element => {
if (element.loadonstartup === "true") {
loaderconfig.Viewer.loadExtension(element.name);
}
});
}
function CreateList() {
var list = document.getElementById(config.ListConfig.ListId);
var ExtensionList = '';
let index = 0;
Extensions.forEach(element => {
if (element.includeinlist === "true") {
let name = element.name;
let checked = '';
let editoptions = '';
if(element.loadonstartup === 'true') checked = ' checked ';
if(element.editoptions === 'true') editoptions = ' <i class="fas fa-cog editoptions" data-index="'+index+'" data-toggle="modal" data-target="#editConfigModal"></i>';
ExtensionList += '<label><input class="checkextension" type="checkbox"'+checked+' name="'+name+'" value="'+name+'" data-index="'+index+'"> '+element.displayname+'</label> <i class="fas fa-info-circle details" data-toggle="popover" ></i>'+editoptions+'<br>';
}
index++;
});
list.innerHTML = ExtensionList;
var checkbox = document.getElementsByClassName('checkextension');
for (var i=0; i < checkbox.length; i++) {
checkbox.item(i).onclick = togglecustomextension;
let index = checkbox.item(i).attributes['data-index'].value;
let element = Extensions[index];
let moredetails = '';
let gif = '';
if(element.bloglink) moredetails = '<a target="_blank" href="'+element.bloglink+'">Learn more</a>';
if(element.gif) gif = '<br><img src="./extensions/'+element.name+'/extension.gif" alt="Sample Image">';
let contents = '<p>'+Extensions[index].description+'</p>'+moredetails+gif;
$(checkbox.item(i).parentNode).next().popover({
html : true,
container: 'body',
boundary: 'viewport',
title: Extensions[index].displayname,
placement:'left',
content : contents
});
$("html").on("mouseup", function (e) {
var l = $(e.target);
if (l[0].className.indexOf("popover") == -1) {
$(".popover").each(function () {
$(this).popover("hide");
});
}
});
}
if (window.extension) {
$("input:checkbox[value='"+window.extension+"']").click();
}
// $('[data-toggle="popover"]').popover();
let editbuttons = document.getElementsByClassName('editoptions');
for (var i=0; i < editbuttons.length; i++) {
let index = editbuttons.item(i).attributes['data-index'].value;
editbuttons.item(i).onclick = editextensionconfig;
}
let editoptionindex;
function editextensionconfig(e) {
editoptionindex = parseInt( e.target.getAttribute('data-index') );
let element = Extensions[editoptionindex];
console.log(element.options);
let options = JSON.stringify(element.options, undefined, 2);
document.getElementById("editextensionconfig").value = options;
document.getElementById("learnmore").setAttribute('href',element.bloglink);
}
document.getElementById("saveconfig").onclick = saveoptions;
function saveoptions() {
console.log(editoptionindex);
Extensions[editoptionindex].options = JSON.parse(document.getElementById('editextensionconfig').value);
loaderconfig.Viewer.unloadExtension(Extensions[editoptionindex].name);
loaderconfig.Viewer.loadExtension(Extensions[editoptionindex].name,Extensions[editoptionindex].options);
}
function togglecustomextension(e) {
console.log(e.target.value)
if (e.target.checked) {
loaderconfig.Viewer.loadExtension(e.target.value,Extensions[parseInt(this.dataset.index)].options)
} else {
loaderconfig.Viewer.unloadExtension(e.target.value)
}
}
}
function ListInbuiltExtensions() {
let Extensions = config.InbuiltExtensions;
let list = document.getElementById(config.InbuiltExtensionsConfig.ListId);
let ExtensionList = '';
for (let index = 0; index < Extensions.length; index++) {
let element = Extensions[index];
if (element.includeinlist !== "false") {
let checked = '';
if(element.default === 'true') checked = ' checked ';
ExtensionList += '<label><input class="checkextensionbuiltin" type="checkbox"'+checked+' name="'+element.name+'" value="'+element.name+'"> '+element.name.slice(9,element.name.length)+'</label><br>';
}
};
list.innerHTML = ExtensionList;
let checkbox = document.getElementsByClassName('checkextensionbuiltin');
for (var i=0; i < checkbox.length; i++) {
checkbox.item(i).onclick = togglebuiltinextension;
}
function togglebuiltinextension(e) {
console.log(e.target.value)
if (e.target.checked) {
loaderconfig.Viewer.loadExtension(e.target.value)
} else {
loaderconfig.Viewer.unloadExtension(e.target.value)
}
}
}
function loadjscssfile(filename, filetype){
if (filetype=="js"){
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined");
document.getElementsByTagName("head")[0].appendChild(fileref);
}
}
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'extensions/config.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(JSON.parse(xobj.responseText));
}
};
xobj.send(null);
}
});