@@ -3,17 +3,18 @@ jQuery( function( $ ) {
3
3
4
4
var g_users = { } ;
5
5
var g_posts = [ ] ;
6
+ var g_searchTimeout = null ;
6
7
7
8
// Updates the search box message.
8
9
var updateMessage = function ( text ) {
9
10
$ ( '#search-message' ) . text ( text ) ;
10
- }
11
+ } ;
11
12
12
13
// Regenerates the table based on the current globals.
13
14
var regenerateTable = function ( ) {
14
15
var newTable = generateTable ( g_posts , g_users ) ;
15
16
$ ( '#post-table' ) . replaceWith ( newTable ) ;
16
- }
17
+ } ;
17
18
18
19
// Creates a new table to display post results.
19
20
var generateTable = function ( posts , users ) {
@@ -28,6 +29,7 @@ jQuery( function( $ ) {
28
29
var headerRow = $ ( '<tr/>' ) . appendTo ( table ) ;
29
30
$ ( '<th/>' , { text : i18n . post } ) . appendTo ( headerRow ) ;
30
31
$ ( '<th/>' , { text : i18n . author } ) . appendTo ( headerRow ) ;
32
+ $ ( '<th/>' , { text : i18n . sticky } ) . appendTo ( headerRow ) ;
31
33
32
34
// For each post, create a row.
33
35
posts . filter ( function ( post ) {
@@ -47,10 +49,21 @@ jQuery( function( $ ) {
47
49
'href' : author . link ,
48
50
text : author . name
49
51
} ) . appendTo ( authorCell ) ;
52
+
53
+ // Sticky
54
+ var stickyCell = $ ( '<td/>' ) . appendTo ( row ) ;
55
+ var stickyInput = $ ( '<input/>' , {
56
+ 'type' : 'checkbox' ,
57
+ } ) . appendTo ( stickyCell ) ;
58
+ stickyInput . prop ( 'checked' , post . sticky ) ;
59
+
60
+ stickyInput . on ( 'change' , function ( evt ) {
61
+ toggleSticky ( post , stickyInput ) ;
62
+ } ) ;
50
63
} ) ;
51
64
52
65
return table ;
53
- }
66
+ } ;
54
67
55
68
// Returns the ids which aren't in the users list.
56
69
var filterUnfetchedUsers = function ( ids , users ) {
@@ -65,7 +78,7 @@ jQuery( function( $ ) {
65
78
}
66
79
67
80
return unfetched ;
68
- }
81
+ } ;
69
82
70
83
// Fetches the users which aren't in the users list, then adds them to it.
71
84
var fetchUsers = function ( ids , users ) {
@@ -81,7 +94,7 @@ jQuery( function( $ ) {
81
94
users [ id ] = {
82
95
name : '?' ,
83
96
link : '#'
84
- }
97
+ } ;
85
98
} ) ;
86
99
87
100
// Fetch those unfetched users.
@@ -103,17 +116,46 @@ jQuery( function( $ ) {
103
116
} ,
104
117
cache : false
105
118
} ) ;
106
- }
119
+ } ;
107
120
121
+ var toggleSticky = function ( post , checkbox ) {
122
+ var sticky = ! post . sticky ;
123
+ checkbox . prop ( 'disabled' , true ) ;
108
124
109
- var timeout = null ;
125
+ $ . ajax ( {
126
+ url : screen_data . api_root + 'wp/v2/posts/' + post . id ,
127
+ method : 'POST' ,
128
+ beforeSend : function ( req ) {
129
+ req . setRequestHeader ( 'X-WP-Nonce' , screen_data . api_nonce ) ;
130
+ } ,
131
+ data : {
132
+ 'sticky' : sticky
133
+ } ,
134
+ success : function ( data ) {
135
+ // Update the global state
136
+ g_posts [ data . id ] = data ;
137
+ } ,
138
+ error : function ( req ) {
139
+ console . error ( 'error on sticky update' ) ;
140
+ console . error ( req ) ;
141
+ } ,
142
+ complete : function ( ) {
143
+ // Either way, make sure the checkbox matches.
144
+ var sticky = g_posts [ post . id ] . sticky ;
145
+ if ( sticky !== checkbox . prop ( 'checked' ) ) {
146
+ checkbox . prop ( 'checked' , g_posts [ post . id ] . sticky ) ;
147
+ }
148
+ checkbox . prop ( 'disabled' , false ) ;
149
+ }
150
+ } ) ;
151
+ } ;
110
152
111
153
var clearTimeout = function ( ) {
112
- if ( timeout ) {
113
- window . clearTimeout ( timeout ) ;
114
- timeout = null ;
154
+ if ( g_searchTimeout ) {
155
+ window . clearTimeout ( g_searchTimeout ) ;
156
+ g_searchTimeout = null ;
115
157
}
116
- }
158
+ } ;
117
159
118
160
var searchPosts = function ( ) {
119
161
updateMessage ( screen_data . i18n . loading ) ;
@@ -137,16 +179,16 @@ jQuery( function( $ ) {
137
179
} ,
138
180
cache : false
139
181
} ) ;
140
- }
182
+ } ;
141
183
142
184
// Automatically search for what's in the text box
143
185
// if it sits for long enough.
144
186
$ ( '#search-box' ) . on ( 'input' , function ( evt ) {
145
187
clearTimeout ( ) ;
146
188
147
- timeout = window . setTimeout ( function ( ) {
189
+ g_searchTimeout = window . setTimeout ( function ( ) {
148
190
searchPosts ( ) ;
149
- timeout = null ;
191
+ g_searchTimeout = null ;
150
192
} , 500 ) ;
151
193
} ) ;
152
194
0 commit comments