|
| 1 | +'use strict'; |
| 2 | +/* global togglbutton, $ */ |
| 3 | + |
| 4 | +/* Hover on Bullet Popup */ |
| 5 | +togglbutton.render( |
| 6 | + // TODO: Change selector if the popup ever gets a constant class. |
| 7 | + '.name:not(.toggl) > div:not(.content)', |
| 8 | + { observe: true }, |
| 9 | + $container => { |
| 10 | + const $bulletInfo = $('.content', $container.parentElement); |
| 11 | + |
| 12 | + const descriptionSelector = () => { |
| 13 | + return getDescription($bulletInfo); |
| 14 | + }; |
| 15 | + |
| 16 | + const tagsSelector = () => { |
| 17 | + return getTags($bulletInfo); |
| 18 | + }; |
| 19 | + |
| 20 | + const link = togglbutton.createTimerLink({ |
| 21 | + className: 'workflowy', |
| 22 | + description: descriptionSelector, |
| 23 | + tags: tagsSelector |
| 24 | + }); |
| 25 | + |
| 26 | + insertLink($container, link); |
| 27 | + } |
| 28 | +); |
| 29 | + |
| 30 | +/* More Options Menu Popup */ |
| 31 | +togglbutton.render( |
| 32 | + // TODO: Change selector if the popup ever gets a constant class. |
| 33 | + '.selected:not(.mainTreeRoot) ~ .pageControls > .pageMenu:not(.toggl) >' + |
| 34 | + ' div:nth-child(2)', |
| 35 | + { observe: true }, |
| 36 | + $container => { |
| 37 | + // Get the content of the top bullet. |
| 38 | + const $bulletInfo = $('.content'); |
| 39 | + |
| 40 | + const descriptionSelector = () => { |
| 41 | + return getDescription($bulletInfo); |
| 42 | + }; |
| 43 | + |
| 44 | + const tagsSelector = () => { |
| 45 | + return getTags($bulletInfo); |
| 46 | + }; |
| 47 | + |
| 48 | + const link = togglbutton.createTimerLink({ |
| 49 | + className: 'workflowy', |
| 50 | + description: descriptionSelector, |
| 51 | + tags: tagsSelector |
| 52 | + }); |
| 53 | + |
| 54 | + insertLink($container, link); |
| 55 | + } |
| 56 | +); |
| 57 | + |
| 58 | +function getDescription (bulletInfo) { |
| 59 | + let description = ''; |
| 60 | + let currentNode = bulletInfo.childNodes[0]; |
| 61 | + while (currentNode !== bulletInfo) { |
| 62 | + // If it is a text node. |
| 63 | + if (currentNode.nodeType === 3) { |
| 64 | + description += currentNode.textContent; |
| 65 | + } |
| 66 | + |
| 67 | + // Try to go down the tree. |
| 68 | + let nextNode = currentNode.firstChild || currentNode.nextSibling; |
| 69 | + // Span means it is a tag, and tag text should not be included. |
| 70 | + while (nextNode && nextNode.nodeName === 'SPAN') { |
| 71 | + nextNode = nextNode.nextSibling; |
| 72 | + } |
| 73 | + // If we couldn't go down try to go back up the tree. |
| 74 | + if (!nextNode) { |
| 75 | + nextNode = currentNode.parentNode; |
| 76 | + while (nextNode !== bulletInfo) { |
| 77 | + // We are valid again! |
| 78 | + if (nextNode.nextSibling) { |
| 79 | + nextNode = nextNode.nextSibling; |
| 80 | + break; |
| 81 | + } |
| 82 | + // Try to go up again. If we reach the bulletInfo node that |
| 83 | + // means we've reached the top and we will break out of both loops. |
| 84 | + nextNode = nextNode.parentNode; |
| 85 | + } |
| 86 | + } |
| 87 | + currentNode = nextNode; |
| 88 | + } |
| 89 | + return description.trim(); |
| 90 | +} |
| 91 | + |
| 92 | +function getTags (bulletInfo) { |
| 93 | + const tagsArray = []; |
| 94 | + let currentNode = bulletInfo.childNodes[0]; |
| 95 | + while (currentNode !== bulletInfo) { |
| 96 | + if (currentNode.classList && |
| 97 | + currentNode.classList.contains('contentTagText')) { |
| 98 | + tagsArray.push(currentNode.textContent.trim()); |
| 99 | + } |
| 100 | + |
| 101 | + // Try to go down the tree. |
| 102 | + let nextNode = currentNode.firstChild || currentNode.nextSibling; |
| 103 | + // If we can't go down try to go back up the tree. |
| 104 | + if (!nextNode) { |
| 105 | + nextNode = currentNode.parentNode; |
| 106 | + while (nextNode !== bulletInfo) { |
| 107 | + // We are valid again! |
| 108 | + if (nextNode.nextSibling) { |
| 109 | + nextNode = nextNode.nextSibling; |
| 110 | + break; |
| 111 | + } |
| 112 | + // Try to go up again. If we reach the bulletInfo node that |
| 113 | + // means we've reached the top and we will break out of both loops. |
| 114 | + nextNode = nextNode.parentNode; |
| 115 | + } |
| 116 | + } |
| 117 | + currentNode = nextNode; |
| 118 | + } |
| 119 | + return tagsArray; |
| 120 | +} |
| 121 | + |
| 122 | +function insertLink (popup, link) { |
| 123 | + const INSERT_POSITION = 3; |
| 124 | + |
| 125 | + /* Massage the DOM */ |
| 126 | + const wrapper = document.createElement('div'); |
| 127 | + wrapper.classList.add('workflowy-toggl-wrapper'); |
| 128 | + // This makes sure the 'Start timer' link matches the style of the other |
| 129 | + // options (even when the theme is changed). |
| 130 | + wrapper.classList.add(popup.children[0].classList[0]); |
| 131 | + wrapper.appendChild(link); |
| 132 | + popup.insertBefore(wrapper, popup.children[INSERT_POSITION]); |
| 133 | + |
| 134 | + // We have to add the toggl class to the parent of the popup, |
| 135 | + // because the popup's classes get overwritten by workflowy. |
| 136 | + const parent = popup.parentElement; |
| 137 | + parent.classList.add('toggl'); |
| 138 | + |
| 139 | + // And remove the class when the parent element's children change (meaning |
| 140 | + // the popup is deleted). |
| 141 | + const observer = new MutationObserver(function (mutationsList, observer) { |
| 142 | + parent.classList.remove('toggl'); |
| 143 | + observer.disconnect(); |
| 144 | + }); |
| 145 | + observer.observe(parent, { childList: true }); |
| 146 | +} |
0 commit comments