2016-05-15 10:11:52 -04:00
|
|
|
|
// TODO: refactor me!
|
2016-05-15 10:17:04 -04:00
|
|
|
|
var sourceVisible = localStorage.getItem('source-visible') === 'true';
|
2016-05-15 10:11:52 -04:00
|
|
|
|
|
2016-05-15 09:27:57 -04:00
|
|
|
|
(function ($) {
|
2016-05-13 03:06:35 -04:00
|
|
|
|
var nodes = document.querySelectorAll('p, li, h1, h2, h3, h4, h5, h6, header, a, button, small');
|
|
|
|
|
_.each(nodes, function (node) {
|
2016-05-15 09:27:57 -04:00
|
|
|
|
var prevNode = node.previousElementSibling;
|
|
|
|
|
if (!prevNode) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isTranslationResult(node, prevNode)) {
|
|
|
|
|
var $prevNode = $(prevNode);
|
|
|
|
|
var $node = $(node);
|
2016-05-17 04:00:56 -04:00
|
|
|
|
if ($prevNode.hasClass('nav-list-item')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isPureEnglish($node.text()) && $node.text() !== $prevNode.text()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isPureEnglish($prevNode.text())) {
|
2016-05-15 09:27:57 -04:00
|
|
|
|
$node.attr('id', prevNode.id);
|
|
|
|
|
$node.addClass('translated');
|
|
|
|
|
$node.addClass('translated-cn');
|
|
|
|
|
$prevNode.removeAttr('id');
|
|
|
|
|
$prevNode.addClass('original-english');
|
2016-05-15 10:11:52 -04:00
|
|
|
|
if (!sourceVisible) {
|
2016-05-15 09:27:57 -04:00
|
|
|
|
$prevNode.addClass('hidden');
|
|
|
|
|
}
|
|
|
|
|
if (node.tagName !== 'A' && node.tagName !== 'BUTTON') {
|
|
|
|
|
$node.on('click', function () {
|
|
|
|
|
$prevNode.toggleClass('hidden');
|
|
|
|
|
});
|
|
|
|
|
$prevNode.on('click', function () {
|
|
|
|
|
$prevNode.addClass('hidden');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
$node.after($prevNode);
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-13 03:37:56 -04:00
|
|
|
|
function isPureEnglish(text) {
|
2016-05-19 04:32:43 -04:00
|
|
|
|
// accept — , quotes, ® and façade too.
|
|
|
|
|
return /^[\1-\255—’“”ç®]*$/.test(text);
|
2016-05-14 04:20:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function attributesToString(node) {
|
|
|
|
|
return _.chain(node.attributes)
|
|
|
|
|
.map(function (value) {
|
|
|
|
|
if (value.name === 'id') {
|
|
|
|
|
return '';
|
|
|
|
|
} else {
|
|
|
|
|
return value.name + '=' + value.value;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.sortBy()
|
|
|
|
|
.value()
|
|
|
|
|
.join(';');
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isClonedNode(node1, node2) {
|
2016-05-14 04:20:01 -04:00
|
|
|
|
return node1.tagName === node2.tagName &&
|
2016-05-14 07:25:47 -04:00
|
|
|
|
attributesToString(node1) === attributesToString(node2);
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 09:27:57 -04:00
|
|
|
|
function indexOfSameType(node) {
|
2016-05-13 01:00:21 -04:00
|
|
|
|
var i = 0;
|
|
|
|
|
var aNode = node.parentNode.firstChild;
|
|
|
|
|
while (aNode !== node) {
|
|
|
|
|
++i;
|
|
|
|
|
if (aNode.tagName !== node.tagName) {
|
|
|
|
|
i = 0;
|
|
|
|
|
}
|
|
|
|
|
aNode = aNode.nextElementSibling;
|
|
|
|
|
}
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 09:27:57 -04:00
|
|
|
|
function isTranslationResult(node, prevNode) {
|
|
|
|
|
return indexOfSameType(node) % 2 === 1 && isClonedNode(node, prevNode) && isPureEnglish(prevNode.innerText);
|
2016-05-13 01:00:21 -04:00
|
|
|
|
}
|
2016-05-15 09:27:57 -04:00
|
|
|
|
})(angular.element);
|