UNPKG

@sysdoc/sharepoint-utils

Version:

Sysdoc's core Sharepoint utilities

185 lines (184 loc) 6.87 kB
"use strict"; /*! * Copyright Sysdoc @ 2019 */ 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SPSearchResultPageModel = exports.makeSPSearchResultEntry = exports.extractTagFromMeta = exports.extractNameFromUserField = void 0; var utilities_1 = require("@sysdoc/utilities"); var mobx_1 = require("mobx"); function extractNameFromUserField(txt) { var val = typeof txt === "string" ? txt.split(";") : ""; if (val.length > 0) { return val[0]; } return ""; } exports.extractNameFromUserField = extractNameFromUserField; function extractTagFromMeta(txt) { if (!txt) { return ""; } if (txt.indexOf("GP0|#") > -1) { var val = txt.split(";")[1].split("|"); return val[val.length - 1]; } else if (txt.indexOf("|") > -1) { return txt.split("|")[1]; } return txt; } exports.extractTagFromMeta = extractTagFromMeta; function makeSPSearchResultEntry(result) { try { return { datePublished: result.Write ? new Date(result.Write) : null, title: result.Title, snippet: utilities_1.shortenText(utilities_1.getTextContentFromHTMLString(result.HitHighlightedSummary) || "", 160), href: result.Path, payload: result }; } catch (err) { console.error(err); return null; } } exports.makeSPSearchResultEntry = makeSPSearchResultEntry; var SPSearchResultPageModel = /** @class */ (function () { function SPSearchResultPageModel(result, rowLimit, resultsTransformer, disableScroll) { var _this = this; if (disableScroll === void 0) { disableScroll = false; } this.result = result; this.rowLimit = rowLimit; this.resultsTransformer = resultsTransformer; this.totalRows = 0; this.page = 1; this.pending = 0; this.entries = []; this.disableScroll = false; this.next = function () { if (_this.page < (_this.noOfPages)) { _this.pending++; _this.result.getPage(++_this.page) .then(_this.onSuccess); } }; this.prev = function () { if (_this.page > 0) { _this.pending++; _this.result.getPage(--_this.page) .then(_this.onSuccess); } }; this.goToPage = function (page) { _this.pending++; _this.page = page; _this.result.getPage(_this.page) .then(_this.onSuccess, _this.onFail); }; this.onSuccess = function (page) { _this.result = page; _this.pending--; setTimeout(function () { return _this.load(); }, 15); }; this.onFail = function (page) { _this.result = page; _this.pending--; setTimeout(function () { return _this.error(); }, 15); }; this.load(); this.scrollElement = document.getElementById("s4-workspace"); this.targetScrollStop = 0; this.resultsTransformer = resultsTransformer || null; this.disableScroll = disableScroll; } Object.defineProperty(SPSearchResultPageModel.prototype, "noOfPages", { get: function () { return Math.ceil(this.totalRows / this.rowLimit); }, enumerable: false, configurable: true }); Object.defineProperty(SPSearchResultPageModel.prototype, "currentPage", { get: function () { return this.page; }, enumerable: false, configurable: true }); SPSearchResultPageModel.prototype.updateTemplates = function (temps) { var entries = this.entries; temps.forEach(function (e, i) { if (i < entries.length) { e.setData(entries[i]); e.isLoading = false; e.isVisible = true; } else { e.isVisible = false; } }); }; SPSearchResultPageModel.prototype.error = function () { }; SPSearchResultPageModel.prototype.load = function () { this.entries = this.result.PrimarySearchResults.map(this.resultsTransformer ? this.resultsTransformer : makeSPSearchResultEntry); this.totalRows = this.result.TotalRows; this.elapsedTime = this.result.ElapsedTime; var el = this.scrollElement; if (el && !this.disableScroll) { el.scrollTop = this.targetScrollStop; return false; } }; __decorate([ mobx_1.computed ], SPSearchResultPageModel.prototype, "noOfPages", null); __decorate([ mobx_1.computed ], SPSearchResultPageModel.prototype, "currentPage", null); __decorate([ mobx_1.observable ], SPSearchResultPageModel.prototype, "totalRows", void 0); __decorate([ mobx_1.observable ], SPSearchResultPageModel.prototype, "elapsedTime", void 0); __decorate([ mobx_1.observable ], SPSearchResultPageModel.prototype, "page", void 0); __decorate([ mobx_1.observable ], SPSearchResultPageModel.prototype, "pending", void 0); __decorate([ mobx_1.observable.ref ], SPSearchResultPageModel.prototype, "entries", void 0); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "error", null); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "next", void 0); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "prev", void 0); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "goToPage", void 0); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "load", null); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "onSuccess", void 0); __decorate([ mobx_1.action ], SPSearchResultPageModel.prototype, "onFail", void 0); return SPSearchResultPageModel; }()); exports.SPSearchResultPageModel = SPSearchResultPageModel;