@raona/sp
Version:
Raona utilities to work with Sharepoint using pnp/sp
133 lines (132 loc) • 6.09 kB
JavaScript
"use strict";
// import { ITerm, ITermData } from "@pnp/sp-taxonomy";
Object.defineProperty(exports, "__esModule", { value: true });
var TermTree = /** @class */ (function () {
function TermTree(terms, keepTopLevelOrder) {
var _this = this;
if (keepTopLevelOrder === void 0) { keepTopLevelOrder = false; }
this.terms = terms;
this.nameToPosition = {};
this.termPathToPosition = {};
this.guidToPosition = {};
this.childrenInfo = {};
this.parentInfo = {};
this.upperTerms = [];
var customSortOrder = [];
if (keepTopLevelOrder) {
for (var i = 0; i < terms.length; i++) {
var t = terms[i];
var path = t.PathOfTerm;
var depth = path.split(';').length;
if (depth > 1)
break;
customSortOrder.push(path);
}
}
this.terms = terms.sort(function (a, b) { return a.PathOfTerm.localeCompare(b.PathOfTerm); });
this.terms.forEach(function (t, i) {
_this.nameToPosition[t.Name] = i;
_this.guidToPosition[t.Id] = i;
_this.termPathToPosition[t.PathOfTerm] = i;
var splittedPath = t.PathOfTerm.split(';');
var level = splittedPath.length - 1;
if (level === 0) {
_this.upperTerms.push(i);
}
else {
// let parentLevel = level - 1;
// let parentName = parentLevel >= 0 ? splittedPath[parentLevel] : null;
var parentPath = splittedPath.slice(0, splittedPath.length - 1).join(';');
var parentPosition = _this.termPathToPosition[parentPath];
var childrenArray = _this.childrenInfo[parentPosition];
_this.parentInfo[t.Id] = parentPosition;
if (!childrenArray) {
childrenArray = [i];
}
else {
childrenArray.push(i);
}
_this.childrenInfo[parentPosition] = childrenArray;
}
});
if (keepTopLevelOrder) {
this.upperTerms = this.upperTerms.sort(function (a, b) {
return customSortOrder.indexOf(_this.terms[a].PathOfTerm) - customSortOrder.indexOf(_this.terms[b].PathOfTerm);
});
}
}
TermTree.prototype.getRootTermsOfTermSet = function () {
var _this = this;
return this.upperTerms.map(function (ut) { return _this.terms[ut]; });
};
TermTree.prototype.getRootTermNamesOfTermSet = function () {
var _this = this;
return this.upperTerms.map(function (ut) { return _this.terms[ut].Name; });
};
TermTree.prototype.getChildrenTermsByTermName = function (termName) {
var _this = this;
return (this.childrenInfo[this.nameToPosition[termName]] || []).map(function (cI) { return _this.terms[cI]; });
};
TermTree.prototype.getChildrenTermNamesByTermName = function (termName) {
var _this = this;
return (this.childrenInfo[this.nameToPosition[termName]] || []).map(function (cI) { return _this.terms[cI].Name; });
};
TermTree.prototype.getChildrenTermsByTermGuid = function (guid) {
var _this = this;
return (this.childrenInfo[this.guidToPosition[guid]] || []).map(function (cI) { return _this.terms[cI]; });
};
TermTree.prototype.getChildrenTermNamesByTermGuid = function (guid) {
var _this = this;
return (this.childrenInfo[this.guidToPosition[guid]] || []).map(function (cI) { return _this.terms[cI].Name; });
};
TermTree.prototype.getTermsById = function () {
var _this = this;
var guids = [];
for (var _i = 0; _i < arguments.length; _i++) {
guids[_i] = arguments[_i];
}
return guids.map(function (g) { return _this.terms[_this.guidToPosition[g]]; });
};
TermTree.prototype.getTermNamesById = function () {
var _this = this;
var guids = [];
for (var _i = 0; _i < arguments.length; _i++) {
guids[_i] = arguments[_i];
}
return guids.map(function (g) { return _this.terms[_this.guidToPosition[g]].Name; });
};
TermTree.prototype.getParenTermById = function (guid) {
return this.terms[this.parentInfo[guid]];
};
/**
* Gets the parent chain optionally cutting it at certain depthLevel
* @param childrenGuid the guid of the children taxonomy
* @example tree -> A[B, C, D[E, F]], B[C, D] -> if you pass the guid from F and don't specify depth level, it will return F, D, A; if you specify a depthLevel of 0 it will return only A; etc.
* @param [untilDepthLevel] Depth level starting from the upper parent from where you want to cut the chain
* @returns parent chain
*/
TermTree.prototype.getParentChain = function (childrenGuid, untilDepthLevel) {
// TODO: improve with a recursive version?
var currentTerm = this.getTermsById(childrenGuid)[0];
if (typeof currentTerm === 'undefined')
return [];
var termChain = [currentTerm];
var isCurrentTermUndefined;
do {
currentTerm = this.getParenTermById(currentTerm.Id);
isCurrentTermUndefined = typeof currentTerm === 'undefined';
if (!isCurrentTermUndefined)
termChain.push(currentTerm);
} while (!isCurrentTermUndefined);
if (typeof untilDepthLevel !== 'undefined') {
// Cuts an array starting from the end (see example at function definition documentation)
termChain = termChain.slice(untilDepthLevel * -1 - 1);
}
return termChain;
};
TermTree.prototype.termExistsOnTermSet = function (guid) {
return this.guidToPosition[guid] !== undefined;
};
return TermTree;
}());
exports.TermTree = TermTree;