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