| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Draggable control for setting a page margin. | 9 * Draggable control for setting a page margin. |
| 10 * @param {!print_preview.ticket_items.CustomMargins.Orientation} orientation | 10 * @param {!print_preview.ticket_items.CustomMargins.Orientation} orientation |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 this.mouseStartPositionInPixels_ = null; | 66 this.mouseStartPositionInPixels_ = null; |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * Processing timeout for the textbox. | 69 * Processing timeout for the textbox. |
| 70 * @type {?number} | 70 * @type {?number} |
| 71 * @private | 71 * @private |
| 72 */ | 72 */ |
| 73 this.textTimeout_ = null; | 73 this.textTimeout_ = null; |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * Value of the textbox when the timeout was started. | |
| 77 * @type {?string} | |
| 78 * @private | |
| 79 */ | |
| 80 this.preTimeoutValue_ = null; | |
| 81 | |
| 82 /** | |
| 83 * Textbox used to display and receive the value of the margin. | 76 * Textbox used to display and receive the value of the margin. |
| 84 * @type {HTMLInputElement} | 77 * @type {HTMLInputElement} |
| 85 * @private | 78 * @private |
| 86 */ | 79 */ |
| 87 this.textbox_ = null; | 80 this.textbox_ = null; |
| 88 | 81 |
| 89 /** | 82 /** |
| 90 * Element of the margin control line. | 83 * Element of the margin control line. |
| 91 * @type {HTMLElement} | 84 * @type {HTMLElement} |
| 92 * @private | 85 * @private |
| (...skipping 26 matching lines...) Expand all Loading... |
| 119 // Dispatched when the text in the margin control's textbox changes. | 112 // Dispatched when the text in the margin control's textbox changes. |
| 120 TEXT_CHANGE: 'print_preview.MarginControl.TEXT_CHANGE' | 113 TEXT_CHANGE: 'print_preview.MarginControl.TEXT_CHANGE' |
| 121 }; | 114 }; |
| 122 | 115 |
| 123 /** | 116 /** |
| 124 * CSS classes used by this component. | 117 * CSS classes used by this component. |
| 125 * @enum {string} | 118 * @enum {string} |
| 126 * @private | 119 * @private |
| 127 */ | 120 */ |
| 128 MarginControl.Classes_ = { | 121 MarginControl.Classes_ = { |
| 129 TOP: 'margin-control-top', | |
| 130 RIGHT: 'margin-control-right', | |
| 131 BOTTOM: 'margin-control-bottom', | |
| 132 LEFT: 'margin-control-left', | |
| 133 TEXTBOX: 'margin-control-textbox', | 122 TEXTBOX: 'margin-control-textbox', |
| 134 INVALID: 'invalid', | |
| 135 INVISIBLE: 'invisible', | |
| 136 DISABLED: 'margin-control-disabled', | |
| 137 DRAGGING: 'margin-control-dragging', | 123 DRAGGING: 'margin-control-dragging', |
| 138 LINE: 'margin-control-line' | 124 LINE: 'margin-control-line' |
| 139 }; | 125 }; |
| 140 | 126 |
| 141 /** | 127 /** |
| 142 * Map from orientation to CSS class name. | |
| 143 * @type {!Object.< | |
| 144 * !print_preview.ticket_items.CustomMargins.Orientation, | |
| 145 * !MarginControl.Classes_>} | |
| 146 * @private | |
| 147 */ | |
| 148 MarginControl.OrientationToClass_ = {}; | |
| 149 MarginControl.OrientationToClass_[ | |
| 150 print_preview.ticket_items.CustomMargins.Orientation.TOP] = | |
| 151 MarginControl.Classes_.TOP; | |
| 152 MarginControl.OrientationToClass_[ | |
| 153 print_preview.ticket_items.CustomMargins.Orientation.RIGHT] = | |
| 154 MarginControl.Classes_.RIGHT; | |
| 155 MarginControl.OrientationToClass_[ | |
| 156 print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] = | |
| 157 MarginControl.Classes_.BOTTOM; | |
| 158 MarginControl.OrientationToClass_[ | |
| 159 print_preview.ticket_items.CustomMargins.Orientation.LEFT] = | |
| 160 MarginControl.Classes_.LEFT; | |
| 161 | |
| 162 /** | |
| 163 * Radius of the margin control in pixels. Padding of control + 1 for border. | 128 * Radius of the margin control in pixels. Padding of control + 1 for border. |
| 164 * @type {number} | 129 * @type {number} |
| 165 * @const | 130 * @const |
| 166 * @private | 131 * @private |
| 167 */ | 132 */ |
| 168 MarginControl.RADIUS_ = 9; | 133 MarginControl.RADIUS_ = 9; |
| 169 | 134 |
| 170 /** | 135 /** |
| 171 * Timeout after a text change after which the text in the textbox is saved to | 136 * Timeout after a text change after which the text in the textbox is saved to |
| 172 * the print ticket. Value in milliseconds. | 137 * the print ticket. Value in milliseconds. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 /** | 179 /** |
| 215 * @param {!print_preview.Size} pageSize New size of the document's pages. | 180 * @param {!print_preview.Size} pageSize New size of the document's pages. |
| 216 */ | 181 */ |
| 217 setPageSize: function(pageSize) { | 182 setPageSize: function(pageSize) { |
| 218 this.pageSize_ = pageSize; | 183 this.pageSize_ = pageSize; |
| 219 this.setPositionInPts(this.positionInPts_); | 184 this.setPositionInPts(this.positionInPts_); |
| 220 }, | 185 }, |
| 221 | 186 |
| 222 /** @param {boolean} isVisible Whether the margin control is visible. */ | 187 /** @param {boolean} isVisible Whether the margin control is visible. */ |
| 223 setIsVisible: function(isVisible) { | 188 setIsVisible: function(isVisible) { |
| 224 if (isVisible) { | 189 this.getElement().classList.toggle('invisible', !isVisible); |
| 225 this.getElement().classList.remove(MarginControl.Classes_.INVISIBLE); | |
| 226 } else { | |
| 227 this.getElement().classList.add(MarginControl.Classes_.INVISIBLE); | |
| 228 } | |
| 229 }, | 190 }, |
| 230 | 191 |
| 231 /** @return {boolean} Whether the margin control is in an error state. */ | 192 /** @return {boolean} Whether the margin control is in an error state. */ |
| 232 getIsInError: function() { | 193 getIsInError: function() { |
| 233 return this.isInError_; | 194 return this.isInError_; |
| 234 }, | 195 }, |
| 235 | 196 |
| 236 /** | 197 /** |
| 237 * @param {boolean} isInError Whether the margin control is in an error | 198 * @param {boolean} isInError Whether the margin control is in an error |
| 238 * state. | 199 * state. |
| 239 */ | 200 */ |
| 240 setIsInError: function(isInError) { | 201 setIsInError: function(isInError) { |
| 241 this.isInError_ = isInError; | 202 this.isInError_ = isInError; |
| 242 if (isInError) { | 203 this.textbox_.classList.toggle('invalid', isInError); |
| 243 this.textbox_.classList.add(MarginControl.Classes_.INVALID); | |
| 244 } else { | |
| 245 this.textbox_.classList.remove(MarginControl.Classes_.INVALID); | |
| 246 } | |
| 247 }, | 204 }, |
| 248 | 205 |
| 249 /** @param {boolean} isEnabled Whether to enable the margin control. */ | 206 /** @param {boolean} isEnabled Whether to enable the margin control. */ |
| 250 setIsEnabled: function(isEnabled) { | 207 setIsEnabled: function(isEnabled) { |
| 251 this.textbox_.disabled = !isEnabled; | 208 this.textbox_.disabled = !isEnabled; |
| 252 if (isEnabled) { | 209 this.getElement().classList.toggle('margin-control-disabled', !isEnabled); |
| 253 this.getElement().classList.remove(MarginControl.Classes_.DISABLED); | |
| 254 } else { | |
| 255 this.getElement().classList.add(MarginControl.Classes_.DISABLED); | |
| 256 } | |
| 257 }, | 210 }, |
| 258 | 211 |
| 259 /** @return {number} Current position of the margin control in points. */ | 212 /** @return {number} Current position of the margin control in points. */ |
| 260 getPositionInPts: function() { | 213 getPositionInPts: function() { |
| 261 return this.positionInPts_; | 214 return this.positionInPts_; |
| 262 }, | 215 }, |
| 263 | 216 |
| 264 /** | 217 /** |
| 265 * @param {number} posInPts New position of the margin control in points. | 218 * @param {number} posInPts New position of the margin control in points. |
| 266 */ | 219 */ |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 mousePosition.x - this.mouseStartPositionInPixels_.x + | 302 mousePosition.x - this.mouseStartPositionInPixels_.x + |
| 350 this.marginStartPositionInPixels_.x, | 303 this.marginStartPositionInPixels_.x, |
| 351 mousePosition.y - this.mouseStartPositionInPixels_.y + | 304 mousePosition.y - this.mouseStartPositionInPixels_.y + |
| 352 this.marginStartPositionInPixels_.y); | 305 this.marginStartPositionInPixels_.y); |
| 353 }, | 306 }, |
| 354 | 307 |
| 355 /** @override */ | 308 /** @override */ |
| 356 createDom: function() { | 309 createDom: function() { |
| 357 this.setElementInternal(this.cloneTemplateInternal( | 310 this.setElementInternal(this.cloneTemplateInternal( |
| 358 'margin-control-template')); | 311 'margin-control-template')); |
| 359 this.getElement().classList.add(MarginControl.OrientationToClass_[ | 312 this.getElement().classList.add('margin-control-' + this.orientation_); |
| 360 this.orientation_]); | |
| 361 this.textbox_ = this.getElement().getElementsByClassName( | 313 this.textbox_ = this.getElement().getElementsByClassName( |
| 362 MarginControl.Classes_.TEXTBOX)[0]; | 314 MarginControl.Classes_.TEXTBOX)[0]; |
| 315 this.textbox_.setAttribute( |
| 316 'aria-label', localStrings.getString(this.orientation_)); |
| 363 this.marginLineEl_ = this.getElement().getElementsByClassName( | 317 this.marginLineEl_ = this.getElement().getElementsByClassName( |
| 364 MarginControl.Classes_.LINE)[0]; | 318 MarginControl.Classes_.LINE)[0]; |
| 365 }, | 319 }, |
| 366 | 320 |
| 367 /** @override */ | 321 /** @override */ |
| 368 enterDocument: function() { | 322 enterDocument: function() { |
| 369 print_preview.Component.prototype.enterDocument.call(this); | 323 print_preview.Component.prototype.enterDocument.call(this); |
| 370 this.tracker.add( | 324 this.tracker.add( |
| 371 this.getElement(), 'mousedown', this.onMouseDown_.bind(this)); | 325 this.getElement(), 'mousedown', this.onMouseDown_.bind(this)); |
| 372 this.tracker.add( | 326 this.tracker.add( |
| 327 this.getElement(), |
| 328 'webkitTransitionEnd', |
| 329 this.onWebkitTransitionEnd_.bind(this)); |
| 330 this.tracker.add( |
| 331 this.textbox_, 'input', this.onTextboxInput_.bind(this)); |
| 332 this.tracker.add( |
| 373 this.textbox_, 'keydown', this.onTextboxKeyDown_.bind(this)); | 333 this.textbox_, 'keydown', this.onTextboxKeyDown_.bind(this)); |
| 374 this.tracker.add( | 334 this.tracker.add( |
| 375 this.textbox_, 'focus', this.setIsFocused_.bind(this, true)); | 335 this.textbox_, 'focus', this.setIsFocused_.bind(this, true)); |
| 376 this.tracker.add(this.textbox_, 'blur', this.onTexboxBlur_.bind(this)); | 336 this.tracker.add(this.textbox_, 'blur', this.onTexboxBlur_.bind(this)); |
| 377 }, | 337 }, |
| 378 | 338 |
| 379 /** @override */ | 339 /** @override */ |
| 380 exitDocument: function() { | 340 exitDocument: function() { |
| 381 print_preview.Component.prototype.exitDocument.call(this); | 341 print_preview.Component.prototype.exitDocument.call(this); |
| 382 this.textbox_ = null; | 342 this.textbox_ = null; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 404 this.mouseStartPositionInPixels_ = | 364 this.mouseStartPositionInPixels_ = |
| 405 new print_preview.Coordinate2d(event.x, event.y); | 365 new print_preview.Coordinate2d(event.x, event.y); |
| 406 this.marginStartPositionInPixels_ = new print_preview.Coordinate2d( | 366 this.marginStartPositionInPixels_ = new print_preview.Coordinate2d( |
| 407 this.getElement().offsetLeft, this.getElement().offsetTop); | 367 this.getElement().offsetLeft, this.getElement().offsetTop); |
| 408 this.setIsInError(false); | 368 this.setIsInError(false); |
| 409 cr.dispatchSimpleEvent(this, MarginControl.EventType.DRAG_START); | 369 cr.dispatchSimpleEvent(this, MarginControl.EventType.DRAG_START); |
| 410 } | 370 } |
| 411 }, | 371 }, |
| 412 | 372 |
| 413 /** | 373 /** |
| 374 * Called when opacity CSS transition ends. |
| 375 * @private |
| 376 */ |
| 377 onWebkitTransitionEnd_: function(event) { |
| 378 if (event.propertyName != 'opacity') |
| 379 return; |
| 380 var elStyle = window.getComputedStyle(this.getElement()); |
| 381 var opacity = parseInt(elStyle.getPropertyValue('opacity')); |
| 382 this.textbox_.setAttribute('aria-hidden', opacity == 0); |
| 383 }, |
| 384 |
| 385 /** |
| 386 * Called when textbox content changes. Starts text change timeout. |
| 387 * @private |
| 388 */ |
| 389 onTextboxInput_: function(event) { |
| 390 if (this.textTimeout_) { |
| 391 clearTimeout(this.textTimeout_); |
| 392 this.textTimeout_ = null; |
| 393 } |
| 394 this.textTimeout_ = setTimeout( |
| 395 this.onTextboxTimeout_.bind(this), MarginControl.TEXTBOX_TIMEOUT_); |
| 396 }, |
| 397 |
| 398 /** |
| 414 * Called when a key down event occurs on the textbox. Dispatches a | 399 * Called when a key down event occurs on the textbox. Dispatches a |
| 415 * TEXT_CHANGE event if the "Enter" key was pressed. | 400 * TEXT_CHANGE event if the "Enter" key was pressed. |
| 416 * @param {Event} event Contains the key that was pressed. | 401 * @param {Event} event Contains the key that was pressed. |
| 417 * @private | 402 * @private |
| 418 */ | 403 */ |
| 419 onTextboxKeyDown_: function(event) { | 404 onTextboxKeyDown_: function(event) { |
| 420 if (this.textTimeout_) { | 405 if (this.textTimeout_) { |
| 421 clearTimeout(this.textTimeout_); | 406 clearTimeout(this.textTimeout_); |
| 422 this.textTimeout_ = null; | 407 this.textTimeout_ = null; |
| 423 } | 408 } |
| 424 if (event.keyCode == 13 /*enter*/) { | 409 if (event.keyCode == 13 /*enter*/) { |
| 425 this.preTimeoutValue_ = null; | |
| 426 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); | 410 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); |
| 427 } else { | |
| 428 if (this.preTimeoutValue_ == null) { | |
| 429 this.preTimeoutValue_ = this.textbox_.value; | |
| 430 } | |
| 431 this.textTimeout_ = setTimeout( | |
| 432 this.onTextboxTimeout_.bind(this), MarginControl.TEXTBOX_TIMEOUT_); | |
| 433 } | 411 } |
| 434 }, | 412 }, |
| 435 | 413 |
| 436 /** | 414 /** |
| 437 * Called after a timeout after the text in the textbox has changed. Saves | 415 * Called after a timeout after the text in the textbox has changed. Saves |
| 438 * the textbox's value to the print ticket. | 416 * the textbox's value to the print ticket. |
| 439 * @private | 417 * @private |
| 440 */ | 418 */ |
| 441 onTextboxTimeout_: function() { | 419 onTextboxTimeout_: function() { |
| 442 this.textTimeout_ = null; | 420 this.textTimeout_ = null; |
| 443 if (this.textbox_.value != this.preTimeoutValue_) { | 421 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); |
| 444 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); | |
| 445 } | |
| 446 this.preTimeoutValue_ = null; | |
| 447 }, | 422 }, |
| 448 | 423 |
| 449 /** | 424 /** |
| 450 * Called when the textbox loses focus. Dispatches a TEXT_CHANGE event. | 425 * Called when the textbox loses focus. Dispatches a TEXT_CHANGE event. |
| 451 */ | 426 */ |
| 452 onTexboxBlur_: function() { | 427 onTexboxBlur_: function() { |
| 453 if (this.textTimeout_) { | 428 if (this.textTimeout_) { |
| 454 clearTimeout(this.textTimeout_); | 429 clearTimeout(this.textTimeout_); |
| 455 this.textTimeout_ = null; | 430 this.textTimeout_ = null; |
| 456 this.preTimeoutValue_ = null; | |
| 457 } | 431 } |
| 458 this.setIsFocused_(false); | 432 this.setIsFocused_(false); |
| 459 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); | 433 cr.dispatchSimpleEvent(this, MarginControl.EventType.TEXT_CHANGE); |
| 460 } | 434 } |
| 461 }; | 435 }; |
| 462 | 436 |
| 463 // Export | 437 // Export |
| 464 return { | 438 return { |
| 465 MarginControl: MarginControl | 439 MarginControl: MarginControl |
| 466 }; | 440 }; |
| 467 }); | 441 }); |
| OLD | NEW |