UpgradeBar.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.widget.UpgradeBar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.widget.UpgradeBar"] = true;
  8. dojo.provide("dojox.widget.UpgradeBar");
  9. dojo.require("dojo.window");
  10. dojo.require("dojo.fx");
  11. dojo.require("dojo.cookie");
  12. dojo.require("dijit._Widget");
  13. dojo.require("dijit._Templated");
  14. dojo.experimental("dojox.widget.UpgradeBar");
  15. dojo.declare("dojox.widget.UpgradeBar", [dijit._Widget, dijit._Templated], {
  16. // summary:
  17. // Shows a bar at the top of the screen when the user is to
  18. // be notified that they should upgrade their browser or a
  19. // plugin.
  20. //
  21. // description:
  22. // You can insert custom validations to trigger the UpgradeBar
  23. // to display. An evaluation of 'true' shows the bar (as this
  24. // version *is* less than it should be). Multiple validations
  25. // may be checked, although only the first in the list will be
  26. // displayed.
  27. // Markup and programmatic are supported. Markup is a little
  28. // cleaner, since a majority of the parameters are the HTML
  29. // snippets to be displayed. In markup, the validate code should
  30. // be an expression that will evaluate to true or false. This
  31. // expression is wrapped in a try/catch, so if it blows up, it
  32. // is assumed to be true and trigger the bar.
  33. // In programmtic, a function should be used that returns true
  34. // or false. You would need to use your own try/catch in that.
  35. //
  36. // example: See tests for examples.
  37. //
  38. // notifications: Array
  39. // An array of objects that hold the criteria for upgrades.
  40. // message: String
  41. // The message to display in the bar. Can be HTML.
  42. // validate:Function
  43. // The expression to evaluate to determine if the
  44. // bar should show or not. Should be a simple expression
  45. // if used in HTML:
  46. // | <div validate="!google.gears">
  47. // | <div validate="dojo.isIE<8">
  48. notifications:[],
  49. //
  50. // buttonCancel:String
  51. // The HTML tip show when hovering over the close button.
  52. buttonCancel:"Close for now",
  53. //
  54. // noRemindButton:String
  55. // The text link shown that when clicked, permanently dismisses
  56. // the message (sets a cookie). If this string is blank, this
  57. // link is not displayed.
  58. noRemindButton:"Don't Remind Me Again",
  59. templateString: dojo.cache("dojox.widget", "UpgradeBar/UpgradeBar.html", "<div class=\"dojoxUpgradeBar\">\n\t<div class=\"dojoxUpgradeBarMessage\" dojoAttachPoint=\"messageNode\">message</div>\n\t<div class=\"dojoxUpgradeBarReminderButton\" dojoAttachPoint=\"dontRemindButtonNode\" dojoAttachEvent=\"onclick:_onDontRemindClick\">${noRemindButton}</div>\n\t<span dojoAttachPoint=\"closeButtonNode\" class=\"dojoxUpgradeBarCloseIcon\" dojoAttachEvent=\"onclick: hide, onmouseenter: _onCloseEnter, onmouseleave: _onCloseLeave\" title=\"${buttonCancel}\"></span>\n</div>\n"),
  60. constructor: function(props, node){
  61. if(!props.notifications && node){
  62. // From markup. Create the notifications Array from the
  63. // srcRefNode children.
  64. dojo.forEach(node.childNodes, function(n){
  65. if(n.nodeType==1){
  66. var val = dojo.attr(n, "validate");
  67. this.notifications.push({
  68. message:n.innerHTML,
  69. validate:function(){
  70. // the function that fires to determine if the
  71. // bar shows or not.
  72. var evals = true;
  73. try{
  74. evals = dojo.eval(val);
  75. }catch(e){ /* squelch. it's true.*/ }
  76. return evals;
  77. }
  78. });
  79. }
  80. }, this);
  81. }
  82. },
  83. checkNotifications: function(){
  84. // summary:
  85. // Internal. Go through the notifications Array
  86. // and check for any that evaluate to true.
  87. // tags:
  88. // private
  89. //
  90. if(!this.notifications.length){
  91. // odd. why use the bar but not set any notifications?
  92. return;
  93. }
  94. for(var i=0;i<this.notifications.length;i++){
  95. var evals = this.notifications[i].validate();
  96. if(evals){
  97. this.notify(this.notifications[i].message);
  98. // Validation resulted in true, meaning a feature is missing
  99. // Don't check any other messages. One at a time.
  100. break;
  101. }
  102. }
  103. },
  104. postCreate: function(){
  105. this.inherited(arguments);
  106. if(this.domNode.parentNode){
  107. dojo.style(this.domNode, "display", "none");
  108. }
  109. dojo.mixin(this.attributeMap, {
  110. message:{ node:"messageNode", type:"innerHTML" }
  111. });
  112. if(!this.noRemindButton){
  113. dojo.destroy(this.dontRemindButtonNode)
  114. }
  115. if(dojo.isIE==6){
  116. // IE6 is challenged when it comes to 100% width.
  117. // It thinks the body has more padding and more
  118. // margin than it really does. It would work to
  119. // set the body pad and margin to 0, but we can't
  120. // set that and disturb a potential layout.
  121. //
  122. var self = this;
  123. var setWidth = function(){
  124. var v = dojo.window.getBox();
  125. dojo.style(self.domNode, "width", v.w+"px");
  126. }
  127. this.connect(window, "resize", function(){
  128. setWidth();
  129. });
  130. setWidth();
  131. }
  132. dojo.addOnLoad(this, "checkNotifications");
  133. //this.checkNotifications();
  134. },
  135. notify: function(msg){
  136. // summary:
  137. // Triggers the bar to display. An internal function,
  138. // but could ne called externally for fun.
  139. // tags:
  140. // protected
  141. //
  142. if(dojo.cookie("disableUpgradeReminders")){
  143. return;
  144. }
  145. if(!this.domNode.parentNode || !this.domNode.parentNode.innerHTML){
  146. document.body.appendChild(this.domNode);
  147. }
  148. dojo.style(this.domNode, "display", "");
  149. if(msg){
  150. this.set("message", msg);
  151. }
  152. },
  153. show: function(){
  154. // summary:
  155. // Internal. Shows the bar. Do not call directly.
  156. // Use notify();
  157. // tags:
  158. // private
  159. //
  160. this._bodyMarginTop = dojo.style(dojo.body(), "marginTop");
  161. this._size = dojo.contentBox(this.domNode).h;
  162. dojo.style(this.domNode, { display:"block", height:0, opacity:0 });
  163. if(!this._showAnim){
  164. this._showAnim = dojo.fx.combine([
  165. dojo.animateProperty({ node:dojo.body(), duration:500, properties:{ marginTop:this._bodyMarginTop+this._size } }),
  166. dojo.animateProperty({ node:this.domNode, duration:500, properties:{ height:this._size, opacity:1 } })
  167. ]);
  168. }
  169. this._showAnim.play();
  170. },
  171. hide: function(){
  172. // summary:
  173. // Hides the bar. May be called externally.
  174. //
  175. if(!this._hideAnim){
  176. this._hideAnim = dojo.fx.combine([
  177. dojo.animateProperty({ node:dojo.body(), duration:500, properties:{ marginTop:this._bodyMarginTop } }),
  178. dojo.animateProperty({ node:this.domNode, duration:500, properties:{ height:0, opacity:0 } })
  179. ]);
  180. dojo.connect(this._hideAnim, "onEnd", this, function(){
  181. dojo.style(this.domNode, "display", "none");
  182. });
  183. }
  184. this._hideAnim.play();
  185. },
  186. _onDontRemindClick: function(){
  187. // summary:
  188. // Called when user clicks the "do not remind" link.
  189. // tags:
  190. // private
  191. dojo.cookie("disableUpgradeReminders", true, { expires:3650 });
  192. this.hide();
  193. },
  194. _onCloseEnter: function(){
  195. // summary:
  196. // Called when user hovers over close icon
  197. // tags:
  198. // private
  199. dojo.addClass(this.closeButtonNode, "dojoxUpgradeBarCloseIcon-hover");
  200. },
  201. _onCloseLeave: function(){
  202. // summary:
  203. // Called when user stops hovering over close icon
  204. // tags:
  205. // private
  206. dojo.removeClass(this.closeButtonNode, "dojoxUpgradeBarCloseIcon-hover");
  207. }
  208. });
  209. }