Shadow.js 4.6 KB

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