Completed
Push — master ( 9bbfca...6621c7 )
by J.D.
03:38
created

Extension.extend.initReaction   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

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

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 8 2
A 0 12 1
1
/**
2
 * @summary Disable hook extension controller object.
3
 *
4
 * @since 2.3.0
5
 *
6
 * @module
7
 */
8
9
var Extension = wp.wordpoints.hooks.controller.Extension,
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...
10
	template = wp.wordpoints.hooks.template,
11
	Disable;
12
13
/**
14
 * wp.wordpoints.hooks.extension.Disable
15
 *
16
 * @since 2.3.0
17
 *
18
 * @class
19
 * @augments Backbone.Model
20
 * @augments wp.wordpoints.hooks.controller.Extension
21
 */
22
Disable = Extension.extend({
23
24
	/**
25
	 * @since 2.3.0
26
	 */
27
	defaults: {
28
		slug: 'disable'
29
	},
30
31
	/**
32
	 * @summary The template for the extension settings.
33
	 *
34
	 * @since 2.3.0
35
	 */
36
	template: template( 'hook-disable' ),
37
38
	/**
39
	 * @summary The template for the "disabled" text shown in the reaction title.
40
	 *
41
	 * @since 2.3.0
42
	 */
43
	titleTemplate: template( 'hook-disabled-text' ),
44
45
	/**
46
	 * @since 2.3.0
47
	 */
48
	initReaction: function ( reaction ) {
49
50
		this.listenTo( reaction, 'render:title', function () {
51
52
			if ( ! reaction.$title.find( '.wordpoints-hook-disabled-text' ).length ) {
53
				reaction.$title.prepend( this.titleTemplate() );
54
			}
55
56
			this.setDisabled( reaction );
57
		});
58
59
		this.listenToOnce( reaction, 'render:settings', function () {
60
61
			// We hook this up late so the settings will be below other extensions.
62
			this.listenTo( reaction, 'render:fields', function () {
63
64
				if ( ! reaction.$fields.find( '.disable' ).length ) {
65
					reaction.$fields.append( this.template() );
66
				}
67
68
				this.setDisabled( reaction );
69
			});
70
		} );
71
72
		this.listenTo( reaction.model, 'change:disabled', function () {
73
			this.setDisabled( reaction );
74
		} );
75
76
		this.listenTo( reaction.model, 'sync', function () {
77
			this.setDisabled( reaction );
78
		} );
79
	},
80
81
	/**
82
	 * @summary Set the disabled class if the reaction is disabled.
83
	 *
84
	 * @since 2.3.0
85
	 */
86
	setDisabled: function ( reaction ) {
87
88
		var isDisabled = !! reaction.model.get( this.get( 'slug' ) );
89
90
		reaction.$el.toggleClass( 'disabled', isDisabled );
91
92
		reaction.$fields
93
			.find( 'input[name=disable]' )
94
			.prop( 'checked', isDisabled );
95
	},
96
97
	/**
98
	 * @since 2.3.0
99
	 */
100
	validateReaction: function () {}
101
102
} );
103
104
module.exports = Disable;
105