Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * 'cr-settings-main' displays the selected settings page. | |
| 8 * | |
| 9 * Example: | |
| 10 * | |
| 11 * <cr-settings-main pages="{{pages}}" currentPageIndex="{{index}}"> | |
| 12 * </cr-settings-main> | |
| 13 * | |
| 14 * See cr-settings-drawer for example of use in 'core-panel'. | |
| 15 * | |
| 16 * @group Chrome Settings Elements | |
| 17 * @element cr-settings-main | |
| 18 */ | |
| 19 Polymer('cr-settings-main', { | |
| 20 publish: { | |
| 21 /** | |
| 22 * Pages that may be shown. | |
| 23 * | |
| 24 * @attribute pages | |
| 25 * @type Array<!CrSettingsPage> | |
| 26 * @default [] | |
| 27 */ | |
| 28 pages: [], | |
|
Jeremy Klein
2015/03/07 23:55:14
Same comment about initialization.
| |
| 29 | |
| 30 /** | |
| 31 * Index of the currently selected page. | |
| 32 * | |
| 33 * @attribute selectedIndex | |
| 34 * @type number | |
| 35 * default 0 | |
| 36 */ | |
| 37 currentPageIndex: 0, | |
| 38 }, | |
| 39 | |
| 40 /** | |
| 41 * Polymer pages changed method. Ensures the pages container contains the | |
| 42 * pages in the right order. | |
| 43 * | |
| 44 * @param {*} value The old value if the property value has changed, or the | |
|
Jeremy Klein
2015/03/07 23:55:13
Please use a more specific type.
| |
| 45 * array of splices if the existing array has been changed. | |
| 46 */ | |
| 47 pagesChanged: function(value) { | |
| 48 // If value is an array of plain objects, it represents splices from an | |
| 49 // ArrayObserver. | |
| 50 if (value instanceof Array && value.length && | |
| 51 Object.prototype.isPrototypeOf(value[0])) { | |
| 52 this.applyPageSplices_(value); | |
| 53 } else { | |
| 54 this.updatePages_(this.pages); | |
| 55 } | |
| 56 }, | |
| 57 | |
| 58 /** | |
| 59 * Sets the list of pages shown in the 'core-animated-pages'. Rebuilds the DOM | |
| 60 * for 'core-animated-pages', so this should be called sparingly. | |
| 61 * | |
| 62 * @private | |
| 63 */ | |
| 64 updatePages_: function() { | |
|
Jeremy Klein
2015/03/07 23:55:13
A lot of the logic in this file could be simplifie
| |
| 65 this.$.pages.innerHTML = ''; | |
| 66 if (!this.pages.length) | |
| 67 return; | |
| 68 | |
| 69 if (this.currentPageIndex >= this.pages.length) | |
| 70 this.currentPageIndex = 0; | |
| 71 | |
| 72 for (var i = 0; i < this.pages.length; i++) | |
| 73 this.$.pages.appendChild(this.createSection_(this.pages[i])); | |
| 74 }, | |
| 75 | |
| 76 /** | |
| 77 * Applies the splices for the list of pages to the sections in the DOM. | |
| 78 * | |
| 79 * TODO(michaelpg): core-animated-pages throws when all pages are removed. | |
| 80 * | |
| 81 * @param {!Array<!Object>} splices The changes made to 'pages'. | |
| 82 * @private | |
| 83 */ | |
| 84 applyPageSplices_: function(splices) { | |
| 85 var numRemoved = 0; | |
| 86 splices.forEach(function(splice) { | |
| 87 // Remove the sections to be removed before inserting new children. | |
| 88 for (var i = 0; i < splice.removed.length; i++) | |
| 89 this.$.pages.removeChild(splice.removed[i].parentNode); | |
| 90 numRemoved += splice.removed.length; | |
| 91 | |
| 92 if (!splice.addedCount) | |
| 93 return; | |
| 94 | |
| 95 // Find the node to insert before, unless splice.index indicates the end | |
| 96 // of the array. | |
| 97 var sectionAfter = null; | |
| 98 if (splice.index < this.$.pages.children.length) | |
| 99 sectionAfter = this.$.pages.children[splice.index]; | |
| 100 | |
| 101 for (var i = 0; i < splice.addedCount; i++) { | |
| 102 var section = this.createSection_(this.pages[splice.index + i]); | |
| 103 this.$.pages.insertBefore(section, sectionAfter); | |
| 104 } | |
| 105 }.bind(this)); | |
| 106 | |
| 107 if (this.currentPageIndex >= this.pages.length) | |
| 108 this.currentPageIndex = 0; | |
| 109 }, | |
| 110 | |
| 111 /** Creates a section for 'core-animated-pages' for the given element. | |
| 112 * | |
| 113 * @param {HTMLElement} element Element to add to 'core-animated-pages'. | |
| 114 * @private | |
| 115 */ | |
| 116 createSection_: function(element) { | |
| 117 element.setAttribute('cross-fade', ''); | |
| 118 var section = document.createElement('section'); | |
| 119 section.appendChild(element); | |
| 120 return section; | |
| 121 }, | |
| 122 }); | |
| OLD | NEW |