mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
Merge pull request #6507 from spalger/implement/sharedBinder
[binder] share binder code with ui and server
This commit is contained in:
commit
b4c703cb1c
2 changed files with 48 additions and 44 deletions
|
@ -1,49 +1,34 @@
|
|||
import d3 from 'd3';
|
||||
import { callEach } from 'lodash';
|
||||
import { bindKey } from 'lodash';
|
||||
import { rest } from 'lodash';
|
||||
import $ from 'jquery';
|
||||
|
||||
function Binder($scope) {
|
||||
this.disposal = [];
|
||||
if ($scope) {
|
||||
$scope.$on('$destroy', bindKey(this, 'destroy'));
|
||||
import Binder from '../../../utils/binder';
|
||||
|
||||
export default class UiBinder extends Binder {
|
||||
constructor($scope) {
|
||||
super();
|
||||
|
||||
// support auto-binding to $scope objects
|
||||
if ($scope) {
|
||||
$scope.$on('$destroy', () => this.destroy());
|
||||
}
|
||||
}
|
||||
|
||||
jqOn(el, ...args) {
|
||||
const $el = $(el);
|
||||
$el.on(...args);
|
||||
this.disposal.push(() => $el.off(...args));
|
||||
}
|
||||
|
||||
fakeD3Bind(el, event, handler) {
|
||||
this.jqOn(el, event, (e) => {
|
||||
// mimick https://github.com/mbostock/d3/blob/3abb00113662463e5c19eb87cd33f6d0ddc23bc0/src/selection/on.js#L87-L94
|
||||
const o = d3.event; // Events can be reentrant (e.g., focus).
|
||||
d3.event = e;
|
||||
try {
|
||||
handler.apply(this, [this.__data__]);
|
||||
} finally {
|
||||
d3.event = o;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Binder.prototype._bind = function (on, off, emitter, args) {
|
||||
on.apply(emitter, args);
|
||||
this.disposal.push(function () {
|
||||
off.apply(emitter, args);
|
||||
});
|
||||
};
|
||||
|
||||
Binder.prototype.on = function (emitter/*, ...args */) {
|
||||
this._bind(emitter.on, emitter.off || emitter.removeListener, emitter, rest(arguments));
|
||||
};
|
||||
|
||||
Binder.prototype.jqOn = function (el/*, ...args */) {
|
||||
var $el = $(el);
|
||||
this._bind($el.on, $el.off, $el, rest(arguments));
|
||||
};
|
||||
|
||||
Binder.prototype.fakeD3Bind = function (el, event, handler) {
|
||||
this.jqOn(el, event, function (e) {
|
||||
// mimick https://github.com/mbostock/d3/blob/3abb00113662463e5c19eb87cd33f6d0ddc23bc0/src/selection/on.js#L87-L94
|
||||
var o = d3.event; // Events can be reentrant (e.g., focus).
|
||||
d3.event = e;
|
||||
try {
|
||||
handler.apply(this, [this.__data__]);
|
||||
} finally {
|
||||
d3.event = o;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Binder.prototype.destroy = function () {
|
||||
var destroyers = this.disposal;
|
||||
this.disposal = [];
|
||||
callEach(destroyers);
|
||||
};
|
||||
|
||||
module.exports = Binder;
|
||||
|
|
19
src/utils/binder.js
Normal file
19
src/utils/binder.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
export default class Binder {
|
||||
constructor() {
|
||||
this.disposal = [];
|
||||
}
|
||||
|
||||
on(emitter, ...args) {
|
||||
const on = emitter.on || emitter.addListener;
|
||||
const off = emitter.off || emitter.removeListener;
|
||||
|
||||
on.apply(emitter, args);
|
||||
this.disposal.push(() => off.apply(emitter, args));
|
||||
}
|
||||
|
||||
destroy() {
|
||||
const destroyers = this.disposal;
|
||||
this.disposal = [];
|
||||
destroyers.forEach(fn => fn());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue