Skip to content

Commit e3aab20

Browse files
committed
Added v-confirm directive.
1 parent 0c11539 commit e3aab20

13 files changed

+234
-1247
lines changed

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ this.$dialog.confirm("If you delete this record, it'll be gone forever.", {
7070
});
7171
```
7272

73+
## Usage as a directive (new)
74+
75+
If you don't pass a message, the global/default message would be used.
76+
```html
77+
<button type="submit" v-confirm="">submit</button>
78+
```
79+
80+
81+
```html
82+
// Callbacks can be provided
83+
// Note: If "loader" is set to true, the makeAdmin callback will be passed a "dialog" object
84+
// Which is useful for stoping the loader, or closing the dialog.
85+
<button v-confirm="{ok: makeAdmin, cancel: doNothing, message: 'User will be given admin privileges. Make user an Admin?'}">Make Admin</button>
86+
```
87+
```javascript
88+
methods: {
89+
makeAdmin: function() {
90+
// Do stuffs
91+
92+
},
93+
doNothing: function() {
94+
// Do nothing or some other stuffs
95+
}
96+
}
97+
```
98+
99+
100+
For v-confirm directive, if an "OK" callback is not provided, the default event would be triggered.
101+
102+
```html
103+
// You can use it on links too
104+
<a href="http://example.com" v-confirm="'This will take you to http://example.com. Proceed with caution'">Go to example.com</a>
105+
106+
```
107+
108+
### Options
73109
```javascript
74110
// Parameters and options
75111

@@ -95,7 +131,7 @@ this.$dialog.confirm(message, options)
95131
// This will be triggered when user clicks on cancel
96132
});
97133
```
98-
134+
### Global Configuration
99135
```javascript
100136
// You can also set all your defaults at the oint of installation.
101137
// This will be your global configuration

dist/vuejs-dialog.min.js

