Debian Codegraph @

bqplot/js/src/utils.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import { DOMWidgetView } from '@jupyter-widgets/base';
17 import * as d3 from 'd3';
18 // var d3 =Object.assign({}, require("d3-array"), require("d3-scale"));
19 import isTypedArray from 'is-typedarray';
20 import * as _ from 'underscore';
21
22 // the following is a regex to match all valid time formats that can be
23 // generated with d3 as of 2nd March 2015. If new formats are added to d3
24 // those new formats need to be added to the regex
25 const time_format_regex = new RegExp(
26 '^(((((\\*)|(/*)|(-*))(\\s*)%([aAbBdeHIjmMLpSUwWyYZ]{1}))+)|((\\s*)%([cxX]{1})))$'
27 );
28
29 export function deepCopy(obj) {
30 // This makes a deep copy of JSON-parsable objects
31 // (no cycling or recombining)
32 // Backbone model attributes must be JSON parsable. Hence there is
33 // no need for a fancier logic, and it is surprisingly efficient.
34 if (isTypedArray(obj)) {
35 return Object.assign(obj.slice(), obj);
36 } else if (_.isArray(obj)) {
37 return obj.map((x) => deepCopy(x));
38 } else if (_.isNumber(obj) || _.isString(obj)) {
39 return obj;
40 } else if (_.isDate(obj)) {
41 return new Date(obj.getTime());
42 } else {
43 const newobj = {};
44 for (const key in obj) {
45 if (obj.hasOwnProperty(key)) {
46 newobj[key] = deepCopy(obj[key]);
47 }
48 }
49 return newobj;
50 }
51 }
52
53 export function convert_dates(value) {
54 // check if the array of value contains Date objects, if so
55 // dates are serialized using the timestamp only, some parts of the code
56 // use the Data objects, detect that and convert.
57 // also, we set the .type property of TypedArray, to keep track of it being a
58 // date array
59 if (isTypedArray(value)) {
60 return value;
61 }
62 const convert_to_date = function (x) {
63 const ar: any = new Float64Array(x.map(Number));
64 ar.type = 'date';
65 return ar;
66 };
67 if (value[0] instanceof Array && value[0][0] instanceof Date) {
68 value = value.map(convert_to_date);
69 } else if (value[0] instanceof Date) {
70 value = convert_to_date(value);
71 }
72 return value;
73 }
74
75 export function getCustomRange(array) {
76 const first = array[0];
77 const end = array[array.length - 1];
78 let pivot;
79 if (array[0] > array[1]) {
80 pivot = d3.min(array);
81 } else {
82 pivot = d3.max(array);
83 }
84 return [
85 d3.scaleLinear().range([first, pivot]),
86 d3.scaleLinear().range([pivot, end]),
87 ];
88 }
89
90 export function is_valid_time_format(format) {
91 return time_format_regex.test(format);
92 }
93
94 export function is_array(x) {
95 return x instanceof Array || isTypedArray(x);
96 }
97
98 export function getDate(elem: undefined | null | number | string): Date | null {
99 if (elem === undefined || elem === null) {
100 return null;
101 }
102
103 return new Date(elem);
104 }
105
106 export function applyStyles(d3el, styles) {
107 Object.keys(styles).forEach((key) => d3el.style(key, styles[key]));
108 return d3el;
109 }
110
111 export function applyAttrs(d3el, styles) {
112 Object.keys(styles).forEach((key) => d3el.attr(key, styles[key]));
113 return d3el;
114 }
115
116 export function d3GetEvent() {
117 // In JupyterLab we can have require('d3-selection').event
118 // be a different module as the one that is used in d3.on
119 // leading to a null event, for now we can use window.event as fallback.
120 return require('d3-selection').event || window.event;
121 }
122
123 // Phosphor shim for ipywidgets 7 support
124 export function getLuminoWidget(ipywidget: DOMWidgetView) {
125 return ipywidget.pWidget ? ipywidget.pWidget : ipywidget.luminoWidget;
126 }
127
128 export function getEffectiveBackgroundColor(element) {
129 if (!element) {
130 // If no element provided or we have traversed beyond the body or html,
131 // we default to white as the background color.
132 return 'rgb(255, 255, 255)';
133 }
134
135 const style = window.getComputedStyle(element);
136 const bgColor = style.backgroundColor;
137 const hasBackgroundImage = style.backgroundImage !== 'none';
138 const isTransparent =
139 bgColor === 'transparent' || bgColor === 'rgba(0, 0, 0, 0)';
140
141 if (!isTransparent || hasBackgroundImage) {
142 return bgColor;
143 }
144
145 return getEffectiveBackgroundColor(element.parentElement);
146 }