Passed
Push — master ( 33081a...0dd98d )
by Paul
05:28
created

+/scripts/admin/pinned.js   B

Complexity

Conditions 1
Paths 32

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 32
nop 1
dl 0
loc 88
rs 8.6012
c 1
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A Pinned.save_ 0 6 2
A Pinned.onClickEdit_ 0 8 2
A Pinned.onClickCancel_ 0 5 2
A Pinned.togglePinned_ 0 3 2
A Pinned.onClickSave_ 0 12 1
A Pinned.request_ 0 7 1
A Pinned.onClickToggle_ 0 10 1
A pinned.js ➔ Pinned 0 6 2
A Pinned.init_ 0 6 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
/** global: GLSR, jQuery */
2
;(function( x ) {
3
4
	'use strict';
5
6
	var Pinned = function() {
7
		this.el = x( '#pinned-status-select' );
8
		this.target = null;
9
		if( !this.el )return;
10
		this.init_();
11
	};
12
13
	Pinned.prototype = {
14
		/** @return void */
15
		init_: function() {
16
			x( 'a.cancel-pinned-status' ).on( 'click', this.onClickCancel_.bind( this ));
17
			x( 'a.edit-pinned-status' ).on( 'click', this.onClickEdit_.bind( this ));
18
			x( 'a.save-pinned-status' ).on( 'click', this.onClickSave_.bind( this ));
19
			x( 'table td.pinned i' ).on( 'click', this.onClickToggle_.bind( this ));
20
		},
21
22
		/** @return void */
23
		onClickCancel_: function( ev ) { // MouseEvent
24
			ev.preventDefault();
25
			this.el.slideUp( 'fast' ).siblings( 'a.edit-pinned-status' ).show().focus();
26
			this.el.find( 'select' ).val( x( '#hidden-pinned-status' ).val() === '0' ? 1 : 0 );
27
		},
28
29
		/** @return void */
30
		onClickEdit_: function( ev ) { // MouseEvent
31
			ev.preventDefault();
32
			if( !this.el.is( ':hidden' ))return;
33
			this.el.slideDown( 'fast', function() {
34
				this.el.find( 'select' ).focus();
35
			}.bind( this ));
36
			x( this.el ).hide();
37
		},
38
39
		/** @return void */
40
		onClickSave_: function( ev ) { // MouseEvent
41
			ev.preventDefault();
42
			this.el.slideUp( 'fast' ).siblings( 'a.edit-pinned-status' ).show().focus();
43
			this.target = ev.target;
44
			var request = {
45
				action: 'toggle-pinned',
46
				id: x( '#post_ID' ).val(),
47
				nonce: GLSR.pinned_nonce,
48
				pinned: x( '#pinned-status' ).val(),
49
			};
50
			this.request_( request, this.save_.bind( this ));
51
		},
52
53
		/** @return void */
54
		onClickToggle_: function( ev ) { // MouseEvent
55
			ev.preventDefault();
56
			this.target = ev.target;
57
			var request = {
58
				action: 'toggle-pinned',
59
				id: ev.target.getAttribute( 'data-id' ),
60
				nonce: GLSR.pinned_nonce,
61
			};
62
			this.request_( request, this.togglePinned_.bind( this ));
63
		},
64
65
		/** @return void */
66
		request_: function( request, callback ) { // object, function
67
			var data = {
68
				action: GLSR.action,
69
				request: request,
70
			};
71
			x.post( GLSR.ajaxurl, data, callback.bind( this ));
72
		},
73
74
		/** @return void */
75
		save_: function( response ) {
76
			x( '#pinned-status' ).val( !response.pinned|0 );
77
			x( '#hidden-pinned-status' ).val( response.pinned|0 );
78
			x( '#pinned-status-text' ).text( response.pinned ? this.target.dataset.yes : this.target.dataset.no );
79
			GLSR.Notices( response.notices );
80
		},
81
82
		/** @return void */
83
		togglePinned_: function( response ) {
84
			this.target.classList[response.pinned ? 'add' : 'remove']( 'pinned' );
85
		},
86
	};
87
88
	GLSR.Pinned = Pinned;
89
})( jQuery );
90