forked from wyntau/vue-ios-alertview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-ios-alertview.js
228 lines (187 loc) · 5.74 KB
/
vue-ios-alertview.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
else if(typeof window !== 'undefined' && window.Vue){
window.Vue.use(factory());
}
})(function (require, exports) {
"use strict";
var template = [
'<transition @after-leave="afterLeave">',
'<div class="ios-alertview-overlay" v-if="showModal">',
'<div class="ios-alertview">',
'<div class="ios-alertview-inner" :class="{\'ios-alertview-inner-remind\': !buttons || !buttons.length}">',
'<div class="ios-alertview-title" v-if="title">{{ title }}</div>',
'<div class="ios-alertview-text" v-html="text" v-if="text"></div>',
'<input autofocus class="ios-alertview-text-input" :placeholder="placeholder" v-model="value" v-if="input" />',
'</div>',
'<div class="ios-alertview-buttons" v-if="buttons && buttons.length" :class="{\'ios-alertview-buttons-horizontal\': buttons.length <= 2}">',
'<span class="ios-alertview-button" :class="{\'ios-alertview-button-bold\': button.bold}" v-for="(button, index) in buttons" @click.prevent.stop="onClick(button, index)">{{ button.text }}</span>',
'</div>',
'</div>',
'</div>',
'</transition>'
].join('');
// css 动画执行的时间
var ANIMATION_TIME = 400;
// 默认选项
var defaults = {
defaultOption: 'title',
title: null,
text: null,
input: false,
placeholder: '',
cancelText: 'Cancel',
okText: 'OK',
remindDuration: 650
};
// a mini defer like angular's $q.defer()
var defer = function(){
var promise;
var resolve;
var reject;
promise = new Promise(function(_resolve_, _reject_){
resolve = _resolve_;
reject = _reject_;
});
return {
promise: promise,
resolve: resolve,
reject: reject
};
};
return function(Vue, globalOptions){
globalOptions = globalOptions || {};
if (typeof globalOptions !== 'object') {
throw new Error('Expect Object options');
}
// override defaults
defaults = Vue.util.extend(defaults, globalOptions);
var IosAlertViewComponent = Vue.extend({
template: template,
data: function () {
return {
value: '',
showModal: false
};
},
props: [
'title',
'text',
'input',
'placeholder',
'onClick',
'remindDuration',
'buttons'
],
methods: {
activate: function(){
var self = this;
self._deferred = defer();
self.showModal = true;
// no buttons, remind (ANIMATION_TIME + remindDuration) time, then auto remove
if(!self.buttons || !self.buttons.length){
setTimeout(function(){
self.showModal = false;
self._deferred.resolve();
}, ANIMATION_TIME + self.remindDuration);
}
return self._deferred.promise;
},
onClick: function (button, index) {
var cbkData = {
index: index,
button: button,
value: this.value
};
if (typeof button.onClick === 'function') {
button.onClick(cbkData);
}
this._deferred.resolve(cbkData);
this.showModal = false;
},
afterLeave: function () {
this.$destroy();
}
}
});
function getPropsData(options){
options = options || {};
var propsData = Vue.util.extend({}, defaults);
if (typeof options === 'string') {
propsData[defaults.defaultOption] = options;
} else {
propsData = Vue.util.extend(propsData, options);
}
return propsData;
}
function IosAlertView(options) {
var propsData = getPropsData(options);
var instance = new IosAlertViewComponent({propsData: propsData});
var mount = document.createElement('div');
mount.id = 'ios-alert-view-' + Date.now();
document.body.appendChild(mount);
instance.$mount(mount);
return instance.activate();
}
Vue.prototype.$iosAlertView = IosAlertView;
Vue.prototype.$iosAlert = function (options) {
var deferred = defer();
var propsData = getPropsData(options);
propsData.buttons = [
{
text: propsData.okText,
onClick: deferred.resolve,
bold: true
}
];
IosAlertView(propsData);
return deferred.promise;
};
Vue.prototype.$iosConfirm = function(options){
var deferred = defer();
var propsData = getPropsData(options);
propsData.buttons = [
{
text: propsData.cancelText,
onClick: deferred.reject
},
{
text: propsData.okText,
onClick: deferred.resolve,
bold: true
}
];
IosAlertView(propsData);
return deferred.promise;
};
Vue.prototype.$iosPrompt = function(options){
var deferred = defer();
var propsData = getPropsData(options);
propsData.input = true;
propsData.buttons = [
{
text: propsData.cancelText,
onClick: deferred.reject
},
{
text: propsData.okText,
onClick: function (data) {
deferred.resolve(data.value);
},
bold: true
}
];
IosAlertView(propsData);
return deferred.promise;
};
Vue.prototype.$iosRemind = function(options){
var propsData = getPropsData(options);
return IosAlertView(propsData);
};
}
});