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