Files
mixtape/zero.Web.UI/App/services/overlay.js
T

134 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-04-08 23:33:56 +02:00
import Vue from 'vue';
2020-04-11 22:08:06 +02:00
import AppConfirm from 'zero/components/overlays/confirm';
import Strings from 'zero/services/strings';
import { find as _find, extend as _extend } from 'underscore';
2020-04-08 23:33:56 +02:00
export default new Vue({
2020-04-09 01:04:05 +02:00
data: () => ({
dropdownInstance: null,
2020-04-09 01:04:05 +02:00
instances: []
}),
2020-04-08 23:33:56 +02:00
methods: {
// sets a new active dropdown so the old one gets auto-closed
setDropdown(instance)
{
if (this.dropdownInstance != null)
{
this.dropdownInstance.hide();
}
this.dropdownInstance = instance;
},
// open a deletion confirm dialog with the given options
confirmDelete(title, text)
{
let options = _extend({
title: typeof title === 'string' ? title : '@deleteoverlay.title',
text: text || '@deleteoverlay.text',
confirmLabel: '@deleteoverlay.confirm',
confirmType: 'danger',
closeLabel: '@deleteoverlay.close',
component: AppConfirm,
autoclose: false,
softdismiss: false
}, typeof title === 'object' ? title : {});
return this.open(options);
},
2020-04-08 23:33:56 +02:00
// open a confirm dialog with the given options
confirm(title, text)
{
let options = _extend({
title: title,
text: text,
component: AppConfirm,
autoclose: true,
softdismiss: false
}, typeof title === 'object' ? title : {});
2020-04-08 23:33:56 +02:00
2020-04-09 01:04:05 +02:00
return this.open(options);
2020-04-08 23:33:56 +02:00
},
2020-04-09 01:04:05 +02:00
// opens an overlay
2020-04-08 23:33:56 +02:00
open(options)
{
2020-04-24 19:38:33 +02:00
const defaultWidth = options.display === 'editor' ? 560 : 460;
2020-04-09 01:04:05 +02:00
2020-04-24 15:13:54 +02:00
options = _extend({
id: Strings.guid(),
display: 'dialog',
width: defaultWidth,
hide: this.close,
autoclose: true,
softdismiss: options.display !== 'editor',
closeLabel: '@ui.close',
confirmLabel: '@ui.confirm',
confirmType: 'default'
2020-04-24 15:13:54 +02:00
}, options);
options.theme = 'default';
2020-04-09 01:04:05 +02:00
this.instances.push(options);
2020-04-08 23:33:56 +02:00
return new Promise((resolve, reject) =>
2020-04-09 01:04:05 +02:00
{
options.close = () =>
{
this.close(options);
reject(options);
2020-04-09 01:04:05 +02:00
};
options.confirm = data =>
{
if (options.autoclose)
{
this.close(options);
}
resolve(data, options);
2020-04-09 01:04:05 +02:00
};
});
},
// closes an overlay
close(instance)
{
if (this.instances.length < 1)
{
return;
}
if (!instance)
{
this.instances.pop();
return;
}
if (typeof instance === 'string')
{
instance = _find(this.instances, item => item.id === instance);
}
if (instance)
{
const index = this.instances.indexOf(instance);
this.instances.splice(index, 1);
}
},
// closes all overlays
closeAll()
{
this.instances.forEach(instance =>
2020-04-08 23:33:56 +02:00
{
});
}
}
});