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

Refactor ActionService into install/get pattern #1255

Merged
merged 1 commit into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/analytics.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<script type="application/json">
{
"vars": {
"account": "UA-XXXX-Y"
"account": "UA-YYYY-Y"
},
"triggers": {
"default pageview": {
Expand Down
271 changes: 5 additions & 266 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,274 +14,13 @@
* limitations under the License.
*/

import {log} from './log';

/** @const {string} */
const TAG_ = 'Action';

/** @const {string} */
const ACTION_MAP_ = '__AMP_ACTION_MAP__' + Math.random();

/** @const {string} */
const DEFAULT_METHOD_ = 'activate';
import {getService} from './service';


/**
* @typedef {{
* event: string,
* target: string,
* method: string,
* str: string
* }}
* @param {!Window} win
* @return {!Action}
*/
class ActionInfoDef {};


/**
* The structure that contains all details of the action method invocation.
* @struct
* @const
* TODO(dvoytenko): add action arguments here as well.
*/
class ActionInvocation {
/**
* @param {!Element} target
* @param {string} method
* @param {?Element} source
* @param {?Event} event
*/
constructor(target, method, source, event) {
/** @const {!Element} */
this.target = target;
/** @const {string} */
this.method = method;
/** @const {?Element} */
this.source = source;
/** @const {?Event} */
this.event = event;
}
}



/**
* TODO(dvoytenko): consider splitting this class into two:
* 1. A class that has a method "trigger(element, eventType, data)" and
* simply can search target in DOM and trigger methods on it.
* 2. A class that configures event recognizers and rules and then
* simply calls action.trigger.
*/
export class Action {

/**
* @param {!Window} win
*/
constructor(win) {
/** @const {!Window} */
this.win = win;
}

/**
* @param {string} name
* TODO(dvoytenko): switch to a system where the event recognizers are
* registered with Action instead, e.g. "doubletap", "tap to zoom".
*/
addEvent(name) {
if (name == 'tap') {
// TODO(dvoytenko): if needed, also configure touch-based tap, e.g. for
// fast-click.
this.win.document.addEventListener('click', event => {
if (!event.defaultPrevented) {
this.trigger(event.target, 'tap', event);
}
});
}
}

/**
* Triggers the specified event on the target element.
* @param {!Element} target
* @param {string} eventType
* @param {?Event} event
*/
trigger(target, eventType, event) {
this.action_(target, eventType, event);
}

/**
* Triggers execution of the method on a target/method.
* @param {!Element} target
* @param {string} method
* @param {?Element} source
* @param {?Event} event
*/
execute(target, method, source, event) {
this.invoke_(target, method, source, event, null);
}

/**
* @param {!Element} source
* @param {string} actionEventType
* @param {!Event} event
* @private
*/
action_(source, actionEventType, event) {
const action = this.findAction_(source, actionEventType);
if (!action) {
// TODO(dvoytenko): implement default (catch-all) actions.
return;
}

const target = document.getElementById(action.actionInfo.target);
if (!target) {
this.actionInfoError_('target not found', action.actionInfo, target);
return;
}

this.invoke_(target, action.actionInfo.method,
action.node, event, action.actionInfo);
}

/**
* The errors that are a result of action definition.
* @param {string} s
* @param {?ActionInfo} actionInfo
* @param {?Element} target
* @private
*/
actionInfoError_(s, actionInfo, target) {
// Method not found "activate" on ' + target
throw new Error('Action Error: ' + s +
(actionInfo ? ' in [' + actionInfo.str + ']' : '') +
(target ? ' on [' + target + ']' : ''));
}

/**
* @param {!Element} target
* @param {string} method
* @param {?Element} source
* @param {?Event} event
* @param {?ActionInfo} actionInfo
*/
invoke_(target, method, source, event, actionInfo) {
const invocation = new ActionInvocation(target, method, source, event);

// TODO(dvoytenko): implement common method handlers, e.g. "toggleClass"

// Only amp elements are allowed to proceed further.
if (target.tagName.toLowerCase().substring(0, 4) != 'amp-') {
this.actionInfoError_('Target must be an AMP element', actionInfo,
target);
return;
}

if (!target.enqueAction) {
this.actionInfoError_('Unrecognized AMP element "' +
target.tagName.toLowerCase() + '". ' +
'Did you forget to include it via <script custom-element>?',
actionInfo, target);
return;
}

target.enqueAction(invocation);
}

/**
* @param {!Element} target
* @param {string} actionEventType
* @return {?{node: !Element, actionInfo: !ActionInfoDef}}
*/
findAction_(target, actionEventType) {
// Go from target up the DOM tree and find the applicable action.
let n = target;
let actionInfo = null;
while (n) {
actionInfo = this.matchActionInfo_(n, actionEventType);
if (actionInfo) {
return {node: n, actionInfo: actionInfo};
}
n = n.parentElement;
}
return null;
}

/**
* @param {!Element} node
* @param {string} actionEventType
* @return {?ActionInfoDef}
*/
matchActionInfo_(node, actionEventType) {
const actionMap = this.getActionMap_(node);
if (!actionMap) {
return null;
}
return actionMap[actionEventType] || null;
}

/**
* @param {!Element} node
* @return {?Object<string, ActionInfoDef>}
*/
getActionMap_(node) {
let actionMap = node[ACTION_MAP_];
if (actionMap === undefined) {
actionMap = null;
if (node.hasAttribute('on')) {
actionMap = this.parseActionMap_(node.getAttribute('on'));
}
node[ACTION_MAP_] = actionMap;
}
return actionMap;
}

/**
* @param {string} s
* @return {?Object<string, ActionInfoDef>}
*/
parseActionMap_(s) {
let actionMap = null;
const actions = s.split(';');
if (actions && actions.length > 0) {
for (let i = 0; i < actions.length; i++) {
const actionStr = actions[i];
const actionInfo = this.parseAction_(actionStr);
if (actionInfo) {
if (!actionMap) {
actionMap = {};
}
actionMap[actionInfo.event] = actionInfo;
}
}
}
return actionMap;
}

/**
* @param {string} s
* @return {?ActionInfoDef}
*/
parseAction_(s) {
s = s.trim();
if (!s) {
return null;
}

const eventSep = s.indexOf(':');
const methodSep = s.indexOf('.', eventSep + 1);
const event = (eventSep != -1 ? s.substring(0, eventSep) : '').toLowerCase()
.trim() || null;
const target = s.substring(eventSep + 1, methodSep != -1 ? methodSep :
s.length).trim();
const method = (methodSep != -1 ? s.substring(methodSep + 1) : '')
.trim() || DEFAULT_METHOD_;

if (!event || !target) {
log.error(TAG_, 'invalid action definition: ' + s);
return null;
}
return {event: event, target: target, method: method, str: s};
}
export function actionServiceFor(win) {
return getService(win, 'action');
};


export const action = new Action(window);
2 changes: 2 additions & 0 deletions src/amp-core-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {installActionService} from './service/action-impl';
import {installHistoryService} from './service/history-impl';
import {installViewerService} from './service/viewer-impl';
import {installViewportService} from './service/viewport-impl';
Expand All @@ -30,4 +31,5 @@ export function installCoreServices(window) {
installViewportService(window);
installHistoryService(window);
installVsyncService(window);
installActionService(window).addEvent('tap');
}
2 changes: 0 additions & 2 deletions src/amp.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {installErrorReporting} from './error';
import {stubElements} from './custom-element';
import {adopt} from './runtime';
import {cssText} from '../build/css';
import {action} from './action';
import {maybeValidate} from './validator-integration';

// We must under all circumstances call makeBodyVisible.
Expand All @@ -53,7 +52,6 @@ try {

adopt(window);
stubElements(window);
action.addEvent('tap');

installPullToRefreshBlocker(window);
installGlobalClickListener(window);
Expand Down
Loading