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