Refactor Material Design PDF Viewer toolbar into a single element
[chromium-blink-merge.git] / chrome / browser / resources / pdf / elements / viewer-page-selector / viewer-page-selector.js
blob97713d53eb84e6d5e8c634ef1c63d95f6bd107f7
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
3 // found in the LICENSE file.
5 var DIGIT_LENGTH = 0.6;
7 Polymer('viewer-page-selector', {
8   /**
9    * @type {string}
10    * The current entry in the input field (1-based).
11    */
12   pageNo: '1',
14   /**
15    * @type {number}
16    * The index of the current page being viewed (0-based).
17    */
18   index: 0,
20   /**
21    * @type {number}
22    * The number of pages the document contains.
23    */
24   docLength: 1,
26   /**
27    * @type {string}
28    * The submitted input from the user (1-based).
29    */
30   chosenPageNo: '1',
32   chosenPageNoChanged: function() {
33     var page = parseInt(this.chosenPageNo);
34     if (!isNaN(page)) {
35       this.fire('change-page', {page: page - 1});
36     } else {
37       // Repopulate the input.
38       this.indexChanged();
39       // Change the chosenPageNo so if it is '' again we can repopulate it.
40       this.chosenPageNo = this.pageNo;
41     }
42   },
44   indexChanged: function() {
45     this.pageNo = String(this.index + 1);
46   },
48   docLengthChanged: function() {
49     var numDigits = this.docLength.toString().length;
50     this.$.pageselector.style.width = (numDigits * DIGIT_LENGTH) + 'em';
51   },
53   select: function() {
54     this.$.input.select();
55   }
56 });