igniteui-react-grids
Version:
Ignite UI React grid components.
378 lines (377 loc) • 14.1 kB
JavaScript
import * as React from 'react';
import { IgrGridToolbarContentCollection } from "./igr-grid-toolbar-content-collection";
import { GridToolbar } from "./GridToolbar";
import { TypeRegistrar } from "igniteui-react-core";
import { ReactRenderer, PortalManager } from "igniteui-react-core";
import { ContentChildrenManager } from "igniteui-react-core";
import { CollectionAdapter, NamePatcher, getModifiedProps, isValidProp, ensureBool, toSpinal, initializePropertiesFromCss } from "igniteui-react-core";
import { GridToolbarContent } from "./GridToolbarContent";
import { NotifyCollectionChangedAction } from "igniteui-react-core";
/**
* Provides a context-aware container component for UI operations for the grid components.
* @igxModule IgxGridToolbarModule
* @igxParent IgxGridComponent, IgxTreeGridComponent, IgxHierarchicalGridComponent, IgxPivotGridComponent
*/
export class IgrGridToolbar extends React.Component {
createImplementation() {
var _a, _b;
let impl = new GridToolbar();
let nat;
if (typeof document !== 'undefined') {
nat = document.createElement("igc-grid-toolbar");
}
else {
nat = {
style: {}
};
}
if ((_a = this.props) === null || _a === void 0 ? void 0 : _a.className) {
nat.className = this.props.className;
}
if ((_b = this.props) === null || _b === void 0 ? void 0 : _b.id) {
nat.id = this.props.id;
}
impl.setNativeElement(nat);
return impl;
}
get nativeElement() {
return this._implementation.nativeElement;
}
/**
* @hidden
*/
get i() {
return this._implementation;
} /**
* @hidden
*/
static _createFromInternal(internal) {
if (!internal) {
return null;
}
if (!internal.$type) {
return null;
}
let name = internal.$type.name;
let externalName = "Igr" + name;
if (!TypeRegistrar.isRegistered(externalName)) {
return null;
}
return TypeRegistrar.create(externalName);
}
componentWillUnmount() {
if (this.portaledContentChildren.length > 0) {
for (const item of this.portaledContentChildren) {
const nativeElem = item.ele;
if (item.ele.parentElement !== this.nativeElement) {
// put it back where it should be for react's portals to find.
this.nativeElement.appendChild(nativeElem);
}
}
}
}
onImplementationCreated() {
this._portalManager.getPortal(this._renderer.getWrapper(this._implementation.nativeElement), "ChildContent", (p) => {
for (let i = 0; i < p.portalContainer.getChildCount(); i++) {
this.portaledContentChildren.push(p.portalContainer.getChildAt(i));
}
}, true);
}
_initializeAdapters() {
if (!this._contentChildrenManager) {
const resolveNested = ((props, propName, nestedPropName) => {
var _a;
const prop = props[propName];
if (Array.isArray(prop)) {
return prop.map(x => {
var _a;
return (_a = x[nestedPropName]) !== null && _a !== void 0 ? _a : resolveNested(x.props, propName, nestedPropName);
}).join("_");
}
else if (prop) {
return (_a = prop.props[nestedPropName]) !== null && _a !== void 0 ? _a : resolveNested(prop.props, propName, nestedPropName);
}
else {
return props[nestedPropName];
}
});
this._contentChildrenManager = new ContentChildrenManager((ch) => {
const key = ch.key || ch.props.name;
if (!key) {
const type = (ch.type);
const instance = new type;
const name = instance.i.$type.name;
switch (name) {
case "GridToolbarActions": return "IgrGridToolbarActions";
case "GridToolbarTitle": return "IgrGridToolbarTitle";
default: return undefined;
}
}
return key;
}, (ch) => {
const key = ch.key || ch.props.name;
if (!key) {
const type = (ch.type);
const instance = new type;
const name = instance.i.$type.name;
switch (name) {
case "GridToolbarActions": return "IgrGridToolbarActions";
case "GridToolbarTitle": return "IgrGridToolbarTitle";
default: return undefined;
}
}
return key;
}, () => {
if (this._updateContentChildren) {
this._updateContentChildren();
}
else if (this._updateAdapters) {
this._updateAdapters();
}
});
}
this._toolsAdapter = new CollectionAdapter(this.contentTools, this.i.c, this.actualTools, (c) => c.i, (i) => {
if (this._initializeElement) {
this._initializeElement(i);
}
}, (i) => {
if (this._destroyElement) {
this._destroyElement(i);
}
});
if (super["_initializeAdapters"]) {
super["_initializeAdapters"]();
}
}
_updateAdapters() {
if (super["_updateAdapters"]) {
super["_updateAdapters"]();
}
let contentChildrenActual = this._contentChildrenManager.contentChildrenActual;
this.contentTools.length = 0;
for (var i = 0; i < contentChildrenActual.length; i++) {
if ((GridToolbarContent.$type).isAssignableFrom(contentChildrenActual[i].i.$type)) {
this.contentTools.push(contentChildrenActual[i]);
}
}
if (this._toolsAdapter != null) {
this._toolsAdapter.notifyContentChanged();
}
}
constructor(props) {
super(props);
this.mounted = false;
this.portaledContentChildren = [];
this._tools = null;
this.__p = null;
this._hasUserValues = new Set();
this._stylingContainer = null;
this._stylingParent = null;
this._inStyling = false;
if (this._styling) {
NamePatcher.ensureStylablePatched(Object.getPrototypeOf(this));
}
this._getMainRef = this._getMainRef.bind(this);
this._implementation = this.createImplementation();
this._portalManager = new PortalManager("templates", () => {
if (this.mounted) {
this.setState({});
}
});
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
this._renderer = new ReactRenderer(this._implementation.nativeElement, document, false, null, this._portalManager);
}
this._implementation.externalObject = this;
this.onImplementationCreated();
if (this._initializeAdapters) {
this._initializeAdapters();
}
}
componentDidMount() {
this.mounted = true;
for (const p of Object.keys(this.props)) {
if (isValidProp(this, p)) {
this[p] = this.props[p];
}
}
}
shouldComponentUpdate(nextProps, nextState) {
const mod = getModifiedProps(this.props, nextProps);
for (const p of Object.keys(mod)) {
if (isValidProp(this, p)) {
this[p] = mod[p];
}
}
return true;
}
render() {
const nativePropsName = Object.keys(this.props).filter(prop => !isValidProp(this, prop) && prop !== "originalRef" && prop !== "className");
const nativeProps = {};
nativePropsName.forEach(propName => {
nativeProps[propName] = this.props[propName];
});
const visibleChildren = [];
React.Children.forEach(this.props.children, (ch) => {
if (ch) {
visibleChildren.push(ch);
}
});
let children = this._contentChildrenManager.getChildren(visibleChildren);
this._portalManager.onRender(children);
let style = {};
style.display = 'contents';
if (this.props.style) {
style = this.props.style;
}
let div = React.createElement("div", Object.assign(Object.assign({}, nativeProps), { ref: this._getMainRef, className: this.props.className, style: style, children: children }));
return div;
}
_getMainRef(ref) {
this._elRef = ref;
}
/**
* When enabled, shows the indeterminate progress bar.
* @remarks
* By default this will be toggled, when the default exporter component is present
* and an exporting is in progress.
*/
get showProgress() {
return this.i.f;
}
set showProgress(v) {
this.i.f = ensureBool(v);
}
/**
* Gets/sets the grid component for the toolbar component.
* @deprecated No longer required to be set for the Hierarchical Grid child grid template
*/
get grid() {
const r = this.i.a;
if (r == null) {
return null;
}
return r.externalObject;
}
set grid(v) {
if (v != null && this._stylingContainer && v._styling)
v._styling(this._stylingContainer, this, this);
v == null ? this.i.a = null : this.i.a = v.i;
}
get name() {
return this.i.i;
}
set name(v) {
this.i.i = v;
}
get actualTools() {
if (!this._actualTools) {
this._actualTools = [];
}
return this._actualTools;
}
get contentTools() {
if (!this._contentTools) {
this._contentTools = [];
}
return this._contentTools;
}
get tools() {
if (this._tools === null) {
let coll = new IgrGridToolbarContentCollection();
let inner = coll._innerColl;
this._tools = coll;
inner.addListener((sender, e) => {
switch (e.action) {
case NotifyCollectionChangedAction.Add:
this._toolsAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0));
break;
case NotifyCollectionChangedAction.Remove:
this._toolsAdapter.removeManualItemAt(e.oldStartingIndex);
break;
case NotifyCollectionChangedAction.Replace:
this._toolsAdapter.removeManualItemAt(e.oldStartingIndex);
this._toolsAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0));
break;
case NotifyCollectionChangedAction.Reset:
this._toolsAdapter.clearManualItems();
break;
}
});
;
}
return this._tools;
}
findByName(name) {
if (this.findEphemera) {
if (name && name.indexOf("@@e:") == 0) {
return this.findEphemera(name);
}
}
if (this.grid && this.grid.name && this.grid.name == name) {
return this.grid;
}
if (this.tools != null && this.tools.findByName && this.tools.findByName(name)) {
return this.tools.findByName(name);
}
return null;
}
get hasUserValues() {
return this._hasUserValues;
}
__m(propertyName) {
if (!this._inStyling) {
this._hasUserValues.add(propertyName);
}
}
_styling(container, component, parent) {
if (this._inStyling) {
return;
}
this._inStyling = true;
this._stylingContainer = container;
this._stylingParent = component;
let genericPrefix = "";
let typeName = this.i.$type.name;
if (typeName.indexOf("Xam") === 0) {
typeName = typeName.substring(3);
}
genericPrefix = toSpinal("GridToolbar");
let additionalPrefixes = [];
let prefix = toSpinal(typeName);
additionalPrefixes.push(prefix + "-");
let b = this.i.$type.baseType;
while (b && b.name != "Object" &&
b.name != "Base" &&
b.name != "Control" &&
b.Name != "DependencyObject" &&
b.Name != "FrameworkElement") {
typeName = b.name;
if (typeName.indexOf("Xam") === 0) {
typeName = typeName.substring(3);
}
let basePrefix = toSpinal(typeName);
additionalPrefixes.push(basePrefix + "-");
b = b.baseType;
}
if (parent) {
let parentTypeName = parent.i.$type.name;
if (parentTypeName.indexOf("Xam") === 0) {
parentTypeName = parentTypeName.substring(3);
}
let parentPrefix = toSpinal(parentTypeName);
additionalPrefixes.push(parentPrefix + "-" + genericPrefix + "-");
additionalPrefixes.push(parentPrefix + "-" + prefix + "-");
}
initializePropertiesFromCss(container, this, genericPrefix + "-", this.hasUserValues, false, additionalPrefixes);
if (this.grid && this.grid._styling) {
this.grid._styling(container, component, this);
}
if (this._otherStyling) {
this._otherStyling(container, component, parent);
}
this._inStyling = false;
}
setNativeElement(element) {
this.i.setNativeElement(element);
}
}