|
1 | 1 | import { Logger, LogLevel, ConsoleListener } from "@pnp/logging";
|
2 | 2 | import { sp } from "@pnp/sp";
|
3 | 3 | import * as WebParts from "./@WebParts";
|
4 |
| - |
| 4 | +import { GetSettings } from "prosjektportalen/lib/Settings"; |
5 | 5 | /** If the script was loaded using SP.SOD, we'll set the SOD to loaded */
|
6 | 6 | if (window["_v_dictSod"]) {
|
7 | 7 | window["_v_dictSod"]["pp.program.js"].loaded = true;
|
8 | 8 | }
|
9 | 9 |
|
10 |
| -/** Set up pnp logging */ |
| 10 | +namespace PP.Program { |
| 11 | + /** |
| 12 | + * Sets up PnP logging |
| 13 | + * |
| 14 | + * @param {string} logLevelStr Log level string |
| 15 | + */ |
| 16 | + async function initLogging(logLevelStr: string) { |
| 17 | + let logLevel = LogLevel.Off; |
| 18 | + switch (logLevelStr.toLowerCase()) { |
| 19 | + case "info": { |
| 20 | + logLevel = LogLevel.Info; |
| 21 | + } |
| 22 | + break; |
| 23 | + case "warning": { |
| 24 | + logLevel = LogLevel.Warning; |
| 25 | + } |
| 26 | + break; |
| 27 | + case "error": { |
| 28 | + logLevel = LogLevel.Error; |
| 29 | + } |
| 30 | + break; |
| 31 | + } |
| 32 | + Logger.activeLogLevel = logLevel; |
| 33 | + Logger.subscribe(new ConsoleListener()); |
| 34 | + } |
11 | 35 |
|
12 |
| -Logger.activeLogLevel = LogLevel.Info; |
13 |
| -Logger.subscribe(new ConsoleListener()); |
| 36 | + /** |
| 37 | + * Sets up PnP settings |
| 38 | + * |
| 39 | + * @param {string} defaultCachingTimeoutSecondsStr Default caching timeout (seconds) |
| 40 | + */ |
| 41 | + function initPnp(defaultCachingTimeoutSecondsStr: string) { |
| 42 | + let defaultCachingTimeoutSeconds = 30; |
| 43 | + if (defaultCachingTimeoutSecondsStr) { |
| 44 | + defaultCachingTimeoutSeconds = parseInt(defaultCachingTimeoutSecondsStr, 10); |
| 45 | + } |
| 46 | + sp.setup({ |
| 47 | + sp: { headers: { Accept: "application/json; odata=verbose" } }, |
| 48 | + defaultCachingStore: "session", |
| 49 | + defaultCachingTimeoutSeconds, |
| 50 | + }); |
| 51 | + } |
14 | 52 |
|
15 |
| -/** PnP setup */ |
| 53 | + export async function initialize() { |
| 54 | + const settings = await GetSettings(); |
| 55 | + initLogging(settings.LOG_LEVEL); |
| 56 | + initPnp(settings.DEFAULT_CACHING_TIMEOUT_SECONDS); |
16 | 57 |
|
17 |
| -sp.setup({ |
18 |
| - sp: { headers: { Accept: "application/json; odata=verbose" } }, |
19 |
| - defaultCachingStore: "session", |
20 |
| -}); |
| 58 | + SP.SOD.executeOrDelayUntilScriptLoaded(() => { |
| 59 | + WebParts.Render(); |
| 60 | + }, "sp.js"); |
| 61 | + } |
| 62 | +} |
21 | 63 |
|
22 |
| -ExecuteOrDelayUntilBodyLoaded(WebParts.Render); |
| 64 | +ExecuteOrDelayUntilBodyLoaded(PP.Program.initialize); |
0 commit comments