angular-cn/public/translate/cn/translate.js

135 lines
3.2 KiB
JavaScript
Raw Normal View History

// TODO: refactor me!
var sourceVisible = localStorage.getItem('source-visible') === 'true';
(function ($) {
2016-11-24 14:16:23 +00:00
var content = document.querySelector('article');
var footer = document.querySelector('.main-footer');
processContainer(content);
processContainer(footer);
2016-11-24 14:16:23 +00:00
if (!sourceVisible) {
var nodes = document.querySelectorAll('.original-english');
_.each(nodes, function (node) {
$(node).addClass('hidden');
});
}
2016-11-24 14:16:23 +00:00
// restore
content.style.display = 'block';
footer.style.display = 'block';
2016-11-24 11:45:57 +08:00
2016-11-24 14:16:23 +00:00
/**
* Process container recursively.
* @param container
*/
function processContainer(container) {
2016-11-25 01:15:27 +08:00
var count = 0;
2016-11-24 14:16:23 +00:00
for (var i = 0; i < container.children.length; i++) {
var node = container.children[i];
2016-11-24 11:45:57 +08:00
2016-11-24 14:16:23 +00:00
// ignore example code.
if (node.classList.contains('code-example') ||
2016-11-25 02:38:54 +08:00
node.tagName === 'CODE-EXAMPLE' ||
node.tagName === 'SCRIPT') {
2016-11-24 14:16:23 +00:00
continue;
}
2016-11-24 11:45:57 +08:00
2016-11-24 14:16:23 +00:00
switch (node.tagName) {
case 'P':
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
case 'HEADER':
2016-11-25 01:15:27 +08:00
count++;
2016-11-24 14:16:23 +00:00
if (processBlock(node)) {
i++;
2016-11-25 01:15:27 +08:00
count++;
2016-11-24 14:54:49 +00:00
}
break;
2016-11-24 14:16:23 +00:00
case 'TD':
case 'TH':
2016-11-25 01:15:27 +08:00
case 'UL':
case 'OL':
2016-11-25 02:38:54 +08:00
case 'DIV':
2016-11-24 14:16:23 +00:00
processContainer(node);
break;
default:
2016-11-25 01:42:21 +08:00
if (processContainer(node) <= 1) {
if (processBlock(node)) {
i++;
2016-11-25 01:50:59 +08:00
count++;
2016-11-25 01:15:27 +08:00
}
2016-11-24 14:16:23 +00:00
}
break;
}
2016-11-24 11:45:57 +08:00
}
2016-11-25 01:15:27 +08:00
return count;
2016-11-24 14:16:23 +00:00
}
2016-11-24 14:16:23 +00:00
/**
* Process block elements. The first element is original english, the
* second element is translated one.
* @param current the first element.
* @returns {boolean} Is success?
*/
function processBlock(current) {
var sibling = current.nextElementSibling;
var $current = $(current);
var $sibling = $(sibling);
2016-11-24 11:45:57 +08:00
2016-11-24 14:16:23 +00:00
if (sibling) {
if (isClonedNode(current, sibling)) {
if (isPureEnglish(current.textContent)) {
2016-11-25 02:23:06 +08:00
if (sibling.children) {
processContainer(sibling);
}
2016-11-24 14:16:23 +00:00
$current.addClass('original-english');
$sibling.addClass('translated');
$sibling.addClass('translated-cn');
$sibling.after($current);
2016-11-25 01:15:27 +08:00
$sibling.on('click', function (event) {
// for nested structure.
event.stopPropagation();
2016-11-24 14:16:23 +00:00
$current.toggleClass('hidden');
});
return true;
}
2016-11-24 14:16:23 +00:00
console.error('Error: ' + current.innerText);
}
2016-11-24 11:45:57 +08:00
}
2016-11-24 14:16:23 +00:00
return false;
}
2016-11-24 14:16:23 +00:00
function isPureEnglish(text) {
if(text){
return !/\p{Han}/.test(text);
}
2016-11-24 14:16:23 +00:00
return false;
2016-11-24 14:16:23 +00:00
}
function attributesToString(node) {
return _.chain(node.attributes)
.map(function (value) {
2016-11-24 14:54:49 +00:00
if (value.name === 'id') {
2016-11-24 14:16:23 +00:00
return '';
} else {
return value.name + '=' + value.value;
}
})
.sortBy()
.value()
.join(';');
}
function isClonedNode(node1, node2) {
return node1.tagName === node2.tagName &&
attributesToString(node1) === attributesToString(node2);
}
})(angular.element);