author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 18 | be944660c56a |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-admin/js/widgets.js |
|
3 |
*/ |
|
4 |
||
5 |
/* global ajaxurl, isRtl, wpWidgets */ |
|
6 |
||
0 | 7 |
(function($) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
var $document = $( document ); |
0 | 9 |
|
9 | 10 |
window.wpWidgets = { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* A closed Sidebar that gets a Widget dragged over it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @var {element|null} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
hoveredSidebar: null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* Lookup of which widgets have had change events triggered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* @var {object} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
dirtyWidgets: {}, |
0 | 24 |
|
25 |
init : function() { |
|
5 | 26 |
var rem, the_id, |
27 |
self = this, |
|
28 |
chooser = $('.widgets-chooser'), |
|
29 |
selectSidebar = chooser.find('.widgets-chooser-sidebars'), |
|
30 |
sidebars = $('div.widgets-sortables'), |
|
31 |
isRTL = !! ( 'undefined' !== typeof isRtl && isRtl ); |
|
0 | 32 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
// Handle the widgets containers in the right column. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
$( '#widgets-right .sidebar-name' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* Toggle the widgets containers when clicked and update the toggle |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* button `aria-expanded` attribute value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
*/ |
18 | 39 |
.on( 'click', function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
var $this = $( this ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
$wrap = $this.closest( '.widgets-holder-wrap '), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
$toggle = $this.find( '.handlediv' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
if ( $wrap.hasClass( 'closed' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
$wrap.removeClass( 'closed' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
$toggle.attr( 'aria-expanded', 'true' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
// Refresh the jQuery UI sortable items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
$this.parent().sortable( 'refresh' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
$wrap.addClass( 'closed' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
$toggle.attr( 'aria-expanded', 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
// Update the admin menu "sticky" state. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
$document.triggerHandler( 'wp-pin-menu' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
}) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* Set the initial `aria-expanded` attribute value on the widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* containers toggle button. The first one is expanded by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
.find( '.handlediv' ).each( function( index ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
if ( 0 === index ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
// jQuery equivalent of `continue` within an `each()` loop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
} |
5 | 66 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
$( this ).attr( 'aria-expanded', 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
// Show AYS dialog when there are unsaved widget changes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
$( window ).on( 'beforeunload.widgets', function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
var dirtyWidgetIds = [], unsavedWidgetsElements; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
$.each( self.dirtyWidgets, function( widgetId, dirty ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
if ( dirty ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
dirtyWidgetIds.push( widgetId ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
if ( 0 !== dirtyWidgetIds.length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
unsavedWidgetsElements = $( '#widgets-right' ).find( '.widget' ).filter( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
return -1 !== dirtyWidgetIds.indexOf( $( this ).prop( 'id' ).replace( /^widget-\d+_/, '' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
unsavedWidgetsElements.each( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
if ( ! $( this ).hasClass( 'open' ) ) { |
18 | 84 |
$( this ).find( '.widget-title-action:first' ).trigger( 'click' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
// Bring the first unsaved widget into view and focus on the first tabbable field. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
unsavedWidgetsElements.first().each( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
if ( this.scrollIntoViewIfNeeded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
this.scrollIntoViewIfNeeded(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
this.scrollIntoView(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
} |
18 | 95 |
$( this ).find( '.widget-inside :tabbable:first' ).trigger( 'focus' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
|
16 | 98 |
event.returnValue = wp.i18n.__( 'The changes you made will be lost if you navigate away from this page.' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
return event.returnValue; |
0 | 100 |
} |
101 |
}); |
|
102 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
// Handle the widgets containers in the left column. |
18 | 104 |
$( '#widgets-left .sidebar-name' ).on( 'click', function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
var $wrap = $( this ).closest( '.widgets-holder-wrap' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
$wrap |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
.toggleClass( 'closed' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
.find( '.handlediv' ).attr( 'aria-expanded', ! $wrap.hasClass( 'closed' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
// Update the admin menu "sticky" state. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
$document.triggerHandler( 'wp-pin-menu' ); |
0 | 113 |
}); |
114 |
||
18 | 115 |
$(document.body).on('click.widgets-toggle', function(e) { |
16 | 116 |
var target = $(e.target), css = {}, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
widget, inside, targetWidth, widgetWidth, margin, saveButton, widgetId, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
toggleBtn = target.closest( '.widget' ).find( '.widget-top button.widget-action' ); |
0 | 119 |
|
120 |
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
|
121 |
widget = target.closest('div.widget'); |
|
122 |
inside = widget.children('.widget-inside'); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ); |
5 | 124 |
widgetWidth = widget.parent().width(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
widgetId = inside.find( '.widget-id' ).val(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
// Save button is initially disabled, but is enabled when a field is changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
if ( ! widget.data( 'dirty-state-initialized' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
saveButton = inside.find( '.widget-control-save' ); |
16 | 130 |
saveButton.prop( 'disabled', true ).val( wp.i18n.__( 'Saved' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
inside.on( 'input change', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
self.dirtyWidgets[ widgetId ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
widget.addClass( 'widget-dirty' ); |
16 | 134 |
saveButton.prop( 'disabled', false ).val( wp.i18n.__( 'Save' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
widget.data( 'dirty-state-initialized', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
} |
0 | 138 |
|
139 |
if ( inside.is(':hidden') ) { |
|
5 | 140 |
if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) { |
141 |
if ( widget.closest('div.widget-liquid-right').length ) { |
|
142 |
margin = isRTL ? 'margin-right' : 'margin-left'; |
|
143 |
} else { |
|
144 |
margin = isRTL ? 'margin-left' : 'margin-right'; |
|
145 |
} |
|
146 |
||
147 |
css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px'; |
|
148 |
widget.css( css ); |
|
0 | 149 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* Don't change the order of attributes changes and animation: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* it's important for screen readers, see ticket #31476. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
toggleBtn.attr( 'aria-expanded', 'true' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
inside.slideDown( 'fast', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
widget.addClass( 'open' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
}); |
0 | 158 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* Don't change the order of attributes changes and animation: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* it's important for screen readers, see ticket #31476. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
toggleBtn.attr( 'aria-expanded', 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
inside.slideUp( 'fast', function() { |
5 | 165 |
widget.attr( 'style', '' ); |
166 |
widget.removeClass( 'open' ); |
|
0 | 167 |
}); |
168 |
} |
|
169 |
} else if ( target.hasClass('widget-control-save') ) { |
|
170 |
wpWidgets.save( target.closest('div.widget'), 0, 1, 0 ); |
|
171 |
e.preventDefault(); |
|
172 |
} else if ( target.hasClass('widget-control-remove') ) { |
|
173 |
wpWidgets.save( target.closest('div.widget'), 1, 1, 0 ); |
|
174 |
} else if ( target.hasClass('widget-control-close') ) { |
|
5 | 175 |
widget = target.closest('div.widget'); |
176 |
widget.removeClass( 'open' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
toggleBtn.attr( 'aria-expanded', 'false' ); |
5 | 178 |
wpWidgets.close( widget ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
} else if ( target.attr( 'id' ) === 'inactive-widgets-control-remove' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
wpWidgets.removeInactiveWidgets(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
e.preventDefault(); |
0 | 182 |
} |
183 |
}); |
|
184 |
||
5 | 185 |
sidebars.children('.widget').each( function() { |
186 |
var $this = $(this); |
|
187 |
||
188 |
wpWidgets.appendTitle( this ); |
|
189 |
||
190 |
if ( $this.find( 'p.widget-error' ).length ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
$this.find( '.widget-action' ).trigger( 'click' ).attr( 'aria-expanded', 'true' ); |
5 | 192 |
} |
0 | 193 |
}); |
194 |
||
195 |
$('#widget-list').children('.widget').draggable({ |
|
196 |
connectToSortable: 'div.widgets-sortables', |
|
197 |
handle: '> .widget-top > .widget-title', |
|
198 |
distance: 2, |
|
199 |
helper: 'clone', |
|
16 | 200 |
zIndex: 101, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
containment: '#wpwrap', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
refreshPositions: true, |
5 | 203 |
start: function( event, ui ) { |
204 |
var chooser = $(this).find('.widgets-chooser'); |
|
205 |
||
0 | 206 |
ui.helper.find('div.widget-description').hide(); |
207 |
the_id = this.id; |
|
5 | 208 |
|
209 |
if ( chooser.length ) { |
|
16 | 210 |
// Hide the chooser and move it out of the widget. |
5 | 211 |
$( '#wpbody-content' ).append( chooser.hide() ); |
16 | 212 |
// Delete the cloned chooser from the drag helper. |
5 | 213 |
ui.helper.find('.widgets-chooser').remove(); |
214 |
self.clearWidgetSelection(); |
|
215 |
} |
|
0 | 216 |
}, |
5 | 217 |
stop: function() { |
218 |
if ( rem ) { |
|
0 | 219 |
$(rem).hide(); |
5 | 220 |
} |
0 | 221 |
|
222 |
rem = ''; |
|
223 |
} |
|
224 |
}); |
|
225 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* Opens and closes previously closed Sidebars when Widgets are dragged over/out of them. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
sidebars.droppable( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
tolerance: 'intersect', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
* Open Sidebar when a Widget gets dragged over it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
* |
9 | 235 |
* @ignore |
236 |
* |
|
16 | 237 |
* @param {Object} event jQuery event object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
over: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
var $wrap = $( event.target ).parent(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
if ( wpWidgets.hoveredSidebar && ! $wrap.is( wpWidgets.hoveredSidebar ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
// Close the previous Sidebar as the Widget has been dragged onto another Sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
wpWidgets.closeSidebar( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
if ( $wrap.hasClass( 'closed' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
wpWidgets.hoveredSidebar = $wrap; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
$wrap |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
.removeClass( 'closed' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
.find( '.handlediv' ).attr( 'aria-expanded', 'true' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
$( this ).sortable( 'refresh' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
|
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 |
* Close Sidebar when the Widget gets dragged out of it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
* |
9 | 260 |
* @ignore |
261 |
* |
|
16 | 262 |
* @param {Object} event jQuery event object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
out: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
if ( wpWidgets.hoveredSidebar ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
wpWidgets.closeSidebar( event ); |
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 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
|
0 | 271 |
sidebars.sortable({ |
272 |
placeholder: 'widget-placeholder', |
|
273 |
items: '> .widget', |
|
274 |
handle: '> .widget-top > .widget-title', |
|
275 |
cursor: 'move', |
|
276 |
distance: 2, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
containment: '#wpwrap', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
tolerance: 'pointer', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
refreshPositions: true, |
5 | 280 |
start: function( event, ui ) { |
281 |
var height, $this = $(this), |
|
282 |
$wrap = $this.parent(), |
|
283 |
inside = ui.item.children('.widget-inside'); |
|
284 |
||
285 |
if ( inside.css('display') === 'block' ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
ui.item.removeClass('open'); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
ui.item.find( '.widget-top button.widget-action' ).attr( 'aria-expanded', 'false' ); |
5 | 288 |
inside.hide(); |
289 |
$(this).sortable('refreshPositions'); |
|
290 |
} |
|
291 |
||
292 |
if ( ! $wrap.hasClass('closed') ) { |
|
293 |
// Lock all open sidebars min-height when starting to drag. |
|
294 |
// Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below. |
|
295 |
height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height(); |
|
296 |
$this.css( 'min-height', height + 'px' ); |
|
297 |
} |
|
0 | 298 |
}, |
299 |
||
5 | 300 |
stop: function( event, ui ) { |
301 |
var addNew, widgetNumber, $sidebar, $children, child, item, |
|
302 |
$widget = ui.item, |
|
303 |
id = the_id; |
|
304 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
// Reset the var to hold a previously closed sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
wpWidgets.hoveredSidebar = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
|
5 | 308 |
if ( $widget.hasClass('deleting') ) { |
16 | 309 |
wpWidgets.save( $widget, 1, 0, 1 ); // Delete widget. |
5 | 310 |
$widget.remove(); |
0 | 311 |
return; |
312 |
} |
|
313 |
||
5 | 314 |
addNew = $widget.find('input.add_new').val(); |
315 |
widgetNumber = $widget.find('input.multi_number').val(); |
|
0 | 316 |
|
5 | 317 |
$widget.attr( 'style', '' ).removeClass('ui-draggable'); |
0 | 318 |
the_id = ''; |
319 |
||
5 | 320 |
if ( addNew ) { |
321 |
if ( 'multi' === addNew ) { |
|
322 |
$widget.html( |
|
323 |
$widget.html().replace( /<[^<>]+>/g, function( tag ) { |
|
324 |
return tag.replace( /__i__|%i%/g, widgetNumber ); |
|
325 |
}) |
|
326 |
); |
|
327 |
||
328 |
$widget.attr( 'id', id.replace( '__i__', widgetNumber ) ); |
|
329 |
widgetNumber++; |
|
330 |
||
331 |
$( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber ); |
|
332 |
} else if ( 'single' === addNew ) { |
|
333 |
$widget.attr( 'id', 'new-' + id ); |
|
0 | 334 |
rem = 'div#' + id; |
335 |
} |
|
5 | 336 |
|
337 |
wpWidgets.save( $widget, 0, 0, 1 ); |
|
338 |
$widget.find('input.add_new').val(''); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
$document.trigger( 'widget-added', [ $widget ] ); |
5 | 340 |
} |
341 |
||
342 |
$sidebar = $widget.parent(); |
|
343 |
||
344 |
if ( $sidebar.parent().hasClass('closed') ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
$sidebar.parent() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
.removeClass( 'closed' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
.find( '.handlediv' ).attr( 'aria-expanded', 'true' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
|
5 | 349 |
$children = $sidebar.children('.widget'); |
350 |
||
16 | 351 |
// Make sure the dropped widget is at the top. |
5 | 352 |
if ( $children.length > 1 ) { |
353 |
child = $children.get(0); |
|
354 |
item = $widget.get(0); |
|
355 |
||
356 |
if ( child.id && item.id && child.id !== item.id ) { |
|
357 |
$( child ).before( $widget ); |
|
358 |
} |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
if ( addNew ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
$widget.find( '.widget-action' ).trigger( 'click' ); |
5 | 364 |
} else { |
365 |
wpWidgets.saveOrder( $sidebar.attr('id') ); |
|
366 |
} |
|
367 |
}, |
|
368 |
||
369 |
activate: function() { |
|
370 |
$(this).parent().addClass( 'widget-hover' ); |
|
371 |
}, |
|
372 |
||
373 |
deactivate: function() { |
|
16 | 374 |
// Remove all min-height added on "start". |
5 | 375 |
$(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' ); |
376 |
}, |
|
377 |
||
378 |
receive: function( event, ui ) { |
|
379 |
var $sender = $( ui.sender ); |
|
380 |
||
16 | 381 |
// Don't add more widgets to orphaned sidebars. |
5 | 382 |
if ( this.id.indexOf('orphaned_widgets') > -1 ) { |
383 |
$sender.sortable('cancel'); |
|
0 | 384 |
return; |
385 |
} |
|
386 |
||
5 | 387 |
// If the last widget was moved out of an orphaned sidebar, close and remove it. |
388 |
if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) { |
|
389 |
$sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } ); |
|
0 | 390 |
} |
391 |
} |
|
5 | 392 |
}).sortable( 'option', 'connectWith', 'div.widgets-sortables' ); |
0 | 393 |
|
394 |
$('#available-widgets').droppable({ |
|
395 |
tolerance: 'pointer', |
|
396 |
accept: function(o){ |
|
5 | 397 |
return $(o).parent().attr('id') !== 'widget-list'; |
0 | 398 |
}, |
399 |
drop: function(e,ui) { |
|
400 |
ui.draggable.addClass('deleting'); |
|
5 | 401 |
$('#removing-widget').hide().children('span').empty(); |
0 | 402 |
}, |
403 |
over: function(e,ui) { |
|
404 |
ui.draggable.addClass('deleting'); |
|
405 |
$('div.widget-placeholder').hide(); |
|
406 |
||
5 | 407 |
if ( ui.draggable.hasClass('ui-sortable-helper') ) { |
0 | 408 |
$('#removing-widget').show().children('span') |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
.html( ui.draggable.find( 'div.widget-title' ).children( 'h3' ).html() ); |
5 | 410 |
} |
0 | 411 |
}, |
412 |
out: function(e,ui) { |
|
413 |
ui.draggable.removeClass('deleting'); |
|
414 |
$('div.widget-placeholder').show(); |
|
5 | 415 |
$('#removing-widget').hide().children('span').empty(); |
416 |
} |
|
417 |
}); |
|
418 |
||
16 | 419 |
// Area Chooser. |
5 | 420 |
$( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) { |
421 |
var $element = $( element ), |
|
18 | 422 |
name = $element.find( '.sidebar-name h2' ).text() || '', |
9 | 423 |
ariaLabel = $element.find( '.sidebar-name' ).data( 'add-to' ), |
5 | 424 |
id = $element.find( '.widgets-sortables' ).attr( 'id' ), |
9 | 425 |
li = $( '<li>' ), |
426 |
button = $( '<button>', { |
|
427 |
type: 'button', |
|
428 |
'aria-pressed': 'false', |
|
429 |
'class': 'widgets-chooser-button', |
|
430 |
'aria-label': ariaLabel |
|
18 | 431 |
} ).text( name.toString().trim() ); |
9 | 432 |
|
433 |
li.append( button ); |
|
5 | 434 |
|
435 |
if ( index === 0 ) { |
|
436 |
li.addClass( 'widgets-chooser-selected' ); |
|
9 | 437 |
button.attr( 'aria-pressed', 'true' ); |
5 | 438 |
} |
439 |
||
440 |
selectSidebar.append( li ); |
|
441 |
li.data( 'sidebarId', id ); |
|
442 |
}); |
|
443 |
||
9 | 444 |
$( '#available-widgets .widget .widget-top' ).on( 'click.widgets-chooser', function() { |
445 |
var $widget = $( this ).closest( '.widget' ), |
|
446 |
toggleButton = $( this ).find( '.widget-action' ), |
|
447 |
chooserButtons = selectSidebar.find( '.widgets-chooser-button' ); |
|
5 | 448 |
|
449 |
if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) { |
|
9 | 450 |
toggleButton.attr( 'aria-expanded', 'false' ); |
5 | 451 |
self.closeChooser(); |
452 |
} else { |
|
16 | 453 |
// Open the chooser. |
5 | 454 |
self.clearWidgetSelection(); |
455 |
$( '#widgets-left' ).addClass( 'chooser' ); |
|
9 | 456 |
// Add CSS class and insert the chooser after the widget description. |
5 | 457 |
$widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser ); |
9 | 458 |
// Open the chooser with a slide down animation. |
5 | 459 |
chooser.slideDown( 300, function() { |
9 | 460 |
// Update the toggle button aria-expanded attribute after previous DOM manipulations. |
461 |
toggleButton.attr( 'aria-expanded', 'true' ); |
|
5 | 462 |
}); |
463 |
||
9 | 464 |
chooserButtons.on( 'click.widgets-chooser', function() { |
465 |
selectSidebar.find( '.widgets-chooser-selected' ).removeClass( 'widgets-chooser-selected' ); |
|
466 |
chooserButtons.attr( 'aria-pressed', 'false' ); |
|
467 |
$( this ) |
|
468 |
.attr( 'aria-pressed', 'true' ) |
|
469 |
.closest( 'li' ).addClass( 'widgets-chooser-selected' ); |
|
5 | 470 |
} ); |
471 |
} |
|
472 |
}); |
|
473 |
||
16 | 474 |
// Add event handlers. |
5 | 475 |
chooser.on( 'click.widgets-chooser', function( event ) { |
476 |
var $target = $( event.target ); |
|
477 |
||
478 |
if ( $target.hasClass('button-primary') ) { |
|
479 |
self.addWidget( chooser ); |
|
480 |
self.closeChooser(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
} else if ( $target.hasClass( 'widgets-chooser-cancel' ) ) { |
5 | 482 |
self.closeChooser(); |
483 |
} |
|
484 |
}).on( 'keyup.widgets-chooser', function( event ) { |
|
9 | 485 |
if ( event.which === $.ui.keyCode.ESCAPE ) { |
5 | 486 |
self.closeChooser(); |
0 | 487 |
} |
488 |
}); |
|
489 |
}, |
|
490 |
||
5 | 491 |
saveOrder : function( sidebarId ) { |
492 |
var data = { |
|
0 | 493 |
action: 'widgets-order', |
494 |
savewidgets: $('#_wpnonce_widgets').val(), |
|
495 |
sidebars: [] |
|
496 |
}; |
|
497 |
||
5 | 498 |
if ( sidebarId ) { |
499 |
$( '#' + sidebarId ).find( '.spinner:first' ).addClass( 'is-active' ); |
|
500 |
} |
|
501 |
||
0 | 502 |
$('div.widgets-sortables').each( function() { |
5 | 503 |
if ( $(this).sortable ) { |
504 |
data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(','); |
|
505 |
} |
|
0 | 506 |
}); |
507 |
||
5 | 508 |
$.post( ajaxurl, data, function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length ); |
5 | 510 |
$( '.spinner' ).removeClass( 'is-active' ); |
0 | 511 |
}); |
512 |
}, |
|
513 |
||
5 | 514 |
save : function( widget, del, animate, order ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
var self = this, data, a, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
sidebarId = widget.closest( 'div.widgets-sortables' ).attr( 'id' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
form = widget.find( 'form' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
isAdd = widget.find( 'input.add_new' ).val(); |
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 |
if ( ! del && ! isAdd && form.prop( 'checkValidity' ) && ! form[0].checkValidity() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
data = form.serialize(); |
5 | 525 |
|
0 | 526 |
widget = $(widget); |
5 | 527 |
$( '.spinner', widget ).addClass( 'is-active' ); |
0 | 528 |
|
529 |
a = { |
|
530 |
action: 'save-widget', |
|
531 |
savewidgets: $('#_wpnonce_widgets').val(), |
|
5 | 532 |
sidebar: sidebarId |
0 | 533 |
}; |
534 |
||
5 | 535 |
if ( del ) { |
536 |
a.delete_widget = 1; |
|
537 |
} |
|
0 | 538 |
|
539 |
data += '&' + $.param(a); |
|
540 |
||
5 | 541 |
$.post( ajaxurl, data, function(r) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
var id = $('input.widget-id', widget).val(); |
0 | 543 |
|
544 |
if ( del ) { |
|
5 | 545 |
if ( ! $('input.widget_number', widget).val() ) { |
0 | 546 |
$('#available-widgets').find('input.widget-id').each(function(){ |
5 | 547 |
if ( $(this).val() === id ) { |
0 | 548 |
$(this).closest('div.widget').show(); |
5 | 549 |
} |
0 | 550 |
}); |
551 |
} |
|
552 |
||
553 |
if ( animate ) { |
|
554 |
order = 0; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
widget.slideUp( 'fast', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
$( this ).remove(); |
0 | 557 |
wpWidgets.saveOrder(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
delete self.dirtyWidgets[ id ]; |
0 | 559 |
}); |
560 |
} else { |
|
561 |
widget.remove(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
delete self.dirtyWidgets[ id ]; |
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 |
if ( sidebarId === 'wp_inactive_widgets' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
566 |
} |
0 | 567 |
} |
568 |
} else { |
|
5 | 569 |
$( '.spinner' ).removeClass( 'is-active' ); |
0 | 570 |
if ( r && r.length > 2 ) { |
5 | 571 |
$( 'div.widget-content', widget ).html( r ); |
572 |
wpWidgets.appendTitle( widget ); |
|
7
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 |
// Re-disable the save button. |
16 | 575 |
widget.find( '.widget-control-save' ).prop( 'disabled', true ).val( wp.i18n.__( 'Saved' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
576 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
577 |
widget.removeClass( 'widget-dirty' ); |
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 |
// Clear the dirty flag from the widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
delete self.dirtyWidgets[ id ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
582 |
$document.trigger( 'widget-updated', [ widget ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
if ( sidebarId === 'wp_inactive_widgets' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
} |
0 | 587 |
} |
588 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
|
5 | 590 |
if ( order ) { |
0 | 591 |
wpWidgets.saveOrder(); |
5 | 592 |
} |
0 | 593 |
}); |
594 |
}, |
|
595 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
removeInactiveWidgets : function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
var $element = $( '.remove-inactive-widgets' ), self = this, a, data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
598 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
599 |
$( '.spinner', $element ).addClass( 'is-active' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
600 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
a = { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
action : 'delete-inactive-widgets', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
removeinactivewidgets : $( '#_wpnonce_remove_inactive_widgets' ).val() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
605 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
data = $.param( a ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
$.post( ajaxurl, data, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
$( '#wp_inactive_widgets .widget' ).each(function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
var $widget = $( this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
delete self.dirtyWidgets[ $widget.find( 'input.widget-id' ).val() ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
$widget.remove(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
$( '#inactive-widgets-control-remove' ).prop( 'disabled', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
$( '.spinner', $element ).removeClass( 'is-active' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
|
0 | 619 |
appendTitle : function(widget) { |
620 |
var title = $('input[id*="-title"]', widget).val() || ''; |
|
621 |
||
5 | 622 |
if ( title ) { |
0 | 623 |
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>'); |
5 | 624 |
} |
0 | 625 |
|
626 |
$(widget).children('.widget-top').children('.widget-title').children() |
|
627 |
.children('.in-widget-title').html(title); |
|
628 |
||
629 |
}, |
|
630 |
||
5 | 631 |
close : function(widget) { |
632 |
widget.children('.widget-inside').slideUp('fast', function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
widget.attr( 'style', '' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
.find( '.widget-top button.widget-action' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
.attr( 'aria-expanded', 'false' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
.focus(); |
0 | 637 |
}); |
638 |
}, |
|
639 |
||
5 | 640 |
addWidget: function( chooser ) { |
641 |
var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds, |
|
642 |
sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'), |
|
643 |
sidebar = $( '#' + sidebarId ); |
|
644 |
||
645 |
widget = $('#available-widgets').find('.widget-in-question').clone(); |
|
646 |
widgetId = widget.attr('id'); |
|
647 |
add = widget.find( 'input.add_new' ).val(); |
|
648 |
n = widget.find( 'input.multi_number' ).val(); |
|
649 |
||
16 | 650 |
// Remove the cloned chooser from the widget. |
5 | 651 |
widget.find('.widgets-chooser').remove(); |
652 |
||
653 |
if ( 'multi' === add ) { |
|
654 |
widget.html( |
|
655 |
widget.html().replace( /<[^<>]+>/g, function(m) { |
|
656 |
return m.replace( /__i__|%i%/g, n ); |
|
657 |
}) |
|
658 |
); |
|
659 |
||
660 |
widget.attr( 'id', widgetId.replace( '__i__', n ) ); |
|
661 |
n++; |
|
662 |
$( '#' + widgetId ).find('input.multi_number').val(n); |
|
663 |
} else if ( 'single' === add ) { |
|
664 |
widget.attr( 'id', 'new-' + widgetId ); |
|
665 |
$( '#' + widgetId ).hide(); |
|
666 |
} |
|
667 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
// Open the widgets container. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
sidebar.closest( '.widgets-holder-wrap' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
.removeClass( 'closed' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
.find( '.handlediv' ).attr( 'aria-expanded', 'true' ); |
5 | 672 |
|
673 |
sidebar.append( widget ); |
|
674 |
sidebar.sortable('refresh'); |
|
675 |
||
676 |
wpWidgets.save( widget, 0, 0, 1 ); |
|
16 | 677 |
// No longer "new" widget. |
5 | 678 |
widget.find( 'input.add_new' ).val(''); |
679 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
$document.trigger( 'widget-added', [ widget ] ); |
5 | 681 |
|
682 |
/* |
|
683 |
* Check if any part of the sidebar is visible in the viewport. If it is, don't scroll. |
|
684 |
* Otherwise, scroll up to so the sidebar is in view. |
|
685 |
* |
|
686 |
* We do this by comparing the top and bottom, of the sidebar so see if they are within |
|
687 |
* the bounds of the viewport. |
|
688 |
*/ |
|
689 |
viewportTop = $(window).scrollTop(); |
|
690 |
viewportBottom = viewportTop + $(window).height(); |
|
691 |
sidebarBounds = sidebar.offset(); |
|
692 |
||
693 |
sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight(); |
|
694 |
||
695 |
if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) { |
|
696 |
$( 'html, body' ).animate({ |
|
697 |
scrollTop: sidebarBounds.top - 130 |
|
698 |
}, 200 ); |
|
699 |
} |
|
700 |
||
701 |
window.setTimeout( function() { |
|
702 |
// Cannot use a callback in the animation above as it fires twice, |
|
703 |
// have to queue this "by hand". |
|
704 |
widget.find( '.widget-title' ).trigger('click'); |
|
9 | 705 |
// At the end of the animation, announce the widget has been added. |
16 | 706 |
window.wp.a11y.speak( wp.i18n.__( 'Widget has been added to the selected sidebar' ), 'assertive' ); |
5 | 707 |
}, 250 ); |
708 |
}, |
|
709 |
||
710 |
closeChooser: function() { |
|
9 | 711 |
var self = this, |
712 |
widgetInQuestion = $( '#available-widgets .widget-in-question' ); |
|
5 | 713 |
|
714 |
$( '.widgets-chooser' ).slideUp( 200, function() { |
|
715 |
$( '#wpbody-content' ).append( this ); |
|
716 |
self.clearWidgetSelection(); |
|
9 | 717 |
// Move focus back to the toggle button. |
718 |
widgetInQuestion.find( '.widget-action' ).attr( 'aria-expanded', 'false' ).focus(); |
|
0 | 719 |
}); |
720 |
}, |
|
721 |
||
5 | 722 |
clearWidgetSelection: function() { |
723 |
$( '#widgets-left' ).removeClass( 'chooser' ); |
|
724 |
$( '.widget-in-question' ).removeClass( 'widget-in-question' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
* Closes a Sidebar that was previously closed, but opened by dragging a Widget over it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* Used when a Widget gets dragged in/out of the Sidebar and never dropped. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* |
16 | 732 |
* @param {Object} event jQuery event object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
closeSidebar: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
this.hoveredSidebar |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
.addClass( 'closed' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
.find( '.handlediv' ).attr( 'aria-expanded', 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
$( event.target ).css( 'min-height', '' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
this.hoveredSidebar = null; |
0 | 741 |
} |
742 |
}; |
|
743 |
||
18 | 744 |
$( function(){ wpWidgets.init(); } ); |
0 | 745 |
|
746 |
})(jQuery); |
|
16 | 747 |
|
748 |
/** |
|
749 |
* Removed in 5.5.0, needed for back-compatibility. |
|
750 |
* |
|
751 |
* @since 4.9.0 |
|
752 |
* @deprecated 5.5.0 |
|
753 |
* |
|
754 |
* @type {object} |
|
755 |
*/ |
|
756 |
wpWidgets.l10n = wpWidgets.l10n || { |
|
757 |
save: '', |
|
758 |
saved: '', |
|
759 |
saveAlert: '', |
|
760 |
widgetAdded: '' |
|
761 |
}; |
|
762 |
||
18 | 763 |
wpWidgets.l10n = window.wp.deprecateL10nObject( 'wpWidgets.l10n', wpWidgets.l10n, '5.5.0' ); |