This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBaseComponent.js
120 lines (96 loc) · 3.16 KB
/
BaseComponent.js
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
import React, { Component } from 'react';
import { browserHistory } from 'react-router';
import { findDOMNode } from 'react-dom';
import EventDispatcher from '../dispatcher/EventDispatcher';
import Dataset from '../models/Dataset';
import {omit, isEqual, isEmpty, isFunction, isPlainObject, isString, isArray, debounce} from 'lodash';
import StateHandler from '../utils/StateHandler';
import Registry from '../utils/Registry';
import { makeKey } from '../utils/utils';
import { qFromParams, getOwnQueryParams, getFID, objToQueryString } from '../utils/paramRouting';
const CARD_VARS = ['header', 'footer', 'iconClass', 'cardStyle', 'cardClasses', 'subheader', 'topmatter', 'subheader2', 'topmatter2', 'footerHeader', 'footerSubheader', 'bottommatter', 'footerSubheader2', 'bottommatter2'];
export default class BaseComponent extends Component {
constructor(props) {
super(props);
this.makeKey = makeKey;
this.state = {
data: [],
dataset: null,
queryObj: Object.assign({from: 0}, this.props.queryObj), // dataset query
isFetching: false,
};
}
/**
* LIFECYCLE
**/
componentWillMount() {
// Register to all the actions
EventDispatcher.register(this.onAction.bind(this));
}
componentDidMount(){
// resize magic
let componentWidth = findDOMNode(this).getBoundingClientRect().width;
let newState = this.executeStateHandlers();
newState.componentWidth = componentWidth;
newState.cardVariables = this.getCardVariables();
this.setState(newState);
this.addResizeListener();
this.onResize();
}
componentDidUpdate(prevProps, prevState) {
if (!isEqual(this.props.data, prevProps.data)) {
let newState = this.executeStateHandlers();
newState.cardVariables = this.getCardVariables();
this.setState(newState);
this.onResize();
}
}
componentWillUnmount() {
window.removeEventListener('resize', this._resizeHandler);
}
emit(payload) {
EventDispatcher.dispatch(payload);
}
getGlobalData() {
return this.props.globalData || [];
}
/**
* If stateHandlers are defined on the component call them and return the result
*
* @returns {obj} object with calculated state paramaters
*/
executeStateHandlers() {
let newState = {};
if (this.props.stateHandlers && this.props.stateHandlers.length > 0) {
let handledState = StateHandler.handle(this.props.stateHandlers, this.props.data, this.state.dashboardData);
newState = Object.assign(newState, handledState);
}
return newState;
}
// if we have card variables set on the state, return them
// otherwise use props or undefined
getCardVariables() {
let cardVars = {};
CARD_VARS.forEach(v => {
cardVars[v] = this.state[v] || this.props[v];
});
return cardVars;
}
addResizeListener() {
this._resizeHandler = (e) => {
let componentWidth = findDOMNode(this).getBoundingClientRect().width;
this.setState({ componentWidth : componentWidth});
this.onResize(e);
}
window.addEventListener('resize', this._resizeHandler);
}
/**
* Abstract
*/
onResize() {
/* IMPLEMENT */
}
onAction() {
/* IMPLEMENT */
}
}