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

(function() {

  'use strict';

  var host = document.location.host;
  var domain = host.replace(/(^[\w\d]+\.)?([\w\d]+\.[a-z]+)/, '$2');
  var protocol = document.location.protocol + '//';
  var origin = protocol + 'costcontrol.' + domain;

  var widgetContainer = document.getElementById('cost-control-widget');

  var widgetFrame;
  function _ensureWidget() {
    if (!Applications.ready)
      return;

    // Check widget is there
    widgetFrame = widgetContainer.querySelector('iframe');
    if (widgetFrame && !widgetFrame.dataset.killed)
      return;

    // Create the widget
    if (!widgetFrame) {
      widgetFrame = document.createElement('iframe');
      widgetFrame.addEventListener('mozbrowsererror',
        function ccdriver_onError(e) {
          e.target.dataset.killed = true;
        }
      );
    }

    widgetFrame.dataset.frameType = 'widget';
    widgetFrame.dataset.frameOrigin = origin;
    delete widgetFrame.dataset.killed;

    widgetFrame.setAttribute('mozbrowser', true);
    widgetFrame.setAttribute('remote', 'true');
    widgetFrame.setAttribute('mozapp', origin + '/manifest.webapp');

    widgetFrame.src = origin + '/widget.html';
    widgetContainer.appendChild(widgetFrame);

    _adjustWidgetPosition();
  }

  function _showWidget() {
    _ensureWidget();
    widgetFrame.setVisible(true);
  }

  function _hideWidget() {
    if (widgetFrame) {
      widgetFrame.setVisible(false);
    }
  }

  function _adjustWidgetPosition() {
    // TODO: Remove this when weird bug #809031 (Bugzilla) is solved
    // See cost_control.css as well to remove the last rule
    var offsetY = document.getElementById('notification-bar').clientHeight;
    offsetY +=
      document.getElementById('notifications-container').clientHeight;
    widgetFrame.style.transform = 'translate(0, ' + offsetY + 'px)';
  }

  // Listen to utilitytray show
  window.addEventListener('utilitytrayshow', _showWidget);
  window.addEventListener('utilitytrayhide', _hideWidget);

  window.addEventListener('applicationready', function _onReady() {
    asyncStorage.getItem('ftu.enabled', function _onValue(enabled) {
      if (enabled !== false) {
        window.addEventListener('ftudone', function ftudone(e) {
          window.removeEventListener('ftudone', ftudone);
          _ensureWidget();
          widgetFrame.setVisible(false);
        });
      } else {
        _ensureWidget();
        widgetFrame.setVisible(false);
      }
    });
  });

  window.addEventListener('resize', _adjustWidgetPosition);
}());