Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/system_dialog.js
blob: b0c32d83cae5bca7093f8582418a3fc6c6834c17 (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
/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
'use strict';

/**
 * System app is made of a top-level `<div ="screen"></div>` DOM element
 * which contain all possible screens displayed by the app.
 * Multiple screens can be displayed at a time. We store the list of currently
 * visible screens into this DOM element class attribute.
 */
var SystemScreen = {
  screen: document.getElementById('screen'),

  show: function ss_show(screenName) {
    this.screen.classList.add(screenName);
  },

  hide: function ss_show(screenName) {
    this.screen.classList.remove(screenName);
  },

  isVisible: function ss_isVisible(screenName) {
    return this.screen.classList.contains(screenName);
  }
};

/**
 * System app displays various kind of dialogs.
 * A dialog is a system app 'screen' that has a high z-index and is used to be
 * displayed on top of other apps. But it doesn't display over the status bar,
 * nor the eventually displayed keyboard.
 *
 * `SystemDialog` except the dialog DOM Element `id`.
 * This DOM Element has to have a DOM attribute 'role' set to 'dialog'.
 *
 * It also supports a second `options` object with following attributes:
 *  `onHide`: function called when dialog is hidden, either when `hide()`
 *            method is called, or when dialog is automatically hidden on
 *            home button press
 */
function SystemDialog(id, options) {
  var overlay = document.getElementById('dialog-overlay');
  var dialog = document.getElementById(id);
  var screenName = 'dialog';

  // Listen to keyboard visibility changes and window resizing
  // in order to resize the dialog accordingly
  function updateHeight(keyboardHeight) {
    if (SystemScreen.isVisible(screenName)) {
      var height = window.innerHeight -
                   (keyboardHeight ? keyboardHeight : 0) -
                   StatusBar.height;
      overlay.style.height = height + 'px';
    }
  };
  function handleEvent(evt) {
    switch (evt.type) {
      case 'resize':
      case 'keyboardhide':
        updateHeight();
        break;
      case 'keyboardchange':
        updateHeight(evt.detail.height);
        break;
      case 'home':
      case 'holdhome':
        // Automatically hide the dialog on home button press
        if (SystemScreen.isVisible(screenName)) {
          hide(evt.type);
          // Prevent WindowManager to shift homescreen to the first page
          // when the dialog is on top of the homescreen
          var displayedApp = WindowManager.getDisplayedApp();
          var displayedAppFrame = WindowManager.getAppFrame(displayedApp);
          if (evt.type == 'home' &&
              displayedAppFrame.classList.contains('homescreen'))
            evt.stopImmediatePropagation();
        }
        break;
    }
  };
  window.addEventListener('resize', handleEvent);
  window.addEventListener('keyboardchange', handleEvent);
  window.addEventListener('keyboardhide', handleEvent);
  window.addEventListener('home', handleEvent);
  window.addEventListener('holdhome', handleEvent);

  function show() {
    dialog.hidden = false;
    dialog.classList.add(id);
    SystemScreen.show(screenName);
    updateHeight();
  }

  function hide(reason) {
    dialog.hidden = true;
    dialog.classList.remove(id);
    SystemScreen.hide(screenName);
    if (typeof(options.onHide) == 'function')
      options.onHide(reason);
  }

  function isVisible() {
    return SystemScreen.isVisible(screenName) &&
           overlay.classList.contains(id);
  }

  return {
    show: show,
    hide: hide,
    isVisible: isVisible
  };
}