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

77 lines
1.6 KiB
TypeScript
Raw Normal View History

///<reference path="../../headers/common.d.ts" />
import config from 'app/core/config';
import _ from 'lodash';
import $ from 'jquery';
import coreModule from 'app/core/core_module';
2016-02-22 08:15:01 -06:00
import Drop from 'tether-drop';
/** @ngInject **/
2017-04-14 04:41:02 -05:00
function popoverSrv($compile, $rootScope, $timeout) {
let openDrop = null;
this.close = function() {
if (openDrop) {
openDrop.close();
}
};
this.show = function(options) {
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
}
var scope = _.extend($rootScope.$new(true), options.model);
var drop;
2017-04-14 04:41:02 -05:00
var cleanUp = () => {
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();
};
var 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,
classes: options.classNames || 'drop-popover',
openOn: options.openOn,
hoverCloseDelay: 200,
tetherOptions: {
constraints: [{to: 'scrollParent', attachment: "none both"}]
}
});
drop.on('close', () => {
cleanUp();
});
openDrop = drop;
openDrop.open();
}, 10);
};
}
coreModule.service('popoverSrv', popoverSrv);