1
1
2
2
jQuery ( function ( $ ) {
3
3
4
+ var POST_STATUS = [ 'publish' , 'future' , 'draft' , 'pending' , 'private' ] ;
4
5
var g_users = { } ;
5
6
var g_posts = [ ] ;
6
7
var g_searchTimeout = null ;
@@ -42,7 +43,7 @@ jQuery( function( $ ) {
42
43
var headerRow = $ ( '<tr/>' ) . appendTo ( table ) ;
43
44
$ ( '<th/>' , { text : i18n . post } ) . appendTo ( headerRow ) ;
44
45
$ ( '<th/>' , { text : i18n . author } ) . appendTo ( headerRow ) ;
45
- $ ( '<th/>' , { text : i18n . sticky } ) . appendTo ( headerRow ) ;
46
+ $ ( '<th/>' , { text : i18n . status } ) . appendTo ( headerRow ) ;
46
47
47
48
// For each post, create a row.
48
49
posts . filter ( function ( post ) {
@@ -55,6 +56,7 @@ jQuery( function( $ ) {
55
56
56
57
// Creates a new TR element for a post.
57
58
var generatePostRow = function ( post , users ) {
59
+ var i18n = screen_data . i18n ;
58
60
var row = $ ( '<tr/>' , { 'key' : post . id } ) ;
59
61
60
62
// Post Title
@@ -72,19 +74,23 @@ jQuery( function( $ ) {
72
74
html : author . name
73
75
} ) . appendTo ( authorCell ) ;
74
76
75
- // Sticky
76
- var stickyCell = $ ( '<td/>' ) . appendTo ( row ) ;
77
- var stickyInput = $ ( '<input/>' , {
78
- 'type' : 'checkbox' ,
79
- } ) . appendTo ( stickyCell ) ;
80
- stickyInput . prop ( 'checked' , post . sticky ) ;
81
-
82
- stickyInput . on ( 'change' , function ( evt ) {
83
- stickyInput . prop ( 'disabled' , true ) ;
84
- var props = {
85
- sticky : ( ! post . sticky )
86
- } ;
87
- updatePost ( post , props ) ;
77
+ // Status
78
+ var statusCell = $ ( '<td/>' ) . appendTo ( row ) ;
79
+ var statusSelect = $ ( '<select/>' ) . appendTo ( statusCell ) ;
80
+ POST_STATUS . filter ( function ( value ) {
81
+ var opt = $ ( '<option/>' , {
82
+ 'value' : value ,
83
+ text : i18n [ value ] ,
84
+ 'selected' : ( post . status == value )
85
+ } ) . appendTo ( statusSelect ) ;
86
+ } ) ;
87
+ statusSelect . on ( 'change' , function ( evt ) {
88
+ var selected = $ ( 'option:selected' , this ) ;
89
+ var newStatus = ( selected . length ? selected [ 0 ] . value : null ) ;
90
+ if ( newStatus ) {
91
+ statusSelect . prop ( 'disabled' , true ) ; // This will be regenerated
92
+ updatePost ( post , { status : newStatus } ) ;
93
+ }
88
94
} ) ;
89
95
90
96
return row ;
@@ -157,7 +163,7 @@ jQuery( function( $ ) {
157
163
regeneratePost ( data ) ;
158
164
} ,
159
165
error : function ( req ) {
160
- console . error ( 'error on sticky update' ) ;
166
+ console . error ( 'error on post update' ) ;
161
167
console . error ( req ) ;
162
168
// Reset the post to its previous state.
163
169
regeneratePost ( post ) ;
@@ -179,16 +185,22 @@ jQuery( function( $ ) {
179
185
180
186
var text = $ ( '#search-box' ) . val ( ) ;
181
187
var apiString = 'wp/v2/posts?' +
188
+ 'context=edit&per_page=100&' +
189
+ 'status=' + POST_STATUS . toString ( ) + '&' +
182
190
'search=' + encodeURIComponent ( text ) ;
183
191
184
192
$ . ajax ( {
185
193
url : screen_data . api_root + apiString ,
194
+ beforeSend : function ( req ) {
195
+ req . setRequestHeader ( 'X-WP-Nonce' , screen_data . api_nonce ) ;
196
+ } ,
186
197
success : function ( data ) {
187
198
g_posts = data ;
188
199
regenerateTable ( ) ;
200
+ // TODO: Fetch users here instead of in regenerateTable()
189
201
} ,
190
202
error : function ( req ) {
191
- console . error ( 'error on request' ) ;
203
+ console . error ( 'error on posts request' ) ;
192
204
console . error ( req ) ;
193
205
} ,
194
206
complete : function ( ) {
@@ -206,7 +218,7 @@ jQuery( function( $ ) {
206
218
g_searchTimeout = window . setTimeout ( function ( ) {
207
219
searchPosts ( ) ;
208
220
g_searchTimeout = null ;
209
- } , 500 ) ;
221
+ } , 250 ) ;
210
222
} ) ;
211
223
212
224
// Immediately search when the form is submitted (i.e. <ENTER> key )
0 commit comments