OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 // PrefCheckbox class: |
| 7 |
| 8 // Define a constructor that uses an input element as its underlying element. |
| 9 var PrefCheckbox = cr.ui.define('input'); |
| 10 |
| 11 PrefCheckbox.prototype = { |
| 12 // Set up the prototype chain |
| 13 __proto__: HTMLInputElement.prototype, |
| 14 |
| 15 /** |
| 16 * Initialization function for the cr.ui framework. |
| 17 */ |
| 18 decorate: function() { |
| 19 this.type = 'checkbox'; |
| 20 var self = this; |
| 21 |
| 22 // Listen to pref changes. |
| 23 Preferences.getInstance().addEventListener(this.pref, |
| 24 function(event) { |
| 25 self.checked = event.value; |
| 26 }); |
| 27 |
| 28 // Listen to user events. |
| 29 this.addEventListener('click', |
| 30 function(e) { |
| 31 Preferences.setBooleanPref(self.pref, |
| 32 self.checked); |
| 33 }); |
| 34 }, |
| 35 |
| 36 /** |
| 37 * Getter for preference name attribute. |
| 38 */ |
| 39 get pref() { |
| 40 return this.getAttribute('pref'); |
| 41 }, |
| 42 |
| 43 /** |
| 44 * Setter for preference name attribute. |
| 45 */ |
| 46 set pref(name) { |
| 47 this.setAttribute('pref', name); |
| 48 } |
| 49 }; |
| 50 |
| 51 /////////////////////////////////////////////////////////////////////////////// |
| 52 // PrefRange class: |
| 53 |
| 54 // Define a constructor that uses an input element as its underlying element. |
| 55 var PrefRange = cr.ui.define('input'); |
| 56 |
| 57 PrefRange.prototype = { |
| 58 // Set up the prototype chain |
| 59 __proto__: HTMLInputElement.prototype, |
| 60 |
| 61 /** |
| 62 * Initialization function for the cr.ui framework. |
| 63 */ |
| 64 decorate: function() { |
| 65 this.type = 'range'; |
| 66 var self = this; |
| 67 |
| 68 // Listen to pref changes. |
| 69 Preferences.getInstance().addEventListener(this.pref, |
| 70 function(event) { |
| 71 self.value = event.value; |
| 72 }); |
| 73 |
| 74 // Listen to user events. |
| 75 this.addEventListener('change', |
| 76 function(e) { |
| 77 Preferences.setIntegerPref(self.pref, self.value); |
| 78 }); |
| 79 }, |
| 80 |
| 81 /** |
| 82 * Getter for preference name attribute. |
| 83 */ |
| 84 get pref() { |
| 85 return this.getAttribute('pref'); |
| 86 }, |
| 87 |
| 88 /** |
| 89 * Setter for preference name attribute. |
| 90 */ |
| 91 set pref(name) { |
| 92 this.setAttribute('pref', name); |
| 93 } |
| 94 }; |
| 95 |
| 96 |
| 97 /////////////////////////////////////////////////////////////////////////////// |
| 98 // PrefSelect class: |
| 99 |
| 100 // Define a constructor that uses an select element as its underlying element. |
| 101 var PrefSelect = cr.ui.define('select'); |
| 102 |
| 103 PrefSelect.prototype = { |
| 104 // Set up the prototype chain |
| 105 __proto__: HTMLSelectElement.prototype, |
| 106 |
| 107 /** |
| 108 * Initialization function for the cr.ui framework. |
| 109 */ |
| 110 decorate: function() { |
| 111 var self = this; |
| 112 // Listen to pref changes. |
| 113 Preferences.getInstance().addEventListener(this.pref, |
| 114 function(event) { |
| 115 for (var i = 0; i < self.options.length; i++) { |
| 116 if (self.options[i].value == event.value) { |
| 117 self.selectedIndex = i; |
| 118 return; |
| 119 } |
| 120 } |
| 121 self.selectedIndex = -1; |
| 122 }); |
| 123 |
| 124 // Listen to user events. |
| 125 this.addEventListener('change', |
| 126 function(e) { |
| 127 Preferences.setStringPref(self.pref, |
| 128 self.options[self.selectedIndex].value); |
| 129 }); |
| 130 }, |
| 131 |
| 132 /** |
| 133 * Sets up options in select element. |
| 134 * @param {Array} options List of option and their display text. |
| 135 * Each string in the array contains options value and display text split |
| 136 * with '|' character. |
| 137 * |
| 138 * TODO(zelidrag): move this to that i18n template classes. |
| 139 */ |
| 140 initializeValues: function(options) { |
| 141 var self = this; |
| 142 options.forEach(function (option) { |
| 143 var values = option.split('|'); |
| 144 self.appendChild(new Option(values[1], values[0], false, false)); |
| 145 }); |
| 146 }, |
| 147 /** |
| 148 * Getter for preference name attribute. |
| 149 */ |
| 150 get pref() { |
| 151 return this.getAttribute('pref'); |
| 152 }, |
| 153 |
| 154 /** |
| 155 * Setter for preference name attribute. |
| 156 */ |
| 157 set pref(name) { |
| 158 this.setAttribute('pref', name); |
| 159 } |
| 160 }; |
OLD | NEW |