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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 1× 1× 1× 1× 1× 18× 18× 10× 18× 1× 2× 1× 1× 1× 1× 1× 1× 1× 1× 1× 3× 3× 3× 3× 3× 3× 1× 1× 1× 1× 1× 1× 1× 1× 1× 5× 5× 117× 5× 1× 27× 157× 1× 16× 1× 1× 1× 1× 1× 4× 4× 1× 3× 1× 1× 1× 1× 1× 1× 5× 5× 3× 6× 2× 3× 1× 5× | /* eslint camelcase: 0 */ import d3 from 'd3'; import $ from 'jquery'; import { formatDate, UTC } from './dates'; const siFormatter = d3.format('.3s'); export function defaultNumberFormatter(n) { let si = siFormatter(n); // Removing trailing `.00` if any if (si.slice(-1) < 'A') { si = parseFloat(si).toString(); } return si; } export function d3FormatPreset(format) { // like d3.format, but with support for presets like 'smart_date' if (format === 'smart_date') { return formatDate; } if (Eformat) { return d3.format(format); } return defaultNumberFormatter; } export const d3TimeFormatPreset = function (format) { const effFormat = format || 'smart_date'; if (effFormat === 'smart_date') { return formatDate; } const f = d3.time.format(effFormat); return function (dttm) { const d = UTC(new Date(dttm)); return f(d); }; }; /* Utility function that takes a d3 svg:text selection and a max width, and splits the text's text across multiple tspan lines such that any given line does not exceed max width If text does not span multiple lines AND adjustedY is passed, will set the text to the passed val */ export function wrapSvgText(text, width, adjustedY) { const lineHeight = 1; // ems text.each(function () { const d3Text = d3.select(this); const words = d3Text.text().split(/\s+/); let word; let line = []; let lineNumber = 0; const x = d3Text.attr('x'); const y = d3Text.attr('y'); const dy = parseFloat(d3Text.attr('dy')); let tspan = d3Text.text(null).append('tspan').attr('x', x) .attr('y', y) .attr('dy', dy + 'em'); let didWrap = false; for (let i = 0; i < words.length; i++) { word = words[i]; line.push(word); tspan.text(line.join(' ')); if (tspan.node().getComputedTextLength() > width) { line.pop(); // remove word that pushes over the limit tspan.text(line.join(' ')); line = [word]; tspan = d3Text.append('tspan').attr('x', x).attr('y', y) .attr('dy', ++lineNumber * lineHeight + dy + 'em') .text(word); didWrap = true; } } if (!didWrap && typeof adjustedY !== 'undefined') { tspan.attr('y', adjustedY); } }); } /** * Sets the body and title content of a modal, and shows it. Assumes HTML for modal exists and that * it handles closing (i.e., works with bootstrap) * * @param {object} options object of the form * { * title: {string}, * body: {string}, * modalSelector: {string, default: '.misc-modal' }, * titleSelector: {string, default: '.misc-modal .modal-title' }, * bodySelector: {string, default: '.misc-modal .modal-body' }, * } */ export function showModal(options) { /* eslint no-param-reassign: 0 */ options.modalSelector = options.modalSelector || '.misc-modal'; options.titleSelector = options.titleSelector || '.misc-modal .modal-title'; options.bodySelector = options.bodySelector || '.misc-modal .modal-body'; $(options.titleSelector).html(options.title || ''); $(options.bodySelector).html(options.body || ''); $(options.modalSelector).modal('show'); } function showApiMessage(resp) { const template = '<div class="alert"> ' + '<button type="button" class="close" ' + 'data-dismiss="alert">\xD7</button> </div>'; const severity = resp.severity || 'info'; $(template).addClass('alert-' + severity) .append(resp.message) .appendTo($('#alert-container')); } export function toggleCheckbox(apiUrlPrefix, selector) { const apiUrl = apiUrlPrefix + $(selector)[0].checked; $.get(apiUrl).fail(function (xhr) { const resp = xhr.responseJSON; if (resp && resp.message) { showApiMessage(resp); } }); } /** * Fix the height of the table body of a DataTable with scrollY set */ export const fixDataTableBodyHeight = function ($tableDom, height) { const headHeight = $tableDom.find('.dataTables_scrollHead').height(); const filterHeight = $tableDom.find('.dataTables_filter').height() || 0; const pageLengthHeight = $tableDom.find('.dataTables_length').height() || 0; const paginationHeight = $tableDom.find('.dataTables_paginate').height() || 0; const controlsHeight = (pageLengthHeight > filterHeight) ? pageLengthHeight : filterHeight; $tableDom.find('.dataTables_scrollBody').css('max-height', height - headHeight - controlsHeight - paginationHeight); }; export function d3format(format, number) { const formatters = {}; // Formats a number and memoizes formatters to be reused format = format || '.3s'; if (E!(format in formatters)) { formatters[format] = d3.format(format); } try { return formatters[format](number); } catch (e) { return 'ERR'; } } // Slice objects interact with their context through objects that implement // this controllerInterface (dashboard, explore, standalone) export const controllerInterface = { type: null, done: () => {}, error: () => {}, always: () => {}, addFiler: () => {}, setFilter: () => {}, getFilters: () => false, clearFilter: () => {}, removeFilter: () => {}, filters: {}, }; export function formatSelectOptionsForRange(start, end) { // outputs array of arrays // formatSelectOptionsForRange(1, 5) // returns [[1,1], [2,2], [3,3], [4,4], [5,5]] const options = []; for (let i = start; i <= end; i++) { options.push([i, i.toString()]); } return options; } export function formatSelectOptions(options) { return options.map(opt => [opt, opt.toString()], ); } export function slugify(string) { // slugify('My Neat Label! '); returns 'my-neat-label' return string .toString() .toLowerCase() .trim() .replace(/[\s\W-]+/g, '-') // replace spaces, non-word chars, w/ a single dash (-) .replace(/-$/, ''); // remove last floating dash } export function getAjaxErrorMsg(error) { const respJSON = error.responseJSON; return (respJSON && respJSON.message) ? respJSON.message : error.responseText; } export function customizeToolTip(chart, xAxisFormatter, yAxisFormatters) { chart.useInteractiveGuideline(true); chart.interactiveLayer.tooltip.contentGenerator(function (d) { const tooltipTitle = xAxisFormatter(d.value); let tooltip = ''; tooltip += "<table><thead><tr><td colspan='3'>" + `<strong class='x-value'>${tooltipTitle}</strong>` + '</td></tr></thead><tbody>'; d.series.forEach((series, i) => { const yAxisFormatter = yAxisFormatters[i]; const value = yAxisFormatter(series.value); tooltip += "<tr><td class='legend-color-guide'>" + `<div style="background-color: ${series.color};"></div></td>` + `<td class='key'>${series.key}</td>` + `<td class='value'>${value}</td></tr>`; }); tooltip += '</tbody></table>'; return tooltip; }); } export function initJQueryAjax() { // Works in conjunction with a Flask-WTF token as described here: // http://flask-wtf.readthedocs.io/en/stable/csrf.html#javascript-requests const token = $('input#csrf_token').val(); if (token) { $.ajaxSetup({ beforeSend(xhr, settings) { if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) { xhr.setRequestHeader('X-CSRFToken', token); } }, }); } } export function tryNumify(s) { // Attempts casting to Number, returns string when failing const n = Number(s); if (isNaN(n)) { return s; } return n; } export function getParam(name) { /* eslint no-useless-escape: 0 */ const formattedName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); const regex = new RegExp('[\\?&]' + formattedName + '=([^&#]*)'); const results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); } export function mainMetric(savedMetrics) { // Using 'count' as default metric if it exists, otherwise using whatever one shows up first let metric; if (savedMetrics && savedMetrics.length > 0) { savedMetrics.forEach((m) => { if (m.metric_name === 'count') { metric = 'count'; } }); if (!metric) { metric = savedMetrics[0].metric_name; } } return metric; } |