Debian Codegraph @

dojox/widget/Toaster.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare", // declare
3 "dojo/_base/lang", // lang.getObject...
4 "dojo/on",
5 "dojo/aspect",
6 "dojo/topic",
7 "dojo/_base/fx", // fx.fadeOut
8 "dojo/dom-style", // domStyle.set
9 "dojo/dom-class", // domClass.add
10 "dojo/dom-geometry", // domGeometry.getMarginBox
11 "dijit/registry", // registry.getUniqueId()
12 "dijit/_WidgetBase",
13 "dijit/_TemplatedMixin",
14 "dijit/BackgroundIframe",
15 "dojo/fx",
16 "dojo/has",
17 "dojo/window"
18 ], function(declare, lang, on, aspect, topic, baseFx, domStyle, domClass, domGeometry, registry, _WidgetBase, _TemplatedMixin, BackgroundIframe, coreFx, has, window){
19
20 lang.getObject("dojox.widget", true);
21
22 var capitalize = function(/* String */w){
23 return w.substring(0,1).toUpperCase() + w.substring(1);
24 };
25
26 return declare("dojox.widget.Toaster", [_WidgetBase, _TemplatedMixin], {
27 // summary:
28 // Message that slides in from the corner of the screen, used for notifications
29 // like "new email".
30
31 templateString: '<div class="dijitToasterClip" data-dojo-attach-point="clipNode"><div class="dijitToasterContainer" data-dojo-attach-point="containerNode" data-dojo-attach-event="onclick:onSelect"><div class="dijitToasterContent" data-dojo-attach-point="contentNode"></div></div></div>',
32
33 // messageTopic: String
34 // Name of topic; anything published to this topic will be displayed as a message.
35 // Message format is either String or an object like
36 // {message: "hello word", type: "error", duration: 500}
37 messageTopic: "",
38
39 // messageTypes: Enumeration
40 // Possible message types.
41 messageTypes: {
42 MESSAGE: "message",
43 WARNING: "warning",
44 ERROR: "error",
45 FATAL: "fatal"
46 },
47
48 // defaultType: String
49 // If message type isn't specified (see "messageTopic" parameter),
50 // then display message as this type.
51 // Possible values in messageTypes enumeration ("message", "warning", "error", "fatal")
52 defaultType: "message",
53
54 // positionDirection: String
55 // Position from which message slides into screen, one of
56 // ["br-up", "br-left", "bl-up", "bl-right", "tr-down", "tr-left", "tl-down", "tl-right"]
57 positionDirection: "br-up",
58
59 // positionDirectionTypes: Array
60 // Possible values for positionDirection parameter
61 positionDirectionTypes: ["br-up", "br-left", "bl-up", "bl-right", "tr-down", "tr-left", "tl-down", "tl-right"],
62
63 // duration: Integer
64 // Number of milliseconds to show message
65 duration: 2000,
66
67 // slideDuration: Integer
68 // Number of milliseconds for the slide animation, increasing will cause the Toaster
69 // to slide in more slowly.
70 slideDuration: 500,
71
72 // separator: String
73 // String used to separate messages if consecutive calls are made to setContent before previous messages go away
74 separator: "<hr>",
75
76 postCreate: function(){
77 this.inherited(arguments);
78 this.hide();
79
80 // place node as a child of body for positioning
81 this.ownerDocument.body.appendChild(this.domNode);
82
83 if(this.messageTopic){
84 this.own(topic.subscribe(this.messageTopic, lang.hitch(this, "_handleMessage")));
85 }
86 },
87
88 _handleMessage: function(/*String|Object*/message){
89 if(lang.isString(message)){
90 this.setContent(message);
91 }else{
92 this.setContent(message.message, message.type, message.duration);
93 }
94 },
95
96 setContent: function(/*String|Function*/message, /*String*/messageType, /*int?*/duration){
97 // summary:
98 // sets and displays the given message and show duration
99 // message:
100 // the message. If this is a function, it will be called with this toaster widget as the only argument.
101 // messageType:
102 // type of message; possible values in messageTypes enumeration ("message", "warning", "error", "fatal")
103 // duration:
104 // duration in milliseconds to display message before removing it. Widget has default value.
105
106 duration = (duration === undefined) ? this.duration : duration;
107 // sync animations so there are no ghosted fades and such
108 if(this.slideAnim){
109 if(this.slideAnim.status() != "playing"){
110 this.slideAnim.stop();
111 }
112 if(this.slideAnim.status() == "playing" || (this.fadeAnim && this.fadeAnim.status() == "playing")){
113 setTimeout(lang.hitch(this, function(){
114 this.setContent(message, messageType, duration);
115 }), 50);
116 return;
117 }
118 }
119
120 // determine type of content and apply appropriately
121 for(var type in this.messageTypes){
122 domClass.remove(this.containerNode, "dijitToaster" + capitalize(this.messageTypes[type]));
123 }
124
125 domStyle.set(this.containerNode, "opacity", 1);
126
127 this._setContent(message);
128
129 domClass.add(this.containerNode, "dijitToaster" + capitalize(messageType || this.defaultType));
130
131 // now do funky animation of widget appearing from
132 // bottom right of page and up
133 this.show();
134 var nodeSize = domGeometry.getMarginBox(this.containerNode);
135 this._cancelHideTimer();
136 if(this.isVisible){
137 this._placeClip();
138 //update hide timer if no sticky message in stack
139 if(!this._stickyMessage) {
140 this._setHideTimer(duration);
141 }
142 }else{
143 var style = this.containerNode.style;
144 var pd = this.positionDirection;
145 // sets up initial position of container node and slide-out direction
146 if(pd.indexOf("-up") >= 0){
147 style.left=0+"px";
148 style.top=nodeSize.h + 10 + "px";
149 }else if(pd.indexOf("-left") >= 0){
150 style.left=nodeSize.w + 10 +"px";
151 style.top=0+"px";
152 }else if(pd.indexOf("-right") >= 0){
153 style.left = 0 - nodeSize.w - 10 + "px";
154 style.top = 0+"px";
155 }else if(pd.indexOf("-down") >= 0){
156 style.left = 0+"px";
157 style.top = 0 - nodeSize.h - 10 + "px";
158 }else{
159 throw new Error(this.id + ".positionDirection is invalid: " + pd);
160 }
161 this.slideAnim = coreFx.slideTo({
162 node: this.containerNode,
163 top: 0, left: 0,
164 duration: this.slideDuration,
165 onEnd: lang.hitch(this, function(){
166 //we build the fadeAnim here so we dont have to duplicate it later
167 // can't do a fadeHide because we're fading the
168 // inner node rather than the clipping node
169 this.fadeAnim = (baseFx.fadeOut({
170 node: this.containerNode,
171 duration: 1000,
172 onEnd: lang.hitch(this, function(){
173 this.isVisible = false;
174 this.hide();
175 })
176 }));
177 this.own(this.fadeAnim);
178 this._setHideTimer(duration);
179 this.on('select', lang.hitch(this, function(){
180 this._cancelHideTimer();
181 //force clear sticky message
182 this._stickyMessage=false;
183 this.fadeAnim.play();
184 }));
185
186 this.isVisible = true;
187 })
188 });
189 this.own(this.slideAnim);
190 this.slideAnim.play();
191 }
192 },
193
194 _setContent: function(message){
195 if(lang.isFunction(message)){
196 message(this);
197 return;
198 }
199 if(message && this.isVisible){
200 message = this.contentNode.innerHTML + this.separator + message;
201 }
202 this.contentNode.innerHTML = message;
203 },
204 _cancelHideTimer:function(){
205 if (this._hideTimer){
206 clearTimeout(this._hideTimer);
207 this._hideTimer=null;
208 }
209 },
210
211 _setHideTimer:function(duration){
212 this._cancelHideTimer();
213 //if duration == 0 we keep the message displayed until clicked
214 if(duration>0){
215 this._cancelHideTimer();
216 this._hideTimer = setTimeout(lang.hitch(this, function(evt){
217 // we must hide the iframe in order to fade
218 // TODO: figure out how to fade with a BackgroundIframe
219 if(this.bgIframe && this.bgIframe.iframe){
220 this.bgIframe.iframe.style.display="none";
221 }
222 this._hideTimer=null;
223 //force clear sticky message
224 this._stickyMessage=false;
225 this.fadeAnim.play();
226 }), duration);
227 }
228 else
229 this._stickyMessage=true;
230 },
231
232 _placeClip: function(){
233 var view = window.getBox(this.ownerDocument);
234
235 var nodeSize = domGeometry.getMarginBox(this.containerNode);
236
237 var style = this.clipNode.style;
238 // sets up the size of the clipping node
239 style.height = nodeSize.h+"px";
240 style.width = nodeSize.w+"px";
241
242 // sets up the position of the clipping node
243 var pd = this.positionDirection;
244 if(pd.match(/^t/)){
245 style.top = view.t+"px";
246 }else if(pd.match(/^b/)){
247 style.top = (view.h - nodeSize.h - 2 + view.t)+"px";
248 }
249 if(pd.match(/^[tb]r-/)){
250 style.left = (view.w - nodeSize.w - 1 - view.l)+"px";
251 }else if(pd.match(/^[tb]l-/)){
252 style.left = 0 + "px";
253 }else if(pd.match(/^[tb]c-/)){
254 style.left = Math.round((view.w - nodeSize.w - 1 - view.l)/2)+"px";
255 }
256
257 style.clip = "rect(0px, " + nodeSize.w + "px, " + nodeSize.h + "px, 0px)";
258 if(has("ie")){
259 if(!this.bgIframe){
260 if (!this.clipNode.id) {
261 this.clipNode.id = registry.getUniqueId("dojox_widget_Toaster_clipNode");
262 }
263 this.bgIframe = new BackgroundIframe(this.clipNode);
264 }
265 var iframe = this.bgIframe.iframe;
266 if(iframe){ iframe.style.display="block"; }
267 }
268 },
269
270 onSelect: function(/*Event*/e){
271 // summary:
272 // callback for when user clicks the message
273 },
274
275 show: function(){
276 // summary:'
277 // show the Toaster
278 domStyle.set(this.domNode, 'display', 'block');
279 this._placeClip();
280
281 if(!this._scrollConnected){
282
283 this._scrollConnected = aspect.after(window, "scroll", lang.hitch(this, "_placeClip"));
284 this.own(this._scrollConnected);
285 }
286 },
287
288 hide: function(){
289 // summary:
290 // hide the Toaster
291
292 domStyle.set(this.domNode, 'display', 'none');
293
294 if(this._scrollConnected){
295 this._scrollConnected.remove();
296 this._scrollConnected = false;
297 }
298
299 domStyle.set(this.containerNode, "opacity", 1);
300 }
301 });
302
303 });