@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
329 lines • 17.3 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
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));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAspectRatio = exports.saveAnalytics2 = exports.saveAnalytics3 = exports.getMinPerformance = exports.getMinPerformanceString = void 0;
var ErrorHandler_1 = require("../Logging/ErrorHandler");
var LogFunctions_1 = require("../Logging/LogFunctions");
var LogFunctions_2 = require("../Logging/LogFunctions");
var basicOperations_1 = require("../Math/basicOperations");
var urlServices_1 = require("../Strings/urlServices");
var IPerformance_1 = require("../../Performance/IPerformance");
function getMinPerformanceString(performanceObj, capMS, capValue) {
if (capMS === void 0) { capMS = 7000; }
if (capValue === void 0) { capValue = 'paused?'; }
var minPerformanceString = '';
if (performanceObj) {
var minPerformance = getMinPerformance(performanceObj, capMS, capValue);
minPerformanceString = JSON.stringify(minPerformance);
}
return minPerformanceString;
}
exports.getMinPerformanceString = getMinPerformanceString;
/**
*
* @param performanceObj: ILoadPerformance
* @capMS - max Milliseconds to save.... else return 'error' or null for that value.
* @capValue - if ms value exceeds capMS, return this value in place of value
* @returns
*/
function getMinPerformance(performanceObj, capMS, capValue) {
if (capMS === void 0) { capMS = 7000; }
if (capValue === void 0) { capValue = 'paused?'; }
var minPerformance = {
mode: null,
sets: {},
ops: {},
};
if (performanceObj && performanceObj.mode) {
minPerformance.mode = performanceObj.mode;
}
var keys = __spreadArray(__spreadArray(__spreadArray([], Object.keys(performanceObj), true), Object.keys(performanceObj.sets), true), Object.keys(performanceObj.ops), true);
keys.map(function (key) {
if (IPerformance_1.LoadPerformanceOps.indexOf(key) > -1) {
var thisKey = key;
var performanceX = performanceObj.ops[key];
if (key.indexOf('setting') === 0) {
minPerformance.sets[thisKey] = performanceObj.sets[key]; // NOTE: This line does not TS Error out when in an actual project.
}
else if (performanceX) {
var ms = performanceX['ms'] && performanceX['ms'] <= capMS ? performanceX['ms'] : capValue;
var count = performanceX.c ? performanceX.c : undefined;
minPerformance.ops[thisKey] = {
label: performanceX['label'],
ms: ms,
c: count ? count : undefined,
a: count ? Math.round((ms / count) * 10) / 10 : undefined,
};
}
}
});
if (performanceObj.getAllProps === true || performanceObj.getAllProps === false) {
minPerformance.getAllProps = performanceObj.getAllProps;
}
return minPerformance;
}
exports.getMinPerformance = getMinPerformance;
/**
* Same as 2 but also adds the language to the object
* @param analyticsWeb
* @param analyticsList
* @param saveObject
* @param muteConsole
*/
function saveAnalytics3(analyticsWeb, analyticsList, saveObject, muteConsole, language) {
if (language && language.length > 0) {
saveObject.language = navigator.language;
}
else if (navigator && navigator.language) {
saveObject.language = navigator.language;
}
else {
saveObject.language = 'Unknown';
}
saveObject.AnalyticsVersion = saveObject.AnalyticsVersion ? saveObject.AnalyticsVersion : 'saveAnalytics3';
saveAnalytics2(analyticsWeb, analyticsList, saveObject, muteConsole);
}
exports.saveAnalytics3 = saveAnalytics3;
function saveAnalytics2(analyticsWeb, analyticsList, saveObject, muteConsole) {
return __awaiter(this, void 0, void 0, function () {
var saveOjbectCopy, finalSaveObject, PageURLpieces, SiteLink, SiteTitle, memoryObj, screen, device, OSName;
return __generator(this, function (_a) {
saveOjbectCopy = JSON.parse(JSON.stringify(saveObject));
finalSaveObject = saveOjbectCopy;
finalSaveObject.AnalyticsVersion = finalSaveObject.AnalyticsVersion ? finalSaveObject.AnalyticsVersion : 'saveAnalytics2';
delete finalSaveObject['loadProperties'];
finalSaveObject.SiteID = saveObject.loadProperties.SiteID; //Current site collection ID for easy filtering in large list
finalSaveObject.WebID = saveObject.loadProperties.WebID; //Current web ID for easy filtering in large list
finalSaveObject.SiteTitle = saveObject.loadProperties.SiteTitle; //Web Title
finalSaveObject.ListID = saveObject.loadProperties.ListID; //Current list ID for easy filtering in large list
finalSaveObject.ListTitle = saveObject.loadProperties.ListTitle;
if (typeof saveObject.zzzRichText1 === 'object') {
finalSaveObject.zzzRichText1 = JSON.stringify(saveObject.zzzRichText1);
console.log('Length of zzzRichText1:', finalSaveObject.zzzRichText1.length);
}
else if (typeof saveObject.zzzRichText1 === 'string') {
finalSaveObject.zzzRichText1 = saveObject.zzzRichText1;
}
if (typeof saveObject.zzzRichText2 === 'object') {
finalSaveObject.zzzRichText2 = JSON.stringify(saveObject.zzzRichText2);
console.log('Length of zzzRichText2:', finalSaveObject.zzzRichText2.length);
}
else if (typeof saveObject.zzzRichText2 === 'string') {
finalSaveObject.zzzRichText2 = saveObject.zzzRichText2;
}
if (typeof saveObject.zzzRichText3 === 'object') {
finalSaveObject.zzzRichText3 = JSON.stringify(saveObject.zzzRichText3);
console.log('Length of zzzRichText3:', finalSaveObject.zzzRichText3.length);
}
else if (typeof saveObject.zzzRichText3 === 'string') {
finalSaveObject.zzzRichText3 = saveObject.zzzRichText3;
}
//Convert TargetSite to actual link object
if (typeof saveObject.loadProperties.TargetSite === 'string') {
finalSaveObject.TargetSite = {
'Url': saveObject.loadProperties.TargetSite,
'Description': saveObject.loadProperties.SiteTitle,
};
}
//Convert TargetSite to actual link TargetList
if (typeof saveObject.loadProperties.TargetList === 'string') {
finalSaveObject.TargetList = {
'Url': saveObject.loadProperties.TargetList,
'Description': saveObject.loadProperties.ListTitle,
};
}
//Create CollectionUrl string from TargetSite
if (saveObject.loadProperties.TargetSite && !finalSaveObject.CollectionUrl) {
if (typeof saveObject.loadProperties.TargetSite === 'string') {
finalSaveObject.CollectionUrl = (0, urlServices_1.getSiteCollectionUrlFromLink)(saveObject.loadProperties.TargetSite); // Should be target Site Collection Url
}
else if (typeof saveObject.loadProperties.TargetSite === 'object') {
finalSaveObject.CollectionUrl = (0, urlServices_1.getSiteCollectionUrlFromLink)(saveObject.loadProperties.TargetSite); // Should be target Site Collection Url
}
}
//Add current Page Link and Url
finalSaveObject.PageLink = (0, LogFunctions_2.getCurrentPageLink)();
PageURLpieces = finalSaveObject.PageLink.Url.split('?');
finalSaveObject.PageURL = PageURLpieces[0];
// Fix issue: https://github.com/mikezimm/PageInfo/issues/108
if (finalSaveObject.PageLink.Url && finalSaveObject.PageLink.Url.length > 254) {
finalSaveObject.PageLink.Url = finalSaveObject.PageLink.Url.substring(0, 254);
finalSaveObject.PageLink.Description += ' - ( Trimmed Url )';
}
//Add parameters
finalSaveObject.getParams = (0, LogFunctions_1.getUrlVars)().join(' & ');
SiteLink = (0, LogFunctions_1.getWebUrlFromLink)('', 'abs');
SiteTitle = SiteLink.substring(SiteLink.lastIndexOf("/") + 1);
finalSaveObject.SiteLink = {
'Url': SiteLink,
'Description': SiteTitle,
};
memoryObj = window.performance['memory'];
if (memoryObj) {
memoryObj.usedPerTotal = memoryObj.totalJSHeapSize && memoryObj.totalJSHeapSize !== 0 ? memoryObj.usedJSHeapSize / memoryObj.totalJSHeapSize : null;
memoryObj.totalPerLimit = memoryObj.jsHeapSizeLimit && memoryObj.jsHeapSizeLimit !== 0 ? memoryObj.totalJSHeapSize / memoryObj.jsHeapSizeLimit : null;
memoryObj.usedPerLimit = memoryObj.jsHeapSizeLimit && memoryObj.jsHeapSizeLimit !== 0 ? memoryObj.usedJSHeapSize / memoryObj.jsHeapSizeLimit : null;
memoryObj.Limit = (0, basicOperations_1.getSizeLabel)(memoryObj.jsHeapSizeLimit);
memoryObj.Total = (0, basicOperations_1.getSizeLabel)(memoryObj.totalJSHeapSize);
memoryObj.Used = (0, basicOperations_1.getSizeLabel)(memoryObj.usedJSHeapSize);
finalSaveObject.memory = JSON.stringify(memoryObj);
finalSaveObject.browser = 'Chromium';
finalSaveObject.JSHeapSize = memoryObj.totalJSHeapSize;
}
else {
finalSaveObject.browser = 'Not Chromium';
}
screen = null;
if (window && window.screen) {
screen = {
screenWidth: window.screen.width,
screenHeight: window.screen.height,
outerWidth: window.outerWidth,
outerHeight: window.outerHeight,
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
ratio: window.screen.width / window.screen.height,
aspect: getAspectRatio(window.screen.width, window.screen.height),
};
}
finalSaveObject.screen = JSON.stringify(screen);
finalSaveObject.screenSize = "".concat(innerHeight, " x ").concat(innerWidth);
device = null;
if (navigator && navigator.appVersion) {
OSName = null;
if (navigator.appVersion) {
if (navigator.appVersion.indexOf("Win") != -1)
OSName = "Windows";
else if (navigator.appVersion.indexOf("Mac") != -1)
OSName = "MacOS";
else if (navigator.appVersion.indexOf("X11") != -1)
OSName = "UNIX";
else if (navigator.appVersion.indexOf("Linux") != -1)
OSName = "Linux";
}
device = {
OSName: OSName,
platform: navigator.platform,
maxTouchPoints: navigator.maxTouchPoints,
vendor: navigator.vendor,
language: navigator.language,
deviceMemory: navigator['deviceMemory'],
};
}
finalSaveObject.device = JSON.stringify(device);
(0, ErrorHandler_1.saveThisLogItem)(analyticsWeb, analyticsList, finalSaveObject, muteConsole);
return [2 /*return*/];
});
});
}
exports.saveAnalytics2 = saveAnalytics2;
function getAspectRatio(width, height) {
if (height === 0 || width === 0) {
return 'na';
}
else {
var result = "".concat(width, " / ").concat(height);
var ratio = (0, basicOperations_1.roundRatio)(width / height);
if (ratio === (0, basicOperations_1.roundRatio)(16 / 9)) {
result = '16 / 9';
}
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 16)) {
result = '9 / 16';
}
else if (ratio === (0, basicOperations_1.roundRatio)(4 / 3)) {
result = '4 / 3';
}
else if (ratio === (0, basicOperations_1.roundRatio)(3 / 4)) {
result = '3 / 4';
}
else if (ratio === (0, basicOperations_1.roundRatio)(21 / 9)) {
result = '21 / 9';
}
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 21)) {
result = '9 / 21';
}
else if (ratio === (0, basicOperations_1.roundRatio)(14 / 9)) {
result = '14 / 9';
}
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 14)) {
result = '9 / 14';
}
else if (ratio === (0, basicOperations_1.roundRatio)(18 / 9)) {
result = '18 / 9';
}
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 18)) {
result = '9 / 18';
}
else if (ratio === (0, basicOperations_1.roundRatio)(23 / 16)) {
result = '23 / 16';
} // Ipad Air 4
else if (ratio === (0, basicOperations_1.roundRatio)(16 / 23)) {
result = '16 / 23';
} // Ipad Air 4
else if (ratio === (0, basicOperations_1.roundRatio)(19.5 / 9)) {
result = '19.5 / 9';
} // Iphone 11-12-XR
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 23)) {
result = '9 / 19.5';
} // Iphone 11-12-XR
else if (ratio === (0, basicOperations_1.roundRatio)(4 / 5)) {
result = '4 / 5';
}
else if (ratio === (0, basicOperations_1.roundRatio)(5 / 4)) {
result = '5 / 4';
}
else if (ratio === (0, basicOperations_1.roundRatio)(32 / 9)) {
result = '32 / 9';
}
else if (ratio === (0, basicOperations_1.roundRatio)(9 / 32)) {
result = '9 / 32';
}
return result;
}
}
exports.getAspectRatio = getAspectRatio;
//# sourceMappingURL=analytics2.js.map