]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-tax.js
WordPress 4.1.1-scripts
[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,
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                 if ( $( t.what + id ).hasClass( 'alternate' ) ) {
60                         $(editRow).addClass('alternate');
61                 }
62
63                 $(t.what+id).hide().after(editRow);
64
65                 $(':input[name="name"]', editRow).val( $('.name', rowData).text() );
66                 $(':input[name="slug"]', editRow).val( $('.slug', rowData).text() );
67
68                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
69                 $('.ptitle', editRow).eq(0).focus();
70
71                 return false;
72         },
73
74         save : function(id) {
75                 var params, fields, tax = $('input[name="taxonomy"]').val() || '';
76
77                 if( typeof(id) === 'object' ) {
78                         id = this.getId(id);
79                 }
80
81                 $('table.widefat .spinner').show();
82
83                 params = {
84                         action: 'inline-save-tax',
85                         tax_type: this.type,
86                         tax_ID: id,
87                         taxonomy: tax
88                 };
89
90                 fields = $('#edit-'+id).find(':input').serialize();
91                 params = fields + '&' + $.param(params);
92
93                 // make ajax request
94                 $.post( ajaxurl, params,
95                         function(r) {
96                                 var row, new_id;
97                                 $('table.widefat .spinner').hide();
98
99                                 if (r) {
100                                         if ( -1 !== r.indexOf( '<tr' ) ) {
101                                                 $(inlineEditTax.what+id).remove();
102                                                 new_id = $(r).attr('id');
103
104                                                 $('#edit-'+id).before(r).remove();
105                                                 row = new_id ? $('#'+new_id) : $(inlineEditTax.what+id);
106                                                 row.hide().fadeIn();
107                                         } else {
108                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
109                                         }
110                                 } else {
111                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
112                                 }
113
114                                 if ( $( row ).prev( 'tr' ).hasClass( 'alternate' ) ) {
115                                         $(row).removeClass('alternate');
116                                 }
117                         }
118                 );
119                 return false;
120         },
121
122         revert : function() {
123                 var id = $('table.widefat tr.inline-editor').attr('id');
124
125                 if ( id ) {
126                         $('table.widefat .spinner').hide();
127                         $('#'+id).remove();
128                         id = id.substr( id.lastIndexOf('-') + 1 );
129                         $(this.what+id).show();
130                 }
131
132                 return false;
133         },
134
135         getId : function(o) {
136                 var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
137                 return parts[parts.length - 1];
138         }
139 };
140
141 $(document).ready(function(){inlineEditTax.init();});
142 })(jQuery);