Highlight.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.action2d.Highlight"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.action2d.Highlight"] = true;
  8. dojo.provide("dojox.charting.action2d.Highlight");
  9. dojo.require("dojox.charting.action2d.Base");
  10. dojo.require("dojox.color");
  11. /*=====
  12. dojo.declare("dojox.charting.action2d.__HighlightCtorArgs", dojox.charting.action2d.__BaseCtorArgs, {
  13. // summary:
  14. // Additional arguments for highlighting actions.
  15. // highlight: String|dojo.Color|Function?
  16. // Either a color or a function that creates a color when highlighting happens.
  17. highlight: null
  18. });
  19. =====*/
  20. (function(){
  21. var DEFAULT_SATURATION = 100, // %
  22. DEFAULT_LUMINOSITY1 = 75, // %
  23. DEFAULT_LUMINOSITY2 = 50, // %
  24. c = dojox.color,
  25. cc = function(color){
  26. return function(){ return color; };
  27. },
  28. hl = function(color){
  29. var a = new c.Color(color),
  30. x = a.toHsl();
  31. if(x.s == 0){
  32. x.l = x.l < 50 ? 100 : 0;
  33. }else{
  34. x.s = DEFAULT_SATURATION;
  35. if(x.l < DEFAULT_LUMINOSITY2){
  36. x.l = DEFAULT_LUMINOSITY1;
  37. }else if(x.l > DEFAULT_LUMINOSITY1){
  38. x.l = DEFAULT_LUMINOSITY2;
  39. }else{
  40. x.l = x.l - DEFAULT_LUMINOSITY2 > DEFAULT_LUMINOSITY1 - x.l ?
  41. DEFAULT_LUMINOSITY2 : DEFAULT_LUMINOSITY1;
  42. }
  43. }
  44. return c.fromHsl(x);
  45. };
  46. dojo.declare("dojox.charting.action2d.Highlight", dojox.charting.action2d.Base, {
  47. // summary:
  48. // Creates a highlighting action on a plot, where an element on that plot
  49. // has a highlight on it.
  50. // the data description block for the widget parser
  51. defaultParams: {
  52. duration: 400, // duration of the action in ms
  53. easing: dojo.fx.easing.backOut // easing for the action
  54. },
  55. optionalParams: {
  56. highlight: "red" // name for the highlight color
  57. // programmatic instantiation can use functions and color objects
  58. },
  59. constructor: function(chart, plot, kwArgs){
  60. // summary:
  61. // Create the highlighting action and connect it to the plot.
  62. // chart: dojox.charting.Chart2D
  63. // The chart this action belongs to.
  64. // plot: String?
  65. // The plot this action is attached to. If not passed, "default" is assumed.
  66. // kwArgs: dojox.charting.action2d.__HighlightCtorArgs?
  67. // Optional keyword arguments object for setting parameters.
  68. var a = kwArgs && kwArgs.highlight;
  69. this.colorFun = a ? (dojo.isFunction(a) ? a : cc(a)) : hl;
  70. this.connect();
  71. },
  72. process: function(o){
  73. // summary:
  74. // Process the action on the given object.
  75. // o: dojox.gfx.Shape
  76. // The object on which to process the highlighting action.
  77. if(!o.shape || !(o.type in this.overOutEvents)){ return; }
  78. var runName = o.run.name, index = o.index, anim, startFill, endFill;
  79. if(runName in this.anim){
  80. anim = this.anim[runName][index];
  81. }else{
  82. this.anim[runName] = {};
  83. }
  84. if(anim){
  85. anim.action.stop(true);
  86. }else{
  87. var color = o.shape.getFill();
  88. if(!color || !(color instanceof dojo.Color)){
  89. return;
  90. }
  91. this.anim[runName][index] = anim = {
  92. start: color,
  93. end: this.colorFun(color)
  94. };
  95. }
  96. var start = anim.start, end = anim.end;
  97. if(o.type == "onmouseout"){
  98. // swap colors
  99. var t = start;
  100. start = end;
  101. end = t;
  102. }
  103. anim.action = dojox.gfx.fx.animateFill({
  104. shape: o.shape,
  105. duration: this.duration,
  106. easing: this.easing,
  107. color: {start: start, end: end}
  108. });
  109. if(o.type == "onmouseout"){
  110. dojo.connect(anim.action, "onEnd", this, function(){
  111. if(this.anim[runName]){
  112. delete this.anim[runName][index];
  113. }
  114. });
  115. }
  116. anim.action.play();
  117. }
  118. });
  119. })();
  120. }