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