apps.js revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5function getAppsCallback(data) {
6  logEvent('recieved apps');
7  var appsSection = $('apps-section');
8  var debugSection = $('debug');
9  appsSection.textContent = '';
10
11  data.apps.forEach(function(app) {
12    appsSection.appendChild(apps.createElement(app));
13  });
14
15  // TODO(aa): Figure out what to do with the debug mode when we turn apps on
16  // for everyone.
17  if (appsSection.hasChildNodes()) {
18    appsSection.classList.remove('disabled');
19    if (data.showDebugLink) {
20      debugSection.classList.remove('disabled');
21    }
22
23    appsSection.appendChild(apps.createWebStoreElement());
24  } else {
25    appsSection.classList.add('disabled');
26    debugSection.classList.add('disabled');
27  }
28}
29
30var apps = {
31  /**
32   * @this {!HTMLAnchorElement}
33   */
34  handleClick_: function() {
35    var launchType = '';
36    var inputElements = document.querySelectorAll(
37        '#apps-launch-control input');
38    for (var i = 0, input; input = inputElements[i]; i++) {
39      if (input.checked) {
40        launchType = input.value;
41        break;
42      }
43    }
44
45    // TODO(arv): Handle zoom?
46    var rect = this.getBoundingClientRect();
47    var cs = getComputedStyle(this);
48    var size = cs.backgroundSize.split(/\s+/);  // background-size has the
49                                                // format '123px 456px'.
50    var width = parseInt(size[0], 10);
51    var height = parseInt(size[1], 10);
52    // We are using background-position-x 50%.
53    var left = rect.left + ((rect.width - width) >> 1);  // Integer divide by 2.
54    var top = rect.top + parseInt(cs.backgroundPositionY, 10);
55
56    chrome.send('launchApp', [this.id, launchType,
57                              String(left), String(top),
58                              String(width), String(height)]);
59    return false;
60  },
61
62  createElement_: function(app) {
63    var div = document.createElement('div');
64    div.className = 'app';
65
66    var front = div.appendChild(document.createElement('div'));
67    front.className = 'front';
68
69    var a = front.appendChild(document.createElement('a'));
70    a.id = app['id'];
71    a.xtitle = a.textContent = app['name'];
72    a.href = app['launch_url'];
73
74    return div;
75  },
76
77  createElement: function(app) {
78    var div = this.createElement_(app);
79    var front = div.firstChild;
80    var a = front.firstChild;
81
82    a.onclick = apps.handleClick_;
83    a.style.backgroundImage = url(app['icon']);
84    if (hashParams['app-id'] == app['id']) {
85      div.setAttribute('new', 'new');
86      // Delay changing the attribute a bit to let the page settle down a bit.
87      setTimeout(function() {
88        div.setAttribute('new', 'installed');
89      }, 500);
90    }
91
92    var settingsButton = front.appendChild(document.createElement('button'));
93    settingsButton.className = 'flip';
94    settingsButton.title = localStrings.getString('appsettings');
95
96    var back = div.appendChild(document.createElement('div'));
97    back.className = 'back';
98
99    var header = back.appendChild(document.createElement('h2'));
100    header.textContent = app['name'];
101
102    var optionsButton = back.appendChild(document.createElement('button'));
103    optionsButton.textContent = localStrings.getString('appoptions');
104    optionsButton.disabled = !app['options_url'];
105    optionsButton.onclick = function() {
106      window.location = app['options_url'];
107    };
108
109    var uninstallButton = back.appendChild(document.createElement('button'));
110    uninstallButton.textContent = uninstallButton.xtitle =
111        localStrings.getString('appuninstall');
112    uninstallButton.onclick = function() {
113      chrome.send('uninstallApp', [app['id']]);
114    };
115
116    var closeButton = back.appendChild(document.createElement('button'));
117    closeButton.title = localStrings.getString('close');
118    closeButton.className = 'flip';
119    closeButton.onclick = settingsButton.onclick = function() {
120      div.classList.toggle('config');
121    };
122
123    return div;
124  },
125
126  createWebStoreElement: function() {
127    return this.createElement_({
128      'id': 'web-store-entry',
129      'name': localStrings.getString('web_store_title'),
130      'launch_url': localStrings.getString('web_store_url')
131    });
132  }
133};
134