OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 cr.define('print_preview', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * Creates a PageSettings object. This object encapsulates all settings and |
| 10 * logic related to page selection. |
| 11 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and |
| 12 * write page range settings. |
| 13 * @constructor |
| 14 * @extends {print_preview.Component} |
| 15 */ |
| 16 function PageSettings(printTicketStore) { |
| 17 print_preview.Component.call(this); |
| 18 |
| 19 /** |
| 20 * Used to read and write page range settings. |
| 21 * @type {!print_preview.PrintTicketStore} |
| 22 * @private |
| 23 */ |
| 24 this.printTicketStore_ = printTicketStore; |
| 25 |
| 26 /** |
| 27 * Timeout used to delay processing of the custom page range input. |
| 28 * @type {Object} |
| 29 * @private |
| 30 */ |
| 31 this.customInputTimeout_ = null; |
| 32 |
| 33 /** |
| 34 * Custom page range input. |
| 35 * @type {HTMLInputElement} |
| 36 * @private |
| 37 */ |
| 38 this.customInput_ = null; |
| 39 |
| 40 /** |
| 41 * Custom page range radio button. |
| 42 * @type {HTMLInputElement} |
| 43 * @private |
| 44 */ |
| 45 this.customRadio_ = null; |
| 46 |
| 47 /** |
| 48 * All page rage radio button. |
| 49 * @type {HTMLInputElement} |
| 50 * @private |
| 51 */ |
| 52 this.allRadio_ = null; |
| 53 |
| 54 /** |
| 55 * Container of a hint to show when the custom page range is invalid. |
| 56 * @type {HTMLElement} |
| 57 * @private |
| 58 */ |
| 59 this.customHintEl_ = null; |
| 60 }; |
| 61 |
| 62 /** |
| 63 * CSS classes used by the page settings. |
| 64 * @enum {string} |
| 65 * @private |
| 66 */ |
| 67 PageSettings.Classes_ = { |
| 68 ALL_RADIO: 'page-settings-all-radio', |
| 69 CUSTOM_HINT: 'page-settings-custom-hint', |
| 70 CUSTOM_INPUT: 'page-settings-custom-input', |
| 71 CUSTOM_RADIO: 'page-settings-custom-radio' |
| 72 }; |
| 73 |
| 74 /** |
| 75 * Delay in milliseconds before processing custom page range input. |
| 76 * @type {number} |
| 77 * @private |
| 78 */ |
| 79 PageSettings.CUSTOM_INPUT_DELAY_ = 500; |
| 80 |
| 81 PageSettings.prototype = { |
| 82 __proto__: print_preview.Component.prototype, |
| 83 |
| 84 set isEnabled(isEnabled) { |
| 85 this.customInput_.disabled = !isEnabled; |
| 86 this.allRadio_.disabled = !isEnabled; |
| 87 this.customRadio_.disabled = !isEnabled; |
| 88 }, |
| 89 |
| 90 /** @override */ |
| 91 enterDocument: function() { |
| 92 print_preview.Component.prototype.enterDocument.call(this); |
| 93 this.tracker.add( |
| 94 this.allRadio_, 'click', this.onAllRadioClick_.bind(this)); |
| 95 this.tracker.add( |
| 96 this.customRadio_, 'click', this.onCustomRadioClick_.bind(this)); |
| 97 this.tracker.add( |
| 98 this.customInput_, 'blur', this.onCustomInputBlur_.bind(this)); |
| 99 this.tracker.add( |
| 100 this.customInput_, 'keyup', this.onCustomInputKeyUp_.bind(this)); |
| 101 this.tracker.add( |
| 102 this.printTicketStore_, |
| 103 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE, |
| 104 this.onPrintTicketStoreChange_.bind(this)); |
| 105 this.tracker.add( |
| 106 this.printTicketStore_, |
| 107 print_preview.PrintTicketStore.EventType.TICKET_CHANGE, |
| 108 this.onPrintTicketStoreChange_.bind(this)); |
| 109 this.tracker.add( |
| 110 this.printTicketStore_, |
| 111 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE, |
| 112 this.onPrintTicketStoreChange_.bind(this)); |
| 113 }, |
| 114 |
| 115 /** @override */ |
| 116 exitDocument: function() { |
| 117 print_preview.Component.prototype.exitDocument.call(this); |
| 118 this.customInput_ = null; |
| 119 this.customRadio_ = null; |
| 120 this.allRadio_ = null; |
| 121 this.customHintEl_ = null; |
| 122 }, |
| 123 |
| 124 /** @override */ |
| 125 decorateInternal: function() { |
| 126 this.customInput_ = this.getElement().getElementsByClassName( |
| 127 PageSettings.Classes_.CUSTOM_INPUT)[0]; |
| 128 this.allRadio_ = this.getElement().getElementsByClassName( |
| 129 PageSettings.Classes_.ALL_RADIO)[0]; |
| 130 this.customRadio_ = this.getElement().getElementsByClassName( |
| 131 PageSettings.Classes_.CUSTOM_RADIO)[0]; |
| 132 this.customHintEl_ = this.getElement().getElementsByClassName( |
| 133 PageSettings.Classes_.CUSTOM_HINT)[0]; |
| 134 this.customHintEl_.textContent = localStrings.getStringF( |
| 135 'pageRangeInstruction', |
| 136 localStrings.getString('examplePageRangeText')); |
| 137 }, |
| 138 |
| 139 /** |
| 140 * @param {boolean} Whether the custom hint is visible. |
| 141 * @private |
| 142 */ |
| 143 setInvalidStateVisible_: function(isVisible) { |
| 144 if (isVisible) { |
| 145 this.customInput_.classList.add('invalid'); |
| 146 this.customHintEl_.setAttribute('aria-hidden', 'false'); |
| 147 fadeInElement(this.customHintEl_); |
| 148 } else { |
| 149 this.customInput_.classList.remove('invalid'); |
| 150 fadeOutElement(this.customHintEl_); |
| 151 this.customHintEl_.setAttribute('aria-hidden', 'true'); |
| 152 } |
| 153 }, |
| 154 |
| 155 /** |
| 156 * Called when the all radio button is clicked. Updates the print ticket. |
| 157 * @private |
| 158 */ |
| 159 onAllRadioClick_: function() { |
| 160 this.printTicketStore_.updatePageRange(''); |
| 161 }, |
| 162 |
| 163 /** |
| 164 * Called when the custom radio button is clicked. Updates the print ticket. |
| 165 * @private |
| 166 */ |
| 167 onCustomRadioClick_: function() { |
| 168 this.customInput_.focus(); |
| 169 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 170 }, |
| 171 |
| 172 /** |
| 173 * Called when the custom input is blurred. Enables the all radio button if |
| 174 * the custom input is empty. |
| 175 * @private |
| 176 */ |
| 177 onCustomInputBlur_: function() { |
| 178 if (this.customInput_.value == '') { |
| 179 this.allRadio_.checked = true; |
| 180 this.customRadio_.checked = false; |
| 181 } |
| 182 }, |
| 183 |
| 184 /** |
| 185 * Called when a key is pressed on the custom input. |
| 186 * @param {Event} event Contains the key that was pressed. |
| 187 * @private |
| 188 */ |
| 189 onCustomInputKeyUp_: function(event) { |
| 190 if (this.customInputTimeout_) { |
| 191 clearTimeout(this.customInputTimeout_); |
| 192 } |
| 193 if (event.keyIdentifier == 'Enter') { |
| 194 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 195 } else { |
| 196 this.allRadio_.checked = false; |
| 197 this.customRadio_.checked = true; |
| 198 this.customInputTimeout_ = setTimeout( |
| 199 this.onCustomInputTimeout_.bind(this), |
| 200 PageSettings.CUSTOM_INPUT_DELAY_); |
| 201 } |
| 202 }, |
| 203 |
| 204 /** |
| 205 * Called after a delay following a key press in the custom input. |
| 206 * @private |
| 207 */ |
| 208 onCustomInputTimeout_: function() { |
| 209 this.customInputTimeout_ = null; |
| 210 if (this.customRadio_.checked) { |
| 211 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 212 } |
| 213 }, |
| 214 |
| 215 /** |
| 216 * Called when the print ticket changes. Updates the state of the component. |
| 217 * @private |
| 218 */ |
| 219 onPrintTicketStoreChange_: function() { |
| 220 if (this.printTicketStore_.hasPageRangeCapability()) { |
| 221 var pageRangeStr = this.printTicketStore_.getPageRangeStr(); |
| 222 if (pageRangeStr || this.customRadio_.checked) { |
| 223 if (!document.hasFocus() || |
| 224 document.activeElement != this.customInput_) { |
| 225 this.customInput_.value = pageRangeStr; |
| 226 } |
| 227 this.customRadio_.checked = true; |
| 228 this.allRadio_.checked = false; |
| 229 this.setInvalidStateVisible_( |
| 230 !this.printTicketStore_.isPageRangeValid()); |
| 231 } else { |
| 232 this.allRadio_.checked = true; |
| 233 this.customRadio_.checked = false; |
| 234 this.setInvalidStateVisible_(false); |
| 235 } |
| 236 fadeInOption(this.getElement()); |
| 237 } else { |
| 238 fadeOutOption(this.getElement()); |
| 239 } |
| 240 } |
| 241 }; |
| 242 |
| 243 // Export |
| 244 return { |
| 245 PageSettings: PageSettings |
| 246 }; |
| 247 }); |
OLD | NEW |