| 1 |
/*! |
| 2 |
* Filter Everything seo rules admin 1.9.7 |
| 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 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
function addNotice( messages ) |
| 50 |
{ |
| 51 |
let target = $('form#post'); |
| 52 |
let text = ''; |
| 53 |
$.each( messages, function ( index, message ) { |
| 54 |
text += '<p>' + message + '</p>'; |
| 55 |
}); |
| 56 |
|
| 57 |
let html = '<div id="message" class="wpc-error notice notice-error is-dismissible flrt-notice">' |
| 58 |
+ text + |
| 59 |
'<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>' + |
| 60 |
'</div>'; |
| 61 |
if( typeof target !== 'undefined' ){ |
| 62 |
if( $("#message").length > 0 ){ |
| 63 |
$("#message").remove(); |
| 64 |
} |
| 65 |
target.before( html ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function removeElement($el) |
| 70 |
{ |
| 71 |
$el.fadeTo(100, 0, function() { |
| 72 |
$el.slideUp(100, function() { |
| 73 |
$el.remove(); |
| 74 |
}); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
function addFieldError( fieldId, message ) |
| 79 |
{ |
| 80 |
let target = $('#'+fieldId); |
| 81 |
let html = '<div class="wpc-field-notice wpc-field-notice-error"><p>'+message+'</p></div>'; |
| 82 |
if( typeof target !== 'undefined' ){ |
| 83 |
target.before( html ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$.fn.getCursorPosition = function() { |
| 88 |
var input = this.get(0); |
| 89 |
if (!input) return; // No (input) element found |
| 90 |
if ('selectionStart' in input) { |
| 91 |
// Standard-compliant browsers |
| 92 |
return input.selectionStart; |
| 93 |
} else if (document.selection) { |
| 94 |
// IE |
| 95 |
input.focus(); |
| 96 |
var sel = document.selection.createRange(); |
| 97 |
var selLen = document.selection.createRange().text.length; |
| 98 |
sel.moveStart('character', -input.value.length); |
| 99 |
return sel.text.length - selLen; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$.fn.createSeoVarsList = function( seoVars ) |
| 104 |
{ |
| 105 |
let html = '<ul class="wpc-seo-vars-list">'; |
| 106 |
|
| 107 |
if( Object.keys(seoVars).length > 0 ){ |
| 108 |
$.each( seoVars, function ( slug, label ){ |
| 109 |
html += '<li data-seovar="{'+slug+'}">'+label+'</li>'; |
| 110 |
}); |
| 111 |
}else{ |
| 112 |
html += '<li>'+wpcSeoVars.noSeoVarsMsg+'</li>'; |
| 113 |
} |
| 114 |
|
| 115 |
html += '</ul>'; |
| 116 |
$(this).replaceWith(html); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
$(document).mouseup(function(e) |
| 121 |
{ |
| 122 |
let $wpcContainer = $('.wpc-vars-container'); |
| 123 |
if( $wpcContainer.length > 0 ){ |
| 124 |
// var container = $('.wpc-vars-container'); |
| 125 |
// if the target of the click isn't the container nor a descendant of the container |
| 126 |
if (! $wpcContainer.is(e.target) && $wpcContainer.has(e.target).length === 0) |
| 127 |
{ |
| 128 |
$wpcContainer.hide(); |
| 129 |
} |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
$(document).ready(function (){ |
| 134 |
|
| 135 |
$('form#post').on('submit', function(e){ |
| 136 |
|
| 137 |
// Clear all errors |
| 138 |
removeElement( $('.wpc-field-notice') ); |
| 139 |
|
| 140 |
// Clear Notice |
| 141 |
removeElement( $('#message') ); |
| 142 |
|
| 143 |
if( ! seoRulesFormValid ){ |
| 144 |
e.preventDefault(); |
| 145 |
// Validate form. We will submit it from validation method |
| 146 |
validateSeoRulesForm($(this)); |
| 147 |
} |
| 148 |
}); |
| 149 |
|
| 150 |
$('.wpc-seo-vars-list').createSeoVarsList( wpcSeoVars.seovars ); |
| 151 |
|
| 152 |
$('body').on('click', '.wpc-open-container', function (e){ |
| 153 |
e.preventDefault(); |
| 154 |
let link = $(this); |
| 155 |
let fieldId = link.data('field'); |
| 156 |
let wpcContainer = $('#wpc-vars-container-'+fieldId ); |
| 157 |
$('#wpc_seo_rules-'+fieldId).focus(); |
| 158 |
wpcContainer.toggle(); |
| 159 |
}); |
| 160 |
|
| 161 |
$('body').on('focus keypress blur', '.wpc-vars-insertable', function (e){ |
| 162 |
let wpcPosition = $(this).getCursorPosition(); |
| 163 |
$(this).data('caret', wpcPosition); |
| 164 |
}); |
| 165 |
|
| 166 |
$('body').on('click', '.wpc-seo-vars-list li', function (){ |
| 167 |
let seoVar = $(this).data('seovar'); |
| 168 |
|
| 169 |
let wpcContainer = $(this).parents('.wpc-vars-container'); |
| 170 |
|
| 171 |
if( typeof seoVar !== 'undefined' ){ |
| 172 |
let inputField = $('#wpc_seo_rules-'+wpcContainer.data('container')); |
| 173 |
let caretPos = inputField.data('caret'); |
| 174 |
|
| 175 |
if( caretPos === '' ){ |
| 176 |
caretPos = inputField.val().length; |
| 177 |
} |
| 178 |
|
| 179 |
if( caretPos === 0 ){ |
| 180 |
seoVar = seoVar+' '; |
| 181 |
}else if( caretPos === inputField.val().length ){ |
| 182 |
seoVar = ' '+seoVar; |
| 183 |
}else{ |
| 184 |
seoVar = ' '+seoVar+' '; |
| 185 |
} |
| 186 |
|
| 187 |
insertAtCaret( inputField, seoVar, caretPos ); |
| 188 |
} |
| 189 |
|
| 190 |
wpcContainer.hide(); |
| 191 |
}); |
| 192 |
|
| 193 |
$('body').on('change', '#wpc_seo_rules-rule_post_type', function (){ |
| 194 |
removeElement( $('.wpc-field-notice') ); |
| 195 |
let selected = $(this).val(); |
| 196 |
let $spinner = $( '.wpc-intersection-fields-wrapper' ).children( '.spinner' ); |
| 197 |
$spinner.addClass( 'is-active' ); |
| 198 |
let requestParams = {}; |
| 199 |
requestParams._wpnonce = $("#wpc_seo_rule_nonce").val(); |
| 200 |
requestParams.postType = selected; |
| 201 |
requestParams.postId = $("#post_ID").val(); |
| 202 |
|
| 203 |
wp.ajax.post( 'wpc-get-indexed-filters', requestParams ) |
| 204 |
.always( function() { |
| 205 |
$spinner.removeClass( 'is-active' ); |
| 206 |
}) |
| 207 |
.done( function( response ) { |
| 208 |
|
| 209 |
$( '#wpc-intersections-table' ).replaceWith( response.html ); |
| 210 |
$('.wpc-seo-vars-list').createSeoVarsList( response.seovars ); |
| 211 |
}) |
| 212 |
|
| 213 |
.fail( function(response) { |
| 214 |
if( typeof response !== 'undefined'){ |
| 215 |
addFieldError( 'wpc-intersection-fields-container', response.message ); |
| 216 |
} |
| 217 |
}); |
| 218 |
}); |
| 219 |
}); |
| 220 |
|
| 221 |
function wpcSerialize( $el ){ |
| 222 |
|
| 223 |
var obj = {}; |
| 224 |
var inputs = $el.find('select, textarea, input').serializeArray(); |
| 225 |
|
| 226 |
for( var i = 0; i < inputs.length; i++ ) { |
| 227 |
wpcBuildObject( obj, inputs[i].name, inputs[i].value ); |
| 228 |
} |
| 229 |
return obj; |
| 230 |
}; |
| 231 |
|
| 232 |
function wpcBuildObject( obj, name, value ){ |
| 233 |
name = name.replace('[]', '[%%index%%]'); |
| 234 |
|
| 235 |
var keys = name.match(/([^\[\]])+/g); |
| 236 |
if( !keys ) return; |
| 237 |
var length = keys.length; |
| 238 |
var ref = obj; |
| 239 |
|
| 240 |
for( var i = 0; i < length; i++ ) { |
| 241 |
var key = String( keys[i] ); |
| 242 |
if( i == length - 1 ) { |
| 243 |
if( key === '%%index%%' ) { |
| 244 |
ref.push( value ); |
| 245 |
} else { |
| 246 |
ref[ key ] = value; |
| 247 |
} |
| 248 |
} else { |
| 249 |
if( keys[i+1] === '%%index%%' ) { |
| 250 |
if( !wpcIsArray(ref[ key ]) ) { |
| 251 |
ref[ key ] = []; |
| 252 |
} |
| 253 |
} else { |
| 254 |
if( !wpcIsObject(ref[ key ]) ) { |
| 255 |
ref[ key ] = {}; |
| 256 |
} |
| 257 |
} |
| 258 |
ref = ref[ key ]; |
| 259 |
} |
| 260 |
} |
| 261 |
}; |
| 262 |
|
| 263 |
function wpcIsArray( a ){ |
| 264 |
return Array.isArray(a); |
| 265 |
}; |
| 266 |
|
| 267 |
function wpcIsObject( a ){ |
| 268 |
return ( typeof a === 'object' ); |
| 269 |
} |
| 270 |
|
| 271 |
})(jQuery); |
| 272 |
|
| 273 |
function insertAtCaret( target, text, caretPos ) |
| 274 |
{ |
| 275 |
let textAreaTxt = target.val(); |
| 276 |
let result = textAreaTxt.substring(0, caretPos) + text + textAreaTxt.substring(caretPos); |
| 277 |
result = result.replace(/ +(?= )/g,''); |
| 278 |
target.val(result); |
| 279 |
|
| 280 |
return true; |
| 281 |
} |