Button.js 6.7 KB

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