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