]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-lists.js
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / wp-lists.js
1 (function($) {
2 var currentFormEl = false;
3 var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'};
4
5 var wpList = {
6         settings: {
7                 url: wpListL10n.url, type: 'POST',
8                 response: 'ajax-response',
9
10                 what: '',
11                 alt: 'alternate', altOffset: 0,
12                 addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
13
14                 confirm: null,
15                 addBefore: null, addAfter: null,
16                 delBefore: null, delAfter: null,
17                 dimBefore: null, dimAfter: null
18         },
19
20         nonce: function(e,s) {
21                 var url = wpAjax.unserialize(e.attr('href'));
22                 return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name=_ajax_nonce]').val() || url._wpnonce || $('#' + s.element + ' input[name=_wpnonce]').val() || 0;
23         },
24
25         parseClass: function(e,t) {
26                 var c = [], cl;
27                 try {
28                         cl = $(e).attr('class') || '';
29                         cl = cl.match(new RegExp(t+':[A-Za-z0-9:_=-]+'));
30                         if ( cl ) { c = cl[0].split(':'); }
31                 } catch(r) {}
32                 return c;
33         },
34
35         pre: function(e,s,a) {
36                 var bg; var r;
37                 s = $.extend( {}, this.wpList.settings, {
38                         element: null,
39                         nonce: 0,
40                         target: e.get(0)
41                 }, s || {} );
42                 if ( $.isFunction( s.confirm ) ) {
43                         if ( 'add' != a ) {
44                                 bg = $('#' + s.element).css('backgroundColor');
45                                 $('#' + s.element).css('backgroundColor', '#FF9966');
46                         }
47                         r = s.confirm.call(this,e,s,a,bg);
48                         if ( 'add' != a ) { $('#' + s.element).css('backgroundColor', bg ); }
49                         if ( !r ) { return false; }
50                 }
51                 return s;
52         },
53
54         ajaxAdd: function( e, s ) {
55                 var list = this; e = $(e); s = s || {};
56                 var cls = wpList.parseClass(e,'add');
57                 s = wpList.pre.call( list, e, s, 'add' );
58
59                 s.element = cls[2] || e.attr( 'id' ) || s.element || null;
60                 if ( cls[3] ) { s.addColor = '#' + cls[3]; }
61                 else { s.addColor = s.addColor || '#FFFF33'; }
62
63                 if ( !s ) { return false; }
64
65                 if ( !e.is("[class^=add:" + list.id + ":]") ) { return !wpList.add.call( list, e, s ); }
66
67                 if ( !s.element ) { return true; }
68
69                 s.action = 'add-' + s.what;
70
71                 s.nonce = wpList.nonce(e,s);
72
73                 var es = $('#' + s.element + ' :input').not('[name=_ajax_nonce], [name=_wpnonce], [name=action]');
74                 var valid = wpAjax.validateForm( '#' + s.element );
75                 if ( !valid ) { return false; }
76
77                 s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( cls[4] || '' ) ) );
78                 var formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
79                 if ( formData ) { s.data += '&' + formData; }
80
81                 if ( $.isFunction(s.addBefore) ) {
82                         s = s.addBefore( s );
83                         if ( !s ) { return true; }
84                 }
85                 if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) ) { return true; }
86
87                 s.success = function(r) {
88                         var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
89                         if ( !res || res.errors ) { return false; }
90
91                         jQuery.each( res.responses, function() {
92                                 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
93                                         pos: this.position || 0,
94                                         id: this.id || 0,
95                                         oldId: this.oldId || null
96                                 } ) );
97                         } );
98
99                         if ( $.isFunction(s.addAfter) ) {
100                                 var o = this.complete;
101                                 this.complete = function(x,st) {
102                                         var _s = $.extend( { xml: x, status: st, parsed: res }, s );
103                                         s.addAfter( r, _s );
104                                         if ( $.isFunction(o) ) { o(x,st); }
105                                 };
106                         }
107                         list.wpList.recolor();
108                         wpList.clear.call(list,'#' + s.element);
109                 };
110
111                 $.ajax( s );
112                 return false;
113         },
114
115         ajaxDel: function( e, s ) {
116                 var list = this; e = $(e); s = s || {};
117                 var cls = wpList.parseClass(e,'delete');
118                 s = wpList.pre.call( list, e, s, 'delete' );
119
120                 s.element = cls[2] || s.element || null;
121                 if ( cls[3] ) { s.delColor = '#' + cls[3]; }
122                 else { s.delColor = s.delColor || '#FF3333'; }
123
124                 if ( !s || !s.element ) { return false; }
125
126                 s.action = 'delete-' + s.what;
127
128                 s.nonce = wpList.nonce(e,s);
129
130                 s.data = $.extend(
131                         { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
132                         wpAjax.unserialize( cls[4] || '' )
133                 );
134
135                 if ( $.isFunction(s.delBefore) ) {
136                         s = s.delBefore( s );
137                         if ( !s ) { return true; }
138                 }
139                 if ( !s.data._ajax_nonce ) { return true; }
140
141                 var element = $('#' + s.element);
142
143                 if ( 'none' != s.delColor ) {
144                         var anim = 'slideUp';
145                         if ( element.css( 'display' ).match(/table/) )
146                                 anim = 'fadeOut'; // Can't slideup table rows and other table elements.  Known jQuery bug
147                         element
148                                 .animate( { backgroundColor: s.delColor }, 'fast'  )[anim]( 'fast' )
149                                 .queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
150                 } else {
151                         list.wpList.recolor();
152                 }
153
154                 s.success = function(r) {
155                         var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
156                         if ( !res || res.errors ) {
157                                 element.stop().stop().css( 'backgroundColor', '#FF3333' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
158                                 return false;
159                         }
160                         if ( $.isFunction(s.delAfter) ) {
161                                 var o = this.complete;
162                                 this.complete = function(x,st) {
163                                         element.queue( function() {
164                                                 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
165                                                 s.delAfter( r, _s );
166                                                 if ( $.isFunction(o) ) { o(x,st); }
167                                         } ).dequeue();
168                                 };
169                         }
170                 };
171                 $.ajax( s );
172                 return false;
173         },
174
175         ajaxDim: function( e, s ) {
176                 var list = this; e = $(e); s = s || {};
177                 var cls = wpList.parseClass(e,'dim');
178                 s = wpList.pre.call( list, e, s, 'dim' );
179
180                 s.element = cls[2] || s.element || null;
181                 s.dimClass =  cls[3] || s.dimClass || null;
182                 if ( cls[4] ) { s.dimAddColor = '#' + cls[4]; }
183                 else { s.dimAddColor = s.dimAddColor || '#FFFF33'; }
184                 if ( cls[5] ) { s.dimDelColor = '#' + cls[5]; }
185                 else { s.dimDelColor = s.dimDelColor || '#FF3333'; }
186
187                 if ( !s || !s.element || !s.dimClass ) { return true; }
188
189                 s.action = 'dim-' + s.what;
190
191                 s.nonce = wpList.nonce(e,s);
192
193                 s.data = $.extend(
194                         { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
195                         wpAjax.unserialize( cls[6] || '' )
196                 );
197
198                 if ( $.isFunction(s.dimBefore) ) {
199                         s = s.dimBefore( s );
200                         if ( !s ) { return true; }
201                 }
202
203                 var element = $('#' + s.element);
204                 var isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
205                 var color = wpList.getColor( element );
206                 element.toggleClass( s.dimClass )
207                 var dimColor = isClass ? s.dimAddColor : s.dimDelColor;
208                 if ( 'none' != dimColor ) {
209                         element
210                                 .animate( { backgroundColor: dimColor }, 'fast' )
211                                 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
212                                 .animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
213                 }
214
215                 if ( !s.data._ajax_nonce ) { return true; }
216
217                 s.success = function(r) {
218                         var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
219                         if ( !res || res.errors ) {
220                                 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
221                                 return false;
222                         }
223                         if ( $.isFunction(s.dimAfter) ) {
224                                 var o = this.complete;
225                                 this.complete = function(x,st) {
226                                         element.queue( function() {
227                                                 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
228                                                 s.dimAfter( r, _s );
229                                                 if ( $.isFunction(o) ) { o(x,st); }
230                                         } ).dequeue();
231                                 };
232                         }
233                 };
234
235                 $.ajax( s );
236                 return false;
237         },
238
239         // From jquery.color.js: jQuery Color Animation by John Resig
240         getColor: function( el ) {
241                 if ( el.constructor == Object )
242                         el = el.get(0);
243                 var elem = el, color, rgbaTrans = new RegExp( "rgba\\(\\s*0,\\s*0,\\s*0,\\s*0\\s*\\)", "i" );
244                 do {
245                         color = jQuery.curCSS(elem, 'backgroundColor');
246                         if ( color != '' && color != 'transparent' && !color.match(rgbaTrans) || jQuery.nodeName(elem, "body") )
247                                 break;
248                 } while ( elem = elem.parentNode );
249                 return color || '#ffffff';
250         },
251
252         add: function( e, s ) {
253                 var list = $(this);
254                 e = $(e);
255
256                 var old = false;
257                 var _s = { pos: 0, id: 0, oldId: null };
258                 if ( 'string' == typeof s ) { s = { what: s }; }
259                 s = $.extend(_s, this.wpList.settings, s);
260                 if ( !e.size() || !s.what ) { return false; }
261                 if ( s.oldId ) { old = $('#' + s.what + '-' + s.oldId); }
262                 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) ) { $('#' + s.what + '-' + s.id).remove(); }
263
264                 if ( old && old.size() ) {
265                         old.replaceWith(e);
266                 } else if ( isNaN(s.pos) ) {
267                         var ba = 'after';
268                         if ( '-' == s.pos.substr(0,1) ) {
269                                 s.pos = s.pos.substr(1);
270                                 ba = 'before';
271                         }
272                         var ref = list.find( '#' + s.pos );
273                         if ( 1 === ref.size() ) { ref[ba](e); }
274                         else { list.append(e); }
275                 } else if ( s.pos < 0 ) {
276                         list.prepend(e);
277                 } else {
278                         list.append(e);
279                 }
280
281                 if ( s.alt ) {
282                         if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
283                         else { e.addClass( s.alt ); }
284                 }
285
286                 if ( 'none' != s.addColor ) {
287                         var color = wpList.getColor( e );
288                         e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
289                 }
290                 list.each( function() { this.wpList.process( e ); } );
291                 return e;
292         },
293
294         clear: function(e) {
295                 var list = this;
296                 e = $(e);
297                 if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; }
298                 e.find(':input').each( function() {
299                         if ( $(this).parents('.form-no-clear').size() )
300                                 return;
301                         var t = this.type.toLowerCase(); var tag = this.tagName.toLowerCase();
302                         if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; }
303                         else if ( 'checkbox' == t || 'radio' == t ) { this.checked = false; }
304                         else if ( 'select' == tag ) { this.selectedIndex = null; }
305                 });
306         },
307
308         process: function(el) {
309                 var list = this;
310                 $("[class^=add:" + list.id + ":]", el || null)
311                         .filter('form').submit( function() { return list.wpList.add(this); } ).end()
312                         .not('form').click( function() { return list.wpList.add(this); } ).each( function() {
313                                 var addEl = this;
314                                 var c = wpList.parseClass(this,'add')[2] || addEl.id;
315                                 if ( !c ) { return; }
316                                 var forms = []; var ins = []; // this is all really inefficient
317                                 $('#' + c + ' :input').focus( function() { currentFormEl = this; } ).blur( function() { currentFormEl = false; } ).each( function() {
318                                         ins.push(this);
319                                         var f = $(this).parents('form:first').get(0);
320                                         if ( $.inArray(f,forms) < 0 ) { forms.push(f); }
321                                 } );
322                                 $(forms).submit( function() {
323                                         if ( 0 <= $.inArray(currentFormEl,ins) ) {
324                                                 $(addEl).trigger( 'click' );
325                                                 $(currentFormEl).focus();
326                                                 return false;
327                                         }
328                                 } );
329                         } );
330                 $("[class^=delete:" + list.id + ":]", el || null).click( function() { return list.wpList.del(this); } );
331                 $("[class^=dim:" + list.id + ":]", el || null).click( function() { return list.wpList.dim(this); } );
332         },
333
334         recolor: function() {
335                 var list = this;
336                 if ( !list.wpList.settings.alt ) { return; }
337                 var items = $('.list-item:visible', list);
338                 if ( !items.size() ) { items = $(list).children(':visible'); }
339                 var eo = [':even',':odd'];
340                 if ( list.wpList.settings.altOffset % 2 ) { eo.reverse(); }
341                 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
342         },
343
344         init: function() {
345                 var lists = this;
346                 lists.wpList.process = function(a) {
347                         lists.each( function() {
348                                 this.wpList.process(a);
349                         } );
350                 };
351                 lists.wpList.recolor = function() {
352                         lists.each( function() {
353                                 this.wpList.recolor();
354                         } );
355                 };
356         }
357 };
358
359 $.fn.wpList = function( settings ) {
360         this.each( function() {
361                 var _this = this;
362                 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
363                 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
364         } );
365         wpList.init.call(this);
366         this.wpList.process();
367         return this;
368 };
369
370 })(jQuery);