+2-577
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuejs-dialog.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/app.main.css

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
/* reset */
2-
* {
3-
box-sizing: border-box; }
4-
5-
body {
6-
margin: 0;
7-
padding: 0; }
8-
9-
/* header */
1+
*{box-sizing:border-box}body{margin:0;padding:0}.page-title{border-bottom:2px solid #0096d9}

docs/app.main.js

+1-623
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<title>VueJs Plugin usage example</title>
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<link href="app.main.css?6b138ee33d23ac1b91c1" rel="stylesheet"></head>
7+
<link href="app.main.css?4c2bab1f9ccdd0470ea4" rel="stylesheet"></head>
88
<body>
99
<div id="app"></div>
1010

@@ -21,5 +21,5 @@
2121
ga('send', 'pageview');
2222

2323
</script>
24-
<script type="text/javascript" src="app.main.js?6b138ee33d23ac1b91c1"></script></body>
24+
<script type="text/javascript" src="app.main.js?4c2bab1f9ccdd0470ea4"></script></body>
2525
</html>

src/docs/components/app.vue

+90-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,73 @@
11
<template>
22
<div style="max-width: 350px; margin:auto; font-family: 'Noto Sans', sans-serif; text-align: center">
3-
<h1>Vuejs-Dialog Plugin Examples</h1>
4-
5-
<h3>
6-
<button @click="showAlertDialog()">Alert Dialog - one button</button>
7-
</h3>
8-
9-
<h3>
10-
<button @click="showHtmlDialog()">Html Dialog - style/format content</button>
11-
</h3>
12-
13-
<h3>
14-
<button @click="showBasicDialog()">Basic confirm - close instantly</button>
15-
</h3>
16-
17-
<h3>
18-
<button @click="showLoadingDialog()">Loading Dialog - Useful with ajax</button>
19-
</h3>
20-
21-
<h3>
22-
<button @click="showReversedDialog()">Reversed Dialog - switch buttons</button>
23-
</h3>
24-
25-
<h3>
26-
<button @click="showAnimationFadeDialog()">Fade Dialog - Animation</button>
27-
</h3>
28-
29-
<h3>
30-
<button @click="showAnimationBounceDialog()">Bounce Dialog - Animation</button>
31-
</h3>
32-
33-
3+
<h1 class="page-title">Vuejs-Dialog Plugin Examples</h1>
4+
5+
<section>
6+
<h2>Usage as a method</h2>
7+
<hr/>
8+
<h4>
9+
<button @click="showAlertDialog()">Alert Dialog - one button</button>
10+
</h4>
11+
12+
<h4>
13+
<button @click="showHtmlDialog()">Html Dialog - style/format content</button>
14+
</h4>
15+
16+
<h4>
17+
<button @click="showBasicDialog()">Basic confirm - close instantly</button>
18+
</h4>
19+
20+
<h4>
21+
<button @click="showLoadingDialog()">Loading Dialog - Useful with ajax</button>
22+
</h4>
23+
24+
<h4>
25+
<button @click="showReversedDialog()">Reversed Dialog - switch buttons</button>
26+
</h4>
27+
28+
<h4>
29+
<button @click="showAnimationFadeDialog()">Fade Dialog - Animation</button>
30+
</h4>
31+
32+
<h4>
33+
<button @click="showAnimationBounceDialog()">Bounce Dialog - Animation</button>
34+
</h4>
35+
</section>
36+
37+
<section>
38+
<h2>Usage as a directive</h2>
39+
<hr/>
40+
41+
<h4>
42+
<a href="http://example.com" v-confirm="'This will take you to http://example.com. Proceed with caution'">Go to example.com</a>
43+
</h4>
44+
45+
<h4>
46+
<button v-confirm="`This is a message`">
47+
Give it a string v-confirm="This is a message"
48+
</button>
49+
</h4>
50+
51+
<h4>
52+
<button v-confirm="{
53+
message: 'This dialog was also triggered using a v-confirm directive. It has both OK and CANCEL callback',
54+
ok: clickOkHandler,
55+
cancel: clickCancelHandler}"
56+
>
57+
Give it an object v-confirm="messageAndCallback"
58+
</button>
59+
</h4>
60+
61+
<h4>
62+
<form @submit.prevent="submitDemo1Form()">
63+
<fieldset>
64+
<input v-model="forms.demo1.name" type="text" name="name" placeholder="Your name"/>
65+
<button type="reset" v-confirm="'Changes would be discarded. Reset this form?'">Reset</button>
66+
<button type="submit" v-confirm="'Submit this form?'">submit</button>
67+
</fieldset>
68+
</form>
69+
</h4>
70+
</section>
3471

3572
<notifications position="bottom left"></notifications>
3673
</div>
@@ -40,10 +77,24 @@
4077
import trans from '../js/translations'
4178
4279
export default {
80+
data(){
81+
return {
82+
forms: {
83+
demo1: {
84+
name: null
85+
}
86+
}
87+
}
88+
},
4389
mounted(){
4490
console.log('mounted app')
4591
},
4692
methods: {
93+
submitDemo1Form(){
94+
let msg = "Form Submited. Your Name: "
95+
msg += this.forms.demo1.name || 'The name field is empty'
96+
this.$notify(msg)
97+
},
4798
showAlertDialog(){
4899
this.$dialog.alert(trans('messages.alert'))
49100
},
@@ -98,12 +149,20 @@
98149
}).catch(() => {
99150
this.$notify({text: trans('messages.loading_canceled')})
100151
})
152+
},
153+
clickOkHandler(){
154+
this.$notify({type: 'success', text: trans('messages.click_continue')})
155+
},
156+
clickCancelHandler(){
157+
this.$notify({type: 'error', text: trans('messages.click_cancel')})
101158
}
102159
}
103160
}
104161
</script>
105162

106163
<style>
164+
@import "../scss/app.scss";
165+
107166
.vue-notification {
108167
padding: 10px;
109168
margin: 15px;

src/docs/js/app.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
import Vue from "vue"
44
import Notification from 'vue-notification'
5+
import VuejsDialog from 'vuejs-dialog'
56
import AppComponent from '../components/app.vue'
67

78
Vue.use(Notification)
89

10+
// Install VuejsDialog
11+
Vue.use(VuejsDialog, {
12+
message: 'Please confirm action'
13+
})
14+
915
let App = Vue.extend(AppComponent)
1016

1117
window.vm = new App()

src/docs/scss/app.scss

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
}
55

