refactor: rename isBrowser to IS_BROWSER + use only it for detection

This commit is contained in:
Roman Hotsiy 2018-03-26 18:14:38 +03:00
parent 040bc239a2
commit 33eaf77dc5
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
7 changed files with 18 additions and 17 deletions

View File

@ -6,9 +6,10 @@ import { RedocNormalizedOptions, RedocRawOptions } from '../../services/RedocNor
import styled, { media, withProps } from '../../styled-components'; import styled, { media, withProps } from '../../styled-components';
import { OptionsContext } from '../OptionsProvider'; import { OptionsContext } from '../OptionsProvider';
import { AnimatedChevronButton } from './ChevronSvg'; import { AnimatedChevronButton } from './ChevronSvg';
import { IS_BROWSER } from '../../utils/index';
let Stickyfill; let Stickyfill;
if (typeof window !== 'undefined') { if (IS_BROWSER) {
Stickyfill = require('stickyfill').default; Stickyfill = require('stickyfill').default;
} }

View File

@ -1,6 +1,6 @@
import { bind, debounce } from 'decko'; import { bind, debounce } from 'decko';
import { EventEmitter } from 'eventemitter3'; import { EventEmitter } from 'eventemitter3';
import { isBrowser } from '../utils/'; import { IS_BROWSER } from '../utils/';
const EVENT = 'hashchange'; const EVENT = 'hashchange';
@ -18,7 +18,7 @@ class IntHistoryService {
} }
get hash(): string { get hash(): string {
return isBrowser ? window.location.hash : ''; return IS_BROWSER ? window.location.hash : '';
} }
subscribe(cb): () => void { subscribe(cb): () => void {
@ -35,13 +35,13 @@ class IntHistoryService {
}; };
bind() { bind() {
if (isBrowser) { if (IS_BROWSER) {
window.addEventListener('hashchange', this.emit, false); window.addEventListener('hashchange', this.emit, false);
} }
} }
dispose() { dispose() {
if (isBrowser) { if (IS_BROWSER) {
window.removeEventListener('hashchange', this.emit); window.removeEventListener('hashchange', this.emit);
} }
this.causedHashChange = false; this.causedHashChange = false;
@ -54,13 +54,13 @@ class IntHistoryService {
return; return;
} }
if (rewriteHistory) { if (rewriteHistory) {
if (isBrowser) { if (IS_BROWSER) {
window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash); window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash);
} }
return; return;
} }
this.causedHashChange = true; this.causedHashChange = true;
if (isBrowser) { if (IS_BROWSER) {
window.location.hash = hash; window.location.hash = hash;
} }
} }

View File

@ -3,7 +3,7 @@ import { resolve as urlResolve } from 'url';
import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types'; import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';
import { appendToMdHeading, isBrowser } from '../utils/'; import { appendToMdHeading, IS_BROWSER } from '../utils/';
import { JsonPointer } from '../utils/JsonPointer'; import { JsonPointer } from '../utils/JsonPointer';
import { isNamedDefinition } from '../utils/openapi'; import { isNamedDefinition } from '../utils/openapi';
import { buildComponentComment, COMPONENT_REGEXP } from './MarkdownRenderer'; import { buildComponentComment, COMPONENT_REGEXP } from './MarkdownRenderer';
@ -54,7 +54,7 @@ export class OpenAPIParser {
this.spec = spec; this.spec = spec;
const href = isBrowser ? window.location.href : ''; const href = IS_BROWSER ? window.location.href : '';
if (typeof specUrl === 'string') { if (typeof specUrl === 'string') {
this.specUrl = urlResolve(href, specUrl); this.specUrl = urlResolve(href, specUrl);
} else { } else {

View File

@ -1,7 +1,7 @@
import { bind } from 'decko'; import { bind } from 'decko';
import * as EventEmitter from 'eventemitter3'; import * as EventEmitter from 'eventemitter3';
import { isBrowser, querySelector, Throttle } from '../utils'; import { IS_BROWSER, querySelector, Throttle } from '../utils';
import { RedocNormalizedOptions } from './RedocNormalizedOptions'; import { RedocNormalizedOptions } from './RedocNormalizedOptions';
const EVENT = 'scroll'; const EVENT = 'scroll';
@ -11,7 +11,7 @@ export class ScrollService {
private _emiter: EventEmitter; private _emiter: EventEmitter;
private _prevOffsetY: number = 0; private _prevOffsetY: number = 0;
constructor(private options: RedocNormalizedOptions) { constructor(private options: RedocNormalizedOptions) {
this._scrollParent = isBrowser ? window : undefined; this._scrollParent = IS_BROWSER ? window : undefined;
this._emiter = new EventEmitter(); this._emiter = new EventEmitter();
this.bind(); this.bind();
} }

View File

@ -1,11 +1,11 @@
import { IMenuItem } from './MenuStore'; import { IMenuItem } from './MenuStore';
import { OperationModel } from './models'; import { OperationModel } from './models';
import { IS_BROWSER } from '../utils/';
let worker; let worker;
if (typeof URL !== 'undefined') { if (IS_BROWSER) {
try { try {
// tslint:disable-next-line
worker = require('workerize-loader?inline&fallback=false!./SearchWorker.worker'); worker = require('workerize-loader?inline&fallback=false!./SearchWorker.worker');
} catch (e) { } catch (e) {
worker = require('./SearchWorker.worker').default; worker = require('./SearchWorker.worker').default;

View File

@ -1,5 +1,5 @@
import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types'; import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types';
import { isBrowser } from '../../utils/'; import { IS_BROWSER } from '../../utils/';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
@ -25,12 +25,12 @@ export class ApiInfoModel implements OpenAPIInfo {
return this.parser.specUrl; return this.parser.specUrl;
} }
if (isBrowser && window.Blob && window.URL) { if (IS_BROWSER && window.Blob && window.URL) {
const blob = new Blob([JSON.stringify(this.parser.spec, null, 2)], { const blob = new Blob([JSON.stringify(this.parser.spec, null, 2)], {
type: 'application/json', type: 'application/json',
}); });
return window.URL.createObjectURL(blob); return window.URL.createObjectURL(blob);
} else if (!isBrowser) { } else if (!IS_BROWSER) {
return ( return (
'data:application/octet-stream;base64,' + 'data:application/octet-stream;base64,' +
new Buffer(JSON.stringify(this.parser.spec, null, 2)).toString('base64') new Buffer(JSON.stringify(this.parser.spec, null, 2)).toString('base64')

View File

@ -1,4 +1,4 @@
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'; export const IS_BROWSER = typeof window !== 'undefined' && 'HTMLElement' in window;
export function querySelector(selector: string): Element | null { export function querySelector(selector: string): Element | null {
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {