Toolbar.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.Toolbar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.drawing.ui.Toolbar"] = true;
  8. dojo.provide("dojox.drawing.ui.Toolbar");
  9. dojo.require("dojox.drawing.library.icons");
  10. dojo.declare("dojox.drawing.ui.Toolbar", [], {
  11. // summary:
  12. // A Toolbar used for holding buttons; typically representing the Stencils
  13. // used for a DojoX Drawing.
  14. // description:
  15. // Creates a GFX-based toobar that holds GFX-based buttons. Can be either created
  16. // within the actual drawing or within a seperate DOM element. When within the
  17. // drawing, the toolbar will cover a portion of the drawing; hence the option.
  18. //
  19. // A Toolbar can be created programmtically or in markup. Currently markup is as
  20. // a separate DOM element and programmtic is within the drawing.
  21. // examples:
  22. // | dojo.connect(myDrawing, "onSurfaceReady", function(){
  23. // | new dojox.drawing.ui.Toolbar({
  24. // | drawing:myDrawing,
  25. // | tools:"all",
  26. // | plugs:"all",
  27. // | selected:"ellipse"
  28. // | });
  29. // | });
  30. //
  31. // | <div dojoType="dojox.drawing.ui.Toolbar" id="gfxToolbarNode" drawingId="drawingNode"
  32. // | class="gfxToolbar" tools="all" plugs="all" selected="ellipse" orient="H"></div>
  33. //
  34. //
  35. constructor: function(props, node){
  36. //console.warn("GFX Toolbar:", props, node)
  37. this.util = dojox.drawing.util.common;
  38. // no mixin. painful.
  39. if(props.drawing){
  40. // programmatic
  41. this.toolDrawing = props.drawing;
  42. this.drawing = this.toolDrawing;
  43. this.width = this.toolDrawing.width;
  44. this.height = this.toolDrawing.height;
  45. this.strSelected = props.selected;
  46. this.strTools = props.tools;
  47. this.strPlugs = props.plugs;
  48. this._mixprops(["padding", "margin", "size", "radius"], props);
  49. this.addBack();
  50. this.orient = props.orient ? props.orient : false;
  51. }else{
  52. // markup
  53. var box = dojo.marginBox(node);
  54. this.width = box.w;
  55. this.height = box.h;
  56. this.strSelected = dojo.attr(node, "selected");
  57. this.strTools = dojo.attr(node, "tools");
  58. this.strPlugs = dojo.attr(node, "plugs");
  59. this._mixprops(["padding", "margin", "size", "radius"], node);
  60. this.toolDrawing = new dojox.drawing.Drawing({mode:"ui"}, node);
  61. this.orient = dojo.attr(node, "orient");
  62. }
  63. this.horizontal = this.orient ? this.orient == "H" : this.width > this.height;
  64. console.log("this.hor: ",this.horizontal," orient: ",this.orient);
  65. if(this.toolDrawing.ready){
  66. this.makeButtons();
  67. if(!this.strSelected && this.drawing.defaults.clickMode){ this.drawing.mouse.setCursor('default'); };
  68. }else{
  69. var c = dojo.connect(this.toolDrawing, "onSurfaceReady", this, function(){
  70. //console.log("TB built")
  71. dojo.disconnect(c);
  72. this.drawing = dojox.drawing.getRegistered("drawing", dojo.attr(node, "drawingId")); //
  73. this.makeButtons();
  74. if(!this.strSelected && this.drawing.defaults.clickMode){
  75. var c = dojo.connect(this.drawing, "onSurfaceReady", this, function(){
  76. dojo.disconnect(c);
  77. this.drawing.mouse.setCursor('default');
  78. });
  79. }
  80. });
  81. }
  82. },
  83. // padding:Number
  84. // The amount of spce between the top and left of the toolbar and the buttons.
  85. padding:10,
  86. // margin: Number
  87. // The space between each button.
  88. margin:5,
  89. // size: Number
  90. // The width and height of the button
  91. size:30,
  92. // radius: Number
  93. // The size of the button's rounded corner
  94. radius:3,
  95. //
  96. // toolPlugGap: number
  97. // The distnce between the tool buttons and plug buttons
  98. toolPlugGap:20,
  99. // strSlelected | selected: String
  100. // The button that should be selected at startup.
  101. strSelected:"",
  102. // strTools | tools: String
  103. // A comma delineated list of the Stencil-tools to include in the Toolbar.
  104. // If "all" is used, all registered tools are included.
  105. strTools:"",
  106. // strPlugs | plugs: String
  107. // A comma delineated list of the plugins to include in the Toolbar.
  108. // If "all" is used, all registered plugins are included.
  109. strPlugs:"",
  110. makeButtons: function(){
  111. // summary:
  112. // Internal. create buttons.
  113. this.buttons = [];
  114. this.plugins = [];
  115. var x = this.padding, y = this.padding, w = this.size, h = this.size, r = this.radius, g = this.margin,
  116. sym = dojox.drawing.library.icons,
  117. s = {place:"BR", size:2, mult:4};
  118. if(this.strTools){
  119. var toolAr = [];
  120. var tools = dojox.drawing.getRegistered("tool");
  121. var toolMap = {};
  122. for(var nm in tools){
  123. var tool = this.util.abbr(nm);
  124. toolMap[tool] = tools[nm];
  125. if(this.strTools=="all"){
  126. toolAr.push(tool);
  127. var details = dojox.drawing.getRegistered("tool",nm);
  128. if(details.secondary){
  129. toolAr.push(details.secondary.name);
  130. }
  131. }
  132. }
  133. if(this.strTools!="all"){
  134. var toolTmp = this.strTools.split(",");
  135. dojo.forEach(toolTmp, function(tool){
  136. tool = dojo.trim(tool);
  137. toolAr.push(tool);
  138. var details = dojox.drawing.getRegistered("tool",toolMap[tool].name);
  139. if(details.secondary){
  140. toolAr.push(details.secondary.name);
  141. }
  142. }, this);
  143. //dojo.map(toolAr, function(t){ return dojo.trim(t); });
  144. }
  145. dojo.forEach(toolAr, function(t){
  146. t = dojo.trim(t);
  147. var secondary = false;
  148. if(t.indexOf("Secondary")>-1){
  149. var prim = t.substring(0,t.indexOf("Secondary"));
  150. var sec = dojox.drawing.getRegistered("tool",toolMap[prim].name).secondary;
  151. var label = sec.label;
  152. this[t] = sec.funct;
  153. if(sec.setup){ dojo.hitch(this, sec.setup)(); };
  154. var btn = this.toolDrawing.addUI("button", {data:{x:x, y:y, width:w, height:h/2, r:r}, toolType:t, secondary:true, text:label, shadow:s, scope:this, callback:this[t]});
  155. if(sec.postSetup){ dojo.hitch(this, sec.postSetup, btn)(); };
  156. secondary = true;
  157. } else {
  158. var btn = this.toolDrawing.addUI("button", {data:{x:x, y:y, width:w, height:h, r:r}, toolType:t, icon:sym[t], shadow:s, scope:this, callback:"onToolClick"});
  159. }
  160. dojox.drawing.register(btn, "button");
  161. this.buttons.push(btn);
  162. if(this.strSelected==t){
  163. btn.select();
  164. this.selected = btn;
  165. this.drawing.setTool(btn.toolType);
  166. }
  167. if(this.horizontal){
  168. x += h + g;
  169. }else{
  170. var space = secondary ? h/2 + g : h + g;
  171. y += space;
  172. }
  173. }, this);
  174. }
  175. if(this.horizontal){
  176. x += this.toolPlugGap;
  177. }else{
  178. y += this.toolPlugGap;
  179. }
  180. if(this.strPlugs){
  181. var plugAr = [];
  182. var plugs = dojox.drawing.getRegistered("plugin");
  183. var plugMap = {};
  184. for(var nm in plugs){
  185. var abbr = this.util.abbr(nm);
  186. plugMap[abbr] = plugs[nm];
  187. if(this.strPlugs=="all"){ plugAr.push(abbr); }
  188. }
  189. if(this.strPlugs!="all"){
  190. plugAr = this.strPlugs.split(",");
  191. dojo.map(plugAr, function(p){ return dojo.trim(p); });
  192. }
  193. dojo.forEach(plugAr, function(p){
  194. var t = dojo.trim(p);
  195. //console.log(" plugin:", p);
  196. if(plugMap[p].button != false){
  197. var btn = this.toolDrawing.addUI("button", {data:{x:x, y:y, width:w, height:h, r:r}, toolType:t, icon:sym[t], shadow:s, scope:this, callback:"onPlugClick"});
  198. dojox.drawing.register(btn, "button");
  199. this.plugins.push(btn);
  200. if(this.horizontal){
  201. x += h + g;
  202. }else{
  203. y += h + g;
  204. }
  205. }
  206. var addPlug = {}
  207. plugMap[p].button == false ? addPlug = {name:this.drawing.stencilTypeMap[p]} : addPlug = {name:this.drawing.stencilTypeMap[p], options:{button:btn}};
  208. this.drawing.addPlugin(addPlug);
  209. }, this);
  210. }
  211. dojo.connect(this.drawing, "onRenderStencil", this, "onRenderStencil");
  212. },
  213. onRenderStencil: function(/* Object */stencil){
  214. // summary:
  215. // Stencil render event.
  216. if(this.drawing.defaults.clickMode){
  217. this.drawing.mouse.setCursor("default");
  218. this.selected && this.selected.deselect();
  219. this.selected = null;
  220. }
  221. },
  222. addTool: function(){
  223. // TODO: add button here
  224. },
  225. addPlugin: function(){
  226. // TODO: add button here
  227. },
  228. addBack: function(){
  229. // summary:
  230. // Internal. Adds the back, behind the toolbar.
  231. this.toolDrawing.addUI("rect", {data:{x:0, y:0, width:this.width, height:this.size + (this.padding*2), fill:"#ffffff", borderWidth:0}});
  232. },
  233. onToolClick: function(/*Object*/button){
  234. // summary:
  235. // Tool click event. May be connected to.
  236. //
  237. if(this.drawing.defaults.clickMode){ this.drawing.mouse.setCursor("crosshair"); }
  238. dojo.forEach(this.buttons, function(b){
  239. if(b.id==button.id){
  240. b.select();
  241. this.selected = b;
  242. this.drawing.setTool(button.toolType)
  243. }else{
  244. if(!b.secondary){ b.deselect(); }
  245. }
  246. },this)
  247. },
  248. onPlugClick: function(/*Object*/button){
  249. // summary:
  250. // Plugin click event. May be connected to.
  251. },
  252. _mixprops: function(/*Array*/props, /*Object | Node*/objNode){
  253. // summary:
  254. // Internally used for mixing in props from an object or
  255. // from a dom node.
  256. dojo.forEach(props, function(p){
  257. this[p] = objNode.tagName
  258. ? dojo.attr(objNode, p)===null ? this[p] : dojo.attr(objNode, p)
  259. : objNode[p]===undefined ? this[p] : objNode[p];
  260. }, this);
  261. }
  262. });
  263. }