Files
grafana/public/app/core/services/popover_srv.ts

80 lines
1.6 KiB
TypeScript
Raw Normal View History

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