@hashuplabs/omni-filter
Version:
OmniFilter Search component
375 lines • 18.4 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import React from 'react';
import { Component } from 'react';
import { OmniLikeOperator } from '../operators/OmniLikeOperator';
var OmniFilterSuggestionWindow = /** @class */ (function (_super) {
__extends(OmniFilterSuggestionWindow, _super);
// private _ev_lock=false;
function OmniFilterSuggestionWindow(props) {
var _this = _super.call(this, props) || this;
_this._isWaiting = false;
_this._hasInput = false;
_this._entities = props.filters;
_this._entities.forEach(function (x) {
if (x.filter.default) {
_this._defaultEntity = x;
}
});
_this.state = {
_selectionIndex: null,
_operator: null,
_entity: null,
_entityName: null,
_values: null,
_awaitedValue: null,
_waiting: false,
_selectionList: [],
_selectionWidget: null,
};
return _this;
}
OmniFilterSuggestionWindow.prototype._handleUpdate = function () {
if (this.props.inputElement)
this.props.inputElement.type = 'text';
this.setState({ _selectionIndex: 0, _selectionWidget: null });
this.props.onUpdate(this.props.token);
};
OmniFilterSuggestionWindow.prototype._handleCompleted = function () {
if (this.props.inputElement)
this.props.inputElement.type = 'text';
this.setState({
_operator: null,
_entity: null,
_entityName: null,
_values: null,
_selectionIndex: null,
_selectionList: [],
_waiting: false,
_awaitedValue: null,
_selectionWidget: null,
});
this.props.onComplete(this.props.token);
};
OmniFilterSuggestionWindow.prototype.refocus = function () {
var _this = this;
var _a;
(_a = this.props.inputElement) === null || _a === void 0 ? void 0 : _a.blur();
setTimeout(function () { var _a; return (_a = _this.props.inputElement) === null || _a === void 0 ? void 0 : _a.focus(); }, 101);
};
OmniFilterSuggestionWindow.prototype.applyValue = function () {
var filtered = this._filteredSelections();
if (this.props.tokenInput.length && !filtered.length && this._defaultEntity) {
// Apply all defaults and set completed
if (!this.props.token.entity) {
this.props.token.entity = this._defaultEntity;
this.props.token.entityName = this._defaultEntity.entityName;
}
if (!this.props.token.operator) {
this.props.token.operator = new OmniLikeOperator(); // FIXME: Make this a prop ( defaultTokenOperator )
}
var value = this.props.tokenInput;
this.props.token.values = [{ id: value, values: [value], label: value }];
this._handleCompleted();
this.refocus();
return;
}
var selected = filtered[this.state._selectionIndex || 0];
selected.onSelect(selected);
// TODO: this.props.inputElement?.blur();
// TODO: setTimeout(()=>this.props.inputElement?.focus(),100); //500
this.refocus();
};
OmniFilterSuggestionWindow.prototype.componentDidUpdate = function () {
var _this = this;
this._list_i();
if (!this._hasInput && this.props.inputElement) {
this.props.inputElement.onkeyup = function (e) {
// console.log('______ KEYUP ______', e.key);
var dir = 0;
switch (e.key) {
case 'Enter':
case 'Tab':
_this.applyValue();
e.preventDefault();
e.stopPropagation();
break;
case 'ArrowDown':
dir = 1;
break;
case 'ArrowUp':
dir = -1;
break;
}
if (dir !== 0) {
// TODO: FIXME: This is screwy
var origIndex = _this.state._selectionIndex;
if (!origIndex)
origIndex = 0;
if (origIndex < 0 || origIndex > _this.state._selectionList.length) {
// console.warn('INDEX ', origIndex, ' out of bounds');
return;
}
// console.log('SET_KEYUP_STATE', origIndex, dir);
_this.setState({ _selectionIndex: origIndex + dir });
}
};
this._hasInput = true;
}
};
OmniFilterSuggestionWindow.prototype.boldify = function (input) {
if (this.props.tokenInput.length < 1)
return input;
var s = input.toLowerCase().indexOf(this.props.tokenInput.toLowerCase());
var len = this.props.tokenInput.length;
var before = input.substring(0, s);
var during = input.substring(s, s + len);
var after = input.substring(s + len);
if (s > -1) {
return (React.createElement("div", null,
React.createElement("span", null, before),
React.createElement("span", { style: { fontWeight: 'bold' } }, during),
React.createElement("span", null, after)));
}
return React.createElement("div", null, input);
};
OmniFilterSuggestionWindow.prototype._renderEntities = function () {
var _this = this;
if (this.props.inputElement)
this.props.inputElement.type = 'text';
var visibleItems = [];
this._entities.map(function (entity) {
visibleItems.push({
label: entity.filter.label,
value: entity,
onSelect: function (v) {
_this.props.token.entity = v.value;
_this.props.token.entityName = v.value.entityName;
// console.log(this.state);
_this._handleUpdate();
},
});
});
this.setState({ _selectionList: visibleItems });
};
OmniFilterSuggestionWindow.prototype._renderOperators = function () {
var _this = this;
if (this.props.inputElement)
this.props.inputElement.type = 'text';
if (this.props.token.entity != null) {
var visibleItems_1 = [];
this.props.token.entity.type.operators.map(function (op) {
visibleItems_1.push({
label: op.label,
value: op,
onSelect: function (v) {
_this.setState({ _operator: v.value });
_this.props.token.operator = v.value;
_this._handleUpdate();
},
});
});
this.setState({ _selectionList: visibleItems_1 });
}
};
OmniFilterSuggestionWindow.prototype._renderValues = function () {
var _a, _b, _c, _d, _e, _f;
return __awaiter(this, void 0, void 0, function () {
var values, visibleItems_2;
var _this = this;
return __generator(this, function (_g) {
switch (_g.label) {
case 0:
values = [];
if (!((_b = (_a = this.props.token.entity) === null || _a === void 0 ? void 0 : _a.filter) === null || _b === void 0 ? void 0 : _b.resolve)) return [3 /*break*/, 2];
return [4 /*yield*/, this.props.token.entity.filter.resolve()];
case 1:
values = _g.sent();
return [3 /*break*/, 3];
case 2:
values = [
{
id: '"' + this.props.tokenInput + '"',
label: this.props.tokenInput.length ? '"' + this.props.tokenInput + '"' : '',
values: [this.props.tokenInput],
},
];
_g.label = 3;
case 3:
if (values.length) {
if (((_c = this.props.token.entity) === null || _c === void 0 ? void 0 : _c.type.inputType) && this.props.inputElement) {
this.props.inputElement.type = this.props.token.entity.type.inputType;
}
if ((_d = this.props.token.operator) === null || _d === void 0 ? void 0 : _d.editor) {
this.setState({
_selectionList: [],
_selectionWidget: this.props.token.operator.editor(this.props.token, function (value) {
_this.setState({
_values: value,
});
_this.props.token.values = [{ id: value.join('\r\n'), values: value, label: "".concat(value.length, " items") }];
_this._handleCompleted();
}),
});
return [2 /*return*/];
}
else if ((_e = this.props.token.entity) === null || _e === void 0 ? void 0 : _e.type.picker) {
this.setState({
_selectionList: [],
_selectionWidget: (_f = this.props.token.entity) === null || _f === void 0 ? void 0 : _f.type.picker(function (value) {
_this.setState({
_values: [value],
});
_this.props.token.values = [{ id: value, label: value, values: [value] }];
_this._handleCompleted();
}),
});
return [2 /*return*/];
}
visibleItems_2 = [];
values.map(function (v) {
visibleItems_2.push({
label: v.label,
value: v,
onSelect: function (rval) {
_this.setState({
_values: [rval.value.id],
});
_this.props.token.values = [rval.value];
_this._handleCompleted();
},
});
});
// console.log('RENDER_VALUES ==> SET STATE');
this.setState({ _selectionList: visibleItems_2, _selectionWidget: null });
}
return [2 /*return*/];
}
});
});
};
OmniFilterSuggestionWindow.prototype._inner = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!this.props.token.hasEntityName) return [3 /*break*/, 1];
return [2 /*return*/, this._renderEntities()];
case 1:
if (!!this.props.token.hasOperator) return [3 /*break*/, 2];
return [2 /*return*/, this._renderOperators()];
case 2: return [4 /*yield*/, this._renderValues()];
case 3: return [2 /*return*/, _a.sent()];
}
});
});
};
OmniFilterSuggestionWindow.prototype._filteredSelections = function () {
var _this = this;
return this.state._selectionList.filter(function (ent) {
return _this.props.tokenInput.length === 0 || ent.label.toLowerCase().includes(_this.props.tokenInput.toLowerCase());
});
};
OmniFilterSuggestionWindow.prototype.renderSuggestions = function () {
var _this = this;
if (this.state._selectionWidget != null && this.props.token.hasEntityName && this.props.token.hasOperator) {
return this.state._selectionWidget;
}
var filtered = this._filteredSelections();
if (filtered.length === 0) {
return React.createElement("li", { className: 'selected' }, this.boldify(this.props.tokenInput));
}
return filtered.map(function (v, k) {
var className = k === _this.state._selectionIndex ? 'selected' : '';
return (React.createElement("li", { className: className, key: k },
React.createElement("button", { onClick: function () { return v.onSelect(v); } }, _this.boldify(v.label))));
});
};
OmniFilterSuggestionWindow.prototype._list_i = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._isWaiting)
return [2 /*return*/];
this._isWaiting = true;
return [4 /*yield*/, this._inner()];
case 1:
_a.sent();
this.setState({
_waiting: false,
});
this._isWaiting = false;
return [2 /*return*/];
}
});
});
};
OmniFilterSuggestionWindow.prototype._list = function () {
return (React.createElement("ul", { style: { listStyle: 'none' }, className: "list-unstyled" }, this.state._waiting ? React.createElement("div", null, "...") : React.createElement("div", null, this.renderSuggestions())));
};
OmniFilterSuggestionWindow.prototype.render = function () {
return (React.createElement("div", { className: "OmniSuggestions", style: {
display: this.props.visible || this.props.token.entityName ? 'block' : 'none',
position: 'absolute',
zIndex: 999,
minWidth: '240px',
padding: '.5em',
boxShadow: '1px 2px 20px rgba(0,0,0,0.3)',
backgroundColor: '#fff',
left: "".concat(this.props.offset, "px"),
} }, this._list()));
};
return OmniFilterSuggestionWindow;
}(Component));
export { OmniFilterSuggestionWindow };
//# sourceMappingURL=OmniFilterSuggestionWindow.js.map