summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/ntp/apps.js
blob: 64b490fdbc4b280828034309888ff571e3407f07 (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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

function getAppsCallback(data) {
  logEvent('recieved apps');
  var appsSection = $('apps-section');
  var appsSectionContent = $('apps-section-content');
  appsSectionContent.textContent = '';

  data.apps.forEach(function(app) {
    appsSectionContent.appendChild(apps.createElement(app));
  });

  appsSectionContent.appendChild(apps.createWebStoreElement());
}

var apps = {
  /**
   * @this {!HTMLAnchorElement}
   */
  handleClick_: function() {
    var launchType = '';
    var inputElements = document.querySelectorAll(
        '#apps-launch-control input');
    for (var i = 0, input; input = inputElements[i]; i++) {
      if (input.checked) {
        launchType = input.value;
        break;
      }
    }

    // TODO(arv): Handle zoom?
    var rect = this.getBoundingClientRect();
    var cs = getComputedStyle(this);
    var size = cs.backgroundSize.split(/\s+/);  // background-size has the
                                                // format '123px 456px'.
    var width = parseInt(size[0], 10);
    var height = parseInt(size[1], 10);
    // We are using background-position-x 50%.
    var left = rect.left + ((rect.width - width) >> 1);  // Integer divide by 2.
    var top = rect.top + parseInt(cs.backgroundPositionY, 10);

    chrome.send('launchApp', [this.id, launchType,
                              String(left), String(top),
                              String(width), String(height)]);
    return false;
  },

  createElement_: function(app) {
    var div = document.createElement('div');
    div.className = 'app';

    var front = div.appendChild(document.createElement('div'));
    front.className = 'front';

    var a = front.appendChild(document.createElement('a'));
    a.id = app['id'];
    a.xtitle = a.textContent = app['name'];
    a.href = app['launch_url'];

    return div;
  },

  createElement: function(app) {
    var div = this.createElement_(app);
    var front = div.firstChild;
    var a = front.firstChild;

    a.onclick = apps.handleClick_;
    a.style.backgroundImage = url(app['icon']);
    if (hashParams['app-id'] == app['id']) {
      div.setAttribute('new', 'new');
      // Delay changing the attribute a bit to let the page settle down a bit.
      setTimeout(function() {
        div.setAttribute('new', 'installed');
      }, 500);
    }

    var settingsButton = front.appendChild(document.createElement('button'));
    settingsButton.className = 'flip';
    settingsButton.title = localStrings.getString('appsettings');

    var back = div.appendChild(document.createElement('div'));
    back.className = 'back';

    var header = back.appendChild(document.createElement('h2'));
    header.textContent = app['name'];

    var optionsButton = back.appendChild(document.createElement('button'));
    optionsButton.textContent = localStrings.getString('appoptions');
    optionsButton.disabled = !app['options_url'];
    optionsButton.onclick = function() {
      window.location = app['options_url'];
    };

    var uninstallButton = back.appendChild(document.createElement('button'));
    uninstallButton.textContent = uninstallButton.xtitle =
        localStrings.getString('appuninstall');
    uninstallButton.onclick = function() {
      chrome.send('uninstallApp', [app['id']]);
    };

    var closeButton = back.appendChild(document.createElement('button'));
    closeButton.title = localStrings.getString('close');
    closeButton.className = 'flip';
    closeButton.onclick = settingsButton.onclick = function() {
      div.classList.toggle('config');
    };

    return div;
  },

  createWebStoreElement: function() {
    return this.createElement_({
      'id': 'web-store-entry',
      'name': localStrings.getString('web_store_title'),
      'launch_url': localStrings.getString('web_store_url')
    });
  }
};