]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-tax.js
WordPress 4.2.3
[autoinstalls/wordpress.git] / wp-admin / js / inline-edit-tax.js
1 /* global inlineEditL10n, ajaxurl */
2
3 var inlineEditTax;
4 (function($) {
5 inlineEditTax = {
6
7         init : function() {
8                 var t = this, row = $('#inline-edit');
9
10                 t.type = $('#the-list').attr('data-wp-lists').substr(5);
11                 t.what = '#'+t.type+'-';
12
13                 $('#the-list').on('click', 'a.editinline', function(){
14                         inlineEditTax.edit(this);
15                         return false;
16                 });
17
18                 // prepare the edit row
19                 row.keyup( function( e ) {
20                         if ( e.which === 27 ) {
21                                 return inlineEditTax.revert();
22                         }
23                 });
24
25                 $( 'a.cancel', row ).click( function() {
26                         return inlineEditTax.revert();
27                 });
28                 $( 'a.save', row ).click( function() {
29                         return inlineEditTax.save(this);
30                 });
31                 $( 'input, select', row ).keydown( function( e ) {
32                         if ( e.which === 13 ) {
33                                 return inlineEditTax.save( this );
34                         }
35                 });
36
37                 $( '#posts-filter input[type="submit"]' ).mousedown( function() {
38                         t.revert();
39                 });
40         },
41
42         toggle : function(el) {
43                 var t = this;
44                 $(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el);
45         },
46
47         edit : function(id) {
48                 var editRow, rowData, val,
49                         t = this;
50                 t.revert();
51
52                 if ( typeof(id) === 'object' ) {
53                         id = t.getId(id);
54                 }
55
56                 editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
57                 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
58
59                 $(t.what+id).hide().after(editRow).after('<tr class="hidden"></tr>');
60
61                 val = $('.name', rowData);
62                 val.find( 'img' ).replaceWith( function() { return this.alt; } );
63                 val = val.text();
64                 $(':input[name="name"]', editRow).val( val );
65
66                 val = $('.slug', rowData);
67                 val.find( 'img' ).replaceWith( function() { return this.alt; } );
68                 val = val.text();
69                 $(':input[name="slug"]', editRow).val( val );
70
71                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
72                 $('.ptitle', editRow).eq(0).focus();
73
74                 return false;
75         },
76
77         save : function(id) {
78                 var params, fields, tax = $('input[name="taxonomy"]').val() || '';
79
80                 if( typeof(id) === 'object' ) {
81                         id = this.getId(id);
82                 }
83
84                 $( 'table.widefat .spinner' ).addClass( 'is-active' );
85
86                 params = {
87                         action: 'inline-save-tax',
88                         tax_type: this.type,
89                         tax_ID: id,
90                         taxonomy: tax
91                 };
92
93                 fields = $('#edit-'+id).find(':input').serialize();
94                 params = fields + '&' + $.param(params);
95
96                 // make ajax request
97                 $.post( ajaxurl, params,
98                         function(r) {
99                                 var row, new_id, option_value;
100                                 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
101
102                                 if (r) {
103                                         if ( -1 !== r.indexOf( '<tr' ) ) {
104                                                 $(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
105                                                 new_id = $(r).attr('id');
106
107                                                 $('#edit-'+id).before(r).remove();
108
109                                                 if ( new_id ) {
110                                                         option_value = new_id.replace( inlineEditTax.type + '-', '' );
111                                                         row = $( '#' + new_id );
112                                                 } else {
113                                                         option_value = id;
114                                                         row = $( inlineEditTax.what + id );
115                                                 }
116
117                                                 // Update the value in the Parent dropdown.
118                                                 $( '#parent' ).find( 'option[value=' + option_value + ']' ).text( row.find( '.row-title' ).text() );
119
120                                                 row.hide().fadeIn();
121                                         } else {
122                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
123                                         }
124                                 } else {
125                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
126                                 }
127                         }
128                 );
129                 return false;
130         },
131
132         revert : function() {
133                 var id = $('table.widefat tr.inline-editor').attr('id');
134
135                 if ( id ) {
136                         $( 'table.widefat .spinner' ).removeClass( 'is-active' );
137                         $('#'+id).siblings('tr.hidden').addBack().remove();
138                         id = id.substr( id.lastIndexOf('-') + 1 );
139                         $(this.what+id).show();
140                 }
141
142                 return false;
143         },
144
145         getId : function(o) {
146                 var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
147                 return parts[parts.length - 1];
148         }
149 };
150
151 $(document).ready(function(){inlineEditTax.init();});
152 })(jQuery);