All files / src helper.sysinfo.js

100% Statements 54/54
87.04% Branches 47/54
100% Functions 3/3
100% Lines 52/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237                                                                                                                                                    3x               23x   23x   23x   23x   23x   23x 23x   18x   5x   2x     23x 23x     23x 21x   15x 15x 15x   15x             21x 21x   15x 15x 15x 15x   15x                   2x     23x         23x   23x   8x     23x 23x   23x 23x   23x       23x                                                             35x 35x   1x   35x   1x     35x   4x   35x   1x   34x   1x   35x               69x   4x       65x            
/*
 * Part of Pleiar.no - a collection of tools for nurses
 *
 * Copyright (C) Fagforbundet 2020
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
// @flow
import type { appInstallModes } from './mobile-appinstall';
import auth from './auth';
import device from './helper.device';
import PleiarSearcher from './searcher';
import MedSearcher from './med-synonym-searcher';
import featureDetection from './helper.feature-detection';
import { appInstall } from './mobile-appinstall';
import { quizDataManager } from "./quiz-manager";
 
export type SourceInfoType = {|
    type: "script" | "link",
    src: string,
    rel?: string
|};
export type SearcherState = "lastet" | "ikke lastet";
export type FeatureStatus = "ikke tilgjengelig" | "tilgjengelig, i bruk" | "tilgjengelig, ikke i bruk" | "tilgjengelig";
export type AppInstallSystemInfo = {|
    mode: appInstallModes,
    canInstall: boolean,
|};
export type SystemInfoDimensions = {|
    w: number,
    h: number
|};
export type SystemInfoDevice = {|
    isAppMode: boolean,
    isTouchScreen: boolean,
|};
export type SystemInfoType = {|
    featuresDetected: Array<string>,
    sources: Array<SourceInfoType>,
    auth: boolean,
    platform: string,
    UA: string,
    bodyClass: string | null,
    appInstall: AppInstallSystemInfo,
    dimensions: SystemInfoDimensions,
    quizDataManagerStatus: SearcherState,
    medSearcherStatus: SearcherState,
    pleiarSearcherStatus: SearcherState,
    serviceWorkerStatus: FeatureStatus,
    device: SystemInfoDevice,
    appVersion: string,
    dataVersion: string,
|};
// Let flow know about our GIT_REVISION globals (from webpack)
declare var GIT_REVISION:string;
declare var GIT_REVISION_FULL:string;
declare var GIT_REVISION_DATA:string;
 
/**
 * A collection of helper functions that can be used to identify the device
 * or mode that we're running in
 */
const SysInfo = {
    /**
     * Retrieves all of the system information in a SystemInfoType object.
     * This is all of the information that is NOT dependant on CSS.
     */
    getSystemInfo (): SystemInfoType
    {
        // The user agent
        const UA = navigator.userAgent;
        // Load state for MedSearcher
        const medSearcherStatus: SearcherState = SysInfo.getSearcherState(MedSearcher);
        // Load state for quizDataManager
        const quizDataManagerStatus: SearcherState = SysInfo.getSearcherState(quizDataManager);
        // Load state for PleiarSearcher
        const pleiarSearcherStatus: SearcherState = SysInfo.getSearcherState(PleiarSearcher);
        // List of features
        const featuresDetected = SysInfo.featureDetection();
        // State of the service worker
        let serviceWorkerStatus: FeatureStatus = "ikke tilgjengelig";
        if(navigator.serviceWorker && navigator.serviceWorker.controller !== null)
        {
            serviceWorkerStatus = "tilgjengelig, i bruk";
        }
        else if(navigator.serviceWorker)
        {
            serviceWorkerStatus = "tilgjengelig, ikke i bruk";
        }
        // An array of sources we have loaded
        const sources: Array<SourceInfoType> = [];
        try
        {
            // Script sources
            const scripts = document.getElementsByTagName('script');
            for(let sourceI: number = 0; sourceI < scripts.length; sourceI++)
            {
                const source = scripts[sourceI];
                const src = source.getAttribute('src');
                Eif(src !== undefined && src !== null)
                {
                    sources.push({
                        type: "script",
                        src
                    });
                }
            }
            // Link (preload or stylesheet) sources
            const links = document.getElementsByTagName('link');
            for(let linkI: number = 0; linkI < links.length; linkI++)
            {
                const source = links[linkI];
                const href = source.getAttribute('href');
                const rel = source.getAttribute('rel');
                Eif(href !== undefined && href !== null && (rel === "stylesheet" || rel === "prefetch" || rel === "preload"))
                {
                    sources.push({
                        type: "link",
                        src: href,
                        rel,
                    });
                }
            }
        }
        catch(e)
        {
            console && console.log && console.log('(klarte ikke hente kildeliste: '+e+')');
        }
        // Screen dimensions
        const dimensions: SystemInfoDimensions = {
            w: Math.max( (document.documentElement ? document.documentElement.clientWidth : window.innerWidth) || 0),
            h: Math.max( (document.documentElement ? document.documentElement.clientHeight : window.innerHeight ) || 0),
        };
        // The plattform as specified by the navigator
        let platform: string = navigator.platform;
        // Android identifies as "Linux" only, add Android to it
        if (navigator.userAgent.indexOf('Android') !== -1)
        {
            platform = platform.replace(/^Linux/,'Android (Linux)');
        }
        // The "class" tag of the body, if any.
        let bodyClass: string | null = null;
        Eif(document.body !== null)
        {
            const BC = document.body.getAttribute('class');
            Eif(BC !== undefined)
            {
                bodyClass = BC;
            }
        }
        // The information object
        return {
            UA,
            sources,
            dimensions,
            bodyClass,
            auth: auth.isAuthenticated(),
            platform,
            quizDataManagerStatus,
            medSearcherStatus,
            pleiarSearcherStatus,
            serviceWorkerStatus,
            appVersion: GIT_REVISION_FULL,
            dataVersion: GIT_REVISION_DATA,
            featuresDetected,
            device: {
                isAppMode: device.isAppMode(),
                isTouchScreen: device.isTouchScreen(),
            },
            appInstall: {
                mode: appInstall.mode(),
                canInstall: appInstall.canInstall()
            },
        };
    },
 
    /**
     * Detects various browser features and returns human-readable innformation
     * about which was found
     */
    featureDetection (): Array<string>
    {
        const featuresDetected: Array<string> = [];
        if(featureDetection.aSupportsRel('noopener'))
        {
            featuresDetected.push('a-rel-noopener');
        }
        if (window.navigator.storage && window.navigator.storage.persist && typeof(window.navigator.storage.persist) === 'function')
        {
            featuresDetected.push('storage.persist');
        }
        // $FlowIssue[prop-missing]: These are nonstandard or in-development, so flow doesn't know about them
        if(navigator.connection || navigator.mozConnection || navigator.webkitConnection)
        {
            featuresDetected.push('navigator.connection');
        }
        if(window.applicationCache && window.applicationCache.UNCACHED === window.applicationCache.status)
        {
            featuresDetected.push('appCache(unused)');
        }
        else if(window.applicationCache)
        {
            featuresDetected.push('appCache(inUse)');
        }
        return featuresDetected;
    },
 
    /**
     * Method that retrieves the load state of any object that inherits from {@link asyncLoaderRole}
     */
    getSearcherState(searcher: typeof MedSearcher | typeof PleiarSearcher | typeof quizDataManager): "lastet" | "ikke lastet"
    {
        if(searcher.hasInitialized())
        {
            return "lastet";
        }
        else
        {
            return "ikke lastet";
        }
    }
};
 
export default SysInfo;