]> git.proxmox.com Git - extjs.git/blame - extjs/modern/modern/src/Toast.js
add extjs 6.0.1 sources
[extjs.git] / extjs / modern / modern / src / Toast.js
CommitLineData
6527f429
DM
1/**\r
2 * A 'Toast' is a simple modal message that is displayed on the screen and then automatically closed by a timeout or by a user tapping\r
3 * outside of the toast itself. Think about it like a text only alert box that will self destruct. **A Toast should not be instantiated manually**\r
4 * but creating by calling 'Ext.toast(message, timeout)'. This will create one reusable toast container and content will be swapped out as\r
5 * toast messages are queued or displayed.\r
6 *\r
7 * # Simple Toast\r
8 *\r
9 * @example miniphone\r
10 * Ext.toast('Hello Sencha!'); // Toast will close in 1000 milliseconds (default)\r
11 *\r
12 * # Toast with Timeout\r
13 *\r
14 * @example miniphone\r
15 * Ext.toast('Hello Sencha!', 5000); // Toast will close in 5000 milliseconds\r
16 *\r
17 * # Toast with config\r
18 *\r
19 * @example miniphone\r
20 * Ext.toast({message: 'Hello Sencha!', timeout: 2000}); // Toast will close in 2000 milliseconds\r
21 *\r
22 * # Multiple Toasts queued\r
23 *\r
24 * @example miniphone\r
25 * Ext.toast('Hello Sencha!');\r
26 * Ext.toast('Hello Sencha Again!');\r
27 * Ext.toast('Hello Sencha One More Time!');\r
28 */\r
29Ext.define('Ext.Toast', {\r
30 extend: 'Ext.Sheet',\r
31 requires: [\r
32 'Ext.util.InputBlocker'\r
33 ],\r
34\r
35 config: {\r
36 /**\r
37 * @cfg\r
38 * @inheritdoc\r
39 */\r
40 ui: 'dark',\r
41\r
42 /**\r
43 * @cfg\r
44 * @inheritdoc\r
45 */\r
46 baseCls: Ext.baseCSSPrefix + 'toast',\r
47\r
48 /**\r
49 * @cfg\r
50 * @inheritdoc\r
51 */\r
52 showAnimation: {\r
53 type: 'popIn',\r
54 duration: 250,\r
55 easing: 'ease-out'\r
56 },\r
57\r
58 /**\r
59 * @cfg\r
60 * @inheritdoc\r
61 */\r
62 hideAnimation: {\r
63 type: 'popOut',\r
64 duration: 250,\r
65 easing: 'ease-out'\r
66 },\r
67\r
68 /**\r
69 * Override the default `zIndex` so it is normally always above floating components.\r
70 */\r
71 zIndex: 999,\r
72\r
73 /**\r
74 * @cfg {String} message\r
75 * The message to be displayed in the {@link Ext.Toast}.\r
76 * @accessor\r
77 */\r
78 message: null,\r
79\r
80 /**\r
81 * @cfg {Number} timeout\r
82 * The amount of time in milliseconds to wait before destroying the toast automatically\r
83 */\r
84 timeout: 1000,\r
85\r
86 /**\r
87 * @cfg{Boolean/Object} animation\r
88 * The animation that should be used between toast messages when they are queued up\r
89 */\r
90 messageAnimation: true,\r
91\r
92 /**\r
93 * @cfg\r
94 * @inheritdoc\r
95 */\r
96 hideOnMaskTap: true,\r
97\r
98 /**\r
99 * @private\r
100 */\r
101 modal: true,\r
102\r
103 /**\r
104 * @cfg\r
105 * @inheritdoc\r
106 */\r
107 layout: {\r
108 type: 'vbox',\r
109 pack: 'center'\r
110 }\r
111 },\r
112\r
113 /**\r
114 * @private\r
115 */\r
116 applyMessage: function(config) {\r
117 config = {\r
118 html: config,\r
119 cls: this.getBaseCls() + '-text'\r
120 };\r
121\r
122 return Ext.factory(config, Ext.Component, this._message);\r
123 },\r
124\r
125 /**\r
126 * @private\r
127 */\r
128 updateMessage: function(newMessage) {\r
129 if (newMessage) {\r
130 this.add(newMessage);\r
131 }\r
132 },\r
133\r
134 /**\r
135 * @private\r
136 */\r
137 applyTimeout: function(timeout) {\r
138 if (this._timeoutID) {\r
139 clearTimeout(this._timeoutID);\r
140 if (!Ext.isEmpty(timeout)) {\r
141 this._timeoutID = setTimeout(Ext.bind(this.onTimeout, this), timeout);\r
142 }\r
143 }\r
144 return timeout;\r
145 },\r
146\r
147 /**\r
148 * @internal\r
149 */\r
150 next: Ext.emptyFn,\r
151\r
152 /**\r
153 * @private\r
154 */\r
155 show: function(config) {\r
156 var me = this,\r
157 timeout = config.timeout,\r
158 msgAnimation = me.getMessageAnimation(),\r
159 message = me.getMessage();\r
160\r
161 if (me.isRendered() && me.isHidden() === false) {\r
162 config.timeout = null;\r
163 message.onAfter({\r
164 hiddenchange: function() {\r
165 me.setMessage(config.message);\r
166 message = me.getMessage();\r
167 message.onAfter({\r
168 hiddenchange: function() {\r
169\r
170 // Forces applyTimeout to create a timer\r
171 this._timeoutID = true;\r
172 me.setTimeout(timeout);\r
173 },\r
174 scope: me,\r
175 single: true\r
176 });\r
177 message.show(msgAnimation);\r
178 },\r
179 scope: me,\r
180 single: true\r
181 });\r
182\r
183 message.hide(msgAnimation);\r
184 } else {\r
185 Ext.util.InputBlocker.blockInputs();\r
186 me.setConfig(config);\r
187\r
188 //if it has not been added to a container, add it to the Viewport.\r
189 if (!me.getParent() && Ext.Viewport) {\r
190 Ext.Viewport.add(me);\r
191 }\r
192\r
193 if (!Ext.isEmpty(timeout)) {\r
194 me._timeoutID = setTimeout(Ext.bind(me.onTimeout, me), timeout);\r
195 }\r
196\r
197 me.callParent(arguments);\r
198 }\r
199 },\r
200\r
201 /**\r
202 * @private\r
203 */\r
204 hide: function(animation) {\r
205 clearTimeout(this._timeoutID);\r
206 if (!this.next()) {\r
207 this.callParent(arguments);\r
208 }\r
209 },\r
210\r
211 /**\r
212 * @private\r
213 */\r
214 onTimeout: function() {\r
215 this.hide();\r
216 }\r
217}, function(Toast) {\r
218 var _queue = [], _isToasting = false;\r
219\r
220 function next() {\r
221 var config = _queue.shift();\r
222\r
223 if (config) {\r
224 _isToasting = true;\r
225 this.show(config);\r
226 } else {\r
227 _isToasting = false;\r
228 }\r
229\r
230 return _isToasting;\r
231 }\r
232\r
233 function getInstance() {\r
234 if (!Ext.Toast._instance) {\r
235 Ext.Toast._instance = Ext.create('Ext.Toast');\r
236 Ext.Toast._instance.next = next;\r
237 }\r
238 return Ext.Toast._instance;\r
239 }\r
240\r
241 Ext.toast = function(message, timeout) {\r
242 var toast = getInstance(),\r
243 config = message;\r
244\r
245 if (Ext.isString(message)) {\r
246 config = {\r
247 message: message,\r
248 timeout: timeout\r
249 };\r
250 }\r
251\r
252 if (config.timeout === undefined) {\r
253 config.timeout = Ext.Toast.prototype.config.timeout;\r
254 }\r
255\r
256 _queue.push(config);\r
257 if (!_isToasting) {\r
258 toast.next();\r
259 }\r
260\r
261 return toast;\r
262 }\r
263});\r
264\r