1 // Copyright (c) 2012 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.
8 * @fileoverview A list of things, and a viewer for the currently selected
12 base.require('ui.list_view');
13 base.requireStylesheet('ui.list_and_associated_view');
14 base.exportTo('ui', function() {
19 var ListAndAssociatedView = ui.define('x-list-and-associated-view');
20 ListAndAssociatedView.prototype = {
21 __proto__: HTMLUnknownElement.prototype,
23 decorate: function() {
24 this.list_ = undefined;
25 this.listProperty_ = undefined;
26 this.view_ = undefined;
27 this.viewProperty_ = undefined;
28 this.listView_ = new ui.ListView();
29 this.listView_.addEventListener('selection-changed',
30 this.onSelectionChanged_.bind(this));
31 this.placeholder_ = document.createElement('div');
32 this.appendChild(this.listView_);
33 this.appendChild(this.placeholder_);
37 return this.listView_;
46 this.updateChildren_();
50 return this.listProperty_;
53 set listProperty(listProperty) {
54 this.listProperty_ = listProperty;
55 this.updateChildren_();
64 this.updateChildren_();
68 return this.viewProperty_;
71 set viewProperty(viewProperty) {
72 this.viewProperty_ = viewProperty;
73 this.updateChildren_();
76 updateChildren_: function() {
77 var complete = this.list_ &&
82 this.replaceChild(this.placeholder_,
87 for (var i = 0; i < this.list_.length; i++) {
89 if (i >= this.listView_.children.length) {
90 itemEl = document.createElement('div');
91 this.listView_.appendChild(itemEl);
93 itemEl = this.listView_.children[i];
95 itemEl.item = this.list_[i];
96 var getter = this.list_[i].__lookupGetter__(this.listProperty_);
98 itemEl.textContent = getter.call(this.list_[i]);
100 itemEl.textContent = this.list_[i][this.listProperty_];
103 if (this.children[1] == this.placeholder_) {
104 this.replaceChild(this.view_,
107 if (this.listView_.children.length &&
108 !this.listView_.selectedElement)
109 this.listView_.selectedElement = this.listView_.children[0];
112 onSelectionChanged_: function(e) {
113 var setter = this.view_.__lookupSetter__(this.viewProperty_);
115 var prop = this.viewProperty_;
116 setter = function(value) { this[prop] = value; }
118 if (this.listView_.selectedElement) {
119 setter.call(this.view_,
120 this.listView_.selectedElement.item);
122 setter.call(this.view_,
129 ListAndAssociatedView: ListAndAssociatedView