PluginProbe
Filter Everything — WordPress & WooCommerce Filters / 1.2.4
Filter Everything — WordPress & WooCommerce Filters v1.2.4
1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 All 52 releases
filter-everything / assets / js / wpc-seo-rules-admin.js

wpc-seo-rules-admin.js in Filter Everything — WordPress & WooCommerce Filters 1.2.4, at assets/js/wpc-seo-rules-admin.js

303 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*!
2 * Filter Everything seo rules admin 1.2.3
3 */
4 (function($) {
5 "use strict";
6 let seoRulesFormValid = false;
7
8 function validateSeoRulesForm( $el )
9 {
10 let $spinner = $('#publishing-action .spinner');
11 let requestParams = {};
12
13 $spinner.addClass( 'is-active' );
14 /**
15 * @todo checkboxes does not validates correctly because they send the same value !!! IMPORTANT
16 * independently from checked status
17 */
18
19 requestParams.validateData = wpcSerialize( $el );
20
21 wp.ajax.post( 'wpc-validate-seo-rules', requestParams )
22 .always( function() {
23 $spinner.removeClass( 'is-active' );
24 })
25 .done( function( response ) {
26 seoRulesFormValid = true;
27 $el.submit();
28 })
29 .fail( function( response ) {
30
31 let notices = [];
32
33 if( typeof response.errors !== 'undefined' ){
34 $.each( response.errors, function ( index, error ){
35 notices.push( error.message );
36 });
37
38 if( notices.length < 1 ){
39 notices.push( 'Error: Set was not saved.' );
40 }
41
42 addNotice( notices );
43 makeNoticesDismissible();
44 }
45 });
46
47 return false;
48 }
49
50 function addNotice( messages )
51 {
52 let target = $('form#post');
53 let text = '';
54 $.each( messages, function ( index, message ) {
55 text += '<p>' + message + '</p>';
56 });
57
58 let html = '<div id="message" class="wpc-error notice notice-error is-dismissible">'
59 + text +
60 '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>' +
61 '</div>';
62 if( typeof target !== 'undefined' ){
63 if( $("#message").length > 0 ){
64 $("#message").remove();
65 }
66 target.before( html );
67 }
68 }
69
70 function removeElement($el)
71 {
72 $el.fadeTo(100, 0, function() {
73 $el.slideUp(100, function() {
74 $el.remove();
75 });
76 });
77 }
78
79 function addFieldError( fieldId, message )
80 {
81 let target = $('#'+fieldId);
82 let html = '<div class="wpc-field-notice wpc-field-notice-error"><p>'+message+'</p></div>';
83 if( typeof target !== 'undefined' ){
84 target.before( html );
85 }
86 }
87
88 $.fn.getCursorPosition = function() {
89 var input = this.get(0);
90 if (!input) return; // No (input) element found
91 if ('selectionStart' in input) {
92 // Standard-compliant browsers
93 return input.selectionStart;
94 } else if (document.selection) {
95 // IE
96 input.focus();
97 var sel = document.selection.createRange();
98 var selLen = document.selection.createRange().text.length;
99 sel.moveStart('character', -input.value.length);
100 return sel.text.length - selLen;
101 }
102 }
103
104 $.fn.createSeoVarsList = function( seoVars )
105 {
106 let html = '<ul class="wpc-seo-vars-list">';
107
108 if( Object.keys(seoVars).length > 0 ){
109 $.each( seoVars, function ( slug, label ){
110 html += '<li data-seovar="{'+slug+'}">'+label+'</li>';
111 });
112 }else{
113 html += '<li>'+wpcSeoVars.noSeoVarsMsg+'</li>';
114 }
115
116 html += '</ul>';
117 $(this).replaceWith(html);
118 return true;
119 }
120
121 $(document).mouseup(function(e)
122 {
123 let $wpcContainer = $('.wpc-vars-container');
124 if( $wpcContainer.length > 0 ){
125 // var container = $('.wpc-vars-container');
126 // if the target of the click isn't the container nor a descendant of the container
127 if (! $wpcContainer.is(e.target) && $wpcContainer.has(e.target).length === 0)
128 {
129 $wpcContainer.hide();
130 }
131 }
132 });
133
134 $(document).ready(function (){
135
136 $('form#post').on('submit', function(e){
137
138 // Clear all errors
139 removeElement( $('.wpc-field-notice') );
140
141 // Clear Notice
142 removeElement( $('#message') );
143
144 if( ! seoRulesFormValid ){
145 e.preventDefault();
146 // Validate form. We will submit it from validation method
147 validateSeoRulesForm($(this));
148 }
149 });
150
151 $('.wpc-seo-vars-list').createSeoVarsList( wpcSeoVars.seovars );
152
153 $('body').on('click', '.wpc-open-container', function (e){
154 e.preventDefault();
155 let link = $(this);
156 let fieldId = link.data('field');
157 let wpcContainer = $('#wpc-vars-container-'+fieldId );
158 $('#wpc_seo_rules-'+fieldId).focus();
159 wpcContainer.toggle();
160 });
161
162 $('body').on('focus keypress blur', '.wpc-vars-insertable', function (e){
163 let wpcPosition = $(this).getCursorPosition();
164 $(this).data('caret', wpcPosition);
165 });
166
167 $('body').on('click', '.wpc-seo-vars-list li', function (){
168 let seoVar = $(this).data('seovar');
169
170 let wpcContainer = $(this).parents('.wpc-vars-container');
171
172 if( typeof seoVar !== 'undefined' ){
173 let inputField = $('#wpc_seo_rules-'+wpcContainer.data('container'));
174 let caretPos = inputField.data('caret');
175
176 if( caretPos === '' ){
177 caretPos = inputField.val().length;
178 }
179
180 if( caretPos === 0 ){
181 seoVar = seoVar+' ';
182 }else if( caretPos === inputField.val().length ){
183 seoVar = ' '+seoVar;
184 }else{
185 seoVar = ' '+seoVar+' ';
186 }
187
188 insertAtCaret( inputField, seoVar, caretPos );
189 }
190
191 wpcContainer.hide();
192 });
193
194 $('body').on('change', '#wpc_seo_rules-rule_post_type', function (){
195 removeElement( $('.wpc-field-notice') );
196 let selected = $(this).val();
197 let $spinner = $( '.wpc-intersection-fields-wrapper' ).children( '.spinner' );
198 $spinner.addClass( 'is-active' );
199 let requestParams = {};
200 requestParams._wpnonce = $("#wpc_seo_rule_nonce").val();
201 requestParams.postType = selected;
202 requestParams.postId = $("#post_ID").val();
203
204 wp.ajax.post( 'wpc-get-indexed-filters', requestParams )
205 .always( function() {
206 $spinner.removeClass( 'is-active' );
207 })
208 .done( function( response ) {
209
210 $( '#wpc-intersections-table' ).replaceWith( response.html );
211 $('.wpc-seo-vars-list').createSeoVarsList( response.seovars );
212 })
213
214 .fail( function(response) {
215 if( typeof response !== 'undefined'){
216 addFieldError( 'wpc-intersection-fields-container', response.message );
217 }
218 });
219 });
220
221 makeNoticesDismissible();
222
223 });
224
225 function makeNoticesDismissible() {
226 $( '.wpc-error.is-dismissible' ).each( function() {
227 var $el = $( this ),
228 $button = $el.find('.notice-dismiss');
229 // Ensure plain text.
230 $button.on( 'click', function( event ) {
231 event.preventDefault();
232 $el.fadeTo( 100, 0, function() {
233 $el.slideUp( 100, function() {
234 $el.remove();
235 });
236 });
237 });
238
239 $el.append( $button );
240 });
241 }
242
243 function wpcSerialize( $el ){
244
245 var obj = {};
246 var inputs = $el.find('select, textarea, input').serializeArray();
247
248 for( var i = 0; i < inputs.length; i++ ) {
249 wpcBuildObject( obj, inputs[i].name, inputs[i].value );
250 }
251 return obj;
252 };
253
254 function wpcBuildObject( obj, name, value ){
255 name = name.replace('[]', '[%%index%%]');
256
257 var keys = name.match(/([^\[\]])+/g);
258 if( !keys ) return;
259 var length = keys.length;
260 var ref = obj;
261
262 for( var i = 0; i < length; i++ ) {
263 var key = String( keys[i] );
264 if( i == length - 1 ) {
265 if( key === '%%index%%' ) {
266 ref.push( value );
267 } else {
268 ref[ key ] = value;
269 }
270 } else {
271 if( keys[i+1] === '%%index%%' ) {
272 if( !wpcIsArray(ref[ key ]) ) {
273 ref[ key ] = [];
274 }
275 } else {
276 if( !wpcIsObject(ref[ key ]) ) {
277 ref[ key ] = {};
278 }
279 }
280 ref = ref[ key ];
281 }
282 }
283 };
284
285 function wpcIsArray( a ){
286 return Array.isArray(a);
287 };
288
289 function wpcIsObject( a ){
290 return ( typeof a === 'object' );
291 }
292
293 })(jQuery);
294
295 function insertAtCaret( target, text, caretPos )
296 {
297 let textAreaTxt = target.val();
298 let result = textAreaTxt.substring(0, caretPos) + text + textAreaTxt.substring(caretPos);
299 result = result.replace(/ +(?= )/g,'');
300 target.val(result);
301
302 return true;
303 }