Shadow.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.fx.Shadow"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.fx.Shadow"] = true;
  8. dojo.provide("dojox.fx.Shadow");
  9. dojo.experimental("dojox.fx.Shadow");
  10. dojo.require("dijit._Widget");
  11. dojo.require("dojo.NodeList-fx");
  12. dojo.declare("dojox.fx.Shadow",
  13. dijit._Widget,{
  14. // summary: Adds a drop-shadow to a node.
  15. //
  16. // example:
  17. // | // add drop shadows to all nodes with class="hasShadow"
  18. // | dojo.query(".hasShadow").forEach(function(n){
  19. // | var foo = new dojox.fx.Shadow({ node: n });
  20. // | foo.startup();
  21. // | });
  22. //
  23. // shadowPng: String
  24. // Base location for drop-shadow images
  25. shadowPng: dojo.moduleUrl("dojox.fx", "resources/shadow"),
  26. // shadowThickness: Integer
  27. // How wide (in px) to make the shadow
  28. shadowThickness: 7,
  29. // shadowOffset: Integer
  30. // How deep to make the shadow appear to be
  31. shadowOffset: 3,
  32. // opacity: Float
  33. // Overall opacity of the shadow
  34. opacity: 0.75,
  35. // animate: Boolean
  36. // A toggle to disable animated transitions
  37. animate: false,
  38. // node: DomNode
  39. // The node we will be applying this shadow to
  40. node: null,
  41. startup: function(){
  42. // summary: Initializes the shadow.
  43. this.inherited(arguments);
  44. this.node.style.position = "relative";
  45. // make all the pieces of the shadow, and position/size them as much
  46. // as possible (but a lot of the coordinates are set in sizeShadow
  47. this.pieces={};
  48. var x1 = -1 * this.shadowThickness;
  49. var y0 = this.shadowOffset;
  50. var y1 = this.shadowOffset + this.shadowThickness;
  51. this._makePiece("tl", "top", y0, "left", x1);
  52. this._makePiece("l", "top", y1, "left", x1, "scale");
  53. this._makePiece("tr", "top", y0, "left", 0);
  54. this._makePiece("r", "top", y1, "left", 0, "scale");
  55. this._makePiece("bl", "top", 0, "left", x1);
  56. this._makePiece("b", "top", 0, "left", 0, "crop");
  57. this._makePiece("br", "top", 0, "left", 0);
  58. this.nodeList = dojo.query(".shadowPiece",this.node);
  59. this.setOpacity(this.opacity);
  60. this.resize();
  61. },
  62. _makePiece: function(name, vertAttach, vertCoord, horzAttach, horzCoord, sizing){
  63. // summary: append a shadow pieces to the node, and position it
  64. var img;
  65. var url = this.shadowPng + name.toUpperCase() + ".png";
  66. if(dojo.isIE < 7){
  67. img = dojo.create("div");
  68. img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+url+"'"+
  69. (sizing?", sizingMethod='"+sizing+"'":"") + ")";
  70. }else{
  71. img = dojo.create("img", { src:url });
  72. }
  73. img.style.position="absolute";
  74. img.style[vertAttach]=vertCoord+"px";
  75. img.style[horzAttach]=horzCoord+"px";
  76. img.style.width=this.shadowThickness+"px";
  77. img.style.height=this.shadowThickness+"px";
  78. dojo.addClass(img,"shadowPiece");
  79. this.pieces[name]=img;
  80. this.node.appendChild(img);
  81. },
  82. setOpacity: function(/* Float */n,/* Object? */animArgs){
  83. // summary: set the opacity of the underlay
  84. // note: does not work in IE? FIXME.
  85. if(dojo.isIE){ return; }
  86. if(!animArgs){ animArgs = {}; }
  87. if(this.animate){
  88. var _anims = [];
  89. this.nodeList.forEach(function(node){
  90. _anims.push(dojo._fade(dojo.mixin(animArgs,{ node: node, end: n })));
  91. });
  92. dojo.fx.combine(_anims).play();
  93. }else{
  94. this.nodeList.style("opacity",n);
  95. }
  96. },
  97. setDisabled: function(/* Boolean */disabled){
  98. // summary: enable / disable the shadow
  99. if(disabled){
  100. if(this.disabled){ return; }
  101. if(this.animate){ this.nodeList.fadeOut().play();
  102. }else{ this.nodeList.style("visibility","hidden"); }
  103. this.disabled = true;
  104. }else{
  105. if(!this.disabled){ return; }
  106. if(this.animate){ this.nodeList.fadeIn().play();
  107. }else{ this.nodeList.style("visibility","visible"); }
  108. this.disabled = false;
  109. }
  110. },
  111. resize: function(/* dojox.fx._arg.ShadowResizeArgs */args){
  112. // summary: Resizes the shadow based on width and height.
  113. var x; var y;
  114. if(args){ x = args.x; y = args.y;
  115. }else{
  116. var co = dojo._getBorderBox(this.node);
  117. x = co.w; y = co.h;
  118. }
  119. var sideHeight = y - (this.shadowOffset+this.shadowThickness);
  120. if (sideHeight < 0) { sideHeight = 0; }
  121. if (y < 1) { y = 1; }
  122. if (x < 1) { x = 1; }
  123. with(this.pieces){
  124. l.style.height = sideHeight+"px";
  125. r.style.height = sideHeight+"px";
  126. b.style.width = x+"px";
  127. bl.style.top = y+"px";
  128. b.style.top = y+"px";
  129. br.style.top = y+"px";
  130. tr.style.left = x+"px";
  131. r.style.left = x+"px";
  132. br.style.left = x+"px";
  133. }
  134. }
  135. });
  136. }