-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support lightbox & sidebar history #43
Conversation
src/history.js
Outdated
}); | ||
} | ||
|
||
/** | ||
* Updates the history state. | ||
*/ | ||
decreaseCurrentStateId() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/history.js
Outdated
/** | ||
* Updates the history state. | ||
*/ | ||
increaseCurrentStateId() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/viewer-messaging.js
Outdated
* Registers a method that will handle requests sent to the specified | ||
* message name. | ||
* @param {string} messageName The name of the message to handle. | ||
* @param {!RequestHandler} requestHandler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is RequestHandler defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/viewer.js
Outdated
@@ -50,18 +49,34 @@ class Viewer { | |||
/** | |||
* @param {!Function} showViewer method that shows the viewer. | |||
* @param {!Function} hideViewer method that hides the viewer. | |||
* @param {!Function} isViewerHidden method that determines if viewer is hidden. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{!Function():boolean}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/viewer.js
Outdated
} | ||
|
||
/** | ||
* Attaches the AMP Doc Iframe to the Host Element. | ||
*/ | ||
attach() { | ||
if (this.isLoaded_()) { | ||
this.history_.pushState(this.ampDocUrl_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstance would attache get called when it is already attached? Are you sure you would want to push again? Couldn't the URL already be correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use case: if you went back & hid the viewer and then clicked on the element that opens the viewer again. It shouldn't push a new state but instead go forward in history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a click, you should always push a new state, even if it's the same state. That replicates how a browser would handle clicks. Whenever you click on a link, it pushes a new state, clearing all forward state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is, what you had the first time was likely fine. I think I didn't understand how it was being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is still calling goForward() which may be wrong in some cases.
src/history.js
Outdated
} | ||
|
||
// The url should have /amp/ + url added to it. For example: | ||
// example.com -> example.com/amp/https://www.ampproject.org |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this actually the URL format you want? Google Search would be:
http://www.example.com -> /amp/www.example.com
https://www.example.com -> /amp/s/www.example.com
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/viewer.js
Outdated
if (isBack) { | ||
this.viewerMessaging_.sendRequest( | ||
'historyPopped', | ||
{'newStackIndex': opt_stackIndex - 1}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opt_stackIndex is the new stackIndex, right? So you shouldn't need to subtract one.
Can you also add jsdoc to clarify is opt_stackIndex is expected to be old or new stack index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated comments to clarify. this is the old one so we need to subtract 1. this tells the amp doc to go to the previous state (close the lightbox or sidebar)
src/history.js
Outdated
} | ||
|
||
// The popped browser history state id. | ||
const poppedStateId = (typeof state.stateId !== 'undefined') ? state.stateId : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state.stateId !== undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/history.js
Outdated
this.decreaseCurrentStateId(); | ||
} else { | ||
this.increaseCurrentStateId(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you just take the value of poppedStateId
instead of incrementing/decrementing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
src/viewer.js
Outdated
if (this.hideViewer_) this.hideViewer_(); | ||
handleChangeHistoryState_(isBack, isLastBack, opt_stackIndex) { | ||
if (isBack) { | ||
this.viewerMessaging_.sendRequest( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this isLastBack, then I don't think you need to send this to the doc. You're going to a state the doc doesn't know about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the last back within the viewer. The viewer needs to know when to hide.
src/history.js
Outdated
const parsedUrl = parseUrl(url); | ||
let urlStr = '/amp/'; | ||
if (parsedUrl.protocol == 'https:') urlStr += 's/'; | ||
urlStr += parsedUrl.host; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't mean to bring URL construction into scope for this PR. But this is still incomplete since it doesn't include the path or query parameters in the url
parameter.
It may make sense to pull this out into a function, but that doesn't need to be part of the is PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added TODO
src/viewer.js
Outdated
* @param {boolean} rsvp | ||
* @return {!Promise<*>|undefined} | ||
*/ | ||
messageHandler(name, data, rsvp) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/viewer.js
Outdated
} else { | ||
if (this.hideViewer_) this.hideViewer_(); | ||
handleChangeHistoryState_(isLastBack, opt_poppedAmpHistoryIndex) { | ||
if (this.showViewer_ && this.isViewerHidden_ && this.isViewerHidden_()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be true for history events that have nothing to do with the AMP viewer. That is, since the AMP Viewer is embedded on some other page, there could be totally unrelated history events occurring. AMP history needs to correctly ignore then.
I think this code is intended to reopen the viewer on forward. It should verify that the new history state does correspond to the AMP Viewer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/viewer.js
Outdated
this.showViewer_(); | ||
return; | ||
} | ||
if (isLastBack && this.hideViewer_) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want to return even if there is no this.hideViewer_
. A history pop that closes the viewer should never to sent to the AMP doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/viewer.js
Outdated
} | ||
|
||
/** | ||
* Attaches the AMP Doc Iframe to the Host Element. | ||
*/ | ||
attach() { | ||
if (this.isLoaded_()) { | ||
this.history_.pushState(this.ampDocUrl_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a click, you should always push a new state, even if it's the same state. That replicates how a browser would handle clicks. Whenever you click on a link, it pushes a new state, clearing all forward state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for my slow response. I was travelling.
src/viewer.js
Outdated
@@ -140,16 +140,17 @@ class Viewer { | |||
|
|||
/** | |||
* @param {boolean} isLastBack true if back button was hit and viewer should hide. | |||
* @param {number} opt_poppedAmpHistoryIndex The AMP History Index that was just popped. | |||
* @param {boolean} isAMP true if going to AMP document. | |||
* @param {number|undefined} opt_poppedAmpHistoryIndex The AMP History Index that was just popped. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is still a bit unclear. When you pop, the state in the event will be that of the incoming state, not the previous state. I think it's confusing what is meant by "just popped".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed. should be more clear now
src/viewer.js
Outdated
// The back button was hit while a lightbox or sidebar is open. | ||
// We subtract 1 from opt_poppedAmpHistoryIndex to tell the AMP | ||
// page to go back to the previous state (close the lightbox/sidebar). | ||
{'newStackIndex': opt_poppedAmpHistoryIndex - 1}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to subtract 1. The state should be the newly active state, which is what you want to provide to the document.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I don't need this part of the code at all
src/viewer.js
Outdated
this.history_.goBack(); | ||
return Promise.resolve(); | ||
default: | ||
return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider rejecting with an unsupported message error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/viewer.js
Outdated
} | ||
|
||
/** | ||
* Attaches the AMP Doc Iframe to the Host Element. | ||
*/ | ||
attach() { | ||
if (this.isLoaded_()) { | ||
this.history_.goForward(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I had a comment about this before, but don't see it now. I think you don't want to depend on what's on the forward history stack when you're reattching. If something else on the page has manipulated the history, it could be totally unrelated to AMP.
Sorry if my previous comment mislead you. I think what you had initially seemed reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
#24