66
body {
7-
margin: 0;
8-
padding: 0;
9-
}
7+
margin: 0;
8+
padding: 0;
9+
}
1010

1111
/* header */
12+
.page-title {
13+
border-bottom: 2px solid #0096D9;
14+
}

src/plugin/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import DialogComponent from './components/dialog.vue'
44
import {DIALOG_TYPES, DEFAULT_OPTIONS} from './js/constants'
5+
import directives from './js/directives'
56
import {mergeObjs} from './js/utilities'
67

78
let Plugin = function(Vue, globalOptions = {}){
@@ -42,6 +43,8 @@ Plugin.prototype.open = function(type, localOptions = {}){
4243
}
4344

4445
Plugin.install = function (Vue, options) {
46+
directives(Vue)
47+
4548
Vue.dialog = new Plugin(Vue, options)
4649

4750
Object.defineProperties(Vue.prototype, {
@@ -51,6 +54,7 @@ Plugin.install = function (Vue, options) {
5154
}
5255
}
5356
})
57+
5458
}
5559

5660
export default Plugin

src/plugin/js/directives.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Directives
2+
3+
import {noop} from './utilities'
4+
5+
export default function (Vue) {
6+
let Directives = function (Vue) {
7+
this.Vue = Vue
8+
this.registerConfirm()
9+
}
10+
11+
Directives.prototype.registerConfirm = function () {
12+
const _this = this
13+
14+
const clickHandler = function (event, el, binding, vnode) {
15+
event.preventDefault()
16+
event.stopImmediatePropagation()
17+
18+
let confirmMessage = (function () {
19+
if (binding.value && binding.value.message) {
20+
return binding.value.message
21+
}
22+
return typeof binding.value === 'string' ? binding.value : null
23+
})()
24+
25+
let thenCallback = (function () {
26+
if (binding.value && binding.value.ok) {
27+
return binding.value.ok
28+
} else {
29+
return () => {
30+
el.removeEventListener('click', el.VuejsDialog.confirmHandler, true)
31+
32+
_this.Vue.nextTick(() => {
33+
(function (node) {
34+
if (document.createEvent) {
35+
let evt = document.createEvent('MouseEvents');
36+
evt.initEvent('click', true, false);
37+
node.dispatchEvent(evt);
38+
} else if (document.createEventObject) {
39+
node.fireEvent('onclick');
40+
} else if (typeof node.onclick === 'function') {
41+
node.onclick();
42+
}
43+
})(el)
44+
45+
el.addEventListener('click', el.VuejsDialog.confirmHandler, true)
46+
})
47+
}
48+
}
49+
})()
50+
51+
let catchCallback = (function () {
52+
if (binding.value && binding.value.cancel) {
53+
return binding.value.cancel
54+
}
55+
return noop
56+
})()
57+
58+
_this.Vue.dialog.confirm(confirmMessage).then(thenCallback).catch(catchCallback)
59+
}
60+
61+
this.Vue.directive('confirm', {
62+
bind (el, binding, vnode) {
63+
if (el.VuejsDialog === undefined)
64+
el.VuejsDialog = {}
65+
66+
el.VuejsDialog.confirmHandler = function clickEventHandler(event) {
67+
clickHandler(event, el, binding, vnode)
68+
}
69+
70+
el.addEventListener('click', el.VuejsDialog.confirmHandler, true)
71+
},
72+
unbind (el) {
73+
el.removeEventListener('click', el.VuejsDialog.confirmHandler, true)
74+
}
75+
})
76+
77+
78+
}
79+
80+
new Directives(Vue)
81+
}

src/plugin/js/utilities.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// utilities
22

3+
export const noop = () => {}
4+
35
export const cloneObj = function (obj) {
46
return Object.assign({}, obj)
57
}

src/plugin/styles/default.css

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
text-align: center;
6969
text-decoration: none;
7070
cursor: pointer;
71+
outline: none;
7172
-webkit-appearance: none;
7273
-moz-appearance: none;
7374
appearance: none;

0 commit comments

Comments
 (0)