api-console-assets
Version:
This repo only exists to publish api console components to npm
106 lines (100 loc) • 3.44 kB
HTML
<!--
@license
Copyright 2017 The Advanced REST client authors <arc@mulesoft.com>
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.
-->
<link rel="import" href="../polymer/polymer.html">
<script>
(function() {
;
window.ArcBehaviors = window.ArcBehaviors || {};
/**
* A `ArcBehaviors.EventsTargetBehavior` is a base behavior that allows setting
* up event listeners on a default or set node.
*
* By default the element listens on the `window` element for events. By setting
* `eventsTarget` property of this element it removes all previously set
* listeners and adds the same listeners to the node.
* It also restores default state when the `eventsTarget` is removed.
*
* Implementations should implement two abstract methods:
* `_attachListeners(node)` and `_detachListeners(node)`. Both of them will be
* called with event target argument when it's required to either set or remove
* listeners.
*
* ### Example (Polymer 1.x)
*
* ```javascript
* Polymer({
* is: 'my-element',
* behaviors: [ArcBehaviors.EventsTargetBehavior],
* _attachListeners: function(node) {
* this.listen(node, 'event-type', 'handlerName');
* },
* _detachListeners: function(node) {
* this.unlisten(node, 'event-type', 'handlerName');
* }
* });
* ```
*
* The behavior handles attached / detached state and calls the functions with
* required parameters.
*
* @polymerBehavior ArcBehaviors.EventsTargetBehavior
* @group Logic Elements
*/
window.ArcBehaviors.EventsTargetBehavior = {
properties: {
/**
* Events handlers target. By default the element listens on
* `window` object. If set, all events listeners will be attached to this
* object instead of `window`.
*/
eventsTarget: {
type: Object,
observer: '_eventsTargetChanged'
},
// An event target used to attach listeners.
_oldEventsTarget: Object
},
attached: function() {
this._eventsTargetChanged(this.eventsTarget);
},
detached: function() {
if (this._oldEventsTarget) {
this._detachListeners(this._oldEventsTarget);
}
},
/**
* Removes old handlers (if any) and attaches listeners on new event
* event target.
*
* @param {?Node} eventsTarget Event target to set handlers on. If not set it
* will set handlers on the `window` object.
*/
_eventsTargetChanged: function(eventsTarget) {
if (this._oldEventsTarget) {
this._detachListeners(this._oldEventsTarget);
}
this._oldEventsTarget = eventsTarget || window;
this._attachListeners(this._oldEventsTarget);
},
// To be implement by the element to set event listeners on the target
_attachListeners: function() {
console.warn('_attachListeners not found in the element methods');
},
// To be implement by the element to remove event listeners from the target
_detachListeners: function() {
console.warn('_detachListeners not found in the element methods');
}
};
})();
</script>