Skip to content

Commit

Permalink
CPT editor loads all extension files
Browse files Browse the repository at this point in the history
  • Loading branch information
christianp committed Aug 25, 2022
1 parent dfc2d1a commit 88a6c64
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
75 changes: 51 additions & 24 deletions editor/static/js/custom_part_type/edit.js
Expand Up @@ -355,35 +355,67 @@ $(document).ready(function() {

this.cpt = cpt;

["location","name","edit_url","hasScript","url","scriptURL","author","pk"].forEach(function(k) {
["location","name","edit_url","hasScript","url","script_url","author","pk","scripts"].forEach(function(k) {
ext[k] = data[k];
});

this.used = ko.observable(false);
this.loading = ko.observable(false);
this.loaded = ko.observable(false);
this.error = ko.observable(false);

ko.computed(function() {
try {
if(this.used()) {
if(!this.loaded()) {
try {
Numbas.activateExtension(this.location);
this.loaded(true);
} catch(e) {
console.error(e);
setTimeout(function() {
this.error(true);
},1);
if(this.used()) {
if(!this.loaded()) {
try {
ext.load();
} catch(e) {
console.error(e);
setTimeout(function() {
this.error(true);
},1);
}
}
this.cpt.find_input_widgets();
}
this.cpt.find_input_widgets();
}
}catch(e) {
} catch(e) {
console.error(e);
}
},this);
}
Extension.prototype = {
load: function() {
this.loading(true);
var ext = this;
if(this.loaded()) {
return;
}
var script_promises = [];
this.scripts.forEach(function(name) {
var script = document.createElement('script');
script.setAttribute('src', ext.script_url+name);
var promise = new Promise(function(resolve,reject) {
script.addEventListener('load',function(e) {
resolve(e);
});
script.addEventListener('error',function(e) {
reject(e);
});
});
script_promises.push(promise);
document.head.appendChild(script);
});
Promise.all(script_promises).then(function() {
Numbas.activateExtension(ext.location);
ext.loaded(true);
}).catch(function(err) {
console.error(err);
}).finally(function() {
ext.loading(false);
});
}
};


var CustomPartType = Editor.custom_part_type.CustomPartType = function(data, save_url, set_access_url) {
Expand Down Expand Up @@ -419,11 +451,11 @@ $(document).ready(function() {
pt.setTab('description')();
}

this.input_widgets = Editor.custom_part_type.input_widgets.map(function(data) {
this.input_widgets = ko.observableArray(Editor.custom_part_type.input_widgets.map(function(data) {
return new InputWidget(pt,data);
});
}));
this.find_input_widgets();
this.input_widget = ko.observable(this.input_widgets[0])
this.input_widget = ko.observable(this.input_widgets()[0])
this.input_options = {
correctAnswer: ko.observable(''),
hint: new MaybeStaticOption('','""')
Expand Down Expand Up @@ -574,7 +606,7 @@ $(document).ready(function() {
find_input_widgets: function() {
var cpt = this;
Object.values(Numbas.answer_widgets.custom_widgets).forEach(function(w) {
if(!cpt.input_widgets.find(function(iw) { return w.name==iw.name; })) {
if(!cpt.input_widgets().find(function(iw) { return w.name==iw.name; })) {
cpt.input_widgets.push(new InputWidget(cpt, {
name: w.name,
niceName: w.niceName,
Expand Down Expand Up @@ -621,7 +653,7 @@ $(document).ready(function() {
pt.current_marking_note(pt.marking_notes()[0]);
}

tryLoadMatchingId(data,'input_widget','name',this.input_widgets,this);
tryLoadMatchingId(data,'input_widget','name',this.input_widgets(),this);
if('input_options' in data) {
tryLoad(data.input_options,['correctAnswer'],this.input_options);
this.input_options.hint.load(data.input_options.hint);
Expand Down Expand Up @@ -1119,11 +1151,6 @@ $(document).ready(function() {

Numbas.queueScript('knockout',[],function() {});
var deps = ['jme-display','jme','answer-widgets'];
if('extensions' in item_json.data) {
item_json.data.extensions.forEach(function(extension) {
deps.push('extensions/'+extension+'/'+extension+'.js');
});
};
Numbas.queueScript('start-editor',deps,function() {
try {
var item_json = window.item_json;
Expand Down
29 changes: 13 additions & 16 deletions editor/static/js/question/edit.js
Expand Up @@ -397,7 +397,7 @@ $(document).ready(function() {
this.baseScopeWithoutConstants= ko.pureComputed(function() {
var jme = Numbas.jme;
var scope = new jme.Scope(jme.builtinScope);
var extensions = this.extensions().filter(function(e){return e.used_or_required()});
var extensions = this.extensions().filter(function(e){return e.used_or_required() && e.loaded()});
for(var i=0;i<extensions.length;i++) {
var extension = extensions[i].location;
if(extension in Numbas.extensions && 'scope' in Numbas.extensions[extension]) {
Expand Down Expand Up @@ -1767,35 +1767,33 @@ $(document).ready(function() {

ko.computed(function() {
try {
if(this.used_or_required()) {
if(!this.loaded()) {
try {
ext.load();
} catch(e) {
console.error(e);
setTimeout(function() {
ext.error(true);
},1);
if(this.used_or_required()) {
if(!this.loaded()) {
try {
ext.load();
} catch(e) {
console.error(e);
setTimeout(function() {
ext.error(true);
},1);
}
}
find_jme_types();
}
find_jme_types();
}
}catch(e) {
} catch(e) {
console.error(e);
}
},this);
}
Extension.prototype = {
load: function() {
console.log('loading',this.name);
this.loading(true);
var ext = this;
if(this.loaded()) {
return;
}
var script_promises = [];
this.scripts.forEach(function(name) {
console.log('load script',name);
var script = document.createElement('script');
script.setAttribute('src', ext.script_url+name);
var promise = new Promise(function(resolve,reject) {
Expand All @@ -1822,7 +1820,6 @@ $(document).ready(function() {
}
}


function Ruleset(exam,data)
{
this.name = ko.observable('');
Expand Down
6 changes: 0 additions & 6 deletions editor/templates/custom_part_type/edit.html
Expand Up @@ -20,12 +20,6 @@

{% include 'numbas_runtime.html' %}

{% for extension in extensions %}
{% if extension.hasScript %}
<script src="{% sstatic extension.scriptURL %}" type="text/javascript"></script>
{% endif %}
{% endfor %}

<script type="text/javascript">
$(document).ready(function() {
Editor.custom_part_type = {};
Expand Down

0 comments on commit 88a6c64

Please sign in to comment.