src/css/adjustCSS.js

Maintainability

63.13

Lines of code

71

Created with Raphaël 2.1.002550751002016-7-102016-6-102016-5-112016-4-112016-3-122016-2-112016-1-12

2016-8-9
Maintainability: 63.13

Created with Raphaël 2.1.00204060802016-7-102016-6-102016-5-112016-4-112016-3-122016-2-112016-1-12

2016-8-9
Lines of Code: 71

Difficulty

29.65

Estimated Errors

0.39

Function weight

By Complexity

Created with Raphaël 2.1.0adjustCSS22

By SLOC

Created with Raphaël 2.1.0<anonymous>68
1
define( [
2
    "../core",
3
    "../var/rcssNum"
4
], function( jQuery, rcssNum ) {
5
 
6
"use strict";
7
 
8
function adjustCSS( elem, prop, valueParts, tween ) {
9
    var adjusted,
10
        scale = 1,
11
        maxIterations = 20,
12
        currentValue = tween ?
13
            function() {
14
                return tween.cur();
15
            } :
16
            function() {
17
                return jQuery.css( elem, prop, "" );
18
            },
19
        initial = currentValue(),
20
        unit = valueParts && valueParts[ 3 ] || ( jQuery.cssNumber[ prop ] ? "" : "px" ),
21
 
22
        // Starting value computation is required for potential unit mismatches
23
        initialInUnit = ( jQuery.cssNumber[ prop ] || unit !== "px" && +initial ) &&
24
            rcssNum.exec( jQuery.css( elem, prop ) );
25
 
26
    if ( initialInUnit && initialInUnit[ 3 ] !== unit ) {
27
 
28
        // Trust units reported by jQuery.css
29
        unit = unit || initialInUnit[ 3 ];
30
 
31
        // Make sure we update the tween properties later on
32
        valueParts = valueParts || [];
33
 
34
        // Iteratively approximate from a nonzero starting point
35
        initialInUnit = +initial || 1;
36
 
37
        do {
38
 
39
            // If previous iteration zeroed out, double until we get *something*.
40
            // Use string for doubling so we don't accidentally see scale as unchanged below
41
            scale = scale || ".5";
42
 
43
            // Adjust and apply
44
            initialInUnit = initialInUnit / scale;
45
            jQuery.style( elem, prop, initialInUnit + unit );
46
 
47
        // Update scale, tolerating zero or NaN from tween.cur()
48
        // Break the loop if scale is unchanged or perfect, or if we've just had enough.
49
        } while (
50
            scale !== ( scale = currentValue() / initial ) && scale !== 1 && --maxIterations
51
        );
52
    }
53
 
54
    if ( valueParts ) {
55
        initialInUnit = +initialInUnit || +initial || 0;
56
 
57
        // Apply relative offset (+=/-=) if specified
58
        adjusted = valueParts[ 1 ] ?
59
            initialInUnit + ( valueParts[ 1 ] + 1 ) * valueParts[ 2 ] :
60
            +valueParts[ 2 ];
61
        if ( tween ) {
62
            tween.unit = unit;
63
            tween.start = initialInUnit;
64
            tween.end = adjusted;
65
        }
66
    }
67
    return adjusted;
68
}
69
 
70
return adjustCSS;
71
} );