UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

76 lines 3.06 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var Link_1; /* eslint-disable no-useless-escape */ import { JsonObject, JsonProperty } from "ta-json"; import URI from "urijs"; import Guard from "./guard"; let Link = Link_1 = class Link { constructor(uri, title, templated) { this.templated = false; if (typeof uri === "string") { this.href = uri; } else { this.href = uri.href(); } if (title) { this.title = title; } this.templated = !!templated; } getVariableValue(template, variable) { Guard.notNullOrUndefined(template); Guard.stringNotNullOrEmpty(variable); Guard.stringNotNullOrEmpty(this.href); const templateUri = new URI(template.href); const currentUri = new URI(this.href); const index = templateUri .path() .split("/") .findIndex(item => item.startsWith(`{${variable}}`)); if (index) { return decodeURIComponent(currentUri.path().split("/")[index]); } let queryRegexStr = `\?|,${variable.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")},|$`; let queryRegex = new RegExp(queryRegexStr); if (queryRegex.test(templateUri.query())) { queryRegexStr = `(?:^|&)${variable.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")}=([^#&]+)(?:[#&]?|$)`; queryRegex = new RegExp(queryRegexStr); const matches = queryRegex.exec(currentUri.query()); if (matches) { return decodeURIComponent(matches[1]); } } throw `Variable '${variable}' not found in template '${template.href}'.`; } static copyFrom(source) { return new Link_1(source.href, source.title, source.templated); } }; __decorate([ JsonProperty("href"), __metadata("design:type", String) ], Link.prototype, "href", void 0); __decorate([ JsonProperty("title"), __metadata("design:type", String) ], Link.prototype, "title", void 0); __decorate([ JsonProperty("templated"), __metadata("design:type", Boolean) ], Link.prototype, "templated", void 0); Link = Link_1 = __decorate([ JsonObject(), __metadata("design:paramtypes", [Object, String, Boolean]) ], Link); export default Link; export { Link }; //# sourceMappingURL=link.js.map