Another attempt to fix the http blog redirects.
[WebKit-https.git] / Websites / webkit.org / blog / wp-admin / js / postbox.js
1 /* global ajaxurl */
2
3 var postboxes;
4
5 (function($) {
6         var $document = $( document );
7
8         postboxes = {
9                 add_postbox_toggles : function(page, args) {
10                         var self = this;
11
12                         self.init(page, args);
13
14                         $('.postbox .hndle, .postbox .handlediv').bind('click.postboxes', function() {
15                                 var p = $(this).parent('.postbox'), id = p.attr('id');
16
17                                 if ( 'dashboard_browser_nag' == id )
18                                         return;
19
20                                 p.toggleClass('closed');
21
22                                 if ( page != 'press-this' )
23                                         self.save_state(page);
24
25                                 if ( id ) {
26                                         if ( !p.hasClass('closed') && $.isFunction(postboxes.pbshow) )
27                                                 self.pbshow(id);
28                                         else if ( p.hasClass('closed') && $.isFunction(postboxes.pbhide) )
29                                                 self.pbhide(id);
30                                 }
31
32                                 $document.trigger( 'postbox-toggled', p );
33                         });
34
35                         $('.postbox .hndle a').click( function(e) {
36                                 e.stopPropagation();
37                         });
38
39                         $( '.postbox a.dismiss' ).bind( 'click.postboxes', function() {
40                                 var hide_id = $(this).parents('.postbox').attr('id') + '-hide';
41                                 $( '#' + hide_id ).prop('checked', false).triggerHandler('click');
42                                 return false;
43                         });
44
45                         $('.hide-postbox-tog').bind('click.postboxes', function() {
46                                 var boxId = $(this).val(),
47                                         $postbox = $( '#' + boxId );
48
49                                 if ( $(this).prop('checked') ) {
50                                         $postbox.show();
51                                         if ( $.isFunction( postboxes.pbshow ) )
52                                                 self.pbshow( boxId );
53                                 } else {
54                                         $postbox.hide();
55                                         if ( $.isFunction( postboxes.pbhide ) )
56                                                 self.pbhide( boxId );
57                                 }
58                                 self.save_state(page);
59                                 self._mark_area();
60                                 $document.trigger( 'postbox-toggled', $postbox );
61                         });
62
63                         $('.columns-prefs input[type="radio"]').bind('click.postboxes', function(){
64                                 var n = parseInt($(this).val(), 10);
65
66                                 if ( n ) {
67                                         self._pb_edit(n);
68                                         self.save_order(page);
69                                 }
70                         });
71                 },
72
73                 init : function(page, args) {
74                         var isMobile = $(document.body).hasClass('mobile');
75
76                         $.extend( this, args || {} );
77                         $('#wpbody-content').css('overflow','hidden');
78                         $('.meta-box-sortables').sortable({
79                                 placeholder: 'sortable-placeholder',
80                                 connectWith: '.meta-box-sortables',
81                                 items: '.postbox',
82                                 handle: '.hndle',
83                                 cursor: 'move',
84                                 delay: ( isMobile ? 200 : 0 ),
85                                 distance: 2,
86                                 tolerance: 'pointer',
87                                 forcePlaceholderSize: true,
88                                 helper: 'clone',
89                                 opacity: 0.65,
90                                 stop: function() {
91                                         if ( $(this).find('#dashboard_browser_nag').is(':visible') && 'dashboard_browser_nag' != this.firstChild.id ) {
92                                                 $(this).sortable('cancel');
93                                                 return;
94                                         }
95
96                                         postboxes.save_order(page);
97                                 },
98                                 receive: function(e,ui) {
99                                         if ( 'dashboard_browser_nag' == ui.item[0].id )
100                                                 $(ui.sender).sortable('cancel');
101
102                                         postboxes._mark_area();
103                                 }
104                         });
105
106                         if ( isMobile ) {
107                                 $(document.body).bind('orientationchange.postboxes', function(){ postboxes._pb_change(); });
108                                 this._pb_change();
109                         }
110
111                         this._mark_area();
112                 },
113
114                 save_state : function(page) {
115                         var closed = $('.postbox').filter('.closed').map(function() { return this.id; }).get().join(','),
116                                 hidden = $('.postbox').filter(':hidden').map(function() { return this.id; }).get().join(',');
117
118                         $.post(ajaxurl, {
119                                 action: 'closed-postboxes',
120                                 closed: closed,
121                                 hidden: hidden,
122                                 closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(),
123                                 page: page
124                         });
125                 },
126
127                 save_order : function(page) {
128                         var postVars, page_columns = $('.columns-prefs input:checked').val() || 0;
129
130                         postVars = {
131                                 action: 'meta-box-order',
132                                 _ajax_nonce: $('#meta-box-order-nonce').val(),
133                                 page_columns: page_columns,
134                                 page: page
135                         };
136                         $('.meta-box-sortables').each( function() {
137                                 postVars[ 'order[' + this.id.split( '-' )[0] + ']' ] = $( this ).sortable( 'toArray' ).join( ',' );
138                         } );
139                         $.post( ajaxurl, postVars );
140                 },
141
142                 _mark_area : function() {
143                         var visible = $('div.postbox:visible').length, side = $('#post-body #side-sortables');
144
145                         $( '#dashboard-widgets .meta-box-sortables:visible' ).each( function() {
146                                 var t = $(this);
147
148                                 if ( visible == 1 || t.children('.postbox:visible').length )
149                                         t.removeClass('empty-container');
150                                 else
151                                         t.addClass('empty-container');
152                         });
153
154                         if ( side.length ) {
155                                 if ( side.children('.postbox:visible').length )
156                                         side.removeClass('empty-container');
157                                 else if ( $('#postbox-container-1').css('width') == '280px' )
158                                         side.addClass('empty-container');
159                         }
160                 },
161
162                 _pb_edit : function(n) {
163                         var el = $('.metabox-holder').get(0);
164
165                         if ( el ) {
166                                 el.className = el.className.replace(/columns-\d+/, 'columns-' + n);
167                         }
168
169                         $( document ).trigger( 'postboxes-columnchange' );
170                 },
171
172                 _pb_change : function() {
173                         var check = $( 'label.columns-prefs-1 input[type="radio"]' );
174
175                         switch ( window.orientation ) {
176                                 case 90:
177                                 case -90:
178                                         if ( !check.length || !check.is(':checked') )
179                                                 this._pb_edit(2);
180                                         break;
181                                 case 0:
182                                 case 180:
183                                         if ( $('#poststuff').length ) {
184                                                 this._pb_edit(1);
185                                         } else {
186                                                 if ( !check.length || !check.is(':checked') )
187                                                         this._pb_edit(2);
188                                         }
189                                         break;
190                         }
191                 },
192
193                 /* Callbacks */
194                 pbshow : false,
195
196                 pbhide : false
197         };
198
199 }(jQuery));