1 var wpWidgets;(function(a){wpWidgets={init:function(){var f,d=a("div.widgets-sortables"),c=!!("undefined"!=typeof isRtl&&isRtl),e=(isRtl?"marginRight":"marginLeft"),b;a("#widgets-right").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){var h=a(this).siblings(".widgets-sortables"),g=a(this).parent();if(!g.hasClass("closed")){h.sortable("disable");g.addClass("closed")}else{g.removeClass("closed");h.sortable("enable").sortable("refresh")}});a("#widgets-left").children(".widgets-holder-wrap").children(".sidebar-name").click(function(){a(this).parent().toggleClass("closed")});d.each(function(){if(a(this).parent().hasClass("inactive")){return true}var i=50,g=a(this).children(".widget").length;i=i+parseInt(g*48,10);a(this).css("minHeight",i+"px")});a("a.widget-action").live("click",function(){var i={},j=a(this).closest("div.widget"),g=j.children(".widget-inside"),h=parseInt(j.find("input.widget-width").val(),10);if(g.is(":hidden")){if(h>250&&g.closest("div.widgets-sortables").length){i.width=h+30+"px";if(g.closest("div.widget-liquid-right").length){i[e]=235-h+"px"}j.css(i)}wpWidgets.fixLabels(j);g.slideDown("fast")}else{g.slideUp("fast",function(){j.css({width:"",margin:""})})}return false});a("input.widget-control-save").live("click",function(){wpWidgets.save(a(this).closest("div.widget"),0,1,0);return false});a("a.widget-control-remove").live("click",function(){wpWidgets.save(a(this).closest("div.widget"),1,1,0);return false});a("a.widget-control-close").live("click",function(){wpWidgets.close(a(this).closest("div.widget"));return false});d.children(".widget").each(function(){wpWidgets.appendTitle(this);if(a("p.widget-error",this).length){a("a.widget-action",this).click()}});a("#widget-list").children(".widget").draggable({connectToSortable:"div.widgets-sortables",handle:"> .widget-top > .widget-title",distance:2,helper:"clone",zIndex:5,containment:"document",start:function(h,g){g.helper.find("div.widget-description").hide();b=this.id},stop:function(h,g){if(f){a(f).hide()}f=""}});d.sortable({placeholder:"widget-placeholder",items:"> .widget",handle:"> .widget-top > .widget-title",cursor:"move",distance:2,containment:"document",start:function(h,g){g.item.children(".widget-inside").hide();g.item.css({margin:"",width:""})},stop:function(i,g){if(g.item.hasClass("ui-draggable")&&g.item.data("draggable")){g.item.draggable("destroy")}if(g.item.hasClass("deleting")){wpWidgets.save(g.item,1,0,1);g.item.remove();return}var h=g.item.find("input.add_new").val(),l=g.item.find("input.multi_number").val(),k=b,j=a(this).attr("id");g.item.css({margin:"",width:""});b="";if(h){if("multi"==h){g.item.html(g.item.html().replace(/<[^<>]+>/g,function(n){return n.replace(/__i__|%i%/g,l)}));g.item.attr("id",k.replace("__i__",l));l++;a("div#"+k).find("input.multi_number").val(l)}else{if("single"==h){g.item.attr("id","new-"+k);f="div#"+k}}wpWidgets.save(g.item,0,0,1);g.item.find("input.add_new").val("");g.item.find("a.widget-action").click();return}wpWidgets.saveOrder(j)},receive:function(i,h){var g=a(h.sender);if(!a(this).is(":visible")||this.id.indexOf("orphaned_widgets")!=-1){g.sortable("cancel")}if(g.attr("id").indexOf("orphaned_widgets")!=-1&&!g.children(".widget").length){g.parents(".orphan-sidebar").slideUp(400,function(){a(this).remove()})}}}).sortable("option","connectWith","div.widgets-sortables").parent().filter(".closed").children(".widgets-sortables").sortable("disable");a("#available-widgets").droppable({tolerance:"pointer",accept:function(g){return a(g).parent().attr("id")!="widget-list"},drop:function(h,g){g.draggable.addClass("deleting");a("#removing-widget").hide().children("span").html("")},over:function(h,g){g.draggable.addClass("deleting");a("div.widget-placeholder").hide();if(g.draggable.hasClass("ui-sortable-helper")){a("#removing-widget").show().children("span").html(g.draggable.find("div.widget-title").children("h4").html())}},out:function(h,g){g.draggable.removeClass("deleting");a("div.widget-placeholder").show();a("#removing-widget").hide().children("span").html("")}})},saveOrder:function(c){if(c){a("#"+c).closest("div.widgets-holder-wrap").find("img.ajax-feedback").css("visibility","visible")}var b={action:"widgets-order",savewidgets:a("#_wpnonce_widgets").val(),sidebars:[]};a("div.widgets-sortables").each(function(){if(a(this).sortable){b["sidebars["+a(this).attr("id")+"]"]=a(this).sortable("toArray").join(",")}});a.post(ajaxurl,b,function(){a("img.ajax-feedback").css("visibility","hidden")});this.resize()},save:function(g,d,e,b){var h=g.closest("div.widgets-sortables").attr("id"),f=g.find("form").serialize(),c;g=a(g);a(".ajax-feedback",g).css("visibility","visible");c={action:"save-widget",savewidgets:a("#_wpnonce_widgets").val(),sidebar:h};if(d){c.delete_widget=1}f+="&"+a.param(c);a.post(ajaxurl,f,function(i){var j;if(d){if(!a("input.widget_number",g).val()){j=a("input.widget-id",g).val();a("#available-widgets").find("input.widget-id").each(function(){if(a(this).val()==j){a(this).closest("div.widget").show()}})}if(e){b=0;g.slideUp("fast",function(){a(this).remove();wpWidgets.saveOrder()})}else{g.remove();wpWidgets.resize()}}else{a(".ajax-feedback").css("visibility","hidden");if(i&&i.length>2){a("div.widget-content",g).html(i);wpWidgets.appendTitle(g);wpWidgets.fixLabels(g)}}if(b){wpWidgets.saveOrder()}})},appendTitle:function(b){var c=a('input[id*="-title"]',b).val()||"";if(c){c=": "+c.replace(/<[^<>]+>/g,"").replace(/</g,"<").replace(/>/g,">")}a(b).children(".widget-top").children(".widget-title").children().children(".in-widget-title").html(c)},resize:function(){a("div.widgets-sortables").each(function(){if(a(this).parent().hasClass("inactive")){return true}var c=50,b=a(this).children(".widget").length;c=c+parseInt(b*48,10);a(this).css("minHeight",c+"px")})},fixLabels:function(b){b.children(".widget-inside").find("label").each(function(){var c=a(this).attr("for");if(c&&c==a("input",this).attr("id")){a(this).removeAttr("for")}})},close:function(b){b.children(".widget-inside").slideUp("fast",function(){b.css({width:"",margin:""})})}};a(document).ready(function(b){wpWidgets.init()})})(jQuery); |
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); |