|
1 /*global ajaxurl, isRtl */ |
1 var wpWidgets; |
2 var wpWidgets; |
2 (function($) { |
3 (function($) { |
3 |
4 |
4 wpWidgets = { |
5 wpWidgets = { |
5 |
6 |
6 init : function() { |
7 init : function() { |
7 var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ), |
8 var rem, the_id, |
8 margin = ( isRtl ? 'marginRight' : 'marginLeft' ), the_id; |
9 self = this, |
9 |
10 chooser = $('.widgets-chooser'), |
10 $('#widgets-right').children('.widgets-holder-wrap').children('.sidebar-name').click(function(){ |
11 selectSidebar = chooser.find('.widgets-chooser-sidebars'), |
11 var c = $(this).siblings('.widgets-sortables'), p = $(this).parent(); |
12 sidebars = $('div.widgets-sortables'), |
12 if ( !p.hasClass('closed') ) { |
13 isRTL = !! ( 'undefined' !== typeof isRtl && isRtl ); |
13 c.sortable('disable'); |
14 |
14 p.addClass('closed'); |
15 $('#widgets-right .sidebar-name').click( function() { |
|
16 var $this = $(this), |
|
17 $wrap = $this.closest('.widgets-holder-wrap'); |
|
18 |
|
19 if ( $wrap.hasClass('closed') ) { |
|
20 $wrap.removeClass('closed'); |
|
21 $this.parent().sortable('refresh'); |
15 } else { |
22 } else { |
16 p.removeClass('closed'); |
23 $wrap.addClass('closed'); |
17 c.sortable('enable').sortable('refresh'); |
24 } |
18 } |
25 }); |
19 }); |
26 |
20 |
27 $('#widgets-left .sidebar-name').click( function() { |
21 $('#widgets-left').children('.widgets-holder-wrap').children('.sidebar-name').click(function() { |
28 $(this).closest('.widgets-holder-wrap').toggleClass('closed'); |
22 $(this).parent().toggleClass('closed'); |
29 }); |
23 }); |
30 |
24 |
31 $(document.body).bind('click.widgets-toggle', function(e) { |
25 sidebars.each(function(){ |
32 var target = $(e.target), |
26 if ( $(this).parent().hasClass('inactive') ) |
33 css = { 'z-index': 100 }, |
27 return true; |
34 widget, inside, targetWidth, widgetWidth, margin; |
28 |
|
29 var h = 50, H = $(this).children('.widget').length; |
|
30 h = h + parseInt(H * 48, 10); |
|
31 $(this).css( 'minHeight', h + 'px' ); |
|
32 }); |
|
33 |
|
34 $(document.body).bind('click.widgets-toggle', function(e){ |
|
35 var target = $(e.target), css = {}, widget, inside, w; |
|
36 |
35 |
37 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
36 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
38 widget = target.closest('div.widget'); |
37 widget = target.closest('div.widget'); |
39 inside = widget.children('.widget-inside'); |
38 inside = widget.children('.widget-inside'); |
40 w = parseInt( widget.find('input.widget-width').val(), 10 ); |
39 targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ), |
|
40 widgetWidth = widget.parent().width(); |
41 |
41 |
42 if ( inside.is(':hidden') ) { |
42 if ( inside.is(':hidden') ) { |
43 if ( w > 250 && inside.closest('div.widgets-sortables').length ) { |
43 if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) { |
44 css['width'] = w + 30 + 'px'; |
44 if ( widget.closest('div.widget-liquid-right').length ) { |
45 if ( inside.closest('div.widget-liquid-right').length ) |
45 margin = isRTL ? 'margin-right' : 'margin-left'; |
46 css[margin] = 235 - w + 'px'; |
46 } else { |
47 widget.css(css); |
47 margin = isRTL ? 'margin-left' : 'margin-right'; |
|
48 } |
|
49 |
|
50 css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px'; |
|
51 widget.css( css ); |
48 } |
52 } |
49 wpWidgets.fixLabels(widget); |
53 widget.addClass( 'open' ); |
50 inside.slideDown('fast'); |
54 inside.slideDown('fast'); |
51 } else { |
55 } else { |
52 inside.slideUp('fast', function() { |
56 inside.slideUp('fast', function() { |
53 widget.css({'width':'', margin:''}); |
57 widget.attr( 'style', '' ); |
|
58 widget.removeClass( 'open' ); |
54 }); |
59 }); |
55 } |
60 } |
56 e.preventDefault(); |
61 e.preventDefault(); |
57 } else if ( target.hasClass('widget-control-save') ) { |
62 } else if ( target.hasClass('widget-control-save') ) { |
58 wpWidgets.save( target.closest('div.widget'), 0, 1, 0 ); |
63 wpWidgets.save( target.closest('div.widget'), 0, 1, 0 ); |
59 e.preventDefault(); |
64 e.preventDefault(); |
60 } else if ( target.hasClass('widget-control-remove') ) { |
65 } else if ( target.hasClass('widget-control-remove') ) { |
61 wpWidgets.save( target.closest('div.widget'), 1, 1, 0 ); |
66 wpWidgets.save( target.closest('div.widget'), 1, 1, 0 ); |
62 e.preventDefault(); |
67 e.preventDefault(); |
63 } else if ( target.hasClass('widget-control-close') ) { |
68 } else if ( target.hasClass('widget-control-close') ) { |
64 wpWidgets.close( target.closest('div.widget') ); |
69 widget = target.closest('div.widget'); |
|
70 widget.removeClass( 'open' ); |
|
71 wpWidgets.close( widget ); |
65 e.preventDefault(); |
72 e.preventDefault(); |
66 } |
73 } |
67 }); |
74 }); |
68 |
75 |
69 sidebars.children('.widget').each(function() { |
76 sidebars.children('.widget').each( function() { |
70 wpWidgets.appendTitle(this); |
77 var $this = $(this); |
71 if ( $('p.widget-error', this).length ) |
78 |
72 $('a.widget-action', this).click(); |
79 wpWidgets.appendTitle( this ); |
|
80 |
|
81 if ( $this.find( 'p.widget-error' ).length ) { |
|
82 $this.find( 'a.widget-action' ).trigger('click'); |
|
83 } |
73 }); |
84 }); |
74 |
85 |
75 $('#widget-list').children('.widget').draggable({ |
86 $('#widget-list').children('.widget').draggable({ |
76 connectToSortable: 'div.widgets-sortables', |
87 connectToSortable: 'div.widgets-sortables', |
77 handle: '> .widget-top > .widget-title', |
88 handle: '> .widget-top > .widget-title', |
78 distance: 2, |
89 distance: 2, |
79 helper: 'clone', |
90 helper: 'clone', |
80 zIndex: 100, |
91 zIndex: 100, |
81 containment: 'document', |
92 containment: 'document', |
82 start: function(e,ui) { |
93 start: function( event, ui ) { |
|
94 var chooser = $(this).find('.widgets-chooser'); |
|
95 |
83 ui.helper.find('div.widget-description').hide(); |
96 ui.helper.find('div.widget-description').hide(); |
84 the_id = this.id; |
97 the_id = this.id; |
85 }, |
98 |
86 stop: function(e,ui) { |
99 if ( chooser.length ) { |
87 if ( rem ) |
100 // Hide the chooser and move it out of the widget |
|
101 $( '#wpbody-content' ).append( chooser.hide() ); |
|
102 // Delete the cloned chooser from the drag helper |
|
103 ui.helper.find('.widgets-chooser').remove(); |
|
104 self.clearWidgetSelection(); |
|
105 } |
|
106 }, |
|
107 stop: function() { |
|
108 if ( rem ) { |
88 $(rem).hide(); |
109 $(rem).hide(); |
|
110 } |
89 |
111 |
90 rem = ''; |
112 rem = ''; |
91 } |
113 } |
92 }); |
114 }); |
93 |
115 |
96 items: '> .widget', |
118 items: '> .widget', |
97 handle: '> .widget-top > .widget-title', |
119 handle: '> .widget-top > .widget-title', |
98 cursor: 'move', |
120 cursor: 'move', |
99 distance: 2, |
121 distance: 2, |
100 containment: 'document', |
122 containment: 'document', |
101 start: function(e,ui) { |
123 start: function( event, ui ) { |
102 ui.item.children('.widget-inside').hide(); |
124 var height, $this = $(this), |
103 ui.item.css({margin:'', 'width':''}); |
125 $wrap = $this.parent(), |
104 }, |
126 inside = ui.item.children('.widget-inside'); |
105 stop: function(e,ui) { |
127 |
106 if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') ) |
128 if ( inside.css('display') === 'block' ) { |
107 ui.item.draggable('destroy'); |
129 inside.hide(); |
108 |
130 $(this).sortable('refreshPositions'); |
109 if ( ui.item.hasClass('deleting') ) { |
131 } |
110 wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget |
132 |
111 ui.item.remove(); |
133 if ( ! $wrap.hasClass('closed') ) { |
|
134 // Lock all open sidebars min-height when starting to drag. |
|
135 // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below. |
|
136 height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height(); |
|
137 $this.css( 'min-height', height + 'px' ); |
|
138 } |
|
139 }, |
|
140 |
|
141 stop: function( event, ui ) { |
|
142 var addNew, widgetNumber, $sidebar, $children, child, item, |
|
143 $widget = ui.item, |
|
144 id = the_id; |
|
145 |
|
146 if ( $widget.hasClass('deleting') ) { |
|
147 wpWidgets.save( $widget, 1, 0, 1 ); // delete widget |
|
148 $widget.remove(); |
112 return; |
149 return; |
113 } |
150 } |
114 |
151 |
115 var add = ui.item.find('input.add_new').val(), |
152 addNew = $widget.find('input.add_new').val(); |
116 n = ui.item.find('input.multi_number').val(), |
153 widgetNumber = $widget.find('input.multi_number').val(); |
117 id = the_id, |
154 |
118 sb = $(this).attr('id'); |
155 $widget.attr( 'style', '' ).removeClass('ui-draggable'); |
119 |
|
120 ui.item.css({margin:'', 'width':''}); |
|
121 the_id = ''; |
156 the_id = ''; |
122 |
157 |
123 if ( add ) { |
158 if ( addNew ) { |
124 if ( 'multi' == add ) { |
159 if ( 'multi' === addNew ) { |
125 ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) ); |
160 $widget.html( |
126 ui.item.attr( 'id', id.replace('__i__', n) ); |
161 $widget.html().replace( /<[^<>]+>/g, function( tag ) { |
127 n++; |
162 return tag.replace( /__i__|%i%/g, widgetNumber ); |
128 $('div#' + id).find('input.multi_number').val(n); |
163 }) |
129 } else if ( 'single' == add ) { |
164 ); |
130 ui.item.attr( 'id', 'new-' + id ); |
165 |
|
166 $widget.attr( 'id', id.replace( '__i__', widgetNumber ) ); |
|
167 widgetNumber++; |
|
168 |
|
169 $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber ); |
|
170 } else if ( 'single' === addNew ) { |
|
171 $widget.attr( 'id', 'new-' + id ); |
131 rem = 'div#' + id; |
172 rem = 'div#' + id; |
132 } |
173 } |
133 wpWidgets.save( ui.item, 0, 0, 1 ); |
174 |
134 ui.item.find('input.add_new').val(''); |
175 wpWidgets.save( $widget, 0, 0, 1 ); |
135 ui.item.find('a.widget-action').click(); |
176 $widget.find('input.add_new').val(''); |
|
177 $( document ).trigger( 'widget-added', [ $widget ] ); |
|
178 } |
|
179 |
|
180 $sidebar = $widget.parent(); |
|
181 |
|
182 if ( $sidebar.parent().hasClass('closed') ) { |
|
183 $sidebar.parent().removeClass('closed'); |
|
184 $children = $sidebar.children('.widget'); |
|
185 |
|
186 // Make sure the dropped widget is at the top |
|
187 if ( $children.length > 1 ) { |
|
188 child = $children.get(0); |
|
189 item = $widget.get(0); |
|
190 |
|
191 if ( child.id && item.id && child.id !== item.id ) { |
|
192 $( child ).before( $widget ); |
|
193 } |
|
194 } |
|
195 } |
|
196 |
|
197 if ( addNew ) { |
|
198 $widget.find( 'a.widget-action' ).trigger('click'); |
|
199 } else { |
|
200 wpWidgets.saveOrder( $sidebar.attr('id') ); |
|
201 } |
|
202 }, |
|
203 |
|
204 activate: function() { |
|
205 $(this).parent().addClass( 'widget-hover' ); |
|
206 }, |
|
207 |
|
208 deactivate: function() { |
|
209 // Remove all min-height added on "start" |
|
210 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' ); |
|
211 }, |
|
212 |
|
213 receive: function( event, ui ) { |
|
214 var $sender = $( ui.sender ); |
|
215 |
|
216 // Don't add more widgets to orphaned sidebars |
|
217 if ( this.id.indexOf('orphaned_widgets') > -1 ) { |
|
218 $sender.sortable('cancel'); |
136 return; |
219 return; |
137 } |
220 } |
138 wpWidgets.saveOrder(sb); |
221 |
139 }, |
222 // If the last widget was moved out of an orphaned sidebar, close and remove it. |
140 receive: function(e, ui) { |
223 if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) { |
141 var sender = $(ui.sender); |
224 $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } ); |
142 |
225 } |
143 if ( !$(this).is(':visible') || this.id.indexOf('orphaned_widgets') != -1 ) |
226 } |
144 sender.sortable('cancel'); |
227 }).sortable( 'option', 'connectWith', 'div.widgets-sortables' ); |
145 |
|
146 if ( sender.attr('id').indexOf('orphaned_widgets') != -1 && !sender.children('.widget').length ) { |
|
147 sender.parents('.orphan-sidebar').slideUp(400, function(){ $(this).remove(); }); |
|
148 } |
|
149 } |
|
150 }).sortable('option', 'connectWith', 'div.widgets-sortables').parent().filter('.closed').children('.widgets-sortables').sortable('disable'); |
|
151 |
228 |
152 $('#available-widgets').droppable({ |
229 $('#available-widgets').droppable({ |
153 tolerance: 'pointer', |
230 tolerance: 'pointer', |
154 accept: function(o){ |
231 accept: function(o){ |
155 return $(o).parent().attr('id') != 'widget-list'; |
232 return $(o).parent().attr('id') !== 'widget-list'; |
156 }, |
233 }, |
157 drop: function(e,ui) { |
234 drop: function(e,ui) { |
158 ui.draggable.addClass('deleting'); |
235 ui.draggable.addClass('deleting'); |
159 $('#removing-widget').hide().children('span').html(''); |
236 $('#removing-widget').hide().children('span').empty(); |
160 }, |
237 }, |
161 over: function(e,ui) { |
238 over: function(e,ui) { |
162 ui.draggable.addClass('deleting'); |
239 ui.draggable.addClass('deleting'); |
163 $('div.widget-placeholder').hide(); |
240 $('div.widget-placeholder').hide(); |
164 |
241 |
165 if ( ui.draggable.hasClass('ui-sortable-helper') ) |
242 if ( ui.draggable.hasClass('ui-sortable-helper') ) { |
166 $('#removing-widget').show().children('span') |
243 $('#removing-widget').show().children('span') |
167 .html( ui.draggable.find('div.widget-title').children('h4').html() ); |
244 .html( ui.draggable.find('div.widget-title').children('h4').html() ); |
|
245 } |
168 }, |
246 }, |
169 out: function(e,ui) { |
247 out: function(e,ui) { |
170 ui.draggable.removeClass('deleting'); |
248 ui.draggable.removeClass('deleting'); |
171 $('div.widget-placeholder').show(); |
249 $('div.widget-placeholder').show(); |
172 $('#removing-widget').hide().children('span').html(''); |
250 $('#removing-widget').hide().children('span').empty(); |
173 } |
251 } |
174 }); |
252 }); |
175 }, |
253 |
176 |
254 // Area Chooser |
177 saveOrder : function(sb) { |
255 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) { |
178 if ( sb ) |
256 var $element = $( element ), |
179 $('#' + sb).closest('div.widgets-holder-wrap').find('.spinner').css('display', 'inline-block'); |
257 name = $element.find( '.sidebar-name h3' ).text(), |
180 |
258 id = $element.find( '.widgets-sortables' ).attr( 'id' ), |
181 var a = { |
259 li = $('<li tabindex="0">').text( $.trim( name ) ); |
|
260 |
|
261 if ( index === 0 ) { |
|
262 li.addClass( 'widgets-chooser-selected' ); |
|
263 } |
|
264 |
|
265 selectSidebar.append( li ); |
|
266 li.data( 'sidebarId', id ); |
|
267 }); |
|
268 |
|
269 $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() { |
|
270 var $widget = $(this).closest( '.widget' ); |
|
271 |
|
272 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) { |
|
273 self.closeChooser(); |
|
274 } else { |
|
275 // Open the chooser |
|
276 self.clearWidgetSelection(); |
|
277 $( '#widgets-left' ).addClass( 'chooser' ); |
|
278 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser ); |
|
279 |
|
280 chooser.slideDown( 300, function() { |
|
281 selectSidebar.find('.widgets-chooser-selected').focus(); |
|
282 }); |
|
283 |
|
284 selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() { |
|
285 selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' ); |
|
286 $(this).addClass( 'widgets-chooser-selected' ); |
|
287 } ); |
|
288 } |
|
289 }); |
|
290 |
|
291 // Add event handlers |
|
292 chooser.on( 'click.widgets-chooser', function( event ) { |
|
293 var $target = $( event.target ); |
|
294 |
|
295 if ( $target.hasClass('button-primary') ) { |
|
296 self.addWidget( chooser ); |
|
297 self.closeChooser(); |
|
298 } else if ( $target.hasClass('button-secondary') ) { |
|
299 self.closeChooser(); |
|
300 } |
|
301 }).on( 'keyup.widgets-chooser', function( event ) { |
|
302 if ( event.which === $.ui.keyCode.ENTER ) { |
|
303 if ( $( event.target ).hasClass('button-secondary') ) { |
|
304 // Close instead of adding when pressing Enter on the Cancel button |
|
305 self.closeChooser(); |
|
306 } else { |
|
307 self.addWidget( chooser ); |
|
308 self.closeChooser(); |
|
309 } |
|
310 } else if ( event.which === $.ui.keyCode.ESCAPE ) { |
|
311 self.closeChooser(); |
|
312 } |
|
313 }); |
|
314 }, |
|
315 |
|
316 saveOrder : function( sidebarId ) { |
|
317 var data = { |
182 action: 'widgets-order', |
318 action: 'widgets-order', |
183 savewidgets: $('#_wpnonce_widgets').val(), |
319 savewidgets: $('#_wpnonce_widgets').val(), |
184 sidebars: [] |
320 sidebars: [] |
185 }; |
321 }; |
186 |
322 |
|
323 if ( sidebarId ) { |
|
324 $( '#' + sidebarId ).find( '.spinner:first' ).addClass( 'is-active' ); |
|
325 } |
|
326 |
187 $('div.widgets-sortables').each( function() { |
327 $('div.widgets-sortables').each( function() { |
188 if ( $(this).sortable ) |
328 if ( $(this).sortable ) { |
189 a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(','); |
329 data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(','); |
190 }); |
330 } |
191 |
331 }); |
192 $.post( ajaxurl, a, function() { |
332 |
193 $('.spinner').hide(); |
333 $.post( ajaxurl, data, function() { |
194 }); |
334 $( '.spinner' ).removeClass( 'is-active' ); |
195 |
335 }); |
196 this.resize(); |
336 }, |
197 }, |
337 |
198 |
338 save : function( widget, del, animate, order ) { |
199 save : function(widget, del, animate, order) { |
339 var sidebarId = widget.closest('div.widgets-sortables').attr('id'), |
200 var sb = widget.closest('div.widgets-sortables').attr('id'), data = widget.find('form').serialize(), a; |
340 data = widget.find('form').serialize(), a; |
|
341 |
201 widget = $(widget); |
342 widget = $(widget); |
202 $('.spinner', widget).show(); |
343 $( '.spinner', widget ).addClass( 'is-active' ); |
203 |
344 |
204 a = { |
345 a = { |
205 action: 'save-widget', |
346 action: 'save-widget', |
206 savewidgets: $('#_wpnonce_widgets').val(), |
347 savewidgets: $('#_wpnonce_widgets').val(), |
207 sidebar: sb |
348 sidebar: sidebarId |
208 }; |
349 }; |
209 |
350 |
210 if ( del ) |
351 if ( del ) { |
211 a['delete_widget'] = 1; |
352 a.delete_widget = 1; |
|
353 } |
212 |
354 |
213 data += '&' + $.param(a); |
355 data += '&' + $.param(a); |
214 |
356 |
215 $.post( ajaxurl, data, function(r){ |
357 $.post( ajaxurl, data, function(r) { |
216 var id; |
358 var id; |
217 |
359 |
218 if ( del ) { |
360 if ( del ) { |
219 if ( !$('input.widget_number', widget).val() ) { |
361 if ( ! $('input.widget_number', widget).val() ) { |
220 id = $('input.widget-id', widget).val(); |
362 id = $('input.widget-id', widget).val(); |
221 $('#available-widgets').find('input.widget-id').each(function(){ |
363 $('#available-widgets').find('input.widget-id').each(function(){ |
222 if ( $(this).val() == id ) |
364 if ( $(this).val() === id ) { |
223 $(this).closest('div.widget').show(); |
365 $(this).closest('div.widget').show(); |
|
366 } |
224 }); |
367 }); |
225 } |
368 } |
226 |
369 |
227 if ( animate ) { |
370 if ( animate ) { |
228 order = 0; |
371 order = 0; |
230 $(this).remove(); |
373 $(this).remove(); |
231 wpWidgets.saveOrder(); |
374 wpWidgets.saveOrder(); |
232 }); |
375 }); |
233 } else { |
376 } else { |
234 widget.remove(); |
377 widget.remove(); |
235 wpWidgets.resize(); |
|
236 } |
378 } |
237 } else { |
379 } else { |
238 $('.spinner').hide(); |
380 $( '.spinner' ).removeClass( 'is-active' ); |
239 if ( r && r.length > 2 ) { |
381 if ( r && r.length > 2 ) { |
240 $('div.widget-content', widget).html(r); |
382 $( 'div.widget-content', widget ).html( r ); |
241 wpWidgets.appendTitle(widget); |
383 wpWidgets.appendTitle( widget ); |
242 wpWidgets.fixLabels(widget); |
384 $( document ).trigger( 'widget-updated', [ widget ] ); |
243 } |
385 } |
244 } |
386 } |
245 if ( order ) |
387 if ( order ) { |
246 wpWidgets.saveOrder(); |
388 wpWidgets.saveOrder(); |
|
389 } |
247 }); |
390 }); |
248 }, |
391 }, |
249 |
392 |
250 appendTitle : function(widget) { |
393 appendTitle : function(widget) { |
251 var title = $('input[id*="-title"]', widget).val() || ''; |
394 var title = $('input[id*="-title"]', widget).val() || ''; |
252 |
395 |
253 if ( title ) |
396 if ( title ) { |
254 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>'); |
397 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>'); |
|
398 } |
255 |
399 |
256 $(widget).children('.widget-top').children('.widget-title').children() |
400 $(widget).children('.widget-top').children('.widget-title').children() |
257 .children('.in-widget-title').html(title); |
401 .children('.in-widget-title').html(title); |
258 |
402 |
259 }, |
403 }, |
260 |
404 |
261 resize : function() { |
|
262 $('div.widgets-sortables').each(function(){ |
|
263 if ( $(this).parent().hasClass('inactive') ) |
|
264 return true; |
|
265 |
|
266 var h = 50, H = $(this).children('.widget').length; |
|
267 h = h + parseInt(H * 48, 10); |
|
268 $(this).css( 'minHeight', h + 'px' ); |
|
269 }); |
|
270 }, |
|
271 |
|
272 fixLabels : function(widget) { |
|
273 widget.children('.widget-inside').find('label').each(function(){ |
|
274 var f = $(this).attr('for'); |
|
275 if ( f && f == $('input', this).attr('id') ) |
|
276 $(this).removeAttr('for'); |
|
277 }); |
|
278 }, |
|
279 |
|
280 close : function(widget) { |
405 close : function(widget) { |
281 widget.children('.widget-inside').slideUp('fast', function(){ |
406 widget.children('.widget-inside').slideUp('fast', function() { |
282 widget.css({'width':'', margin:''}); |
407 widget.attr( 'style', '' ); |
283 }); |
408 }); |
|
409 }, |
|
410 |
|
411 addWidget: function( chooser ) { |
|
412 var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds, |
|
413 sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'), |
|
414 sidebar = $( '#' + sidebarId ); |
|
415 |
|
416 widget = $('#available-widgets').find('.widget-in-question').clone(); |
|
417 widgetId = widget.attr('id'); |
|
418 add = widget.find( 'input.add_new' ).val(); |
|
419 n = widget.find( 'input.multi_number' ).val(); |
|
420 |
|
421 // Remove the cloned chooser from the widget |
|
422 widget.find('.widgets-chooser').remove(); |
|
423 |
|
424 if ( 'multi' === add ) { |
|
425 widget.html( |
|
426 widget.html().replace( /<[^<>]+>/g, function(m) { |
|
427 return m.replace( /__i__|%i%/g, n ); |
|
428 }) |
|
429 ); |
|
430 |
|
431 widget.attr( 'id', widgetId.replace( '__i__', n ) ); |
|
432 n++; |
|
433 $( '#' + widgetId ).find('input.multi_number').val(n); |
|
434 } else if ( 'single' === add ) { |
|
435 widget.attr( 'id', 'new-' + widgetId ); |
|
436 $( '#' + widgetId ).hide(); |
|
437 } |
|
438 |
|
439 // Open the widgets container |
|
440 sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed'); |
|
441 |
|
442 sidebar.append( widget ); |
|
443 sidebar.sortable('refresh'); |
|
444 |
|
445 wpWidgets.save( widget, 0, 0, 1 ); |
|
446 // No longer "new" widget |
|
447 widget.find( 'input.add_new' ).val(''); |
|
448 |
|
449 $( document ).trigger( 'widget-added', [ widget ] ); |
|
450 |
|
451 /* |
|
452 * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll. |
|
453 * Otherwise, scroll up to so the sidebar is in view. |
|
454 * |
|
455 * We do this by comparing the top and bottom, of the sidebar so see if they are within |
|
456 * the bounds of the viewport. |
|
457 */ |
|
458 viewportTop = $(window).scrollTop(); |
|
459 viewportBottom = viewportTop + $(window).height(); |
|
460 sidebarBounds = sidebar.offset(); |
|
461 |
|
462 sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight(); |
|
463 |
|
464 if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) { |
|
465 $( 'html, body' ).animate({ |
|
466 scrollTop: sidebarBounds.top - 130 |
|
467 }, 200 ); |
|
468 } |
|
469 |
|
470 window.setTimeout( function() { |
|
471 // Cannot use a callback in the animation above as it fires twice, |
|
472 // have to queue this "by hand". |
|
473 widget.find( '.widget-title' ).trigger('click'); |
|
474 }, 250 ); |
|
475 }, |
|
476 |
|
477 closeChooser: function() { |
|
478 var self = this; |
|
479 |
|
480 $( '.widgets-chooser' ).slideUp( 200, function() { |
|
481 $( '#wpbody-content' ).append( this ); |
|
482 self.clearWidgetSelection(); |
|
483 }); |
|
484 }, |
|
485 |
|
486 clearWidgetSelection: function() { |
|
487 $( '#widgets-left' ).removeClass( 'chooser' ); |
|
488 $( '.widget-in-question' ).removeClass( 'widget-in-question' ); |
284 } |
489 } |
285 }; |
490 }; |
286 |
491 |
287 $(document).ready(function($){ wpWidgets.init(); }); |
492 $(document).ready( function(){ wpWidgets.init(); } ); |
288 |
493 |
289 })(jQuery); |
494 })(jQuery); |