Toolbar.js 8.8 KB

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