| 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 CopiesSettings object. | |
| 10 * @constructor | |
| 11 */ | |
| 12 function CopiesSettings() { | |
| 13 this.copiesOption_ = $('copies-option'); | |
| 14 this.textfield_ = $('copies'); | |
| 15 this.incrementButton_ = $('increment'); | |
| 16 this.decrementButton_ = $('decrement'); | |
| 17 // Minimum allowed value for number of copies. | |
| 18 this.minValue_ = 1; | |
| 19 // Maximum allowed value for number of copies. | |
| 20 this.maxValue_ = 999; | |
| 21 this.collateOption_ = $('collate-option'); | |
| 22 this.collateCheckbox_ = $('collate'); | |
| 23 this.hint_ = $('copies-hint'); | |
| 24 this.twoSidedCheckbox_ = $('two-sided'); | |
| 25 this.twoSidedOption_ = $('two-sided-option'); | |
| 26 | |
| 27 this.previousDuplexMode_ = CopiesSettings.UNKNOWN_DUPLEX_MODE; | |
| 28 this.addEventListeners_(); | |
| 29 } | |
| 30 | |
| 31 // Constant values matches printing::DuplexMode enum. | |
| 32 CopiesSettings.SIMPLEX = 0; | |
| 33 CopiesSettings.LONG_EDGE = 1; | |
| 34 CopiesSettings.UNKNOWN_DUPLEX_MODE = -1; | |
| 35 | |
| 36 cr.addSingletonGetter(CopiesSettings); | |
| 37 | |
| 38 CopiesSettings.prototype = { | |
| 39 /** | |
| 40 * The number of copies represented by the contents of |this.textfield_|. | |
| 41 * If the text is not valid returns |this.minValue_|. | |
| 42 * @type {number} | |
| 43 */ | |
| 44 get numberOfCopies() { | |
| 45 var value = parseInt(this.textfield_.value, 10); | |
| 46 if (!value || value <= this.minValue_) | |
| 47 value = this.minValue_; | |
| 48 return value; | |
| 49 }, | |
| 50 | |
| 51 /** | |
| 52 * Getter method for |collateOption_|. | |
| 53 * @type {HTMLElement} | |
| 54 */ | |
| 55 get collateOption() { | |
| 56 return this.collateOption_; | |
| 57 }, | |
| 58 | |
| 59 /** | |
| 60 * Getter method for |twoSidedCheckbox_|. | |
| 61 * @type {HTMLInputElement} | |
| 62 */ | |
| 63 get twoSidedCheckbox() { | |
| 64 return this.twoSidedCheckbox_; | |
| 65 }, | |
| 66 | |
| 67 /** | |
| 68 * Gets the duplex mode information for printing. | |
| 69 * @return {number} duplex mode. | |
| 70 */ | |
| 71 get duplexMode() { | |
| 72 if (this.twoSidedOption_.hidden) | |
| 73 return CopiesSettings.UNKNOWN_DUPLEX_MODE; | |
| 74 else if (this.twoSidedCheckbox_.checked) | |
| 75 return CopiesSettings.LONG_EDGE; | |
| 76 else | |
| 77 return CopiesSettings.SIMPLEX; | |
| 78 }, | |
| 79 | |
| 80 set previousDuplexMode(duplexMode) { | |
| 81 this.previousDuplexMode_ = duplexMode; | |
| 82 }, | |
| 83 | |
| 84 /** | |
| 85 * @return {boolean} true if |this.textfield_| is empty, or represents a | |
| 86 * positive integer value. | |
| 87 */ | |
| 88 isValid: function() { | |
| 89 return !this.textfield_.value || isPositiveInteger(this.textfield_.value); | |
| 90 }, | |
| 91 | |
| 92 /** | |
| 93 * Checks whether the preview collate setting value is set or not. | |
| 94 * @return {boolean} true if collate option is enabled and checked. | |
| 95 */ | |
| 96 isCollated: function() { | |
| 97 return !this.collateOption_.hidden && this.collateCheckbox_.checked; | |
| 98 }, | |
| 99 | |
| 100 /** | |
| 101 * Resets |this.textfield_| to |this.minValue_|. | |
| 102 * @private | |
| 103 */ | |
| 104 reset_: function() { | |
| 105 this.textfield_.value = this.minValue_; | |
| 106 }, | |
| 107 | |
| 108 /** | |
| 109 * Listener function to execute whenever the increment/decrement buttons are | |
| 110 * clicked. | |
| 111 * @private | |
| 112 * @param {int} sign Must be 1 for an increment button click and -1 for a | |
| 113 * decrement button click. | |
| 114 */ | |
| 115 onButtonClicked_: function(sign) { | |
| 116 if (!this.isValid()) { | |
| 117 this.reset_(); | |
| 118 } else { | |
| 119 var newValue = this.numberOfCopies + sign; | |
| 120 this.textfield_.value = newValue; | |
| 121 } | |
| 122 this.updateButtonsState_(); | |
| 123 this.showHideCollateOption_(); | |
| 124 | |
| 125 if (!hasPendingPreviewRequest) { | |
| 126 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 127 cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); | |
| 128 } | |
| 129 }, | |
| 130 | |
| 131 /** | |
| 132 * Listener function to execute whenever a change occurs in |textfield_| | |
| 133 * textfield. | |
| 134 * @private | |
| 135 */ | |
| 136 onTextfieldChanged_: function() { | |
| 137 this.updateButtonsState_(); | |
| 138 this.showHideCollateOption_(); | |
| 139 if (!hasPendingPreviewRequest) { | |
| 140 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 141 cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); | |
| 142 } | |
| 143 }, | |
| 144 | |
| 145 /** | |
| 146 * Adding listeners to all copies related controls. The listeners take care | |
| 147 * of altering their behavior depending on |hasPendingPreviewRequest|. | |
| 148 * @private | |
| 149 */ | |
| 150 addEventListeners_: function() { | |
| 151 this.textfield_.oninput = this.onTextfieldChanged_.bind(this); | |
| 152 this.incrementButton_.onclick = this.onIncrementButtonClicked_.bind(this); | |
| 153 this.decrementButton_.onclick = this.onDecrementButtonClicked_.bind(this); | |
| 154 this.twoSidedCheckbox_.onclick = function() { | |
| 155 if (!hasPendingPreviewRequest) | |
| 156 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 157 } | |
| 158 document.addEventListener(customEvents.PDF_LOADED, | |
| 159 this.updateButtonsState_.bind(this)); | |
| 160 document.addEventListener(customEvents.PRINTER_CAPABILITIES_UPDATED, | |
| 161 this.onPrinterCapabilitiesUpdated_.bind(this)); | |
| 162 }, | |
| 163 | |
| 164 /** | |
| 165 * Executes when a |customEvents.PRINTER_CAPABILITIES_UPDATED| event occurs. | |
| 166 * @private | |
| 167 */ | |
| 168 onPrinterCapabilitiesUpdated_: function(e) { | |
| 169 var duplexValue = e.printerCapabilities.printerDefaultDuplexValue; | |
| 170 if (duplexValue != CopiesSettings.UNKNOWN_DUPLEX_MODE && | |
| 171 this.previousDuplexMode_ != CopiesSettings.UNKNOWN_DUPLEX_MODE) { | |
| 172 duplexValue = this.previousDuplexMode_; | |
| 173 } | |
| 174 this.updateTwoSidedOption_(duplexValue); | |
| 175 e.printerCapabilities.disableCopiesOption ? | |
| 176 fadeOutOption(this.copiesOption_) : | |
| 177 fadeInOption(this.copiesOption_); | |
| 178 }, | |
| 179 | |
| 180 /** | |
| 181 * Listener triggered when |incrementButton_| is clicked. | |
| 182 * @private | |
| 183 */ | |
| 184 onIncrementButtonClicked_: function() { | |
| 185 this.onButtonClicked_(1); | |
| 186 }, | |
| 187 | |
| 188 /** | |
| 189 * Listener triggered when |decrementButton_| is clicked. | |
| 190 * @private | |
| 191 */ | |
| 192 onDecrementButtonClicked_: function() { | |
| 193 this.onButtonClicked_(-1); | |
| 194 }, | |
| 195 | |
| 196 /** | |
| 197 * Takes care of showing/hiding the collate option. | |
| 198 * @private | |
| 199 */ | |
| 200 showHideCollateOption_: function() { | |
| 201 this.collateOption_.hidden = this.numberOfCopies <= 1; | |
| 202 }, | |
| 203 | |
| 204 /* | |
| 205 * Takes care of showing/hiding the two sided option. | |
| 206 * @param {number} defaultDuplexValue Specifies the default duplex value. | |
| 207 * @private | |
| 208 */ | |
| 209 updateTwoSidedOption_: function(defaultDuplexValue) { | |
| 210 // On Windows, some printers don't specify their duplex values in the | |
| 211 // printer schema. If the printer duplex value is UNKNOWN_DUPLEX_MODE, | |
| 212 // hide the two sided option in preview tab UI. | |
| 213 // Ref bug: http://crbug.com/89204 | |
| 214 this.twoSidedOption_.hidden = | |
| 215 (defaultDuplexValue == CopiesSettings.UNKNOWN_DUPLEX_MODE); | |
| 216 | |
| 217 if (!this.twoSidedOption_.hidden) { | |
| 218 this.twoSidedCheckbox_.checked = !!defaultDuplexValue; | |
| 219 if (pageSettings.totalPageCount) | |
| 220 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 221 } | |
| 222 }, | |
| 223 | |
| 224 /** | |
| 225 * Updates the state of the increment/decrement buttons based on the current | |
| 226 * |textfield_| value. | |
| 227 * @private | |
| 228 */ | |
| 229 updateButtonsState_: function() { | |
| 230 if (!this.isValid()) { | |
| 231 this.textfield_.classList.add('invalid'); | |
| 232 this.incrementButton_.disabled = false; | |
| 233 this.decrementButton_.disabled = false; | |
| 234 fadeInElement(this.hint_); | |
| 235 } else { | |
| 236 this.textfield_.classList.remove('invalid'); | |
| 237 this.incrementButton_.disabled = this.numberOfCopies == this.maxValue_; | |
| 238 this.decrementButton_.disabled = this.numberOfCopies == this.minValue_; | |
| 239 fadeOutElement(this.hint_); | |
| 240 } | |
| 241 this.hint_.setAttribute('aria-hidden', this.isValid()); | |
| 242 } | |
| 243 }; | |
| 244 | |
| 245 return { | |
| 246 CopiesSettings: CopiesSettings | |
| 247 }; | |
| 248 }); | |
| OLD | NEW |