UNPKG

semantic-network

Version:

A utility library for manipulating a list of links that form a semantic interface to a network of resources.

193 lines 10.7 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 (g && (g = 0, op[0] && (_ = 0)), _) 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 }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SearchUtil = void 0; var semantic_link_1 = require("semantic-link"); var instanceOfTrackedRepresentation_1 = require("../utils/instanceOf/instanceOfTrackedRepresentation"); var instanceOfCollection_1 = require("../utils/instanceOf/instanceOfCollection"); var sparseRepresentationFactory_1 = require("../representation/sparseRepresentationFactory"); var singletonMerger_1 = require("../representation/singletonMerger"); var anylogger_1 = __importDefault(require("anylogger")); var create_1 = require("../representation/create"); var collectionMerger_1 = require("../representation/collectionMerger"); var linkRelation_1 = require("../linkRelation"); var resourceUtil_1 = require("../utils/resourceUtil"); var get_1 = require("../representation/get"); var equalityUtil_1 = require("../utils/equalityUtil"); var log = (0, anylogger_1.default)('SearchUtil'); var SearchUtil = /** @class */ (function () { function SearchUtil() { } /** * Create or get a tracked resource that is a collection to store search collections. It * is likely (but not required) that the resource is backed by a 'real' resource. * * This can be used to store search results in an ad-hoc manner where the search * result doesn't have to have a name. The 'self' link of the search result can be used * to identify what the search was (and should be unique). */ SearchUtil.makePooledCollection = function (context, options) { if ((0, instanceOfTrackedRepresentation_1.instanceOfTrackedRepresentation)(context)) { var _a = __assign({}, options).rel, rel = _a === void 0 ? undefined : _a; if (rel) { var uri = semantic_link_1.LinkUtil.getUri(context, rel); if (uri) { var poolName = resourceUtil_1.ResourceUtil.makeName(options); var pool = context[poolName]; if (pool) { if ((0, instanceOfTrackedRepresentation_1.instanceOfTrackedRepresentation)(pool)) { if ((0, instanceOfCollection_1.instanceOfCollection)(pool)) { return pool; } else { throw new Error("Pool '".concat(poolName, "' is not a collection")); } } else { throw new Error("Attribute '".concat(poolName, "' is not a tracked resource")); } } else { var newSearches = sparseRepresentationFactory_1.SparseRepresentationFactory.make(__assign(__assign({}, options), { sparseType: 'collection', uri: uri })); singletonMerger_1.SingletonMerger.add(context, poolName, newSearches, options); return newSearches; } } else { throw new Error("Link relation '".concat(rel, "' not found")); } } else { throw new Error("The pool collection requires a link relation"); } } throw new Error("Failed to create pool collection"); }; /** * * Return a search result collection that is a tracked resource collection on the context collection. * * Each time this is called the new search result is merged into the search result collection that can * be bound to the view. * * With each call, the pooled search collection will use existing items or fetch new ones to minimise the * across the wire calls. * * The default implementation will create a '.pooled-search' sub-collection that can be view in devtool * * @param context resource that has the search collection as a link relation * @param document the search fields that will be used for the search (and is merged into the create/search form) * @param options */ SearchUtil.search = function (context, document, options) { return __awaiter(this, void 0, void 0, function () { var _a, _b, searchRel, _c, searchName, _d, searchPooledPrefix, opts, pooledResource, searchResource, results; return __generator(this, function (_e) { switch (_e.label) { case 0: _a = __assign({}, options), _b = _a.searchRel, searchRel = _b === void 0 ? linkRelation_1.LinkRelation.Search : _b, _c = _a.searchName, searchName = _c === void 0 ? undefined : _c, _d = _a.searchPooledPrefix, searchPooledPrefix = _d === void 0 ? '.pooled-' : _d, opts = __rest(_a, ["searchRel", "searchName", "searchPooledPrefix"]); pooledResource = SearchUtil.makePooledCollection(context, __assign(__assign({}, options), { rel: searchRel, name: searchName || "".concat(searchPooledPrefix).concat(searchRel) })); return [4 /*yield*/, (0, get_1.get)(context, __assign(__assign({}, opts), { rel: searchRel }))]; case 1: searchResource = _e.sent(); return [4 /*yield*/, (0, create_1.create)(document, __assign(__assign({}, opts), { createContext: searchResource, makeSparseStrategy: function (options) { return (0, sparseRepresentationFactory_1.pooledCollectionMakeStrategy)(pooledResource, options); } }))]; case 2: results = _e.sent(); if (results) { // search collections don't want to merge the result links into the original search links collectionMerger_1.CollectionMerger.merge(searchResource, results, __assign({ mergeLinks: false }, opts)); } else { log.debug('no search results available'); } return [2 /*return*/, searchResource]; } }); }); }; /** * In-place add to a (search) collection a new item. This is used where the current list is being added to independently * of a newly added collection without returning the entire search collection * * @default equalityMatch {@link CanonicalOrSelf} */ SearchUtil.update = function (collection, item, equalityMatch) { if ((0, instanceOfCollection_1.instanceOfCollection)(collection) && item) { var index = collection.items.findIndex(function (r) { return equalityUtil_1.EqualityUtil.matches(item, r, equalityMatch); }); if (index >= 0) { // if found, replace collection.items.splice(index, 1, item); } else { // else add to head collection.items.splice(0, 0, item); } } else { log.debug('No collection found to update'); } }; return SearchUtil; }()); exports.SearchUtil = SearchUtil; //# sourceMappingURL=searchUtil.js.map