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 MarginSettings object. This object encapsulates all settings and |
| 10 * logic related to the margins mode. |
| 11 * |
| 12 * @param {!print_preview.PrintTicketStore} printTicketStore Used to get |
| 13 * ticket margins value. |
| 14 * @constructor |
| 15 * @extends {print_preview.Component} |
| 16 */ |
| 17 function MarginSettings(printTicketStore) { |
| 18 print_preview.Component.call(this); |
| 19 |
| 20 /** |
| 21 * Used to get ticket margins value. |
| 22 * @type {!print_preview.PrintTicketStore} |
| 23 * @private |
| 24 */ |
| 25 this.printTicketStore_ = printTicketStore; |
| 26 |
| 27 /** |
| 28 * @private |
| 29 */ |
| 30 this.marginsUI_ = null; |
| 31 |
| 32 /** |
| 33 * Holds the custom margin values in points (if set). |
| 34 * @type {print_preview.Margins} |
| 35 * @private |
| 36 */ |
| 37 this.customMargins_ = null; |
| 38 |
| 39 /** |
| 40 * Holds the previous custom margin values in points. |
| 41 * @type {print_preview.Margins} |
| 42 * @private |
| 43 */ |
| 44 this.previousCustomMargins_ = null; |
| 45 |
| 46 /** |
| 47 * Holds the width of the page in points. |
| 48 * @type {number} |
| 49 * @private |
| 50 */ |
| 51 this.pageWidth_ = -1; |
| 52 |
| 53 /** |
| 54 * Holds the height of the page in points. |
| 55 * @type {number} |
| 56 * @private |
| 57 */ |
| 58 this.pageHeight_ = -1; |
| 59 |
| 60 /** |
| 61 * True if the margins UI should be diplayed when the next |
| 62 * |customEvents.PDF_LOADED| event occurs. |
| 63 * @type {boolean} |
| 64 * @private |
| 65 */ |
| 66 this.forceMarginsUIOnPDFLoad_ = false; |
| 67 |
| 68 /** |
| 69 * Holds the currently updated default page layout values. |
| 70 * @type {print_preview.PageLayout} |
| 71 * @private |
| 72 */ |
| 73 this.currentDefaultPageLayout = null; |
| 74 |
| 75 /** |
| 76 * True if the margins UI should be shown regardless of mouse position. |
| 77 * @private |
| 78 */ |
| 79 this.forceDisplayingMarginLines_ = true; |
| 80 }; |
| 81 |
| 82 /** |
| 83 * Minimum allowed distance in points between top-bottom, left-right margins. |
| 84 * @type {number} |
| 85 * @private |
| 86 */ |
| 87 MarginSettings.MINIMUM_MARGINS_DISTANCE_ = 36; |
| 88 |
| 89 /** |
| 90 * Default Margins option index. |
| 91 * @type {string} |
| 92 * @private |
| 93 */ |
| 94 MarginSettings.DEFAULT_MARGINS_VALUE_ = 0; |
| 95 |
| 96 /** |
| 97 * CSS classes used by the margin settings component. |
| 98 * @enum {string} |
| 99 * @private |
| 100 */ |
| 101 MarginSettings.Classes_ = { |
| 102 SELECT: 'margin-settings-select' |
| 103 }; |
| 104 |
| 105 MarginSettings.prototype = { |
| 106 __proto__: print_preview.Component.prototype, |
| 107 |
| 108 set isEnabled(isEnabled) { |
| 109 this.select_.disabled = !isEnabled; |
| 110 }, |
| 111 |
| 112 /** @override */ |
| 113 enterDocument: function() { |
| 114 print_preview.Component.prototype.enterDocument.call(this); |
| 115 this.tracker.add( |
| 116 this.select_, 'change', this.onSelectChange_.bind(this)); |
| 117 this.tracker.add( |
| 118 this.printTicketStore_, |
| 119 print_preview.PrintTicketStore.Event.DOCUMENT_CHANGE, |
| 120 this.onPrintTicketStoreChange_.bind(this)); |
| 121 this.tracker.add( |
| 122 this.printTicketStore_, |
| 123 print_preview.PrintTicketStore.Event.TICKET_CHANGE, |
| 124 this.onPrintTicketStoreChange_.bind(this)); |
| 125 this.tracker.add( |
| 126 this.printTicketStore_, |
| 127 print_preview.PrintTicketStore.Event.CAPABILITIES_CHANGE, |
| 128 this.onPrintTicketStoreChange_.bind(this)); |
| 129 }, |
| 130 |
| 131 /** |
| 132 * @return {HTMLSelectElement} Select element containing the margin options. |
| 133 * @private |
| 134 */ |
| 135 get select_() { |
| 136 return this.getElement().getElementsByClassName( |
| 137 MarginSettings.Classes_.SELECT)[0]; |
| 138 }, |
| 139 |
| 140 /** |
| 141 * Called when the select element is changed. Updates the print ticket |
| 142 * margin type. |
| 143 * @private |
| 144 */ |
| 145 onSelectChange_: function() { |
| 146 var select = this.select_; |
| 147 var marginsType = /** @type {print_preview.Margins.Type} */ ( |
| 148 select.options[select.selectedIndex].value); |
| 149 this.printTicketStore_.updateMarginsType(marginsType); |
| 150 }, |
| 151 |
| 152 /** |
| 153 * Called when the print ticket store changes. |
| 154 * @private |
| 155 */ |
| 156 onPrintTicketStoreChange_: function() { |
| 157 if (this.printTicketStore_.hasMarginsCapability()) { |
| 158 var select = this.select_; |
| 159 var marginsType = this.printTicketStore_.getMarginsType(); |
| 160 var selectedOption = select.options[select.selectedIndex]; |
| 161 if (marginsType != selectedOption.value) { |
| 162 selectedOption.selected = false; |
| 163 for (var option, i = 0; option = select.options[i]; i++) { |
| 164 if (option.value == marginsType) { |
| 165 option.selected = true; |
| 166 break; |
| 167 } |
| 168 } |
| 169 } |
| 170 fadeInOption(this.getElement()); |
| 171 } else { |
| 172 fadeOutOption(this.getElement()); |
| 173 } |
| 174 } |
| 175 }; |
| 176 |
| 177 // Export |
| 178 return { |
| 179 MarginSettings: MarginSettings |
| 180 }; |
| 181 }); |
OLD | NEW |