Completed
Push — master ( 243c15...8baeba )
by Rafael
04:09
created

colorPalette.bindTransitions   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 17
dl 0
loc 79
rs 6.0572
cc 8
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
var self, colorPalette,
2
	$ = window.jQuery,
3
	$window = $( window );
4
5
window.BOLDGRID = window.BOLDGRID || {};
6
window.BOLDGRID.COLOR_PALETTE = window.BOLDGRID.COLOR_PALETTE || {};
7
window.BOLDGRID.COLOR_PALETTE.Modify = window.BOLDGRID.COLOR_PALETTE.Modify || {};
8
9
import BrehautColorJs from 'color-js/color';
10
11
colorPalette = window.BOLDGRID.COLOR_PALETTE.Modify;
12
self = colorPalette;
13
14
/**
15
 * Get the body of the previewer iframe.
16
 *
17
 * @since 1.1.7
18
 *
19
 * @return jQuery Body of iframe.
20
 */
21
colorPalette.getPreviewerBody = function() {
22
23
	if ( ! self.configs.enableCustomizerTransitions ) {
24
		return $();
25
	}
26
27
	// Get the previewer frame.
28
	return $( wp.customize.previewer.container )
0 ignored issues
show
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
29
		.find( 'iframe' ).last().contents().find( 'body' );
30
};
31
32
/**
33
 * Add the color palette transitions class to the body of the iframe.
34
 * This allows transitions to affect all elements, class is removed after the direation
35
 * of the transition.
36
 *
37
 * @since 1.1.7
38
 */
39
colorPalette.addColorTransition = function() {
40
	var $previewerBody,
41
		timeout = 600,
42
		curTime = new Date().getTime();
43
44
	if ( ! self.configs.enableCustomizerTransitions ) {
45
		return;
46
	}
47
48
	$previewerBody = colorPalette.getPreviewerBody();
49
50
	$previewerBody.addClass( 'color-palette-transitions duration-long' );
51
	self.lastTransitionTime = curTime;
52
	setTimeout( function() {
53
		if ( self.lastTransitionTime === curTime ) {
54
			$previewerBody.removeClass( 'color-palette-transitions duration-long' );
55
		}
56
	}, timeout );
57
};
58
59
/**
60
* Upon clicking a color in the active palette, fade in and out the color on the iframe.
61
*
62
* @since 1.1.7
63
*/
64
colorPalette.bindTransitions = function() {
65
66
	if ( ! self.configs.enableCustomizerTransitions ) {
67
		return;
68
	}
69
70
	self.$palette_control_wrapper.on( 'click', '.boldgrid-active-palette li', function( e ) {
71
		var transitionColor,
72
			backgroundColor,
73
			$previewerBody,
74
			timeStartedCompile,
75
			$this = $( this ),
76
			originalColor = $this.css( 'background-color' ),
77
			isNeutral = false,
78
			desiredDelay = 350,
79
			transitionDistance = 0.4,
80
			darknessThreshold = 0.5;
81
82
		if ( self.fadeEffectInProgress || ! e.originalEvent ) {
83
			return;
84
		}
85
86
		// Get the previewer frame.
87
		$previewerBody = colorPalette.getPreviewerBody();
88
89
		if ( self.hasNeutral && $this.is( ':last-of-type' ) ) {
90
			isNeutral = true;
91
		}
92
93
		// Calculate the color to transition to.
94
		backgroundColor = BrehautColorJs( originalColor );
95
		if ( backgroundColor.getLuminance() > darknessThreshold ) {
96
			transitionColor = backgroundColor.darkenByAmount( transitionDistance );
97
			transitionColor = transitionColor.toCSS();
98
		} else {
99
			transitionColor = backgroundColor.lightenByAmount( transitionDistance );
100
			transitionColor = transitionColor.toCSS();
101
		}
102
103
		// Set color to transition to.
104
		$this.css( 'background-color', transitionColor );
105
		if ( isNeutral ) {
106
			$this.closest( '.boldgrid-active-palette' ).attr( 'data-neutral-color', transitionColor );
107
		}
108
109
		// Enable transitions for the colors.
110
		$previewerBody.addClass( 'color-palette-transitions' );
111
112
		// Compile.
113
		colorPalette.update_theme_option();
114
115
		// Reset Color.
116
		$this.css( 'background-color', originalColor );
117
		if ( isNeutral ) {
118
			$this.closest( '.boldgrid-active-palette' ).attr( 'data-neutral-color', originalColor );
119
		}
120
121
		timeStartedCompile = new Date().getTime();
122
		self.fadeEffectInProgress = true;
123
124
		$window.one( 'boldgrid_sass_compile_done', function() {
125
			var timeout = 0,
126
				duration = new Date().getTime() - timeStartedCompile;
127
128
			// The compile to fade back in should trigger at a minimum time of desiredDelay.
129
130
			// If the compile time exceeds the min than the the timeout will be 0.
131
			if ( duration < desiredDelay ) {
132
				timeout = desiredDelay;
133
			}
134
135
			// Wait for compile to finish then fade back in.
136
			$window.one( 'boldgrid_sass_compile_done', function( event, data ) {
137
				setTimeout( function() {
138
					colorPalette.update_iframe( data );
139
					setTimeout( function() {
140
						$previewerBody.removeClass( 'color-palette-transitions' );
141
						self.fadeEffectInProgress = false;
142
					}, 250 );
143
				}, timeout );
144
			} );
145
146
			colorPalette.update_theme_option( { source: 'color_palette_focus' } );
147
		} );
148
	} );
149
};
150