grafana/public/app/angular/services/popover_srv.ts

82 lines
1.7 KiB
TypeScript
Raw Normal View History

import { extend } from 'lodash';
// @ts-ignore
2017-12-20 05:33:33 -06:00
import Drop from 'tether-drop';
import { GrafanaRootScope } from 'app/angular/GrafanaCtrl';
import coreModule from 'app/angular/core_module';
coreModule.service('popoverSrv', ['$compile', '$rootScope', '$timeout', popoverSrv]);
function popoverSrv(this: any, $compile: any, $rootScope: GrafanaRootScope, $timeout: any) {
let openDrop: any = null;
2017-04-14 04:41:02 -05:00
this.close = () => {
2017-04-14 04:41:02 -05:00
if (openDrop) {
openDrop.close();
}
};
this.show = (options: any) => {
2017-04-14 04:41:02 -05:00
if (openDrop) {
openDrop.close();
2017-04-14 05:23:32 -05:00
openDrop = null;
2017-04-14 04:41:02 -05:00
}
const scope = extend($rootScope.$new(true), options.model);
let drop: any;
const cleanUp = () => {
2017-04-14 04:41:02 -05:00
setTimeout(() => {
scope.$destroy();
2017-04-14 05:23:32 -05:00
if (drop.tether) {
drop.destroy();
}
2017-04-13 11:39:49 -05:00
2017-04-14 04:41:02 -05:00
if (options.onClose) {
options.onClose();
}
});
2017-04-14 05:23:32 -05:00
openDrop = null;
2017-04-14 04:41:02 -05:00
};
2017-04-14 04:41:02 -05:00
scope.dismiss = () => {
drop.close();
};
const contentElement = document.createElement('div');
contentElement.innerHTML = options.template;
2017-04-14 04:41:02 -05:00
$compile(contentElement)(scope);
2017-04-14 05:23:32 -05:00
$timeout(() => {
drop = new Drop({
target: options.element,
content: contentElement,
position: options.position,
2017-12-20 05:33:33 -06:00
classes: options.classNames || 'drop-popover',
2017-04-14 05:23:32 -05:00
openOn: options.openOn,
hoverCloseDelay: 200,
tetherOptions: {
2017-12-20 05:33:33 -06:00
constraints: [{ to: 'scrollParent', attachment: 'together' }],
},
2017-04-14 05:23:32 -05:00
});
2017-12-20 05:33:33 -06:00
drop.on('close', () => {
2017-04-14 05:23:32 -05:00
cleanUp();
});
openDrop = drop;
openDrop.open();
2017-04-14 05:46:02 -05:00
}, 100);
// return close function
return () => {
if (drop) {
drop.close();
}
};
};
}