Skip to content

Plotting in child window #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions build/plotcss.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var Plotly = require('../src/plotly');
var rules = {
"X,X div": "font-family:'Open Sans', verdana, arial, sans-serif;margin:0;padding:0;",
"X input,X button": "font-family:'Open Sans', verdana, arial, sans-serif;",
Expand Down Expand Up @@ -54,9 +53,4 @@ var rules = {
"Y .notifier-close:hover": "color:#444;text-decoration:none;cursor:pointer;"
};

for(var selector in rules) {
var fullSelector = selector.replace(/^,/,' ,')
.replace(/X/g, '.js-plotly-plot .plotly')
.replace(/Y/g, '.plotly-notifier');
Plotly.Lib.addStyleRule(fullSelector, rules[selector]);
}
module.exports = rules;
10 changes: 5 additions & 5 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ dragElement.init = function init(options) {

if(options.prepFn) options.prepFn(e, startX, startY);

dragCover = coverSlip();
dragCover = coverSlip(gd);

dragCover.onmousemove = onMove;
dragCover.onmouseup = onDone;
Expand Down Expand Up @@ -139,7 +139,7 @@ dragElement.init = function init(options) {
if(options.doneFn) options.doneFn(gd._dragged, numClicks);

if(!gd._dragged) {
var e2 = document.createEvent('MouseEvents');
var e2 = gd._document.createEvent('MouseEvents');
e2.initEvent('click', true, true);
initialTarget.dispatchEvent(e2);
}
Expand All @@ -159,8 +159,8 @@ dragElement.init = function init(options) {
options.element.style.pointerEvents = 'all';
};

function coverSlip() {
var cover = document.createElement('div');
function coverSlip(gd) {
var cover = gd._document.createElement('div');

cover.className = 'dragcover';
var cStyle = cover.style;
Expand All @@ -172,7 +172,7 @@ function coverSlip() {
cStyle.zIndex = 999999999;
cStyle.background = 'none';

document.body.appendChild(cover);
gd._document.body.appendChild(cover);

return cover;
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ modeBarButtons.toImage = {
click: function(gd) {
var format = 'png';

Lib.notifier('Taking snapshot - this may take a few seconds', 'long');
Lib.notifier(gd, 'Taking snapshot - this may take a few seconds', 'long');

if(Lib.isIE()) {
Lib.notifier('IE only supports svg. Changing format to svg.', 'long');
Lib.notifier(gd, 'IE only supports svg. Changing format to svg.', 'long');
format = 'svg';
}

downloadImage(gd, {'format': format})
.then(function(filename) {
Lib.notifier('Snapshot succeeded - ' + filename, 'long');
Lib.notifier(gd, 'Snapshot succeeded - ' + filename, 'long');
})
.catch(function() {
Lib.notifier('Sorry there was a problem downloading your snapshot!', 'long');
Lib.notifier(gd, 'Sorry there was a problem downloading your snapshot!', 'long');
});
}
};
Expand Down
34 changes: 17 additions & 17 deletions src/components/rangeslider/create_slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function createSlider(gd) {
var minStart = 0,
maxStart = width;

var slider = document.createElementNS(svgNS, 'g');
var slider = gd._document.createElementNS(svgNS, 'g');
helpers.setAttributes(slider, {
'class': 'range-slider',
'data-min': minStart,
Expand All @@ -43,7 +43,7 @@ module.exports = function createSlider(gd) {
});


var sliderBg = document.createElementNS(svgNS, 'rect'),
var sliderBg = gd._document.createElementNS(svgNS, 'rect'),
borderCorrect = options.borderwidth % 2 === 0 ? options.borderwidth : options.borderwidth - 1;
helpers.setAttributes(sliderBg, {
'fill': options.bgcolor,
Expand All @@ -56,7 +56,7 @@ module.exports = function createSlider(gd) {
});


var maskMin = document.createElementNS(svgNS, 'rect');
var maskMin = gd._document.createElementNS(svgNS, 'rect');
helpers.setAttributes(maskMin, {
'x': 0,
'width': minStart,
Expand All @@ -65,7 +65,7 @@ module.exports = function createSlider(gd) {
});


var maskMax = document.createElementNS(svgNS, 'rect');
var maskMax = gd._document.createElementNS(svgNS, 'rect');
helpers.setAttributes(maskMax, {
'x': maxStart,
'width': width - maxStart,
Expand All @@ -74,9 +74,9 @@ module.exports = function createSlider(gd) {
});


var grabberMin = document.createElementNS(svgNS, 'g'),
grabAreaMin = document.createElementNS(svgNS, 'rect'),
handleMin = document.createElementNS(svgNS, 'rect');
var grabberMin = gd._document.createElementNS(svgNS, 'g'),
grabAreaMin = gd._document.createElementNS(svgNS, 'rect'),
handleMin = gd._document.createElementNS(svgNS, 'rect');
helpers.setAttributes(grabberMin, { 'transform': 'translate(' + (minStart - handleWidth - 1) + ')' });
helpers.setAttributes(grabAreaMin, {
'width': 10,
Expand All @@ -97,9 +97,9 @@ module.exports = function createSlider(gd) {
helpers.appendChildren(grabberMin, [handleMin, grabAreaMin]);


var grabberMax = document.createElementNS(svgNS, 'g'),
grabAreaMax = document.createElementNS(svgNS, 'rect'),
handleMax = document.createElementNS(svgNS, 'rect');
var grabberMax = gd._document.createElementNS(svgNS, 'g'),
grabAreaMax = gd._document.createElementNS(svgNS, 'rect'),
handleMax = gd._document.createElementNS(svgNS, 'rect');
helpers.setAttributes(grabberMax, { 'transform': 'translate(' + maxStart + ')' });
helpers.setAttributes(grabAreaMax, {
'width': 10,
Expand All @@ -120,7 +120,7 @@ module.exports = function createSlider(gd) {
helpers.appendChildren(grabberMax, [handleMax, grabAreaMax]);


var slideBox = document.createElementNS(svgNS, 'rect');
var slideBox = gd._document.createElementNS(svgNS, 'rect');
helpers.setAttributes(slideBox, {
'x': minStart,
'width': maxStart - minStart,
Expand All @@ -137,8 +137,8 @@ module.exports = function createSlider(gd) {
minVal = slider.getAttribute('data-min'),
maxVal = slider.getAttribute('data-max');

window.addEventListener('mousemove', mouseMove);
window.addEventListener('mouseup', mouseUp);
gd._document.defaultView.addEventListener('mousemove', mouseMove);
gd._document.defaultView.addEventListener('mouseup', mouseUp);

function mouseMove(e) {
var delta = +e.clientX - startX,
Expand Down Expand Up @@ -189,8 +189,8 @@ module.exports = function createSlider(gd) {
}

function mouseUp() {
window.removeEventListener('mousemove', mouseMove);
window.removeEventListener('mouseup', mouseUp);
gd._document.defaultView.removeEventListener('mousemove', mouseMove);
gd._document.defaultView.removeEventListener('mouseup', mouseUp);
slider.style.cursor = 'auto';
}
});
Expand Down Expand Up @@ -222,8 +222,8 @@ module.exports = function createSlider(gd) {

function setDataRange(dataMin, dataMax) {

if(window.requestAnimationFrame) {
window.requestAnimationFrame(function() {
if(gd._document.defaultView.requestAnimationFrame) {
gd._document.defaultView.requestAnimationFrame(function() {
Plotly.relayout(gd, 'xaxis.range', [dataMin, dataMax]);
});
} else {
Expand Down
38 changes: 38 additions & 0 deletions src/css/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

// expands a plotcss selector
exports.buildFullSelector = function buildFullSelector(selector) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, would you mind moving this file and src/css/plotcss_injector.js to src/lib/. I would prefer leaving the src/css/ directory for css (and scss) files only.

I would also vote for merging the two files into one and naming it something like plotcss_utils.js

Sorry for the confusion.

var fullSelector = selector.replace(/,/, ', ')
.replace(/:after/g, '::after')
.replace(/:before/g, '::before')
.replace(/X/g, '.js-plotly-plot .plotly')
.replace(/Y/g, '.plotly-notifier');

return fullSelector;
};

// Gets all the rules currently attached to the document
exports.getAllRuleSelectors = function getAllRuleSelectors(sourceDocument) {
var allSelectors = [];

for(var i = 0; i < sourceDocument.styleSheets.length; i++) {
var styleSheet = sourceDocument.styleSheets[i];

for(var j = 0; j < styleSheet.cssRules.length; j++) {
var cssRule = styleSheet.cssRules[j];

allSelectors.push(cssRule.selectorText);
}
}

return allSelectors;
};
52 changes: 52 additions & 0 deletions src/css/plotcss_injector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var helpers = require('./helpers');
var lib = require('../lib');
var plotcss = require('../../build/plotcss');

// Inject styling information into the document containing the graph div
module.exports = function injectStyles(gd) {
// If the graph div has already been styled, bail
if(gd._plotCSSLoaded) return;

var targetSelectors = helpers.getAllRuleSelectors(gd._document);
var targetStyleSheet = null;

if(gd._document.getElementsByTagName('style').length === 0) {
var style = gd._document.createElement('style');
// WebKit hack :(
style.appendChild(gd._document.createTextNode(''));
gd._document.head.appendChild(style);
targetStyleSheet = style.sheet;
}
else {
// Just grab the first style element to append to
targetStyleSheet = gd._document.getElementsByTagName('style')[0].sheet;
}

for(var selector in plotcss) {
var fullSelector = helpers.buildFullSelector(selector);

// Don't duplicate selectors
if(targetSelectors.indexOf(fullSelector) === -1) {
if(targetStyleSheet.insertRule) {
targetStyleSheet.insertRule(fullSelector + '{' + plotcss[selector] + '}', 0);
}
else if(targetStyleSheet.addRule) {
targetStyleSheet.addRule(fullSelector, plotcss[selector], 0);
}
else lib.warn('injectStyles failed');
}
}

gd._plotCSSLoaded = true;
};
24 changes: 0 additions & 24 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,30 +388,6 @@ lib.removeElement = function(el) {
if(elParent) elParent.removeChild(el);
};

/**
* for dynamically adding style rules
* makes one stylesheet that contains all rules added
* by all calls to this function
*/
lib.addStyleRule = function(selector, styleString) {
if(!lib.styleSheet) {
var style = document.createElement('style');
// WebKit hack :(
style.appendChild(document.createTextNode(''));
document.head.appendChild(style);
lib.styleSheet = style.sheet;
}
var styleSheet = lib.styleSheet;

if(styleSheet.insertRule) {
styleSheet.insertRule(selector + '{' + styleString + '}', 0);
}
else if(styleSheet.addRule) {
styleSheet.addRule(selector, styleString, 0);
}
else lib.warn('addStyleRule failed');
};

lib.getTranslate = function(element) {

var re = /.*\btranslate\((\d*\.?\d*)[^\d]*(\d*\.?\d*)[^\d].*/,
Expand Down
5 changes: 3 additions & 2 deletions src/lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var NOTEDATA = [];

/**
* notifier
* @param {object} gd figure Object
* @param {String} text The person's user name
* @param {Number} [delay=1000] The delay time in milliseconds
* or 'long' which provides 2000 ms delay time.
* @return {undefined} this function does not return a value
*/
module.exports = function(text, displayLength) {
module.exports = function(gd, text, displayLength) {
if(NOTEDATA.indexOf(text) !== -1) return;

NOTEDATA.push(text);
Expand All @@ -30,7 +31,7 @@ module.exports = function(text, displayLength) {
if(isNumeric(displayLength)) ts = displayLength;
else if(displayLength === 'long') ts = 3000;

var notifierContainer = d3.select('body')
var notifierContainer = d3.select(gd._document.body)
.selectAll('.plotly-notifier')
.data([0]);
notifierContainer.enter()
Expand Down
18 changes: 14 additions & 4 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var Lib = require('../lib');
var Events = require('../lib/events');
var Queue = require('../lib/queue');

var injectStyles = require('../css/plotcss_injector');

var Plots = require('../plots/plots');
var Fx = require('../plots/cartesian/graph_interact');

Expand Down Expand Up @@ -54,6 +56,14 @@ Plotly.plot = function(gd, data, layout, config) {

gd = getGraphDiv(gd);

// Get the document the graph div lives in, so we can make sure things like
// drag covers are attached to the correct document
gd._document = gd.ownerDocument || window.document;

// Inject the plot styles into the document where we're plotting, bails if
// already styled
injectStyles(gd);

// Events.init is idempotent and bails early if gd has already been init'd
Events.init(gd);

Expand Down Expand Up @@ -2541,12 +2551,12 @@ function plotAutoSize(gd, aobj) {
// embedded in an iframe - just take the full iframe size
// if we get to this point, with no aspect ratio restrictions
if(gd._context.fillFrame) {
newWidth = window.innerWidth;
newHeight = window.innerHeight;
newWidth = gd._document.defaultView.innerWidth;
newHeight = gd._document.defaultView.innerHeight;

// somehow we get a few extra px height sometimes...
// just hide it
document.body.style.overflow = 'hidden';
gd._document.body.style.overflow = 'hidden';
}
else if(isNumeric(context.frameMargins) && context.frameMargins > 0) {
var reservedMargins = calculateReservedMargins(gd._boundingBoxMargins),
Expand All @@ -2563,7 +2573,7 @@ function plotAutoSize(gd, aobj) {
// provide height and width for the container div,
// specify size in layout, or take the defaults,
// but don't enforce any ratio restrictions
computedStyle = window.getComputedStyle(gd);
computedStyle = gd._document.defaultView.getComputedStyle(gd);
newHeight = parseFloat(computedStyle.height) || fullLayout.height;
newWidth = parseFloat(computedStyle.width) || fullLayout.width;
}
Expand Down
3 changes: 0 additions & 3 deletions src/plotly.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ exports.Lib = require('./lib');
exports.util = require('./lib/svg_text_utils');
exports.Queue = require('./lib/queue');

// plot css
require('../build/plotcss');

// configuration
exports.MathJaxConfig = require('./fonts/mathjax_config');
exports.defaultConfig = require('./plot_api/plot_config');
Expand Down
2 changes: 1 addition & 1 deletion src/plots/cartesian/dragbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
dragTail(zoomMode);

if(SHOWZOOMOUTTIP && gd.data && gd._context.showTips) {
Lib.notifier('Double-click to<br>zoom back out', 'long');
Lib.notifier(gd, 'Double-click to<br>zoom back out', 'long');
SHOWZOOMOUTTIP = false;
}
}
Expand Down
Loading