@looker/embed-services
Version:
83 lines • 3.64 kB
JavaScript
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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); }
export var ContentType = function (ContentType) {
ContentType["Look"] = "look";
ContentType["Explore"] = "explore";
ContentType["Dashboard"] = "dashboard";
ContentType["Invalid"] = "invalid";
return ContentType;
}({});
var THEMABLE_CONTENT = [ContentType.Dashboard, ContentType.Explore];
export class EmbedUrl {
constructor() {
var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.location.href;
_defineProperty(this, "_url", void 0);
_defineProperty(this, "path", void 0);
_defineProperty(this, "searchParams", void 0);
_defineProperty(this, "isDashboard", void 0);
_defineProperty(this, "isExplore", void 0);
_defineProperty(this, "isLook", void 0);
_defineProperty(this, "isThemable", void 0);
_defineProperty(this, "contentType", void 0);
_defineProperty(this, "_embedUrl", void 0);
this._url = new URL(url);
this.searchParams = Object.fromEntries(this._url.searchParams);
this.path = this._url.pathname;
this.isDashboard = /^(\/embed)?\/dashboards\//i.test(this.path);
this.isExplore = /^(\/embed)?\/explore\//i.test(this.path);
this.isLook = /^(\/embed)?\/looks\//i.test(this.path);
this.contentType = this.type();
if (this.path.startsWith('/embed/')) {
this._embedUrl = "".concat(this._url.origin).concat(this._url.pathname);
} else {
this._embedUrl = "".concat(this._url.origin, "/embed").concat(this._url.pathname);
}
this.isThemable = THEMABLE_CONTENT.includes(this.contentType);
}
get url() {
return this._url.href;
}
type() {
var type;
if (this.isLook) {
type = ContentType.Look;
} else if (this.isExplore) {
type = ContentType.Explore;
} else if (this.isDashboard) {
type = ContentType.Dashboard;
} else {
type = ContentType.Invalid;
}
return type;
}
embedUrl() {
var includeSearchParams = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.contentType === ContentType.Invalid) {
throw new Error('Invalid content type');
}
var embedUrlParams = includeSearchParams ? this.searchParams : {};
if (overrides) {
Object.entries(overrides).forEach(_ref => {
var [key, value] = _ref;
var overrideValue = value;
if (typeof value === 'object') {
overrideValue = JSON.stringify(value);
}
if (key === 'theme' && overrideValue === 'Looker') {
delete embedUrlParams.theme;
} else {
embedUrlParams[key] = overrideValue;
}
});
}
var searchString = '';
if (Object.keys(embedUrlParams).length > 0) {
searchString = '?' + new URLSearchParams(embedUrlParams).toString();
}
var url = new URL("".concat(this._embedUrl).concat(searchString));
return url.href;
}
}
//# sourceMappingURL=EmbedUrl.js.map