Skip to content

Commit

Permalink
factor out event construction
Browse files Browse the repository at this point in the history
No need to repeat the event construction block four times, easier to
have a helper method.
  • Loading branch information
scottmadinsonos committed Mar 24, 2016
1 parent 976f2ba commit 29881a4
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions js/simple-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var __slice = [].slice,
SimpleSlider = (function() {

function SimpleSlider(input, options) {
var ratio,
_this = this;
var _this = this;
this.input = input;
this.defaultOptions = {
animate: true,
Expand Down Expand Up @@ -78,15 +77,7 @@ var __slice = [].slice,
}
_this.dragging = true;
_this.dragger.addClass("dragging");
ratio = _this.valueToRatio(this.value);
var eventData = {
value: _this.value,
ratio: ratio,
position: ratio * _this.slider.outerWidth(),
el: _this.slider
};

_this.input.trigger("slider:drag-started", eventData);
_this.input.trigger("slider:drag-started", _this._makeEventData());
_this.domDrag(e.pageX, e.pageY);
return false;
});
Expand All @@ -101,15 +92,7 @@ var __slice = [].slice,
if (_this.dragging) {
_this.dragging = false;
_this.dragger.removeClass("dragging");
ratio = _this.valueToRatio(this.value);
var eventData = {
value: _this.value,
ratio: ratio,
position: ratio * _this.slider.outerWidth(),
el: _this.slider
};

_this.input.trigger("slider:drag-ended", eventData);
_this.input.trigger("slider:drag-ended", _this._makeEventData());
// Post final position here
_this.domDrag(e.pageX, e.pageY, false);
return $("body").css({
Expand All @@ -125,13 +108,7 @@ var __slice = [].slice,
this.value = this.nearestValidValue(this.input.val());
}
this.setSliderPositionFromValue(this.value);
ratio = this.valueToRatio(this.value);
this.input.trigger("slider:ready", {
value: this.value,
ratio: ratio,
position: ratio * this.slider.outerWidth(),
el: this.slider
});
this.input.trigger("slider:ready", this._makeEventData());
}

SimpleSlider.prototype.createDivElement = function(classname) {
Expand Down Expand Up @@ -312,14 +289,20 @@ var __slice = [].slice,
return;
}
this.value = value;
eventData = {
value: value,
ratio: ratio,
position: ratio * this.slider.outerWidth(),
eventData = this._makeEventData(value, ratio, trigger);
return this.input.val(value).trigger($.Event("change", eventData)).trigger("slider:changed", eventData);
};

SimpleSlider.prototype._makeEventData = function(value, ratio, trigger) {
var v = value || this.value,
r = ratio || this.valueToRatio(v);
return {
value: v,
ratio: r,
position: r * this.slider.outerWidth(),
trigger: trigger,
el: this.slider
};
return this.input.val(value).trigger($.Event("change", eventData)).trigger("slider:changed", eventData);
};

return SimpleSlider;
Expand Down

0 comments on commit 29881a4

Please sign in to comment.