2016-06-19 04:46:50 -04:00
|
|
|
// TODO: refactor me!
|
|
|
|
var sourceVisible = localStorage.getItem('source-visible') === 'true';
|
|
|
|
(function ($) {
|
2016-11-24 09:16:23 -05:00
|
|
|
var content = document.querySelector('article');
|
|
|
|
var footer = document.querySelector('.main-footer');
|
|
|
|
processContainer(content);
|
|
|
|
processContainer(footer);
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
if (!sourceVisible) {
|
|
|
|
var nodes = document.querySelectorAll('.original-english');
|
|
|
|
_.each(nodes, function (node) {
|
|
|
|
$(node).addClass('hidden');
|
|
|
|
});
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
// restore
|
|
|
|
content.style.display = 'block';
|
|
|
|
footer.style.display = 'block';
|
2016-11-23 22:45:57 -05:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
/**
|
|
|
|
* Process container recursively.
|
|
|
|
* @param container
|
|
|
|
*/
|
|
|
|
function processContainer(container) {
|
|
|
|
for (var i = 0; i < container.children.length; i++) {
|
|
|
|
var node = container.children[i];
|
2016-11-23 22:45:57 -05:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
// ignore example code.
|
|
|
|
if (node.classList.contains('code-example') ||
|
|
|
|
node.tagName === 'CODE-EXAMPLE') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
switch (node.tagName) {
|
|
|
|
case 'P':
|
|
|
|
case 'H1':
|
|
|
|
case 'H2':
|
|
|
|
case 'H3':
|
|
|
|
case 'H4':
|
|
|
|
case 'H5':
|
|
|
|
case 'H6':
|
|
|
|
case 'HEADER':
|
|
|
|
if (processBlock(node)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-24 09:54:49 -05:00
|
|
|
case 'LI':
|
|
|
|
if($(node).find('p').length <= 1){
|
|
|
|
if (processBlock(node)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-11-24 09:16:23 -05:00
|
|
|
case 'TD':
|
|
|
|
case 'TH':
|
|
|
|
processContainer(node);
|
|
|
|
// return; // stop
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (node.children.length > 0) {
|
|
|
|
processContainer(node);
|
|
|
|
// For <li><p>...</p></li>, processes it as block.
|
2016-11-24 09:54:49 -05:00
|
|
|
// if (node.children.length === 1) {
|
|
|
|
// if (processBlock(node)) {
|
|
|
|
// i++;
|
|
|
|
// }
|
|
|
|
// }
|
2016-11-24 09:16:23 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
}
|
2016-11-24 09:16:23 -05:00
|
|
|
}
|
2016-07-16 06:29:42 -04:00
|
|
|
|
2016-11-24 09:16:23 -05: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-23 22:45:57 -05:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
if (sibling) {
|
|
|
|
if (isClonedNode(current, sibling)) {
|
|
|
|
if (isPureEnglish(current.textContent)) {
|
|
|
|
$current.addClass('original-english');
|
|
|
|
$sibling.addClass('translated');
|
|
|
|
$sibling.addClass('translated-cn');
|
|
|
|
$sibling.after($current);
|
|
|
|
$sibling.on('click', function () {
|
|
|
|
$current.toggleClass('hidden');
|
|
|
|
});
|
|
|
|
return true;
|
2016-06-19 07:31:02 -04:00
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
console.error('Error: ' + current.innerText);
|
|
|
|
}
|
2016-11-23 22:45:57 -05:00
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
function isPureEnglish(text) {
|
|
|
|
if(text){
|
|
|
|
return !/\p{Han}/.test(text);
|
2016-06-19 04:46:50 -04:00
|
|
|
}
|
2016-11-24 09:16:23 -05:00
|
|
|
return false;
|
2016-06-19 04:46:50 -04:00
|
|
|
|
2016-11-24 09:16:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function attributesToString(node) {
|
|
|
|
return _.chain(node.attributes)
|
|
|
|
.map(function (value) {
|
2016-11-24 09:54:49 -05:00
|
|
|
if (value.name === 'id') {
|
2016-11-24 09:16:23 -05:00
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return value.name + '=' + value.value;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.sortBy()
|
|
|
|
.value()
|
|
|
|
.join(';');
|
|
|
|
}
|
|
|
|
|
|
|
|
function isClonedNode(node1, node2) {
|
|
|
|
return node1.tagName === node2.tagName &&
|
|
|
|
attributesToString(node1) === attributesToString(node2);
|
|
|
|
}
|
2016-06-19 04:46:50 -04:00
|
|
|
})(angular.element);
|