UNPKG

igniteui-react-grids

Version:

Ignite UI React grid components.

474 lines (473 loc) 18.2 kB
import { __assign, __extends, __values } from "tslib"; 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 */ var IgrGridToolbar = /** @class */ /*@__PURE__*/ (function (_super) { __extends(IgrGridToolbar, _super); function IgrGridToolbar(props) { var _this = _super.call(this, props) || this; _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", function () { 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(); } return _this; } IgrGridToolbar.prototype.createImplementation = function () { var _a, _b; var impl = new GridToolbar(); var 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; }; Object.defineProperty(IgrGridToolbar.prototype, "nativeElement", { get: function () { return this._implementation.nativeElement; }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "i", { /** * @hidden */ get: function () { return this._implementation; } /** * @hidden */, enumerable: false, configurable: true }); IgrGridToolbar._createFromInternal = function (internal) { if (!internal) { return null; } if (!internal.$type) { return null; } var name = internal.$type.name; var externalName = "Igr" + name; if (!TypeRegistrar.isRegistered(externalName)) { return null; } return TypeRegistrar.create(externalName); }; IgrGridToolbar.prototype.componentWillUnmount = function () { var e_1, _a; if (this.portaledContentChildren.length > 0) { try { for (var _b = __values(this.portaledContentChildren), _c = _b.next(); !_c.done; _c = _b.next()) { var item = _c.value; var 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); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } } }; IgrGridToolbar.prototype.onImplementationCreated = function () { var _this = this; this._portalManager.getPortal(this._renderer.getWrapper(this._implementation.nativeElement), "ChildContent", function (p) { for (var i = 0; i < p.portalContainer.getChildCount(); i++) { _this.portaledContentChildren.push(p.portalContainer.getChildAt(i)); } }, true); }; IgrGridToolbar.prototype._initializeAdapters = function () { var _this = this; if (!this._contentChildrenManager) { var resolveNested_1 = (function (props, propName, nestedPropName) { var _a; var prop = props[propName]; if (Array.isArray(prop)) { return prop.map(function (x) { var _a; return (_a = x[nestedPropName]) !== null && _a !== void 0 ? _a : resolveNested_1(x.props, propName, nestedPropName); }).join("_"); } else if (prop) { return (_a = prop.props[nestedPropName]) !== null && _a !== void 0 ? _a : resolveNested_1(prop.props, propName, nestedPropName); } else { return props[nestedPropName]; } }); this._contentChildrenManager = new ContentChildrenManager(function (ch) { var key = ch.key || ch.props.name; if (!key) { var type = (ch.type); var instance = new type; var name_1 = instance.i.$type.name; switch (name_1) { case "GridToolbarActions": return "IgrGridToolbarActions"; case "GridToolbarTitle": return "IgrGridToolbarTitle"; default: return undefined; } } return key; }, function (ch) { var key = ch.key || ch.props.name; if (!key) { var type = (ch.type); var instance = new type; var name_2 = instance.i.$type.name; switch (name_2) { case "GridToolbarActions": return "IgrGridToolbarActions"; case "GridToolbarTitle": return "IgrGridToolbarTitle"; default: return undefined; } } return key; }, function () { if (_this._updateContentChildren) { _this._updateContentChildren(); } else if (_this._updateAdapters) { _this._updateAdapters(); } }); } this._toolsAdapter = new CollectionAdapter(this.contentTools, this.i.c, this.actualTools, function (c) { return c.i; }, function (i) { if (_this._initializeElement) { _this._initializeElement(i); } }, function (i) { if (_this._destroyElement) { _this._destroyElement(i); } }); if (_super.prototype["_initializeAdapters"]) { _super.prototype["_initializeAdapters"].call(this); } }; IgrGridToolbar.prototype._updateAdapters = function () { if (_super.prototype["_updateAdapters"]) { _super.prototype["_updateAdapters"].call(this); } var 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(); } }; IgrGridToolbar.prototype.componentDidMount = function () { var e_2, _a; this.mounted = true; try { for (var _b = __values(Object.keys(this.props)), _c = _b.next(); !_c.done; _c = _b.next()) { var p = _c.value; if (isValidProp(this, p)) { this[p] = this.props[p]; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } }; IgrGridToolbar.prototype.shouldComponentUpdate = function (nextProps, nextState) { var e_3, _a; var mod = getModifiedProps(this.props, nextProps); try { for (var _b = __values(Object.keys(mod)), _c = _b.next(); !_c.done; _c = _b.next()) { var p = _c.value; if (isValidProp(this, p)) { this[p] = mod[p]; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return true; }; IgrGridToolbar.prototype.render = function () { var _this = this; var nativePropsName = Object.keys(this.props).filter(function (prop) { return !isValidProp(_this, prop) && prop !== "originalRef" && prop !== "className"; }); var nativeProps = {}; nativePropsName.forEach(function (propName) { nativeProps[propName] = _this.props[propName]; }); var visibleChildren = []; React.Children.forEach(this.props.children, function (ch) { if (ch) { visibleChildren.push(ch); } }); var children = this._contentChildrenManager.getChildren(visibleChildren); this._portalManager.onRender(children); var style = {}; style.display = 'contents'; if (this.props.style) { style = this.props.style; } var div = React.createElement("div", __assign(__assign({}, nativeProps), { ref: this._getMainRef, className: this.props.className, style: style, children: children })); return div; }; IgrGridToolbar.prototype._getMainRef = function (ref) { this._elRef = ref; }; Object.defineProperty(IgrGridToolbar.prototype, "showProgress", { /** * 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: function () { return this.i.f; }, set: function (v) { this.i.f = ensureBool(v); }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "grid", { /** * Gets/sets the grid component for the toolbar component. * @deprecated No longer required to be set for the Hierarchical Grid child grid template */ get: function () { var r = this.i.a; if (r == null) { return null; } return r.externalObject; }, set: function (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; }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "name", { get: function () { return this.i.i; }, set: function (v) { this.i.i = v; }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "actualTools", { get: function () { if (!this._actualTools) { this._actualTools = []; } return this._actualTools; }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "contentTools", { get: function () { if (!this._contentTools) { this._contentTools = []; } return this._contentTools; }, enumerable: false, configurable: true }); Object.defineProperty(IgrGridToolbar.prototype, "tools", { get: function () { var _this = this; if (this._tools === null) { var coll = new IgrGridToolbarContentCollection(); var inner = coll._innerColl; this._tools = coll; inner.addListener(function (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; }, enumerable: false, configurable: true }); IgrGridToolbar.prototype.findByName = function (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; }; Object.defineProperty(IgrGridToolbar.prototype, "hasUserValues", { get: function () { return this._hasUserValues; }, enumerable: false, configurable: true }); IgrGridToolbar.prototype.__m = function (propertyName) { if (!this._inStyling) { this._hasUserValues.add(propertyName); } }; IgrGridToolbar.prototype._styling = function (container, component, parent) { if (this._inStyling) { return; } this._inStyling = true; this._stylingContainer = container; this._stylingParent = component; var genericPrefix = ""; var typeName = this.i.$type.name; if (typeName.indexOf("Xam") === 0) { typeName = typeName.substring(3); } genericPrefix = toSpinal("GridToolbar"); var additionalPrefixes = []; var prefix = toSpinal(typeName); additionalPrefixes.push(prefix + "-"); var 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); } var basePrefix = toSpinal(typeName); additionalPrefixes.push(basePrefix + "-"); b = b.baseType; } if (parent) { var parentTypeName = parent.i.$type.name; if (parentTypeName.indexOf("Xam") === 0) { parentTypeName = parentTypeName.substring(3); } var 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; }; IgrGridToolbar.prototype.setNativeElement = function (element) { this.i.setNativeElement(element); }; return IgrGridToolbar; }(React.Component)); export { IgrGridToolbar };