| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 Polymer({ | 5 Polymer({ |
| 6 is: 'viewer-page-selector', | 6 is: 'viewer-page-selector', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 /** | 9 /** |
| 10 * The number of pages the document contains. | 10 * The number of pages the document contains. |
| 11 */ | 11 */ |
| 12 docLength: { | 12 docLength: { |
| 13 type: Number, | 13 type: Number, |
| 14 value: 1, | 14 value: 1, |
| 15 observer: 'docLengthChanged' | 15 observer: 'docLengthChanged' |
| 16 }, | 16 }, |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * The current page being viewed (1-based). | 19 * The current page being viewed (1-based). A change to pageNo is mirrored |
| 20 * immediately to the input field. A change to the input field is not |
| 21 * mirrored back until pageNoCommitted() is called and change-page is fired. |
| 20 */ | 22 */ |
| 21 pageNo: { | 23 pageNo: { |
| 22 type: String, | 24 type: Number, |
| 23 value: '1' | 25 value: 1 |
| 24 } | 26 } |
| 25 }, | 27 }, |
| 26 | 28 |
| 27 pageNoCommitted: function() { | 29 pageNoCommitted: function() { |
| 28 var page = parseInt(this.pageNo); | 30 var page = parseInt(this.$.input.value); |
| 29 if (!isNaN(page)) { | 31 |
| 32 if (!isNaN(page) && page <= this.docLength && page > 0) |
| 30 this.fire('change-page', {page: page - 1}); | 33 this.fire('change-page', {page: page - 1}); |
| 31 this.$.input.blur(); | 34 else |
| 32 } | 35 this.$.input.value = this.pageNo; |
| 36 this.$.input.blur(); |
| 33 }, | 37 }, |
| 34 | 38 |
| 35 docLengthChanged: function() { | 39 docLengthChanged: function() { |
| 36 var numDigits = this.docLength.toString().length; | 40 var numDigits = this.docLength.toString().length; |
| 37 this.$.pageselector.style.width = numDigits + 'ch'; | 41 this.$.pageselector.style.width = numDigits + 'ch'; |
| 38 // Set both sides of the slash to the same width, so that the layout is | 42 // Set both sides of the slash to the same width, so that the layout is |
| 39 // exactly centered. | 43 // exactly centered. |
| 40 this.$['pagelength-spacer'].style.width = numDigits + 'ch'; | 44 this.$['pagelength-spacer'].style.width = numDigits + 'ch'; |
| 41 }, | 45 }, |
| 42 | 46 |
| 43 select: function() { | 47 select: function() { |
| 44 this.$.input.select(); | 48 this.$.input.select(); |
| 45 }, | 49 }, |
| 46 | 50 |
| 47 /** | 51 /** |
| 48 * @return {boolean} True if the selector input field is currently focused. | 52 * @return {boolean} True if the selector input field is currently focused. |
| 49 */ | 53 */ |
| 50 isActive: function() { | 54 isActive: function() { |
| 51 return this.shadowRoot.activeElement == this.$.input; | 55 return this.shadowRoot.activeElement == this.$.input; |
| 52 } | 56 } |
| 53 }); | 57 }); |
| OLD | NEW |