5
|
1 |
/* global ajaxurl */ |
|
2 |
|
0
|
3 |
var postboxes; |
|
4 |
|
|
5 |
(function($) { |
5
|
6 |
var $document = $( document ); |
|
7 |
|
0
|
8 |
postboxes = { |
|
9 |
add_postbox_toggles : function(page, args) { |
|
10 |
var self = this; |
|
11 |
|
|
12 |
self.init(page, args); |
|
13 |
|
5
|
14 |
$('.postbox .hndle, .postbox .handlediv').bind('click.postboxes', function() { |
0
|
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 |
} |
5
|
31 |
|
|
32 |
$document.trigger( 'postbox-toggled', p ); |
0
|
33 |
}); |
|
34 |
|
5
|
35 |
$('.postbox .hndle a').click( function(e) { |
0
|
36 |
e.stopPropagation(); |
|
37 |
}); |
|
38 |
|
5
|
39 |
$( '.postbox a.dismiss' ).bind( 'click.postboxes', function() { |
0
|
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() { |
5
|
46 |
var boxId = $(this).val(), |
|
47 |
$postbox = $( '#' + boxId ); |
0
|
48 |
|
|
49 |
if ( $(this).prop('checked') ) { |
5
|
50 |
$postbox.show(); |
0
|
51 |
if ( $.isFunction( postboxes.pbshow ) ) |
5
|
52 |
self.pbshow( boxId ); |
0
|
53 |
} else { |
5
|
54 |
$postbox.hide(); |
0
|
55 |
if ( $.isFunction( postboxes.pbhide ) ) |
5
|
56 |
self.pbhide( boxId ); |
0
|
57 |
} |
|
58 |
self.save_state(page); |
|
59 |
self._mark_area(); |
5
|
60 |
$document.trigger( 'postbox-toggled', $postbox ); |
0
|
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, |
5
|
90 |
stop: function() { |
0
|
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 |
5
|
135 |
}; |
0
|
136 |
$('.meta-box-sortables').each( function() { |
5
|
137 |
postVars[ 'order[' + this.id.split( '-' )[0] + ']' ] = $( this ).sortable( 'toArray' ).join( ',' ); |
0
|
138 |
} ); |
|
139 |
$.post( ajaxurl, postVars ); |
|
140 |
}, |
|
141 |
|
|
142 |
_mark_area : function() { |
|
143 |
var visible = $('div.postbox:visible').length, side = $('#post-body #side-sortables'); |
|
144 |
|
5
|
145 |
$( '#dashboard-widgets .meta-box-sortables:visible' ).each( function() { |
0
|
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); |
5
|
164 |
|
|
165 |
if ( el ) { |
|
166 |
el.className = el.className.replace(/columns-\d+/, 'columns-' + n); |
|
167 |
} |
|
168 |
|
|
169 |
$( document ).trigger( 'postboxes-columnchange' ); |
0
|
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)); |