Skip to content

Commit

Permalink
cleanup of the js code
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Nov 2, 2018
1 parent be0c634 commit 4e372a9
Showing 1 changed file with 68 additions and 52 deletions.
120 changes: 68 additions & 52 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,16 @@ export class Manager extends HTMLManager {
}
})
console.log('views', all_view_promises);
Promise.all(all_view_promises).then((views) => {
views.forEach(async (view) => {
// convert bqplot figures by a static png before we do the html2canvas
if(view.model.name == 'FigureModel') {
console.log(view)
// TODO: change bqplot such that we have a promise to wait for
// instead of polling it
while(!view.mark_views) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
let marks = await Promise.all(view.mark_views.views);

let all_views = await Promise.all(all_view_promises);
let bqplot_figure_views = all_views.filter((view) => view.model.name == 'FigureModel');
console.log('bqplot_figure_views', bqplot_figure_views);
await Promise.all(bqplot_figure_views.map(async (view) => {
while(!view.mark_views) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
let marks = await Promise.all(view.mark_views.views);
await new Promise((resolve) => {
setTimeout(async () => {
// this is code from bqplot, make bqplot such that we can rely on a method
// of figure
var xml = view.get_svg();
Expand All @@ -149,49 +147,67 @@ export class Manager extends HTMLManager {
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
view.el.parentElement.replaceChild(canvas, view.el)
}
})
})
Promise.all(all_view_promises).then((views) => {
views.forEach(async (view) => {
let callbacks = (window as any)._webgl_update_callbacks;
console.log('callbacks', callbacks)
if(callbacks) {
callbacks.forEach((callback) => callback())
}
// let dependencies = [];
if(view.model.name == 'LeafletMapModel') {
// TODO: change jupyter-leaflet such that we have a promise to wait for
// instead of polling it
while(!view.obj) {
await new Promise((resolve) => setTimeout(resolve, 100));
view.el = canvas; // dirty, but then html2canvas can find it
console.log('bqplot figure replaced by snapshot')
resolve()
}, 1000);
});
}));
/*await views.map((view) => {
return new Promise(async (resolve_view) => {
// convert bqplot figures by a static png before we do the html2canvas
if(view.model.name == 'FigureModel') {
console.log(view)
// TODO: change bqplot such that we have a promise to wait for
// instead of polling it
resolve_view();
} else {
// await Promise.resolve(1);
resolve_view();
await Promise.resolve(1);
}
for(var i=0; i < view.layer_views.views.length; i++) {
let layer = await view.layer_views.views[i];
// wait for max 10 seconds
let timeout = new Promise((resolve) => {setTimeout(() => {console.log('timeout'); resolve()}, 10*1000)});
let loaded = new Promise((resolve) => {console.log('loaded'); layer.obj.on('load', resolve)})
layer.obj.on('load', () => console.log('loaded!!'))
await Promise.race([timeout, loaded]);
console.log('done')
});
}))
})*/
console.log('All views converted')
views.forEach(async (view) => {
let callbacks = (window as any)._webgl_update_callbacks;
console.log('callbacks', callbacks)
if(callbacks) {
callbacks.forEach((callback) => callback())
}
// let dependencies = [];
if(view.model.name == 'LeafletMapModel') {
// TODO: change jupyter-leaflet such that we have a promise to wait for
// instead of polling it
while(!view.obj) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
for(var i=0; i < view.layer_views.views.length; i++) {
let layer = await view.layer_views.views[i];
// wait for max 10 seconds
let timeout = new Promise((resolve) => {setTimeout(() => {console.log('timeout'); resolve()}, 10*1000)});
let loaded = new Promise((resolve) => {console.log('loaded'); layer.obj.on('load', resolve)})
layer.obj.on('load', () => console.log('loaded!!'))
await Promise.race([timeout, loaded]);
console.log('done')

}
}
setTimeout(() => {
console.log('make screenshot')
html2canvas(view.el, {useCORS: true}).then(canvas => {
// document.body.appendChild(canvas)
view.el.parentElement.appendChild(canvas)
let imageData = canvas.toDataURL('image/png');
let outputElement = view.el.parentElement.parentElement;''
let cell_index = Number(outputElement.getAttribute('data-nb-cell-index'));
let output_index = Number(outputElement.getAttribute('data-nb-output-index'));
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "/send_snapshot");
xmlHttp.send(JSON.stringify({cell_index: cell_index, output_index: output_index, image_data: imageData}));
});
}, 1000);
})
}
setTimeout(() => {
console.log('make screenshot')
html2canvas(view.el, {useCORS: true}).then(canvas => {
// document.body.appendChild(canvas)
view.el.parentElement.appendChild(canvas)
let imageData = canvas.toDataURL('image/png');
let outputElement = view.el.parentElement.parentElement;''
let cell_index = Number(outputElement.getAttribute('data-nb-cell-index'));
let output_index = Number(outputElement.getAttribute('data-nb-output-index'));
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "/send_snapshot");
xmlHttp.send(JSON.stringify({cell_index: cell_index, output_index: output_index, image_data: imageData}));
});
}, 1000);
})


Expand Down

0 comments on commit 4e372a9

Please sign in to comment.