Skip to content

Commit f3846bf

Browse files
committed
Chore: add more typescript support
1 parent 4b042d4 commit f3846bf

11 files changed

+770
-151
lines changed

package-lock.json

Lines changed: 558 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"sass": "^1.77.6",
3838
"typescript": "~5.4.0",
3939
"vite": "^5.1.6",
40+
"vite-plugin-dts": "^3.9.1",
4041
"vite-plugin-vue-devtools": "^7.0.18",
4142
"vitest": "^1.4.0",
4243
"vue-tsc": "^2.0.6"

src/plugin/components/OkButton.vue

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<button v-if="visible" :class="['dg-btn', 'dg-btn--ok', {'dg-btn--loading': loading}, {'dg-pull-right': !options.reverse}]"
3-
@click.prevent="proceed()" ref="btn" :disabled="is_disabled">
3+
@click.prevent="proceed()" ref="btn" :disabled="disabled">
44
<span class="dg-btn-content">
55
<slot></slot>
66
<span v-if="soft_confirm">({{ clicks_remaining }})</span>
@@ -43,6 +43,14 @@ export default defineComponent({
4343
required: false,
4444
type: Boolean,
4545
'default': false
46+
},
47+
btnState: {
48+
required: true,
49+
type: Object,
50+
'default': {
51+
disabled: true,
52+
visible: true,
53+
}
4654
}
4755
},
4856
mounted(){
@@ -55,16 +63,16 @@ export default defineComponent({
5563
hard_confirm(){
5664
return (this.options.type === CONFIRM_TYPES.HARD)
5765
},
58-
is_disabled(){
59-
return (this.$parent.okBtnDisabled)
66+
disabled(){
67+
return this.btnState.disabled
6068
},
6169
clicks_remaining(){
6270
return Math.max((this.options.clicksCount - this.clicks_count), 0)
6371
}
6472
},
6573
methods: {
6674
proceed(){
67-
if(!this.is_disabled && this.validateProceed()){
75+
if(!this.disabled && this.validateProceed()){
6876
this.$emit('click')
6977
}
7078
},
@@ -73,7 +81,6 @@ export default defineComponent({
7381
case CONFIRM_TYPES.SOFT:
7482
this.clicks_count++
7583
return (this.clicks_count >= this.options.clicksCount)
76-
break;
7784
case CONFIRM_TYPES.BASIC:
7885
default:
7986
return true;

src/plugin/directives.ts renamed to src/plugin/directive.dialog.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { noop, clickNode, cloneObj } from './utilities'
44
import { CONFIRM_TYPES } from './constants'
55

6-
const Directives = function (app) {
6+
const DirectiveDialog = function (app) {
77
Object.defineProperties(this, {
88
Vue: { get: () => app },
99
confirmDefinition: {
@@ -12,21 +12,14 @@ const Directives = function (app) {
1212
})
1313
}
1414

15-
Directives.prototype.getConfirmDirective = function (binding) {
15+
DirectiveDialog.prototype.getConfirmMessage = function (binding) {
1616
if (binding.value && binding.value.message) {
1717
return binding.value.message
1818
}
1919
return typeof binding.value === 'string' ? binding.value : null
2020
}
2121

22-
Directives.prototype.getConfirmMessage = function (binding) {
23-
if (binding.value && binding.value.message) {
24-
return binding.value.message
25-
}
26-
return typeof binding.value === 'string' ? binding.value : null
27-
}
28-
29-
Directives.prototype.getOptions = function (binding) {
22+
DirectiveDialog.prototype.getOptions = function (binding) {
3023
const options = typeof binding.value === 'object' ? cloneObj(binding.value) : {}
3124

3225
delete options['ok']
@@ -39,7 +32,7 @@ Directives.prototype.getOptions = function (binding) {
3932
return options
4033
}
4134

42-
Directives.prototype.getThenCallback = function (binding, el) {
35+
DirectiveDialog.prototype.getThenCallback = function (binding, el) {
4336
if (binding.value && binding.value.ok) {
4437
return dialog => binding.value.ok({ ...dialog, node: el })
4538
} else {
@@ -60,14 +53,14 @@ Directives.prototype.getThenCallback = function (binding, el) {
6053
}
6154
}
6255

63-
Directives.prototype.getCatchCallback = function (binding) {
56+
DirectiveDialog.prototype.getCatchCallback = function (binding) {
6457
if (binding.value && binding.value.cancel) {
6558
return binding.value.cancel
6659
}
6760
return noop
6861
}
6962

70-
Directives.prototype.clickHandler = function (event, el, binding) {
63+
DirectiveDialog.prototype.clickHandler = function (event, el, binding) {
7164
event.preventDefault()
7265
event.stopImmediatePropagation()
7366

@@ -82,7 +75,7 @@ Directives.prototype.clickHandler = function (event, el, binding) {
8275
.catch(catchCallback)
8376
}
8477

85-
Directives.prototype.defineConfirm = function () {
78+
DirectiveDialog.prototype.defineConfirm = function () {
8679
type BindFn = (el: unknown, binding: unknown) => void
8780
const DirectiveDefinition: {bind: BindFn, unbind: BindFn} = {}
8881

@@ -101,4 +94,4 @@ Directives.prototype.defineConfirm = function () {
10194
return DirectiveDefinition
10295
}
10396

104-
export default Directives
97+
export default DirectiveDialog

src/plugin/index.ts

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,20 @@
11
'use strict'
22

3-
import Promise from 'promise-polyfill'
4-
import DialogComponent from './components/DialogApp.vue'
5-
import { DIALOG_TYPES, DEFAULT_OPTIONS } from './constants'
6-
import Directives from './directives'
7-
import { mergeObjs } from './utilities'
8-
import {createApp} from "vue";
3+
import DirectiveDialog from './directive.dialog'
94

10-
import type {App, ComponentInstance} from "vue";
5+
import type {App} from "vue";
6+
import type {DialogWindowOptionsInterface} from "./interface";
7+
import {PromiseDialog} from "./promise.dialog";
118

12-
enum DialogType {
13-
alert = DIALOG_TYPES.ALERT,
14-
confirm = DIALOG_TYPES.CONFIRM,
15-
prompt = DIALOG_TYPES.PROMPT,
16-
}
17-
18-
interface DialogWindowOptions {
19-
id: string;
20-
message: string;
21-
window: DialogType;
22-
promiseResolver: () => void;
23-
promiseRejecter: () => void;
24-
}
25-
26-
interface DialogPluginOptions extends Omit<DialogWindowOptions, 'id'>{}
27-
28-
const registeredViews = {}
29-
30-
class DialogPluginV3 {
31-
private dgApp: ComponentInstance<typeof DialogComponent>;
32-
33-
private mounted = false;
34-
private registeredViews = {}
35-
36-
constructor(
37-
private readonly app: App,
38-
private readonly globalOptions: Partial<DialogPluginOptions>,
39-
) {
40-
this.globalOptions = mergeObjs(DEFAULT_OPTIONS, globalOptions)
41-
}
42-
43-
public open(type: DialogType, message: string = undefined, localOptions: Partial<DialogWindowOptions> = {}) {
44-
this.mountIfNotMounted()
45-
return new Promise((resolve, reject) => {
46-
localOptions.id = 'dialog.' + Date.now()
47-
localOptions.window = type
48-
localOptions.message = message
49-
localOptions.promiseResolver = resolve
50-
localOptions.promiseRejecter = reject
51-
52-
this.dgApp.commit(mergeObjs(this.globalOptions, localOptions))
53-
})
54-
}
55-
56-
public alert(message?: string, options?: Partial<DialogWindowOptions>) {
57-
return this.open(DialogType.alert, message, {
58-
...(options || {}),
59-
...(message ? {message} : {}),
60-
})
61-
}
62-
63-
public confirm(message?: string, options?: Partial<DialogWindowOptions>) {
64-
return this.open(DialogType.confirm, message, {
65-
...(options || {}),
66-
...(message ? {message} : {}),
67-
})
68-
}
699

70-
public prompt(message?: string, options?: Partial<DialogWindowOptions>) {
71-
return this.open(DialogType.prompt, message, {
72-
...(options || {}),
73-
...(message ? {message} : {}),
74-
})
75-
}
76-
77-
private mountIfNotMounted(): void {
78-
if (this.mounted) return
79-
80-
this.dgApp = (() => {
81-
const dialogApp = createApp(DialogComponent)
82-
const node = document.createElement('div')
83-
document.querySelector('body').appendChild(node)
84-
return dialogApp.mount(node) as ComponentInstance<DialogComponent>
85-
})()
86-
87-
this.mounted = true
88-
}
89-
}
10+
interface DialogPluginOptions extends Omit<DialogWindowOptionsInterface, 'id'>{}
9011

91-
const Plugin = {
12+
const DialogPlugin = {
9213
install(app: App, options: DialogPluginOptions) {
93-
const DirectivesObj = new Directives(app)
94-
app.directive('confirm', DirectivesObj.defineConfirm())
14+
const DirectivesObj = new DirectiveDialog(app)
15+
app.directive('confirm', DirectivesObj.confirmDefinition)
9516

96-
const dialog = new DialogPluginV3(app, options)
17+
const dialog = new PromiseDialog(app, options)
9718

9819
Object.defineProperties(app.config.globalProperties, {
9920
$dialog: {
@@ -105,4 +26,4 @@ const Plugin = {
10526
},
10627
}
10728

108-
export default Plugin
29+
export default DialogPlugin

src/plugin/interface.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
export interface ButtonStateInterface {
3+
loading: boolean;
4+
disabled: boolean;
5+
visible: boolean;
6+
options: {[k: string]: any};
7+
component: any;
8+
}
9+
10+
11+
export enum DialogTypeEnum {
12+
alert = 'alert',
13+
confirm = 'confirm',
14+
prompt = 'prompt',
15+
}
16+
17+
export interface DialogWindowOptionsInterface {
18+
id: string;
19+
message: string;
20+
window: DialogTypeEnum;
21+
promiseResolver: (payload?: {
22+
data: string;
23+
node?: HTMLElement;
24+
setLoading: (value: boolean) => void;
25+
close: (isLoading: boolean) => void;
26+
}) => void;
27+
promiseRejecter: () => void;
28+
}

src/plugin/promise.dialog.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
import Promise from 'promise-polyfill'
4+
import DialogComponent from './components/DialogApp.vue'
5+
import { DEFAULT_OPTIONS } from './constants'
6+
import { mergeObjs } from './utilities'
7+
import {createApp} from "vue";
8+
9+
import type {App, ComponentInstance} from "vue";
10+
import {DialogTypeEnum} from "./interface";
11+
import type {DialogWindowOptionsInterface} from "./interface";
12+
13+
14+
interface DialogPluginOptions extends Omit<DialogWindowOptionsInterface, 'id'>{}
15+
16+
17+
export class PromiseDialog {
18+
private dgApp: ComponentInstance<typeof DialogComponent>;
19+
20+
private mounted = false;
21+
22+
constructor(
23+
private readonly app: App,
24+
private readonly globalOptions: Partial<DialogPluginOptions>,
25+
) {
26+
this.globalOptions = mergeObjs(DEFAULT_OPTIONS, globalOptions)
27+
}
28+
29+
public open(type: DialogTypeEnum, message: string = undefined, localOptions: Partial<DialogWindowOptionsInterface> = {}) {
30+
this.mountIfNotMounted()
31+
return new Promise((resolve, reject) => {
32+
localOptions.id = 'dialog.' + Date.now()
33+
localOptions.window = type
34+
localOptions.message = message
35+
localOptions.promiseResolver = resolve
36+
localOptions.promiseRejecter = reject
37+
38+
this.dgApp.commit(mergeObjs(this.globalOptions, localOptions))
39+
})
40+
}
41+
42+
public alert(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
43+
return this.open(DialogTypeEnum.alert, message, {
44+
...(options || {}),
45+
...(message ? {message} : {}),
46+
})
47+
}
48+
49+
public confirm(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
50+
return this.open(DialogTypeEnum.confirm, message, {
51+
...(options || {}),
52+
...(message ? {message} : {}),
53+
})
54+
}
55+
56+
public prompt(message?: string, options?: Partial<DialogWindowOptionsInterface>) {
57+
return this.open(DialogTypeEnum.prompt, message, {
58+
...(options || {}),
59+
...(message ? {message} : {}),
60+
})
61+
}
62+
63+
private mountIfNotMounted(): void {
64+
if (this.mounted) return
65+
66+
this.dgApp = (() => {
67+
const dialogApp = createApp(DialogComponent)
68+
const node = document.createElement('div')
69+
document.querySelector('body').appendChild(node)
70+
return dialogApp.mount(node) as ComponentInstance<DialogComponent>
71+
})()
72+
73+
this.mounted = true
74+
}
75+
}

src/plugin/types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {createApp} from "vue";
2+
3+
declare module '@vue/runtime-core' {
4+
import {PromiseDialog} from "./promise.dialog";
5+
6+
interface ComponentCustomProperties {
7+
$dialog: typeof PromiseDialog
8+
}
9+
}

0 commit comments

Comments
 (0)