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