• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      for (let i = 0; i < this._mqlArray.length; i++) {
94        const mediaQueryList:MediaQueryList = this._mqlArray[i];
95        const result: boolean = matchMediaQueryCondition(mediaQueryList.condition, data, true);
96
97        mediaQueryList.matches = result;
98        const matchData: MatchData = { matches: false };
99        matchData.matches = result;
100
101        if (mediaQueryList.listeners && mediaQueryList.listeners.length !== 0) {
102          for (let i = 0; i < mediaQueryList.listeners.length; i++) {
103            const matchFunc = mediaQueryList.listeners[i];
104            matchFunc(matchData);
105          }
106        }
107
108        if (typeof mediaQueryList.onchange === 'function') {
109          mediaQueryList.onchange(matchData);
110        }
111      }
112    });
113  }
114
115  /**
116   * Creates a MediaQueryList object based on the query condition.
117   * @param {string} condition - Media query condition.
118   * @return {MediaQueryList}
119   */
120  public matchMedia(condition: string): MediaQueryList {
121    const mediaquerylist: MediaQueryList = new MediaQueryList(condition);
122    this._mqlArray.push(mediaquerylist);
123    return mediaquerylist;
124  }
125}
126