first commit
This commit is contained in:
34
wp-content/plugins/weglot/app/javascripts/filter-polyfill.js
Normal file
34
wp-content/plugins/weglot/app/javascripts/filter-polyfill.js
Normal file
@@ -0,0 +1,34 @@
|
||||
if (!Array.prototype.filter) {
|
||||
Array.prototype.filter = function (func, thisArg) {
|
||||
'use strict';
|
||||
if (!((typeof func === 'Function' || typeof func === 'function') && this))
|
||||
throw new TypeError();
|
||||
|
||||
var len = this.length >>> 0,
|
||||
res = new Array(len), // preallocate array
|
||||
t = this, c = 0, i = -1;
|
||||
if (thisArg === undefined) {
|
||||
while (++i !== len) {
|
||||
// checks to see if the key was set
|
||||
if (i in this) {
|
||||
if (func(t[i], i, t)) {
|
||||
res[c++] = t[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (++i !== len) {
|
||||
// checks to see if the key was set
|
||||
if (i in this) {
|
||||
if (func.call(thisArg, t[i], i, t)) {
|
||||
res[c++] = t[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.length = c; // shrink down array to proper size
|
||||
return res;
|
||||
};
|
||||
}
|
||||
46
wp-content/plugins/weglot/app/javascripts/find-polyfill.js
Normal file
46
wp-content/plugins/weglot/app/javascripts/find-polyfill.js
Normal file
@@ -0,0 +1,46 @@
|
||||
// https://tc39.github.io/ecma262/#sec-array.prototype.find
|
||||
if (!Array.prototype.find) {
|
||||
Object.defineProperty(Array.prototype, 'find', {
|
||||
value: function (predicate) {
|
||||
// 1. Let O be ? ToObject(this value).
|
||||
if (this == null) {
|
||||
throw new TypeError('"this" is null or not defined');
|
||||
}
|
||||
|
||||
var o = Object(this);
|
||||
|
||||
// 2. Let len be ? ToLength(? Get(O, "length")).
|
||||
var len = o.length >>> 0;
|
||||
|
||||
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
|
||||
if (typeof predicate !== 'function') {
|
||||
throw new TypeError('predicate must be a function');
|
||||
}
|
||||
|
||||
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
|
||||
var thisArg = arguments[1];
|
||||
|
||||
// 5. Let k be 0.
|
||||
var k = 0;
|
||||
|
||||
// 6. Repeat, while k < len
|
||||
while (k < len) {
|
||||
// a. Let Pk be ! ToString(k).
|
||||
// b. Let kValue be ? Get(O, Pk).
|
||||
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
|
||||
// d. If testResult is true, return kValue.
|
||||
var kValue = o[k];
|
||||
if (predicate.call(thisArg, kValue, k, o)) {
|
||||
return kValue;
|
||||
}
|
||||
// e. Increase k by 1.
|
||||
k++;
|
||||
}
|
||||
|
||||
// 7. Return undefined.
|
||||
return undefined;
|
||||
},
|
||||
configurable: true,
|
||||
writable: true
|
||||
});
|
||||
}
|
||||
271
wp-content/plugins/weglot/app/javascripts/front.js
Normal file
271
wp-content/plugins/weglot/app/javascripts/front.js
Normal file
@@ -0,0 +1,271 @@
|
||||
//find and place wg-ajax-button-switcher
|
||||
|
||||
function switcherPlacement() {
|
||||
const button_switcher_ajax = document.querySelectorAll(".weglot-custom-switcher-ajax")
|
||||
Array.prototype.forEach.call(button_switcher_ajax, function (el, i) {
|
||||
let button_sibling = null;
|
||||
let button_target = null;
|
||||
let targetSelector = null;
|
||||
let siblingSelector = null;
|
||||
|
||||
if (el.getAttribute('data-wg-target') !== '') {
|
||||
targetSelector = el.getAttribute('data-wg-target');
|
||||
}
|
||||
|
||||
if (el.getAttribute('data-wg-sibling') !== '') {
|
||||
siblingSelector = el.getAttribute('data-wg-sibling');
|
||||
}
|
||||
|
||||
if (targetSelector) {
|
||||
button_target = document.querySelector(targetSelector)
|
||||
}
|
||||
if (siblingSelector) {
|
||||
button_sibling = document.querySelector(siblingSelector)
|
||||
}
|
||||
|
||||
if (button_target != null && button_sibling != null) {
|
||||
button_target.insertBefore(el, button_sibling)
|
||||
el.classList.remove("weglot-custom-switcher-ajax")
|
||||
} else if (button_target && button_sibling == null) {
|
||||
button_target.insertBefore(el, button_target.firstChild)
|
||||
el.classList.remove("weglot-custom-switcher-ajax")
|
||||
} else if (button_sibling && button_target == null) {
|
||||
button_sibling.parentNode.insertBefore(el, button_sibling)
|
||||
el.classList.remove("weglot-custom-switcher-ajax")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//detect iframe
|
||||
function inFrame() {
|
||||
try {
|
||||
return window.frameElement || window.self !== window.top;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener( "DOMContentLoaded", () => switcherPlacement() );
|
||||
} else {
|
||||
switcherPlacement();
|
||||
}
|
||||
}, 1500);
|
||||
|
||||
document.addEventListener( "DOMContentLoaded", function ( event ) {
|
||||
|
||||
function getOffset(element) {
|
||||
let top = 0, left = 0;
|
||||
do {
|
||||
top += element.offsetTop || 0;
|
||||
left += element.offsetLeft || 0;
|
||||
element = element.offsetParent;
|
||||
} while (element);
|
||||
|
||||
return {
|
||||
top: top, left: left
|
||||
};
|
||||
}
|
||||
|
||||
const button = document.querySelector(".country-selector");
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
const h = getOffset(button).top;
|
||||
const body = document.body, html = document.documentElement;
|
||||
const page_height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
|
||||
|
||||
const position = window.getComputedStyle(button).getPropertyValue("position");
|
||||
const bottom = window.getComputedStyle(button).getPropertyValue("bottom");
|
||||
const top = window.getComputedStyle(button).getPropertyValue("top");
|
||||
|
||||
if ((position !== "fixed" && h > page_height / 2) || (position === "fixed" && h > 100)) {
|
||||
button.className += " weglot-invert";
|
||||
}
|
||||
|
||||
//check if your page is load by an iframe
|
||||
if (inFrame()) {
|
||||
const switchers = document.querySelectorAll('.weglot-dropdown')
|
||||
if (switchers !== null) {
|
||||
[].forEach.call(switchers, function (switcher) {
|
||||
switcher.style.display = "none";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("click", (evt) => {
|
||||
let targetEl = evt.target;
|
||||
|
||||
if(targetEl.closest('.country-selector.close_outside_click') == null){
|
||||
document.querySelectorAll(".country-selector.close_outside_click.weglot-dropdown input").forEach( (node) => {
|
||||
node.checked = false
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
const asides = document.getElementsByClassName("country-selector");
|
||||
const isOpen = link => !link.className.includes("closed");
|
||||
let focusedLang;
|
||||
if (asides.length > 0) {
|
||||
const selectedLang = document.getElementsByClassName("wgcurrent");
|
||||
for (let aside of asides) {
|
||||
|
||||
// accessiblity button
|
||||
const KEYCODE = {
|
||||
ENTER: 13, ESCAPE: 27, ARROWUP: 38, ARROWDOWN: 40,
|
||||
};
|
||||
|
||||
const isOpenUp = () => {
|
||||
// If switcher is in second half of page, set weg-openup class
|
||||
const {bottom = 0} = aside.getBoundingClientRect();
|
||||
return bottom > window.innerHeight / 2;
|
||||
};
|
||||
|
||||
const openSwitcher = () => {
|
||||
aside.classList.remove("closed");
|
||||
document.querySelectorAll(".country-selector.weglot-dropdown input").checked = true;
|
||||
aside.setAttribute("aria-expanded", "true");
|
||||
};
|
||||
|
||||
const closeSwitcher = () => {
|
||||
aside.classList.add("closed");
|
||||
document.querySelectorAll(".country-selector.weglot-dropdown input").checked = false
|
||||
aside.setAttribute("aria-expanded", "false");
|
||||
if (focusedLang) {
|
||||
focusedLang.classList.remove("focus");
|
||||
focusedLang = null;
|
||||
}
|
||||
};
|
||||
|
||||
const setAriaLabel = code => {
|
||||
const fullNameLang = getLangNameFromCode(code);
|
||||
//aside.setAttribute("aria-activedescendant", "weglot-language-" + code);
|
||||
aside.setAttribute("aria-label", "Language selected: " + code);
|
||||
};
|
||||
|
||||
const toggleSwitcher = () => {
|
||||
if (aside.classList.contains("closed")) {
|
||||
openSwitcher();
|
||||
} else {
|
||||
closeSwitcher();
|
||||
}
|
||||
if (focusedLang) {
|
||||
focusedLang.classList.remove("focus");
|
||||
}
|
||||
focusedLang = null;
|
||||
};
|
||||
|
||||
// Toggle when focused and keydown ENTER.
|
||||
aside.addEventListener("keydown", event => {
|
||||
if (event.keyCode === KEYCODE.ENTER) {
|
||||
//event.preventDefault();
|
||||
//selectedLang.click();
|
||||
for (var i = 0; i < selectedLang.length; i++) {
|
||||
selectedLang[i].click();
|
||||
}
|
||||
if (focusedLang) {
|
||||
const destinationLanguage = focusedLang.getAttribute("data-l");
|
||||
setAriaLabel(destinationLanguage);
|
||||
aside.focus();
|
||||
}
|
||||
toggleSwitcher();
|
||||
return;
|
||||
}
|
||||
if (event.keyCode === KEYCODE.ARROWDOWN || event.keyCode === KEYCODE.ARROWUP) {
|
||||
event.preventDefault();
|
||||
moveFocus(event.keyCode);
|
||||
return;
|
||||
}
|
||||
if (event.keyCode === KEYCODE.ESCAPE && isOpen(aside)) {
|
||||
// Close switcher.
|
||||
event.preventDefault();
|
||||
closeSwitcher();
|
||||
aside.focus();
|
||||
}
|
||||
});
|
||||
|
||||
aside.addEventListener("mousedown", event => {
|
||||
if (focusedLang) {
|
||||
const destinationLanguage = focusedLang.getAttribute("data-l");
|
||||
setAriaLabel(destinationLanguage);
|
||||
aside.focus();
|
||||
}
|
||||
toggleSwitcher();
|
||||
return;
|
||||
});
|
||||
|
||||
if(aside.className.includes("open_hover")){
|
||||
aside.addEventListener("mouseenter", event => {
|
||||
if (focusedLang) {
|
||||
const destinationLanguage = focusedLang.getAttribute("data-l");
|
||||
setAriaLabel(destinationLanguage);
|
||||
aside.focus();
|
||||
}
|
||||
toggleSwitcher();
|
||||
aside.querySelector("input.weglot_choice").checked = true
|
||||
return;
|
||||
});
|
||||
|
||||
aside.addEventListener("mouseleave", event => {
|
||||
if (focusedLang) {
|
||||
const destinationLanguage = focusedLang.getAttribute("data-l");
|
||||
setAriaLabel(destinationLanguage);
|
||||
aside.focus();
|
||||
}
|
||||
toggleSwitcher();
|
||||
aside.querySelector("input.weglot_choice").checked = false
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const moveFocus = keyCode => {
|
||||
const direction = keyCode === KEYCODE.ARROWDOWN ? "nextSibling" : "previousSibling";
|
||||
const openUp = isOpenUp();
|
||||
|
||||
if (!focusedLang || !isOpen(aside)) {
|
||||
// Focus the first or last language
|
||||
const selector = openUp ? "ul li.wg-li:last-child" : "ul li.wg-li";
|
||||
|
||||
for (var i = 0; i < selectedLang.length; i++) {
|
||||
//selectedLang[i].click();
|
||||
focusedLang = selectedLang[i].parentNode.querySelector(selector);
|
||||
}
|
||||
|
||||
if (!focusedLang) {
|
||||
return;
|
||||
}
|
||||
focusedLang.classList.add("focus");
|
||||
focusedLang.childNodes[0].focus();
|
||||
focusedLang.scrollIntoView({block: "center"});
|
||||
|
||||
// if right direction, open it
|
||||
const needToOpen = (keyCode === KEYCODE.ARROWUP && openUp) || (keyCode === KEYCODE.ARROWDOWN && !openUp);
|
||||
if (!isOpen(aside) && needToOpen) {
|
||||
openSwitcher();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Focus next or prev language, if exists
|
||||
if (!focusedLang[direction]) {
|
||||
// if last element, close it
|
||||
if ((keyCode === KEYCODE.ARROWUP && !openUp) || (keyCode === KEYCODE.ARROWDOWN && openUp)) {
|
||||
closeSwitcher();
|
||||
aside.focus();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
focusedLang.classList.remove("focus");
|
||||
focusedLang = focusedLang[direction];
|
||||
focusedLang.classList.add("focus");
|
||||
focusedLang.childNodes[0].focus();
|
||||
focusedLang.scrollIntoView({block: "center"});
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
17
wp-content/plugins/weglot/app/javascripts/index.js
Normal file
17
wp-content/plugins/weglot/app/javascripts/index.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import InitAdminSelect from './settings/admin-select'
|
||||
import InitAdminWeglotBox from './settings/admin-weglot-box'
|
||||
import InitAdminButtonPreview from './settings/admin-button-preview'
|
||||
import InitAdminCheckApiKey from './settings/admin-check-api-key'
|
||||
import initAdminCodeEditor from './settings/admin-code-editor'
|
||||
import InitAdminChangeCountry from './settings/admin-change-country'
|
||||
import InitAdminPrivateMode from './settings/admin-private-mode'
|
||||
import './find-polyfill'
|
||||
import './filter-polyfill'
|
||||
|
||||
InitAdminSelect()
|
||||
InitAdminWeglotBox();
|
||||
InitAdminButtonPreview();
|
||||
InitAdminCheckApiKey();
|
||||
initAdminCodeEditor();
|
||||
InitAdminChangeCountry();
|
||||
InitAdminPrivateMode();
|
||||
@@ -0,0 +1,105 @@
|
||||
import slugify from 'slugify'
|
||||
|
||||
const init_url_translate = () => {
|
||||
const $ = jQuery;
|
||||
|
||||
const execute = () => {
|
||||
let old_text = {}
|
||||
|
||||
const edit_weglot_post_name = function(e) {
|
||||
const code = $(this).data('lang')
|
||||
const post_name = slugify($(`#lang-${code}`).val(), {
|
||||
lower: true,
|
||||
replacement: '-'
|
||||
});
|
||||
|
||||
$(`#text-edit-${code}`).text( post_name );
|
||||
|
||||
$(`#lang-${code}`).hide();
|
||||
$(this).hide()
|
||||
$(`.button-weglot-lang[data-lang=${code}]`).show()
|
||||
|
||||
$.ajax({
|
||||
url: ajaxurl,
|
||||
method: "POST",
|
||||
data: {
|
||||
action: "weglot_post_name",
|
||||
lang: code,
|
||||
id: $("#weglot_post_id").data('id'),
|
||||
post_name: post_name
|
||||
},
|
||||
success: function(res) {
|
||||
if(res.data && res.data.code && res.data.code === 'same_post_name'){
|
||||
$(`#text-edit-${code}`).text(old_text[code]);
|
||||
$(`#lang-${code}`).val('');
|
||||
return
|
||||
}
|
||||
else if (res.data && res.data.code && res.data.code ==='not_available'){
|
||||
$(`#weglot_permalink_not_available_${code}`).show();
|
||||
$(`#lang-${code}`).val("");
|
||||
setTimeout(() => {
|
||||
$(`#weglot_permalink_not_available_${code}`).hide();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
$(`#text-edit-${code}`).text(res.data.result.slug);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(".button-weglot-lang").each((key, itm) => {
|
||||
$(itm).on('click', function (e) {
|
||||
e.preventDefault()
|
||||
|
||||
const code = $(this).data('lang')
|
||||
const text = $(`#text-edit-${code}`).text();
|
||||
old_text[code] = text
|
||||
|
||||
$(`#text-edit-${code}`).text(' ');
|
||||
$(`#lang-${code}`).val(text).show();
|
||||
$(`.button-weglot-lang-submit[data-lang=${code}]`).show();
|
||||
$(this).hide()
|
||||
})
|
||||
|
||||
const code = $(itm).data('lang')
|
||||
|
||||
$(`.button-weglot-lang-submit[data-lang=${code}]`)
|
||||
.on("click", edit_weglot_post_name);
|
||||
})
|
||||
|
||||
$(".weglot_reset").each((key, itm) => {
|
||||
$(itm).on("click", function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const code = $(this).data("lang");
|
||||
const custom_url = $(this).attr('href')
|
||||
const id = $(this).data('id')
|
||||
|
||||
$.ajax({
|
||||
url: ajaxurl,
|
||||
method: "POST",
|
||||
data: {
|
||||
action: "weglot_reset_custom_url",
|
||||
code_lang: code,
|
||||
id: id,
|
||||
custom_url: custom_url
|
||||
},
|
||||
success: function(res) {
|
||||
$(`#text-edit-${code}`).text(
|
||||
res.data.result.slug
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
execute();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export default init_url_translate
|
||||
63
wp-content/plugins/weglot/app/javascripts/nav.js
Normal file
63
wp-content/plugins/weglot/app/javascripts/nav.js
Normal file
@@ -0,0 +1,63 @@
|
||||
jQuery(document).ready(function ($) {
|
||||
$('#update-nav-menu').bind('click', function (e) {
|
||||
|
||||
if (e.target && e.target.className && -1 != e.target.className.indexOf('item-edit')) {
|
||||
|
||||
$("input[value='#weglot_switcher'][type=text]").parents('.menu-item-settings').each(function () {
|
||||
const id = $(this).attr('id').substring(19);
|
||||
$(this).children('p:not( .field-move )').remove(); // remove default fields we don't need
|
||||
|
||||
$(this).append($('<input>').attr({ // phpcs:ignore
|
||||
type: 'hidden',
|
||||
id: 'edit-menu-item-title-' + id,
|
||||
name: 'menu-item-title[' + id + ']',
|
||||
value: weglot_data.title
|
||||
}));
|
||||
|
||||
$(this).append($("<input>").attr({ // phpcs:ignore
|
||||
type: "hidden",
|
||||
id: "edit-menu-item-url-" + id,
|
||||
name: "menu-item-url[" + id + "]",
|
||||
value: "#weglot_switcher"
|
||||
}));
|
||||
|
||||
$(this).append($('<input>').attr({ // phpcs:ignore
|
||||
type: 'hidden',
|
||||
id: 'edit-menu-item-weglot-detect-' + id,
|
||||
name: 'menu-item-weglot-detect[' + id + ']',
|
||||
value: 1
|
||||
}));
|
||||
|
||||
|
||||
$.each(weglot_data.list_options, (key, option) => {
|
||||
const paragraph = $("<p>").attr("class", "description");
|
||||
const label = $("<label>")
|
||||
.attr("for", `edit-menu-item-${option.key}-${id}`)
|
||||
.text(` ${option.title}`);
|
||||
|
||||
$(this).prepend(paragraph); // phpcs:ignore
|
||||
paragraph.append(label); // phpcs:ignore
|
||||
|
||||
const checkbox = $("<input>").attr({
|
||||
type: "checkbox",
|
||||
id: `edit-menu-item-${
|
||||
option.key
|
||||
}-${id}`,
|
||||
name: `menu-item-weglot-${
|
||||
option.key
|
||||
}[${id}]`,
|
||||
value: 1
|
||||
});
|
||||
|
||||
|
||||
if (weglot_data.options && weglot_data.options[`menu-item-${id}`] && weglot_data.options[`menu-item-${id}`][ option.key ] === 1 ){
|
||||
checkbox.prop("checked", true);
|
||||
}
|
||||
|
||||
label.prepend(checkbox); // phpcs:ignore
|
||||
})
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
3
wp-content/plugins/weglot/app/javascripts/selectize.js
Normal file
3
wp-content/plugins/weglot/app/javascripts/selectize.js
Normal file
File diff suppressed because one or more lines are too long
@@ -0,0 +1,114 @@
|
||||
const init_admin_button_preview = function () {
|
||||
const $ = jQuery
|
||||
|
||||
const execute = () => {
|
||||
// Init old type flags
|
||||
let old_type_flags = $("#type_flags option:selected").data('value')
|
||||
|
||||
let destination_languages = []
|
||||
destination_languages.push($(".country-selector label").data("code-language"));
|
||||
$(".country-selector li").each((key, itm) => {
|
||||
destination_languages.push($(itm).data("code-language"));
|
||||
})
|
||||
|
||||
const weglot_desination_languages = weglot_languages.available.filter(itm => {
|
||||
return destination_languages.indexOf(itm.external_code) >= 0;
|
||||
})
|
||||
|
||||
$("#weglot-css-inline").text(weglot_css.inline);
|
||||
|
||||
// Change dropdown
|
||||
$("#is_dropdown").on("change", function(){
|
||||
$(".country-selector").toggleClass("weglot-inline");
|
||||
$(".country-selector").toggleClass("weglot-dropdown");
|
||||
})
|
||||
|
||||
// Change with flags
|
||||
$("#with_flags").on("change", function() {
|
||||
$(".country-selector label, .country-selector li").toggleClass("weglot-flags");
|
||||
});
|
||||
|
||||
// Change type flags
|
||||
$("#type_flags").on("change", function(e) {
|
||||
$(".country-selector label, .country-selector li").removeClass(`flag-${old_type_flags}`);
|
||||
const new_type_flags = $(':selected', this).data('value')
|
||||
$(".country-selector label, .country-selector li").addClass(`flag-${new_type_flags}`);
|
||||
old_type_flags = new_type_flags;
|
||||
});
|
||||
|
||||
const set_languages = () => {
|
||||
const label_language = weglot_desination_languages.find(
|
||||
(itm) => itm.external_code === $(".country-selector label").data("code-language")
|
||||
);
|
||||
const is_fullname = $("#is_fullname").is(":checked");
|
||||
|
||||
const label = is_fullname ? label_language.local : label_language.internal_code.toUpperCase();
|
||||
|
||||
$(".country-selector label a, .country-selector label span").text(label);
|
||||
|
||||
$(".country-selector li").each((key, itm) => {
|
||||
const li_language = weglot_desination_languages.find(
|
||||
(lang) => lang.internal_code === $(itm).data("code-language")
|
||||
);
|
||||
|
||||
const label = is_fullname ? li_language.local : li_language.internal_code.toUpperCase();
|
||||
|
||||
$(itm)
|
||||
.find("a")
|
||||
.text(label);
|
||||
})
|
||||
}
|
||||
|
||||
// Change with name
|
||||
$("#with_name").on("change", function(e) {
|
||||
if (e.target.checked) {
|
||||
set_languages();
|
||||
} else {
|
||||
$(".country-selector label a, .country-selector label span").text("");
|
||||
$(".country-selector li a, .country-selector li span").each(
|
||||
(key, itm) => {
|
||||
$(itm).text("");
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
$("#is_fullname").on("change", function(e){
|
||||
if ( !$("#with_name").is(":checked") ) {
|
||||
return
|
||||
}
|
||||
|
||||
if (e.target.checked) {
|
||||
set_languages();
|
||||
}
|
||||
else {
|
||||
const label_language = weglot_desination_languages.find(itm => itm.internal_code === $(".country-selector label").data("code-language"));
|
||||
|
||||
$(".country-selector label a, .country-selector label span").text(label_language.internal_code.toUpperCase());
|
||||
$(".country-selector li").each((key, itm) => {
|
||||
const language = weglot_desination_languages.find(lang => lang.internal_code === $(itm).data("code-language"));
|
||||
|
||||
$(itm).find("a").text(language.internal_code.toUpperCase());
|
||||
$(itm).find("span").text(language.internal_code.toUpperCase());
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$("#override_css").on("keyup", function(e) {
|
||||
$("#weglot-css-inline").text(e.target.value);
|
||||
})
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
if ($(".weglot-preview").length === 0){
|
||||
return
|
||||
}
|
||||
|
||||
execute();
|
||||
})
|
||||
}
|
||||
|
||||
export default init_admin_button_preview;
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
const init_admin_change_country = function() {
|
||||
const $ = jQuery;
|
||||
|
||||
if(typeof weglot_css !== "undefined"){
|
||||
$("#weglot-css-flag-css").text(weglot_css.flag_css);
|
||||
}
|
||||
|
||||
function refresh_flag_css() {
|
||||
var en_flags = new Array();
|
||||
var es_flags = new Array();
|
||||
var fr_flags = new Array();
|
||||
var ar_flags = new Array();
|
||||
var tw_flags = new Array();
|
||||
var zh_flags = new Array();
|
||||
var pt_flags = new Array();
|
||||
|
||||
en_flags[1] = [3570, 7841, 48, 2712];
|
||||
en_flags[2] = [3720, 449, 3048, 4440];
|
||||
en_flags[3] = [3840, 1281, 2712, 4224];
|
||||
en_flags[4] = [3240, 5217, 1224, 2112];
|
||||
en_flags[5] = [4050, 3585, 1944, 2496];
|
||||
en_flags[6] = [2340, 3457, 2016, 2016];
|
||||
|
||||
es_flags[1] = [4320, 4641, 3144, 3552];
|
||||
es_flags[2] = [3750, 353, 2880, 4656];
|
||||
es_flags[3] = [4200, 1601, 2568, 3192];
|
||||
es_flags[4] = [3990, 5793, 1032, 2232];
|
||||
es_flags[5] = [5460, 897, 4104, 3120];
|
||||
es_flags[6] = [3810, 7905, 216, 3888];
|
||||
es_flags[7] = [3630, 8065, 192, 2376];
|
||||
es_flags[8] = [3780, 1473, 2496, 4104];
|
||||
es_flags[9] = [6120, 2145, 4680, 2568];
|
||||
es_flags[10] = [4440, 3009, 3240, 1176];
|
||||
es_flags[11] = [5280, 1825, 3936, 2976];
|
||||
es_flags[12] = [4770, 2081, 3624, 1008];
|
||||
es_flags[13] = [4080, 3201, 2160, 2544];
|
||||
es_flags[14] = [4590, 5761, 3432, 624];
|
||||
es_flags[15] = [4350, 2209, 3360, 2688];
|
||||
es_flags[16] = [5610, 5249, 3168, 528];
|
||||
es_flags[17] = [5070, 1729, 3792, 2952];
|
||||
es_flags[18] = [6870, 5953, 96, 3408];
|
||||
es_flags[19] = [4020, 5697, 1056, 1224];
|
||||
|
||||
fr_flags[1] = [2760, 736, 2856, 4416];
|
||||
fr_flags[2] = [3840, 1280, 2712, 4224];
|
||||
fr_flags[3] = [5700, 7201, 5016, 2400];
|
||||
fr_flags[4] = [2220, 4160, 1632, 1944];
|
||||
|
||||
ar_flags[1] = [1830, 129, 3096, 5664];
|
||||
ar_flags[2] = [5100, 2177, 3840, 2904];
|
||||
ar_flags[3] = [4890, 3425, 3648, 2136];
|
||||
ar_flags[4] = [1320, 3681, 1896, 4080];
|
||||
ar_flags[5] = [1260, 3841, 1824, 1200];
|
||||
ar_flags[6] = [1020, 3969, 1608, 312];
|
||||
ar_flags[7] = [4800, 4065, 3600, 72];
|
||||
ar_flags[8] = [4710, 4865, 3504, 480];
|
||||
ar_flags[9] = [6720, 5984, 5112, 3792];
|
||||
ar_flags[10] = [4500, 7233, 3288, 1800];
|
||||
ar_flags[11] = [720, 7522, 384, 3936];
|
||||
ar_flags[12] = [690, 7745, 336, 1104];
|
||||
ar_flags[13] = [600, 8225, 120, 1272];
|
||||
ar_flags[14] = [660, 5569, 840, 576];
|
||||
|
||||
tw_flags[1] = [3690, 1505, 2592, 3240]; // China
|
||||
tw_flags[2] = [3600, 3233, 2112, 48]; // Hong Kong
|
||||
|
||||
zh_flags[1] = [2970, 6369, 3408, 4008]; // Taiwan
|
||||
zh_flags[2] = [3600, 3233, 2112, 48]; // Hong Kong
|
||||
|
||||
pt_flags[1] = [6630, 993, 2784, 4344];
|
||||
|
||||
var enval = $("select.flag-en-type").val();
|
||||
var esval = $("select.flag-es-type").val();
|
||||
var frval = $("select.flag-fr-type").val();
|
||||
var arval = $("select.flag-ar-type").val();
|
||||
var twval = $("select.flag-tw-type").val();
|
||||
var zhval = $("select.flag-zh-type").val();
|
||||
var ptval = $("select.flag-pt-type").val();
|
||||
var en_style = enval <= 0 ? "" : ".weglot-flags.en > a:before, .weglot-flags.en > span:before { background-position: -" + en_flags[enval][0] + "px 0 !important; } .weglot-flags.flag-1.en > a:before, .weglot-flags.flag-1.en > span:before { background-position: -" + en_flags[enval][1] + "px 0 !important; } .weglot-flags.flag-2.en > a:before, .weglot-flags.flag-2.en > span:before { background-position: -" + en_flags[enval][2] + "px 0 !important; } .weglot-flags.flag-3.en > a:before, .weglot-flags.flag-3.en > span:before { background-position: -" + en_flags[enval][3] + "px 0 !important; } ";
|
||||
var es_style = esval <= 0 ? "" : ".weglot-flags.es > a:before, .weglot-flags.es > span:before { background-position: -" + es_flags[esval][0] + "px 0 !important; } .weglot-flags.flag-1.es > a:before, .weglot-flags.flag-1.es > span:before { background-position: -" + es_flags[esval][1] + "px 0 !important; } .weglot-flags.flag-2.es > a:before, .weglot-flags.flag-2.es > span:before { background-position: -" + es_flags[esval][2] + "px 0 !important; } .weglot-flags.flag-3.es > a:before, .weglot-flags.flag-3.es > span:before { background-position: -" + es_flags[esval][3] + "px 0 !important; } ";
|
||||
var fr_style = frval <= 0 ? "" : ".weglot-flags.fr > a:before, .weglot-flags.fr > span:before { background-position: -" + fr_flags[frval][0] + "px 0 !important; } .weglot-flags.flag-1.fr > a:before, .weglot-flags.flag-1.fr > span:before { background-position: -" + fr_flags[frval][1] + "px 0 !important; } .weglot-flags.flag-2.fr > a:before, .weglot-flags.flag-2.fr > span:before { background-position: -" + fr_flags[frval][2] + "px 0 !important; } .weglot-flags.flag-3.fr > a:before, .weglot-flags.flag-3.fr > span:before { background-position: -" + fr_flags[frval][3] + "px 0 !important; } ";
|
||||
var ar_style = arval <= 0 ? "" : ".weglot-flags.ar > a:before, .weglot-flags.ar > span:before { background-position: -" + ar_flags[arval][0] + "px 0 !important; } .weglot-flags.flag-1.ar > a:before, .weglot-flags.flag-1.ar > span:before { background-position: -" + ar_flags[arval][1] + "px 0 !important; } .weglot-flags.flag-2.ar > a:before, .weglot-flags.flag-2.ar > span:before { background-position: -" + ar_flags[arval][2] + "px 0 !important; } .weglot-flags.flag-3.ar > a:before, .weglot-flags.flag-3.ar > span:before { background-position: -" + ar_flags[arval][3] + "px 0 !important; } ";
|
||||
var tw_style = twval <= 0 ? "" : ".weglot-flags.tw > a:before, .weglot-flags.tw > span:before { background-position: -" + tw_flags[twval][0] + "px 0 !important; } .weglot-flags.flag-1.tw > a:before, .weglot-flags.flag-1.tw > span:before { background-position: -" + tw_flags[twval][1] + "px 0 !important; } .weglot-flags.flag-2.tw > a:before, .weglot-flags.flag-2.tw > span:before { background-position: -" + tw_flags[twval][2] + "px 0 !important; } .weglot-flags.flag-3.tw > a:before, .weglot-flags.flag-3.tw > span:before { background-position: -" + tw_flags[twval][3] + "px 0 !important; } ";
|
||||
var zh_style = zhval <= 0 ? "" : ".weglot-flags.zh > a:before, .weglot-flags.zh > span:before { background-position: -" + zh_flags[zhval][0] + "px 0 !important; } .weglot-flags.flag-1.zh > a:before, .weglot-flags.flag-1.zh > span:before { background-position: -" + zh_flags[zhval][1] + "px 0 !important; } .weglot-flags.flag-2.zh > a:before, .weglot-flags.flag-2.zh > span:before { background-position: -" + zh_flags[zhval][2] + "px 0 !important; } .weglot-flags.flag-3.zh > a:before, .weglot-flags.flag-3.zh > span:before { background-position: -" + zh_flags[zhval][3] + "px 0 !important; } ";
|
||||
var pt_style = ptval <= 0 ? "" : ".weglot-flags.pt > a:before, .weglot-flags.pt > span:before { background-position: -" + pt_flags[ptval][0] + "px 0 !important; } .weglot-flags.flag-1.pt > a:before, .weglot-flags.flag-1.pt > span:before { background-position: -" + pt_flags[ptval][1] + "px 0 !important; } .weglot-flags.flag-2.pt > a:before, .weglot-flags.flag-2.pt > span:before { background-position: -" + pt_flags[ptval][2] + "px 0 !important; } .weglot-flags.flag-3.pt > a:before, .weglot-flags.flag-3.pt > span:before { background-position: -" + pt_flags[ptval][3] + "px 0 !important; } ";
|
||||
|
||||
$("#flag_css, #weglot-css-flag-css").text(en_style + es_style + fr_style + ar_style + tw_style + zh_style + pt_style);
|
||||
}
|
||||
|
||||
const execute = () => {
|
||||
|
||||
$('.flag-style-openclose').on('click',
|
||||
function () {
|
||||
$('.flag-style-wrapper').toggle();
|
||||
}
|
||||
);
|
||||
|
||||
$('.old-flag-style').on('click',
|
||||
function () {
|
||||
$('.old-flag-wrapper').toggle();
|
||||
}
|
||||
);
|
||||
|
||||
$("select.flag-en-type, select.flag-es-type, select.flag-pt-type, select.flag-fr-type, select.flag-ar-type, select.flag-tw-type, select.flag-zh-type").on('change',
|
||||
function () {
|
||||
refresh_flag_css()
|
||||
}
|
||||
);
|
||||
|
||||
var flag_css = $("#flag_css").text();
|
||||
if (flag_css.trim()) {
|
||||
$("#weglot-css-flag-css").text(flag_css);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
execute();
|
||||
});
|
||||
};
|
||||
|
||||
export default init_admin_change_country;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
const init_admin_button_preview = function () {
|
||||
const $ = jQuery
|
||||
|
||||
const execute = () => {
|
||||
|
||||
$("#api_key_private").blur(function() {
|
||||
var key = $(this).val();
|
||||
if( key.length === 0){
|
||||
$(".weglot-keyres").remove();
|
||||
$("#api_key_private").after('<span class="weglot-keyres weglot-nokkey"></span>');
|
||||
$("#wrap-weglot #submit").prop("disabled", true);
|
||||
return;
|
||||
}
|
||||
|
||||
function validApiKey(response){
|
||||
|
||||
$(".weglot-keyres").remove();
|
||||
$("#api_key_private").after(
|
||||
'<span class="weglot-keyres weglot-okkey"></span>'
|
||||
);
|
||||
|
||||
$("#wrap-weglot #submit").prop(
|
||||
"disabled",
|
||||
false
|
||||
);
|
||||
|
||||
const evt = new CustomEvent("weglotCheckApi", {
|
||||
detail: response
|
||||
});
|
||||
|
||||
window.dispatchEvent(evt);
|
||||
}
|
||||
|
||||
function unvalidApiKey(){
|
||||
$(".weglot-keyres").remove();
|
||||
$("#api_key_private").after('<span class="weglot-keyres weglot-nokkey"></span><p class="weglot-keyres">Make sure you enter a valid Weglot API key. If the key is still not validating, you can contact your host provider and ask if it\'s possible to whitelist api.weglot.com and weglot.com</p>');
|
||||
$("#wrap-weglot #submit").prop("disabled", true);
|
||||
}
|
||||
|
||||
$(".weglot-keyres").remove();
|
||||
$("#api_key_private").after('<span class="weglot-keyres weglot-ckeckkey"></span>');
|
||||
|
||||
$.ajax(
|
||||
{
|
||||
method: 'POST',
|
||||
url: ajaxurl,
|
||||
data : {
|
||||
action: 'get_user_info',
|
||||
api_key: key,
|
||||
},
|
||||
success: ({data, success}) => {
|
||||
$(".weglot-keyres").remove();
|
||||
if (success ){
|
||||
validApiKey(data)
|
||||
}
|
||||
else{
|
||||
unvalidApiKey()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
).fail(function() {
|
||||
unvalidApiKey()
|
||||
});
|
||||
});
|
||||
|
||||
$( ".toplevel_page_weglot-settings form" ).submit(function( event ) {
|
||||
$("#wrap-weglot #submit").prop("disabled", true);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
execute();
|
||||
})
|
||||
}
|
||||
|
||||
export default init_admin_button_preview;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
const init_admin_weglot_code_editor = function () {
|
||||
const $ = jQuery
|
||||
|
||||
const execute = () => {
|
||||
jQuery(document).ready(function($) {
|
||||
wp.codeEditor.initialize($('#override_css'), cm_settings);
|
||||
})
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
execute();
|
||||
})
|
||||
}
|
||||
|
||||
export default init_admin_weglot_code_editor;
|
||||
@@ -0,0 +1,31 @@
|
||||
const init_private_mode = function () {
|
||||
|
||||
const $ = jQuery
|
||||
|
||||
const execute = () => {
|
||||
document.querySelector("#private_mode").addEventListener('change', function(e) {
|
||||
|
||||
document.querySelectorAll(".private-mode-lang--input").forEach((itm) => {
|
||||
itm.checked = e.target.checked;
|
||||
})
|
||||
})
|
||||
|
||||
document.querySelectorAll(".private-mode-lang--input").forEach((itm) => {
|
||||
itm.addEventListener('change', function(e){
|
||||
if (document.querySelectorAll(".private-mode-lang--input:checked").length === 0){
|
||||
document.querySelector("#private_mode").checked = false
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const private_mode = document.querySelector("#private_mode")
|
||||
if (private_mode && private_mode.length != 0){
|
||||
execute();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default init_private_mode;
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
const init_admin_select = function(){
|
||||
|
||||
const $ = jQuery
|
||||
const generate_destination_language = () => {
|
||||
return weglot_languages.available.filter(itm => {
|
||||
return itm.internal_code !== $("#original_language").val()
|
||||
});
|
||||
}
|
||||
|
||||
let destination_selectize
|
||||
|
||||
const load_destination_selectize = () => {
|
||||
destination_selectize = $(".weglot-select-destination")
|
||||
.selectize({
|
||||
delimiter: "|",
|
||||
persist: false,
|
||||
valueField: "internal_code",
|
||||
labelField: "local",
|
||||
searchField: ["internal_code", "english", "local"],
|
||||
sortField: [{ field: "english", direction: "asc" }],
|
||||
maxItems: weglot_languages.limit,
|
||||
plugins: ["remove_button"],
|
||||
options: generate_destination_language(),
|
||||
render: {
|
||||
option: function(item, escape) {
|
||||
var english = escape(item.english);
|
||||
var local = escape(item.local);
|
||||
var external = escape(item.external_code);
|
||||
return `<div class="weglot__choice__language"><span class="weglot__choice__language--english">${english}</span><span class="weglot__choice__language--local">${local}[${external}]</span></div>`;
|
||||
}
|
||||
}
|
||||
})
|
||||
.on("change", (value) => {
|
||||
const code_languages = destination_selectize[0].selectize.getValue()
|
||||
|
||||
const template = $("#li-button-tpl");
|
||||
|
||||
if (template.length === 0){
|
||||
return;
|
||||
}
|
||||
|
||||
const is_fullname = $("#is_fullname").is(":checked")
|
||||
const with_name = $("#with_name").is(":checked")
|
||||
const with_flags = $("#with_flags").is(":checked")
|
||||
|
||||
let classes = ''
|
||||
if (with_flags) {
|
||||
classes = "weglot-flags";
|
||||
}
|
||||
|
||||
let new_dest_language = ''
|
||||
var currentFlagClasses = $("label.weglot-flags").attr("class")
|
||||
var classArr = currentFlagClasses.split(/\s+/);
|
||||
$.each(classArr, function(index, value){
|
||||
if(value.includes('flag-') == true){
|
||||
classes += ' '+value;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
code_languages.forEach(element => {
|
||||
const language = weglot_languages.available.find(itm => itm.internal_code === element);
|
||||
let label = ''
|
||||
if(with_name){
|
||||
if (is_fullname){
|
||||
label = language.local
|
||||
}
|
||||
else{
|
||||
label = element.toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
new_dest_language += template
|
||||
.html()
|
||||
.replace("{LABEL_LANGUAGE}", label)
|
||||
.replace(new RegExp("{CODE_LANGUAGE}", "g"), element)
|
||||
.replace("{CLASSES}", classes)
|
||||
});
|
||||
$(".country-selector ul").html(new_dest_language) //phpcs:ignore
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
const execute = () => {
|
||||
let work_original_language = $("#original_language").val()
|
||||
|
||||
$("#original_language").on("change", function (e) {
|
||||
const old_original_language = work_original_language;
|
||||
const new_destination_option = work_original_language;
|
||||
work_original_language = e.target.value;
|
||||
destination_selectize[0].selectize.removeOption(work_original_language);
|
||||
|
||||
const new_option = weglot_languages.available.find(itm => {
|
||||
return itm.internal_code === new_destination_option
|
||||
});
|
||||
|
||||
const new_original_option = weglot_languages.available.find(itm => {
|
||||
return itm.internal_code === work_original_language;
|
||||
});
|
||||
|
||||
destination_selectize[0].selectize.addOption(new_option);
|
||||
|
||||
|
||||
const is_fullname = $("#is_fullname").is(":checked")
|
||||
const with_name = $("#with_name").is(":checked")
|
||||
let label = ''
|
||||
if(with_name){
|
||||
label = is_fullname ? new_original_option.local : new_original_option.internal_code.toUpperCase();
|
||||
}
|
||||
|
||||
$(".wgcurrent.wg-li")
|
||||
.removeClass(old_original_language)
|
||||
.addClass(work_original_language)
|
||||
.attr("data-code-language", work_original_language)
|
||||
.find('span').text(label)
|
||||
});
|
||||
|
||||
|
||||
load_destination_selectize();
|
||||
|
||||
window.addEventListener("weglotCheckApi", (data) => {
|
||||
destination_selectize[0].selectize.settings.maxItems = weglot_languages.limit;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
execute();
|
||||
})
|
||||
}
|
||||
|
||||
export default init_admin_select;
|
||||
@@ -0,0 +1,49 @@
|
||||
const init_admin_weglot_box = function () {
|
||||
const $ = jQuery
|
||||
|
||||
const execute = () => {
|
||||
$("#weglot-box-first-settings .weglot-btn-close").on("click", function (e) {
|
||||
e.preventDefault();
|
||||
$("#weglot-box-first-settings").hide();
|
||||
})
|
||||
|
||||
$('a[href*="#"]')
|
||||
// Remove links that don't actually link to anything
|
||||
.not('[href="#"]')
|
||||
.not('[href="#0"]')
|
||||
.click(function (event) {
|
||||
// On-page links
|
||||
// Figure out element to scroll to
|
||||
var target = $(this.hash);
|
||||
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
|
||||
// Does a scroll target exist?
|
||||
if (target.length) {
|
||||
// Only prevent default if animation is actually gonna happen
|
||||
event.preventDefault();
|
||||
$('html, body').animate({
|
||||
scrollTop: target.offset().top
|
||||
}, 1000, function () {
|
||||
// Callback after animation
|
||||
// Must change focus!
|
||||
var $target = $(target);
|
||||
$target.focus();
|
||||
if ($target.is(":focus")) { // Checking if the target was focused
|
||||
return false;
|
||||
} else {
|
||||
$target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
|
||||
$target.focus(); // Set focus again
|
||||
}
|
||||
;
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
execute();
|
||||
})
|
||||
}
|
||||
|
||||
export default init_admin_weglot_box;
|
||||
|
||||
Reference in New Issue
Block a user