-
Notifications
You must be signed in to change notification settings - Fork 2
/
x-swal.html
152 lines (141 loc) · 4.44 KB
/
x-swal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<link rel="import" href="../polymer/polymer.html">
<script src="../sweetalert2/dist/sweetalert2.min.js"></script>
<link rel="stylesheet" href="../sweetalert2/dist/sweetalert2.css">
<!--
The `x-swal` element that display an alternative for alert() javascript-function.
##### Example: Sweet Alert
<x-swal id="7" type="success" >
<title>Something went wrong!</title>
<text>Oops...</text>
<img src="images/thumbs-up.jpg">
</x-swal>
<script>
document.querySelector('x-swal').active();
</script>
@element x-swal
@blurb Element that display an alternative for alert() javascript-function.
@status alpha
@homepage https://ruben96.github.io/x-swal/components/x-swal/
-->
<polymer-element name="x-swal" attributes="type">
<template>
<style>
:host {
display: none;
}
</style>
<content id="title" select="title, text, confirm, cancel, img"></content>
</template>
<script>
(function () {
'use strict';
Polymer({
publish: {
// auto: false,
/**
* Set to <i>false</i> if you want the modal to stay open even if the
* user presses the "Confirm"-button. This is especially useful if the
* function attached to the "Confirm"-button is another x-swal.
*
* @property closeOnConfirm
* @type boolean
* @default false
*/
closeOnConfirm: {
value: false
},
/**
* If set to <strong>true</strong>, the user can dismiss the modal by
* clicking outside it.
*
* @property allowOutsideClick
* @type boolean
* @default false
*/
allowOutsideClick: {
value: false
},
/**
* Set to <i>false</i> if you want the modal to stay open even if the
* user presses the "Cancel"-button. This is especially useful if the
* function attached to the "Cancel"-button is another SweetAlert.
*
* @property closeOnCancel
* @type boolean
* @default true
*/
closeOnCancel: {
value: true
},
/**
* If set to <strong>true</strong>, a "Cancel"-button will be shown,
* which the user can click on to dismiss the modal.
*
* @property showCancelButton
* @type boolean
* @default false
*/
showCancelButton: {
value: false
}
},
attached: function (document) {
this.elements = Array.prototype.slice.call(
this.$.title.getDistributedNodes()
);
for (var a = 0, m; m = this.elements[a]; ++a) {
var src = m.getAttribute('src'),
tagName = m.localName,
textContent = m.innerHTML;
if (tagName === 'img' && src) {
textContent = src;
} else if (tagName === 'confirm') {
var color = m.getAttribute('color');
if (color) {
this.confirmButtonColor = color;
}
}
else if (tagName === 'cancel') {
var color = m.getAttribute('color');
if (color) {
this.cancelButtonColor = color;
}
}
this[tagName] = textContent;
}
},
autoChanged: function (oldVal, newVal) {
if (newVal) {
this.active();
}
},
/**
* Active the alert.
*
* @method active
*/
active: function (cb) {
swal({
title: this.title,
text: this.text,
type: this.type,
showCancelButton: this.showCancelButton,
confirmButtonColor: this.confirmButtonColor,
cancelButtonColor: this.cancelButtonColor,
confirmButtonText: this.confirm,
cancelButtonText: this.cancel,
closeOnConfirm: this.closeOnConfirm,
closeOnCancel: this.closeOnCancel,
imageUrl: this.img
}, function (isConfirm) {
this.fire('confirmation', isConfirm);
if (cb && typeof cb === "function") {
return cb(isConfirm);
}
return;
}.bind(this));
}
});
})(document);
</script>
</polymer-element>