OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 /** |
| 6 * @fileoverview Element which shows toasts. |
| 7 */ |
| 8 cr.define('bookmarks', function() { |
| 9 |
| 10 var ToastManager = Polymer({ |
| 11 is: 'bookmarks-toast-manager', |
| 12 |
| 13 properties: { |
| 14 duration: { |
| 15 type: Number, |
| 16 value: 0, |
| 17 }, |
| 18 |
| 19 /** @private */ |
| 20 open_: { |
| 21 type: Boolean, |
| 22 reflectToAttribute: true, |
| 23 }, |
| 24 |
| 25 /** @private */ |
| 26 showUndo_: Boolean, |
| 27 }, |
| 28 |
| 29 /** @private {function(number)} */ |
| 30 clearTimeout_: window.clearTimeout.bind(window), |
| 31 |
| 32 /** @private {function((Function|null|string), number)} */ |
| 33 setTimeout_: window.setTimeout.bind(window), |
| 34 |
| 35 /** @private {number|null} */ |
| 36 hideTimeout_: null, |
| 37 |
| 38 /** @override */ |
| 39 attached: function() { |
| 40 assert(ToastManager.instance_ == null); |
| 41 ToastManager.instance_ = this; |
| 42 }, |
| 43 |
| 44 /** @override */ |
| 45 detached: function() { |
| 46 ToastManager.instance_ = null; |
| 47 }, |
| 48 |
| 49 /** |
| 50 * @param {string} label The label to display inside the toast. |
| 51 * @param {boolean} showUndo Whether the undo button should be shown. |
| 52 */ |
| 53 show: function(label, showUndo) { |
| 54 this.open_ = true; |
| 55 // TODO(calamity): Support collapsing of long bookmark names in label. |
| 56 this.$.content.textContent = label; |
| 57 this.showUndo_ = showUndo; |
| 58 |
| 59 if (!this.duration) |
| 60 return; |
| 61 |
| 62 if (this.hideTimeout_ != null) { |
| 63 this.clearTimeout_(this.hideTimeout_); |
| 64 this.hideTimeout_ = null; |
| 65 } |
| 66 |
| 67 this.hideTimeout_ = this.setTimeout_(function() { |
| 68 this.open_ = false; |
| 69 this.hideTimeout_ = null; |
| 70 }.bind(this), this.duration); |
| 71 }, |
| 72 |
| 73 hide: function() { |
| 74 this.open_ = false; |
| 75 }, |
| 76 |
| 77 /** @private */ |
| 78 onUndoTap_: function() { |
| 79 // Will hide the toast. |
| 80 this.fire('command-undo'); |
| 81 }, |
| 82 }); |
| 83 |
| 84 /** @private {?bookmarks.ToastManager} */ |
| 85 ToastManager.instance_ = null; |
| 86 |
| 87 /** @return {!bookmarks.ToastManager} */ |
| 88 ToastManager.getInstance = function() { |
| 89 return assert(ToastManager.instance_); |
| 90 }; |
| 91 |
| 92 return { |
| 93 ToastManager: ToastManager, |
| 94 }; |
| 95 }); |
OLD | NEW |