|
| 1 | +var sourceVisible = localStorage.getItem('source-visible') === 'true'; |
| 2 | + |
| 3 | +(function ($) { |
| 4 | + function addOriginalToggler() { |
| 5 | + var nodes = document.querySelectorAll('p, li, h1, h2, h3, h4, h5, h6, header, a, button, small'); |
| 6 | + _.each(nodes, function (node) { |
| 7 | + var $node = $(node); |
| 8 | + |
| 9 | + if (isLink(node) || isButton(node)) { |
| 10 | + $node.on('click', function (event) { |
| 11 | + event.stopPropagation(); |
| 12 | + }); |
| 13 | + |
| 14 | + if (/^http?s:\/\//.test($node.attr('href')) && !$node.attr('target')) { |
| 15 | + $node.attr('target', '_blank'); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + var prevNode = node.previousElementSibling; |
| 20 | + var $prevNode = $(prevNode); |
| 21 | + |
| 22 | + if (!prevNode) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + if (isTranslationResult(node, prevNode)) { |
| 27 | + if ($prevNode.hasClass('nav-list-item') || |
| 28 | + $prevNode.hasClass('l-right') || |
| 29 | + $prevNode.hasClass('l-left')) { |
| 30 | + return; |
| 31 | + } |
| 32 | + if ($node.text() === $prevNode.text()) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $node.attr('id', prevNode.id); |
| 37 | + $node.addClass('translated'); |
| 38 | + $prevNode.removeAttr('id'); |
| 39 | + $prevNode.addClass('original-english'); |
| 40 | + if (!sourceVisible) { |
| 41 | + $prevNode.addClass('hidden'); |
| 42 | + } |
| 43 | + if (!isLink(node) && !isButton(node)) { |
| 44 | + var isDragging = false; |
| 45 | + $node.on('mousedown', function () { |
| 46 | + $(window).on('mousemove', function () { |
| 47 | + isDragging = true; |
| 48 | + $(window).unbind('mousemove'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + $prevNode.on('mousedown', function () { |
| 52 | + $(window).on('mousemove', function () { |
| 53 | + isDragging = true; |
| 54 | + $(window).unbind('mousemove'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + $node.on('mouseup', function () { |
| 58 | + var wasDragging = isDragging; |
| 59 | + isDragging = false; |
| 60 | + $(window).unbind('mousemove'); |
| 61 | + if (!wasDragging) { |
| 62 | + $prevNode.toggleClass('hidden'); |
| 63 | + } |
| 64 | + }); |
| 65 | + $prevNode.on('mouseup', function () { |
| 66 | + var wasDragging = isDragging; |
| 67 | + isDragging = false; |
| 68 | + $(window).unbind('mousemove'); |
| 69 | + if (!wasDragging) { |
| 70 | + $prevNode.addClass('hidden'); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + $node.after($prevNode); |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + function attributesToString(node) { |
| 80 | + return _.chain(node.attributes) |
| 81 | + .map(function (value) { |
| 82 | + if (value.name === 'id') { |
| 83 | + return ''; |
| 84 | + } else { |
| 85 | + return value.name + '=' + value.value; |
| 86 | + } |
| 87 | + }) |
| 88 | + .sortBy() |
| 89 | + .value() |
| 90 | + .join(';'); |
| 91 | + } |
| 92 | + |
| 93 | + function isLink(node) { |
| 94 | + return node.tagName.toUpperCase() === 'A'; |
| 95 | + } |
| 96 | + |
| 97 | + function isButton(node) { |
| 98 | + return node.tagName.toUpperCase() === 'BUTTON'; |
| 99 | + } |
| 100 | + |
| 101 | + function isClonedNode(node1, node2) { |
| 102 | + return node1.tagName === node2.tagName && |
| 103 | + attributesToString(node1) === attributesToString(node2); |
| 104 | + } |
| 105 | + |
| 106 | + function indexOfSameType(node) { |
| 107 | + var i = 0; |
| 108 | + var aNode = node.parentNode.firstChild; |
| 109 | + while (aNode !== node) { |
| 110 | + ++i; |
| 111 | + if (aNode.tagName !== node.tagName) { |
| 112 | + i = 0; |
| 113 | + } |
| 114 | + aNode = aNode.nextElementSibling; |
| 115 | + } |
| 116 | + return i; |
| 117 | + } |
| 118 | + |
| 119 | + function isTranslationResult(node, prevNode) { |
| 120 | + return indexOfSameType(node) % 2 === 1 && isClonedNode(node, prevNode); |
| 121 | + } |
| 122 | + |
| 123 | + addOriginalToggler(); |
| 124 | +})(angular.element); |
0 commit comments