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