Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 (function() { | |
| 2 'use strict'; | |
| 3 | |
| 4 // Keeps track of the toast currently opened. | |
| 5 var currentToast = null; | |
| 6 | |
| 7 Polymer({ | |
| 8 is: 'paper-toast', | |
| 9 | |
| 10 behaviors: [ | |
| 11 Polymer.IronOverlayBehavior | |
|
tsergeant
2017/05/26 05:19:24
Adding a dependency on IronOverlayBehavior is a bi
Dan Beam
2017/05/30 23:25:31
+1, why do we need this?
dpapad
2017/05/30 23:33:39
Would it be too hard to roll our own toast now? In
calamity
2017/05/31 08:10:42
Rolled.
| |
| 12 ], | |
| 13 | |
| 14 properties: { | |
| 15 /** | |
| 16 * The element to fit `this` into. | |
| 17 * Overridden from `Polymer.IronFitBehavior`. | |
| 18 */ | |
| 19 fitInto: { | |
| 20 type: Object, | |
| 21 value: window, | |
| 22 observer: '_onFitIntoChanged' | |
| 23 }, | |
| 24 | |
| 25 /** | |
| 26 * The orientation against which to align the dropdown content | |
| 27 * horizontally relative to `positionTarget`. | |
| 28 * Overridden from `Polymer.IronFitBehavior`. | |
| 29 */ | |
| 30 horizontalAlign: { | |
| 31 type: String, | |
| 32 value: 'left' | |
| 33 }, | |
| 34 | |
| 35 /** | |
| 36 * The orientation against which to align the dropdown content | |
| 37 * vertically relative to `positionTarget`. | |
| 38 * Overridden from `Polymer.IronFitBehavior`. | |
| 39 */ | |
| 40 verticalAlign: { | |
| 41 type: String, | |
| 42 value: 'bottom' | |
| 43 }, | |
| 44 | |
| 45 /** | |
| 46 * The duration in milliseconds to show the toast. | |
| 47 * Set to `0`, a negative number, or `Infinity`, to disable the | |
| 48 * toast auto-closing. | |
| 49 */ | |
| 50 duration: { | |
| 51 type: Number, | |
| 52 value: 3000 | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * The text to display in the toast. | |
| 57 */ | |
| 58 text: { | |
| 59 type: String, | |
| 60 value: '' | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * Overridden from `IronOverlayBehavior`. | |
| 65 * Set to false to enable closing of the toast by clicking outside it. | |
| 66 */ | |
| 67 noCancelOnOutsideClick: { | |
| 68 type: Boolean, | |
| 69 value: true | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * Overridden from `IronOverlayBehavior`. | |
| 74 * Set to true to disable auto-focusing the toast or child nodes with | |
| 75 * the `autofocus` attribute` when the overlay is opened. | |
| 76 */ | |
| 77 noAutoFocus: { | |
| 78 type: Boolean, | |
| 79 value: true | |
| 80 } | |
| 81 }, | |
| 82 | |
| 83 listeners: { | |
| 84 'transitionend': '__onTransitionEnd' | |
| 85 }, | |
| 86 | |
| 87 /** | |
| 88 * Read-only. Deprecated. Use `opened` from `IronOverlayBehavior`. | |
| 89 * @property visible | |
| 90 * @deprecated | |
| 91 */ | |
| 92 get visible() { | |
| 93 Polymer.Base._warn('`visible` is deprecated, use `opened` instead'); | |
| 94 return this.opened; | |
| 95 }, | |
| 96 | |
| 97 /** | |
| 98 * Read-only. Can auto-close if duration is a positive finite number. | |
| 99 * @property _canAutoClose | |
| 100 */ | |
| 101 get _canAutoClose() { | |
| 102 return this.duration > 0 && this.duration !== Infinity; | |
| 103 }, | |
| 104 | |
| 105 created: function() { | |
| 106 this._autoClose = null; | |
| 107 Polymer.IronA11yAnnouncer.requestAvailability(); | |
| 108 }, | |
| 109 | |
| 110 /** | |
| 111 * Show the toast. Without arguments, this is the same as `open()` from `IronOverlayBehavior`. | |
| 112 * @param {(Object|string)=} properties Properties to be set before open ing the toast. | |
| 113 * e.g. `toast.show('hello')` or `toast.show({text: 'hello', duration: 3 000})` | |
| 114 */ | |
| 115 show: function(properties) { | |
| 116 if (typeof properties == 'string') { | |
| 117 properties = { text: properties }; | |
| 118 } | |
| 119 for (var property in properties) { | |
| 120 if (property.indexOf('_') === 0) { | |
| 121 Polymer.Base._warn('The property "' + property + '" is private and was not set.'); | |
| 122 } else if (property in this) { | |
| 123 this[property] = properties[property]; | |
| 124 } else { | |
| 125 Polymer.Base._warn('The property "' + property + '" is not valid.' ); | |
| 126 } | |
| 127 } | |
| 128 this.open(); | |
| 129 }, | |
| 130 | |
| 131 /** | |
| 132 * Hide the toast. Same as `close()` from `IronOverlayBehavior`. | |
| 133 */ | |
| 134 hide: function() { | |
| 135 this.close(); | |
| 136 }, | |
| 137 | |
| 138 /** | |
| 139 * Called on transitions of the toast, indicating a finished animation | |
| 140 * @private | |
| 141 */ | |
| 142 __onTransitionEnd: function(e) { | |
| 143 // there are different transitions that are happening when opening and | |
| 144 // closing the toast. The last one so far is for `opacity`. | |
| 145 // This marks the end of the transition, so we check for this to deter mine if this | |
| 146 // is the correct event. | |
| 147 if (e && e.target === this && e.propertyName === 'opacity') { | |
| 148 if (this.opened) { | |
| 149 this._finishRenderOpened(); | |
| 150 } else { | |
| 151 this._finishRenderClosed(); | |
| 152 } | |
| 153 } | |
| 154 }, | |
| 155 | |
| 156 /** | |
| 157 * Overridden from `IronOverlayBehavior`. | |
| 158 * Called when the value of `opened` changes. | |
| 159 */ | |
| 160 _openedChanged: function() { | |
| 161 if (this._autoClose !== null) { | |
| 162 this.cancelAsync(this._autoClose); | |
| 163 this._autoClose = null; | |
| 164 } | |
| 165 if (this.opened) { | |
| 166 if (currentToast && currentToast !== this) { | |
| 167 currentToast.close(); | |
| 168 } | |
| 169 currentToast = this; | |
| 170 this.fire('iron-announce', { | |
| 171 text: this.text | |
| 172 }); | |
| 173 if (this._canAutoClose) { | |
| 174 this._autoClose = this.async(this.close, this.duration); | |
| 175 } | |
| 176 } else if (currentToast === this) { | |
| 177 currentToast = null; | |
| 178 } | |
| 179 Polymer.IronOverlayBehaviorImpl._openedChanged.apply(this, arguments); | |
| 180 }, | |
| 181 | |
| 182 /** | |
| 183 * Overridden from `IronOverlayBehavior`. | |
| 184 */ | |
| 185 _renderOpened: function() { | |
| 186 this.classList.add('paper-toast-open'); | |
| 187 }, | |
| 188 | |
| 189 /** | |
| 190 * Overridden from `IronOverlayBehavior`. | |
| 191 */ | |
| 192 _renderClosed: function() { | |
| 193 this.classList.remove('paper-toast-open'); | |
| 194 }, | |
| 195 | |
| 196 /** | |
| 197 * @private | |
| 198 */ | |
| 199 _onFitIntoChanged: function(fitInto) { | |
| 200 this.positionTarget = fitInto; | |
| 201 } | |
| 202 | |
| 203 /** | |
| 204 * Fired when `paper-toast` is opened. | |
| 205 * | |
| 206 * @event 'iron-announce' | |
| 207 * @param {{text: string}} detail Contains text that will be announced. | |
| 208 */ | |
| 209 }); | |
| 210 })(); | |
| OLD | NEW |