Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Client.ts « src - github.com/jsxc/jsxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 43be17bee9031c8cc017196a9bfee747f7524661 (plain)
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
import { IPlugin } from './plugin/AbstractPlugin';
import Storage from './Storage';
import { NoticeManager } from './NoticeManager';
import PluginRepository from './plugin/PluginRepository';
import Log from './util/Log';
import Options from './Options';
import PresenceController from './PresenceController';
import PageVisibility from './PageVisibility';
import ChatWindowList from './ui/ChatWindowList';
import AccountManager from './AccountManager';
import Translation from '@util/Translation';
import Migration from './Migration';

export default class Client {
   private static storage: Storage;

   private static noticeManager: NoticeManager;

   private static presenceController: PresenceController;

   private static accountManager: AccountManager;

   private static initialized = false;

   private static options: Options;

   public static init(options?): number {
      if (Client.initialized) {
         Log.warn('JSXC was already initialized');

         return;
      }

      Client.initialized = true;

      if (typeof options === 'object' && options !== null) {
         Options.overwriteDefaults(options);
      }

      Translation.initialize();
      PageVisibility.init();

      let storage = Client.getStorage();

      Client.accountManager = new AccountManager(storage);
      Client.presenceController = new PresenceController(storage, () => Client.accountManager.getAccounts());

      Client.watchFileDrag();

      Migration.run(Client.getVersion(), storage);

      return Options.getDefault('automaticallyRestoreAccounts') ? Client.accountManager.restoreAccounts() : 0;
   }

   public static getVersion(): string {
      return __VERSION__;
   }

   public static addPlugin(Plugin: IPlugin) {
      try {
         PluginRepository.add(Plugin);
      } catch (err) {
         Log.warn('Error while adding Plugin: ' + err);
      }
   }

   public static hasTabFocus() {
      let hasFocus = true;

      if (typeof document.hasFocus === 'function') {
         hasFocus = document.hasFocus();
      }

      return hasFocus;
   }

   public static isVisible() {
      return PageVisibility.isVisible();
   }

   public static isExtraSmallDevice(): boolean {
      return $(window).width() < 500;
   }

   public static isDebugMode(): boolean {
      return Client.getStorage().getItem('debug') === true;
   }

   public static getStorage(): Storage {
      if (!Client.storage) {
         Client.storage = new Storage();
      }

      return Client.storage;
   }

   public static getAccountManager(): AccountManager {
      return Client.accountManager;
   }

   public static getNoticeManager(): NoticeManager {
      if (!Client.noticeManager) {
         Client.noticeManager = new NoticeManager(Client.getStorage());
      }

      return Client.noticeManager;
   }

   public static getPresenceController(): PresenceController {
      return Client.presenceController;
   }

   public static getChatWindowList(): ChatWindowList {
      return ChatWindowList.get();
   }

   public static getOptions(): Options {
      if (!Client.options) {
         Client.options = new Options(Client.getStorage());
      }

      return Client.options;
   }

   public static getOption<IOption = any>(key: string, defaultValue?: IOption): IOption {
      let value = Client.getOptions().get(key);

      return <IOption>(typeof value !== 'undefined' ? value : defaultValue);
   }

   public static setOption(key: string, value) {
      Client.getOptions().set(key, value);
   }

   private static watchFileDrag() {
      let enterCounter = 0;

      $(document).on('dragenter', ev => {
         enterCounter++;

         if (enterCounter === 1) {
            $('.jsxc-droppable').addClass('jsxc-dragactive');
         }
      });

      $(document).on('dragleave', ev => {
         enterCounter--;

         if (enterCounter === 0) {
            $('.jsxc-droppable').removeClass('jsxc-dragactive');
         }
      });

      $(document).on('dragover', ev => {
         ev.preventDefault();

         (<any>ev.originalEvent).dataTransfer.dropEffect = 'copy';
      });

      $(document).on('drop', () => {
         enterCounter = 0;

         $('.jsxc-droppable').removeClass('jsxc-dragactive jsxc-dragover');
      });
   }

   public static isTrustedDomain(url: URL): boolean {
      let trustedDomains = Client.getOption<string[]>('trustedDomains', []);

      return (
         trustedDomains.filter(domain => {
            let result = url.hostname === domain;

            if (!result && domain.indexOf('*.') > -1) {
               let wildcardtestdomain = domain.substring(domain.lastIndexOf('*.') + 2);
               result = url.hostname.endsWith(wildcardtestdomain);
            }

            return result;
         }).length > 0
      );
   }
}