• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 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
5cr.define('extensions', function() {
6  /** @const */ var List = cr.ui.List;
7  /** @const */ var ListItem = cr.ui.ListItem;
8  /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel;
9
10  /**
11   * Creates a list for showing kiosk apps.
12   * @constructor
13   * @extends {cr.ui.List}
14   */
15  var KioskAppList = cr.ui.define('list');
16
17  KioskAppList.prototype = {
18    __proto__: List.prototype,
19
20    /**
21     * True if auto launch feature can be configured.
22     * @type {?boolean}
23     */
24    autoLaunchEnabled_: false,
25
26    /** @override */
27    createItem: function(app) {
28      var item = new KioskAppListItem();
29      item.data = app;
30      item.autoLaunchEnabled = this.autoLaunchEnabled_;
31      return item;
32    },
33
34    /**
35     * Sets auto launch enabled flag.
36     * @param {boolean} enabled True if auto launch should be enabled.
37     */
38    setAutoLaunchEnabled: function(enabled) {
39      this.autoLaunchEnabled_ = enabled;
40    },
41
42    /**
43     * Loads the given list of apps.
44     * @param {!Array.<!Object>} apps An array of app info objects.
45     */
46    setApps: function(apps) {
47      this.dataModel = new ArrayDataModel(apps);
48    },
49
50    /**
51     * Updates the given app.
52     * @param {!Object} app An app info object.
53     */
54    updateApp: function(app) {
55      for (var i = 0; i < this.items.length; ++i) {
56        if (this.items[i].data.id == app.id) {
57          this.items[i].data = app;
58          break;
59        }
60      }
61    }
62  };
63
64  /**
65   * Creates a list item for a kiosk app.
66   * @constructor
67   * @extends {cr.ui.ListItem}
68   */
69  var KioskAppListItem = cr.ui.define(function() {
70    var el = $('kiosk-app-list-item-template').cloneNode(true);
71    el.removeAttribute('id');
72    el.hidden = false;
73    return el;
74  });
75
76  KioskAppListItem.prototype = {
77    __proto__: ListItem.prototype,
78
79    /**
80     * Data object to hold app info.
81     * @type {Object}
82     * @private
83     */
84    data_: null,
85
86    get data() {
87      assert(this.data_);
88      return this.data_;
89    },
90
91    set data(data) {
92      this.data_ = data;
93      this.redraw();
94    },
95
96    set autoLaunchEnabled(enabled) {
97      this.querySelector('.enable-auto-launch-button').hidden = !enabled;
98      this.querySelector('.disable-auto-launch-button').hidden = !enabled;
99    },
100
101    /**
102     * Getter for the icon element.
103     * @type {Element}
104     */
105    get icon() {
106      return this.querySelector('.kiosk-app-icon');
107    },
108
109    /**
110     * Getter for the name element.
111     * @type {Element}
112     */
113    get name() {
114      return this.querySelector('.kiosk-app-name');
115    },
116
117    /**
118     * Getter for the status text element.
119     * @type {Element}
120     */
121    get status() {
122      return this.querySelector('.kiosk-app-status');
123    },
124
125    /** @override */
126    decorate: function() {
127      ListItem.prototype.decorate.call(this);
128
129      var sendMessageWithId = function(msg) {
130        return function() {
131          chrome.send(msg, [this.data.id]);
132        }.bind(this);
133      }.bind(this);
134
135      this.querySelector('.enable-auto-launch-button').onclick =
136        sendMessageWithId('enableKioskAutoLaunch');
137      this.querySelector('.disable-auto-launch-button').onclick =
138        sendMessageWithId('disableKioskAutoLaunch');
139      this.querySelector('.row-delete-button').onclick =
140          sendMessageWithId('removeKioskApp');
141    },
142
143    /**
144     * Updates UI from app info data.
145     */
146    redraw: function() {
147      this.icon.classList.toggle('spinner', this.data.isLoading);
148      this.icon.style.backgroundImage = 'url(' + this.data.iconURL + ')';
149
150      this.name.textContent = this.data.name || this.data.id;
151      this.status.textContent = this.data.autoLaunch ?
152          loadTimeData.getString('autoLaunch') : '';
153
154      this.autoLaunch = this.data.autoLaunch;
155    }
156  };
157
158  /*
159   * True if the app represented by this item will auto launch.
160   * @type {boolean}
161   */
162  cr.defineProperty(KioskAppListItem, 'autoLaunch', cr.PropertyKind.BOOL_ATTR);
163
164  // Export
165  return {
166    KioskAppList: KioskAppList
167  };
168});
169