@darwino/darwino-react
Version:
A set of Javascript classes and utilities
103 lines (82 loc) • 2.7 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
/*
* (c) Copyright Darwino Inc. 2014-2017.
*/
import React, { Component } from "react";
import PropTypes from 'prop-types';
import { DEV_OPTIONS, JstoreCursor, EmptyDataFetcher, PagingDataFetcher, ArrayDataFetcher, jsonEquals } from '@darwino/darwino';
/*
* Expected properties:
* - databaseId (string)
* - storeId (string)
* - params (object)
* - columns (array of objects (title/key))
*
* Note: 'store' is a reserved prop with redux so the parameter is 'storeId', and 'databaseId' for consistency.
*/
class BaseCursorComponent extends Component {
// Cursor property
// CLEANUP: Is that used?
constructor(props) {
super(props);
_defineProperty(this, "ftSearch", null);
this.state = {};
} // PHIL: TODO:
// Should the getForm() be at this level or in BaseCursorList
getForm() {
return this.context.documentForm;
}
componentDidMount() {
this.reinitData();
if (this.context.documentForm) {
this.context.documentForm._registerComponent(this);
}
}
componentWillUnmount() {
if (this.context.documentForm) {
this.context.documentForm._unregisterComponent(this);
}
}
componentDidUpdate(prevProps) {
// Recreate the cursor if the properties changed
if (this.shouldReinitData(prevProps)) {
this.reinitData();
}
}
reinitData() {
throw new Error("Not implemented");
}
shouldReinitData(prevProps) {
// Check if a property change would force recreate the cursor
// This can be overriden by a specific implementation if the cursor uses other parameters
var p = this.props;
if (p.databaseId !== prevProps.databaseId || p.storeId !== prevProps.storeId || !jsonEquals(p.params, prevProps.params) || p.sortColumn !== prevProps.sortColumn || p.sortDescending !== prevProps.sortDescending || p.ftSearch !== prevProps.ftSearch) {
return true;
}
return false;
}
createJstoreCursor() {
var {
databaseId,
storeId,
params
} = this.props;
var jsc = new JstoreCursor().database(databaseId).store(storeId).queryParams(params); // if(this.props.groupBy) {
// jsc.orderby(this.props.groupBy,this.descending)
// jsc.categoryCount(this.props.groupBy.length)
// } else
if (!this.props.inMemorySort && this.sortColumn) {
jsc.orderby(this.sortColumn, this.sortDescending);
}
if (this.ftSearch) {
jsc.ftsearch(this.ftSearch);
}
return jsc;
}
setFTSearch(search) {
this.ftSearch = search;
this.reinitData();
}
}
export default BaseCursorComponent;
//# sourceMappingURL=BaseCursorComponent.js.map