-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Add AMP.navigateTo action #9932
Changes from all commits
ffc0936
7609c53
e0b7047
143d929
2bca73d
f004ff3
cd6d9dd
4a402ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,14 @@ | |
import {ActionTrust} from '../action-trust'; | ||
import {OBJECT_STRING_ARGS_KEY} from '../service/action-impl'; | ||
import {Layout, getLayoutClass} from '../layout'; | ||
import {actionServiceForDoc} from '../services'; | ||
import {actionServiceForDoc, urlReplacementsForDoc} from '../services'; | ||
import {bindForDoc} from '../services'; | ||
import {computedStyle, getStyle, toggle} from '../style'; | ||
import {dev, user} from '../log'; | ||
import {registerServiceBuilderForDoc} from '../service'; | ||
import {historyForDoc} from '../services'; | ||
import {isProtocolValid} from '../url'; | ||
import {registerServiceBuilderForDoc} from '../service'; | ||
import {resourcesForDoc} from '../services'; | ||
import {computedStyle, getStyle, toggle} from '../style'; | ||
import {vsyncFor} from '../services'; | ||
|
||
/** | ||
|
@@ -35,6 +36,9 @@ function isShowable(element) { | |
|| element.hasAttribute('hidden'); | ||
} | ||
|
||
/** @const {string} */ | ||
const TAG = 'STANDARD-ACTIONS'; | ||
|
||
/** | ||
* This service contains implementations of some of the most typical actions, | ||
* such as hiding DOM elements. | ||
|
@@ -55,6 +59,9 @@ export class StandardActions { | |
/** @const @private {!./resources-impl.Resources} */ | ||
this.resources_ = resourcesForDoc(ampdoc); | ||
|
||
/** @const @private {!./url-replacements-impl.UrlReplacements} */ | ||
this.urlReplacements_ = urlReplacementsForDoc(ampdoc); | ||
|
||
this.installActions_(this.actions_); | ||
} | ||
|
||
|
@@ -87,6 +94,9 @@ export class StandardActions { | |
case 'setState': | ||
this.handleAmpSetState_(invocation); | ||
return; | ||
case 'navigateTo': | ||
this.handleAmpNavigateTo_(invocation); | ||
return; | ||
case 'goBack': | ||
this.handleAmpGoBack_(invocation); | ||
return; | ||
|
@@ -121,6 +131,25 @@ export class StandardActions { | |
}); | ||
} | ||
|
||
/** | ||
* @param {!./action-impl.ActionInvocation} invocation | ||
* @private | ||
*/ | ||
handleAmpNavigateTo_(invocation) { | ||
if (!invocation.satisfiesTrust(ActionTrust.HIGH)) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already happens inside |
||
} | ||
const url = invocation.args['url']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would an error message here be better, or would you prefer it silently fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Added a user error. |
||
if (!isProtocolValid(url)) { | ||
user().error(TAG, 'Cannot navigate to invalid protocol: ' + url); | ||
return; | ||
} | ||
const expandedUrl = this.urlReplacements_.expandUrlSync(url); | ||
const node = invocation.target; | ||
const win = (node.ownerDocument || node).defaultView; | ||
win.location = expandedUrl; | ||
} | ||
|
||
/** | ||
* @param {!./action-impl.ActionInvocation} invocation | ||
* @private | ||
|
@@ -161,7 +190,7 @@ export class StandardActions { | |
|
||
if (target.classList.contains(getLayoutClass(Layout.NODISPLAY))) { | ||
user().warn( | ||
'STANDARD-ACTIONS', | ||
TAG, | ||
'Elements with layout=nodisplay cannot be dynamically shown.', | ||
target); | ||
|
||
|
@@ -173,7 +202,7 @@ export class StandardActions { | |
!isShowable(target)) { | ||
|
||
user().warn( | ||
'STANDARD-ACTIONS', | ||
TAG, | ||
'Elements can only be dynamically shown when they have the ' + | ||
'"hidden" attribute set or when they were dynamically hidden.', | ||
target); | ||
|
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.
@jridgewell FYI I think this is the correct trust level for clicking a button via keyboard.