UNPKG

igniteui-react-grids

Version:

Ignite UI React grid components.

389 lines (386 loc) 14.3 kB
import * as React from 'react'; import { IgrGridActionsBaseDirectiveCollection } from "./igr-grid-actions-base-directive-collection"; import { ActionStrip } from "./ActionStrip"; 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, interfaceToInternal, toSpinal, initializePropertiesFromCss } from "igniteui-react-core"; import { GridActionsBaseDirective } from "./GridActionsBaseDirective"; import { NotifyCollectionChangedAction } from "igniteui-react-core"; import { ActionStripResourceStrings } from "./ActionStripResourceStrings"; export class IgrActionStrip extends React.Component { createImplementation() { var _a, _b; let impl = new ActionStrip(); let nat; if (typeof document !== 'undefined') { nat = document.createElement("igc-action-strip"); } 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 "GridEditingActions": return "IgrGridEditingActions"; case "GridPinningActions": return "IgrGridPinningActions"; 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 "GridEditingActions": return "IgrGridEditingActions"; case "GridPinningActions": return "IgrGridPinningActions"; default: return undefined; } } return key; }, () => { if (this._updateContentChildren) { this._updateContentChildren(); } else if (this._updateAdapters) { this._updateAdapters(); } }); } this._actionButtonsAdapter = new CollectionAdapter(this.contentActionButtons, this.i.b, this.actualActionButtons, (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.contentActionButtons.length = 0; for (var i = 0; i < contentChildrenActual.length; i++) { if ((GridActionsBaseDirective.$type).isAssignableFrom(contentChildrenActual[i].i.$type)) { this.contentActionButtons.push(contentChildrenActual[i]); } } if (this._actionButtonsAdapter != null) { this._actionButtonsAdapter.notifyContentChanged(); } } constructor(props) { super(props); this.mounted = false; this.portaledContentChildren = []; this._actionButtons = 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; } get actualActionButtons() { if (!this._actualActionButtons) { this._actualActionButtons = []; } return this._actualActionButtons; } get contentActionButtons() { if (!this._contentActionButtons) { this._contentActionButtons = []; } return this._contentActionButtons; } /** * ActionButton as ContentChildren inside the Action Strip * @hidden * @internal */ get actionButtons() { if (this._actionButtons === null) { let coll = new IgrGridActionsBaseDirectiveCollection(); let inner = coll._innerColl; this._actionButtons = coll; inner.addListener((sender, e) => { switch (e.action) { case NotifyCollectionChangedAction.Add: this._actionButtonsAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0)); break; case NotifyCollectionChangedAction.Remove: this._actionButtonsAdapter.removeManualItemAt(e.oldStartingIndex); break; case NotifyCollectionChangedAction.Replace: this._actionButtonsAdapter.removeManualItemAt(e.oldStartingIndex); this._actionButtonsAdapter.insertManualItem(e.newStartingIndex, e.newItems.item(0)); break; case NotifyCollectionChangedAction.Reset: this._actionButtonsAdapter.clearManualItems(); break; } }); ; } return this._actionButtons; } get hidden() { return this.i.e; } set hidden(v) { this.i.e = ensureBool(v); } /** * Gets/Sets the resource strings. * @remarks * By default it uses EN resources. */ get resourceStrings() { return this.i.a.nativeElement; } set resourceStrings(v) { this.i.a = interfaceToInternal(v, () => new ActionStripResourceStrings()); } get name() { return this.i.h; } set name(v) { this.i.h = v; } findByName(name) { if (this.findEphemera) { if (name && name.indexOf("@@e:") == 0) { return this.findEphemera(name); } } if (this.actionButtons != null && this.actionButtons.findByName && this.actionButtons.findByName(name)) { return this.actionButtons.findByName(name); } if (this.resourceStrings && this.resourceStrings.name && this.resourceStrings.name == name) { return this.resourceStrings; } 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("ActionStrip"); 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._otherStyling) { this._otherStyling(container, component, parent); } this._inStyling = false; } setNativeElement(element) { this.i.setNativeElement(element); } /** * Showing the Action Strip and appending it the specified context element. * context * @example * ```typescript * this.actionStrip.show(row); * ``` * @param * context */ show(context) { this.i.k(context); } /** * Hiding the Action Strip and removing it from its current context element. * @example * ```typescript * this.actionStrip.hide(); * ``` */ hide() { this.i.i(); } }