author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-admin/js/customize-widgets.js |
|
3 |
*/ |
|
4 |
||
5 | 5 |
/* global _wpCustomizeWidgetsSettings */ |
6 |
(function( wp, $ ){ |
|
7 |
||
8 |
if ( ! wp || ! wp.customize ) { return; } |
|
9 |
||
10 |
// Set up our namespace... |
|
11 |
var api = wp.customize, |
|
12 |
l10n; |
|
13 |
||
9 | 14 |
/** |
15 |
* @namespace wp.customize.Widgets |
|
16 |
*/ |
|
5 | 17 |
api.Widgets = api.Widgets || {}; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
api.Widgets.savedWidgetIds = {}; |
5 | 19 |
|
16 | 20 |
// Link settings. |
5 | 21 |
api.Widgets.data = _wpCustomizeWidgetsSettings || {}; |
22 |
l10n = api.Widgets.data.l10n; |
|
23 |
||
24 |
/** |
|
25 |
* wp.customize.Widgets.WidgetModel |
|
26 |
* |
|
27 |
* A single widget model. |
|
28 |
* |
|
9 | 29 |
* @class wp.customize.Widgets.WidgetModel |
5 | 30 |
* @augments Backbone.Model |
31 |
*/ |
|
9 | 32 |
api.Widgets.WidgetModel = Backbone.Model.extend(/** @lends wp.customize.Widgets.WidgetModel.prototype */{ |
5 | 33 |
id: null, |
34 |
temp_id: null, |
|
35 |
classname: null, |
|
36 |
control_tpl: null, |
|
37 |
description: null, |
|
38 |
is_disabled: null, |
|
39 |
is_multi: null, |
|
40 |
multi_number: null, |
|
41 |
name: null, |
|
42 |
id_base: null, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
transport: null, |
5 | 44 |
params: [], |
45 |
width: null, |
|
46 |
height: null, |
|
47 |
search_matched: true |
|
48 |
}); |
|
49 |
||
50 |
/** |
|
51 |
* wp.customize.Widgets.WidgetCollection |
|
52 |
* |
|
53 |
* Collection for widget models. |
|
54 |
* |
|
9 | 55 |
* @class wp.customize.Widgets.WidgetCollection |
56 |
* @augments Backbone.Collection |
|
5 | 57 |
*/ |
9 | 58 |
api.Widgets.WidgetCollection = Backbone.Collection.extend(/** @lends wp.customize.Widgets.WidgetCollection.prototype */{ |
5 | 59 |
model: api.Widgets.WidgetModel, |
60 |
||
61 |
// Controls searching on the current widget collection |
|
16 | 62 |
// and triggers an update event. |
5 | 63 |
doSearch: function( value ) { |
64 |
||
16 | 65 |
// Don't do anything if we've already done this search. |
66 |
// Useful because the search handler fires multiple times per keystroke. |
|
5 | 67 |
if ( this.terms === value ) { |
68 |
return; |
|
69 |
} |
|
70 |
||
16 | 71 |
// Updates terms with the value passed. |
5 | 72 |
this.terms = value; |
73 |
||
74 |
// If we have terms, run a search... |
|
75 |
if ( this.terms.length > 0 ) { |
|
76 |
this.search( this.terms ); |
|
77 |
} |
|
78 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
// If search is blank, set all the widgets as they matched the search to reset the views. |
5 | 80 |
if ( this.terms === '' ) { |
81 |
this.each( function ( widget ) { |
|
82 |
widget.set( 'search_matched', true ); |
|
83 |
} ); |
|
84 |
} |
|
85 |
}, |
|
86 |
||
16 | 87 |
// Performs a search within the collection. |
5 | 88 |
// @uses RegExp |
89 |
search: function( term ) { |
|
90 |
var match, haystack; |
|
91 |
||
16 | 92 |
// Escape the term string for RegExp meta characters. |
5 | 93 |
term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' ); |
94 |
||
95 |
// Consider spaces as word delimiters and match the whole string |
|
16 | 96 |
// so matching terms can be combined. |
5 | 97 |
term = term.replace( / /g, ')(?=.*' ); |
98 |
match = new RegExp( '^(?=.*' + term + ').+', 'i' ); |
|
99 |
||
100 |
this.each( function ( data ) { |
|
18 | 101 |
haystack = [ data.get( 'name' ), data.get( 'description' ) ].join( ' ' ); |
5 | 102 |
data.set( 'search_matched', match.test( haystack ) ); |
103 |
} ); |
|
104 |
} |
|
105 |
}); |
|
106 |
api.Widgets.availableWidgets = new api.Widgets.WidgetCollection( api.Widgets.data.availableWidgets ); |
|
107 |
||
108 |
/** |
|
109 |
* wp.customize.Widgets.SidebarModel |
|
110 |
* |
|
111 |
* A single sidebar model. |
|
112 |
* |
|
9 | 113 |
* @class wp.customize.Widgets.SidebarModel |
5 | 114 |
* @augments Backbone.Model |
115 |
*/ |
|
9 | 116 |
api.Widgets.SidebarModel = Backbone.Model.extend(/** @lends wp.customize.Widgets.SidebarModel.prototype */{ |
5 | 117 |
after_title: null, |
118 |
after_widget: null, |
|
119 |
before_title: null, |
|
120 |
before_widget: null, |
|
121 |
'class': null, |
|
122 |
description: null, |
|
123 |
id: null, |
|
124 |
name: null, |
|
125 |
is_rendered: false |
|
126 |
}); |
|
127 |
||
128 |
/** |
|
129 |
* wp.customize.Widgets.SidebarCollection |
|
130 |
* |
|
131 |
* Collection for sidebar models. |
|
132 |
* |
|
9 | 133 |
* @class wp.customize.Widgets.SidebarCollection |
5 | 134 |
* @augments Backbone.Collection |
135 |
*/ |
|
9 | 136 |
api.Widgets.SidebarCollection = Backbone.Collection.extend(/** @lends wp.customize.Widgets.SidebarCollection.prototype */{ |
5 | 137 |
model: api.Widgets.SidebarModel |
138 |
}); |
|
139 |
api.Widgets.registeredSidebars = new api.Widgets.SidebarCollection( api.Widgets.data.registeredSidebars ); |
|
140 |
||
9 | 141 |
api.Widgets.AvailableWidgetsPanelView = wp.Backbone.View.extend(/** @lends wp.customize.Widgets.AvailableWidgetsPanelView.prototype */{ |
5 | 142 |
|
143 |
el: '#available-widgets', |
|
144 |
||
145 |
events: { |
|
146 |
'input #widgets-search': 'search', |
|
147 |
'focus .widget-tpl' : 'focus', |
|
148 |
'click .widget-tpl' : '_submit', |
|
149 |
'keypress .widget-tpl' : '_submit', |
|
150 |
'keydown' : 'keyboardAccessible' |
|
151 |
}, |
|
152 |
||
16 | 153 |
// Cache current selected widget. |
5 | 154 |
selected: null, |
155 |
||
16 | 156 |
// Cache sidebar control which has opened panel. |
5 | 157 |
currentSidebarControl: null, |
158 |
$search: null, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
$clearResults: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
searchMatchesCount: null, |
5 | 161 |
|
9 | 162 |
/** |
163 |
* View class for the available widgets panel. |
|
164 |
* |
|
165 |
* @constructs wp.customize.Widgets.AvailableWidgetsPanelView |
|
166 |
* @augments wp.Backbone.View |
|
167 |
*/ |
|
5 | 168 |
initialize: function() { |
169 |
var self = this; |
|
170 |
||
171 |
this.$search = $( '#widgets-search' ); |
|
172 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
this.$clearResults = this.$el.find( '.clear-results' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
|
5 | 175 |
_.bindAll( this, 'close' ); |
176 |
||
177 |
this.listenTo( this.collection, 'change', this.updateList ); |
|
178 |
||
179 |
this.updateList(); |
|
180 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
// Set the initial search count to the number of available widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
this.searchMatchesCount = this.collection.length; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
|
16 | 184 |
/* |
185 |
* If the available widgets panel is open and the customize controls |
|
186 |
* are interacted with (i.e. available widgets panel is blurred) then |
|
187 |
* close the available widgets panel. Also close on back button click. |
|
188 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
$( '#customize-controls, #available-widgets .customize-section-title' ).on( 'click keydown', function( e ) { |
5 | 190 |
var isAddNewBtn = $( e.target ).is( '.add-new-widget, .add-new-widget *' ); |
191 |
if ( $( 'body' ).hasClass( 'adding-widget' ) && ! isAddNewBtn ) { |
|
192 |
self.close(); |
|
193 |
} |
|
194 |
} ); |
|
195 |
||
16 | 196 |
// Clear the search results and trigger an `input` event to fire a new search. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
this.$clearResults.on( 'click', function() { |
18 | 198 |
self.$search.val( '' ).trigger( 'focus' ).trigger( 'input' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
|
16 | 201 |
// Close the panel if the URL in the preview changes. |
5 | 202 |
api.previewer.bind( 'url', this.close ); |
203 |
}, |
|
204 |
||
9 | 205 |
/** |
206 |
* Performs a search and handles selected widget. |
|
207 |
*/ |
|
208 |
search: _.debounce( function( event ) { |
|
5 | 209 |
var firstVisible; |
210 |
||
211 |
this.collection.doSearch( event.target.value ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
// Update the search matches count. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
this.updateSearchMatchesCount(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
// Announce how many search results. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
this.announceSearchMatches(); |
5 | 216 |
|
16 | 217 |
// Remove a widget from being selected if it is no longer visible. |
5 | 218 |
if ( this.selected && ! this.selected.is( ':visible' ) ) { |
219 |
this.selected.removeClass( 'selected' ); |
|
220 |
this.selected = null; |
|
221 |
} |
|
222 |
||
16 | 223 |
// If a widget was selected but the filter value has been cleared out, clear selection. |
5 | 224 |
if ( this.selected && ! event.target.value ) { |
225 |
this.selected.removeClass( 'selected' ); |
|
226 |
this.selected = null; |
|
227 |
} |
|
228 |
||
16 | 229 |
// If a filter has been entered and a widget hasn't been selected, select the first one shown. |
5 | 230 |
if ( ! this.selected && event.target.value ) { |
231 |
firstVisible = this.$el.find( '> .widget-tpl:visible:first' ); |
|
232 |
if ( firstVisible.length ) { |
|
233 |
this.select( firstVisible ); |
|
234 |
} |
|
235 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
// Toggle the clear search results button. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
if ( '' !== event.target.value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
this.$clearResults.addClass( 'is-visible' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
} else if ( '' === event.target.value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
this.$clearResults.removeClass( 'is-visible' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
// Set a CSS class on the search container when there are no search results. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
if ( ! this.searchMatchesCount ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
this.$el.addClass( 'no-widgets-found' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
this.$el.removeClass( 'no-widgets-found' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
} |
9 | 250 |
}, 500 ), |
5 | 251 |
|
9 | 252 |
/** |
253 |
* Updates the count of the available widgets that have the `search_matched` attribute. |
|
254 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
updateSearchMatchesCount: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
this.searchMatchesCount = this.collection.where({ search_matched: true }).length; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
|
9 | 259 |
/** |
260 |
* Sends a message to the aria-live region to announce how many search results. |
|
261 |
*/ |
|
262 |
announceSearchMatches: function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
var message = l10n.widgetsFound.replace( '%d', this.searchMatchesCount ) ; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
if ( ! this.searchMatchesCount ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
message = l10n.noWidgetsFound; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
wp.a11y.speak( message ); |
9 | 270 |
}, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
|
9 | 272 |
/** |
273 |
* Changes visibility of available widgets. |
|
274 |
*/ |
|
5 | 275 |
updateList: function() { |
276 |
this.collection.each( function( widget ) { |
|
277 |
var widgetTpl = $( '#widget-tpl-' + widget.id ); |
|
278 |
widgetTpl.toggle( widget.get( 'search_matched' ) && ! widget.get( 'is_disabled' ) ); |
|
279 |
if ( widget.get( 'is_disabled' ) && widgetTpl.is( this.selected ) ) { |
|
280 |
this.selected = null; |
|
281 |
} |
|
282 |
} ); |
|
283 |
}, |
|
284 |
||
9 | 285 |
/** |
286 |
* Highlights a widget. |
|
287 |
*/ |
|
5 | 288 |
select: function( widgetTpl ) { |
289 |
this.selected = $( widgetTpl ); |
|
290 |
this.selected.siblings( '.widget-tpl' ).removeClass( 'selected' ); |
|
291 |
this.selected.addClass( 'selected' ); |
|
292 |
}, |
|
293 |
||
9 | 294 |
/** |
295 |
* Highlights a widget on focus. |
|
296 |
*/ |
|
5 | 297 |
focus: function( event ) { |
298 |
this.select( $( event.currentTarget ) ); |
|
299 |
}, |
|
300 |
||
9 | 301 |
/** |
302 |
* Handles submit for keypress and click on widget. |
|
303 |
*/ |
|
5 | 304 |
_submit: function( event ) { |
16 | 305 |
// Only proceed with keypress if it is Enter or Spacebar. |
5 | 306 |
if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { |
307 |
return; |
|
308 |
} |
|
309 |
||
310 |
this.submit( $( event.currentTarget ) ); |
|
311 |
}, |
|
312 |
||
9 | 313 |
/** |
314 |
* Adds a selected widget to the sidebar. |
|
315 |
*/ |
|
5 | 316 |
submit: function( widgetTpl ) { |
317 |
var widgetId, widget, widgetFormControl; |
|
318 |
||
319 |
if ( ! widgetTpl ) { |
|
320 |
widgetTpl = this.selected; |
|
321 |
} |
|
322 |
||
323 |
if ( ! widgetTpl || ! this.currentSidebarControl ) { |
|
324 |
return; |
|
325 |
} |
|
326 |
||
327 |
this.select( widgetTpl ); |
|
328 |
||
329 |
widgetId = $( this.selected ).data( 'widget-id' ); |
|
330 |
widget = this.collection.findWhere( { id: widgetId } ); |
|
331 |
if ( ! widget ) { |
|
332 |
return; |
|
333 |
} |
|
334 |
||
335 |
widgetFormControl = this.currentSidebarControl.addWidget( widget.get( 'id_base' ) ); |
|
336 |
if ( widgetFormControl ) { |
|
337 |
widgetFormControl.focus(); |
|
338 |
} |
|
339 |
||
340 |
this.close(); |
|
341 |
}, |
|
342 |
||
9 | 343 |
/** |
344 |
* Opens the panel. |
|
345 |
*/ |
|
5 | 346 |
open: function( sidebarControl ) { |
347 |
this.currentSidebarControl = sidebarControl; |
|
348 |
||
16 | 349 |
// Wide widget controls appear over the preview, and so they need to be collapsed when the panel opens. |
5 | 350 |
_( this.currentSidebarControl.getWidgetFormControls() ).each( function( control ) { |
351 |
if ( control.params.is_wide ) { |
|
352 |
control.collapseForm(); |
|
353 |
} |
|
354 |
} ); |
|
355 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
if ( api.section.has( 'publish_settings' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
api.section( 'publish_settings' ).collapse(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
|
5 | 360 |
$( 'body' ).addClass( 'adding-widget' ); |
361 |
||
362 |
this.$el.find( '.selected' ).removeClass( 'selected' ); |
|
363 |
||
16 | 364 |
// Reset search. |
5 | 365 |
this.collection.doSearch( '' ); |
366 |
||
367 |
if ( ! api.settings.browser.mobile ) { |
|
18 | 368 |
this.$search.trigger( 'focus' ); |
5 | 369 |
} |
370 |
}, |
|
371 |
||
9 | 372 |
/** |
373 |
* Closes the panel. |
|
374 |
*/ |
|
5 | 375 |
close: function( options ) { |
376 |
options = options || {}; |
|
377 |
||
378 |
if ( options.returnFocus && this.currentSidebarControl ) { |
|
379 |
this.currentSidebarControl.container.find( '.add-new-widget' ).focus(); |
|
380 |
} |
|
381 |
||
382 |
this.currentSidebarControl = null; |
|
383 |
this.selected = null; |
|
384 |
||
385 |
$( 'body' ).removeClass( 'adding-widget' ); |
|
386 |
||
16 | 387 |
this.$search.val( '' ).trigger( 'input' ); |
5 | 388 |
}, |
389 |
||
9 | 390 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
391 |
* Adds keyboard accessibility to the panel. |
9 | 392 |
*/ |
5 | 393 |
keyboardAccessible: function( event ) { |
394 |
var isEnter = ( event.which === 13 ), |
|
395 |
isEsc = ( event.which === 27 ), |
|
396 |
isDown = ( event.which === 40 ), |
|
397 |
isUp = ( event.which === 38 ), |
|
398 |
isTab = ( event.which === 9 ), |
|
399 |
isShift = ( event.shiftKey ), |
|
400 |
selected = null, |
|
401 |
firstVisible = this.$el.find( '> .widget-tpl:visible:first' ), |
|
402 |
lastVisible = this.$el.find( '> .widget-tpl:visible:last' ), |
|
403 |
isSearchFocused = $( event.target ).is( this.$search ), |
|
404 |
isLastWidgetFocused = $( event.target ).is( '.widget-tpl:visible:last' ); |
|
405 |
||
406 |
if ( isDown || isUp ) { |
|
407 |
if ( isDown ) { |
|
408 |
if ( isSearchFocused ) { |
|
409 |
selected = firstVisible; |
|
410 |
} else if ( this.selected && this.selected.nextAll( '.widget-tpl:visible' ).length !== 0 ) { |
|
411 |
selected = this.selected.nextAll( '.widget-tpl:visible:first' ); |
|
412 |
} |
|
413 |
} else if ( isUp ) { |
|
414 |
if ( isSearchFocused ) { |
|
415 |
selected = lastVisible; |
|
416 |
} else if ( this.selected && this.selected.prevAll( '.widget-tpl:visible' ).length !== 0 ) { |
|
417 |
selected = this.selected.prevAll( '.widget-tpl:visible:first' ); |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
this.select( selected ); |
|
422 |
||
423 |
if ( selected ) { |
|
18 | 424 |
selected.trigger( 'focus' ); |
5 | 425 |
} else { |
18 | 426 |
this.$search.trigger( 'focus' ); |
5 | 427 |
} |
428 |
||
429 |
return; |
|
430 |
} |
|
431 |
||
16 | 432 |
// If enter pressed but nothing entered, don't do anything. |
5 | 433 |
if ( isEnter && ! this.$search.val() ) { |
434 |
return; |
|
435 |
} |
|
436 |
||
437 |
if ( isEnter ) { |
|
438 |
this.submit(); |
|
439 |
} else if ( isEsc ) { |
|
440 |
this.close( { returnFocus: true } ); |
|
441 |
} |
|
442 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
if ( this.currentSidebarControl && isTab && ( isShift && isSearchFocused || ! isShift && isLastWidgetFocused ) ) { |
5 | 444 |
this.currentSidebarControl.container.find( '.add-new-widget' ).focus(); |
445 |
event.preventDefault(); |
|
446 |
} |
|
447 |
} |
|
448 |
}); |
|
449 |
||
450 |
/** |
|
451 |
* Handlers for the widget-synced event, organized by widget ID base. |
|
452 |
* Other widgets may provide their own update handlers by adding |
|
453 |
* listeners for the widget-synced event. |
|
9 | 454 |
* |
455 |
* @alias wp.customize.Widgets.formSyncHandlers |
|
5 | 456 |
*/ |
457 |
api.Widgets.formSyncHandlers = { |
|
458 |
||
459 |
/** |
|
460 |
* @param {jQuery.Event} e |
|
461 |
* @param {jQuery} widget |
|
16 | 462 |
* @param {string} newForm |
5 | 463 |
*/ |
464 |
rss: function( e, widget, newForm ) { |
|
465 |
var oldWidgetError = widget.find( '.widget-error:first' ), |
|
466 |
newWidgetError = $( '<div>' + newForm + '</div>' ).find( '.widget-error:first' ); |
|
467 |
||
468 |
if ( oldWidgetError.length && newWidgetError.length ) { |
|
469 |
oldWidgetError.replaceWith( newWidgetError ); |
|
470 |
} else if ( oldWidgetError.length ) { |
|
471 |
oldWidgetError.remove(); |
|
472 |
} else if ( newWidgetError.length ) { |
|
473 |
widget.find( '.widget-content:first' ).prepend( newWidgetError ); |
|
474 |
} |
|
475 |
} |
|
476 |
}; |
|
477 |
||
9 | 478 |
api.Widgets.WidgetControl = api.Control.extend(/** @lends wp.customize.Widgets.WidgetControl.prototype */{ |
5 | 479 |
defaultExpandedArguments: { |
480 |
duration: 'fast', |
|
481 |
completeCallback: $.noop |
|
482 |
}, |
|
483 |
||
484 |
/** |
|
9 | 485 |
* wp.customize.Widgets.WidgetControl |
486 |
* |
|
487 |
* Customizer control for widgets. |
|
488 |
* Note that 'widget_form' must match the WP_Widget_Form_Customize_Control::$type |
|
489 |
* |
|
5 | 490 |
* @since 4.1.0 |
9 | 491 |
* |
492 |
* @constructs wp.customize.Widgets.WidgetControl |
|
493 |
* @augments wp.customize.Control |
|
5 | 494 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
initialize: function( id, options ) { |
5 | 496 |
var control = this; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
control.widgetControlEmbedded = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
control.widgetContentEmbedded = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
500 |
control.expanded = new api.Value( false ); |
5 | 501 |
control.expandedArgumentsQueue = []; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
control.expanded.bind( function( expanded ) { |
5 | 503 |
var args = control.expandedArgumentsQueue.shift(); |
504 |
args = $.extend( {}, control.defaultExpandedArguments, args ); |
|
505 |
control.onChangeExpanded( expanded, args ); |
|
506 |
}); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
control.altNotice = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
api.Control.prototype.initialize.call( control, id, options ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
510 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
* Set up the control. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
ready: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
var control = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* Embed a placeholder once the section is expanded. The full widget |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
* form content will be embedded once the control itself is expanded, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
* and at this point the widget-added event will be triggered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
if ( ! control.section() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
control.embedWidgetControl(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
api.section( control.section(), function( section ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
var onExpanded = function( isExpanded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
if ( isExpanded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
control.embedWidgetControl(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
section.expanded.unbind( onExpanded ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
if ( section.expanded() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
onExpanded( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
section.expanded.bind( onExpanded ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
} |
5 | 542 |
}, |
543 |
||
544 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
* Embed the .widget element inside the li container. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
* @since 4.4.0 |
5 | 548 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
embedWidgetControl: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
var control = this, widgetControl; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
if ( control.widgetControlEmbedded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
control.widgetControlEmbedded = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
widgetControl = $( control.params.widget_control ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
control.container.append( widgetControl ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
control._setupModel(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
control._setupWideWidget(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
control._setupControlToggle(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
control._setupWidgetTitle(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
control._setupReorderUI(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
control._setupHighlightEffects(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
control._setupUpdateUI(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
control._setupRemoveUI(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* Embed the actual widget form inside of .widget-content and finally trigger the widget-added event. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
575 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
embedWidgetContent: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
var control = this, widgetContent; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
579 |
control.embedWidgetControl(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
if ( control.widgetContentEmbedded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
control.widgetContentEmbedded = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
// Update the notification container element now that the widget content has been embedded. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
control.notifications.container = control.getNotificationsContainerElement(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
control.notifications.render(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
widgetContent = $( control.params.widget_content ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
control.container.find( '.widget-content:first' ).append( widgetContent ); |
5 | 591 |
|
592 |
/* |
|
593 |
* Trigger widget-added event so that plugins can attach any event |
|
594 |
* listeners and dynamic UI elements. |
|
595 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
$( document ).trigger( 'widget-added', [ control.container.find( '.widget:first' ) ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
|
5 | 598 |
}, |
599 |
||
600 |
/** |
|
601 |
* Handle changes to the setting |
|
602 |
*/ |
|
603 |
_setupModel: function() { |
|
604 |
var self = this, rememberSavedWidgetId; |
|
605 |
||
16 | 606 |
// Remember saved widgets so we know which to trash (move to inactive widgets sidebar). |
5 | 607 |
rememberSavedWidgetId = function() { |
608 |
api.Widgets.savedWidgetIds[self.params.widget_id] = true; |
|
609 |
}; |
|
610 |
api.bind( 'ready', rememberSavedWidgetId ); |
|
611 |
api.bind( 'saved', rememberSavedWidgetId ); |
|
612 |
||
613 |
this._updateCount = 0; |
|
614 |
this.isWidgetUpdating = false; |
|
615 |
this.liveUpdateMode = true; |
|
616 |
||
16 | 617 |
// Update widget whenever model changes. |
5 | 618 |
this.setting.bind( function( to, from ) { |
619 |
if ( ! _( from ).isEqual( to ) && ! self.isWidgetUpdating ) { |
|
620 |
self.updateWidget( { instance: to } ); |
|
621 |
} |
|
622 |
} ); |
|
623 |
}, |
|
624 |
||
625 |
/** |
|
626 |
* Add special behaviors for wide widget controls |
|
627 |
*/ |
|
628 |
_setupWideWidget: function() { |
|
629 |
var self = this, $widgetInside, $widgetForm, $customizeSidebar, |
|
630 |
$themeControlsContainer, positionWidget; |
|
631 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
if ( ! this.params.is_wide || $( window ).width() <= 640 /* max-width breakpoint in customize-controls.css */ ) { |
5 | 633 |
return; |
634 |
} |
|
635 |
||
636 |
$widgetInside = this.container.find( '.widget-inside' ); |
|
637 |
$widgetForm = $widgetInside.find( '> .form' ); |
|
638 |
$customizeSidebar = $( '.wp-full-overlay-sidebar-content:first' ); |
|
639 |
this.container.addClass( 'wide-widget-control' ); |
|
640 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
this.container.find( '.form:first' ).css( { |
5 | 642 |
'max-width': this.params.width, |
643 |
'min-height': this.params.height |
|
644 |
} ); |
|
645 |
||
646 |
/** |
|
647 |
* Keep the widget-inside positioned so the top of fixed-positioned |
|
648 |
* element is at the same top position as the widget-top. When the |
|
649 |
* widget-top is scrolled out of view, keep the widget-top in view; |
|
650 |
* likewise, don't allow the widget to drop off the bottom of the window. |
|
651 |
* If a widget is too tall to fit in the window, don't let the height |
|
652 |
* exceed the window height so that the contents of the widget control |
|
653 |
* will become scrollable (overflow:auto). |
|
654 |
*/ |
|
655 |
positionWidget = function() { |
|
656 |
var offsetTop = self.container.offset().top, |
|
657 |
windowHeight = $( window ).height(), |
|
658 |
formHeight = $widgetForm.outerHeight(), |
|
659 |
top; |
|
660 |
$widgetInside.css( 'max-height', windowHeight ); |
|
661 |
top = Math.max( |
|
16 | 662 |
0, // Prevent top from going off screen. |
5 | 663 |
Math.min( |
16 | 664 |
Math.max( offsetTop, 0 ), // Distance widget in panel is from top of screen. |
665 |
windowHeight - formHeight // Flush up against bottom of screen. |
|
5 | 666 |
) |
667 |
); |
|
668 |
$widgetInside.css( 'top', top ); |
|
669 |
}; |
|
670 |
||
671 |
$themeControlsContainer = $( '#customize-theme-controls' ); |
|
672 |
this.container.on( 'expand', function() { |
|
673 |
positionWidget(); |
|
674 |
$customizeSidebar.on( 'scroll', positionWidget ); |
|
675 |
$( window ).on( 'resize', positionWidget ); |
|
676 |
$themeControlsContainer.on( 'expanded collapsed', positionWidget ); |
|
677 |
} ); |
|
678 |
this.container.on( 'collapsed', function() { |
|
679 |
$customizeSidebar.off( 'scroll', positionWidget ); |
|
680 |
$( window ).off( 'resize', positionWidget ); |
|
681 |
$themeControlsContainer.off( 'expanded collapsed', positionWidget ); |
|
682 |
} ); |
|
683 |
||
16 | 684 |
// Reposition whenever a sidebar's widgets are changed. |
5 | 685 |
api.each( function( setting ) { |
686 |
if ( 0 === setting.id.indexOf( 'sidebars_widgets[' ) ) { |
|
687 |
setting.bind( function() { |
|
688 |
if ( self.container.hasClass( 'expanded' ) ) { |
|
689 |
positionWidget(); |
|
690 |
} |
|
691 |
} ); |
|
692 |
} |
|
693 |
} ); |
|
694 |
}, |
|
695 |
||
696 |
/** |
|
697 |
* Show/hide the control when clicking on the form title, when clicking |
|
698 |
* the close button |
|
699 |
*/ |
|
700 |
_setupControlToggle: function() { |
|
701 |
var self = this, $closeBtn; |
|
702 |
||
703 |
this.container.find( '.widget-top' ).on( 'click', function( e ) { |
|
704 |
e.preventDefault(); |
|
705 |
var sidebarWidgetsControl = self.getSidebarWidgetsControl(); |
|
706 |
if ( sidebarWidgetsControl.isReordering ) { |
|
707 |
return; |
|
708 |
} |
|
709 |
self.expanded( ! self.expanded() ); |
|
710 |
} ); |
|
711 |
||
712 |
$closeBtn = this.container.find( '.widget-control-close' ); |
|
9 | 713 |
$closeBtn.on( 'click', function() { |
5 | 714 |
self.collapse(); |
16 | 715 |
self.container.find( '.widget-top .widget-action:first' ).focus(); // Keyboard accessibility. |
5 | 716 |
} ); |
717 |
}, |
|
718 |
||
719 |
/** |
|
720 |
* Update the title of the form if a title field is entered |
|
721 |
*/ |
|
722 |
_setupWidgetTitle: function() { |
|
723 |
var self = this, updateTitle; |
|
724 |
||
725 |
updateTitle = function() { |
|
726 |
var title = self.setting().title, |
|
727 |
inWidgetTitle = self.container.find( '.in-widget-title' ); |
|
728 |
||
729 |
if ( title ) { |
|
730 |
inWidgetTitle.text( ': ' + title ); |
|
731 |
} else { |
|
732 |
inWidgetTitle.text( '' ); |
|
733 |
} |
|
734 |
}; |
|
735 |
this.setting.bind( updateTitle ); |
|
736 |
updateTitle(); |
|
737 |
}, |
|
738 |
||
739 |
/** |
|
740 |
* Set up the widget-reorder-nav |
|
741 |
*/ |
|
742 |
_setupReorderUI: function() { |
|
743 |
var self = this, selectSidebarItem, $moveWidgetArea, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
744 |
$reorderNav, updateAvailableSidebars, template; |
5 | 745 |
|
746 |
/** |
|
747 |
* select the provided sidebar list item in the move widget area |
|
748 |
* |
|
749 |
* @param {jQuery} li |
|
750 |
*/ |
|
751 |
selectSidebarItem = function( li ) { |
|
752 |
li.siblings( '.selected' ).removeClass( 'selected' ); |
|
753 |
li.addClass( 'selected' ); |
|
754 |
var isSelfSidebar = ( li.data( 'id' ) === self.params.sidebar_id ); |
|
755 |
self.container.find( '.move-widget-btn' ).prop( 'disabled', isSelfSidebar ); |
|
756 |
}; |
|
757 |
||
758 |
/** |
|
759 |
* Add the widget reordering elements to the widget control |
|
760 |
*/ |
|
761 |
this.container.find( '.widget-title-action' ).after( $( api.Widgets.data.tpl.widgetReorderNav ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
762 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
template = _.template( api.Widgets.data.tpl.moveWidgetArea ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
$moveWidgetArea = $( template( { |
5 | 766 |
sidebars: _( api.Widgets.registeredSidebars.toArray() ).pluck( 'attributes' ) |
767 |
} ) |
|
768 |
); |
|
769 |
this.container.find( '.widget-top' ).after( $moveWidgetArea ); |
|
770 |
||
771 |
/** |
|
772 |
* Update available sidebars when their rendered state changes |
|
773 |
*/ |
|
774 |
updateAvailableSidebars = function() { |
|
775 |
var $sidebarItems = $moveWidgetArea.find( 'li' ), selfSidebarItem, |
|
776 |
renderedSidebarCount = 0; |
|
777 |
||
778 |
selfSidebarItem = $sidebarItems.filter( function(){ |
|
779 |
return $( this ).data( 'id' ) === self.params.sidebar_id; |
|
780 |
} ); |
|
781 |
||
782 |
$sidebarItems.each( function() { |
|
783 |
var li = $( this ), |
|
784 |
sidebarId, sidebar, sidebarIsRendered; |
|
785 |
||
786 |
sidebarId = li.data( 'id' ); |
|
787 |
sidebar = api.Widgets.registeredSidebars.get( sidebarId ); |
|
788 |
sidebarIsRendered = sidebar.get( 'is_rendered' ); |
|
789 |
||
790 |
li.toggle( sidebarIsRendered ); |
|
791 |
||
792 |
if ( sidebarIsRendered ) { |
|
793 |
renderedSidebarCount += 1; |
|
794 |
} |
|
795 |
||
796 |
if ( li.hasClass( 'selected' ) && ! sidebarIsRendered ) { |
|
797 |
selectSidebarItem( selfSidebarItem ); |
|
798 |
} |
|
799 |
} ); |
|
800 |
||
801 |
if ( renderedSidebarCount > 1 ) { |
|
802 |
self.container.find( '.move-widget' ).show(); |
|
803 |
} else { |
|
804 |
self.container.find( '.move-widget' ).hide(); |
|
805 |
} |
|
806 |
}; |
|
807 |
||
808 |
updateAvailableSidebars(); |
|
809 |
api.Widgets.registeredSidebars.on( 'change:is_rendered', updateAvailableSidebars ); |
|
810 |
||
811 |
/** |
|
812 |
* Handle clicks for up/down/move on the reorder nav |
|
813 |
*/ |
|
814 |
$reorderNav = this.container.find( '.widget-reorder-nav' ); |
|
815 |
$reorderNav.find( '.move-widget, .move-widget-down, .move-widget-up' ).each( function() { |
|
816 |
$( this ).prepend( self.container.find( '.widget-title' ).text() + ': ' ); |
|
817 |
} ).on( 'click keypress', function( event ) { |
|
818 |
if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { |
|
819 |
return; |
|
820 |
} |
|
18 | 821 |
$( this ).trigger( 'focus' ); |
5 | 822 |
|
823 |
if ( $( this ).is( '.move-widget' ) ) { |
|
824 |
self.toggleWidgetMoveArea(); |
|
825 |
} else { |
|
826 |
var isMoveDown = $( this ).is( '.move-widget-down' ), |
|
827 |
isMoveUp = $( this ).is( '.move-widget-up' ), |
|
828 |
i = self.getWidgetSidebarPosition(); |
|
829 |
||
830 |
if ( ( isMoveUp && i === 0 ) || ( isMoveDown && i === self.getSidebarWidgetsControl().setting().length - 1 ) ) { |
|
831 |
return; |
|
832 |
} |
|
833 |
||
834 |
if ( isMoveUp ) { |
|
835 |
self.moveUp(); |
|
836 |
wp.a11y.speak( l10n.widgetMovedUp ); |
|
837 |
} else { |
|
838 |
self.moveDown(); |
|
839 |
wp.a11y.speak( l10n.widgetMovedDown ); |
|
840 |
} |
|
841 |
||
18 | 842 |
$( this ).trigger( 'focus' ); // Re-focus after the container was moved. |
5 | 843 |
} |
844 |
} ); |
|
845 |
||
846 |
/** |
|
847 |
* Handle selecting a sidebar to move to |
|
848 |
*/ |
|
849 |
this.container.find( '.widget-area-select' ).on( 'click keypress', 'li', function( event ) { |
|
850 |
if ( event.type === 'keypress' && ( event.which !== 13 && event.which !== 32 ) ) { |
|
851 |
return; |
|
852 |
} |
|
853 |
event.preventDefault(); |
|
854 |
selectSidebarItem( $( this ) ); |
|
855 |
} ); |
|
856 |
||
857 |
/** |
|
858 |
* Move widget to another sidebar |
|
859 |
*/ |
|
860 |
this.container.find( '.move-widget-btn' ).click( function() { |
|
861 |
self.getSidebarWidgetsControl().toggleReordering( false ); |
|
862 |
||
863 |
var oldSidebarId = self.params.sidebar_id, |
|
864 |
newSidebarId = self.container.find( '.widget-area-select li.selected' ).data( 'id' ), |
|
865 |
oldSidebarWidgetsSetting, newSidebarWidgetsSetting, |
|
866 |
oldSidebarWidgetIds, newSidebarWidgetIds, i; |
|
867 |
||
868 |
oldSidebarWidgetsSetting = api( 'sidebars_widgets[' + oldSidebarId + ']' ); |
|
869 |
newSidebarWidgetsSetting = api( 'sidebars_widgets[' + newSidebarId + ']' ); |
|
870 |
oldSidebarWidgetIds = Array.prototype.slice.call( oldSidebarWidgetsSetting() ); |
|
871 |
newSidebarWidgetIds = Array.prototype.slice.call( newSidebarWidgetsSetting() ); |
|
872 |
||
873 |
i = self.getWidgetSidebarPosition(); |
|
874 |
oldSidebarWidgetIds.splice( i, 1 ); |
|
875 |
newSidebarWidgetIds.push( self.params.widget_id ); |
|
876 |
||
877 |
oldSidebarWidgetsSetting( oldSidebarWidgetIds ); |
|
878 |
newSidebarWidgetsSetting( newSidebarWidgetIds ); |
|
879 |
||
880 |
self.focus(); |
|
881 |
} ); |
|
882 |
}, |
|
883 |
||
884 |
/** |
|
885 |
* Highlight widgets in preview when interacted with in the Customizer |
|
886 |
*/ |
|
887 |
_setupHighlightEffects: function() { |
|
888 |
var self = this; |
|
889 |
||
16 | 890 |
// Highlight whenever hovering or clicking over the form. |
5 | 891 |
this.container.on( 'mouseenter click', function() { |
892 |
self.setting.previewer.send( 'highlight-widget', self.params.widget_id ); |
|
893 |
} ); |
|
894 |
||
16 | 895 |
// Highlight when the setting is updated. |
5 | 896 |
this.setting.bind( function() { |
897 |
self.setting.previewer.send( 'highlight-widget', self.params.widget_id ); |
|
898 |
} ); |
|
899 |
}, |
|
900 |
||
901 |
/** |
|
902 |
* Set up event handlers for widget updating |
|
903 |
*/ |
|
904 |
_setupUpdateUI: function() { |
|
905 |
var self = this, $widgetRoot, $widgetContent, |
|
906 |
$saveBtn, updateWidgetDebounced, formSyncHandler; |
|
907 |
||
908 |
$widgetRoot = this.container.find( '.widget:first' ); |
|
909 |
$widgetContent = $widgetRoot.find( '.widget-content:first' ); |
|
910 |
||
16 | 911 |
// Configure update button. |
5 | 912 |
$saveBtn = this.container.find( '.widget-control-save' ); |
913 |
$saveBtn.val( l10n.saveBtnLabel ); |
|
914 |
$saveBtn.attr( 'title', l10n.saveBtnTooltip ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
$saveBtn.removeClass( 'button-primary' ); |
5 | 916 |
$saveBtn.on( 'click', function( e ) { |
917 |
e.preventDefault(); |
|
918 |
self.updateWidget( { disable_form: true } ); // @todo disable_form is unused? |
|
919 |
} ); |
|
920 |
||
921 |
updateWidgetDebounced = _.debounce( function() { |
|
922 |
self.updateWidget(); |
|
923 |
}, 250 ); |
|
924 |
||
16 | 925 |
// Trigger widget form update when hitting Enter within an input. |
5 | 926 |
$widgetContent.on( 'keydown', 'input', function( e ) { |
16 | 927 |
if ( 13 === e.which ) { // Enter. |
5 | 928 |
e.preventDefault(); |
929 |
self.updateWidget( { ignoreActiveElement: true } ); |
|
930 |
} |
|
931 |
} ); |
|
932 |
||
16 | 933 |
// Handle widgets that support live previews. |
5 | 934 |
$widgetContent.on( 'change input propertychange', ':input', function( e ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
if ( ! self.liveUpdateMode ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
if ( e.type === 'change' || ( this.checkValidity && this.checkValidity() ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
updateWidgetDebounced(); |
5 | 940 |
} |
941 |
} ); |
|
942 |
||
16 | 943 |
// Remove loading indicators when the setting is saved and the preview updates. |
5 | 944 |
this.setting.previewer.channel.bind( 'synced', function() { |
945 |
self.container.removeClass( 'previewer-loading' ); |
|
946 |
} ); |
|
947 |
||
948 |
api.previewer.bind( 'widget-updated', function( updatedWidgetId ) { |
|
949 |
if ( updatedWidgetId === self.params.widget_id ) { |
|
950 |
self.container.removeClass( 'previewer-loading' ); |
|
951 |
} |
|
952 |
} ); |
|
953 |
||
954 |
formSyncHandler = api.Widgets.formSyncHandlers[ this.params.widget_id_base ]; |
|
955 |
if ( formSyncHandler ) { |
|
956 |
$( document ).on( 'widget-synced', function( e, widget ) { |
|
957 |
if ( $widgetRoot.is( widget ) ) { |
|
958 |
formSyncHandler.apply( document, arguments ); |
|
959 |
} |
|
960 |
} ); |
|
961 |
} |
|
962 |
}, |
|
963 |
||
964 |
/** |
|
965 |
* Update widget control to indicate whether it is currently rendered. |
|
966 |
* |
|
967 |
* Overrides api.Control.toggle() |
|
968 |
* |
|
969 |
* @since 4.1.0 |
|
970 |
* |
|
16 | 971 |
* @param {boolean} active |
5 | 972 |
* @param {Object} args |
9 | 973 |
* @param {function} args.completeCallback |
5 | 974 |
*/ |
975 |
onChangeActive: function ( active, args ) { |
|
16 | 976 |
// Note: there is a second 'args' parameter being passed, merged on top of this.defaultActiveArguments. |
5 | 977 |
this.container.toggleClass( 'widget-rendered', active ); |
978 |
if ( args.completeCallback ) { |
|
979 |
args.completeCallback(); |
|
980 |
} |
|
981 |
}, |
|
982 |
||
983 |
/** |
|
984 |
* Set up event handlers for widget removal |
|
985 |
*/ |
|
986 |
_setupRemoveUI: function() { |
|
987 |
var self = this, $removeBtn, replaceDeleteWithRemove; |
|
988 |
||
16 | 989 |
// Configure remove button. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
$removeBtn = this.container.find( '.widget-control-remove' ); |
9 | 991 |
$removeBtn.on( 'click', function() { |
16 | 992 |
// Find an adjacent element to add focus to when this widget goes away. |
5 | 993 |
var $adjacentFocusTarget; |
994 |
if ( self.container.next().is( '.customize-control-widget_form' ) ) { |
|
995 |
$adjacentFocusTarget = self.container.next().find( '.widget-action:first' ); |
|
996 |
} else if ( self.container.prev().is( '.customize-control-widget_form' ) ) { |
|
997 |
$adjacentFocusTarget = self.container.prev().find( '.widget-action:first' ); |
|
998 |
} else { |
|
999 |
$adjacentFocusTarget = self.container.next( '.customize-control-sidebar_widgets' ).find( '.add-new-widget:first' ); |
|
1000 |
} |
|
1001 |
||
1002 |
self.container.slideUp( function() { |
|
1003 |
var sidebarsWidgetsControl = api.Widgets.getSidebarWidgetControlContainingWidget( self.params.widget_id ), |
|
1004 |
sidebarWidgetIds, i; |
|
1005 |
||
1006 |
if ( ! sidebarsWidgetsControl ) { |
|
1007 |
return; |
|
1008 |
} |
|
1009 |
||
1010 |
sidebarWidgetIds = sidebarsWidgetsControl.setting().slice(); |
|
1011 |
i = _.indexOf( sidebarWidgetIds, self.params.widget_id ); |
|
1012 |
if ( -1 === i ) { |
|
1013 |
return; |
|
1014 |
} |
|
1015 |
||
1016 |
sidebarWidgetIds.splice( i, 1 ); |
|
1017 |
sidebarsWidgetsControl.setting( sidebarWidgetIds ); |
|
1018 |
||
16 | 1019 |
$adjacentFocusTarget.focus(); // Keyboard accessibility. |
5 | 1020 |
} ); |
1021 |
} ); |
|
1022 |
||
1023 |
replaceDeleteWithRemove = function() { |
|
16 | 1024 |
$removeBtn.text( l10n.removeBtnLabel ); // wp_widget_control() outputs the button as "Delete". |
5 | 1025 |
$removeBtn.attr( 'title', l10n.removeBtnTooltip ); |
1026 |
}; |
|
1027 |
||
1028 |
if ( this.params.is_new ) { |
|
1029 |
api.bind( 'saved', replaceDeleteWithRemove ); |
|
1030 |
} else { |
|
1031 |
replaceDeleteWithRemove(); |
|
1032 |
} |
|
1033 |
}, |
|
1034 |
||
1035 |
/** |
|
1036 |
* Find all inputs in a widget container that should be considered when |
|
1037 |
* comparing the loaded form with the sanitized form, whose fields will |
|
1038 |
* be aligned to copy the sanitized over. The elements returned by this |
|
1039 |
* are passed into this._getInputsSignature(), and they are iterated |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
* over when copying sanitized values over to the form loaded. |
5 | 1041 |
* |
1042 |
* @param {jQuery} container element in which to look for inputs |
|
16 | 1043 |
* @return {jQuery} inputs |
5 | 1044 |
* @private |
1045 |
*/ |
|
1046 |
_getInputs: function( container ) { |
|
1047 |
return $( container ).find( ':input[name]' ); |
|
1048 |
}, |
|
1049 |
||
1050 |
/** |
|
1051 |
* Iterate over supplied inputs and create a signature string for all of them together. |
|
1052 |
* This string can be used to compare whether or not the form has all of the same fields. |
|
1053 |
* |
|
1054 |
* @param {jQuery} inputs |
|
16 | 1055 |
* @return {string} |
5 | 1056 |
* @private |
1057 |
*/ |
|
1058 |
_getInputsSignature: function( inputs ) { |
|
1059 |
var inputsSignatures = _( inputs ).map( function( input ) { |
|
1060 |
var $input = $( input ), signatureParts; |
|
1061 |
||
1062 |
if ( $input.is( ':checkbox, :radio' ) ) { |
|
1063 |
signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ), $input.prop( 'value' ) ]; |
|
1064 |
} else { |
|
1065 |
signatureParts = [ $input.attr( 'id' ), $input.attr( 'name' ) ]; |
|
1066 |
} |
|
1067 |
||
1068 |
return signatureParts.join( ',' ); |
|
1069 |
} ); |
|
1070 |
||
1071 |
return inputsSignatures.join( ';' ); |
|
1072 |
}, |
|
1073 |
||
1074 |
/** |
|
1075 |
* Get the state for an input depending on its type. |
|
1076 |
* |
|
1077 |
* @param {jQuery|Element} input |
|
16 | 1078 |
* @return {string|boolean|Array|*} |
5 | 1079 |
* @private |
1080 |
*/ |
|
1081 |
_getInputState: function( input ) { |
|
1082 |
input = $( input ); |
|
1083 |
if ( input.is( ':radio, :checkbox' ) ) { |
|
1084 |
return input.prop( 'checked' ); |
|
1085 |
} else if ( input.is( 'select[multiple]' ) ) { |
|
1086 |
return input.find( 'option:selected' ).map( function () { |
|
1087 |
return $( this ).val(); |
|
1088 |
} ).get(); |
|
1089 |
} else { |
|
1090 |
return input.val(); |
|
1091 |
} |
|
1092 |
}, |
|
1093 |
||
1094 |
/** |
|
1095 |
* Update an input's state based on its type. |
|
1096 |
* |
|
1097 |
* @param {jQuery|Element} input |
|
16 | 1098 |
* @param {string|boolean|Array|*} state |
5 | 1099 |
* @private |
1100 |
*/ |
|
1101 |
_setInputState: function ( input, state ) { |
|
1102 |
input = $( input ); |
|
1103 |
if ( input.is( ':radio, :checkbox' ) ) { |
|
1104 |
input.prop( 'checked', state ); |
|
1105 |
} else if ( input.is( 'select[multiple]' ) ) { |
|
18 | 1106 |
if ( ! Array.isArray( state ) ) { |
5 | 1107 |
state = []; |
1108 |
} else { |
|
16 | 1109 |
// Make sure all state items are strings since the DOM value is a string. |
5 | 1110 |
state = _.map( state, function ( value ) { |
1111 |
return String( value ); |
|
1112 |
} ); |
|
1113 |
} |
|
1114 |
input.find( 'option' ).each( function () { |
|
1115 |
$( this ).prop( 'selected', -1 !== _.indexOf( state, String( this.value ) ) ); |
|
1116 |
} ); |
|
1117 |
} else { |
|
1118 |
input.val( state ); |
|
1119 |
} |
|
1120 |
}, |
|
1121 |
||
1122 |
/*********************************************************************** |
|
1123 |
* Begin public API methods |
|
1124 |
**********************************************************************/ |
|
1125 |
||
1126 |
/** |
|
1127 |
* @return {wp.customize.controlConstructor.sidebar_widgets[]} |
|
1128 |
*/ |
|
1129 |
getSidebarWidgetsControl: function() { |
|
1130 |
var settingId, sidebarWidgetsControl; |
|
1131 |
||
1132 |
settingId = 'sidebars_widgets[' + this.params.sidebar_id + ']'; |
|
1133 |
sidebarWidgetsControl = api.control( settingId ); |
|
1134 |
||
1135 |
if ( ! sidebarWidgetsControl ) { |
|
1136 |
return; |
|
1137 |
} |
|
1138 |
||
1139 |
return sidebarWidgetsControl; |
|
1140 |
}, |
|
1141 |
||
1142 |
/** |
|
1143 |
* Submit the widget form via Ajax and get back the updated instance, |
|
1144 |
* along with the new widget control form to render. |
|
1145 |
* |
|
16 | 1146 |
* @param {Object} [args] |
5 | 1147 |
* @param {Object|null} [args.instance=null] When the model changes, the instance is sent here; otherwise, the inputs from the form are used |
1148 |
* @param {Function|null} [args.complete=null] Function which is called when the request finishes. Context is bound to the control. First argument is any error. Following arguments are for success. |
|
16 | 1149 |
* @param {boolean} [args.ignoreActiveElement=false] Whether or not updating a field will be deferred if focus is still on the element. |
5 | 1150 |
*/ |
1151 |
updateWidget: function( args ) { |
|
1152 |
var self = this, instanceOverride, completeCallback, $widgetRoot, $widgetContent, |
|
1153 |
updateNumber, params, data, $inputs, processing, jqxhr, isChanged; |
|
1154 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
// The updateWidget logic requires that the form fields to be fully present. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
self.embedWidgetContent(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
|
5 | 1158 |
args = $.extend( { |
1159 |
instance: null, |
|
1160 |
complete: null, |
|
1161 |
ignoreActiveElement: false |
|
1162 |
}, args ); |
|
1163 |
||
1164 |
instanceOverride = args.instance; |
|
1165 |
completeCallback = args.complete; |
|
1166 |
||
1167 |
this._updateCount += 1; |
|
1168 |
updateNumber = this._updateCount; |
|
1169 |
||
1170 |
$widgetRoot = this.container.find( '.widget:first' ); |
|
1171 |
$widgetContent = $widgetRoot.find( '.widget-content:first' ); |
|
1172 |
||
16 | 1173 |
// Remove a previous error message. |
5 | 1174 |
$widgetContent.find( '.widget-error' ).remove(); |
1175 |
||
1176 |
this.container.addClass( 'widget-form-loading' ); |
|
1177 |
this.container.addClass( 'previewer-loading' ); |
|
1178 |
processing = api.state( 'processing' ); |
|
1179 |
processing( processing() + 1 ); |
|
1180 |
||
1181 |
if ( ! this.liveUpdateMode ) { |
|
1182 |
this.container.addClass( 'widget-form-disabled' ); |
|
1183 |
} |
|
1184 |
||
1185 |
params = {}; |
|
1186 |
params.action = 'update-widget'; |
|
1187 |
params.wp_customize = 'on'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
params.nonce = api.settings.nonce['update-widget']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
params.customize_theme = api.settings.theme.stylesheet; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
params.customized = wp.customize.previewer.query().customized; |
5 | 1191 |
|
1192 |
data = $.param( params ); |
|
1193 |
$inputs = this._getInputs( $widgetContent ); |
|
1194 |
||
16 | 1195 |
/* |
1196 |
* Store the value we're submitting in data so that when the response comes back, |
|
1197 |
* we know if it got sanitized; if there is no difference in the sanitized value, |
|
1198 |
* then we do not need to touch the UI and mess up the user's ongoing editing. |
|
1199 |
*/ |
|
5 | 1200 |
$inputs.each( function() { |
1201 |
$( this ).data( 'state' + updateNumber, self._getInputState( this ) ); |
|
1202 |
} ); |
|
1203 |
||
1204 |
if ( instanceOverride ) { |
|
1205 |
data += '&' + $.param( { 'sanitized_widget_setting': JSON.stringify( instanceOverride ) } ); |
|
1206 |
} else { |
|
1207 |
data += '&' + $inputs.serialize(); |
|
1208 |
} |
|
1209 |
data += '&' + $widgetContent.find( '~ :input' ).serialize(); |
|
1210 |
||
1211 |
if ( this._previousUpdateRequest ) { |
|
1212 |
this._previousUpdateRequest.abort(); |
|
1213 |
} |
|
1214 |
jqxhr = $.post( wp.ajax.settings.url, data ); |
|
1215 |
this._previousUpdateRequest = jqxhr; |
|
1216 |
||
1217 |
jqxhr.done( function( r ) { |
|
1218 |
var message, sanitizedForm, $sanitizedInputs, hasSameInputsInResponse, |
|
1219 |
isLiveUpdateAborted = false; |
|
1220 |
||
1221 |
// Check if the user is logged out. |
|
1222 |
if ( '0' === r ) { |
|
1223 |
api.previewer.preview.iframe.hide(); |
|
1224 |
api.previewer.login().done( function() { |
|
1225 |
self.updateWidget( args ); |
|
1226 |
api.previewer.preview.iframe.show(); |
|
1227 |
} ); |
|
1228 |
return; |
|
1229 |
} |
|
1230 |
||
1231 |
// Check for cheaters. |
|
1232 |
if ( '-1' === r ) { |
|
1233 |
api.previewer.cheatin(); |
|
1234 |
return; |
|
1235 |
} |
|
1236 |
||
1237 |
if ( r.success ) { |
|
1238 |
sanitizedForm = $( '<div>' + r.data.form + '</div>' ); |
|
1239 |
$sanitizedInputs = self._getInputs( sanitizedForm ); |
|
1240 |
hasSameInputsInResponse = self._getInputsSignature( $inputs ) === self._getInputsSignature( $sanitizedInputs ); |
|
1241 |
||
16 | 1242 |
// Restore live update mode if sanitized fields are now aligned with the existing fields. |
5 | 1243 |
if ( hasSameInputsInResponse && ! self.liveUpdateMode ) { |
1244 |
self.liveUpdateMode = true; |
|
1245 |
self.container.removeClass( 'widget-form-disabled' ); |
|
1246 |
self.container.find( 'input[name="savewidget"]' ).hide(); |
|
1247 |
} |
|
1248 |
||
16 | 1249 |
// Sync sanitized field states to existing fields if they are aligned. |
5 | 1250 |
if ( hasSameInputsInResponse && self.liveUpdateMode ) { |
1251 |
$inputs.each( function( i ) { |
|
1252 |
var $input = $( this ), |
|
1253 |
$sanitizedInput = $( $sanitizedInputs[i] ), |
|
1254 |
submittedState, sanitizedState, canUpdateState; |
|
1255 |
||
1256 |
submittedState = $input.data( 'state' + updateNumber ); |
|
1257 |
sanitizedState = self._getInputState( $sanitizedInput ); |
|
1258 |
$input.data( 'sanitized', sanitizedState ); |
|
1259 |
||
1260 |
canUpdateState = ( ! _.isEqual( submittedState, sanitizedState ) && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) ); |
|
1261 |
if ( canUpdateState ) { |
|
1262 |
self._setInputState( $input, sanitizedState ); |
|
1263 |
} |
|
1264 |
} ); |
|
1265 |
||
1266 |
$( document ).trigger( 'widget-synced', [ $widgetRoot, r.data.form ] ); |
|
1267 |
||
16 | 1268 |
// Otherwise, if sanitized fields are not aligned with existing fields, disable live update mode if enabled. |
5 | 1269 |
} else if ( self.liveUpdateMode ) { |
1270 |
self.liveUpdateMode = false; |
|
1271 |
self.container.find( 'input[name="savewidget"]' ).show(); |
|
1272 |
isLiveUpdateAborted = true; |
|
1273 |
||
16 | 1274 |
// Otherwise, replace existing form with the sanitized form. |
5 | 1275 |
} else { |
1276 |
$widgetContent.html( r.data.form ); |
|
1277 |
||
1278 |
self.container.removeClass( 'widget-form-disabled' ); |
|
1279 |
||
1280 |
$( document ).trigger( 'widget-updated', [ $widgetRoot ] ); |
|
1281 |
} |
|
1282 |
||
1283 |
/** |
|
1284 |
* If the old instance is identical to the new one, there is nothing new |
|
1285 |
* needing to be rendered, and so we can preempt the event for the |
|
1286 |
* preview finishing loading. |
|
1287 |
*/ |
|
1288 |
isChanged = ! isLiveUpdateAborted && ! _( self.setting() ).isEqual( r.data.instance ); |
|
1289 |
if ( isChanged ) { |
|
16 | 1290 |
self.isWidgetUpdating = true; // Suppress triggering another updateWidget. |
5 | 1291 |
self.setting( r.data.instance ); |
1292 |
self.isWidgetUpdating = false; |
|
1293 |
} else { |
|
16 | 1294 |
// No change was made, so stop the spinner now instead of when the preview would updates. |
5 | 1295 |
self.container.removeClass( 'previewer-loading' ); |
1296 |
} |
|
1297 |
||
1298 |
if ( completeCallback ) { |
|
1299 |
completeCallback.call( self, null, { noChange: ! isChanged, ajaxFinished: true } ); |
|
1300 |
} |
|
1301 |
} else { |
|
16 | 1302 |
// General error message. |
5 | 1303 |
message = l10n.error; |
1304 |
||
1305 |
if ( r.data && r.data.message ) { |
|
1306 |
message = r.data.message; |
|
1307 |
} |
|
1308 |
||
1309 |
if ( completeCallback ) { |
|
1310 |
completeCallback.call( self, message ); |
|
1311 |
} else { |
|
1312 |
$widgetContent.prepend( '<p class="widget-error"><strong>' + message + '</strong></p>' ); |
|
1313 |
} |
|
1314 |
} |
|
1315 |
} ); |
|
1316 |
||
1317 |
jqxhr.fail( function( jqXHR, textStatus ) { |
|
1318 |
if ( completeCallback ) { |
|
1319 |
completeCallback.call( self, textStatus ); |
|
1320 |
} |
|
1321 |
} ); |
|
1322 |
||
1323 |
jqxhr.always( function() { |
|
1324 |
self.container.removeClass( 'widget-form-loading' ); |
|
1325 |
||
1326 |
$inputs.each( function() { |
|
1327 |
$( this ).removeData( 'state' + updateNumber ); |
|
1328 |
} ); |
|
1329 |
||
1330 |
processing( processing() - 1 ); |
|
1331 |
} ); |
|
1332 |
}, |
|
1333 |
||
1334 |
/** |
|
1335 |
* Expand the accordion section containing a control |
|
1336 |
*/ |
|
1337 |
expandControlSection: function() { |
|
1338 |
api.Control.prototype.expand.call( this ); |
|
1339 |
}, |
|
1340 |
||
1341 |
/** |
|
1342 |
* @since 4.1.0 |
|
1343 |
* |
|
1344 |
* @param {Boolean} expanded |
|
1345 |
* @param {Object} [params] |
|
16 | 1346 |
* @return {Boolean} False if state already applied. |
5 | 1347 |
*/ |
1348 |
_toggleExpanded: api.Section.prototype._toggleExpanded, |
|
1349 |
||
1350 |
/** |
|
1351 |
* @since 4.1.0 |
|
1352 |
* |
|
1353 |
* @param {Object} [params] |
|
16 | 1354 |
* @return {Boolean} False if already expanded. |
5 | 1355 |
*/ |
1356 |
expand: api.Section.prototype.expand, |
|
1357 |
||
1358 |
/** |
|
1359 |
* Expand the widget form control |
|
1360 |
* |
|
1361 |
* @deprecated 4.1.0 Use this.expand() instead. |
|
1362 |
*/ |
|
1363 |
expandForm: function() { |
|
1364 |
this.expand(); |
|
1365 |
}, |
|
1366 |
||
1367 |
/** |
|
1368 |
* @since 4.1.0 |
|
1369 |
* |
|
1370 |
* @param {Object} [params] |
|
16 | 1371 |
* @return {Boolean} False if already collapsed. |
5 | 1372 |
*/ |
1373 |
collapse: api.Section.prototype.collapse, |
|
1374 |
||
1375 |
/** |
|
1376 |
* Collapse the widget form control |
|
1377 |
* |
|
1378 |
* @deprecated 4.1.0 Use this.collapse() instead. |
|
1379 |
*/ |
|
1380 |
collapseForm: function() { |
|
1381 |
this.collapse(); |
|
1382 |
}, |
|
1383 |
||
1384 |
/** |
|
1385 |
* Expand or collapse the widget control |
|
1386 |
* |
|
1387 |
* @deprecated this is poor naming, and it is better to directly set control.expanded( showOrHide ) |
|
1388 |
* |
|
1389 |
* @param {boolean|undefined} [showOrHide] If not supplied, will be inverse of current visibility |
|
1390 |
*/ |
|
1391 |
toggleForm: function( showOrHide ) { |
|
1392 |
if ( typeof showOrHide === 'undefined' ) { |
|
1393 |
showOrHide = ! this.expanded(); |
|
1394 |
} |
|
1395 |
this.expanded( showOrHide ); |
|
1396 |
}, |
|
1397 |
||
1398 |
/** |
|
1399 |
* Respond to change in the expanded state. |
|
1400 |
* |
|
16 | 1401 |
* @param {boolean} expanded |
5 | 1402 |
* @param {Object} args merged on top of this.defaultActiveArguments |
1403 |
*/ |
|
1404 |
onChangeExpanded: function ( expanded, args ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
var self = this, $widget, $inside, complete, prevComplete, expandControl, $toggleBtn; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
self.embedWidgetControl(); // Make sure the outer form is embedded so that the expanded state can be set in the UI. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
if ( expanded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
self.embedWidgetContent(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
} |
5 | 1411 |
|
16 | 1412 |
// If the expanded state is unchanged only manipulate container expanded states. |
5 | 1413 |
if ( args.unchanged ) { |
1414 |
if ( expanded ) { |
|
1415 |
api.Control.prototype.expand.call( self, { |
|
1416 |
completeCallback: args.completeCallback |
|
1417 |
}); |
|
1418 |
} |
|
1419 |
return; |
|
1420 |
} |
|
1421 |
||
1422 |
$widget = this.container.find( 'div.widget:first' ); |
|
1423 |
$inside = $widget.find( '.widget-inside:first' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
$toggleBtn = this.container.find( '.widget-top button.widget-action' ); |
5 | 1425 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
expandControl = function() { |
5 | 1427 |
|
16 | 1428 |
// Close all other widget controls before expanding this one. |
5 | 1429 |
api.control.each( function( otherControl ) { |
1430 |
if ( self.params.type === otherControl.params.type && self !== otherControl ) { |
|
1431 |
otherControl.collapse(); |
|
1432 |
} |
|
1433 |
} ); |
|
1434 |
||
1435 |
complete = function() { |
|
1436 |
self.container.removeClass( 'expanding' ); |
|
1437 |
self.container.addClass( 'expanded' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
$widget.addClass( 'open' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
$toggleBtn.attr( 'aria-expanded', 'true' ); |
5 | 1440 |
self.container.trigger( 'expanded' ); |
1441 |
}; |
|
1442 |
if ( args.completeCallback ) { |
|
1443 |
prevComplete = complete; |
|
1444 |
complete = function () { |
|
1445 |
prevComplete(); |
|
1446 |
args.completeCallback(); |
|
1447 |
}; |
|
1448 |
} |
|
1449 |
||
1450 |
if ( self.params.is_wide ) { |
|
1451 |
$inside.fadeIn( args.duration, complete ); |
|
1452 |
} else { |
|
1453 |
$inside.slideDown( args.duration, complete ); |
|
1454 |
} |
|
1455 |
||
1456 |
self.container.trigger( 'expand' ); |
|
1457 |
self.container.addClass( 'expanding' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
|
19 | 1460 |
if ( $toggleBtn.attr( 'aria-expanded' ) === 'false' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
if ( api.section.has( self.section() ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
api.section( self.section() ).expand( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
completeCallback: expandControl |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1466 |
expandControl(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
} |
5 | 1468 |
} else { |
1469 |
complete = function() { |
|
1470 |
self.container.removeClass( 'collapsing' ); |
|
1471 |
self.container.removeClass( 'expanded' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
$widget.removeClass( 'open' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1473 |
$toggleBtn.attr( 'aria-expanded', 'false' ); |
5 | 1474 |
self.container.trigger( 'collapsed' ); |
1475 |
}; |
|
1476 |
if ( args.completeCallback ) { |
|
1477 |
prevComplete = complete; |
|
1478 |
complete = function () { |
|
1479 |
prevComplete(); |
|
1480 |
args.completeCallback(); |
|
1481 |
}; |
|
1482 |
} |
|
1483 |
||
1484 |
self.container.trigger( 'collapse' ); |
|
1485 |
self.container.addClass( 'collapsing' ); |
|
1486 |
||
1487 |
if ( self.params.is_wide ) { |
|
1488 |
$inside.fadeOut( args.duration, complete ); |
|
1489 |
} else { |
|
1490 |
$inside.slideUp( args.duration, function() { |
|
1491 |
$widget.css( { width:'', margin:'' } ); |
|
1492 |
complete(); |
|
1493 |
} ); |
|
1494 |
} |
|
1495 |
} |
|
1496 |
}, |
|
1497 |
||
1498 |
/** |
|
1499 |
* Get the position (index) of the widget in the containing sidebar |
|
1500 |
* |
|
16 | 1501 |
* @return {number} |
5 | 1502 |
*/ |
1503 |
getWidgetSidebarPosition: function() { |
|
1504 |
var sidebarWidgetIds, position; |
|
1505 |
||
1506 |
sidebarWidgetIds = this.getSidebarWidgetsControl().setting(); |
|
1507 |
position = _.indexOf( sidebarWidgetIds, this.params.widget_id ); |
|
1508 |
||
1509 |
if ( position === -1 ) { |
|
1510 |
return; |
|
1511 |
} |
|
1512 |
||
1513 |
return position; |
|
1514 |
}, |
|
1515 |
||
1516 |
/** |
|
1517 |
* Move widget up one in the sidebar |
|
1518 |
*/ |
|
1519 |
moveUp: function() { |
|
1520 |
this._moveWidgetByOne( -1 ); |
|
1521 |
}, |
|
1522 |
||
1523 |
/** |
|
1524 |
* Move widget up one in the sidebar |
|
1525 |
*/ |
|
1526 |
moveDown: function() { |
|
1527 |
this._moveWidgetByOne( 1 ); |
|
1528 |
}, |
|
1529 |
||
1530 |
/** |
|
1531 |
* @private |
|
1532 |
* |
|
16 | 1533 |
* @param {number} offset 1|-1 |
5 | 1534 |
*/ |
1535 |
_moveWidgetByOne: function( offset ) { |
|
1536 |
var i, sidebarWidgetsSetting, sidebarWidgetIds, adjacentWidgetId; |
|
1537 |
||
1538 |
i = this.getWidgetSidebarPosition(); |
|
1539 |
||
1540 |
sidebarWidgetsSetting = this.getSidebarWidgetsControl().setting; |
|
16 | 1541 |
sidebarWidgetIds = Array.prototype.slice.call( sidebarWidgetsSetting() ); // Clone. |
5 | 1542 |
adjacentWidgetId = sidebarWidgetIds[i + offset]; |
1543 |
sidebarWidgetIds[i + offset] = this.params.widget_id; |
|
1544 |
sidebarWidgetIds[i] = adjacentWidgetId; |
|
1545 |
||
1546 |
sidebarWidgetsSetting( sidebarWidgetIds ); |
|
1547 |
}, |
|
1548 |
||
1549 |
/** |
|
1550 |
* Toggle visibility of the widget move area |
|
1551 |
* |
|
16 | 1552 |
* @param {boolean} [showOrHide] |
5 | 1553 |
*/ |
1554 |
toggleWidgetMoveArea: function( showOrHide ) { |
|
1555 |
var self = this, $moveWidgetArea; |
|
1556 |
||
1557 |
$moveWidgetArea = this.container.find( '.move-widget-area' ); |
|
1558 |
||
1559 |
if ( typeof showOrHide === 'undefined' ) { |
|
1560 |
showOrHide = ! $moveWidgetArea.hasClass( 'active' ); |
|
1561 |
} |
|
1562 |
||
1563 |
if ( showOrHide ) { |
|
16 | 1564 |
// Reset the selected sidebar. |
5 | 1565 |
$moveWidgetArea.find( '.selected' ).removeClass( 'selected' ); |
1566 |
||
1567 |
$moveWidgetArea.find( 'li' ).filter( function() { |
|
1568 |
return $( this ).data( 'id' ) === self.params.sidebar_id; |
|
1569 |
} ).addClass( 'selected' ); |
|
1570 |
||
1571 |
this.container.find( '.move-widget-btn' ).prop( 'disabled', true ); |
|
1572 |
} |
|
1573 |
||
1574 |
$moveWidgetArea.toggleClass( 'active', showOrHide ); |
|
1575 |
}, |
|
1576 |
||
1577 |
/** |
|
1578 |
* Highlight the widget control and section |
|
1579 |
*/ |
|
1580 |
highlightSectionAndControl: function() { |
|
1581 |
var $target; |
|
1582 |
||
1583 |
if ( this.container.is( ':hidden' ) ) { |
|
1584 |
$target = this.container.closest( '.control-section' ); |
|
1585 |
} else { |
|
1586 |
$target = this.container; |
|
1587 |
} |
|
1588 |
||
1589 |
$( '.highlighted' ).removeClass( 'highlighted' ); |
|
1590 |
$target.addClass( 'highlighted' ); |
|
1591 |
||
1592 |
setTimeout( function() { |
|
1593 |
$target.removeClass( 'highlighted' ); |
|
1594 |
}, 500 ); |
|
1595 |
} |
|
1596 |
} ); |
|
1597 |
||
1598 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1599 |
* wp.customize.Widgets.WidgetsPanel |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1600 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1601 |
* Customizer panel containing the widget area sections. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1603 |
* @since 4.4.0 |
9 | 1604 |
* |
1605 |
* @class wp.customize.Widgets.WidgetsPanel |
|
1606 |
* @augments wp.customize.Panel |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1607 |
*/ |
9 | 1608 |
api.Widgets.WidgetsPanel = api.Panel.extend(/** @lends wp.customize.Widgets.WigetsPanel.prototype */{ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1609 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1610 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1611 |
* Add and manage the display of the no-rendered-areas notice. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1612 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1613 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1614 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1615 |
ready: function () { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
var panel = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1617 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1618 |
api.Panel.prototype.ready.call( panel ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1619 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1620 |
panel.deferred.embedded.done(function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1621 |
var panelMetaContainer, noticeContainer, updateNotice, getActiveSectionCount, shouldShowNotice; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1622 |
panelMetaContainer = panel.container.find( '.panel-meta' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1623 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
// @todo This should use the Notifications API introduced to panels. See <https://core.trac.wordpress.org/ticket/38794>. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1625 |
noticeContainer = $( '<div></div>', { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1626 |
'class': 'no-widget-areas-rendered-notice', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1627 |
'role': 'alert' |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1628 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1629 |
panelMetaContainer.append( noticeContainer ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1630 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1631 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1632 |
* Get the number of active sections in the panel. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1633 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1634 |
* @return {number} Number of active sidebar sections. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1635 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1636 |
getActiveSectionCount = function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1637 |
return _.filter( panel.sections(), function( section ) { |
9 | 1638 |
return 'sidebar' === section.params.type && section.active(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1639 |
} ).length; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1640 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1641 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1642 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1643 |
* Determine whether or not the notice should be displayed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1644 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1645 |
* @return {boolean} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1646 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1647 |
shouldShowNotice = function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1648 |
var activeSectionCount = getActiveSectionCount(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1649 |
if ( 0 === activeSectionCount ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1650 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1651 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1652 |
return activeSectionCount !== api.Widgets.data.registeredSidebars.length; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1653 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1654 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1655 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1656 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1657 |
* Update the notice. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1658 |
* |
16 | 1659 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1660 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1661 |
updateNotice = function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1662 |
var activeSectionCount = getActiveSectionCount(), someRenderedMessage, nonRenderedAreaCount, registeredAreaCount; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1663 |
noticeContainer.empty(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1664 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1665 |
registeredAreaCount = api.Widgets.data.registeredSidebars.length; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1666 |
if ( activeSectionCount !== registeredAreaCount ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1667 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1668 |
if ( 0 !== activeSectionCount ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1669 |
nonRenderedAreaCount = registeredAreaCount - activeSectionCount; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1670 |
someRenderedMessage = l10n.someAreasShown[ nonRenderedAreaCount ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1671 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1672 |
someRenderedMessage = l10n.noAreasShown; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
if ( someRenderedMessage ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
noticeContainer.append( $( '<p></p>', { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
text: someRenderedMessage |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1677 |
} ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1679 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1680 |
noticeContainer.append( $( '<p></p>', { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1681 |
text: l10n.navigatePreview |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1682 |
} ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1683 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1684 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
updateNotice(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1686 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
* Set the initial visibility state for rendered notice. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
* Update the visibility of the notice whenever a reflow happens. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1690 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
noticeContainer.toggle( shouldShowNotice() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
api.previewer.deferred.active.done( function () { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
noticeContainer.toggle( shouldShowNotice() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1694 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1695 |
api.bind( 'pane-contents-reflowed', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
var duration = ( 'resolved' === api.previewer.deferred.active.state() ) ? 'fast' : 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
updateNotice(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
if ( shouldShowNotice() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
noticeContainer.slideDown( duration ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1701 |
noticeContainer.slideUp( duration ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1702 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1703 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1704 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1705 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1706 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1707 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
* Allow an active widgets panel to be contextually active even when it has no active sections (widget areas). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
* This ensures that the widgets panel appears even when there are no |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
* sidebars displayed on the URL currently being previewed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1712 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1713 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1714 |
* |
16 | 1715 |
* @return {boolean} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1717 |
isContextuallyActive: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1718 |
var panel = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1719 |
return panel.active(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1720 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1721 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1722 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
/** |
5 | 1724 |
* wp.customize.Widgets.SidebarSection |
1725 |
* |
|
1726 |
* Customizer section representing a widget area widget |
|
1727 |
* |
|
1728 |
* @since 4.1.0 |
|
9 | 1729 |
* |
1730 |
* @class wp.customize.Widgets.SidebarSection |
|
1731 |
* @augments wp.customize.Section |
|
5 | 1732 |
*/ |
9 | 1733 |
api.Widgets.SidebarSection = api.Section.extend(/** @lends wp.customize.Widgets.SidebarSection.prototype */{ |
5 | 1734 |
|
1735 |
/** |
|
1736 |
* Sync the section's active state back to the Backbone model's is_rendered attribute |
|
1737 |
* |
|
1738 |
* @since 4.1.0 |
|
1739 |
*/ |
|
1740 |
ready: function () { |
|
1741 |
var section = this, registeredSidebar; |
|
1742 |
api.Section.prototype.ready.call( this ); |
|
1743 |
registeredSidebar = api.Widgets.registeredSidebars.get( section.params.sidebarId ); |
|
1744 |
section.active.bind( function ( active ) { |
|
1745 |
registeredSidebar.set( 'is_rendered', active ); |
|
1746 |
}); |
|
1747 |
registeredSidebar.set( 'is_rendered', section.active() ); |
|
1748 |
} |
|
1749 |
}); |
|
1750 |
||
1751 |
/** |
|
1752 |
* wp.customize.Widgets.SidebarControl |
|
1753 |
* |
|
1754 |
* Customizer control for widgets. |
|
1755 |
* Note that 'sidebar_widgets' must match the WP_Widget_Area_Customize_Control::$type |
|
1756 |
* |
|
1757 |
* @since 3.9.0 |
|
1758 |
* |
|
9 | 1759 |
* @class wp.customize.Widgets.SidebarControl |
5 | 1760 |
* @augments wp.customize.Control |
1761 |
*/ |
|
9 | 1762 |
api.Widgets.SidebarControl = api.Control.extend(/** @lends wp.customize.Widgets.SidebarControl.prototype */{ |
5 | 1763 |
|
1764 |
/** |
|
1765 |
* Set up the control |
|
1766 |
*/ |
|
1767 |
ready: function() { |
|
1768 |
this.$controlSection = this.container.closest( '.control-section' ); |
|
1769 |
this.$sectionContent = this.container.closest( '.accordion-section-content' ); |
|
1770 |
||
1771 |
this._setupModel(); |
|
1772 |
this._setupSortable(); |
|
1773 |
this._setupAddition(); |
|
1774 |
this._applyCardinalOrderClassNames(); |
|
1775 |
}, |
|
1776 |
||
1777 |
/** |
|
1778 |
* Update ordering of widget control forms when the setting is updated |
|
1779 |
*/ |
|
1780 |
_setupModel: function() { |
|
1781 |
var self = this; |
|
1782 |
||
1783 |
this.setting.bind( function( newWidgetIds, oldWidgetIds ) { |
|
1784 |
var widgetFormControls, removedWidgetIds, priority; |
|
1785 |
||
1786 |
removedWidgetIds = _( oldWidgetIds ).difference( newWidgetIds ); |
|
1787 |
||
16 | 1788 |
// Filter out any persistent widget IDs for widgets which have been deactivated. |
5 | 1789 |
newWidgetIds = _( newWidgetIds ).filter( function( newWidgetId ) { |
1790 |
var parsedWidgetId = parseWidgetId( newWidgetId ); |
|
1791 |
||
1792 |
return !! api.Widgets.availableWidgets.findWhere( { id_base: parsedWidgetId.id_base } ); |
|
1793 |
} ); |
|
1794 |
||
1795 |
widgetFormControls = _( newWidgetIds ).map( function( widgetId ) { |
|
1796 |
var widgetFormControl = api.Widgets.getWidgetFormControlForWidget( widgetId ); |
|
1797 |
||
1798 |
if ( ! widgetFormControl ) { |
|
1799 |
widgetFormControl = self.addWidget( widgetId ); |
|
1800 |
} |
|
1801 |
||
1802 |
return widgetFormControl; |
|
1803 |
} ); |
|
1804 |
||
16 | 1805 |
// Sort widget controls to their new positions. |
5 | 1806 |
widgetFormControls.sort( function( a, b ) { |
1807 |
var aIndex = _.indexOf( newWidgetIds, a.params.widget_id ), |
|
1808 |
bIndex = _.indexOf( newWidgetIds, b.params.widget_id ); |
|
1809 |
return aIndex - bIndex; |
|
1810 |
}); |
|
1811 |
||
1812 |
priority = 0; |
|
1813 |
_( widgetFormControls ).each( function ( control ) { |
|
1814 |
control.priority( priority ); |
|
1815 |
control.section( self.section() ); |
|
1816 |
priority += 1; |
|
1817 |
}); |
|
16 | 1818 |
self.priority( priority ); // Make sure sidebar control remains at end. |
5 | 1819 |
|
16 | 1820 |
// Re-sort widget form controls (including widgets form other sidebars newly moved here). |
5 | 1821 |
self._applyCardinalOrderClassNames(); |
1822 |
||
16 | 1823 |
// If the widget was dragged into the sidebar, make sure the sidebar_id param is updated. |
5 | 1824 |
_( widgetFormControls ).each( function( widgetFormControl ) { |
1825 |
widgetFormControl.params.sidebar_id = self.params.sidebar_id; |
|
1826 |
} ); |
|
1827 |
||
16 | 1828 |
// Cleanup after widget removal. |
5 | 1829 |
_( removedWidgetIds ).each( function( removedWidgetId ) { |
1830 |
||
16 | 1831 |
// Using setTimeout so that when moving a widget to another sidebar, |
1832 |
// the other sidebars_widgets settings get a chance to update. |
|
5 | 1833 |
setTimeout( function() { |
1834 |
var removedControl, wasDraggedToAnotherSidebar, inactiveWidgets, removedIdBase, |
|
1835 |
widget, isPresentInAnotherSidebar = false; |
|
1836 |
||
16 | 1837 |
// Check if the widget is in another sidebar. |
5 | 1838 |
api.each( function( otherSetting ) { |
1839 |
if ( otherSetting.id === self.setting.id || 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) || otherSetting.id === 'sidebars_widgets[wp_inactive_widgets]' ) { |
|
1840 |
return; |
|
1841 |
} |
|
1842 |
||
1843 |
var otherSidebarWidgets = otherSetting(), i; |
|
1844 |
||
1845 |
i = _.indexOf( otherSidebarWidgets, removedWidgetId ); |
|
1846 |
if ( -1 !== i ) { |
|
1847 |
isPresentInAnotherSidebar = true; |
|
1848 |
} |
|
1849 |
} ); |
|
1850 |
||
1851 |
// If the widget is present in another sidebar, abort! |
|
1852 |
if ( isPresentInAnotherSidebar ) { |
|
1853 |
return; |
|
1854 |
} |
|
1855 |
||
1856 |
removedControl = api.Widgets.getWidgetFormControlForWidget( removedWidgetId ); |
|
1857 |
||
16 | 1858 |
// Detect if widget control was dragged to another sidebar. |
5 | 1859 |
wasDraggedToAnotherSidebar = removedControl && $.contains( document, removedControl.container[0] ) && ! $.contains( self.$sectionContent[0], removedControl.container[0] ); |
1860 |
||
16 | 1861 |
// Delete any widget form controls for removed widgets. |
5 | 1862 |
if ( removedControl && ! wasDraggedToAnotherSidebar ) { |
1863 |
api.control.remove( removedControl.id ); |
|
1864 |
removedControl.container.remove(); |
|
1865 |
} |
|
1866 |
||
16 | 1867 |
// Move widget to inactive widgets sidebar (move it to Trash) if has been previously saved. |
1868 |
// This prevents the inactive widgets sidebar from overflowing with throwaway widgets. |
|
5 | 1869 |
if ( api.Widgets.savedWidgetIds[removedWidgetId] ) { |
1870 |
inactiveWidgets = api.value( 'sidebars_widgets[wp_inactive_widgets]' )().slice(); |
|
1871 |
inactiveWidgets.push( removedWidgetId ); |
|
1872 |
api.value( 'sidebars_widgets[wp_inactive_widgets]' )( _( inactiveWidgets ).unique() ); |
|
1873 |
} |
|
1874 |
||
16 | 1875 |
// Make old single widget available for adding again. |
5 | 1876 |
removedIdBase = parseWidgetId( removedWidgetId ).id_base; |
1877 |
widget = api.Widgets.availableWidgets.findWhere( { id_base: removedIdBase } ); |
|
1878 |
if ( widget && ! widget.get( 'is_multi' ) ) { |
|
1879 |
widget.set( 'is_disabled', false ); |
|
1880 |
} |
|
1881 |
} ); |
|
1882 |
||
1883 |
} ); |
|
1884 |
} ); |
|
1885 |
}, |
|
1886 |
||
1887 |
/** |
|
1888 |
* Allow widgets in sidebar to be re-ordered, and for the order to be previewed |
|
1889 |
*/ |
|
1890 |
_setupSortable: function() { |
|
1891 |
var self = this; |
|
1892 |
||
1893 |
this.isReordering = false; |
|
1894 |
||
1895 |
/** |
|
1896 |
* Update widget order setting when controls are re-ordered |
|
1897 |
*/ |
|
1898 |
this.$sectionContent.sortable( { |
|
1899 |
items: '> .customize-control-widget_form', |
|
1900 |
handle: '.widget-top', |
|
1901 |
axis: 'y', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1902 |
tolerance: 'pointer', |
5 | 1903 |
connectWith: '.accordion-section-content:has(.customize-control-sidebar_widgets)', |
1904 |
update: function() { |
|
1905 |
var widgetContainerIds = self.$sectionContent.sortable( 'toArray' ), widgetIds; |
|
1906 |
||
1907 |
widgetIds = $.map( widgetContainerIds, function( widgetContainerId ) { |
|
1908 |
return $( '#' + widgetContainerId ).find( ':input[name=widget-id]' ).val(); |
|
1909 |
} ); |
|
1910 |
||
1911 |
self.setting( widgetIds ); |
|
1912 |
} |
|
1913 |
} ); |
|
1914 |
||
1915 |
/** |
|
1916 |
* Expand other Customizer sidebar section when dragging a control widget over it, |
|
1917 |
* allowing the control to be dropped into another section |
|
1918 |
*/ |
|
1919 |
this.$controlSection.find( '.accordion-section-title' ).droppable({ |
|
1920 |
accept: '.customize-control-widget_form', |
|
1921 |
over: function() { |
|
1922 |
var section = api.section( self.section.get() ); |
|
1923 |
section.expand({ |
|
16 | 1924 |
allowMultiple: true, // Prevent the section being dragged from to be collapsed. |
5 | 1925 |
completeCallback: function () { |
16 | 1926 |
// @todo It is not clear when refreshPositions should be called on which sections, or if it is even needed. |
5 | 1927 |
api.section.each( function ( otherSection ) { |
1928 |
if ( otherSection.container.find( '.customize-control-sidebar_widgets' ).length ) { |
|
1929 |
otherSection.container.find( '.accordion-section-content:first' ).sortable( 'refreshPositions' ); |
|
1930 |
} |
|
1931 |
} ); |
|
1932 |
} |
|
1933 |
}); |
|
1934 |
} |
|
1935 |
}); |
|
1936 |
||
1937 |
/** |
|
1938 |
* Keyboard-accessible reordering |
|
1939 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
this.container.find( '.reorder-toggle' ).on( 'click', function() { |
5 | 1941 |
self.toggleReordering( ! self.isReordering ); |
1942 |
} ); |
|
1943 |
}, |
|
1944 |
||
1945 |
/** |
|
1946 |
* Set up UI for adding a new widget |
|
1947 |
*/ |
|
1948 |
_setupAddition: function() { |
|
1949 |
var self = this; |
|
1950 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1951 |
this.container.find( '.add-new-widget' ).on( 'click', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1952 |
var addNewWidgetBtn = $( this ); |
5 | 1953 |
|
1954 |
if ( self.$sectionContent.hasClass( 'reordering' ) ) { |
|
1955 |
return; |
|
1956 |
} |
|
1957 |
||
1958 |
if ( ! $( 'body' ).hasClass( 'adding-widget' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1959 |
addNewWidgetBtn.attr( 'aria-expanded', 'true' ); |
5 | 1960 |
api.Widgets.availableWidgetsPanel.open( self ); |
1961 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
addNewWidgetBtn.attr( 'aria-expanded', 'false' ); |
5 | 1963 |
api.Widgets.availableWidgetsPanel.close(); |
1964 |
} |
|
1965 |
} ); |
|
1966 |
}, |
|
1967 |
||
1968 |
/** |
|
1969 |
* Add classes to the widget_form controls to assist with styling |
|
1970 |
*/ |
|
1971 |
_applyCardinalOrderClassNames: function() { |
|
1972 |
var widgetControls = []; |
|
1973 |
_.each( this.setting(), function ( widgetId ) { |
|
1974 |
var widgetControl = api.Widgets.getWidgetFormControlForWidget( widgetId ); |
|
1975 |
if ( widgetControl ) { |
|
1976 |
widgetControls.push( widgetControl ); |
|
1977 |
} |
|
1978 |
}); |
|
1979 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1980 |
if ( 0 === widgetControls.length || ( 1 === api.Widgets.registeredSidebars.length && widgetControls.length <= 1 ) ) { |
5 | 1981 |
this.container.find( '.reorder-toggle' ).hide(); |
1982 |
return; |
|
1983 |
} else { |
|
1984 |
this.container.find( '.reorder-toggle' ).show(); |
|
1985 |
} |
|
1986 |
||
1987 |
$( widgetControls ).each( function () { |
|
1988 |
$( this.container ) |
|
1989 |
.removeClass( 'first-widget' ) |
|
1990 |
.removeClass( 'last-widget' ) |
|
1991 |
.find( '.move-widget-down, .move-widget-up' ).prop( 'tabIndex', 0 ); |
|
1992 |
}); |
|
1993 |
||
1994 |
_.first( widgetControls ).container |
|
1995 |
.addClass( 'first-widget' ) |
|
1996 |
.find( '.move-widget-up' ).prop( 'tabIndex', -1 ); |
|
1997 |
||
1998 |
_.last( widgetControls ).container |
|
1999 |
.addClass( 'last-widget' ) |
|
2000 |
.find( '.move-widget-down' ).prop( 'tabIndex', -1 ); |
|
2001 |
}, |
|
2002 |
||
2003 |
||
2004 |
/*********************************************************************** |
|
2005 |
* Begin public API methods |
|
2006 |
**********************************************************************/ |
|
2007 |
||
2008 |
/** |
|
2009 |
* Enable/disable the reordering UI |
|
2010 |
* |
|
16 | 2011 |
* @param {boolean} showOrHide to enable/disable reordering |
5 | 2012 |
* |
2013 |
* @todo We should have a reordering state instead and rename this to onChangeReordering |
|
2014 |
*/ |
|
2015 |
toggleReordering: function( showOrHide ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
var addNewWidgetBtn = this.$sectionContent.find( '.add-new-widget' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2017 |
reorderBtn = this.container.find( '.reorder-toggle' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2018 |
widgetsTitle = this.$sectionContent.find( '.widget-title' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2019 |
|
5 | 2020 |
showOrHide = Boolean( showOrHide ); |
2021 |
||
2022 |
if ( showOrHide === this.$sectionContent.hasClass( 'reordering' ) ) { |
|
2023 |
return; |
|
2024 |
} |
|
2025 |
||
2026 |
this.isReordering = showOrHide; |
|
2027 |
this.$sectionContent.toggleClass( 'reordering', showOrHide ); |
|
2028 |
||
2029 |
if ( showOrHide ) { |
|
2030 |
_( this.getWidgetFormControls() ).each( function( formControl ) { |
|
2031 |
formControl.collapse(); |
|
2032 |
} ); |
|
2033 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
addNewWidgetBtn.attr({ 'tabindex': '-1', 'aria-hidden': 'true' }); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2035 |
reorderBtn.attr( 'aria-label', l10n.reorderLabelOff ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2036 |
wp.a11y.speak( l10n.reorderModeOn ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2037 |
// Hide widget titles while reordering: title is already in the reorder controls. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2038 |
widgetsTitle.attr( 'aria-hidden', 'true' ); |
5 | 2039 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2040 |
addNewWidgetBtn.removeAttr( 'tabindex aria-hidden' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2041 |
reorderBtn.attr( 'aria-label', l10n.reorderLabelOn ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2042 |
wp.a11y.speak( l10n.reorderModeOff ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2043 |
widgetsTitle.attr( 'aria-hidden', 'false' ); |
5 | 2044 |
} |
2045 |
}, |
|
2046 |
||
2047 |
/** |
|
2048 |
* Get the widget_form Customize controls associated with the current sidebar. |
|
2049 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2050 |
* @since 3.9.0 |
5 | 2051 |
* @return {wp.customize.controlConstructor.widget_form[]} |
2052 |
*/ |
|
2053 |
getWidgetFormControls: function() { |
|
2054 |
var formControls = []; |
|
2055 |
||
2056 |
_( this.setting() ).each( function( widgetId ) { |
|
2057 |
var settingId = widgetIdToSettingId( widgetId ), |
|
2058 |
formControl = api.control( settingId ); |
|
2059 |
if ( formControl ) { |
|
2060 |
formControls.push( formControl ); |
|
2061 |
} |
|
2062 |
} ); |
|
2063 |
||
2064 |
return formControls; |
|
2065 |
}, |
|
2066 |
||
2067 |
/** |
|
16 | 2068 |
* @param {string} widgetId or an id_base for adding a previously non-existing widget. |
2069 |
* @return {Object|false} widget_form control instance, or false on error. |
|
5 | 2070 |
*/ |
2071 |
addWidget: function( widgetId ) { |
|
2072 |
var self = this, controlHtml, $widget, controlType = 'widget_form', controlContainer, controlConstructor, |
|
2073 |
parsedWidgetId = parseWidgetId( widgetId ), |
|
2074 |
widgetNumber = parsedWidgetId.number, |
|
2075 |
widgetIdBase = parsedWidgetId.id_base, |
|
2076 |
widget = api.Widgets.availableWidgets.findWhere( {id_base: widgetIdBase} ), |
|
2077 |
settingId, isExistingWidget, widgetFormControl, sidebarWidgets, settingArgs, setting; |
|
2078 |
||
2079 |
if ( ! widget ) { |
|
2080 |
return false; |
|
2081 |
} |
|
2082 |
||
2083 |
if ( widgetNumber && ! widget.get( 'is_multi' ) ) { |
|
2084 |
return false; |
|
2085 |
} |
|
2086 |
||
16 | 2087 |
// Set up new multi widget. |
5 | 2088 |
if ( widget.get( 'is_multi' ) && ! widgetNumber ) { |
2089 |
widget.set( 'multi_number', widget.get( 'multi_number' ) + 1 ); |
|
2090 |
widgetNumber = widget.get( 'multi_number' ); |
|
2091 |
} |
|
2092 |
||
18 | 2093 |
controlHtml = $( '#widget-tpl-' + widget.get( 'id' ) ).html().trim(); |
5 | 2094 |
if ( widget.get( 'is_multi' ) ) { |
2095 |
controlHtml = controlHtml.replace( /<[^<>]+>/g, function( m ) { |
|
2096 |
return m.replace( /__i__|%i%/g, widgetNumber ); |
|
2097 |
} ); |
|
2098 |
} else { |
|
16 | 2099 |
widget.set( 'is_disabled', true ); // Prevent single widget from being added again now. |
5 | 2100 |
} |
2101 |
||
2102 |
$widget = $( controlHtml ); |
|
2103 |
||
2104 |
controlContainer = $( '<li/>' ) |
|
2105 |
.addClass( 'customize-control' ) |
|
2106 |
.addClass( 'customize-control-' + controlType ) |
|
2107 |
.append( $widget ); |
|
2108 |
||
16 | 2109 |
// Remove icon which is visible inside the panel. |
5 | 2110 |
controlContainer.find( '> .widget-icon' ).remove(); |
2111 |
||
2112 |
if ( widget.get( 'is_multi' ) ) { |
|
2113 |
controlContainer.find( 'input[name="widget_number"]' ).val( widgetNumber ); |
|
2114 |
controlContainer.find( 'input[name="multi_number"]' ).val( widgetNumber ); |
|
2115 |
} |
|
2116 |
||
2117 |
widgetId = controlContainer.find( '[name="widget-id"]' ).val(); |
|
2118 |
||
16 | 2119 |
controlContainer.hide(); // To be slid-down below. |
5 | 2120 |
|
2121 |
settingId = 'widget_' + widget.get( 'id_base' ); |
|
2122 |
if ( widget.get( 'is_multi' ) ) { |
|
2123 |
settingId += '[' + widgetNumber + ']'; |
|
2124 |
} |
|
2125 |
controlContainer.attr( 'id', 'customize-control-' + settingId.replace( /\]/g, '' ).replace( /\[/g, '-' ) ); |
|
2126 |
||
16 | 2127 |
// Only create setting if it doesn't already exist (if we're adding a pre-existing inactive widget). |
5 | 2128 |
isExistingWidget = api.has( settingId ); |
2129 |
if ( ! isExistingWidget ) { |
|
2130 |
settingArgs = { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2131 |
transport: api.Widgets.data.selectiveRefreshableWidgets[ widget.get( 'id_base' ) ] ? 'postMessage' : 'refresh', |
5 | 2132 |
previewer: this.setting.previewer |
2133 |
}; |
|
2134 |
setting = api.create( settingId, settingId, '', settingArgs ); |
|
16 | 2135 |
setting.set( {} ); // Mark dirty, changing from '' to {}. |
5 | 2136 |
} |
2137 |
||
2138 |
controlConstructor = api.controlConstructor[controlType]; |
|
2139 |
widgetFormControl = new controlConstructor( settingId, { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2140 |
settings: { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2141 |
'default': settingId |
5 | 2142 |
}, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2143 |
content: controlContainer, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2144 |
sidebar_id: self.params.sidebar_id, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2145 |
widget_id: widgetId, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2146 |
widget_id_base: widget.get( 'id_base' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2147 |
type: controlType, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2148 |
is_new: ! isExistingWidget, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2149 |
width: widget.get( 'width' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2150 |
height: widget.get( 'height' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2151 |
is_wide: widget.get( 'is_wide' ) |
5 | 2152 |
} ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2153 |
api.control.add( widgetFormControl ); |
5 | 2154 |
|
16 | 2155 |
// Make sure widget is removed from the other sidebars. |
5 | 2156 |
api.each( function( otherSetting ) { |
2157 |
if ( otherSetting.id === self.setting.id ) { |
|
2158 |
return; |
|
2159 |
} |
|
2160 |
||
2161 |
if ( 0 !== otherSetting.id.indexOf( 'sidebars_widgets[' ) ) { |
|
2162 |
return; |
|
2163 |
} |
|
2164 |
||
2165 |
var otherSidebarWidgets = otherSetting().slice(), |
|
2166 |
i = _.indexOf( otherSidebarWidgets, widgetId ); |
|
2167 |
||
2168 |
if ( -1 !== i ) { |
|
2169 |
otherSidebarWidgets.splice( i ); |
|
2170 |
otherSetting( otherSidebarWidgets ); |
|
2171 |
} |
|
2172 |
} ); |
|
2173 |
||
16 | 2174 |
// Add widget to this sidebar. |
5 | 2175 |
sidebarWidgets = this.setting().slice(); |
2176 |
if ( -1 === _.indexOf( sidebarWidgets, widgetId ) ) { |
|
2177 |
sidebarWidgets.push( widgetId ); |
|
2178 |
this.setting( sidebarWidgets ); |
|
2179 |
} |
|
2180 |
||
2181 |
controlContainer.slideDown( function() { |
|
2182 |
if ( isExistingWidget ) { |
|
2183 |
widgetFormControl.updateWidget( { |
|
2184 |
instance: widgetFormControl.setting() |
|
2185 |
} ); |
|
2186 |
} |
|
2187 |
} ); |
|
2188 |
||
2189 |
return widgetFormControl; |
|
2190 |
} |
|
2191 |
} ); |
|
2192 |
||
16 | 2193 |
// Register models for custom panel, section, and control types. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
$.extend( api.panelConstructor, { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2195 |
widgets: api.Widgets.WidgetsPanel |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2196 |
}); |
5 | 2197 |
$.extend( api.sectionConstructor, { |
2198 |
sidebar: api.Widgets.SidebarSection |
|
2199 |
}); |
|
2200 |
$.extend( api.controlConstructor, { |
|
2201 |
widget_form: api.Widgets.WidgetControl, |
|
2202 |
sidebar_widgets: api.Widgets.SidebarControl |
|
2203 |
}); |
|
2204 |
||
2205 |
/** |
|
2206 |
* Init Customizer for widgets. |
|
2207 |
*/ |
|
2208 |
api.bind( 'ready', function() { |
|
16 | 2209 |
// Set up the widgets panel. |
5 | 2210 |
api.Widgets.availableWidgetsPanel = new api.Widgets.AvailableWidgetsPanelView({ |
2211 |
collection: api.Widgets.availableWidgets |
|
2212 |
}); |
|
2213 |
||
16 | 2214 |
// Highlight widget control. |
5 | 2215 |
api.previewer.bind( 'highlight-widget-control', api.Widgets.highlightWidgetFormControl ); |
2216 |
||
16 | 2217 |
// Open and focus widget control. |
5 | 2218 |
api.previewer.bind( 'focus-widget-control', api.Widgets.focusWidgetFormControl ); |
2219 |
} ); |
|
2220 |
||
2221 |
/** |
|
2222 |
* Highlight a widget control. |
|
2223 |
* |
|
2224 |
* @param {string} widgetId |
|
2225 |
*/ |
|
2226 |
api.Widgets.highlightWidgetFormControl = function( widgetId ) { |
|
2227 |
var control = api.Widgets.getWidgetFormControlForWidget( widgetId ); |
|
2228 |
||
2229 |
if ( control ) { |
|
2230 |
control.highlightSectionAndControl(); |
|
2231 |
} |
|
2232 |
}, |
|
2233 |
||
2234 |
/** |
|
2235 |
* Focus a widget control. |
|
2236 |
* |
|
2237 |
* @param {string} widgetId |
|
2238 |
*/ |
|
2239 |
api.Widgets.focusWidgetFormControl = function( widgetId ) { |
|
2240 |
var control = api.Widgets.getWidgetFormControlForWidget( widgetId ); |
|
2241 |
||
2242 |
if ( control ) { |
|
2243 |
control.focus(); |
|
2244 |
} |
|
2245 |
}, |
|
2246 |
||
2247 |
/** |
|
2248 |
* Given a widget control, find the sidebar widgets control that contains it. |
|
2249 |
* @param {string} widgetId |
|
16 | 2250 |
* @return {Object|null} |
5 | 2251 |
*/ |
2252 |
api.Widgets.getSidebarWidgetControlContainingWidget = function( widgetId ) { |
|
2253 |
var foundControl = null; |
|
2254 |
||
16 | 2255 |
// @todo This can use widgetIdToSettingId(), then pass into wp.customize.control( x ).getSidebarWidgetsControl(). |
5 | 2256 |
api.control.each( function( control ) { |
2257 |
if ( control.params.type === 'sidebar_widgets' && -1 !== _.indexOf( control.setting(), widgetId ) ) { |
|
2258 |
foundControl = control; |
|
2259 |
} |
|
2260 |
} ); |
|
2261 |
||
2262 |
return foundControl; |
|
2263 |
}; |
|
2264 |
||
2265 |
/** |
|
2266 |
* Given a widget ID for a widget appearing in the preview, get the widget form control associated with it. |
|
2267 |
* |
|
2268 |
* @param {string} widgetId |
|
16 | 2269 |
* @return {Object|null} |
5 | 2270 |
*/ |
2271 |
api.Widgets.getWidgetFormControlForWidget = function( widgetId ) { |
|
2272 |
var foundControl = null; |
|
2273 |
||
16 | 2274 |
// @todo We can just use widgetIdToSettingId() here. |
5 | 2275 |
api.control.each( function( control ) { |
2276 |
if ( control.params.type === 'widget_form' && control.params.widget_id === widgetId ) { |
|
2277 |
foundControl = control; |
|
2278 |
} |
|
2279 |
} ); |
|
2280 |
||
2281 |
return foundControl; |
|
2282 |
}; |
|
2283 |
||
2284 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2285 |
* Initialize Edit Menu button in Nav Menu widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2286 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2287 |
$( document ).on( 'widget-added', function( event, widgetContainer ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2288 |
var parsedWidgetId, widgetControl, navMenuSelect, editMenuButton; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2289 |
parsedWidgetId = parseWidgetId( widgetContainer.find( '> .widget-inside > .form > .widget-id' ).val() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
if ( 'nav_menu' !== parsedWidgetId.id_base ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2291 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2292 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
widgetControl = api.control( 'widget_nav_menu[' + String( parsedWidgetId.number ) + ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2294 |
if ( ! widgetControl ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2295 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2296 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2297 |
navMenuSelect = widgetContainer.find( 'select[name*="nav_menu"]' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2298 |
editMenuButton = widgetContainer.find( '.edit-selected-nav-menu > button' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2299 |
if ( 0 === navMenuSelect.length || 0 === editMenuButton.length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2300 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2301 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2302 |
navMenuSelect.on( 'change', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2303 |
if ( api.section.has( 'nav_menu[' + navMenuSelect.val() + ']' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2304 |
editMenuButton.parent().show(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2305 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2306 |
editMenuButton.parent().hide(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2307 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2309 |
editMenuButton.on( 'click', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2310 |
var section = api.section( 'nav_menu[' + navMenuSelect.val() + ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2311 |
if ( section ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2312 |
focusConstructWithBreadcrumb( section, widgetControl ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2313 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2314 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2315 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2317 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2318 |
* Focus (expand) one construct and then focus on another construct after the first is collapsed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2320 |
* This overrides the back button to serve the purpose of breadcrumb navigation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2321 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2322 |
* @param {wp.customize.Section|wp.customize.Panel|wp.customize.Control} focusConstruct - The object to initially focus. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2323 |
* @param {wp.customize.Section|wp.customize.Panel|wp.customize.Control} returnConstruct - The object to return focus. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
function focusConstructWithBreadcrumb( focusConstruct, returnConstruct ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
focusConstruct.focus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
function onceCollapsed( isExpanded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2328 |
if ( ! isExpanded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2329 |
focusConstruct.expanded.unbind( onceCollapsed ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2330 |
returnConstruct.focus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2331 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2332 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2333 |
focusConstruct.expanded.bind( onceCollapsed ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2334 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2335 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2336 |
/** |
16 | 2337 |
* @param {string} widgetId |
2338 |
* @return {Object} |
|
5 | 2339 |
*/ |
2340 |
function parseWidgetId( widgetId ) { |
|
2341 |
var matches, parsed = { |
|
2342 |
number: null, |
|
2343 |
id_base: null |
|
2344 |
}; |
|
2345 |
||
2346 |
matches = widgetId.match( /^(.+)-(\d+)$/ ); |
|
2347 |
if ( matches ) { |
|
2348 |
parsed.id_base = matches[1]; |
|
2349 |
parsed.number = parseInt( matches[2], 10 ); |
|
2350 |
} else { |
|
16 | 2351 |
// Likely an old single widget. |
5 | 2352 |
parsed.id_base = widgetId; |
2353 |
} |
|
2354 |
||
2355 |
return parsed; |
|
2356 |
} |
|
2357 |
||
2358 |
/** |
|
16 | 2359 |
* @param {string} widgetId |
2360 |
* @return {string} settingId |
|
5 | 2361 |
*/ |
2362 |
function widgetIdToSettingId( widgetId ) { |
|
2363 |
var parsed = parseWidgetId( widgetId ), settingId; |
|
2364 |
||
2365 |
settingId = 'widget_' + parsed.id_base; |
|
2366 |
if ( parsed.number ) { |
|
2367 |
settingId += '[' + parsed.number + ']'; |
|
2368 |
} |
|
2369 |
||
2370 |
return settingId; |
|
2371 |
} |
|
2372 |
||
2373 |
})( window.wp, jQuery ); |