This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcustomize-posts-panel.js
More file actions
228 lines (192 loc) · 6.45 KB
/
Copy pathcustomize-posts-panel.js
File metadata and controls
228 lines (192 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* global wp, jQuery */
/* eslint consistent-this: [ "error", "panel" ], no-magic-numbers: [ "error", { "ignore": [-1,0,1,100] } ] */
(function( api, $ ) {
'use strict';
if ( ! api.Posts ) {
api.Posts = {};
}
/**
* A panel for managing posts.
*
* @class
* @augments wp.customize.Panel
* @augments wp.customize.Class
*/
api.Posts.PostsPanel = api.Panel.extend({
postType: 'post',
trashNotificationCode: 'untrash',
ready: function() {
var panel = this;
// @todo Let the panel label a count for the number of posts in it.
api.Panel.prototype.ready.call( panel );
if ( panel.params.post_type ) {
panel.postType = panel.params.post_type;
}
panel.deferred.embedded.done(function() {
panel.setupPanelActions();
});
api.bind( 'saved', function() {
panel.removeTrashNotifications();
} );
},
/**
* Add new post stub, which builds the UI & listens for click events.
*
* @return {void}
*/
setupPanelActions: function() {
var panel = this, descriptionContainer, panelActionsTemplate, postTypeObj, actionsContainer;
descriptionContainer = panel.container.find( '.panel-meta:first' );
panelActionsTemplate = wp.template( 'customize-posts-' + panel.postType + '-panel-actions' );
postTypeObj = api.Posts.data.postTypes[ panel.postType ];
panel.queriedPostSelect2ItemSelectionTemplate = wp.template( 'customize-posts-' + panel.postType + '-panel-select2-selection-item' );
panel.queriedPostSelect2ItemResultTemplate = wp.template( 'customize-posts-' + panel.postType + '-panel-select2-result-item' );
actionsContainer = $( panelActionsTemplate( {
can_create_posts: postTypeObj.current_user_can.create_posts,
add_new_post_label: postTypeObj.labels.add_new_item
} ) );
panel.postSelectionLookupSelect2 = actionsContainer.find( '.post-selection-lookup' ).select2({
ajax: {
transport: function( params, success, failure ) {
var request = panel.queryPosts({
s: params.data.term,
paged: params.data.page || 1
});
request.done( success );
request.fail( failure );
return request;
},
delay: 250
},
templateResult: function( data ) {
return panel.queriedPostSelect2ItemResultTemplate( data );
},
templateSelection: function( data ) {
return panel.queriedPostSelect2ItemSelectionTemplate( data );
},
escapeMarkup: function( m ) {
// Do not escape HTML in the select options text.
return m;
},
multiple: false,
placeholder: postTypeObj.labels.search_items,
width: '80%'
});
panel.postSelectionLookupSelect2.on( 'select2:select', function() {
var postId = panel.postSelectionLookupSelect2.val(), ensuredPromise;
panel.postSelectionLookupSelect2.prop( 'disabled', true );
postId = parseInt( postId, 10 );
ensuredPromise = api.Posts.ensurePosts( [ postId ] );
ensuredPromise.done( function( postsData ) {
var postData = postsData[ postId ], isPostVisibleInPreview;
if ( ! postData ) {
return;
}
isPostVisibleInPreview = -1 !== _.indexOf( api.Posts.previewedQuery.get().postIds, postId );
postData.section.focus();
if ( postTypeObj['public'] && ! isPostVisibleInPreview ) {
api.previewer.previewUrl( api.Posts.getPreviewUrl( {
post_type: panel.postType,
post_id: postId
} ) );
}
if ( postData.setting.trashedPreviously && api.Notification ) {
panel.toggleTrashNotification( postData );
}
} );
ensuredPromise.always( function() {
panel.postSelectionLookupSelect2.val( null ).trigger( 'change' );
panel.postSelectionLookupSelect2.prop( 'disabled', false );
} );
} );
if ( postTypeObj.current_user_can.create_posts ) {
actionsContainer.find( '.add-new-post-stub' ).on( 'click', function( event ) {
panel.onClickAddPostButton( event );
} );
}
descriptionContainer.after( actionsContainer );
},
/**
* Query posts.
*
* @param {object} queryVars Query vars.
* @returns {jQuery.promise} Promise.
*/
queryPosts: function( queryVars ) {
var panel = this, action, data;
action = 'customize-posts-select2-query';
data = _.extend(
api.previewer.query(),
{
'customize-posts-nonce': api.settings.nonce['customize-posts'],
post_type: panel.postType
},
queryVars || {}
);
return wp.ajax.post( action, data );
},
/**
* Handle click on add post button.
*
* @param {jQuery.Event} event Event.
* @returns {void}
*/
onClickAddPostButton: function onClickAddPostButton( event ) {
var panel = this, button = $( event.target );
event.preventDefault();
api.Posts.startCreatePostFlow( {
postType: panel.postType,
initiatingButton: button,
restorePreviousUrl: false
} );
},
/**
* Allow an active panel to be contextually active even when it has no active controls.
*
* @returns {boolean} Whether contextually active.
*/
isContextuallyActive: function() {
var panel = this;
return panel.active();
},
/**
* Toggle trash notification if a post is restored or moved to trash.
*
* @param {object} postData Post data.
* @returns {void}
*/
toggleTrashNotification: function toggleTrashNotification( postData ) {
var panel = this, notification, statusControl, removeNotification;
notification = new api.Notification( panel.trashNotificationCode, {
type: 'info',
message: api.Posts.data.l10n.trashPostNotification
} );
statusControl = api.control( postData.section.id + '[post_status]' );
if ( ! statusControl || 'trash' === statusControl.setting.get().post_status ) {
return;
}
statusControl.notifications.add( panel.trashNotificationCode, notification );
removeNotification = function() {
statusControl.notifications.remove( panel.trashNotificationCode );
statusControl.setting.unbind( removeNotification );
};
statusControl.setting.bind( removeNotification );
},
/**
* Remove trash notifications from all post sections on customize save.
*
* @returns {void}
*/
removeTrashNotifications: function removeTrashNotifications() {
var panel = this, statusControl;
api.section.each( function( section ) {
if ( section.extended( api.Posts.PostSection ) ) {
statusControl = api.control( section.id + '[post_status]' );
if ( statusControl && statusControl.notifications ) {
statusControl.notifications.remove( panel.trashNotificationCode );
}
}
} );
}
});
})( wp.customize, jQuery );