Toolbar.js 6.6 KB

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