forked from xwp/wp-widget-customizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget-customizer.js
266 lines (235 loc) · 7.03 KB
/
widget-customizer.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*global wp, jQuery, WidgetCustomizer_exports, console, alert */
var WidgetCustomizer = (function ($) {
'use strict';
var customize = wp.customize;
var self = {
control: null,
ajax_action: null,
nonce_value: null,
nonce_post_key: null
};
$.extend(self, WidgetCustomizer_exports);
self.constuctor = customize.Control.extend({
/**
* Set up the control
*/
ready: function() {
var control = this;
control.setting.bind( function( to ) {
control.updateWidget( to );
});
control.container.find( '.widget-control-update' ).on( 'click', function (e) {
control.updateWidget();
});
control.setting.previewer.channel.bind( 'synced', function () {
control.container.removeClass( 'previewer-loading' );
});
control.setupControlToggle();
control.setupWidgetTitle();
control.editingEffects();
},
/**
* @param {object} instance_override When the model changes, the instance is sent this way
*/
updateWidget: function ( instance_override ) {
var control = this;
var data = control.container.find(':input').serialize();
control.container.addClass( 'widget-form-loading' );
control.container.addClass( 'previewer-loading' );
control.container.find( '.widget-content' ).prop( 'disabled', true );
var params = {};
params.action = self.ajax_action;
params[self.nonce_post_key] = self.nonce_value;
if ( instance_override ) {
params.json_instance_override = JSON.stringify( instance_override );
}
data += '&' + $.param( params );
var jqxhr = $.post( wp.ajax.settings.url, data, function (r) {
if ( r.success ) {
control.container.find( '.widget-content' ).html( r.data.form );
if ( ! instance_override ) {
control.setting( r.data.instance );
}
}
else {
var message = 'FAIL';
if ( r.data && r.data.message ) {
message = r.data.message;
}
alert( message );
}
});
jqxhr.fail( function (jqXHR, textStatus, errorThrown) {
alert( textStatus );
});
jqxhr.always( function () {
control.container.find( '.widget-content' ).prop( 'disabled', false );
control.container.removeClass( 'widget-form-loading' );
});
},
/**
* Show/hide the control when clicking on the form title
*/
setupControlToggle: function() {
var control = this;
control.container.find('.widget-top').on( 'click', function (e) {
control.toggleForm();
e.preventDefault();
} );
},
/**
* Expand the accordion section containing a control
*/
expandControlSection: function () {
var section = this.container.closest( '.accordion-section' );
if ( ! section.hasClass('open') ) {
section.find('.accordion-section-title:first').trigger('click');
}
},
/**
* Expand the widget form control
*/
expandForm: function () {
this.toggleForm( true );
},
/**
* Collapse the widget form control
*/
collapseForm: function () {
this.toggleForm( false );
},
/**
* Expand or collapse the widget control
* @param {boolean|undefined} [do_expand] If not supplied, will be inverse of current visibility
*/
toggleForm: function ( do_expand ) {
var control = this;
var widget = control.container.find('div.widget:first');
var inside = widget.find('.widget-inside:first');
if ( typeof do_expand === 'undefined' ) {
do_expand = ! inside.is(':visible');
}
if ( do_expand ) {
control.container.trigger('expand');
inside.slideDown('fast');
}
else {
control.container.trigger('collapse');
inside.slideUp('fast', function() {
widget.css({'width':'', 'margin':''});
});
}
},
/**
* Update the title of the form if a title field is entered
*/
setupWidgetTitle: function () {
var control = this;
control.setting.bind( function() {
control.updateInWidgetTitle();
});
control.updateInWidgetTitle();
},
/**
* Set the widget control title based on any title setting
*/
updateInWidgetTitle: function () {
var control = this;
var title = control.setting().title;
var in_widget_title = control.container.find('.in-widget-title');
if ( title ) {
in_widget_title.text( ': ' + title );
}
else {
in_widget_title.text( '' );
}
},
/**
* Inverse of WidgetCustomizer.getControlInstanceForWidget
* @return {jQuery}
*/
getPreviewWidgetElement: function () {
var control = this;
var iframe_contents = $('#customize-preview iframe').contents();
return iframe_contents.find('#' + control.params.widget_id);
},
/**
* Inside of the customizer preview, scroll the widget into view
*/
scrollPreviewWidgetIntoView: function () {
var control = this;
var widget_el = control.getPreviewWidgetElement();
if ( widget_el.length ) {
widget_el[0].scrollIntoView( false );
}
},
/**
* Highlight the widget control and section
*/
highlightSectionAndControl: function() {
var control = this;
var target_element;
if ( control.container.is(':hidden') ) {
target_element = control.container.closest('.control-section');
}
else {
target_element = control.container;
}
$('.widget-customizer-highlighted').removeClass('widget-customizer-highlighted');
target_element.addClass('widget-customizer-highlighted');
setTimeout( function () {
target_element.removeClass('widget-customizer-highlighted');
}, 500 );
},
/**
* Add the widget-customizer-highlighted-widget class to the widget for 500ms
*/
highlightPreviewWidget: function () {
var control = this;
var widget_el = control.getPreviewWidgetElement();
var root_el = widget_el.closest('html');
root_el.find('.widget-customizer-highlighted-widget').removeClass('widget-customizer-highlighted-widget');
widget_el.addClass('widget-customizer-highlighted-widget');
setTimeout( function () {
widget_el.removeClass('widget-customizer-highlighted-widget');
}, 500 );
},
/**
* Highlight widgets in the preview when
*/
editingEffects: function() {
var control = this;
// Highlight whenever hovering or clicking over the form
control.container.on('mouseenter click', function () {
control.highlightPreviewWidget();
});
// Highlight when the setting is updated
control.setting.bind( function() {
control.scrollPreviewWidgetIntoView();
control.highlightPreviewWidget();
});
// Highlight when the widget form is expanded
control.container.on('expand', function () {
control.scrollPreviewWidgetIntoView();
});
}
});
/**
* Given a widget_id for a widget appearing in the preview.
* @param {string} widget_id
* @return {null|object}
*/
self.getControlInstanceForWidget = function ( widget_id ) {
var widget_control = null;
wp.customize.control.each(function ( control ) {
if ( control.params.type === 'widget_form' && control.params.widget_id === widget_id ) {
widget_control = control;
// @todo How do we break?
}
});
return widget_control;
};
// Note that 'widget_form' must match the Widget_Form_WP_Customize_Control::$type
customize.controlConstructor.widget_form = self.constuctor;
return self;
}( jQuery ));