| 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-pref-tracker` is a utility element used to track the | 7 * `cr-settings-pref-tracker` is a utility element used to track the |
| 8 * initialization of a specified preference and throw an error if the pref | 8 * initialization of a specified preference and throw an error if the pref |
| 9 * is not defined after prefs have all been fetched. | 9 * is not defined after prefs have all been fetched. |
| 10 * | 10 * |
| 11 * Example: | 11 * Example: |
| 12 * | 12 * |
| 13 * <cr-settings-pref-tracker pref="{{prefs.settings.foo.bar}}"> | 13 * <cr-settings-pref-tracker pref="{{prefs.settings.foo.bar}}"> |
| 14 * </cr-settings-pref-tracker> | 14 * </cr-settings-pref-tracker> |
| 15 * | 15 * |
| 16 * @element cr-settings-pref-tracker | 16 * @element cr-settings-pref-tracker |
| 17 */ | 17 */ |
| 18 (function() { | 18 (function() { |
| 19 | 19 |
| 20 /** | |
| 21 * An array of all the tracker instances. | |
| 22 * @type {!Array<!CrSettingsPrefTrackerElement>} | |
| 23 */ | |
| 24 var instances = []; | |
| 25 | |
| 26 /** | |
| 27 * Validates all tracker instances. | |
| 28 * @private | |
| 29 */ | |
| 30 var validateAll_ = function() { | |
| 31 instances.forEach(function(tracker) { | |
| 32 tracker.validate_(); | |
| 33 }); | |
| 34 }; | |
| 35 | |
| 36 document.addEventListener(CrSettingsPrefs.INITIALIZED, validateAll_); | |
| 37 | |
| 38 Polymer({ | 20 Polymer({ |
| 39 is: 'cr-settings-pref-tracker', | 21 is: 'cr-settings-pref-tracker', |
| 40 | 22 |
| 41 properties: { | 23 properties: { |
| 42 /** | 24 /** |
| 43 * The Preference object being tracked. | 25 * The Preference object being tracked. |
| 44 * @type {?PrefObject} | 26 * @type {?PrefObject} |
| 45 */ | 27 */ |
| 46 pref: { | 28 pref: { |
| 47 type: Object, | 29 type: Object, |
| 48 observer: 'validate_', | 30 observer: 'validate_', |
| 49 }, | 31 }, |
| 50 }, | 32 }, |
| 51 | 33 |
| 52 /** @override */ | 34 /** @override */ |
| 53 ready: function() { | 35 ready: function() { |
| 54 this.validate_(); | 36 this.validate_(); |
| 55 | |
| 56 instances.push(this); | |
| 57 }, | 37 }, |
| 58 | 38 |
| 59 /** | 39 /** |
| 60 * Throws an error if prefs are initialized and the tracked pref is not | 40 * Logs an error once prefs are initialized if the tracked pref is |
| 61 * found. | 41 * not found. |
| 62 * @private | 42 * @private |
| 63 */ | 43 */ |
| 64 validate_: function() { | 44 validate_: function() { |
| 65 this.async(function() { | 45 CrSettingsPrefs.initialized.then(function() { |
| 66 // Note that null == undefined. | 46 // Note that null == undefined. |
| 67 if (CrSettingsPrefs.isInitialized && this.pref == null) { | 47 if (this.pref == null) { |
| 68 // HACK ALERT: This is the best clue we have as to the pref key for | 48 // HACK ALERT: This is the best clue we have as to the pref key for |
| 69 // this tracker. This value should not be relied upon anywhere or | 49 // this tracker. This value should not be relied upon anywhere or |
| 70 // actually used besides for this error message. | 50 // actually used besides for this error message. |
| 71 var parentControlHTML = this.parentNode && this.parentNode.host && | 51 var parentControlHTML = this.domHost && this.domHost.outerHTML; |
| 72 this.parentNode.host.outerHTML; | |
| 73 | 52 |
| 74 throw new Error('Pref not found. Parent control:' + | 53 console.error('Pref not found. Parent control:' + |
| 75 (parentControlHTML || 'Unknown')); | 54 (parentControlHTML || 'Unknown')); |
| 76 } | 55 } |
| 77 }); | 56 }.bind(this)); |
| 78 }, | 57 }, |
| 79 }); | 58 }); |
| 80 })(); | 59 })(); |
| OLD | NEW |