]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.js
WordPress 3.9.1
[autoinstalls/wordpress.git] / wp-admin / js / inline-edit-post.js
1 /* global inlineEditL10n, ajaxurl, typenow */
2
3 var inlineEditPost;
4 (function($) {
5 inlineEditPost = {
6
7         init : function(){
8                 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
9
10                 t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
11                 t.what = '#post-';
12
13                 // prepare the edit rows
14                 qeRow.keyup(function(e){
15                         if ( e.which === 27 ) {
16                                 return inlineEditPost.revert();
17                         }
18                 });
19                 bulkRow.keyup(function(e){
20                         if ( e.which === 27 ) {
21                                 return inlineEditPost.revert();
22                         }
23                 });
24
25                 $('a.cancel', qeRow).click(function(){
26                         return inlineEditPost.revert();
27                 });
28                 $('a.save', qeRow).click(function(){
29                         return inlineEditPost.save(this);
30                 });
31                 $('td', qeRow).keydown(function(e){
32                         if ( e.which === 13 ) {
33                                 return inlineEditPost.save(this);
34                         }
35                 });
36
37                 $('a.cancel', bulkRow).click(function(){
38                         return inlineEditPost.revert();
39                 });
40
41                 $('#inline-edit .inline-edit-private input[value="private"]').click( function(){
42                         var pw = $('input.inline-edit-password-input');
43                         if ( $(this).prop('checked') ) {
44                                 pw.val('').prop('disabled', true);
45                         } else {
46                                 pw.prop('disabled', false);
47                         }
48                 });
49
50                 // add events
51                 $('#the-list').on('click', 'a.editinline', function(){
52                         inlineEditPost.edit(this);
53                         return false;
54                 });
55
56                 $('#bulk-title-div').parents('fieldset').after(
57                         $('#inline-edit fieldset.inline-edit-categories').clone()
58                 ).siblings( 'fieldset:last' ).prepend(
59                         $('#inline-edit label.inline-edit-tags').clone()
60                 );
61
62                 $('select[name="_status"] option[value="future"]', bulkRow).remove();
63
64                 $('#doaction, #doaction2').click(function(e){
65                         var n = $(this).attr('id').substr(2);
66                         if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
67                                 e.preventDefault();
68                                 t.setBulk();
69                         } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
70                                 t.revert();
71                         }
72                 });
73         },
74
75         toggle : function(el){
76                 var t = this;
77                 $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
78         },
79
80         setBulk : function(){
81                 var te = '', type = this.type, tax, c = true;
82                 this.revert();
83
84                 $('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
85                 $('table.widefat tbody').prepend( $('#bulk-edit') );
86                 $('#bulk-edit').addClass('inline-editor').show();
87
88                 $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
89                         if ( $(this).prop('checked') ) {
90                                 c = false;
91                                 var id = $(this).val(), theTitle;
92                                 theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
93                                 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
94                         }
95                 });
96
97                 if ( c ) {
98                         return this.revert();
99                 }
100
101                 $('#bulk-titles').html(te);
102                 $('#bulk-titles a').click(function(){
103                         var id = $(this).attr('id').substr(1);
104
105                         $('table.widefat input[value="' + id + '"]').prop('checked', false);
106                         $('#ttle'+id).remove();
107                 });
108
109                 // enable autocomplete for tags
110                 if ( 'post' === type ) {
111                         // support multi taxonomies?
112                         tax = 'post_tag';
113                         $('tr.inline-editor textarea[name="tax_input['+tax+']"]').suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
114                 }
115                 $('html, body').animate( { scrollTop: 0 }, 'fast' );
116         },
117
118         edit : function(id) {
119                 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
120                 t.revert();
121
122                 if ( typeof(id) === 'object' ) {
123                         id = t.getId(id);
124                 }
125
126                 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order'];
127                 if ( t.type === 'page' ) {
128                         fields.push('post_parent', 'page_template');
129                 }
130
131                 // add the new blank row
132                 editRow = $('#inline-edit').clone(true);
133                 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
134
135                 if ( $( t.what + id ).hasClass( 'alternate' ) ) {
136                         $(editRow).addClass('alternate');
137                 }
138                 $(t.what+id).hide().after(editRow);
139
140                 // populate the data
141                 rowData = $('#inline_'+id);
142                 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
143                         // author no longer has edit caps, so we need to add them to the list of authors
144                         $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
145                 }
146                 if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
147                         $('label.inline-edit-author', editRow).hide();
148                 }
149
150                 // hide unsupported formats, but leave the current format alone
151                 cur_format = $('.post_format', rowData).text();
152                 $('option.unsupported', editRow).each(function() {
153                         var $this = $(this);
154                         if ( $this.val() !== cur_format ) {
155                                 $this.remove();
156                         }
157                 });
158
159                 for ( f = 0; f < fields.length; f++ ) {
160                         $(':input[name="' + fields[f] + '"]', editRow).val( $('.'+fields[f], rowData).text() );
161                 }
162
163                 if ( $( '.comment_status', rowData ).text() === 'open' ) {
164                         $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
165                 }
166                 if ( $( '.ping_status', rowData ).text() === 'open' ) {
167                         $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
168                 }
169                 if ( $( '.sticky', rowData ).text() === 'sticky' ) {
170                         $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
171                 }
172
173                 // hierarchical taxonomies
174                 $('.post_category', rowData).each(function(){
175                         var taxname,
176                                 term_ids = $(this).text();
177
178                         if ( term_ids ) {
179                                 taxname = $(this).attr('id').replace('_'+id, '');
180                                 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
181                         }
182                 });
183
184                 //flat taxonomies
185                 $('.tags_input', rowData).each(function(){
186                         var terms = $(this).text(),
187                                 taxname = $(this).attr('id').replace('_' + id, ''),
188                                 textarea = $('textarea.tax_input_' + taxname, editRow),
189                                 comma = inlineEditL10n.comma;
190
191                         if ( terms ) {
192                                 if ( ',' !== comma ) {
193                                         terms = terms.replace(/,/g, comma);
194                                 }
195                                 textarea.val(terms);
196                         }
197
198                         textarea.suggest( ajaxurl + '?action=ajax-tag-search&tax=' + taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
199                 });
200
201                 // handle the post status
202                 status = $('._status', rowData).text();
203                 if ( 'future' !== status ) {
204                         $('select[name="_status"] option[value="future"]', editRow).remove();
205                 }
206
207                 if ( 'private' === status ) {
208                         $('input[name="keep_private"]', editRow).prop('checked', true);
209                         $('input.inline-edit-password-input').val('').prop('disabled', true);
210                 }
211
212                 // remove the current page and children from the parent dropdown
213                 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
214                 if ( pageOpt.length > 0 ) {
215                         pageLevel = pageOpt[0].className.split('-')[1];
216                         nextPage = pageOpt;
217                         while ( pageLoop ) {
218                                 nextPage = nextPage.next('option');
219                                 if ( nextPage.length === 0 ) {
220                                         break;
221                                 }
222
223                                 nextLevel = nextPage[0].className.split('-')[1];
224
225                                 if ( nextLevel <= pageLevel ) {
226                                         pageLoop = false;
227                                 } else {
228                                         nextPage.remove();
229                                         nextPage = pageOpt;
230                                 }
231                         }
232                         pageOpt.remove();
233                 }
234
235                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
236                 $('.ptitle', editRow).focus();
237
238                 return false;
239         },
240
241         save : function(id) {
242                 var params, fields, page = $('.post_status_page').val() || '';
243
244                 if ( typeof(id) === 'object' ) {
245                         id = this.getId(id);
246                 }
247
248                 $('table.widefat .spinner').show();
249
250                 params = {
251                         action: 'inline-save',
252                         post_type: typenow,
253                         post_ID: id,
254                         edit_date: 'true',
255                         post_status: page
256                 };
257
258                 fields = $('#edit-'+id).find(':input').serialize();
259                 params = fields + '&' + $.param(params);
260
261                 // make ajax request
262                 $.post( ajaxurl, params,
263                         function(r) {
264                                 $('table.widefat .spinner').hide();
265
266                                 if (r) {
267                                         if ( -1 !== r.indexOf( '<tr' ) ) {
268                                                 $(inlineEditPost.what+id).remove();
269                                                 $('#edit-'+id).before(r).remove();
270                                                 $(inlineEditPost.what+id).hide().fadeIn();
271                                         } else {
272                                                 r = r.replace( /<.[^<>]*?>/g, '' );
273                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
274                                         }
275                                 } else {
276                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
277                                 }
278
279                                 if ( $('#post-'+id).prev().hasClass('alternate') ) {
280                                         $('#post-'+id).removeClass('alternate');
281                                 }
282                         },
283                 'html');
284                 return false;
285         },
286
287         revert : function(){
288                 var id = $('table.widefat tr.inline-editor').attr('id');
289
290                 if ( id ) {
291                         $('table.widefat .spinner').hide();
292
293                         if ( 'bulk-edit' === id ) {
294                                 $('table.widefat #bulk-edit').removeClass('inline-editor').hide();
295                                 $('#bulk-titles').html('');
296                                 $('#inlineedit').append( $('#bulk-edit') );
297                         } else {
298                                 $('#'+id).remove();
299                                 id = id.substr( id.lastIndexOf('-') + 1 );
300                                 $(this.what+id).show();
301                         }
302                 }
303
304                 return false;
305         },
306
307         getId : function(o) {
308                 var id = $(o).closest('tr').attr('id'),
309                         parts = id.split('-');
310                 return parts[parts.length - 1];
311         }
312 };
313
314 $( document ).ready( function(){ inlineEditPost.init(); } );
315
316 // Show/hide locks on posts
317 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
318         var locked = data['wp-check-locked-posts'] || {};
319
320         $('#the-list tr').each( function(i, el) {
321                 var key = el.id, row = $(el), lock_data, avatar;
322
323                 if ( locked.hasOwnProperty( key ) ) {
324                         if ( ! row.hasClass('wp-locked') ) {
325                                 lock_data = locked[key];
326                                 row.find('.column-title .locked-text').text( lock_data.text );
327                                 row.find('.check-column checkbox').prop('checked', false);
328
329                                 if ( lock_data.avatar_src ) {
330                                         avatar = $('<img class="avatar avatar-18 photo" width="18" height="18" />').attr( 'src', lock_data.avatar_src.replace(/&amp;/g, '&') );
331                                         row.find('.column-title .locked-avatar').empty().append( avatar );
332                                 }
333                                 row.addClass('wp-locked');
334                         }
335                 } else if ( row.hasClass('wp-locked') ) {
336                         // Make room for the CSS animation
337                         row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
338                 }
339         });
340 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
341         var check = [];
342
343         $('#the-list tr').each( function(i, el) {
344                 if ( el.id ) {
345                         check.push( el.id );
346                 }
347         });
348
349         if ( check.length ) {
350                 data['wp-check-locked-posts'] = check;
351         }
352 }).ready( function() {
353         // Set the heartbeat interval to 15 sec.
354         if ( typeof wp !== 'undefined' && wp.heartbeat ) {
355                 wp.heartbeat.interval( 15 );
356         }
357 });
358
359 }(jQuery));