5
|
1 |
/* global inlineEditL10n, ajaxurl */ |
0
|
2 |
|
5
|
3 |
var inlineEditTax; |
0
|
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 |
5
|
19 |
row.keyup( function( e ) { |
|
20 |
if ( e.which === 27 ) { |
|
21 |
return inlineEditTax.revert(); |
|
22 |
} |
|
23 |
}); |
0
|
24 |
|
5
|
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 |
}); |
0
|
36 |
|
5
|
37 |
$( '#posts-filter input[type="submit"]' ).mousedown( function() { |
0
|
38 |
t.revert(); |
|
39 |
}); |
|
40 |
}, |
|
41 |
|
|
42 |
toggle : function(el) { |
|
43 |
var t = this; |
5
|
44 |
$(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el); |
0
|
45 |
}, |
|
46 |
|
|
47 |
edit : function(id) { |
5
|
48 |
var editRow, rowData, val, |
|
49 |
t = this; |
0
|
50 |
t.revert(); |
|
51 |
|
5
|
52 |
if ( typeof(id) === 'object' ) { |
0
|
53 |
id = t.getId(id); |
5
|
54 |
} |
0
|
55 |
|
|
56 |
editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id); |
|
57 |
$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length); |
|
58 |
|
5
|
59 |
$(t.what+id).hide().after(editRow).after('<tr class="hidden"></tr>'); |
0
|
60 |
|
5
|
61 |
val = $('.name', rowData); |
|
62 |
val.find( 'img' ).replaceWith( function() { return this.alt; } ); |
|
63 |
val = val.text(); |
|
64 |
$(':input[name="name"]', editRow).val( val ); |
0
|
65 |
|
5
|
66 |
val = $('.slug', rowData); |
|
67 |
val.find( 'img' ).replaceWith( function() { return this.alt; } ); |
|
68 |
val = val.text(); |
|
69 |
$(':input[name="slug"]', editRow).val( val ); |
0
|
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 |
|
5
|
80 |
if( typeof(id) === 'object' ) { |
0
|
81 |
id = this.getId(id); |
5
|
82 |
} |
0
|
83 |
|
5
|
84 |
$( 'table.widefat .spinner' ).addClass( 'is-active' ); |
0
|
85 |
|
|
86 |
params = { |
|
87 |
action: 'inline-save-tax', |
|
88 |
tax_type: this.type, |
|
89 |
tax_ID: id, |
|
90 |
taxonomy: tax |
|
91 |
}; |
|
92 |
|
5
|
93 |
fields = $('#edit-'+id).find(':input').serialize(); |
0
|
94 |
params = fields + '&' + $.param(params); |
|
95 |
|
|
96 |
// make ajax request |
|
97 |
$.post( ajaxurl, params, |
|
98 |
function(r) { |
5
|
99 |
var row, new_id, option_value; |
|
100 |
$( 'table.widefat .spinner' ).removeClass( 'is-active' ); |
0
|
101 |
|
|
102 |
if (r) { |
5
|
103 |
if ( -1 !== r.indexOf( '<tr' ) ) { |
|
104 |
$(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove(); |
0
|
105 |
new_id = $(r).attr('id'); |
|
106 |
|
|
107 |
$('#edit-'+id).before(r).remove(); |
5
|
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 |
|
0
|
120 |
row.hide().fadeIn(); |
5
|
121 |
} else { |
0
|
122 |
$('#edit-'+id+' .inline-edit-save .error').html(r).show(); |
5
|
123 |
} |
|
124 |
} else { |
0
|
125 |
$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show(); |
5
|
126 |
} |
0
|
127 |
} |
|
128 |
); |
|
129 |
return false; |
|
130 |
}, |
|
131 |
|
|
132 |
revert : function() { |
|
133 |
var id = $('table.widefat tr.inline-editor').attr('id'); |
|
134 |
|
|
135 |
if ( id ) { |
5
|
136 |
$( 'table.widefat .spinner' ).removeClass( 'is-active' ); |
|
137 |
$('#'+id).siblings('tr.hidden').addBack().remove(); |
0
|
138 |
id = id.substr( id.lastIndexOf('-') + 1 ); |
|
139 |
$(this.what+id).show(); |
|
140 |
} |
|
141 |
|
|
142 |
return false; |
|
143 |
}, |
|
144 |
|
|
145 |
getId : function(o) { |
5
|
146 |
var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-'); |
0
|
147 |
return parts[parts.length - 1]; |
|
148 |
} |
|
149 |
}; |
|
150 |
|
|
151 |
$(document).ready(function(){inlineEditTax.init();}); |
|
152 |
})(jQuery); |