Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/js/core/behavior-watch-anchor.js
| Show All 17 Lines | JX.behavior('phabricator-watch-anchor', function() { | ||||
| // Number of milliseconds we'll keep trying to find an anchor for. | // Number of milliseconds we'll keep trying to find an anchor for. | ||||
| var wait_max = 5000; | var wait_max = 5000; | ||||
| // Wait between retries. | // Wait between retries. | ||||
| var wait_ms = 100; | var wait_ms = 100; | ||||
| var target; | var target; | ||||
| var display_target; | |||||
| var retry_ms; | var retry_ms; | ||||
| function try_anchor() { | function try_anchor() { | ||||
| retry_ms = wait_max; | retry_ms = wait_max; | ||||
| seek_anchor(); | seek_anchor(); | ||||
| } | } | ||||
| function seek_anchor() { | function seek_anchor() { | ||||
| Show All 35 Lines | if (!node) { | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| // If we already have an anchor highlighted, unhighlight it and throw | // If we already have an anchor highlighted, unhighlight it and throw | ||||
| // it away if it doesn't match the new target. | // it away if it doesn't match the new target. | ||||
| if (target && (target !== node)) { | if (target && (target !== node)) { | ||||
| JX.DOM.alterClass(target, 'anchor-target', false); | JX.DOM.alterClass(display_target, 'anchor-target', false); | ||||
| target = null; | target = null; | ||||
| display_target = null; | |||||
| } | } | ||||
| // If we didn't find a matching anchor, try again soon. This allows | // If we didn't find a matching anchor, try again soon. This allows | ||||
| // rendering logic some time to complete Ajax requests and draw elements | // rendering logic some time to complete Ajax requests and draw elements | ||||
| // onto the page. | // onto the page. | ||||
| if (!node) { | if (!node) { | ||||
| if (retry_ms > 0) { | if (retry_ms > 0) { | ||||
| retry_ms -= wait_ms; | retry_ms -= wait_ms; | ||||
| setTimeout(try_anchor, wait_ms); | setTimeout(try_anchor, wait_ms); | ||||
| return; | return; | ||||
| } | } | ||||
| } | } | ||||
| // If we've found a new target, highlight it. | // If we've found a new target, highlight it. | ||||
| if (target !== node) { | if (target !== node) { | ||||
| target = node; | target = node; | ||||
| JX.DOM.alterClass(target, 'anchor-target', true); | |||||
| // If there's an "anchor-container" parent element, we'll make the | |||||
| // display adjustment to that node instead. For example, this is used | |||||
| // by the timeline to highlight timeline stories. | |||||
| var container = JX.DOM.findAbove(node, null, 'anchor-container'); | |||||
| if (container) { | |||||
| display_target = container; | |||||
| } else { | |||||
| display_target = node; | |||||
| } | |||||
| JX.DOM.alterClass(display_target, 'anchor-target', true); | |||||
| } | } | ||||
| // Try to scroll to the new target. | // Try to scroll to the new target. | ||||
| try { | try { | ||||
| var pos = JX.Vector.getPosWithScroll(node); | var pos = JX.Vector.getPosWithScroll(node); | ||||
| JX.DOM.scrollToPosition(0, pos.y - 60); | JX.DOM.scrollToPosition(0, pos.y - 60); | ||||
| } catch (e) { | } catch (e) { | ||||
| // Ignore issues with scrolling the document. | // Ignore issues with scrolling the document. | ||||
| } | } | ||||
| } | } | ||||
| JX.Stratcom.listen('hashchange', null, try_anchor); | JX.Stratcom.listen('hashchange', null, try_anchor); | ||||
| try_anchor(); | try_anchor(); | ||||
| }); | }); | ||||