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