Skip to content

Commit

Permalink
Merge pull request #1434 from dvoytenko/access17c
Browse files Browse the repository at this point in the history
Standard action to hide elements
  • Loading branch information
dvoytenko committed Jan 15, 2016
2 parents 2061eed + 5415a33 commit 4d1ba58
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
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}}!
</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');
});
});

0 comments on commit 4d1ba58

Please sign in to comment.