1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { matchMediaQueryCondition } from './mediaQuery'; 17 18interface MediaQueryModule { 19 addListener: Function; 20 getDeviceType: Function; 21} 22 23interface MatchData { 24 matches: boolean; 25} 26 27class MediaQueryList { 28 private _matches: boolean; 29 private _condition: string; 30 private _onchange: any; 31 private _listeners: any[]; 32 33 constructor(condition: string) { 34 this._condition = condition; 35 this._onchange = null; 36 this._listeners = []; 37 } 38 39 get matches() { 40 return this._matches; 41 } 42 43 set matches(matches: boolean) { 44 this._matches = matches; 45 } 46 47 get condition() { 48 return this._condition; 49 } 50 51 get media() { 52 return this._condition; 53 } 54 55 get onchange() { 56 return this._onchange; 57 } 58 59 set onchange(onchange: any) { 60 this._onchange = onchange; 61 } 62 63 get listeners() { 64 return this._listeners; 65 } 66 67 public addListener(matchFunction: any) { 68 this._listeners.push(matchFunction); 69 } 70 71 public removeListener(matchFunction: any) { 72 const index: number = this._listeners.indexOf(matchFunction); 73 if (index > -1) { 74 this._listeners.splice(index, 1); 75 } 76 } 77} 78 79/** 80 * MediaQuery api. 81 */ 82export class MediaQueryApi { 83 private _module: MediaQueryModule; 84 private _mqlArray: MediaQueryList[]; 85 86 constructor(module: MediaQueryModule) { 87 this._module = module; 88 this._mqlArray = []; 89 this._module.addListener((data) => { 90 if (!this._mqlArray || this._mqlArray.length === 0) { 91 return; 92 } 93 data['device-type'] = data.deviceType; 94 data['aspect-ratio'] = data.aspectRatio; 95 data['device-width'] = data.deviceWidth; 96 data['device-height'] = data.deviceHeight; 97 data['round-screen'] = data.roundScreen; 98 data['dark-mode'] = data.darkMode; 99 for (let i = 0; i < this._mqlArray.length; i++) { 100 const mediaQueryList:MediaQueryList = this._mqlArray[i]; 101 const result: boolean = matchMediaQueryCondition(mediaQueryList.condition, data, true); 102 103 mediaQueryList.matches = result; 104 const matchData: MatchData = { matches: false }; 105 matchData.matches = result; 106 107 if (mediaQueryList.listeners && mediaQueryList.listeners.length !== 0) { 108 for (let i = 0; i < mediaQueryList.listeners.length; i++) { 109 const matchFunc = mediaQueryList.listeners[i]; 110 matchFunc(matchData); 111 } 112 } 113 114 if (typeof mediaQueryList.onchange === 'function') { 115 mediaQueryList.onchange(matchData); 116 } 117 } 118 }); 119 } 120 121 /** 122 * Creates a MediaQueryList object based on the query condition. 123 * @param {string} condition - Media query condition. 124 * @return {MediaQueryList} 125 */ 126 public matchMedia(condition: string): MediaQueryList { 127 const mediaquerylist: MediaQueryList = new MediaQueryList(condition); 128 this._mqlArray.push(mediaquerylist); 129 return mediaquerylist; 130 } 131} 132