Guest User

Untitled

a guest
Jun 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. /**************************************************************
  2.  
  3. Script : Image Menu for prototype & script.aculo.us
  4. Version : 0.1
  5. Authors : Bernhard Reiter, Samuel Birch
  6. Desc : Based upon Samuel Birch's Image Menu 2.2
  7. Licence : Open Source MIT Licence
  8. Ack : to user ilyak on #prototype for helping
  9.  
  10. **************************************************************/
  11.  
  12. var ImageMenu = Class.create({
  13.  
  14. getOptions: function(){
  15. return {
  16. onOpen: false,
  17. onClose: Class.empty,
  18. openWidth: 200,
  19. transition: Effect.Transitions.linear, // TODO: quadOut ?
  20. duration: 400,
  21. open: null,
  22. border: 0
  23. };
  24. },
  25.  
  26. initialize: function(elements, options){
  27. this.options = {};
  28. if (options.onOpen) this.options.onOpen = options.onOpen;
  29. if (options.onClose) this.options.onClose = options.onClose;
  30. if (options.openWidth) this.options.openWidth = options.openWidth;
  31. if (options.transition) this.options.transition = options.transition;
  32. if (options.duration) this.options.duration = options.duration;
  33. if (options.open) this.options.open = options.open;
  34. if (options.border) this.options.border = options.border;
  35.  
  36. //this.setOptions(this.getOptions(), options);
  37. this.options.duration = this.options.duration / 1000; // seconds, as opposed to mootols' milliseconds
  38.  
  39. this.elements = elements;
  40. this.widths = {};
  41. this.widths.closed = this.elements[0].getStyle('width'); //.toInt();
  42. this.widths.openSelected = this.options.openWidth;
  43. this.widths.openOthers = Math.round(((this.widths.closed*this.elements.length) - (this.widths.openSelected+this.options.border)) / (this.elements.length-1));
  44.  
  45. //this.fx = new Fx.Elements(this.elements, {wait: false, duration: this.options.duration, transition: this.options.transition});
  46.  
  47. this.elements.each(function(el,i){
  48. el.observe('mouseover', function(e){
  49. e.stop();
  50. alert(i);
  51. this.reset(i);
  52.  
  53. }.bind(this));
  54.  
  55. el.observe('mouseout', function(e){
  56. e.stop();
  57. this.reset(this.options.open);
  58.  
  59. }.bind(this));
  60.  
  61. var obj = this;
  62.  
  63. el.observe('click', function(e){
  64.  
  65. if(obj.options.onOpen){
  66. new Event(e).stop();
  67. if(obj.options.open == i){
  68. obj.options.open = null;
  69. obj.options.onClose(this.href, i);
  70. }else{
  71. obj.options.open = i;
  72. obj.options.onOpen(this.href, i);
  73. }
  74.  
  75.  
  76. }
  77.  
  78. })
  79.  
  80. }.bind(this));
  81.  
  82. if(this.options.open){
  83. if(Object.isNumber(this.options.open)){
  84. this.reset(this.options.open);
  85. }else{
  86. this.elements.each(function(el,i){
  87. if(el.id == this.options.open){
  88. this.reset(i);
  89. }
  90. },this);
  91. }
  92. }
  93.  
  94. },
  95.  
  96. reset: function(num){
  97. if(Object.isNumber(num)){
  98. var width = this.widths.openOthers;
  99. if(num+1 == this.elements.length){
  100. width += this.options.border;
  101. }
  102. }else{
  103. var width = this.widths.closed;
  104. }
  105.  
  106. var obj = {};
  107. this.elements.each(function(el,i){
  108. var w = width;
  109. if(i == this.elements.length-1){
  110. w = width+5
  111. }
  112. obj[i] = {'width': w};
  113. }.bind(this));
  114.  
  115. if(Object.isNumber(num)){
  116. obj[num] = {'width': this.widths.openSelected};
  117. }
  118.  
  119. //this.fx.start(obj);
  120. this.elements.each(function(el,i){
  121. new Effect.Morph(el, {style: {width: obj[i]+'px'}, duration: this.options.duration
  122. , transition: this.options.transition });
  123. }.bind(this));
  124. }
  125.  
  126. });
  127.  
  128. /*************************************************************/
Add Comment
Please, Sign In to add comment