-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.js
96 lines (74 loc) · 2.12 KB
/
wrapper.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
/*\
title: $:/plugins/shaneleonard/plotly/wrapper.js
type: application/javascript
module-type: widget
A widget for including Plotly charts in a Tiddlywiki.
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
var Plotly = require("$:/plugins/shaneleonard/plotly/plotly-latest.js");
var PlotlyWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
/*
* Inherit from the base widget class
*/
PlotlyWidget.prototype = new Widget();
/*
* Render this widget into the DOM
*/
PlotlyWidget.prototype.render = function(parent, nextSibling) {
/* Save the parent dom node */
this.parentDomNode = parent;
/* Compute our attributes */
this.computeAttributes();
this.execute();
/* Create the chart */
var plot = this.createPlot();
/* Insert the plot into the DOM */
parent.insertBefore(plot.domNode,nextSibling);
this.domNodes.push(plot.domNode);
this.updatePlot = plot.updatePlot;
if (this.updatePlot) {
this.updatePlot();
}
};
PlotlyWidget.prototype.createPlot = function(){
var div = document.createElement("div");
this.plotDiv = div;
return {
domNode: div,
updatePlot: function() { Plotly.newPlot(div, this.plotData, this.plotLayout ? this.plotLayout : {autosize: true}); }
}
};
/*
* Compute the widget attributes
*/
PlotlyWidget.prototype.execute = function() {
var d = this.getAttribute("data");
this.plotData = this.wiki.getTiddlerData(d);
if (this.hasAttribute("layout")) {
var l = this.getAttribute("layout");
this.plotLayout = this.wiki.getTiddlerData(l);
}
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
PlotlyWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if (changedAttributes)
{
Plotly.Plots.resize(this.plotDiv);
return true;
}
else
{
return false;
}
};
exports.plotly = PlotlyWidget;
})();