author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 03 Dec 2012 16:30:36 -0800 | |
changeset 195 | c7c0fbc09788 |
parent 194 | 32102edaa81b |
permissions | -rw-r--r-- |
136 | 1 |
var wpWidgets; |
2 |
(function($) { |
|
3 |
||
4 |
wpWidgets = { |
|
5 |
||
6 |
init : function() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
7 |
var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
8 |
margin = ( isRtl ? 'marginRight' : 'marginLeft' ), the_id; |
136 | 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() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
22 |
$(this).parent().toggleClass('closed'); |
136 | 23 |
}); |
24 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
25 |
sidebars.each(function(){ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
26 |
if ( $(this).parent().hasClass('inactive') ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
27 |
return true; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
28 |
|
136 | 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 |
$('a.widget-action').live('click', function(){ |
|
35 |
var css = {}, widget = $(this).closest('div.widget'), inside = widget.children('.widget-inside'), w = parseInt( widget.find('input.widget-width').val(), 10 ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
36 |
|
136 | 37 |
if ( inside.is(':hidden') ) { |
38 |
if ( w > 250 && inside.closest('div.widgets-sortables').length ) { |
|
39 |
css['width'] = w + 30 + 'px'; |
|
40 |
if ( inside.closest('div.widget-liquid-right').length ) |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
41 |
css[margin] = 235 - w + 'px'; |
136 | 42 |
widget.css(css); |
43 |
} |
|
44 |
wpWidgets.fixLabels(widget); |
|
45 |
inside.slideDown('fast'); |
|
46 |
} else { |
|
47 |
inside.slideUp('fast', function() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
widget.css({'width':'', margin:''}); |
136 | 49 |
}); |
50 |
} |
|
51 |
return false; |
|
52 |
}); |
|
53 |
||
54 |
$('input.widget-control-save').live('click', function(){ |
|
55 |
wpWidgets.save( $(this).closest('div.widget'), 0, 1, 0 ); |
|
56 |
return false; |
|
57 |
}); |
|
58 |
||
59 |
$('a.widget-control-remove').live('click', function(){ |
|
60 |
wpWidgets.save( $(this).closest('div.widget'), 1, 1, 0 ); |
|
61 |
return false; |
|
62 |
}); |
|
63 |
||
64 |
$('a.widget-control-close').live('click', function(){ |
|
65 |
wpWidgets.close( $(this).closest('div.widget') ); |
|
66 |
return false; |
|
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: 5, |
|
81 |
containment: 'document', |
|
82 |
start: function(e,ui) { |
|
83 |
ui.helper.find('div.widget-description').hide(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
84 |
the_id = this.id; |
136 | 85 |
}, |
86 |
stop: function(e,ui) { |
|
87 |
if ( rem ) |
|
88 |
$(rem).hide(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
|
136 | 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(); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
103 |
ui.item.css({margin:'', 'width':''}); |
136 | 104 |
}, |
105 |
stop: function(e,ui) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
106 |
if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') ) |
136 | 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(), |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
117 |
id = the_id, |
136 | 118 |
sb = $(this).attr('id'); |
119 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
120 |
ui.item.css({margin:'', 'width':''}); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
121 |
the_id = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
122 |
|
136 | 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); }) ); |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
126 |
ui.item.attr( 'id', id.replace('__i__', n) ); |
136 | 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 |
}, |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
140 |
receive: function(e, ui) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
141 |
var sender = $(ui.sender); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
142 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
143 |
if ( !$(this).is(':visible') || this.id.indexOf('orphaned_widgets') != -1 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
144 |
sender.sortable('cancel'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
145 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
146 |
if ( sender.attr('id').indexOf('orphaned_widgets') != -1 && !sender.children('.widget').length ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
147 |
sender.parents('.orphan-sidebar').slideUp(400, function(){ $(this).remove(); }); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
148 |
} |
136 | 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('img.ajax-feedback').css('visibility', 'visible'); |
|
180 |
||
181 |
var a = { |
|
182 |
action: 'widgets-order', |
|
183 |
savewidgets: $('#_wpnonce_widgets').val(), |
|
184 |
sidebars: [] |
|
185 |
}; |
|
186 |
||
187 |
$('div.widgets-sortables').each( function() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
188 |
if ( $(this).sortable ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
189 |
a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(','); |
136 | 190 |
}); |
191 |
||
192 |
$.post( ajaxurl, a, function() { |
|
193 |
$('img.ajax-feedback').css('visibility', 'hidden'); |
|
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 |
$('.ajax-feedback', widget).css('visibility', 'visible'); |
|
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 |
$('.ajax-feedback').css('visibility', 'hidden'); |
|
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) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
251 |
var title = $('input[id*="-title"]', widget).val() || ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
252 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
253 |
if ( title ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
254 |
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
255 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
256 |
$(widget).children('.widget-top').children('.widget-title').children() |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
257 |
.children('.in-widget-title').html(title); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
258 |
|
136 | 259 |
}, |
260 |
||
261 |
resize : function() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
262 |
$('div.widgets-sortables').each(function(){ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
263 |
if ( $(this).parent().hasClass('inactive') ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
264 |
return true; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
|
136 | 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 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
fixLabels : function(widget) { |
136 | 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 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
280 |
close : function(widget) { |
136 | 281 |
widget.children('.widget-inside').slideUp('fast', function(){ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
282 |
widget.css({'width':'', margin:''}); |
136 | 283 |
}); |
284 |
} |
|
285 |
}; |
|
286 |
||
287 |
$(document).ready(function($){ wpWidgets.init(); }); |
|
288 |
||
289 |
})(jQuery); |