107 this.scrollIntoView(); |
93 this.scrollIntoView(); |
108 } |
94 } |
109 $( this ).find( '.widget-inside :tabbable:first' ).focus(); |
95 $( this ).find( '.widget-inside :tabbable:first' ).focus(); |
110 } ); |
96 } ); |
111 |
97 |
112 event.returnValue = wpWidgets.l10n.saveAlert; |
98 event.returnValue = wp.i18n.__( 'The changes you made will be lost if you navigate away from this page.' ); |
113 return event.returnValue; |
99 return event.returnValue; |
114 } |
100 } |
115 }); |
101 }); |
116 |
102 |
117 // Handle the widgets containers in the left column. |
103 // Handle the widgets containers in the left column. |
125 // Update the admin menu "sticky" state. |
111 // Update the admin menu "sticky" state. |
126 $document.triggerHandler( 'wp-pin-menu' ); |
112 $document.triggerHandler( 'wp-pin-menu' ); |
127 }); |
113 }); |
128 |
114 |
129 $(document.body).bind('click.widgets-toggle', function(e) { |
115 $(document.body).bind('click.widgets-toggle', function(e) { |
130 var target = $(e.target), |
116 var target = $(e.target), css = {}, |
131 css = { 'z-index': 100 }, |
|
132 widget, inside, targetWidth, widgetWidth, margin, saveButton, widgetId, |
117 widget, inside, targetWidth, widgetWidth, margin, saveButton, widgetId, |
133 toggleBtn = target.closest( '.widget' ).find( '.widget-top button.widget-action' ); |
118 toggleBtn = target.closest( '.widget' ).find( '.widget-top button.widget-action' ); |
134 |
119 |
135 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
120 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
136 widget = target.closest('div.widget'); |
121 widget = target.closest('div.widget'); |
140 widgetId = inside.find( '.widget-id' ).val(); |
125 widgetId = inside.find( '.widget-id' ).val(); |
141 |
126 |
142 // Save button is initially disabled, but is enabled when a field is changed. |
127 // Save button is initially disabled, but is enabled when a field is changed. |
143 if ( ! widget.data( 'dirty-state-initialized' ) ) { |
128 if ( ! widget.data( 'dirty-state-initialized' ) ) { |
144 saveButton = inside.find( '.widget-control-save' ); |
129 saveButton = inside.find( '.widget-control-save' ); |
145 saveButton.prop( 'disabled', true ).val( wpWidgets.l10n.saved ); |
130 saveButton.prop( 'disabled', true ).val( wp.i18n.__( 'Saved' ) ); |
146 inside.on( 'input change', function() { |
131 inside.on( 'input change', function() { |
147 self.dirtyWidgets[ widgetId ] = true; |
132 self.dirtyWidgets[ widgetId ] = true; |
148 widget.addClass( 'widget-dirty' ); |
133 widget.addClass( 'widget-dirty' ); |
149 saveButton.prop( 'disabled', false ).val( wpWidgets.l10n.save ); |
134 saveButton.prop( 'disabled', false ).val( wp.i18n.__( 'Save' ) ); |
150 }); |
135 }); |
151 widget.data( 'dirty-state-initialized', true ); |
136 widget.data( 'dirty-state-initialized', true ); |
152 } |
137 } |
153 |
138 |
154 if ( inside.is(':hidden') ) { |
139 if ( inside.is(':hidden') ) { |
210 $('#widget-list').children('.widget').draggable({ |
195 $('#widget-list').children('.widget').draggable({ |
211 connectToSortable: 'div.widgets-sortables', |
196 connectToSortable: 'div.widgets-sortables', |
212 handle: '> .widget-top > .widget-title', |
197 handle: '> .widget-top > .widget-title', |
213 distance: 2, |
198 distance: 2, |
214 helper: 'clone', |
199 helper: 'clone', |
215 zIndex: 100, |
200 zIndex: 101, |
216 containment: '#wpwrap', |
201 containment: '#wpwrap', |
217 refreshPositions: true, |
202 refreshPositions: true, |
218 start: function( event, ui ) { |
203 start: function( event, ui ) { |
219 var chooser = $(this).find('.widgets-chooser'); |
204 var chooser = $(this).find('.widgets-chooser'); |
220 |
205 |
221 ui.helper.find('div.widget-description').hide(); |
206 ui.helper.find('div.widget-description').hide(); |
222 the_id = this.id; |
207 the_id = this.id; |
223 |
208 |
224 if ( chooser.length ) { |
209 if ( chooser.length ) { |
225 // Hide the chooser and move it out of the widget |
210 // Hide the chooser and move it out of the widget. |
226 $( '#wpbody-content' ).append( chooser.hide() ); |
211 $( '#wpbody-content' ).append( chooser.hide() ); |
227 // Delete the cloned chooser from the drag helper |
212 // Delete the cloned chooser from the drag helper. |
228 ui.helper.find('.widgets-chooser').remove(); |
213 ui.helper.find('.widgets-chooser').remove(); |
229 self.clearWidgetSelection(); |
214 self.clearWidgetSelection(); |
230 } |
215 } |
231 }, |
216 }, |
232 stop: function() { |
217 stop: function() { |
247 /** |
232 /** |
248 * Open Sidebar when a Widget gets dragged over it. |
233 * Open Sidebar when a Widget gets dragged over it. |
249 * |
234 * |
250 * @ignore |
235 * @ignore |
251 * |
236 * |
252 * @param {object} event jQuery event object. |
237 * @param {Object} event jQuery event object. |
253 */ |
238 */ |
254 over: function( event ) { |
239 over: function( event ) { |
255 var $wrap = $( event.target ).parent(); |
240 var $wrap = $( event.target ).parent(); |
256 |
241 |
257 if ( wpWidgets.hoveredSidebar && ! $wrap.is( wpWidgets.hoveredSidebar ) ) { |
242 if ( wpWidgets.hoveredSidebar && ! $wrap.is( wpWidgets.hoveredSidebar ) ) { |
272 /** |
257 /** |
273 * Close Sidebar when the Widget gets dragged out of it. |
258 * Close Sidebar when the Widget gets dragged out of it. |
274 * |
259 * |
275 * @ignore |
260 * @ignore |
276 * |
261 * |
277 * @param {object} event jQuery event object. |
262 * @param {Object} event jQuery event object. |
278 */ |
263 */ |
279 out: function( event ) { |
264 out: function( event ) { |
280 if ( wpWidgets.hoveredSidebar ) { |
265 if ( wpWidgets.hoveredSidebar ) { |
281 wpWidgets.closeSidebar( event ); |
266 wpWidgets.closeSidebar( event ); |
282 } |
267 } |
319 |
304 |
320 // Reset the var to hold a previously closed sidebar. |
305 // Reset the var to hold a previously closed sidebar. |
321 wpWidgets.hoveredSidebar = null; |
306 wpWidgets.hoveredSidebar = null; |
322 |
307 |
323 if ( $widget.hasClass('deleting') ) { |
308 if ( $widget.hasClass('deleting') ) { |
324 wpWidgets.save( $widget, 1, 0, 1 ); // delete widget |
309 wpWidgets.save( $widget, 1, 0, 1 ); // Delete widget. |
325 $widget.remove(); |
310 $widget.remove(); |
326 return; |
311 return; |
327 } |
312 } |
328 |
313 |
329 addNew = $widget.find('input.add_new').val(); |
314 addNew = $widget.find('input.add_new').val(); |
361 .removeClass( 'closed' ) |
346 .removeClass( 'closed' ) |
362 .find( '.handlediv' ).attr( 'aria-expanded', 'true' ); |
347 .find( '.handlediv' ).attr( 'aria-expanded', 'true' ); |
363 |
348 |
364 $children = $sidebar.children('.widget'); |
349 $children = $sidebar.children('.widget'); |
365 |
350 |
366 // Make sure the dropped widget is at the top |
351 // Make sure the dropped widget is at the top. |
367 if ( $children.length > 1 ) { |
352 if ( $children.length > 1 ) { |
368 child = $children.get(0); |
353 child = $children.get(0); |
369 item = $widget.get(0); |
354 item = $widget.get(0); |
370 |
355 |
371 if ( child.id && item.id && child.id !== item.id ) { |
356 if ( child.id && item.id && child.id !== item.id ) { |
384 activate: function() { |
369 activate: function() { |
385 $(this).parent().addClass( 'widget-hover' ); |
370 $(this).parent().addClass( 'widget-hover' ); |
386 }, |
371 }, |
387 |
372 |
388 deactivate: function() { |
373 deactivate: function() { |
389 // Remove all min-height added on "start" |
374 // Remove all min-height added on "start". |
390 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' ); |
375 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' ); |
391 }, |
376 }, |
392 |
377 |
393 receive: function( event, ui ) { |
378 receive: function( event, ui ) { |
394 var $sender = $( ui.sender ); |
379 var $sender = $( ui.sender ); |
395 |
380 |
396 // Don't add more widgets to orphaned sidebars |
381 // Don't add more widgets to orphaned sidebars. |
397 if ( this.id.indexOf('orphaned_widgets') > -1 ) { |
382 if ( this.id.indexOf('orphaned_widgets') > -1 ) { |
398 $sender.sortable('cancel'); |
383 $sender.sortable('cancel'); |
399 return; |
384 return; |
400 } |
385 } |
401 |
386 |
429 $('div.widget-placeholder').show(); |
414 $('div.widget-placeholder').show(); |
430 $('#removing-widget').hide().children('span').empty(); |
415 $('#removing-widget').hide().children('span').empty(); |
431 } |
416 } |
432 }); |
417 }); |
433 |
418 |
434 // Area Chooser |
419 // Area Chooser. |
435 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) { |
420 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) { |
436 var $element = $( element ), |
421 var $element = $( element ), |
437 name = $element.find( '.sidebar-name h2' ).text(), |
422 name = $element.find( '.sidebar-name h2' ).text(), |
438 ariaLabel = $element.find( '.sidebar-name' ).data( 'add-to' ), |
423 ariaLabel = $element.find( '.sidebar-name' ).data( 'add-to' ), |
439 id = $element.find( '.widgets-sortables' ).attr( 'id' ), |
424 id = $element.find( '.widgets-sortables' ).attr( 'id' ), |
463 |
448 |
464 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) { |
449 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) { |
465 toggleButton.attr( 'aria-expanded', 'false' ); |
450 toggleButton.attr( 'aria-expanded', 'false' ); |
466 self.closeChooser(); |
451 self.closeChooser(); |
467 } else { |
452 } else { |
468 // Open the chooser |
453 // Open the chooser. |
469 self.clearWidgetSelection(); |
454 self.clearWidgetSelection(); |
470 $( '#widgets-left' ).addClass( 'chooser' ); |
455 $( '#widgets-left' ).addClass( 'chooser' ); |
471 // Add CSS class and insert the chooser after the widget description. |
456 // Add CSS class and insert the chooser after the widget description. |
472 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser ); |
457 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser ); |
473 // Open the chooser with a slide down animation. |
458 // Open the chooser with a slide down animation. |
484 .closest( 'li' ).addClass( 'widgets-chooser-selected' ); |
469 .closest( 'li' ).addClass( 'widgets-chooser-selected' ); |
485 } ); |
470 } ); |
486 } |
471 } |
487 }); |
472 }); |
488 |
473 |
489 // Add event handlers |
474 // Add event handlers. |
490 chooser.on( 'click.widgets-chooser', function( event ) { |
475 chooser.on( 'click.widgets-chooser', function( event ) { |
491 var $target = $( event.target ); |
476 var $target = $( event.target ); |
492 |
477 |
493 if ( $target.hasClass('button-primary') ) { |
478 if ( $target.hasClass('button-primary') ) { |
494 self.addWidget( chooser ); |
479 self.addWidget( chooser ); |
585 if ( r && r.length > 2 ) { |
570 if ( r && r.length > 2 ) { |
586 $( 'div.widget-content', widget ).html( r ); |
571 $( 'div.widget-content', widget ).html( r ); |
587 wpWidgets.appendTitle( widget ); |
572 wpWidgets.appendTitle( widget ); |
588 |
573 |
589 // Re-disable the save button. |
574 // Re-disable the save button. |
590 widget.find( '.widget-control-save' ).prop( 'disabled', true ).val( wpWidgets.l10n.saved ); |
575 widget.find( '.widget-control-save' ).prop( 'disabled', true ).val( wp.i18n.__( 'Saved' ) ); |
591 |
576 |
592 widget.removeClass( 'widget-dirty' ); |
577 widget.removeClass( 'widget-dirty' ); |
593 |
578 |
594 // Clear the dirty flag from the widget. |
579 // Clear the dirty flag from the widget. |
595 delete self.dirtyWidgets[ id ]; |
580 delete self.dirtyWidgets[ id ]; |
660 widget = $('#available-widgets').find('.widget-in-question').clone(); |
645 widget = $('#available-widgets').find('.widget-in-question').clone(); |
661 widgetId = widget.attr('id'); |
646 widgetId = widget.attr('id'); |
662 add = widget.find( 'input.add_new' ).val(); |
647 add = widget.find( 'input.add_new' ).val(); |
663 n = widget.find( 'input.multi_number' ).val(); |
648 n = widget.find( 'input.multi_number' ).val(); |
664 |
649 |
665 // Remove the cloned chooser from the widget |
650 // Remove the cloned chooser from the widget. |
666 widget.find('.widgets-chooser').remove(); |
651 widget.find('.widgets-chooser').remove(); |
667 |
652 |
668 if ( 'multi' === add ) { |
653 if ( 'multi' === add ) { |
669 widget.html( |
654 widget.html( |
670 widget.html().replace( /<[^<>]+>/g, function(m) { |
655 widget.html().replace( /<[^<>]+>/g, function(m) { |
687 |
672 |
688 sidebar.append( widget ); |
673 sidebar.append( widget ); |
689 sidebar.sortable('refresh'); |
674 sidebar.sortable('refresh'); |
690 |
675 |
691 wpWidgets.save( widget, 0, 0, 1 ); |
676 wpWidgets.save( widget, 0, 0, 1 ); |
692 // No longer "new" widget |
677 // No longer "new" widget. |
693 widget.find( 'input.add_new' ).val(''); |
678 widget.find( 'input.add_new' ).val(''); |
694 |
679 |
695 $document.trigger( 'widget-added', [ widget ] ); |
680 $document.trigger( 'widget-added', [ widget ] ); |
696 |
681 |
697 /* |
682 /* |
716 window.setTimeout( function() { |
701 window.setTimeout( function() { |
717 // Cannot use a callback in the animation above as it fires twice, |
702 // Cannot use a callback in the animation above as it fires twice, |
718 // have to queue this "by hand". |
703 // have to queue this "by hand". |
719 widget.find( '.widget-title' ).trigger('click'); |
704 widget.find( '.widget-title' ).trigger('click'); |
720 // At the end of the animation, announce the widget has been added. |
705 // At the end of the animation, announce the widget has been added. |
721 window.wp.a11y.speak( wpWidgets.l10n.widgetAdded, 'assertive' ); |
706 window.wp.a11y.speak( wp.i18n.__( 'Widget has been added to the selected sidebar' ), 'assertive' ); |
722 }, 250 ); |
707 }, 250 ); |
723 }, |
708 }, |
724 |
709 |
725 closeChooser: function() { |
710 closeChooser: function() { |
726 var self = this, |
711 var self = this, |
742 /** |
727 /** |
743 * Closes a Sidebar that was previously closed, but opened by dragging a Widget over it. |
728 * Closes a Sidebar that was previously closed, but opened by dragging a Widget over it. |
744 * |
729 * |
745 * Used when a Widget gets dragged in/out of the Sidebar and never dropped. |
730 * Used when a Widget gets dragged in/out of the Sidebar and never dropped. |
746 * |
731 * |
747 * @param {object} event jQuery event object. |
732 * @param {Object} event jQuery event object. |
748 */ |
733 */ |
749 closeSidebar: function( event ) { |
734 closeSidebar: function( event ) { |
750 this.hoveredSidebar |
735 this.hoveredSidebar |
751 .addClass( 'closed' ) |
736 .addClass( 'closed' ) |
752 .find( '.handlediv' ).attr( 'aria-expanded', 'false' ); |
737 .find( '.handlediv' ).attr( 'aria-expanded', 'false' ); |