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 /** |
| 35 * CSS classes used by the page settings. |
| 36 * @enum {string} |
| 37 * @private |
| 38 */ |
| 39 PageSettings.Classes_ = { |
| 40 ALL_RADIO: 'page-settings-all-radio', |
| 41 CUSTOM_HINT: 'page-settings-custom-hint', |
| 42 CUSTOM_INPUT: 'page-settings-custom-input', |
| 43 CUSTOM_RADIO: 'page-settings-custom-radio' |
| 44 }; |
| 45 |
| 46 /** |
| 47 * Delay in milliseconds before processing custom page range input. |
| 48 * @type {number} |
| 49 * @private |
| 50 */ |
| 51 PageSettings.CUSTOM_INPUT_DELAY_ = 500; |
| 52 |
| 53 PageSettings.prototype = { |
| 54 __proto__: print_preview.Component.prototype, |
| 55 |
| 56 set isEnabled(isEnabled) { |
| 57 this.customInput_.disabled = !isEnabled; |
| 58 this.allRadio_.disabled = !isEnabled; |
| 59 this.customRadio_.disabled = !isEnabled; |
| 60 }, |
| 61 |
| 62 /** @override */ |
| 63 enterDocument: function() { |
| 64 print_preview.Component.prototype.enterDocument.call(this); |
| 65 this.tracker.add( |
| 66 this.allRadio_, 'click', this.onAllRadioClick_.bind(this)); |
| 67 this.tracker.add( |
| 68 this.customRadio_, 'click', this.onCustomRadioClick_.bind(this)); |
| 69 this.tracker.add( |
| 70 this.customInput_, 'blur', this.onCustomInputBlur_.bind(this)); |
| 71 this.tracker.add( |
| 72 this.customInput_, 'keyup', this.onCustomInputKeyUp_.bind(this)); |
| 73 this.tracker.add( |
| 74 this.printTicketStore_, |
| 75 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE, |
| 76 this.onPrintTicketStoreChange_.bind(this)); |
| 77 this.tracker.add( |
| 78 this.printTicketStore_, |
| 79 print_preview.PrintTicketStore.EventType.TICKET_CHANGE, |
| 80 this.onPrintTicketStoreChange_.bind(this)); |
| 81 this.tracker.add( |
| 82 this.printTicketStore_, |
| 83 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE, |
| 84 this.onPrintTicketStoreChange_.bind(this)); |
| 85 }, |
| 86 |
| 87 /** @override */ |
| 88 decorateInternal: function() { |
| 89 this.customHintEl_.textContent = localStrings.getStringF( |
| 90 'pageRangeInstruction', |
| 91 localStrings.getString('examplePageRangeText')); |
| 92 }, |
| 93 |
| 94 get customInput_() { |
| 95 return this.getElement().getElementsByClassName( |
| 96 PageSettings.Classes_.CUSTOM_INPUT)[0]; |
| 97 }, |
| 98 |
| 99 get allRadio_() { |
| 100 return this.getElement().getElementsByClassName( |
| 101 PageSettings.Classes_.ALL_RADIO)[0]; |
| 102 }, |
| 103 |
| 104 get customRadio_() { |
| 105 return this.getElement().getElementsByClassName( |
| 106 PageSettings.Classes_.CUSTOM_RADIO)[0]; |
| 107 }, |
| 108 |
| 109 get customHintEl_() { |
| 110 return this.getElement().getElementsByClassName( |
| 111 PageSettings.Classes_.CUSTOM_HINT)[0]; |
| 112 }, |
| 113 |
| 114 setInvalidStateVisible_: function(isVisible) { |
| 115 if (isVisible) { |
| 116 this.customInput_.classList.add('invalid'); |
| 117 this.customHintEl_.setAttribute('aria-hidden', 'false'); |
| 118 fadeInElement(this.customHintEl_); |
| 119 } else { |
| 120 this.customInput_.classList.remove('invalid'); |
| 121 fadeOutElement(this.customHintEl_); |
| 122 this.customHintEl_.setAttribute('aria-hidden', 'true'); |
| 123 } |
| 124 }, |
| 125 |
| 126 onAllRadioClick_: function() { |
| 127 this.printTicketStore_.updatePageRange(''); |
| 128 }, |
| 129 |
| 130 onCustomRadioClick_: function() { |
| 131 this.customInput_.focus(); |
| 132 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 133 }, |
| 134 |
| 135 onCustomInputBlur_: function() { |
| 136 if (this.customInput_.value == '') { |
| 137 this.allRadio_.checked = true; |
| 138 this.customRadio_.checked = false; |
| 139 } |
| 140 }, |
| 141 |
| 142 onCustomInputKeyUp_: function(event) { |
| 143 if (this.customInputTimeout_) { |
| 144 clearTimeout(this.customInputTimeout_); |
| 145 } |
| 146 if (event.keyIdentifier == 'Enter') { |
| 147 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 148 } else { |
| 149 this.allRadio_.checked = false; |
| 150 this.customRadio_.checked = true; |
| 151 this.customInputTimeout_ = setTimeout( |
| 152 this.onCustomInputTimeout_.bind(this), |
| 153 PageSettings.CUSTOM_INPUT_DELAY_); |
| 154 } |
| 155 }, |
| 156 |
| 157 onCustomInputTimeout_: function() { |
| 158 this.customInputTimeout_ = null; |
| 159 if (this.customRadio_.checked) { |
| 160 this.printTicketStore_.updatePageRange(this.customInput_.value); |
| 161 } |
| 162 }, |
| 163 |
| 164 onPrintTicketStoreChange_: function() { |
| 165 if (this.printTicketStore_.hasPageRangeCapability()) { |
| 166 var pageRangeStr = this.printTicketStore_.getPageRangeStr(); |
| 167 if (pageRangeStr) { |
| 168 // TODO There might be a bad case here where we overwrite what the |
| 169 // user is typing. Also could be the case in copies. |
| 170 this.customInput_.value = pageRangeStr; |
| 171 this.customRadio_.checked = true; |
| 172 this.allRadio_.checked = false; |
| 173 this.setInvalidStateVisible_( |
| 174 !this.printTicketStore_.isPageRangeValid()); |
| 175 } else { |
| 176 this.allRadio_.checked = true; |
| 177 this.customRadio_.checked = false; |
| 178 this.customInput_.value = ''; |
| 179 this.setInvalidStateVisible_(false); |
| 180 } |
| 181 fadeInOption(this.getElement()); |
| 182 } else { |
| 183 fadeOutOption(this.getElement()); |
| 184 } |
| 185 } |
| 186 }; |
| 187 |
| 188 // Export |
| 189 return { |
| 190 PageSettings: PageSettings |
| 191 }; |
| 192 }); |
OLD | NEW |