OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * 'cr-settings-main' displays the selected settings page. | 7 * 'cr-settings-main' displays the selected settings page. |
8 * | 8 * |
9 * Example: | 9 * Example: |
10 * | 10 * |
11 * <cr-settings-main pages="{{pages}}" selectedPageId="{{selectedId}}"> | 11 * <cr-settings-main pages="[[pages]]" selected-page-id="{{selectedId}}"> |
12 * </cr-settings-main> | 12 * </cr-settings-main> |
13 * | 13 * |
14 * See cr-settings-drawer for example of use in 'core-drawer-panel'. | 14 * See cr-settings-drawer for example of use in 'paper-drawer-panel'. |
15 * | 15 * |
16 * @group Chrome Settings Elements | 16 * @group Chrome Settings Elements |
17 * @element cr-settings-main | 17 * @element cr-settings-main |
18 */ | 18 */ |
19 Polymer('cr-settings-main', { | 19 Polymer({ |
20 publish: { | 20 is: 'cr-settings-main', |
| 21 |
| 22 properties: { |
21 /** | 23 /** |
22 * Preferences state. | 24 * Preferences state. |
23 * | 25 * |
24 * @attribute prefs | 26 * @type {?CrSettingsPrefsElement} |
25 * @type CrSettingsPrefsElement | |
26 * @default null | |
27 */ | 27 */ |
28 prefs: null, | 28 prefs: { |
| 29 type: Object, |
| 30 notify: true, |
| 31 }, |
29 | 32 |
30 /** | 33 /** |
31 * Pages that may be shown. | 34 * Pages that may be shown. |
32 * | 35 * @type {?Array<!HTMLElement>} |
33 * @attribute pages | |
34 * @type Array<!Object> | |
35 * @default null | |
36 */ | 36 */ |
37 pages: null, | 37 pages: Array, |
| 38 |
| 39 /** |
| 40 * Currently selected page. |
| 41 * @type {?HTMLElement} |
| 42 */ |
| 43 selectedPage: { |
| 44 type: Object, |
| 45 notify: true, |
| 46 }, |
38 | 47 |
39 /** | 48 /** |
40 * ID of the currently selected page. | 49 * ID of the currently selected page. |
41 * | |
42 * @attribute selectedPageId | |
43 * @type string | |
44 * default '' | |
45 */ | 50 */ |
46 selectedPageId: '', | 51 selectedPageId: { |
| 52 type: String, |
| 53 notify: true, |
| 54 observe: 'selectedPageIdChanged_', |
| 55 }, |
47 }, | 56 }, |
48 | 57 |
49 /** @override */ | 58 /** @override */ |
50 created: function() { | |
51 this.pages = []; | |
52 }, | |
53 | |
54 /** @override */ | |
55 ready: function() { | 59 ready: function() { |
56 var observer = new MutationObserver(this.pageContainerUpdated_.bind(this)); | 60 var observer = new MutationObserver(this.pageContainerUpdated_.bind(this)); |
57 observer.observe(this.$.pageContainer, | 61 observer.observe(this.$.pageContainer, |
58 /** @type {MutationObserverInit} */ { | 62 /** @type {MutationObserverInit} */ { |
59 childList: true, | 63 childList: true, |
60 }); | 64 }); |
61 this.pageContainerUpdated_(); | 65 this.pageContainerUpdated_(); |
62 }, | 66 }, |
63 | 67 |
64 /** | 68 /** |
65 * Polymer changed event for selectedPageId. See note for onCoreSelect_ below. | 69 * Polymer changed event for selectedPageId. See note for onIronSelect_ below. |
| 70 * @private |
66 */ | 71 */ |
67 selectedPageIdChanged: function() { | 72 selectedPageIdChanged_: function() { |
68 this.$.pageContainer.selected = this.selectedPageId; | 73 this.$.pageContainer.selected = this.selectedPageId; |
69 }, | 74 }, |
70 | 75 |
71 /** | 76 /** |
72 * We observe $.pageContainer.on-core-select instead of using data binding | 77 * We observe $.pageContainer.on-iron-select instead of using data binding |
73 * for two reasons: | 78 * for two reasons: |
74 * 1) We need to exclude subpages | 79 * 1) We need to exclude subpages |
75 * 2) There is a bug with data binding or our useage of it here causing | 80 * 2) There is a bug with data binding or our useage of it here causing |
76 * this.selectedPage to get set to the index of $.pageContainer instead of | 81 * this.selectedPage to get set to the index of $.pageContainer instead of |
77 * the valueattr identifier (PAGE_ID). TODO(stevenjb/jlklien): Investigate | 82 * the valueattr identifier (PAGE_ID). TODO(stevenjb/jlklien): Investigate |
78 * fixing this and using filters once we switch to Polymer 0.8. | 83 * fixing this and using filters once we switch to Polymer 0.8. |
79 * @private | 84 * @private |
80 */ | 85 */ |
81 onCoreSelect_: function(event) { | 86 onIronSelect_: function(event) { |
82 if (event.target != this.$.pageContainer || !event.detail.isSelected || | 87 if (event.target != this.$.pageContainer || !event.detail.isSelected || |
83 event.detail.item.subpage) { | 88 event.detail.item.subpage) { |
84 return; | 89 return; |
85 } | 90 } |
86 this.selectedPageId = event.detail.item.PAGE_ID; | 91 this.selectedPageId = event.detail.item.PAGE_ID; |
87 }, | 92 }, |
88 | 93 |
89 /** | 94 /** |
90 * If no page is selected, selects the first page. This happens on load and | 95 * If no page is selected, selects the first page. This happens on load and |
91 * when a selected page is removed. | 96 * when a selected page is removed. |
92 * | |
93 * @private | 97 * @private |
94 */ | 98 */ |
95 ensureSelection_: function() { | 99 ensureSelection_: function() { |
96 if (!this.pages.length) | 100 if (!this.pages.length) |
97 return; | 101 return; |
98 if (this.selectedPageId == '') | 102 if (this.selectedPageId == '') |
99 this.selectedPageId = this.pages[0].PAGE_ID; | 103 this.selectedPageId = this.pages[0].PAGE_ID; |
100 }, | 104 }, |
101 | 105 |
102 /** | 106 /** |
103 * Updates the list of pages using the pages in core-animated-pages. | 107 * Updates the list of pages using the pages in iron-pages. |
104 * | |
105 * @private | 108 * @private |
106 */ | 109 */ |
107 pageContainerUpdated_: function() { | 110 pageContainerUpdated_: function() { |
108 this.pages = this.$.pageContainer.items.filter(function(item) { | 111 this.pages = this.$.pageContainer.items.filter(function(item) { |
109 return !item.subpage; | 112 return !item.subpage; |
110 }); | 113 }); |
111 this.ensureSelection_(); | 114 this.ensureSelection_(); |
112 }, | 115 }, |
113 }); | 116 }); |
OLD | NEW |