Toolbar.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.sketch.Toolbar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.sketch.Toolbar"] = true;
  8. dojo.provide("dojox.sketch.Toolbar");
  9. dojo.require("dojox.sketch.Annotation");
  10. dojo.require("dijit.Toolbar");
  11. dojo.require("dijit.form.Button");
  12. dojo.declare("dojox.sketch.ButtonGroup", null, {
  13. constructor: function(){
  14. this._childMaps={};
  15. this._children=[];
  16. },
  17. add: function(/*_Plugin*/ plugin){
  18. this._childMaps[plugin]=plugin.connect(plugin,'onActivate',dojo.hitch(this,'_resetGroup',plugin));
  19. this._children.push(plugin);
  20. },
  21. // remove: function(/*_Plugin*/ plugin){
  22. // widget.disconnect(this._childMaps[widget.id]);
  23. // delete this._childMaps[widget.id];
  24. // this._children.splice(this._children.indexOf(widget.id),1);
  25. // },
  26. _resetGroup: function(p){
  27. var cs=this._children;
  28. dojo.forEach(cs,function(c){
  29. if(p!=c && c['attr']){
  30. c.attr('checked',false);
  31. }
  32. });
  33. }
  34. });
  35. dojo.declare("dojox.sketch.Toolbar", dijit.Toolbar, {
  36. figure: null,
  37. plugins: null,
  38. postCreate: function(){
  39. this.inherited(arguments);
  40. this.shapeGroup=new dojox.sketch.ButtonGroup;
  41. if(!this.plugins){
  42. this.plugins=['Lead','SingleArrow','DoubleArrow','Underline','Preexisting','Slider'];
  43. }
  44. this._plugins=[];
  45. dojo.forEach(this.plugins,function(obj){
  46. var name=dojo.isString(obj)?obj:obj.name;
  47. var p=new dojox.sketch.tools[name](obj.args||{});
  48. this._plugins.push(p);
  49. p.setToolbar(this);
  50. if(!this._defaultTool && p.button){
  51. this._defaultTool=p;
  52. }
  53. },this);
  54. },
  55. setFigure: function(f){
  56. this.figure = f;
  57. this.connect(f,'onLoad','reset');
  58. dojo.forEach(this._plugins, function(p){
  59. p.setFigure(f);
  60. });
  61. },
  62. destroy: function(){
  63. dojo.forEach(this._plugins,function(p){
  64. p.destroy();
  65. });
  66. this.inherited(arguments);
  67. delete this._defaultTool;
  68. delete this._plugins;
  69. },
  70. addGroupItem: function(/*_Plugin*/item,group){
  71. if(group!='toolsGroup'){
  72. console.error('not supported group '+group);
  73. return;
  74. }
  75. this.shapeGroup.add(item);
  76. },
  77. reset: function(){
  78. this._defaultTool.activate();
  79. },
  80. _setShape: function(s){
  81. if(!this.figure.surface) return;
  82. // now do the action.
  83. if(this.figure.hasSelections()){
  84. for(var i=0; i<this.figure.selected.length; i++){
  85. var before=this.figure.selected[i].serialize();
  86. this.figure.convert(this.figure.selected[i], s);
  87. this.figure.history.add(dojox.sketch.CommandTypes.Convert, this.figure.selected[i], before);
  88. }
  89. }
  90. }
  91. });
  92. dojox.sketch.makeToolbar=function(node,figure){
  93. var toolbar=new dojox.sketch.Toolbar();
  94. toolbar.setFigure(figure);
  95. node.appendChild(toolbar.domNode);
  96. return toolbar;
  97. };
  98. }