1 addLoadEvent( function() {
2 if ( 'undefined' != typeof listManL10n )
3 Object.extend(listMan.prototype, listManL10n);
4 theList = new listMan();
7 function deleteSomething( what, id, message, obj ) {
11 message = obj.delText.replace(/%thing%/g, what);
12 if( confirm(message) )
13 return obj.ajaxDelete( what, id );
17 function dimSomething( what, id, dimClass, obj ) {
20 return obj.ajaxDimmer(what,id,dimClass);
23 var listMan = Class.create();
24 Object.extend(listMan.prototype, {
25 ajaxRespEl: 'ajax-response',
39 jumpText: '', // We get these from listManL10n
42 initialize: function(theListId) {
43 this.theList = $(theListId ? theListId : 'the-list');
46 Element.cleanWhitespace(this.theList);
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() )
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());
59 ajaxAdd.addOnComplete( function(transport) {
60 var newItems = $A(transport.responseXML.getElementsByTagName(what));
62 var showLinkMessage = '';
64 newItems.each( function(i) {
65 var id = i.getAttribute('id');
66 var exists = $(what+'-'+id);
68 tempObj.replaceListItem( exists, getNodeValue(i,'response_data'), update );
70 tempObj.addListItem( getNodeValue(i, 'response_data') );
71 m = getNodeValue(i, 'show-link');
72 showLinkMessage += showLinkMessage ? "<br />\n" : '';
76 showLinkMessage += "<a href='#" + what + '-' + id + "'>" + tempObj.jumpText + "</a>";
78 if ( tempObj.showLink && showLinkMessage )
79 Element.update(ajaxAdd.myResponseElement,"<div id='jumplink' class='updated fade'><p>" + showLinkMessage + "</p></div>");
81 if ( tempObj.addComplete && typeof tempObj.addComplete == 'function' )
82 tempObj.addComplete( what, where, update, transport );
83 tempObj.recolorList();
84 ajaxAdd.restoreInputs = null;
87 ajaxAdd.addOnWPError( function(transport) { tempObj.restoreForm(ajaxAdd.restoreInputs); });
88 ajaxAdd.request(ajaxAdd.url);
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 ); },
98 // sends delete-what and id#
99 ajaxDelete: function( what, id ) {
100 var ajaxDel = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
101 if( ajaxDel.notInitialized() )
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 );
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 );
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() )
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 );
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 );
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;
150 if ( ( this.theList.childNodes.length + this.altOffset ) % 2 )
151 Element.addClassName($(id),this.alt);
152 Fat.fade_element(id);
155 // only hides the element sa it can be put back again if necessary
156 removeListItem: function( id, noFade ) {
159 Fat.fade_element(id.id,null,700,'#FF3333');
161 var func = function() { id.hide(); tempObj.recolorList(); }
162 setTimeout(func, 705);
169 replaceListItem: function( id, h, update ) {
173 this.addListItem( h );
177 Fat.fade_element(id.id);
181 dimItem: function( id, dimClass, noFade ) {
183 if ( Element.hasClassName(id,dimClass) ) {
185 Fat.fade_element(id.id,null,700,null);
186 Element.removeClassName(id,dimClass);
189 Fat.fade_element(id.id,null,700,'#FF3333');
190 Element.addClassName(id,dimClass);
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);
202 destore: function(action) { delete(this.dataStore[action]); },
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]);
210 func = function() { Element.setStyle($(id),{backgroundColor:'#FF3333'}); }
211 func(); setTimeout(func, 705); // Hit it twice in case it's still fading.
215 // Like Form.serialize, but excludes action and sets up clearInputs
216 grabInputs: function( where, 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 )
224 if ( 'hidden' != elements[i].type && 'submit' != elements[i].type && 'button' != elements[i].type ) {
225 this.clearInputs.push(elements[i]);
227 ajaxObj.restoreInputs.push([elements[i], elements[i].value]);
229 var queryComponent = Form.Element.serialize(elements[i]);
230 if (queryComponent) {
231 queryComponents.push(queryComponent);
234 return queryComponents.join('&');
237 // form.reset() can only do whole forms. This can do subsections.
239 this.clearInputs.each( function(i) {
241 if ( 'textarea' == i.tagName.toLowerCase() )
244 switch ( i.type.toLowerCase() ) {
245 case 'password': case 'text':
248 case 'checkbox': case 'radio':
251 case 'select': case 'select-one':
252 i.selectedIndex = null;
254 case 'select-multiple':
255 for (var o = 0; o < i.length; o++) i.options[o].selected = false;
259 this.clearInputs = [];
262 restoreForm: function(elements) {
263 elements.each( function(i) {
268 recolorList: function() {
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);
278 Element.addClassName(i,alt);
283 //No submit unless code returns true.
284 function killSubmit ( code, e ) {
285 e = e ? e : window.event;
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;
294 //Generic but lame JS closure
295 function encloseFunc(f){var a=arguments[1];return function(){return f(a);}}