Toolbar.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // wrapped by build app
  2. define("dojox/drawing/ui/dom/Toolbar", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.ui.dom.Toolbar");
  4. dojo.deprecated("dojox.drawing.ui.dom.Toolbar", "It may not even make it to the 1.4 release.", 1.4);
  5. (function(){
  6. dojo.declare("dojox.drawing.ui.dom.Toolbar", [], {
  7. // NOTE:
  8. // dojox.drawing.Toolbar is DEPRECATED.
  9. // The intention never was to use HTML as buttons for a Drawing.
  10. // This was implemented in order to finish the project for which
  11. // Drawing was developed.
  12. // Instead use: drawing/ui/Toolbar.js
  13. //
  14. // summary:
  15. // Creates a Toolbar to be used with a DojoX Drawing.
  16. // description:
  17. // Currently works in markup only. A class is required with
  18. // either horizontal or vertical as a class (IE prevented using
  19. // either as a default). Assign an attribute of 'drawingId' with
  20. // the id of the DojoX Drawing to which this is assigned.
  21. // The node children will be assigned as the Tools in the toolbar.
  22. // Plugins can also be assigned.
  23. // The Toolbar is largely self contained and has no real public
  24. // methods or events. the Drawing object should be used.
  25. //
  26. // example:
  27. // | <div dojoType="dojox.drawing.Toolbar" drawingId="drawing" class="drawingToolbar vertical">
  28. // | <div tool="dojox.drawing.tools.Line" selected="false"> Line</div>
  29. // | <div tool="dojox.drawing.tools.Rect" selected="true"> Rect</div>
  30. // | <div plugin="dojox.drawing.plugins.tools.Zoom" options="{zoomInc:.1,minZoom:.5,maxZoom:2}">Zoom</div>
  31. // | </div>
  32. //
  33. // TODO: Toolbar works in markup only. Need programmatic.
  34. // NOTE: There are plans to make the toolbar out of dojox.gfx vectors.
  35. // This may change the APIs in the future.
  36. //
  37. // baseClass:String
  38. // The CSS style to apply to the toolbar node
  39. baseClass:"drawingToolbar",
  40. // buttonClass:String
  41. // The CSS style to apply to each button node
  42. buttonClass:"drawingButton",
  43. // iconClass:String
  44. // The CSS style to apply to each button icon node
  45. iconClass:"icon",
  46. //
  47. constructor: function(props, node){
  48. // props is null from markup
  49. dojo.addOnLoad(this, function(){
  50. this.domNode = dojo.byId(node);
  51. dojo.addClass(this.domNode, this.baseClass);
  52. this.parse();
  53. });
  54. },
  55. createIcon: function(/*HTMLNode*/node, /* ? Function*/constr){
  56. // summary:
  57. // Internal. Creates an icon node for each button.
  58. // arguments:
  59. // node: HTMLNode
  60. // The button node.
  61. // constr: [optional] Function
  62. // Optional. If not supplied, an icon is not created.
  63. // Information for each icon is derived from
  64. // the ToolsSetup object defined at the end
  65. // of each tool. See: stencil._Base
  66. //
  67. var setup = constr && constr.setup ? constr.setup : {};
  68. if(setup.iconClass){
  69. var icon = setup.iconClass ? setup.iconClass : "iconNone";
  70. var tip = setup.tooltip ? setup.tooltip : "Tool";
  71. var iNode = dojo.create("div", {title:tip}, node);
  72. dojo.addClass(iNode, this.iconClass);
  73. dojo.addClass(iNode, icon);
  74. dojo.connect(node, "mouseup", function(evt){
  75. dojo.stopEvent(evt);
  76. dojo.removeClass(node, "active");
  77. });
  78. dojo.connect(node, "mouseover", function(evt){
  79. dojo.stopEvent(evt);
  80. dojo.addClass(node, "hover");
  81. });
  82. dojo.connect(node, "mousedown", this, function(evt){
  83. dojo.stopEvent(evt);
  84. dojo.addClass(node, "active");
  85. });
  86. dojo.connect(node, "mouseout", this, function(evt){
  87. dojo.stopEvent(evt);
  88. dojo.removeClass(node, "hover");
  89. });
  90. }
  91. },
  92. createTool: function(/*HTMLNode*/node){
  93. // summary:
  94. // Creates a button on the Toolbar that is
  95. // a Tool, not a Plugin. Tools draw Stencils,
  96. // Plugins do actions.
  97. // arguments:
  98. // node: HTMLNode
  99. // The button node.
  100. //
  101. node.innerHTML = "";
  102. var type = dojo.attr(node, "tool");
  103. this.toolNodes[type] = node;
  104. dojo.attr(node, "tabIndex", 1);
  105. var constr = dojo.getObject(type);
  106. this.createIcon(node, constr);
  107. this.drawing.registerTool(type, constr);
  108. dojo.connect(node, "mouseup", this, function(evt){
  109. dojo.stopEvent(evt);
  110. dojo.removeClass(node, "active");
  111. this.onClick(type);
  112. });
  113. dojo.connect(node, "mouseover", function(evt){
  114. dojo.stopEvent(evt);
  115. dojo.addClass(node, "hover");
  116. });
  117. dojo.connect(node, "mousedown", this, function(evt){
  118. dojo.stopEvent(evt);
  119. dojo.addClass(node, "active");
  120. });
  121. dojo.connect(node, "mouseout", this, function(evt){
  122. dojo.stopEvent(evt);
  123. dojo.removeClass(node, "hover");
  124. });
  125. },
  126. parse: function(){
  127. // summary:
  128. // Initializing method that reads the dom node and its
  129. // children for tools and plugins.
  130. //
  131. var drawingId = dojo.attr(this.domNode, "drawingId");
  132. this.drawing = dojox.drawing.util.common.byId(drawingId);
  133. !this.drawing && console.error("Drawing not found based on 'drawingId' in Toolbar. ");
  134. this.toolNodes = {};
  135. var _sel;
  136. dojo.query(">", this.domNode).forEach(function(node, i){
  137. node.className = this.buttonClass;
  138. var tool = dojo.attr(node, "tool");
  139. var action = dojo.attr(node, "action");
  140. var plugin = dojo.attr(node, "plugin");
  141. if(tool){
  142. if(i==0 || dojo.attr(node, "selected")=="true"){
  143. _sel = tool;
  144. }
  145. this.createTool(node);
  146. }else if(plugin){
  147. var p = {name:plugin, options:{}},
  148. opt = dojo.attr(node, "options");
  149. if(opt){
  150. p.options = eval("("+opt+")");
  151. }
  152. p.options.node = node;
  153. node.innerHTML = "";
  154. this.drawing.addPlugin(p);
  155. this.createIcon(node, dojo.getObject(dojo.attr(node, "plugin")));
  156. }
  157. }, this);
  158. this.drawing.initPlugins();
  159. dojo.connect(this.drawing, "setTool", this, "onSetTool");
  160. this.drawing.setTool(_sel);
  161. },
  162. onClick: function(/*String*/type){
  163. // summary:
  164. // Event fired from clicking a Tool, not a PLugin.
  165. // Plugin clicks are handled within the plugin's class.
  166. // arguments:
  167. // type: Fully qualified name of class. ex:
  168. // dojox.drawing.tools.Ellipse
  169. //
  170. this.drawing.setTool(type);
  171. },
  172. onSetTool: function(/*String*/type){
  173. // summary:
  174. // handles buttons clicks and selects or deselects
  175. for(var n in this.toolNodes){
  176. if(n == type){
  177. dojo.addClass(this.toolNodes[type], "selected");
  178. this.toolNodes[type].blur();
  179. }else{
  180. dojo.removeClass(this.toolNodes[n], "selected");
  181. }
  182. }
  183. }
  184. });
  185. })();
  186. });