| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/on", |
| 5 |
"dojo/aspect", |
| 6 |
"dojo/topic", |
| 7 |
"dojo/_base/fx", |
| 8 |
"dojo/dom-style", |
| 9 |
"dojo/dom-class", |
| 10 |
"dojo/dom-geometry", |
| 11 |
"dijit/registry", |
| 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(w){ |
| 23 |
return w.substring(0,1).toUpperCase() + w.substring(1); |
| 24 |
}; |
| 25 |
|
| 26 |
return declare("dojox.widget.Toaster", [_WidgetBase, _TemplatedMixin], { |
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
messageTopic: "", |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
messageTypes: { |
| 42 |
MESSAGE: "message", |
| 43 |
WARNING: "warning", |
| 44 |
ERROR: "error", |
| 45 |
FATAL: "fatal" |
| 46 |
}, |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
defaultType: "message", |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
positionDirection: "br-up", |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
positionDirectionTypes: ["br-up", "br-left", "bl-up", "bl-right", "tr-down", "tr-left", "tl-down", "tl-right"], |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
duration: 2000, |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
slideDuration: 500, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
separator: "<hr>", |
| 75 |
|
| 76 |
postCreate: function(){ |
| 77 |
this.inherited(arguments); |
| 78 |
this.hide(); |
| 79 |
|
| 80 |
|
| 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(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(message, messageType, duration){ |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
duration = (duration === undefined) ? this.duration : duration; |
| 107 |
|
| 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 |
|
| 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 |
|
| 132 |
|
| 133 |
this.show(); |
| 134 |
var nodeSize = domGeometry.getMarginBox(this.containerNode); |
| 135 |
this._cancelHideTimer(); |
| 136 |
if(this.isVisible){ |
| 137 |
this._placeClip(); |
| 138 |
|
| 139 |
if(!this._stickyMessage) { |
| 140 |
this._setHideTimer(duration); |
| 141 |
} |
| 142 |
}else{ |
| 143 |
var style = this.containerNode.style; |
| 144 |
var pd = this.positionDirection; |
| 145 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 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 |
|
| 214 |
if(duration>0){ |
| 215 |
this._cancelHideTimer(); |
| 216 |
this._hideTimer = setTimeout(lang.hitch(this, function(evt){ |
| 217 |
|
| 218 |
|
| 219 |
if(this.bgIframe && this.bgIframe.iframe){ |
| 220 |
this.bgIframe.iframe.style.display="none"; |
| 221 |
} |
| 222 |
this._hideTimer=null; |
| 223 |
|
| 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 |
|
| 239 |
style.height = nodeSize.h+"px"; |
| 240 |
style.width = nodeSize.w+"px"; |
| 241 |
|
| 242 |
|
| 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(e){ |
| 271 |
|
| 272 |
|
| 273 |
}, |
| 274 |
|
| 275 |
show: function(){ |
| 276 |
|
| 277 |
|
| 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 |
|
| 290 |
|
| 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 |
}); |