ui.magnifier.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. (function($) {
  2. //If the UI scope is not availalable, add it
  3. $.ui = $.ui || {};
  4. //Make nodes selectable by expression
  5. $.extend($.expr[':'], { magnifier: "(' '+a.className+' ').indexOf(' ui-magnifier ')" });
  6. //Macros for external methods that support chaining
  7. var methods = "destroy,enable,disable,reset".split(",");
  8. for(var i=0;i<methods.length;i++) {
  9. var cur = methods[i], f;
  10. eval('f = function() { var a = arguments; return this.each(function() { if(jQuery(this).is(".ui-magnifier")) jQuery.data(this, "ui-magnifier")["'+cur+'"](a); }); }');
  11. $.fn["magnifier"+cur.substr(0,1).toUpperCase()+cur.substr(1)] = f;
  12. };
  13. //get instance method
  14. $.fn.magnifierInstance = function() {
  15. if($(this[0]).is(".ui-magnifier")) return $.data(this[0], "ui-magnifier");
  16. return false;
  17. };
  18. $.fn.magnifier = function(options) {
  19. return this.each(function() {
  20. new $.ui.magnifier(this,options);
  21. });
  22. };
  23. $.ui.magnifier = function(el,options) {
  24. var self = this; this.items = []; this.element = el;
  25. this.options = options || {}; var o = this.options;
  26. $.data(el, "ui-magnifier", this);
  27. $(el).addClass("ui-magnifier");
  28. o.distance = o.distance || 150;
  29. o.magnification = o.magnification || 2;
  30. o.baseline = o.baseline || 0;
  31. o.verticalLine = o.verticalLine != undefined ? o.verticalLine : -0.5;
  32. this.pp = $(el).offset({ border: false });
  33. $('> *', el).each(function() {
  34. var co = $(this).offset({ border: false });
  35. if(self.options.overlap) var cp = $(this).position();
  36. self.items.push([this, co, [$(this).width(),$(this).height()], (cp || null)]);
  37. if(o.opacity)
  38. $(this).css('opacity', o.opacity.min);
  39. });
  40. if(o.overlap) {
  41. for(var i=0;i<this.items.length;i++) {
  42. //Absolute stuff
  43. $(this.items[i][0]).css({
  44. position: "absolute",
  45. top: this.items[i][3].top,
  46. left: this.items[i][3].left
  47. });
  48. };
  49. }
  50. this.moveEvent = function(e) { if(!self.disabled) self.magnify.apply(self, [e]); }
  51. $(document).bind("mousemove", this.moveEvent);
  52. if(o.click) { //If onclick callback is available
  53. this.clickEvent = function(e) { if(!self.disabled) o.click.apply(this, [e, { options: self.options, current: self.current[0], currentOffset: self.current[1] }]); }
  54. $(el).bind('click', this.clickEvent);
  55. }
  56. }
  57. $.extend($.ui.magnifier.prototype, {
  58. destroy: function() {
  59. $(this.element).removeClass("ui-magnifier").removeClass("ui-magnifier-disabled");
  60. $(document).unbind("mousemove", this.moveEvent);
  61. if(this.clickEvent) $(this.element).unbind("click", this.clickEvent);
  62. },
  63. enable: function() {
  64. $(this.element).removeClass("ui-magnifier-disabled");
  65. this.disabled = false;
  66. },
  67. disable: function() {
  68. $(this.element).addClass("ui-magnifier-disabled");
  69. this.reset();
  70. this.disabled = true;
  71. },
  72. reset: function(e) {
  73. var o = this.options;
  74. var c;
  75. var distance = 1;
  76. for(var i=0;i<this.items.length;i++) {
  77. c = this.items[i];
  78. $(c[0]).css({
  79. width: c[2][0],
  80. height: c[2][1],
  81. top: (c[3] ? c[3].top : 0),
  82. left: (c[3] ? c[3].left : 0)
  83. });
  84. if(o.opacity)
  85. $(c[0]).css('opacity', o.opacity.min);
  86. if(o.zIndex)
  87. $(c[0]).css("z-index", "");
  88. }
  89. },
  90. magnify: function(e) {
  91. var p = [e.pageX,e.pageY];
  92. var o = this.options;
  93. var c;
  94. this.current = this.items[0];
  95. var distance = 1;
  96. //Compute the parents distance, because we don't need to fire anything if we are not near the parent
  97. var overlap = ((p[0] > this.pp.left-o.distance && p[0] < this.pp.left + this.element.offsetWidth + o.distance) && (p[1] > this.pp.top-o.distance && p[1] < this.pp.top + this.element.offsetHeight + o.distance));
  98. if(!overlap) return false;
  99. for(var i=0;i<this.items.length;i++) {
  100. c = this.items[i];
  101. var olddistance = distance;
  102. if(!o.axis) {
  103. distance = Math.sqrt(
  104. Math.pow(p[0] - ((c[3] ? this.pp.left : c[1].left) + parseInt(c[0].style.left)) - (c[0].offsetWidth/2), 2)
  105. + Math.pow(p[1] - ((c[3] ? this.pp.top : c[1].top ) + parseInt(c[0].style.top )) - (c[0].offsetHeight/2), 2)
  106. );
  107. } else {
  108. if(o.axis == "y") {
  109. distance = Math.abs(p[1] - ((c[3] ? this.pp.top : c[1].top ) + parseInt(c[0].style.top )) - (c[0].offsetHeight/2));
  110. } else {
  111. distance = Math.abs(p[0] - ((c[3] ? this.pp.left : c[1].left) + parseInt(c[0].style.left)) - (c[0].offsetWidth/2));
  112. }
  113. }
  114. if(distance < o.distance) {
  115. this.current = distance < olddistance ? this.items[i] : this.current;
  116. if(!o.axis || o.axis != "y") {
  117. $(c[0]).css({
  118. width: c[2][0]+ (c[2][0] * (o.magnification-1)) - (((distance/o.distance)*c[2][0]) * (o.magnification-1)),
  119. left: (c[3] ? (c[3].left + o.verticalLine * ((c[2][1] * (o.magnification-1)) - (((distance/o.distance)*c[2][1]) * (o.magnification-1)))) : 0)
  120. });
  121. }
  122. if(!o.axis || o.axis != "x") {
  123. $(c[0]).css({
  124. height: c[2][1]+ (c[2][1] * (o.magnification-1)) - (((distance/o.distance)*c[2][1]) * (o.magnification-1)),
  125. top: (c[3] ? c[3].top : 0) + (o.baseline-0.5) * ((c[2][0] * (o.magnification-1)) - (((distance/o.distance)*c[2][0]) * (o.magnification-1)))
  126. });
  127. }
  128. if(o.opacity)
  129. $(c[0]).css('opacity', o.opacity.max-(distance/o.distance) < o.opacity.min ? o.opacity.min : o.opacity.max-(distance/o.distance));
  130. } else {
  131. $(c[0]).css({
  132. width: c[2][0],
  133. height: c[2][1],
  134. top: (c[3] ? c[3].top : 0),
  135. left: (c[3] ? c[3].left : 0)
  136. });
  137. if(o.opacity)
  138. $(c[0]).css('opacity', o.opacity.min);
  139. }
  140. if(o.zIndex)
  141. $(c[0]).css("z-index", "");
  142. }
  143. if(this.options.zIndex)
  144. $(this.current[0]).css("z-index", this.options.zIndex);
  145. }
  146. });
  147. })($);