Skip to content

Commit 1ee02da

Browse files
committed
mv DOM-related Lib function to src/lib/dom.js
1 parent 41d167f commit 1ee02da

File tree

4 files changed

+109
-94
lines changed

4 files changed

+109
-94
lines changed

src/components/dragelement/unhover.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

12-
1311
var Events = require('../../lib/events');
1412
var throttle = require('../../lib/throttle');
15-
var getGraphDiv = require('../../lib/get_graph_div');
13+
var getGraphDiv = require('../../lib/dom').getGraphDiv;
1614

1715
var hoverConstants = require('../fx/constants');
1816

1917
var unhover = module.exports = {};
2018

21-
2219
unhover.wrapped = function(gd, evt, subplot) {
2320
gd = getGraphDiv(gd);
2421

src/lib/dom.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Copyright 2012-2019, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var d3 = require('d3');
12+
var loggers = require('./loggers');
13+
14+
/**
15+
* Allow referencing a graph DOM element either directly
16+
* or by its id string
17+
*
18+
* @param {HTMLDivElement|string} gd: a graph element or its id
19+
*
20+
* @returns {HTMLDivElement} the DOM element of the graph
21+
*/
22+
function getGraphDiv(gd) {
23+
var gdElement;
24+
25+
if(typeof gd === 'string') {
26+
gdElement = document.getElementById(gd);
27+
28+
if(gdElement === null) {
29+
throw new Error('No DOM element with id \'' + gd + '\' exists on the page.');
30+
}
31+
32+
return gdElement;
33+
} else if(gd === null || gd === undefined) {
34+
throw new Error('DOM element provided is null or undefined');
35+
}
36+
37+
// otherwise assume that gd is a DOM element
38+
return gd;
39+
}
40+
41+
function isPlotDiv(el) {
42+
var el3 = d3.select(el);
43+
return el3.node() instanceof HTMLElement &&
44+
el3.size() &&
45+
el3.classed('js-plotly-plot');
46+
}
47+
48+
function removeElement(el) {
49+
var elParent = el && el.parentNode;
50+
if(elParent) elParent.removeChild(el);
51+
}
52+
53+
/**
54+
* for dynamically adding style rules
55+
* makes one stylesheet that contains all rules added
56+
* by all calls to this function
57+
*/
58+
function addStyleRule(selector, styleString) {
59+
addRelatedStyleRule('global', selector, styleString);
60+
}
61+
62+
/**
63+
* for dynamically adding style rules
64+
* to a stylesheet uniquely identified by a uid
65+
*/
66+
function addRelatedStyleRule(uid, selector, styleString) {
67+
var id = 'plotly.js-style-' + uid;
68+
var style = document.getElementById(id);
69+
if(!style) {
70+
style = document.createElement('style');
71+
style.setAttribute('id', id);
72+
// WebKit hack :(
73+
style.appendChild(document.createTextNode(''));
74+
document.head.appendChild(style);
75+
}
76+
var styleSheet = style.sheet;
77+
78+
if(styleSheet.insertRule) {
79+
styleSheet.insertRule(selector + '{' + styleString + '}', 0);
80+
} else if(styleSheet.addRule) {
81+
styleSheet.addRule(selector, styleString, 0);
82+
} else loggers.warn('addStyleRule failed');
83+
}
84+
85+
/**
86+
* to remove from the page a stylesheet identified by a given uid
87+
*/
88+
function deleteRelatedStyleRule(uid) {
89+
var id = 'plotly.js-style-' + uid;
90+
var style = document.getElementById(id);
91+
if(style) removeElement(style);
92+
}
93+
94+
module.exports = {
95+
getGraphDiv: getGraphDiv,
96+
isPlotDiv: isPlotDiv,
97+
removeElement: removeElement,
98+
addStyleRule: addStyleRule,
99+
addRelatedStyleRule: addRelatedStyleRule,
100+
deleteRelatedStyleRule: deleteRelatedStyleRule
101+
};

src/lib/get_graph_div.js

-35
This file was deleted.

src/lib/index.js

+7-55
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var d3 = require('d3');
@@ -138,7 +137,13 @@ lib.throttle = throttleModule.throttle;
138137
lib.throttleDone = throttleModule.done;
139138
lib.clearThrottle = throttleModule.clear;
140139

141-
lib.getGraphDiv = require('./get_graph_div');
140+
var domModule = require('./dom');
141+
lib.getGraphDiv = domModule.getGraphDiv;
142+
lib.isPlotDiv = domModule.isPlotDiv;
143+
lib.removeElement = domModule.removeElement;
144+
lib.addStyleRule = domModule.addStyleRule;
145+
lib.addRelatedStyleRule = domModule.addRelatedStyleRule;
146+
lib.deleteRelatedStyleRule = domModule.deleteRelatedStyleRule;
142147

143148
lib.clearResponsive = require('./clear_responsive');
144149

@@ -676,59 +681,6 @@ lib.containsAny = function(s, fragments) {
676681
return false;
677682
};
678683

679-
lib.isPlotDiv = function(el) {
680-
var el3 = d3.select(el);
681-
return el3.node() instanceof HTMLElement &&
682-
el3.size() &&
683-
el3.classed('js-plotly-plot');
684-
};
685-
686-
lib.removeElement = function(el) {
687-
var elParent = el && el.parentNode;
688-
if(elParent) elParent.removeChild(el);
689-
};
690-
691-
/**
692-
* for dynamically adding style rules
693-
* makes one stylesheet that contains all rules added
694-
* by all calls to this function
695-
*/
696-
lib.addStyleRule = function(selector, styleString) {
697-
lib.addRelatedStyleRule('global', selector, styleString);
698-
};
699-
700-
/**
701-
* for dynamically adding style rules
702-
* to a stylesheet uniquely identified by a uid
703-
*/
704-
lib.addRelatedStyleRule = function(uid, selector, styleString) {
705-
var id = 'plotly.js-style-' + uid;
706-
var style = document.getElementById(id);
707-
if(!style) {
708-
style = document.createElement('style');
709-
style.setAttribute('id', id);
710-
// WebKit hack :(
711-
style.appendChild(document.createTextNode(''));
712-
document.head.appendChild(style);
713-
}
714-
var styleSheet = style.sheet;
715-
716-
if(styleSheet.insertRule) {
717-
styleSheet.insertRule(selector + '{' + styleString + '}', 0);
718-
} else if(styleSheet.addRule) {
719-
styleSheet.addRule(selector, styleString, 0);
720-
} else lib.warn('addStyleRule failed');
721-
};
722-
723-
/**
724-
* to remove from the page a stylesheet identified by a given uid
725-
*/
726-
lib.deleteRelatedStyleRule = function(uid) {
727-
var id = 'plotly.js-style-' + uid;
728-
var style = document.getElementById(id);
729-
if(style) lib.removeElement(style);
730-
};
731-
732684
lib.isIE = function() {
733685
return typeof window.navigator.msSaveBlob !== 'undefined';
734686
};

0 commit comments

Comments
 (0)