2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import Drop from 'tether-drop';
|
2016-02-22 07:20:34 -06:00
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2018-08-30 03:49:18 -05:00
|
|
|
function popoverSrv(this: any, $compile, $rootScope, $timeout) {
|
2017-04-14 04:41:02 -05:00
|
|
|
let openDrop = null;
|
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
this.close = () => {
|
2017-04-14 04:41:02 -05:00
|
|
|
if (openDrop) {
|
|
|
|
openDrop.close();
|
|
|
|
}
|
|
|
|
};
|
2016-02-22 07:20:34 -06:00
|
|
|
|
2018-09-05 00:47:30 -05:00
|
|
|
this.show = 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
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const scope = _.extend($rootScope.$new(true), options.model);
|
|
|
|
let drop;
|
2016-02-22 11:46:58 -06:00
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
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();
|
2016-02-22 11:46:58 -06:00
|
|
|
}
|
|
|
|
});
|
2017-04-14 05:23:32 -05:00
|
|
|
|
|
|
|
openDrop = null;
|
2017-04-14 04:41:02 -05:00
|
|
|
};
|
2016-02-22 11:46:58 -06:00
|
|
|
|
2017-04-14 04:41:02 -05:00
|
|
|
scope.dismiss = () => {
|
|
|
|
drop.close();
|
2016-02-22 11:46:58 -06:00
|
|
|
};
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const contentElement = document.createElement('div');
|
2016-02-22 11:46:58 -06:00
|
|
|
contentElement.innerHTML = options.template;
|
2016-02-22 07:20:34 -06:00
|
|
|
|
2017-04-14 04:41:02 -05:00
|
|
|
$compile(contentElement)(scope);
|
2016-02-22 11:46:58 -06:00
|
|
|
|
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);
|
2017-12-03 00:53:47 -06:00
|
|
|
|
|
|
|
// return close function
|
2018-09-05 00:47:30 -05:00
|
|
|
return () => {
|
2017-12-03 00:53:47 -06:00
|
|
|
if (drop) {
|
|
|
|
drop.close();
|
|
|
|
}
|
|
|
|
};
|
2016-02-22 07:20:34 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
coreModule.service('popoverSrv', popoverSrv);
|