0
|
1 |
var wpWidgets; |
|
2 |
(function($) { |
|
3 |
|
|
4 |
wpWidgets = { |
|
5 |
|
|
6 |
init : function() { |
|
7 |
var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ), |
|
8 |
margin = ( isRtl ? 'marginRight' : 'marginLeft' ), the_id; |
|
9 |
|
|
10 |
$('#widgets-right').children('.widgets-holder-wrap').children('.sidebar-name').click(function(){ |
|
11 |
var c = $(this).siblings('.widgets-sortables'), p = $(this).parent(); |
|
12 |
if ( !p.hasClass('closed') ) { |
|
13 |
c.sortable('disable'); |
|
14 |
p.addClass('closed'); |
|
15 |
} else { |
|
16 |
p.removeClass('closed'); |
|
17 |
c.sortable('enable').sortable('refresh'); |
|
18 |
} |
|
19 |
}); |
|
20 |
|
|
21 |
$('#widgets-left').children('.widgets-holder-wrap').children('.sidebar-name').click(function() { |
|
22 |
$(this).parent().toggleClass('closed'); |
|
23 |
}); |
|
24 |
|
|
25 |
sidebars.each(function(){ |
|
26 |
if ( $(this).parent().hasClass('inactive') ) |
|
27 |
return true; |
|
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 |
|
|
37 |
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { |
|
38 |
widget = target.closest('div.widget'); |
|
39 |
inside = widget.children('.widget-inside'); |
|
40 |
w = parseInt( widget.find('input.widget-width').val(), 10 ); |
|
41 |
|
|
42 |
if ( inside.is(':hidden') ) { |
|
43 |
if ( w > 250 && inside.closest('div.widgets-sortables').length ) { |
|
44 |
css['width'] = w + 30 + 'px'; |
|
45 |
if ( inside.closest('div.widget-liquid-right').length ) |
|
46 |
css[margin] = 235 - w + 'px'; |
|
47 |
widget.css(css); |
|
48 |
} |
|
49 |
wpWidgets.fixLabels(widget); |
|
50 |
inside.slideDown('fast'); |
|
51 |
} else { |
|
52 |
inside.slideUp('fast', function() { |
|
53 |
widget.css({'width':'', margin:''}); |
|
54 |
}); |
|
55 |
} |
|
56 |
e.preventDefault(); |
|
57 |
} else if ( target.hasClass('widget-control-save') ) { |
|
58 |
wpWidgets.save( target.closest('div.widget'), 0, 1, 0 ); |
|
59 |
e.preventDefault(); |
|
60 |
} else if ( target.hasClass('widget-control-remove') ) { |
|
61 |
wpWidgets.save( target.closest('div.widget'), 1, 1, 0 ); |
|
62 |
e.preventDefault(); |
|
63 |
} else if ( target.hasClass('widget-control-close') ) { |
|
64 |
wpWidgets.close( target.closest('div.widget') ); |
|
65 |
e.preventDefault(); |
|
66 |
} |
|
67 |
}); |
|
68 |
|
|
69 |
sidebars.children('.widget').each(function() { |
|
70 |
wpWidgets.appendTitle(this); |
|
71 |
if ( $('p.widget-error', this).length ) |
|
72 |
$('a.widget-action', this).click(); |
|
73 |
}); |
|
74 |
|
|
75 |
$('#widget-list').children('.widget').draggable({ |
|
76 |
connectToSortable: 'div.widgets-sortables', |
|
77 |
handle: '> .widget-top > .widget-title', |
|
78 |
distance: 2, |
|
79 |
helper: 'clone', |
|
80 |
zIndex: 100, |
|
81 |
containment: 'document', |
|
82 |
start: function(e,ui) { |
|
83 |
ui.helper.find('div.widget-description').hide(); |
|
84 |
the_id = this.id; |
|
85 |
}, |
|
86 |
stop: function(e,ui) { |
|
87 |
if ( rem ) |
|
88 |
$(rem).hide(); |
|
89 |
|
|
90 |
rem = ''; |
|
91 |
} |
|
92 |
}); |
|
93 |
|
|
94 |
sidebars.sortable({ |
|
95 |
placeholder: 'widget-placeholder', |
|
96 |
items: '> .widget', |
|
97 |
handle: '> .widget-top > .widget-title', |
|
98 |
cursor: 'move', |
|
99 |
distance: 2, |
|
100 |
containment: 'document', |
|
101 |
start: function(e,ui) { |
|
102 |
ui.item.children('.widget-inside').hide(); |
|
103 |
ui.item.css({margin:'', 'width':''}); |
|
104 |
}, |
|
105 |
stop: function(e,ui) { |
|
106 |
if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') ) |
|
107 |
ui.item.draggable('destroy'); |
|
108 |
|
|
109 |
if ( ui.item.hasClass('deleting') ) { |
|
110 |
wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget |
|
111 |
ui.item.remove(); |
|
112 |
return; |
|
113 |
} |
|
114 |
|
|
115 |
var add = ui.item.find('input.add_new').val(), |
|
116 |
n = ui.item.find('input.multi_number').val(), |
|
117 |
id = the_id, |
|
118 |
sb = $(this).attr('id'); |
|
119 |
|
|
120 |
ui.item.css({margin:'', 'width':''}); |
|
121 |
the_id = ''; |
|
122 |
|
|
123 |
if ( add ) { |
|
124 |
if ( 'multi' == add ) { |
|
125 |
ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) ); |
|
126 |
ui.item.attr( 'id', id.replace('__i__', n) ); |
|
127 |
n++; |
|
128 |
$('div#' + id).find('input.multi_number').val(n); |
|
129 |
} else if ( 'single' == add ) { |
|
130 |
ui.item.attr( 'id', 'new-' + id ); |
|
131 |
rem = 'div#' + id; |
|
132 |
} |
|
133 |
wpWidgets.save( ui.item, 0, 0, 1 ); |
|
134 |
ui.item.find('input.add_new').val(''); |
|
135 |
ui.item.find('a.widget-action').click(); |
|
136 |
return; |
|
137 |
} |
|
138 |
wpWidgets.saveOrder(sb); |
|
139 |
}, |
|
140 |
receive: function(e, ui) { |
|
141 |
var sender = $(ui.sender); |
|
142 |
|
|
143 |
if ( !$(this).is(':visible') || this.id.indexOf('orphaned_widgets') != -1 ) |
|
144 |
sender.sortable('cancel'); |
|
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 |
|
|
152 |
$('#available-widgets').droppable({ |
|
153 |
tolerance: 'pointer', |
|
154 |
accept: function(o){ |
|
155 |
return $(o).parent().attr('id') != 'widget-list'; |
|
156 |
}, |
|
157 |
drop: function(e,ui) { |
|
158 |
ui.draggable.addClass('deleting'); |
|
159 |
$('#removing-widget').hide().children('span').html(''); |
|
160 |
}, |
|
161 |
over: function(e,ui) { |
|
162 |
ui.draggable.addClass('deleting'); |
|
163 |
$('div.widget-placeholder').hide(); |
|
164 |
|
|
165 |
if ( ui.draggable.hasClass('ui-sortable-helper') ) |
|
166 |
$('#removing-widget').show().children('span') |
|
167 |
.html( ui.draggable.find('div.widget-title').children('h4').html() ); |
|
168 |
}, |
|
169 |
out: function(e,ui) { |
|
170 |
ui.draggable.removeClass('deleting'); |
|
171 |
$('div.widget-placeholder').show(); |
|
172 |
$('#removing-widget').hide().children('span').html(''); |
|
173 |
} |
|
174 |
}); |
|
175 |
}, |
|
176 |
|
|
177 |
saveOrder : function(sb) { |
|
178 |
if ( sb ) |
|
179 |
$('#' + sb).closest('div.widgets-holder-wrap').find('.spinner').css('display', 'inline-block'); |
|
180 |
|
|
181 |
var a = { |
|
182 |
action: 'widgets-order', |
|
183 |
savewidgets: $('#_wpnonce_widgets').val(), |
|
184 |
sidebars: [] |
|
185 |
}; |
|
186 |
|
|
187 |
$('div.widgets-sortables').each( function() { |
|
188 |
if ( $(this).sortable ) |
|
189 |
a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(','); |
|
190 |
}); |
|
191 |
|
|
192 |
$.post( ajaxurl, a, function() { |
|
193 |
$('.spinner').hide(); |
|
194 |
}); |
|
195 |
|
|
196 |
this.resize(); |
|
197 |
}, |
|
198 |
|
|
199 |
save : function(widget, del, animate, order) { |
|
200 |
var sb = widget.closest('div.widgets-sortables').attr('id'), data = widget.find('form').serialize(), a; |
|
201 |
widget = $(widget); |
|
202 |
$('.spinner', widget).show(); |
|
203 |
|
|
204 |
a = { |
|
205 |
action: 'save-widget', |
|
206 |
savewidgets: $('#_wpnonce_widgets').val(), |
|
207 |
sidebar: sb |
|
208 |
}; |
|
209 |
|
|
210 |
if ( del ) |
|
211 |
a['delete_widget'] = 1; |
|
212 |
|
|
213 |
data += '&' + $.param(a); |
|
214 |
|
|
215 |
$.post( ajaxurl, data, function(r){ |
|
216 |
var id; |
|
217 |
|
|
218 |
if ( del ) { |
|
219 |
if ( !$('input.widget_number', widget).val() ) { |
|
220 |
id = $('input.widget-id', widget).val(); |
|
221 |
$('#available-widgets').find('input.widget-id').each(function(){ |
|
222 |
if ( $(this).val() == id ) |
|
223 |
$(this).closest('div.widget').show(); |
|
224 |
}); |
|
225 |
} |
|
226 |
|
|
227 |
if ( animate ) { |
|
228 |
order = 0; |
|
229 |
widget.slideUp('fast', function(){ |
|
230 |
$(this).remove(); |
|
231 |
wpWidgets.saveOrder(); |
|
232 |
}); |
|
233 |
} else { |
|
234 |
widget.remove(); |
|
235 |
wpWidgets.resize(); |
|
236 |
} |
|
237 |
} else { |
|
238 |
$('.spinner').hide(); |
|
239 |
if ( r && r.length > 2 ) { |
|
240 |
$('div.widget-content', widget).html(r); |
|
241 |
wpWidgets.appendTitle(widget); |
|
242 |
wpWidgets.fixLabels(widget); |
|
243 |
} |
|
244 |
} |
|
245 |
if ( order ) |
|
246 |
wpWidgets.saveOrder(); |
|
247 |
}); |
|
248 |
}, |
|
249 |
|
|
250 |
appendTitle : function(widget) { |
|
251 |
var title = $('input[id*="-title"]', widget).val() || ''; |
|
252 |
|
|
253 |
if ( title ) |
|
254 |
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>'); |
|
255 |
|
|
256 |
$(widget).children('.widget-top').children('.widget-title').children() |
|
257 |
.children('.in-widget-title').html(title); |
|
258 |
|
|
259 |
}, |
|
260 |
|
|
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) { |
|
281 |
widget.children('.widget-inside').slideUp('fast', function(){ |
|
282 |
widget.css({'width':'', margin:''}); |
|
283 |
}); |
|
284 |
} |
|
285 |
}; |
|
286 |
|
|
287 |
$(document).ready(function($){ wpWidgets.init(); }); |
|
288 |
|
|
289 |
})(jQuery); |