@stackend/api
Version:
JS bindings to api.stackend.com
212 lines • 5.77 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scrollToAnchor = exports.getAnchorPart = exports.parseAnchor = exports.getAnchorId = exports.formatAnchor = exports.getRequest = exports.AnchorType = void 0;
var get_1 = __importDefault(require("lodash/get"));
var AnchorType;
(function (AnchorType) {
AnchorType["BLOG"] = "blog";
AnchorType["USER"] = "user";
AnchorType["SITE"] = "site";
AnchorType["COMMENT"] = "comment";
AnchorType["TAGS"] = "tags";
AnchorType["FORUM"] = "forum";
})(AnchorType = exports.AnchorType || (exports.AnchorType = {}));
/**
* Get the request object
*/
function getRequest() {
return function (dispatch, getState) {
return (0, get_1.default)(getState(), 'request');
};
}
exports.getRequest = getRequest;
/**
* Construct the string version of an anchor
* @param anchor
* @returns {string}
*/
function formatAnchor(anchor) {
if (!anchor) {
return '';
}
var s = '#/' + anchor.type + '/' + anchor.permalink;
if (anchor.items) {
for (var i = 0; i < anchor.items.length; i++) {
var a = anchor.items[i];
s += ';' + a.type + '/' + a.permalink;
}
}
return s;
}
exports.formatAnchor = formatAnchor;
/**
* Get the element id for an anchor
* @param anchor
* @returns {null}
*/
function getAnchorId(anchor) {
if (!anchor) {
return null;
}
return '/' + anchor.type + '/' + anchor.permalink;
}
exports.getAnchorId = getAnchorId;
/**
* Parse the stackend anchor
* @param anchor
* @returns {StackendAnchor|null}
*/
function parseAnchor(anchor) {
if (!anchor) {
return null;
}
var s = anchor.replace(/^#?\/?/, '');
var v = s.split(';');
if (v.length === 0) {
return null;
}
var a = {
type: '',
permalink: '/' + s,
items: []
};
for (var i = 0; i < v.length; i++) {
var x = parseAnchorInt(v[i]);
if (x) {
a.items.push(x);
}
}
var items = a.items;
if (items.length !== 0) {
a.type = items[0].type;
var i = s.indexOf('/');
a.permalink = s.substring(i + 1);
}
return a;
}
exports.parseAnchor = parseAnchor;
function parseAnchorInt(anchor) {
if (anchor.indexOf('/') === 0) {
anchor = anchor.substring(1);
}
var i = anchor.indexOf('/');
if (i === -1) {
return null;
}
var type = anchor.substring(0, i);
var permalink = anchor.substring(i + 1);
var a = {
type: type,
permalink: permalink
};
var v = permalink.split('/');
switch (type) {
case AnchorType.BLOG:
// / blog/BLOGKEY/BLOGKEY/ENTRY
if (v.length >= 2) {
// Regular handling
a.blogKey = v.slice(0, 2).join('/');
if (v.length > 2) {
a.blogEntryPermalink = v[v.length - 1];
}
}
break;
case AnchorType.USER:
// user/1/1
if (v.length === 2) {
a.userId = parseInt(v[0]);
}
break;
case AnchorType.SITE:
// site/SITE/PAGE/PAGE
if (v.length >= 2) {
a.sitePermalink = v[0];
v.shift();
a.pagePermalink = v.join('/');
}
break;
case AnchorType.COMMENT:
// comment/REFID/ID/type/....
if (v.length >= 2) {
a.referenceId = parseInt(v[0]);
a.id = parseInt(v[1]);
v.shift();
v.shift();
var n = v.join('/');
a.reference = parseAnchor(n);
}
break;
case AnchorType.TAGS:
// tags/TAG/TAG/TAG
a.tags = __spreadArray([], v, true);
break;
case AnchorType.FORUM:
// forum/FORUM_PERMALINK/FORUM_THREAD_PERMALINK
if (v.length >= 1) {
// Regular handling
a.forumPermalink = v[0];
if (v.length > 1) {
a.forumThreadPermalink = v[1];
}
}
break;
}
return a;
}
/**
* Get a specific part of an anchor
* @param anchor
* @param type
* @returns {StackendAnchor|null}
*/
function getAnchorPart(anchor, type) {
if (!anchor || !type || !anchor.items) {
return null;
}
var items = anchor.items;
if (!items) {
return null;
}
for (var i = 0; i < items.length; i++) {
var x = items[i];
if (x.type === type) {
return x;
}
}
return null;
}
exports.getAnchorPart = getAnchorPart;
/**
* Scroll to a stackend anchor
* @param anchor
*/
function scrollToAnchor(anchor) {
if (!anchor) {
return;
}
var id = getAnchorId(anchor);
if (id) {
var e = document.getElementById(id);
if (e) {
console.log('Stackend: scrolling to', id);
e.scrollIntoView();
}
}
else {
console.warn('Stackend: anchor', id, ' not found');
}
}
exports.scrollToAnchor = scrollToAnchor;
//# sourceMappingURL=index.js.map