Button.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // wrapped by build app
  2. define("dojox/drawing/ui/Button", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.ui.Button");
  4. dojox.drawing.ui.Button = dojox.drawing.util.oo.declare(
  5. // summary:
  6. // Creates a clickable button in "UI" mode of the drawing.
  7. // description:
  8. // Creates a 4-state button: normal, hover, active, selected.
  9. // Optionally may include button text or an icon.
  10. function(options){
  11. options.subShape = true;
  12. dojo.mixin(this, options);
  13. //console.log(" button:", this);
  14. this.width = options.data.width || options.data.rx*2;
  15. this.height = options.data.height || options.data.ry*2;
  16. this.y = options.data.y || options.data.cy - options.data.ry;
  17. //
  18. this.id = this.id || this.util.uid(this.type);
  19. this.util.attr(this.container, "id", this.id);
  20. if(this.callback){
  21. this.hitched = dojo.hitch(this.scope || window, this.callback, this);
  22. }
  23. // Rectangle itself must be "ui" for radio buttons to work.
  24. // This is a work-around for messy code associated with drawingType;
  25. // see http://www.andestutor.org/bugzilla/show_bug.cgi?id=1745
  26. options.drawingType="ui";
  27. // Choose between rectangle and ellipse based on options
  28. if(options.data.width && options.data.height){
  29. this.shape = new dojox.drawing.stencil.Rect(options);
  30. }else{
  31. this.shape = new dojox.drawing.stencil.Ellipse(options);
  32. }
  33. var setGrad = function(s, p, v){
  34. dojo.forEach(["norm", "over", "down", "selected"], function(nm){
  35. s[nm].fill[p] = v;
  36. });
  37. };
  38. // for button backs, not for icons
  39. setGrad(this.style.button, "y2", this.height + this.y);
  40. setGrad(this.style.button, "y1", this.y);
  41. if(options.icon && !options.icon.text){
  42. var constr = this.drawing.getConstructor(options.icon.type);
  43. var o = this.makeOptions(options.icon);
  44. o.data = dojo.mixin(o.data, this.style.button.icon.norm);
  45. if(o.data && o.data.borderWidth===0){
  46. o.data.fill = this.style.button.icon.norm.fill = o.data.color;
  47. }else if(options.icon.type=="line" || (options.icon.type=="path" && !options.icon.closePath)){
  48. this.style.button.icon.selected.color = this.style.button.icon.selected.fill;
  49. }else{
  50. //o.data.fill = this.style.button.icon.norm.fill = o.data.color;
  51. }
  52. this.icon = new constr(o);
  53. //console.log(" button:", this.toolType, this.style.button.icon)
  54. }else if(options.text || (options.icon && options.icon.text)){
  55. //console.warn("button text:", options.text || options.icon.text)
  56. o = this.makeOptions(options.text || options.icon.text);
  57. o.data.color = this.style.button.icon.norm.color; //= o.data.fill;
  58. this.style.button.icon.selected.color = this.style.button.icon.selected.fill;
  59. this.icon = new dojox.drawing.stencil.Text(o);
  60. this.icon.attr({
  61. height: this.icon._lineHeight,
  62. y:((this.height-this.icon._lineHeight)/2)+this.y
  63. });
  64. }
  65. var c = this.drawing.getConstructor(this.toolType);
  66. if(c){
  67. this.drawing.addUI("tooltip", {data:{text:c.setup.tooltip}, button:this});
  68. }
  69. this.onOut();
  70. },{
  71. callback:null,
  72. scope:null,
  73. hitched:null,
  74. toolType:"",
  75. onClick: function(/*Object*/button){
  76. // summary:
  77. // Stub to connect. Event is 'this'
  78. // Alternatively can pass in a scope and a callback
  79. // on creation.
  80. },
  81. makeOptions: function(/*Object*/d, /*Float*/s){
  82. s = s || 1;
  83. d = dojo.clone(d);
  84. var o = {
  85. util: this.util,
  86. mouse: this.mouse,
  87. container: this.container,
  88. subShape:true
  89. };
  90. if(typeof(d)=="string"){
  91. o.data = {
  92. x:this.data.x - 5,
  93. y: this.data.y + 2,
  94. width:this.data.width,
  95. height:this.data.height,
  96. text:d,
  97. makeFit:true
  98. };
  99. }else if(d.points){
  100. //console.warn("points")
  101. dojo.forEach(d.points, function(pt){
  102. pt.x = pt.x * this.data.width * .01 * s + this.data.x;
  103. pt.y = pt.y * this.data.height * .01 * s + this.data.y;
  104. }, this);
  105. o.data = {};
  106. for(var n in d){
  107. if(n!="points"){
  108. o.data[n] = d[n];
  109. }
  110. }
  111. o.points = d.points;
  112. }else{
  113. //console.warn("data")
  114. for(n in d){
  115. if(/x|width/.test(n)){
  116. d[n] = d[n] * this.data.width * .01 * s;
  117. }else if(/y|height/.test(n)){
  118. d[n] = d[n] * this.data.height * .01 * s;
  119. }
  120. if(/x/.test(n) && !/r/.test(n)){
  121. d[n] += this.data.x;
  122. }else if(/y/.test(n) && !/r/.test(n)){
  123. d[n] += this.data.y;
  124. }
  125. }
  126. delete d.type;
  127. o.data = d;
  128. }
  129. o.drawingType = "ui";
  130. return o;
  131. // do style
  132. if(d.borderWidth!==undefined){
  133. //console.log(" -------- bw data:", o.data)
  134. o.data.borderWidth = d.borderWidth;
  135. }
  136. return o;
  137. },
  138. enabled:true,
  139. selected:false,
  140. type:"drawing.library.UI.Button",
  141. // note:
  142. // need to move the Stencil's shape to front, not
  143. // its container. Therefore we can't use the Stencil's
  144. // moveTo.. methods.
  145. select: function(){
  146. this.selected = true;
  147. if(this.icon){this.icon.attr(this.style.button.icon.selected);}
  148. this._change(this.style.button.selected);
  149. this.shape.shadow && this.shape.shadow.hide();
  150. },
  151. deselect: function(){
  152. this.selected = false;
  153. if(this.icon){this.icon.attr(this.style.button.icon.norm);}
  154. this.shape.shadow && this.shape.shadow.show();
  155. this._change(this.style.button.norm);
  156. },
  157. disable: function(){
  158. if(!this.enabled){ return; }
  159. this.enabled = false;
  160. this._change(this.style.button.disabled);
  161. this.icon.attr({color:this.style.button.norm.color});
  162. },
  163. enable: function(){
  164. if(this.enabled){ return; }
  165. this.enabled = true;
  166. this._change(this.style.button.norm);
  167. this.icon.attr({color:this.style.button.icon.norm.color});
  168. },
  169. _change: function(/*Object*/sty){
  170. this.shape.attr(sty);
  171. this.shape.shadow && this.shape.shadow.container.moveToBack();
  172. if(this.icon){this.icon.shape.moveToFront();};
  173. },
  174. onOver: function(){
  175. //console.log("BUTTON OVER")
  176. if(this.selected || !this.enabled){ return; }
  177. this._change(this.style.button.over);
  178. },
  179. onOut: function(){
  180. if(this.selected){ return; }
  181. this._change(this.style.button.norm);
  182. },
  183. onDown: function(){
  184. if(this.selected || !this.enabled){ return; }
  185. this._change(this.style.button.selected);
  186. },
  187. onUp: function(){
  188. //console.log("BUTTON UP")
  189. if(!this.enabled){ return; }
  190. this._change(this.style.button.over);
  191. if(this.hitched){
  192. this.hitched();
  193. }
  194. this.onClick(this);
  195. },
  196. attr: function(options){
  197. if(this.icon){this.icon.attr(options);}
  198. }
  199. }
  200. );
  201. dojox.drawing.register({
  202. name:"dojox.drawing.ui.Button"
  203. }, "stencil");
  204. });