|
1 |
|
2 (function($) { |
|
3 inlineEditPost = { |
|
4 |
|
5 init : function() { |
|
6 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit'); |
|
7 |
|
8 t.type = $('table.widefat').hasClass('page') ? 'page' : 'post'; |
|
9 t.what = '#'+t.type+'-'; |
|
10 |
|
11 // get all editable rows |
|
12 t.rows = $('tr.iedit'); |
|
13 |
|
14 // prepare the edit rows |
|
15 qeRow.keyup(function(e) { if(e.which == 27) return inlineEditPost.revert(); }); |
|
16 bulkRow.keyup(function(e) { if (e.which == 27) return inlineEditPost.revert(); }); |
|
17 |
|
18 $('a.cancel', qeRow).click(function() { return inlineEditPost.revert(); }); |
|
19 $('a.save', qeRow).click(function() { return inlineEditPost.save(this); }); |
|
20 $('td', qeRow).keydown(function(e) { if ( e.which == 13 ) return inlineEditPost.save(this); }); |
|
21 |
|
22 $('a.cancel', bulkRow).click(function() { return inlineEditPost.revert(); }); |
|
23 |
|
24 $('#inline-edit .inline-edit-private input[value=private]').click( function(){ |
|
25 var pw = $('input.inline-edit-password-input'); |
|
26 if ( $(this).attr('checked') ) { |
|
27 pw.val('').attr('disabled', 'disabled'); |
|
28 } else { |
|
29 pw.attr('disabled', ''); |
|
30 } |
|
31 }); |
|
32 |
|
33 // add events |
|
34 t.addEvents(t.rows); |
|
35 |
|
36 $('#bulk-title-div').parents('fieldset').after( |
|
37 $('#inline-edit fieldset.inline-edit-categories').clone() |
|
38 ).siblings( 'fieldset:last' ).prepend( |
|
39 $('#inline-edit label.inline-edit-tags').clone() |
|
40 ); |
|
41 |
|
42 // categories expandable? |
|
43 $('span.catshow').click(function() { |
|
44 $('.inline-editor ul.cat-checklist').addClass("cat-hover"); |
|
45 $('.inline-editor span.cathide').show(); |
|
46 $(this).hide(); |
|
47 }); |
|
48 |
|
49 $('span.cathide').click(function() { |
|
50 $('.inline-editor ul.cat-checklist').removeClass("cat-hover"); |
|
51 $('.inline-editor span.catshow').show(); |
|
52 $(this).hide(); |
|
53 }); |
|
54 |
|
55 $('select[name="_status"] option[value="future"]', bulkRow).remove(); |
|
56 |
|
57 $('#doaction, #doaction2').click(function(e){ |
|
58 var n = $(this).attr('id').substr(2); |
|
59 if ( $('select[name="'+n+'"]').val() == 'edit' ) { |
|
60 e.preventDefault(); |
|
61 t.setBulk(); |
|
62 } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) { |
|
63 t.revert(); |
|
64 } |
|
65 }); |
|
66 |
|
67 $('#post-query-submit').click(function(e){ |
|
68 if ( $('form#posts-filter tr.inline-editor').length > 0 ) |
|
69 t.revert(); |
|
70 }); |
|
71 |
|
72 }, |
|
73 |
|
74 toggle : function(el) { |
|
75 var t = this; |
|
76 $(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el); |
|
77 }, |
|
78 |
|
79 addEvents : function(r) { |
|
80 r.each(function() { |
|
81 var row = $(this); |
|
82 $('a.editinline', row).click(function() { inlineEditPost.edit(this); return false; }); |
|
83 }); |
|
84 }, |
|
85 |
|
86 setBulk : function() { |
|
87 var te = '', type = this.type, tax; |
|
88 this.revert(); |
|
89 |
|
90 $('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length); |
|
91 $('table.widefat tbody').prepend( $('#bulk-edit') ); |
|
92 $('#bulk-edit').addClass('inline-editor').show(); |
|
93 |
|
94 $('tbody th.check-column input[type="checkbox"]').each(function(i){ |
|
95 if ( $(this).attr('checked') ) { |
|
96 var id = $(this).val(), theTitle; |
|
97 theTitle = $('#inline_'+id+' .post_title').text() || inlineEditL10n.notitle; |
|
98 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>'; |
|
99 } |
|
100 }); |
|
101 |
|
102 $('#bulk-titles').html(te); |
|
103 $('#bulk-titles a').click(function() { |
|
104 var id = $(this).attr('id').substr(1); |
|
105 |
|
106 $('table.widefat input[value="'+id+'"]').attr('checked', ''); |
|
107 $('#ttle'+id).remove(); |
|
108 }); |
|
109 |
|
110 // enable autocomplete for tags |
|
111 if ( type == 'post' ) { |
|
112 // support multi taxonomies? |
|
113 tax = 'post_tag'; |
|
114 $('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } ); |
|
115 } |
|
116 }, |
|
117 |
|
118 edit : function(id) { |
|
119 var t = this, fields, editRow, rowData, cats, status, pageOpt, f, pageLevel, nextPage, pageLoop = true, nextLevel, tax; |
|
120 t.revert(); |
|
121 |
|
122 if ( typeof(id) == 'object' ) |
|
123 id = t.getId(id); |
|
124 |
|
125 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password']; |
|
126 if ( t.type == 'page' ) fields.push('post_parent', 'menu_order', 'page_template'); |
|
127 if ( t.type == 'post' ) fields.push('tags_input'); |
|
128 |
|
129 // add the new blank row |
|
130 editRow = $('#inline-edit').clone(true); |
|
131 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length); |
|
132 |
|
133 if ( $(t.what+id).hasClass('alternate') ) |
|
134 $(editRow).addClass('alternate'); |
|
135 $(t.what+id).hide().after(editRow); |
|
136 |
|
137 // populate the data |
|
138 rowData = $('#inline_'+id); |
|
139 for ( f = 0; f < fields.length; f++ ) { |
|
140 $(':input[name="'+fields[f]+'"]', editRow).val( $('.'+fields[f], rowData).text() ); |
|
141 } |
|
142 |
|
143 if ( $('.comment_status', rowData).text() == 'open' ) |
|
144 $('input[name="comment_status"]', editRow).attr("checked", "checked"); |
|
145 if ( $('.ping_status', rowData).text() == 'open' ) |
|
146 $('input[name="ping_status"]', editRow).attr("checked", "checked"); |
|
147 if ( $('.sticky', rowData).text() == 'sticky' ) |
|
148 $('input[name="sticky"]', editRow).attr("checked", "checked"); |
|
149 |
|
150 // categories |
|
151 if ( cats = $('.post_category', rowData).text() ) |
|
152 $('ul.cat-checklist :checkbox', editRow).val(cats.split(',')); |
|
153 |
|
154 // handle the post status |
|
155 status = $('._status', rowData).text(); |
|
156 if ( status != 'future' ) $('select[name="_status"] option[value="future"]', editRow).remove(); |
|
157 if ( status == 'private' ) { |
|
158 $('input[name="keep_private"]', editRow).attr("checked", "checked"); |
|
159 $('input.inline-edit-password-input').val('').attr('disabled', 'disabled'); |
|
160 } |
|
161 |
|
162 // remove the current page and children from the parent dropdown |
|
163 pageOpt = $('select[name="post_parent"] option[value="'+id+'"]', editRow); |
|
164 if ( pageOpt.length > 0 ) { |
|
165 pageLevel = pageOpt[0].className.split('-')[1]; |
|
166 nextPage = pageOpt; |
|
167 while ( pageLoop ) { |
|
168 nextPage = nextPage.next('option'); |
|
169 if (nextPage.length == 0) break; |
|
170 nextLevel = nextPage[0].className.split('-')[1]; |
|
171 if ( nextLevel <= pageLevel ) { |
|
172 pageLoop = false; |
|
173 } else { |
|
174 nextPage.remove(); |
|
175 nextPage = pageOpt; |
|
176 } |
|
177 } |
|
178 pageOpt.remove(); |
|
179 } |
|
180 |
|
181 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show(); |
|
182 $('.ptitle', editRow).focus(); |
|
183 |
|
184 // enable autocomplete for tags |
|
185 if ( t.type == 'post' ) { |
|
186 tax = 'post_tag'; |
|
187 $('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } ); |
|
188 } |
|
189 |
|
190 return false; |
|
191 }, |
|
192 |
|
193 save : function(id) { |
|
194 var params, fields; |
|
195 |
|
196 if( typeof(id) == 'object' ) |
|
197 id = this.getId(id); |
|
198 |
|
199 $('table.widefat .inline-edit-save .waiting').show(); |
|
200 |
|
201 params = { |
|
202 action: 'inline-save', |
|
203 post_type: this.type, |
|
204 post_ID: id, |
|
205 edit_date: 'true' |
|
206 }; |
|
207 |
|
208 fields = $('#edit-'+id+' :input').fieldSerialize(); |
|
209 params = fields + '&' + $.param(params); |
|
210 |
|
211 // make ajax request |
|
212 $.post('admin-ajax.php', params, |
|
213 function(r) { |
|
214 $('table.widefat .inline-edit-save .waiting').hide(); |
|
215 |
|
216 if (r) { |
|
217 if ( -1 != r.indexOf('<tr') ) { |
|
218 $(inlineEditPost.what+id).remove(); |
|
219 $('#edit-'+id).before(r).remove(); |
|
220 |
|
221 var row = $(inlineEditPost.what+id); |
|
222 row.hide(); |
|
223 |
|
224 if ( 'draft' == $('input[name="post_status"]').val() ) |
|
225 row.find('td.column-comments').hide(); |
|
226 |
|
227 inlineEditPost.addEvents(row); |
|
228 row.fadeIn(); |
|
229 } else { |
|
230 r = r.replace( /<.[^<>]*?>/g, '' ); |
|
231 $('#edit-'+id+' .inline-edit-save').append('<span class="error">'+r+'</span>'); |
|
232 } |
|
233 } else { |
|
234 $('#edit-'+id+' .inline-edit-save').append('<span class="error">'+inlineEditL10n.error+'</span>'); |
|
235 } |
|
236 } |
|
237 , 'html'); |
|
238 return false; |
|
239 }, |
|
240 |
|
241 revert : function() { |
|
242 var id; |
|
243 |
|
244 if ( id = $('table.widefat tr.inline-editor').attr('id') ) { |
|
245 $('table.widefat .inline-edit-save .waiting').hide(); |
|
246 |
|
247 if ( 'bulk-edit' == id ) { |
|
248 $('table.widefat #bulk-edit').removeClass('inline-editor').hide(); |
|
249 $('#bulk-titles').html(''); |
|
250 $('#inlineedit').append( $('#bulk-edit') ); |
|
251 } else { |
|
252 $('#'+id).remove(); |
|
253 id = id.substr( id.lastIndexOf('-') + 1 ); |
|
254 $(this.what+id).show(); |
|
255 } |
|
256 } |
|
257 |
|
258 return false; |
|
259 }, |
|
260 |
|
261 getId : function(o) { |
|
262 var id = o.tagName == 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-'); |
|
263 return parts[parts.length - 1]; |
|
264 } |
|
265 }; |
|
266 |
|
267 $(document).ready(function(){inlineEditPost.init();}); |
|
268 })(jQuery); |