Re-connect search on MD downloads
[chromium-blink-merge.git] / chrome / browser / resources / md_downloads / toolbar.js
blob647778d647d8a868721d4b233c62dfa5c73ba549
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 cr.define('downloads', function() {
6   var Toolbar = Polymer({
7     is: 'downloads-toolbar',
9     /** @param {!downloads.ActionService} actionService */
10     setActionService: function(actionService) {
11       /** @private {!downloads.ActionService} */
12       this.actionService_ = actionService;
13     },
15     properties: {
16       downloadsShowing: {
17         type: Boolean,
18         value: false,
19         observer: 'onDownloadsShowingChange_',
20       },
22       showingSearch_: {
23         type: Boolean,
24         value: false,
25       },
26     },
28     /** @return {boolean} Whether removal can be undone. */
29     canUndo: function() {
30       return !this.$['search-term'].shadowRoot.activeElement;
31     },
33     /** @return {boolean} Whether "Clear all" should be allowed. */
34     canClearAll: function() {
35       return !this.$['search-term'].value && this.downloadsShowing;
36     },
38     ready: function() {
39       var term = this.$['search-term'];
40       term.addEventListener('input', this.onSearchTermInput_.bind(this));
41       term.addEventListener('keydown', this.onSearchTermKeydown_.bind(this));
42     },
44     /** @private */
45     onClearAllClick_: function() {
46       assert(this.canClearAll());
47       this.actionService_.clearAll();
48     },
50     /** @private */
51     onDownloadsShowingChange_: function() {
52       this.updateClearAll_();
53     },
55     /** @private */
56     onSearchTermInput_: function() {
57       this.actionService_.search(this.$['search-term'].value);
58       this.updateClearAll_();
59     },
61     /** @private */
62     onSearchTermKeydown_: function(e) {
63       assert(this.showingSearch_);
64       if (e.keyIdentifier == 'U+001B')  // Escape.
65         this.toggleShowingSearch_();
66     },
68     /** @private */
69     onOpenDownloadsFolderClick_: function() {
70       this.actionService_.openDownloadsFolder();
71     },
73     /** @private */
74     toggleShowingSearch_: function() {
75       this.showingSearch_ = !this.showingSearch_;
77       if (this.showingSearch_) {
78         this.$['search-term'].focus();
79       } else {
80         this.$['search-term'].value = '';
81         this.onSearchTermInput_();
82       }
83     },
85     /** @private */
86     updateClearAll_: function() {
87       this.$['clear-all'].hidden = !this.canClearAll();
88     },
89   });
91   return {Toolbar: Toolbar};
92 });