Passed
Push — master ( 06bde6...6d2f62 )
by Paul
04:59
created

+/scripts/admin/pinned.js   B

Complexity

Conditions 1
Paths 32

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

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

8 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 11 1
A Pinned.restoreEditLink_ 0 4 1
A Pinned.onClickToggle_ 0 9 1
A pinned.js ➔ Pinned 0 12 2

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