-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1434 from dvoytenko/access17c
Standard action to hide elements
- Loading branch information
Showing
5 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |