Skip to content

Commit

Permalink
feat(pauseResume): Fix jshint error and apply JSCS.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jongmoon Yoon committed Mar 9, 2016
1 parent f378d24 commit 12012c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
3 changes: 2 additions & 1 deletion config/dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
"src/customEvent/scrollEnd.js",
"src/hook/animate.js",
"src/hook/css.js",
"src/hook/pauseResume.js",
"src/plugin/apply.js",
"src/plugin/persist.js"
],
Expand All @@ -27,4 +28,4 @@ module.exports = {
"bower_components/outlayer/item.js",
"bower_components/outlayer/outlayer.js"
]
};
};
103 changes: 65 additions & 38 deletions src/hook/pauseResume.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
function now() {
return new Date().getTime();
}

function AniProperty(prop, optall, prevProp, isFirst) {
this.opt = optall;
this.start = -1;
Expand All @@ -23,27 +23,38 @@

for (var property in prop) {
var propValue = prop[property];
var prevProp;
var markIndex;

//If it has a absoulte value.
if (typeof(propValue) !== "string" ||
if (typeof(propValue) !== "string" ||
(markIndex = propValue.search(/[+|-]=/)) < 0) {
this.absoulteProp[property] = propValue;
this.absoulteProp[property] = propValue;
continue;
}

var sign = propValue.charAt(markIndex) === "-" ? -1 : 1;

//If it has a relative value
this.absoulteProp[property] = propValue.replace(/([-|+])*([\d|\.])+/g, function(match) {
var currValue = prevProp[property] ? parseFloat(prevProp[property]) : 0;
return currValue + Number(match) * sign;
}).replace(/[-|+]+=/g, "");
this.absoulteProp[property] = propValue
.replace(/([-|+])*([\d|\.])+/g,
generateAbsoluteValMaker(prevProp, property, sign))
.replace(/[-|+]+=/g, "");
}

this.prop = this.absoulteProp;
// console.log("prop", this.absoulteProp);
};
}

/**
* Generate a new absolute value maker.
*
* function to avoid JS Hint error "Don't make functions within a loop"
*/
function generateAbsoluteValMaker(prevProp, propName, sign) {
return function absoluteValMaker(match) {
var currValue = prevProp[propName] ? parseFloat(prevProp[propName]) : 0;
return currValue + Number(match) * sign;
};
}

AniProperty.prototype.init = function() {
this.start = now();
Expand All @@ -52,14 +63,15 @@

AniProperty.prototype.addEasingFn = function(easingName) {
this.easingNames.push(easingName);
}
};

AniProperty.prototype.clearEasingFn = function() {
while(easing = this.easingNames.shift()) {
var easing;
while (easing = this.easingNames.shift()) {
delete $.easing[easing];
}
this.easingNames = [];
}
};

function addAniProperty(prop, optall) {
var prevProp;
Expand All @@ -75,6 +87,7 @@

var newProp = new AniProperty(prop, optall, prevProp, isFirst);
this.__aniProps = this.__aniProps || [];

//Animation is excuted immediately.
if (this.__aniProps.length === 0) {
newProp.init();
Expand All @@ -83,18 +96,21 @@
}

function getOptAll(speed, easing, fn) {
var opt = speed && typeof speed === "object" ? jQuery.extend({}, speed) : {
var opt = speed && typeof speed === "object" ?
jQuery.extend({}, speed) : {
complete: fn || !fn && easing ||
jQuery.isFunction( speed ) && speed,
jQuery.isFunction(speed) && speed,
duration: speed,
easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
easing: fn && easing ||
easing && !jQuery.isFunction(easing) && easing
};

opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ?
opt.duration : opt.duration in jQuery.fx.speeds ?
jQuery.fx.speeds[opt.duration] : jQuery.fx.speeds._default;

// normalize opt.queue - true/undefined/null -> "fx"
if ( opt.queue == null || opt.queue === true ) {
if (opt.queue == null || opt.queue === true) {
opt.queue = "fx";
}

Expand All @@ -116,7 +132,7 @@
if (orginalComplete && typeof(orginalComplete) === "function") {
orginalComplete.call(this);
}
}
};

this.each(function() {
//Queue animation property to recover the current animation.
Expand All @@ -130,9 +146,9 @@
return this.each(function() {
var p;
var type = "fx";

if (!this.__aniProps) {
console.warn("Current element doesn't have animation information. Check 'animate' is applied to this element.");
// console.warn("Current element doesn't have animation information. Check 'animate' is applied to this element.");
return;
}

Expand All @@ -151,7 +167,6 @@
p.paused = true;
}
});

};

function dummy() {
Expand All @@ -166,21 +181,21 @@
var i;

if (!this.__aniProps) {
console.warn("Current element doesn't have animation information. Check 'animate' is applied to this element.");
// console.warn("Current element doesn't have animation information. Check 'animate' is applied to this element.");
return;
}

if (this.__aniProps.length === 0 || !this.__aniProps[0].paused) {
return;
}

//Clear fx-queue,
//Clear fx-queue,
//And this queue will be initialized by animate call.
$.queue(this, type || "fx", []);

// Restore __aniProps
i = 0;
while(p = this.__aniProps[i]) {
while (p = this.__aniProps[i]) {
// Restore easing status
if (p.elapsed > 0 && p.opt.easing) {
var resumePercent = p.elapsed / p.opt.duration;
Expand All @@ -191,10 +206,8 @@
var newEasingName = p.opt.easing + "_" + p.uuid;

// Make new easing function that continues from pause point.
$.easing[newEasingName] = function(percent) {
var newPercent = resumePercent + remainPercent * percent;
return scale(originalEasing(newPercent));
};
$.easing[newEasingName] = generateNewEasingFunc(
resumePercent, remainPercent, scale, originalEasing);
p.opt.easing = newEasingName;

//Store new easing function to clear it later.
Expand All @@ -215,7 +228,19 @@
});
};

$.fn.stop = function(type, clearQueue, gotoEnd) {
/**
* Generate a new easing function.
*
* function to avoid JS Hint error "Don't make functions within a loop"
*/
function generateNewEasingFunc(resumePercent, remainPercent, scale, originalEasing) {
return function easingFunc(percent) {
var newPercent = resumePercent + remainPercent * percent;
return scale(originalEasing(newPercent));
};
}

$.fn.stop = function(type, clearQueue) {
var clearQ = clearQueue;
stopFn.apply(this, arguments);

Expand All @@ -224,19 +249,21 @@
}

return this.each(function() {
var p;

if (!clearQ) {
return;
}

//If clearQueue is requested,
//then all properties must be initialized
//then all properties must be initialized
//for element not to be resumed.
while(p = this.__aniProps.shift()) {
while (p = this.__aniProps.shift()) {
p.clearEasingFn();
};
}
this.__aniProps = [];
})
}
});
};

//Adopt linear scale from d3
function scaler(domain, range) {
Expand Down

0 comments on commit 12012c8

Please sign in to comment.