Skip to content
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

Merged
merged 9 commits into from
Jul 7, 2017
Merged

Conversation

chenshay
Copy link
Collaborator

#24

@chenshay chenshay requested a review from ericfs June 20, 2017 23:04
src/history.js Outdated
});
}

/**
* Updates the history state.
*/
decreaseCurrentStateId() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

Copy link
Collaborator Author

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

* 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is RequestHandler defined?

Copy link
Collaborator Author

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{!Function():boolean}

Copy link
Collaborator Author

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_);
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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
Copy link
Collaborator

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

Copy link
Collaborator Author

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},
Copy link
Collaborator

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

Copy link
Collaborator Author

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state.stateId !== undefined

Copy link
Collaborator Author

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();
}
Copy link
Collaborator

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?

Copy link
Collaborator Author

@chenshay chenshay Jun 21, 2017

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(
Copy link
Collaborator

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.

Copy link
Collaborator Author

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;
Copy link
Collaborator

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.

Copy link
Collaborator Author

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

Copy link
Collaborator Author

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_()) {
Copy link
Collaborator

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.

Copy link
Collaborator Author

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_) {
Copy link
Collaborator

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.

Copy link
Collaborator Author

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_);
Copy link
Collaborator

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.

Copy link
Collaborator

@ericfs ericfs left a 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.
Copy link
Collaborator

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".

Copy link
Collaborator Author

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},
Copy link
Collaborator

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.

Copy link
Collaborator Author

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;
Copy link
Collaborator

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.

Copy link
Collaborator Author

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();
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@chenshay chenshay merged commit 12ffdeb into ampproject:master Jul 7, 2017
@chenshay chenshay deleted the history2 branch July 7, 2017 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants