_PlotEvents.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. define("dojox/charting/plot2d/_PlotEvents", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/connect"],
  2. function(lang, arr, declare, hub){
  3. return declare("dojox.charting.plot2d._PlotEvents", null, {
  4. constructor: function(){
  5. this._shapeEvents = [];
  6. this._eventSeries = {};
  7. },
  8. destroy: function(){
  9. // summary:
  10. // Destroy any internal elements and event handlers.
  11. this.resetEvents();
  12. this.inherited(arguments);
  13. },
  14. plotEvent: function(o){
  15. // summary:
  16. // Stub function for use by specific plots.
  17. // o: Object
  18. // An object intended to represent event parameters.
  19. },
  20. raiseEvent: function(o){
  21. // summary:
  22. // Raises events in predefined order
  23. // o: Object
  24. // An object intended to represent event parameters.
  25. this.plotEvent(o);
  26. var t = lang.delegate(o);
  27. t.originalEvent = o.type;
  28. t.originalPlot = o.plot;
  29. t.type = "onindirect";
  30. arr.forEach(this.chart.stack, function(plot){
  31. if(plot !== this && plot.plotEvent){
  32. t.plot = plot;
  33. plot.plotEvent(t);
  34. }
  35. }, this);
  36. },
  37. connect: function(object, method){
  38. // summary:
  39. // Helper function to connect any object's method to our plotEvent.
  40. // object: Object
  41. // The object to connect to.
  42. // method: String|Function
  43. // The method to fire when our plotEvent is fired.
  44. // returns: Array
  45. // The handle as returned from dojo.connect (see dojo.connect).
  46. this.dirty = true;
  47. return hub.connect(this, "plotEvent", object, method); // Array
  48. },
  49. events: function(){
  50. // summary:
  51. // Find out if any event handlers have been connected to our plotEvent.
  52. // returns: Boolean
  53. // A flag indicating that there are handlers attached.
  54. return !!this.plotEvent.after;
  55. },
  56. resetEvents: function(){
  57. // summary:
  58. // Reset all events attached to our plotEvent (i.e. disconnect).
  59. if(this._shapeEvents.length){
  60. arr.forEach(this._shapeEvents, function(item){
  61. item.shape.disconnect(item.handle);
  62. });
  63. this._shapeEvents = [];
  64. }
  65. this.raiseEvent({type: "onplotreset", plot: this});
  66. },
  67. _connectSingleEvent: function(o, eventName){
  68. this._shapeEvents.push({
  69. shape: o.eventMask,
  70. handle: o.eventMask.connect(eventName, this, function(e){
  71. o.type = eventName;
  72. o.event = e;
  73. this.raiseEvent(o);
  74. o.event = null;
  75. })
  76. });
  77. },
  78. _connectEvents: function(o){
  79. if(o){
  80. o.chart = this.chart;
  81. o.plot = this;
  82. o.hAxis = this.hAxis || null;
  83. o.vAxis = this.vAxis || null;
  84. o.eventMask = o.eventMask || o.shape;
  85. this._connectSingleEvent(o, "onmouseover");
  86. this._connectSingleEvent(o, "onmouseout");
  87. this._connectSingleEvent(o, "onclick");
  88. }
  89. },
  90. _reconnectEvents: function(seriesName){
  91. var a = this._eventSeries[seriesName];
  92. if(a){
  93. arr.forEach(a, this._connectEvents, this);
  94. }
  95. },
  96. fireEvent: function(seriesName, eventName, index, eventObject){
  97. // summary:
  98. // Emulates firing an event for a given data value (specified by
  99. // an index) of a given series.
  100. // seriesName: String:
  101. // Series name.
  102. // eventName: String:
  103. // Event name to emulate.
  104. // index: Number:
  105. // Valid data value index used to raise an event.
  106. // eventObject: Object?:
  107. // Optional event object. Especially useful for synthetic events.
  108. // Default: null.
  109. var s = this._eventSeries[seriesName];
  110. if(s && s.length && index < s.length){
  111. var o = s[index];
  112. o.type = eventName;
  113. o.event = eventObject || null;
  114. this.raiseEvent(o);
  115. o.event = null;
  116. }
  117. }
  118. });
  119. });