]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/list-manipulation.js
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / js / list-manipulation.js
1 addLoadEvent( function() {
2         if ( 'undefined' != typeof listManL10n )
3                 Object.extend(listMan.prototype, listManL10n);
4         theList = new listMan();
5 } );
6
7 function deleteSomething( what, id, message, obj ) {
8         if ( !obj )
9                 obj=theList;
10         if ( !message )
11                 message = obj.delText.replace(/%thing%/g, what);
12         if( confirm(message) )
13                 return obj.ajaxDelete( what, id );
14         else return false;
15 }
16
17 function dimSomething( what, id, dimClass, obj ) {
18         if ( !obj )
19                 obj = theList;
20         return obj.ajaxDimmer(what,id,dimClass);
21 }
22
23 var listMan = Class.create();
24 Object.extend(listMan.prototype, {
25         ajaxRespEl: 'ajax-response',
26         ajaxHandler: false,
27         inputData: '',
28         clearInputs: [],
29         showLink: true,
30         topAdder: false,
31         alt: 'alternate',
32         altOffset: 0,
33         addComplete: null,
34         delComplete: null,
35         dimComplete: null,
36         dataStore: null,
37         formStore: null,
38
39         jumpText: '', // We get these from listManL10n
40         delText: '',
41
42         initialize: function(theListId) {
43                 this.theList = $(theListId ? theListId : 'the-list');
44                 if ( !this.theList )
45                         return false;
46                 Element.cleanWhitespace(this.theList);
47         },
48
49         // sends add-what and fields contained in where
50         // recieves html with top element having an id like what-#
51         ajaxAdder: function( what, where, update ) { // Do NOT wrap TR in TABLE TBODY
52                 var ajaxAdd = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
53                 if ( ajaxAdd.notInitialized() )
54                         return true;
55                 var action = ( update ? 'update-' : 'add-' ) + what;
56                 ajaxAdd.options.parameters = $H(ajaxAdd.options.parameters).merge({action: action}).merge(this.inputData.toQueryParams()).merge(this.grabInputs( where, ajaxAdd ).toQueryParams());
57
58                 var tempObj=this;
59                 ajaxAdd.addOnComplete( function(transport) {
60                         var newItems = $A(transport.responseXML.getElementsByTagName(what));
61                         if ( newItems ) {
62                                 var showLinkMessage = '';
63                                 var m = '';
64                                 newItems.each( function(i) {
65                                         var id = i.getAttribute('id');
66                                         var exists = $(what+'-'+id);
67                                         if ( exists )
68                                                 tempObj.replaceListItem( exists, getNodeValue(i,'response_data'), update );
69                                         else
70                                                 tempObj.addListItem( getNodeValue(i, 'response_data') );
71                                         m = getNodeValue(i, 'show-link');
72                                         showLinkMessage += showLinkMessage ? "<br />\n" : '';
73                                         if ( m )
74                                                 showLinkMessage += m;
75                                         else
76                                                 showLinkMessage += "<a href='#" + what + '-' + id + "'>" + tempObj.jumpText + "</a>";
77                                 });
78                                 if ( tempObj.showLink && showLinkMessage )
79                                         Element.update(ajaxAdd.myResponseElement,"<div id='jumplink' class='updated fade'><p>" + showLinkMessage + "</p></div>");
80                         }
81                         if ( tempObj.addComplete && typeof tempObj.addComplete == 'function' )
82                                 tempObj.addComplete( what, where, update, transport );
83                         tempObj.recolorList();
84                         ajaxAdd.restoreInputs = null;
85                 });
86                 if ( !update )
87                         ajaxAdd.addOnWPError( function(transport) { tempObj.restoreForm(ajaxAdd.restoreInputs); });
88                 ajaxAdd.request(ajaxAdd.url);
89                 if ( !update )
90                         this.clear();
91                 return false;
92         },
93
94         // sends update-what and fields contained in where
95         // recieves html with top element having an id like what-#
96         ajaxUpdater: function( what, where ) { return this.ajaxAdder( what, where, true ); },
97
98         // sends delete-what and id#
99         ajaxDelete: function( what, id ) {
100                 var ajaxDel = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
101                 if( ajaxDel.notInitialized() )
102                         return true;
103                 var tempObj = this;
104                 var action = 'delete-' + what;
105                 var actionId = action + '&id=' + id;
106                 var idName = what.replace('-as-spam','') + '-' + id;
107                 ajaxDel.addOnComplete( function(transport) {
108                         Element.update(ajaxDel.myResponseElement,'');
109                         tempObj.destore(actionId);
110                         if( tempObj.delComplete && typeof tempObj.delComplete == 'function' )
111                                 tempObj.delComplete( what, id, transport );
112                 });
113                 ajaxDel.addOnWPError( function(transport) { tempObj.restore(actionId, true); });
114                 ajaxDel.options.parameters = $H(ajaxDel.options.parameters).merge({action: action, id: id}).merge(this.inputData.toQueryParams());
115                 ajaxDel.request(ajaxDel.url);
116                 this.store(actionId, idName);
117                 tempObj.removeListItem( idName );
118                 return false;
119         },
120
121         // Toggles class nomes
122         // sends dim-what and id#
123         ajaxDimmer: function( what, id, dimClass ) {
124                 ajaxDim = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
125                 if ( ajaxDim.notInitialized() )
126                         return true;
127                 var tempObj = this;
128                 var action = 'dim-' + what;
129                 var actionId = action + '&id=' + id;
130                 var idName = what + '-' + id;
131                 ajaxDim.addOnComplete( function(transport) {
132                         Element.update(ajaxDim.myResponseElement,'');
133                         tempObj.destore(actionId);
134                         if ( tempObj.dimComplete && typeof tempObj.dimComplete == 'function' )
135                                 tempObj.dimComplete( what, id, dimClass, transport );
136                 });
137                 ajaxDim.addOnWPError( function(transport) { tempObj.restore(actionId, true); });
138                 ajaxDim.options.parameters = $H(ajaxDim.options.parameters).merge({action: action, id: id}).merge(this.inputData.toQueryParams());
139                 ajaxDim.request(ajaxDim.url);
140                 this.store(actionId, idName);
141                 this.dimItem( idName, dimClass );
142                 return false;
143         },
144
145         addListItem: function( h ) {
146                 new Insertion[this.topAdder ? 'Top' : 'Bottom'](this.theList,h);
147                 Element.cleanWhitespace(this.theList);
148                 var id = this.topAdder ? this.theList.firstChild.id : this.theList.lastChild.id;
149                 if ( this.alt )
150                         if ( ( this.theList.childNodes.length + this.altOffset ) % 2 )
151                                 Element.addClassName($(id),this.alt);
152                 Fat.fade_element(id);
153         },
154
155         // only hides the element sa it can be put back again if necessary
156         removeListItem: function( id, noFade ) {
157                 id = $(id);
158                 if ( !noFade ) {
159                         Fat.fade_element(id.id,null,700,'#FF3333');
160                         var tempObj = this;
161                         var func = function() { id.hide(); tempObj.recolorList(); }
162                         setTimeout(func, 705);
163                 } else {
164                         id.hide();
165                         this.recolorList();
166                 }
167         },
168
169         replaceListItem: function( id, h, update ) {
170                 id = $(id);
171                 if ( !update ) {
172                         Element.remove(id);
173                         this.addListItem( h );
174                         return;
175                 }
176                 id.replace(h);
177                 Fat.fade_element(id.id);
178         },
179
180         // toggles class
181         dimItem: function( id, dimClass, noFade ) {
182                 id = $(id);
183                 if ( Element.hasClassName(id,dimClass) ) {
184                         if ( !noFade )
185                                 Fat.fade_element(id.id,null,700,null);
186                         Element.removeClassName(id,dimClass);
187                 } else {
188                         if ( !noFade )
189                                 Fat.fade_element(id.id,null,700,'#FF3333');
190                         Element.addClassName(id,dimClass);
191                 }
192         },
193
194         // store an element in case we need it later
195         store: function(action, id) {
196                 if ( !this.dataStore )
197                         this.dataStore = $H();
198                 this.dataStore[action] = $(id).cloneNode(true);
199         },
200
201         // delete from store
202         destore: function(action) { delete(this.dataStore[action]); },
203
204         // restore element from store into existing (possibly hidden) element of same id
205         restore: function(action, error) {
206                 var id = this.dataStore[action].id;
207                 this.theList.replaceChild(this.dataStore[action], $(id));
208                 delete(this.dataStore[action]);
209                 if ( error ) {
210                         func = function() { Element.setStyle($(id),{backgroundColor:'#FF3333'}); }
211                         func(); setTimeout(func, 705); // Hit it twice in case it's still fading.
212                 }
213         },
214
215         // Like Form.serialize, but excludes action and sets up clearInputs
216         grabInputs: function( where, ajaxObj ) {
217                 if ( ajaxObj )
218                         ajaxObj.restoreInputs = [];
219                 var elements = Form.getElements($(where));
220                 var queryComponents = new Array();
221                 for (var i = 0; i < elements.length; i++) {
222                         if ( 'action' == elements[i].name )
223                                 continue;
224                         if ( 'hidden' != elements[i].type && 'submit' != elements[i].type && 'button' != elements[i].type ) {
225                                 this.clearInputs.push(elements[i]);
226                                 if ( ajaxObj )
227                                         ajaxObj.restoreInputs.push([elements[i], elements[i].value]);
228                         }
229                         var queryComponent = Form.Element.serialize(elements[i]);
230                         if (queryComponent) {
231                                 queryComponents.push(queryComponent);
232                         }
233                 }
234                 return queryComponents.join('&');
235         },
236
237         // form.reset() can only do whole forms.  This can do subsections.
238         clear: function() {
239                 this.clearInputs.each( function(i) {
240                         i = $(i);
241                         if ( 'textarea' == i.tagName.toLowerCase() )
242                                 i.value = '';
243                         else
244                                 switch ( i.type.toLowerCase() ) {
245                                         case 'password': case 'text':
246                                                 i.value = '';
247                                                 break;
248                                         case 'checkbox': case 'radio':
249                                                 i.checked = false;
250                                                 break;
251                                         case 'select': case 'select-one':
252                                                 i.selectedIndex = null;
253                                                 break;
254                                         case 'select-multiple':
255                                                 for (var o = 0; o < i.length; o++) i.options[o].selected = false;
256                                                 break;
257                                 }
258                 });
259                 this.clearInputs = [];
260         },
261
262         restoreForm: function(elements) {
263                 elements.each( function(i) {
264                         i[0].value = i[1];
265                 });
266         },
267
268         recolorList: function() {
269                 if ( !this.alt )
270                         return;
271                 var alt = this.alt;
272                 var offset = this.altOffset;
273                 var listItems = $A(this.theList.childNodes).findAll( function(i) { return Element.visible(i) } );
274                 listItems.each( function(i,n) {
275                         if ( ( n + offset ) % 2 )
276                                 Element.removeClassName(i,alt);
277                         else
278                                 Element.addClassName(i,alt);
279                 });
280         }
281 });
282
283 //No submit unless code returns true.
284 function killSubmit ( code, e ) {
285         e = e ? e : window.event;
286         if ( !e ) return;
287         var t = e.target ? e.target : e.srcElement;
288         if ( ( 'text' == t.type && e.keyCode == 13 ) || ( 'submit' == t.type && 'click' == e.type ) ) {
289                 if ( ( 'string' == typeof code && !eval(code) ) || ( 'function' == typeof code && !code() ) ) {
290                         e.returnValue = false; e.cancelBubble = true; return false;
291                 }
292         }
293 }
294 //Generic but lame JS closure
295 function encloseFunc(f){var a=arguments[1];return function(){return f(a);}}