forked from dimadinev/control-color-alpha
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWPTRTColorAlphaControl.js
192 lines (170 loc) · 5.2 KB
/
WPTRTColorAlphaControl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* global wp, jQuery, React, ReactDOM, Color */
import WPTRTColorAlphaForm from './WPTRTColorAlphaForm';
/**
* WPTRTColorAlphaControl.
*
* @class
* @augments wp.customize.Control
* @augments wp.customize.Class
*/
const WPTRTColorAlphaControl = wp.customize.Control.extend({
/**
* After control has been first rendered, start re-rendering when setting changes.
*
* React is able to be used here instead of the wp.customize.Element abstraction.
*
* @returns {void}
*/
ready: function ready() {
const control = this;
// Re-render control when setting changes.
control.setting.bind( () => {
control.renderContent();
} );
control.rgbaControlNotifications();
},
/**
* Embed the control in the document.
*
* Overrides the embed() method to embed the control
* when the section is expanded instead of on load.
*
* @since 1.0.0
* @return {void}
*/
embed: function() {
const control = this;
const sectionId = control.section();
if ( ! sectionId ) {
return;
}
wp.customize.section( sectionId, function( section ) {
section.expanded.bind( function( expanded ) {
if ( expanded ) {
control.actuallyEmbed();
}
} );
} );
},
/**
* Deferred embedding of control.
*
* This function is called in Section.onChangeExpanded() so the control
* will only get embedded when the Section is first expanded.
*
* @since 1.0.0
*/
actuallyEmbed: function() {
const control = this;
if ( 'resolved' === control.deferred.embedded.state() ) {
return;
}
control.renderContent();
control.deferred.embedded.resolve(); // Triggers control.ready().
},
/**
* Initialize.
*
* @param {string} id - Control ID.
* @param {object} params - Control params.
*/
initialize: function( id, params ) {
const control = this;
// Bind functions to this control context for passing as React props.
control.setNotificationContainer = control.setNotificationContainer.bind( control );
wp.customize.Control.prototype.initialize.call( control, id, params );
// The following should be eliminated with <https://core.trac.wordpress.org/ticket/31334>.
function onRemoved( removedControl ) {
if ( control === removedControl ) {
control.destroy();
control.container.remove();
wp.customize.control.unbind( 'removed', onRemoved );
}
}
wp.customize.control.bind( 'removed', onRemoved );
},
/**
* Set notification container and render.
*
* This is called when the React component is mounted.
*
* @param {Element} element - Notification container.
* @returns {void}
*/
setNotificationContainer: function setNotificationContainer( element ) {
const control = this;
control.notifications.container = jQuery( element );
control.notifications.render();
},
/**
* Render the control into the DOM.
*
* This is called from the Control#embed() method in the parent class.
*
* @returns {void}
*/
renderContent: function renderContent() {
const control = this;
const value = control.setting.get();
const form = <WPTRTColorAlphaForm
{ ...control.params }
value={ value }
setNotificationContainer={ control.setNotificationContainer }
customizerSetting={ control.setting }
control={ control }
choices={ control.params.choices }
default={ control.params.defaultValue }
/>;
ReactDOM.render(
form,
control.container[0]
);
if ( false !== control.params.choices.allowCollapse ) {
control.container.addClass( 'allowCollapse colorPickerIsCollapsed' );
}
},
/**
* Handle removal/de-registration of the control.
*
* This is essentially the inverse of the Control#embed() method.
*
* @link https://core.trac.wordpress.org/ticket/31334
* @returns {void}
*/
destroy: function destroy() {
const control = this;
// Garbage collection: undo mounting that was done in the embed/renderContent method.
ReactDOM.unmountComponentAtNode( control.container[0] );
// Call destroy method in parent if it exists (as of #31334).
if ( wp.customize.Control.prototype.destroy ) {
wp.customize.Control.prototype.destroy.call( control );
}
},
/**
* Handles notifications.
*
* @returns {void}
*/
rgbaControlNotifications: function() {
const control = this;
const code = 'long_title';
const patternTest = RegExp( /^(\#[\da-f]{3}|\#[\da-f]{6}|\#[\da-f]{8}|rgba\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)(,\s*(0\.\d+|1))\)|hsla\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)(,\s*(0\.\d+|1))\)|rgb\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)|hsl\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)\))$/ );
// Make sure we have the message before proceeding.
if ( ! window._wpCustomizeControlsL10n.cheatin ) {
return;
}
wp.customize( control.id, function( setting ) {
setting.bind( function( value ) {
if ( false === patternTest.test( value ) ) {
setting.notifications.add( code, new wp.customize.Notification( code, {
type: 'warning',
message: window._wpCustomizeControlsL10n.cheatin
} ) );
} else {
setting.notifications.remove( code );
}
} );
} );
}
});
export default WPTRTColorAlphaControl;