_PlotEvents.js 3.9 KB

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