3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/base/ui.html">
8 <link rel="import" href="/base/ui/list_view.html">
9 <link rel="stylesheet" href="/base/ui/list_and_associated_view.css">
14 * @fileoverview A list of things, and a viewer for the currently selected
17 tv.exportTo('tv.ui', function() {
22 var ListAndAssociatedView = tv.ui.define('x-list-and-associated-view');
23 ListAndAssociatedView.prototype = {
24 __proto__: HTMLUnknownElement.prototype,
26 decorate: function() {
27 this.list_ = undefined;
28 this.listProperty_ = undefined;
29 this.view_ = undefined;
30 this.viewProperty_ = undefined;
31 this.listView_ = new tv.ui.ListView();
32 this.listView_.addEventListener('selection-changed',
33 this.onSelectionChanged_.bind(this));
34 this.placeholder_ = document.createElement('div');
35 this.appendChild(this.listView_);
36 this.appendChild(this.placeholder_);
40 return this.listView_;
49 this.updateChildren_();
53 return this.listProperty_;
56 set listProperty(listProperty) {
57 this.listProperty_ = listProperty;
58 this.updateChildren_();
67 this.updateChildren_();
71 return this.viewProperty_;
74 set viewProperty(viewProperty) {
75 this.viewProperty_ = viewProperty;
76 this.updateChildren_();
79 updateChildren_: function() {
80 var complete = this.list_ &&
85 this.replaceChild(this.placeholder_,
90 for (var i = 0; i < this.list_.length; i++) {
92 if (i >= this.listView_.children.length) {
93 itemEl = document.createElement('div');
94 this.listView_.appendChild(itemEl);
96 itemEl = this.listView_.children[i];
98 itemEl.item = this.list_[i];
99 var getter = this.list_[i].__lookupGetter__(this.listProperty_);
101 itemEl.textContent = getter.call(this.list_[i]);
103 itemEl.textContent = this.list_[i][this.listProperty_];
106 if (this.children[1] == this.placeholder_) {
107 this.replaceChild(this.view_,
110 if (this.listView_.children.length &&
111 !this.listView_.selectedElement)
112 this.listView_.selectedElement = this.listView_.children[0];
115 onSelectionChanged_: function(e) {
116 var setter = this.view_.__lookupSetter__(this.viewProperty_);
118 var prop = this.viewProperty_;
119 setter = function(value) { this[prop] = value; }
121 if (this.listView_.selectedElement) {
122 setter.call(this.view_,
123 this.listView_.selectedElement.item);
125 setter.call(this.view_,
132 ListAndAssociatedView: ListAndAssociatedView