Passed
Push — master ( e448bf...51995c )
by Paul
07:24 queued 04:56
created

+/main.js   A

Complexity

Total Complexity 18
Complexity/F 1.38

Size

Lines of Code 105
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 18
c 1
b 0
f 0
nc 4
mnd 1
bc 16
fnc 13
dl 0
loc 105
rs 10
bpm 1.2306
cpm 1.3846
noi 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A pollux.media.featured.select 0 6 3
A main.js ➔ jQuery 0 4 1
A pollux.media.featured.set 0 11 1
A pollux.metabox.init 0 11 1
A pollux.metabox.setVisibility 0 12 2
A pollux.metabox.onChangeValue 0 4 1
A pollux.metabox.hasValue 0 7 2
A pollux.media.featured.init 0 15 1
1
/** global: wp */
2
3
var pollux = {
4
	media: {
5
		featured: {},
6
	},
7
	metabox: {},
8
};
9
10
/**
11
 * @return void
12
 */
13
pollux.media.featured.init = function()
14
{
15
	jQuery( '#postimagediv' )
16
	.on( 'click', '#pollux-set-featured', function( ev ) {
17
		ev.preventDefault();
18
		wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() );
19
		pollux.media.featured.frame = wp.media.featuredImage.frame;
20
		pollux.media.featured.frame().open();
21
	})
22
	.on( 'click', '#pollux-remove-featured', function( ev ) {
23
		ev.preventDefault();
24
		pollux.media.featured.frame = wp.media.featuredImage.frame;
25
		pollux.media.featured.set(-1);
26
	});
27
};
28
29
/**
30
 * @return void
31
 */
32
pollux.media.featured.select = function()
33
{
34
	if( !wp.media.view.settings.post.featuredImageId )return;
35
	var selection = this.get( 'selection' ).single();
36
	pollux.media.featured.set( selection ? selection.id : -1 );
37
};
38
39
/**
40
 * @return void
41
 */
42
pollux.media.featured.set = function( id )
43
{
44
	wp.media.view.settings.post.featuredImageId = Math.round( id );
45
	wp.media.post( 'pollux/archives/featured/html', {
46
		_wpnonce: jQuery( '#_wpnonce' ).val(),
47
		post_type: jQuery( '#archive-type' ).val(),
48
		thumbnail_id: id,
49
	}).done( function( html ) {
50
		jQuery( '.inside', '#postimagediv' ).html( html );
51
	});
52
};
53
54
/**
55
 * @return bool
56
 */
57
pollux.metabox.hasValue = function( el )
58
{
59
	if( el.type === 'checkbox' ) {
60
		return el.checked === true;
61
	}
62
	return el.value !== '';
63
};
64
65
/**
66
 * @return void
67
 */
68
pollux.metabox.init = function()
69
{
70
	var depends = document.querySelectorAll( '.rwmb-input [data-depends]' );
71
	[].forEach.call( depends, function( el ) {
72
		var dependency = pollux.metabox.setVisibility( el );
73
		var event = dependency.type === 'checkbox' ? 'change' : 'keyup';
74
		dependency.addEventListener( event, function() {
75
			pollux.metabox.onChangeValue( el );
76
		}, false );
77
	});
78
};
79
80
/**
81
 * @return void
82
 */
83
pollux.metabox.onChangeValue = function( el )
84
{
85
	pollux.metabox.setVisibility( el );
86
};
87
88
/**
89
 * @return element
90
 */
91
pollux.metabox.setVisibility = function( el )
92
{
93
	var dependency = document.getElementById( el.getAttribute( 'data-depends' ));
94
	var field = el.closest( '.rwmb-field' );
95
	if( pollux.metabox.hasValue( dependency )) {
96
		field.classList.remove( 'hidden' );
97
	}
98
	else {
99
		field.classList.add( 'hidden' );
100
	}
101
	return dependency;
102
};
103
104
jQuery(function() {
105
	pollux.media.featured.init();
106
	pollux.metabox.init();
107
});
108