Toolbar.js 2.6 KB

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