Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1 from flesler/master

Cleanup + Fixes
  • Loading branch information...
commit ed0d6077fd96c7c528f3ba67269638c7e01f7c8a 2 parents fc4bdf6 + 04ddc71
@damianmr damianmr authored
Showing with 55 additions and 33 deletions.
  1. +22 −33 jquery-transforms.js
  2. +33 −0 test/jquery-transforms.test.js
View
55 jquery-transforms.js
@@ -103,13 +103,16 @@
var TRANSFORM_ORIGIN_PROPERTY = BROWSER_PREFIX + 'transform-origin';
+ function def(val) {
+ return typeof val !== 'undefined';
+ }
+
/**
* Sets/Gets the transform origin.
*/
$.fn.clearTransforms = $.fn.clearTransform = function() {
- return $(this).each(function() {
+ return this.css(TRANSFORM_PROPERTY, 'none').each(function() {
delete this.cssTransformations;
- $(this).css(TRANSFORM_PROPERTY, 'none');
});
};
@@ -117,14 +120,10 @@
* Sets/Gets the transformation origin (pixels)
*/
$.fn.origin = function(x, y) {
- if (x && y) {
- return $(this).each(function() {
- var o = {};
- o[TRANSFORM_ORIGIN_PROPERTY] = x + 'px ' + y + 'px';
- $(this).css(o);
- });
+ if (def(x) && def(y)) {
+ return this.css(TRANSFORM_ORIGIN_PROPERTY, x + 'px ' + y + 'px');
} else {
- var cssValue = $(this).css(TRANSFORM_ORIGIN_PROPERTY);
+ var cssValue = this.css(TRANSFORM_ORIGIN_PROPERTY);
var values = cssValue.match(/[-]?[\d]*[.]?[\d]*px/gi);
// var unit = cssValue.match() // TODO
var top, left;
@@ -142,13 +141,11 @@
* Translate the element the given amount of pixels. Uses hardware acceleration when available.
*/
$.fn.translate = function(x,y) {
- if (x && y) {
+ if (def(x) && def(y)) {
var translate = TRANSLATE_TRANSFORM.replace('$value.x$', x).replace('$value.y$', y);
- return this.each(function() {
- $(this).transform(translate);
- });
+ return this.transform(translate);
} else {
- var translateValue = $(this).transform(TRANSLATE_TRANSFORM.substr(0, TRANSLATE_TRANSFORM.indexOf('(')));
+ var translateValue = this.transform(TRANSLATE_TRANSFORM.substr(0, TRANSLATE_TRANSFORM.indexOf('(')));
translateValue = translateValue.substr(1, translateValue.indexOf(')') - 1);
xy = translateValue.match(/[-]?[\d]*[.]?[\d]/gi);
return {
@@ -167,13 +164,11 @@
* @param value the scale (1 is normal value).
*/
$.fn.scale = function(value) {
- if (value) {
+ if (def(value)) {
var scale = SCALE_TRANSFORM.replace('$value$', value);
- return this.each(function() {
- $(this).transform(scale);
- });
+ return this.transform(scale);
} else {
- var scaleValue = $(this).transform(SCALE_TRANSFORM.substr(0, SCALE_TRANSFORM.indexOf('(')));
+ var scaleValue = this.transform(SCALE_TRANSFORM.substr(0, SCALE_TRANSFORM.indexOf('(')));
scaleValue = scaleValue == null ? '(1)' : scaleValue;
// '(xxx.xx)' turns into -> xxx.xx (number)
return Number(scaleValue.substr(1, scaleValue.length - 2));
@@ -185,17 +180,14 @@
* @param val the amount of degrees you want to use.
*/
$.fn.rotate = function(val) {
- if (typeof val !== 'undefined') {
- var rotate = ROTATE_TRANSFORM.replace('$value$', val);
+ if (def(val)) {
val = parseFloat(val) || 0;
+ var rotate = ROTATE_TRANSFORM.replace('$value$', val);
- return this.each(function() {
- var $el = $(this);
- $el.transform(rotate);
- });
+ return this.transform(rotate);
} else {
var rotateFunction = ROTATE_TRANSFORM.substr(0, ROTATE_TRANSFORM.indexOf('('));
- var rotationValue = $(this).transform(rotateFunction);
+ var rotationValue = this.transform(rotateFunction);
var unit;
if (rotationValue == null) {
@@ -223,17 +215,14 @@
var cssFunction,
cssFunctionValues,
isValidTransform,
- isGetting = false,
- el;
+ el = this[0];
if (!cssTransform) {
- el = $(this)[0];
return el.cssTransformations || 'none';
}
if (cssTransform.indexOf('(') == -1) {
cssFunction = cssTransform;
- el = $(this)[0];
return (el.cssTransformations && el.cssTransformations[cssFunction]) || null;
} else {
@@ -248,8 +237,8 @@
}
return this.each(function(){
- var $el = $(this);
- var el = $(this)[0];
+ el = this;
+
var transformString = '';
// Using the element to store the data is more performant than
@@ -259,7 +248,7 @@
for (var transform in currentTransformations) {
transformString += transform + currentTransformations[transform] + ' ';
}
- $el.css(TRANSFORM_PROPERTY, transformString);
+ $(el).css(TRANSFORM_PROPERTY, transformString);
el.cssTransformations = currentTransformations;
});
};
View
33 test/jquery-transforms.test.js
@@ -47,4 +47,37 @@ describe('jQuery Transforms', function() {
expect(rotation.unit).toBe('deg');
});
+ it('Should support falsy values', function() {
+ element.translate(0, 0);
+ var translation = element.translate();
+ expect(translation.x).toBe(0);
+ expect(translation.y).toBe(0);
+
+ element.rotate(0);
+ var rotation = element.rotate();
+ expect(rotation.value).toBe(0);
+
+ element.scale(0);
+ var scale = element.scale();
+ expect(scale).toBe(0);
+ });
+
+
+ it('Should support strings', function() {
+ element.translate('10', '10');
+ var translation = element.translate();
+ expect(translation.x).toBe(10);
+ expect(translation.y).toBe(10);
+
+ // Note: Doesn't really support setting the unit like this yet
+ element.rotate('10deg');
+ var rotation = element.rotate();
+ expect(rotation.value).toBe(10);
+ expect(rotation.unit).toBe('deg');
+
+ element.scale('10');
+ var scale = element.scale();
+ expect(scale).toBe(10);
+ });
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.