fl-builder-admin-posts.js
4.7 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
(function($){
/**
* Helper class for dealing with the post edit screen.
*
* @class FLBuilderAdminPosts
* @since 1.0
* @static
*/
FLBuilderAdminPosts = {
/**
* Initializes the builder for the post edit screen.
*
* @since 1.0
* @method init
*/
init: function()
{
$('.fl-enable-editor').on('click', this._enableEditorClicked);
$('.fl-enable-builder').on('click', this._enableBuilderClicked);
$('.fl-launch-builder').on('click', this._launchBuilderClicked);
/* WPML Support */
$('#icl_cfo').on('click', this._wpmlCopyClicked);
},
/**
* Fires when the text editor button is clicked
* and switches the current post to use that
* instead of the builder.
*
* @since 1.0
* @access private
* @method _enableEditorClicked
*/
_enableEditorClicked: function()
{
if ( ! $( 'body' ).hasClass( 'fl-builder-enabled' ) ) {
return;
}
if ( confirm( FLBuilderAdminPostsStrings.switchToEditor ) ) {
$('.fl-builder-admin-tabs a').removeClass('fl-active');
$(this).addClass('fl-active');
FLBuilderAdminPosts.ajax({
action: 'fl_builder_disable',
}, FLBuilderAdminPosts._enableEditorComplete);
}
},
/**
* Callback for enabling the editor.
*
* @since 1.0
* @access private
* @method _enableEditorComplete
*/
_enableEditorComplete: function()
{
$('body').removeClass('fl-builder-enabled');
$(window).resize();
},
/**
* Callback for enabling the editor.
*
* @since 1.0
* @access private
* @method _enableBuilderClicked
*/
_enableBuilderClicked: function()
{
if($('body').hasClass('fl-builder-enabled')) {
return;
}
else {
$('.fl-builder-admin-tabs a').removeClass('fl-active');
$(this).addClass('fl-active');
FLBuilderAdminPosts._launchBuilder();
}
},
/**
* Fires when the page builder button is clicked
* and switches the current post to use that
* instead of the text editor.
*
* @since 1.0
* @access private
* @method _launchBuilderClicked
* @param {Object} e An event object.
*/
_launchBuilderClicked: function(e)
{
e.preventDefault();
FLBuilderAdminPosts._launchBuilder();
},
/**
* Callback for enabling the builder.
*
* @since 1.0
* @access private
* @method _launchBuilder
*/
_launchBuilder: function()
{
var redirect = $('.fl-launch-builder').attr('href'),
title = $('#title');
if(typeof title !== 'undefined' && title.val() === '') {
title.val('Post #' + $('#post_ID').val());
}
$(window).off('beforeunload');
$('body').addClass('fl-builder-enabled');
$('.fl-builder-loading').show();
$('form#post').append('<input type="hidden" name="fl-builder-redirect" value="' + redirect + '" />');
$('form#post').submit();
},
/**
* Fires when the WPML copy button is clicked.
*
* @since 1.1.7
* @access private
* @method _wpmlCopyClicked
* @param {Object} e An event object.
*/
_wpmlCopyClicked: function(e)
{
var originalPostId = $('#icl_translation_of').val();
if(typeof originalPostId !== 'undefined') {
$('.fl-builder-loading').show();
FLBuilderAdminPosts.ajax({
action: 'fl_builder_duplicate_wpml_layout',
original_post_id: originalPostId
}, FLBuilderAdminPosts._wpmlCopyComplete);
}
},
/**
* Callback for when the WPML copy button is clicked.
*
* @since 1.1.7
* @access private
* @method _wpmlCopyComplete
* @param {String} response The JSON encoded response.
*/
_wpmlCopyComplete: function(response)
{
response = JSON.parse(response);
$('.fl-builder-loading').hide();
if(response.has_layout && response.enabled) {
$('body').addClass('fl-builder-enabled');
}
},
/**
* Makes an AJAX request.
*
* @since 1.0
* @method ajax
* @param {Object} data An object with data to send in the request.
* @param {Function} callback A function to call when the request is complete.
*/
ajax: function(data, callback)
{
// Add the post ID to the data.
data.post_id = $('#post_ID').val();
// Show the loader.
$('.fl-builder-loading').show();
// Send the request.
$.post(ajaxurl, data, function(response) {
FLBuilderAdminPosts._ajaxComplete();
if(typeof callback !== 'undefined') {
callback.call(this, response);
}
});
},
/**
* Generic callback for when an AJAX request is complete.
*
* @since 1.0
* @access private
* @method _ajaxComplete
*/
_ajaxComplete: function()
{
$('.fl-builder-loading').hide();
}
};
/* Initializes the post edit screen. */
$(function(){
FLBuilderAdminPosts.init();
});
})(jQuery);