Skip to content
Snippets Groups Projects
index.js 5.52 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * Hydrograph charting module.
     */
    
    import { select } from 'd3-selection';
    
    import { link, provide } from '../../lib/redux';
    
    import {isLoadingTS, hasAnyTimeSeries } from '../../selectors/time-series-selector';
    
    import { Actions } from '../../store';
    
    import { callIf } from '../../utils';
    
    import { cursorSlider } from './cursor';
    
    import { drawDateRangeControls } from './date-controls';
    
    import { lineSegmentsByParmCdSelector } from './drawing-data';
    
    import { drawGraphControls } from './graph-controls';
    
    import { SPARK_LINE_DIM } from './layout';
    
    import { drawTimeSeriesLegend } from './legend';
    
    import { drawLoadingIndicator } from '../loading-indicator';
    
    import { drawMethodPicker } from './method-picker';
    
    import { plotSeriesSelectTable, availableTimeSeriesSelector } from './parameters';
    
    import { timeSeriesScalesByParmCdSelector } from './scales';
    import { allTimeSeriesSelector } from './time-series';
    import { drawTimeSeriesGraph } from './time-series-graph';
    
    Bucknell, Mary S.'s avatar
    Bucknell, Mary S. committed
    const drawMessage = function(elem, message) {
    
        // Set up parent element and SVG
        elem.innerHTML = '';
        const alertBox = elem
            .append('div')
    
                .attr('class', 'usa-alert usa-alert-warning')
                .append('div')
    
                    .attr('class', 'usa-alert-body');
        alertBox
            .append('h3')
    
                .attr('class', 'usa-alert-heading')
                .html('Hydrograph Alert');
    
    Bucknell, Mary S.'s avatar
    Bucknell, Mary S. committed
    const controlDisplay = function(elem, showElem) {
    
        elem.attr('hidden', showElem ? null : true);
    
    const dataLoadingAlert = function(elem, message) {
    
        elem.select('#no-data-message').remove();
    
            elem.append('div')
                .attr('id', 'no-data-message')
                .attr('class', 'usa-alert usa-alert-info')
    
                .attr('class', 'usa-alert-body')
                .append('p')
                .attr('class', 'usa-alert-text')
                .text(message);
    
    export const attachToNode = function (store,
                                          node,
                                          {
                                              siteno,
                                              parameter,
                                              compare,
                                              period,
                                              cursorOffset,
                                              showOnlyGraph = false,
                                              showMLName = false
                                          } = {}) {
    
        if (!siteno) {
            select(node).call(drawMessage, 'No data is available.');
            return;
    
        // Initialize hydrograph with the store and show the loading indicator
    
        store.dispatch(Actions.resizeUI(window.innerWidth, node.offsetWidth));
    
            .select('.loading-indicator-container')
    
                .call(link(drawLoadingIndicator, createStructuredSelector({
    
                    showLoadingIndicator: isLoadingTS('current', 'P7D'),
                    sizeClass: () => 'fa-3x'
                })));
    
        // If specified, turn the visibility of the comparison time series on.
        if (compare) {
            store.dispatch(Actions.toggleTimeSeries('compare', true));
        }
    
    
        // If specified, initialize the cursorOffset
        if (cursorOffset !== undefined) {
            store.dispatch(Actions.setCursorOffset(cursorOffset));
        }
    
    
    Bucknell, Mary S.'s avatar
    Bucknell, Mary S. committed
            store.dispatch(Actions.retrieveCustomTimePeriodTimeSeries(siteno, parameter ? parameter : '00060', period))
    
                .catch((message) => dataLoadingAlert(select(node), message ? message : 'No data returned'));
        } else {
            store.dispatch(Actions.retrieveTimeSeries(siteno, parameter ? [parameter] : null))
                .catch(() => dataLoadingAlert((select(node), 'No current time series data available for this site')));
        }
        store.dispatch(Actions.retrieveMedianStatistics(siteno));
    
    
        // Set up rendering functions for the graph-container
    
            .call(link(controlDisplay, hasAnyTimeSeries))
    
            .call(drawTimeSeriesGraph, siteno, showMLName)
    
            .append('div')
                .classed('ts-legend-controls-container', true)
    
                .call(drawTimeSeriesLegend);
    
        // Add UI interactive elements and the provisional data alert.
    
                .call(drawDateRangeControls, siteno);
    
    
            nodeElem.select('.ts-legend-controls-container')
                .call(drawGraphControls);
    
            nodeElem.select('.select-time-series-container')
    
                .call(link(plotSeriesSelectTable, createStructuredSelector({
                    siteno: () => siteno,
                    availableTimeSeries: availableTimeSeriesSelector,
    
                    lineSegmentsByParmCd: lineSegmentsByParmCdSelector('current', 'P7D'),
    
                    timeSeriesScalesByParmCd: timeSeriesScalesByParmCdSelector('current', 'P7D', SPARK_LINE_DIM)
    
                .call(link(function(elem, allTimeSeries) {
                    elem.attr('hidden', Object.keys(allTimeSeries).length ? null : true);
                }, allTimeSeriesSelector));
        }
    
    Yan, Andrew N.'s avatar
    Yan, Andrew N. committed
            store.dispatch(Actions.resizeUI(window.innerWidth, node.offsetWidth));