UNPKG

@exadel/esl

Version:

Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components

80 lines (79 loc) 3.77 kB
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; }; var ESLMediaShortcuts_1; import { isAndroid, isBlink, isEdgeHTML, isGecko, isMobile, isMobileIOS, isMobileSafari, isSafari, isTouchDevice, isWebkit } from '../../../esl-utils/environment/device-detector'; import { ExportNs } from '../../../esl-utils/environment/export-ns'; import { MediaQueryStaticCondition } from '../conditions/media-query-static'; import { ALL, NOT_ALL } from '../conditions/media-query-const'; import { MediaQueryCondition } from '../conditions/media-query-condition'; // Shortcut cannot start with a digit and cannot contain special characters const SHORTCUT_REGEXP = /^[a-z][a-z0-9-_]*$/i; // Global shortcuts store key const SHORTCUTS_STORE = Symbol.for('__esl_media_shortcuts'); /** * Static shortcuts' preprocessor. Used to store device related shortcuts. * @author Alexey Stsefanovich (ala'n) * * @implements IMediaQueryPreprocessor statically */ let ESLMediaShortcuts = ESLMediaShortcuts_1 = class ESLMediaShortcuts { /** Returns shortcuts map, ensures there is a single instance */ static get shortcuts() { if (!window[SHORTCUTS_STORE]) { window[SHORTCUTS_STORE] = new Map(); } return window[SHORTCUTS_STORE]; } /** Resolve shortcut by name. If not found, creates new instance */ static resolve(name) { const { shortcuts } = this; name = name.toLowerCase().trim(); if (!shortcuts.has(name)) shortcuts.set(name, new MediaQueryStaticCondition(name)); return shortcuts.get(name); } /** * Add mapping * @param shortcut - term to find in query * @param value - media query string or boolean result (that represents `all` or `not all` conditions) */ static set(shortcut, value) { if (!value) return ESLMediaShortcuts_1.set(shortcut, NOT_ALL); if (typeof value === 'boolean') return ESLMediaShortcuts_1.set(shortcut, value ? ALL : NOT_ALL); if (typeof value === 'string') return ESLMediaShortcuts_1.set(shortcut, new MediaQueryCondition(value)); if (!SHORTCUT_REGEXP.test(shortcut)) throw new Error(`[ESL] Invalid shortcut name: "${shortcut}". Expected pattern: ${SHORTCUT_REGEXP}`); ESLMediaShortcuts_1.resolve(shortcut).condition = value.optimize(); } /** Replaces shortcut to registered result */ static process(match) { if (!SHORTCUT_REGEXP.test(match)) return NOT_ALL; return this.resolve(match); } }; ESLMediaShortcuts = ESLMediaShortcuts_1 = __decorate([ ExportNs('MediaShortcuts') ], ESLMediaShortcuts); export { ESLMediaShortcuts }; // Touch check ESLMediaShortcuts.set('touch', isTouchDevice); // Basic device type shortcuts ESLMediaShortcuts.set('mobile', isMobile); ESLMediaShortcuts.set('desktop', !isMobile); ESLMediaShortcuts.set('android', isAndroid); ESLMediaShortcuts.set('ios', isMobileIOS); // Basic browser shortcuts ESLMediaShortcuts.set('edge', isEdgeHTML); ESLMediaShortcuts.set('gecko', isGecko); ESLMediaShortcuts.set('webkit', isWebkit); ESLMediaShortcuts.set('blink', isBlink); ESLMediaShortcuts.set('safari', isSafari); ESLMediaShortcuts.set('safari-ios', isMobileSafari);