@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
54 lines (52 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.Provider = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var GlobalId = 0;
var ID_KEY = '$$ID$$';
var Provider = exports.Provider = /*#__PURE__*/function () {
function Provider(prefix) {
var _this = this;
_classCallCheck(this, Provider);
this.generate = function () {
var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ID_KEY;
var idPrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.prefix;
var onUnmount = arguments.length > 2 ? arguments[2] : undefined;
// if an id has been generated for the key, return it
if (key in _this.ids) {
return _this.ids[key].id;
}
// otherwise generate a new id (with an optional prefix), cache it, and return it
var id = "".concat(idPrefix).concat(++GlobalId);
_this.ids[typeof key === 'undefined' ? "generated-".concat(id) : key] = {
id: id,
onUnmount: onUnmount
};
return id;
};
this.prefix = prefix;
this.ids = {};
}
_createClass(Provider, [{
key: "unload",
value: function unload() {
// Call the onUnmount handler for each generated id (note: not the key)
for (var key in this.ids) {
var _this$ids$key = this.ids[key],
id = _this$ids$key.id,
onUnmount = _this$ids$key.onUnmount;
if (typeof onUnmount === 'function') {
onUnmount(id);
}
}
}
}]);
return Provider;
}();
var _default = exports["default"] = Provider;