Magnifier: Partial cleanup.

This commit is contained in:
Scott González 2008-07-15 01:32:24 +00:00
parent 5750375701
commit de8a6ea866

View File

@ -12,28 +12,32 @@
*/
(function($) {
var counter = 0;
$.widget("ui.magnifier", {
init: function() {
var self = this,
o = this.options;
var self = this, o = this.options;
this.items = [];
this.element.addClass("ui-magnifier");
if(!(/^(r|a)/).test(this.element.css("position"))) this.element.css("position", "relative");
if (!(/^(r|a)/).test(this.element.css("position"))) {
this.element.css("position", "relative");
}
this.pp = this.element.offset();
$(o.items, this.element).each(function() {
this.items = [];
this.element.find(o.items).each(function() {
var $this = $(this);
// TODO: use a hash so references to this data is readable
self.items.push([
this,
$(this).offset(),
[$(this).width(),$(this).height()],
(o.overlap ? $(this).position() : null)
$this.offset(),
[$this.width(),$this.height()],
(o.overlap ? $this.position() : null)
]);
if(o.opacity)
$(this).css('opacity', o.opacity.min);
(o.opacity && $this.css('opacity', o.opacity.min));
});
if (o.overlap) {
@ -44,42 +48,44 @@ $.widget("ui.magnifier", {
top: this.items[i][3].top,
left: this.items[i][3].left
});
};
}
}
this.identifier = ++$.ui.magnifier.counter;
this.identifier = ++counter;
$(document).bind("mousemove.magnifier"+this.identifier, function(e) {
if(!self.disabled) self.magnify.apply(self, [e]);
(self.disabled || self.magnify.apply(self, [e]));
});
if (o.click) {
this.element.bind('click.magnifier', function(e) {
if(!self.disabled) o.click.apply(this, [e, { options: self.options, current: self.current[0], currentOffset: self.current[1] }]);
if (!self.disabled) {
o.click.apply(this, [e, {
options: self.options,
current: self.current[0],
currentOffset: self.current[1]
}]);
}
});
}
},
destroy: function() {
this.reset();
this.element
.removeClass("ui-magnifier")
.removeClass("ui-magnifier-disabled")
.removeClass("ui-magnifier ui-magnifier-disabled")
.unbind(".magnifier");
$(document).unbind("mousemove.magnifier"+this.identifier);
},
disable: function() {
this.reset();
this.element.addClass("ui-magnifier-disabled");
this.options.disabled = true;
$.widget.prototype.disable.apply(this, arguments);
},
reset: function(e) {
var o = this.options, c, distance = 1;
reset: function(e) {
var o = this.options, c;
for (var i=0; i < this.items.length; i++) {
c = this.items[i];
$(c[0]).css({
@ -89,26 +95,20 @@ $.widget("ui.magnifier", {
left: (c[3] ? c[3].left : 0)
});
if(o.opacity)
$(c[0]).css('opacity', o.opacity.min);
if(o.zIndex)
$(c[0]).css("z-index", "");
(o.opacity && $(c[0]).css('opacity', o.opacity.min));
(o.zIndex && $(c[0]).css("z-index", ""));
}
},
magnify: function(e) {
magnify: function(e) {
var p = [e.pageX,e.pageY], o = this.options, c, distance = 1;
this.current = this.items[0];
//Compute the parents distance, because we don't need to fire anything if we are not near the parent
var overlap = ((p[0] > this.pp.left-o.distance && p[0] < this.pp.left + this.element[0].offsetWidth + o.distance) && (p[1] > this.pp.top-o.distance && p[1] < this.pp.top + this.element[0].offsetHeight + o.distance));
if(!overlap) return false;
if (!overlap) { return false; }
for (var i=0; i<this.items.length; i++) {
c = this.items[i];
var olddistance = distance;
@ -126,7 +126,6 @@ $.widget("ui.magnifier", {
}
if (distance < o.distance) {
this.current = distance < olddistance ? this.items[i] : this.current;
if (!o.axis || o.axis != "y") {
@ -143,11 +142,10 @@ $.widget("ui.magnifier", {
});
}
if(o.opacity)
if (o.opacity) {
$(c[0]).css('opacity', o.opacity.max-(distance/o.distance) < o.opacity.min ? o.opacity.min : o.opacity.max-(distance/o.distance));
}
} else {
$(c[0]).css({
width: c[2][0],
height: c[2][1],
@ -155,26 +153,18 @@ $.widget("ui.magnifier", {
left: (c[3] ? c[3].left : 0)
});
if(o.opacity)
$(c[0]).css('opacity', o.opacity.min);
(o.opacity && $(c[0]).css('opacity', o.opacity.min));
}
if(o.zIndex)
$(c[0]).css("z-index", "");
(o.zIndex && $(c[0]).css("z-index", ""));
}
if(this.options.zIndex)
$(this.current[0]).css("z-index", this.options.zIndex);
(this.options.zIndex &&
$(this.current[0]).css("z-index", this.options.zIndex));
}
});
$.extend($.ui.magnifier, {
counter: 0,
defaults: {
distance: 150,
magnification: 2,