Completed
Pull Request — master (#1653)
by Aristeides
04:35 queued 02:29
created

modules/tooltips/tooltip.js   A

Complexity

Total Complexity 15
Complexity/F 1.88

Size

Lines of Code 54
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 2
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 15
mnd 2
bc 15
fnc 8
bpm 1.875
cpm 1.875
noi 1
1
/* global kirkiTooltips */
2
jQuery( document ).ready( function() {
3
4
	function kirkiTooltipAdd( control ) {
5
		_.each( kirkiTooltips, function( tooltip ) {
6
			var trigger,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
7
			    controlID,
8
			    content;
9
10
			if ( tooltip.id !== control.id ) {
11
				return;
12
			}
13
14
			if ( control.container.find( '.tooltip-content' ).length ) {
15
				return;
16
			}
17
18
			trigger   = '<span class="tooltip-trigger" data-setting="' + tooltip.id + '"><span class="dashicons dashicons-editor-help"></span></span>';
19
			controlID = '#customize-control-' + tooltip.id;
20
			content   = '<div class="tooltip-content hidden" data-setting="' + tooltip.id + '">' + tooltip.content + '</div>';
21
22
			// Add the trigger & content.
23
			jQuery( '<div class="tooltip-wrapper">' + trigger + content + '</div>' ).prependTo( controlID );
24
25
			// Handle onclick events.
26
			jQuery( '.tooltip-trigger[data-setting="' + tooltip.id + '"]' ).on( 'click', function() {
27
				jQuery( '.tooltip-content[data-setting="' + tooltip.id + '"]' ).toggleClass( 'hidden' );
28
			});
29
		});
30
31
		// Close tooltips if we click anywhere else.
32
		jQuery( document ).mouseup( function( e ) {
33
34
			if ( ! jQuery( '.tooltip-content' ).is( e.target ) ) {
35
				if ( ! jQuery( '.tooltip-content' ).hasClass( 'hidden' ) ) {
36
					jQuery( '.tooltip-content' ).addClass( 'hidden' );
37
				}
38
		    }
39
		});
40
	}
41
42
	wp.customize.control.each( function( control ) {
43
		wp.customize.section( control.section(), function( section ) {
44
			if ( section.expanded() || wp.customize.settings.autofocus.control === control.id ) {
45
				kirkiTooltipAdd( control );
46
			} else {
47
				section.expanded.bind( function( expanded ) {
48
					if ( expanded ) {
49
						kirkiTooltipAdd( control );
50
					}
51
				} );
52
			}
53
		} );
54
	});
55
} );
56