Well, it is all very rudimentary still, but the idea is the following:
In my plugin I import the library and instantiate it, then create the div to pass to it. Then I call the constructor of my library's main object with the div as its argument:
''''
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
//The library I use, besogo:
var besogoPlayer = require("$:/plugins/cleinias/SgfEditor/besogo").besogo
var GoGameWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
GoGameWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
GoGameWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
var div = this.document.createElement("div");
this.goGamePlayer = besogoPlayer;
//
// This is where I would need to know the width of the future widget, so I can pass it to the create function
// this.goGamePlayer.create(div);
var twSgfFile = this.getAttribute("sgfFile");
parent.insertBefore(div,nextSibling);
this.domNodes.push(div);
};
''''
he goGamePlayer.create(div) function is the call to the besogo library, where all the magic happens. Its resizer function computes a bunch of parameters. Withouth going into unnecessary details (I append the function below, relevant lines are bolded and italicized), the width of the parentElement is crucial to determining the overall width of the final widget. For now, I got around the problem in testing by hardcoding a number into the code, but it would really be nice to have a flexible and responsive solution
Cheers,
S.
''''
resizer = function() {
var windowHeight = window.innerHeight, // Viewport height
// Calculated width of parent element
parentWidth = parseFloat(getComputedStyle(container.parentElement).width),
maxWidth = +(options.maxwidth || -1),
orientation = options.orient || 'auto',
portraitRatio = +(options.portratio || 200) / 100,
landscapeRatio = +(options.landratio || 200) / 100,
minPanelsWidth = +(options.minpanelswidth || 350),
minPanelsHeight = +(options.minpanelsheight || 400),
minLandscapeWidth = +(options.transwidth || 600),
// Initial width parent
width = (maxWidth > 0 && maxWidth < parentWidth) ? maxWidth : parentWidth,
height; // Initial height is undefined
// Determine orientation if 'auto' or 'view'
if (orientation !== 'portrait' && orientation !== 'landscape') {
if (width < minLandscapeWidth || (orientation === 'view' && width < windowHeight)) {
orientation = 'portrait';
} else {
orientation = 'landscape';
}
}
if (orientation === 'portrait') { // Portrait mode
if (!isNaN(portraitRatio)) {
height = portraitRatio * width;
if (panelsDiv) {
height = (height - width < minPanelsHeight) ? width + minPanelsHeight : height;
}
} // Otherwise, leave height undefined
} else if (orientation === 'landscape') { // Landscape mode
if (!panelsDiv) { // No panels div
height = width; // Square overall
} else if (isNaN(landscapeRatio)) {
height = windowHeight;
} else { // Otherwise use ratio
height = width / landscapeRatio;
}
if (panelsDiv) {
// Reduce height to ensure minimum width of panels div
height = (width < height + minPanelsWidth) ? (width - minPanelsWidth) : height;
}
}
setDimensions(width, height);
container.style.width = width + 'px';
}
''''