igniteui-react-grids
Version:
Ignite UI React grid components.
485 lines (482 loc) • 18.4 kB
JavaScript
import { __assign, __extends, __values } from "tslib";
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";
var IgrActionStrip = /** @class */ /*@__PURE__*/ (function (_super) {
__extends(IgrActionStrip, _super);
function IgrActionStrip(props) {
var _this = _super.call(this, props) || this;
_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", 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;
}
IgrActionStrip.prototype.createImplementation = function () {
var _a, _b;
var impl = new ActionStrip();
var 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;
};
Object.defineProperty(IgrActionStrip.prototype, "nativeElement", {
get: function () {
return this._implementation.nativeElement;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "i", {
/**
* @hidden
*/
get: function () {
return this._implementation;
} /**
* @hidden
*/,
enumerable: false,
configurable: true
});
IgrActionStrip._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);
};
IgrActionStrip.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;
}
}
}
};
IgrActionStrip.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);
};
IgrActionStrip.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 "GridEditingActions": return "IgrGridEditingActions";
case "GridPinningActions": return "IgrGridPinningActions";
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 "GridEditingActions": return "IgrGridEditingActions";
case "GridPinningActions": return "IgrGridPinningActions";
default: return undefined;
}
}
return key;
}, function () {
if (_this._updateContentChildren) {
_this._updateContentChildren();
}
else if (_this._updateAdapters) {
_this._updateAdapters();
}
});
}
this._actionButtonsAdapter = new CollectionAdapter(this.contentActionButtons, this.i.b, this.actualActionButtons, 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);
}
};
IgrActionStrip.prototype._updateAdapters = function () {
if (_super.prototype["_updateAdapters"]) {
_super.prototype["_updateAdapters"].call(this);
}
var 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();
}
};
IgrActionStrip.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;
}
}
};
IgrActionStrip.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;
};
IgrActionStrip.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;
};
IgrActionStrip.prototype._getMainRef = function (ref) {
this._elRef = ref;
};
Object.defineProperty(IgrActionStrip.prototype, "actualActionButtons", {
get: function () {
if (!this._actualActionButtons) {
this._actualActionButtons = [];
}
return this._actualActionButtons;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "contentActionButtons", {
get: function () {
if (!this._contentActionButtons) {
this._contentActionButtons = [];
}
return this._contentActionButtons;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "actionButtons", {
/**
* ActionButton as ContentChildren inside the Action Strip
* @hidden
* @internal
*/
get: function () {
var _this = this;
if (this._actionButtons === null) {
var coll = new IgrGridActionsBaseDirectiveCollection();
var inner = coll._innerColl;
this._actionButtons = coll;
inner.addListener(function (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;
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "hidden", {
get: function () {
return this.i.e;
},
set: function (v) {
this.i.e = ensureBool(v);
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "resourceStrings", {
/**
* Gets/Sets the resource strings.
* @remarks
* By default it uses EN resources.
*/
get: function () {
return this.i.a.nativeElement;
},
set: function (v) {
this.i.a = interfaceToInternal(v, function () { return new ActionStripResourceStrings(); });
},
enumerable: false,
configurable: true
});
Object.defineProperty(IgrActionStrip.prototype, "name", {
get: function () {
return this.i.h;
},
set: function (v) {
this.i.h = v;
},
enumerable: false,
configurable: true
});
IgrActionStrip.prototype.findByName = function (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;
};
Object.defineProperty(IgrActionStrip.prototype, "hasUserValues", {
get: function () {
return this._hasUserValues;
},
enumerable: false,
configurable: true
});
IgrActionStrip.prototype.__m = function (propertyName) {
if (!this._inStyling) {
this._hasUserValues.add(propertyName);
}
};
IgrActionStrip.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("ActionStrip");
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._otherStyling) {
this._otherStyling(container, component, parent);
}
this._inStyling = false;
};
IgrActionStrip.prototype.setNativeElement = function (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
*/
IgrActionStrip.prototype.show = function (context) {
this.i.k(context);
};
/**
* Hiding the Action Strip and removing it from its current context element.
* @example
* ```typescript
* this.actionStrip.hide();
* ```
*/
IgrActionStrip.prototype.hide = function () {
this.i.i();
};
return IgrActionStrip;
}(React.Component));
export { IgrActionStrip };