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

Standard action to hide elements #1434

Merged
merged 1 commit into from
Jan 15, 2016
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: 2 additions & 0 deletions build-system/tasks/presubmit-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var forbiddenTerms = {
message: privateServiceFactory,
whitelist: [
'src/service/action-impl.js',
'src/service/standard-actions-impl.js',
'src/amp-core-service.js',
],
},
Expand Down Expand Up @@ -104,6 +105,7 @@ var forbiddenTerms = {
whitelist: [
'src/amp-core-service.js',
'src/service/resources-impl.js',
'src/service/standard-actions-impl.js',
],
},
// Privacy sensitive
Expand Down
3 changes: 2 additions & 1 deletion examples/article-access.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ <h1 itemprop="headline">Lorem Ipsum</h1>
</div>
</div>

<section amp-access="views > 0" amp-access-hide class="meter-section">
<section id="meter-notif" amp-access="views > 0" amp-access-hide class="meter-section">
<template amp-access-template type="amp-mustache">
You are viewing article {{views}} of {{maxViews}}!
Copy link

Choose a reason for hiding this comment

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

Nit for complete example: if views > maxViews it can say "You have read your {{maxViews}} free articles. Please login to read more"

</template>
<a on="tap:meter-notif.hide">[Close]</a>
</section>

<section amp-access="NOT access" amp-access-hide class="login-section">
Expand Down
2 changes: 2 additions & 0 deletions src/amp-core-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {installActionService} from './service/action-impl';
import {installHistoryService} from './service/history-impl';
import {installResourcesService} from './service/resources-impl';
import {installStandardActions} from './service/standard-actions-impl';
import {installViewerService} from './service/viewer-impl';
import {installViewportService} from './service/viewport-impl';
import {installVsyncService} from './service/vsync-impl';
Expand All @@ -34,4 +35,5 @@ export function installCoreServices(window) {
installVsyncService(window);
installActionService(window);
installResourcesService(window);
installStandardActions(window);
}
62 changes: 62 additions & 0 deletions src/service/standard-actions-impl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {getService} from '../service';
import {installActionService} from './action-impl';
import {installResourcesService} from './resources-impl';


/**
* This service contains implementations of some of the most typical actions,
* such as hiding DOM elements.
* @private Visible for testing.
*/
export class StandardActions {
/**
* @param {!Window} win
*/
constructor(win) {
/** @const @private {!ActionService} */
this.actions_ = installActionService(win);

/** @const @private {!Resources} */
this.resources_ = installResourcesService(win);

this.actions_.addGlobalMethodHandler('hide', this.handleHide.bind(this));
}

/**
* Handles "hide" action. This is a very simple action where "display: none"
* is applied to the target element.
* @param {!ActionInvocation} invocation
*/
handleHide(invocation) {
this.resources_.mutateElement(invocation.target, () => {
invocation.target.style.display = 'none';
});
}
}


/**
* @param {!Window} win
* @return {!ActionService}
*/
export function installStandardActions(win) {
return getService(win, 'standard-actions', () => {
return new StandardActions(win);
});
};
45 changes: 45 additions & 0 deletions test/functional/test-standard-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {StandardActions} from '../../src/service/standard-actions-impl';
import * as sinon from 'sinon';

describe('StandardActions', () => {

let sandbox;
let actions;
let mutateElementStub;

beforeEach(() => {
sandbox = sinon.sandbox.create();
actions = new StandardActions(window);
mutateElementStub = sandbox.stub(actions.resources_, 'mutateElement',
(unusedElement, mutator) => mutator());
});

afterEach(() => {
sandbox.restore();
sandbox = null;
});

it('should handle "hide" action', () => {
const element = document.createElement('div');
actions.handleHide({target: element});
expect(mutateElementStub.callCount).to.equal(1);
expect(mutateElementStub.firstCall.args[0]).to.equal(element);
expect(element.style.display).to.equal('none');
});
});