Skip to content
This repository was archived by the owner on Oct 10, 2021. It is now read-only.

Commit 9fb4d17

Browse files
author
Treri
committed
refactor: remove Vue requirement
1 parent 92f4f41 commit 9fb4d17

File tree

4 files changed

+90
-95
lines changed

4 files changed

+90
-95
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-ios-alertview",
3-
"version": "0.5.2",
3+
"version": "0.6.0",
44
"description": "iOS7+ style alertview service for Vue.",
55
"main": "vue-ios-alertview.js",
66
"types": "types/index.d.ts",

types/index.d.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { PluginFunction } from 'vue';
55

66
export = VueIosAlertView;
77

8-
declare interface VueIosAlertView<T> {
9-
(options: string | VueIosAlertView.VueIosAlertViewOption): PromiseLike<T>;
10-
}
11-
12-
declare function VueIosAlertView(strictOptions: VueIosAlertView.VueIosAlertViewOption): PromiseLike<VueIosAlertView.VueIosAlertViewButtonData>;
8+
declare let VueIosAlertView: PluginFunction<VueIosAlertView.VueIosAlertViewOption>
139

1410
declare namespace VueIosAlertView {
11+
12+
export interface VueIosAlertView<T> {
13+
(options: string | VueIosAlertView.VueIosAlertViewOption): PromiseLike<T>;
14+
}
15+
16+
export interface VueIosAlertViewStrict {
17+
(strictOptions: VueIosAlertView.VueIosAlertViewOption): PromiseLike<VueIosAlertView.VueIosAlertViewButtonData>;
18+
}
19+
1520
export interface VueIosAlertViewButton {
1621
text: string;
1722
bold?: boolean;
@@ -36,8 +41,6 @@ declare namespace VueIosAlertView {
3641
buttons?: Array<VueIosAlertViewButton>;
3742
[key: string]: any;
3843
}
39-
40-
export const install: PluginFunction<VueIosAlertViewOption>;
4144
}
4245

4346

types/vue.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import VueIosAlertView = require('./index');
44

55
declare module "vue/types/vue" {
66
interface Vue {
7-
$iosAlert: VueIosAlertView<never>;
8-
$iosConfirm: VueIosAlertView<never>;
9-
$iosPrompt: VueIosAlertView<string>;
10-
$iosRemind: VueIosAlertView<never>;
11-
$iosAlertView: typeof VueIosAlertView;
7+
$iosAlert: VueIosAlertView.VueIosAlertView<never>;
8+
$iosConfirm: VueIosAlertView.VueIosAlertView<never>;
9+
$iosPrompt: VueIosAlertView.VueIosAlertView<string>;
10+
$iosRemind: VueIosAlertView.VueIosAlertView<never>;
11+
$iosAlertView: VueIosAlertView.VueIosAlertViewStrict;
1212
}
1313
}

vue-ios-alertview.js

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
define(["require", "exports"], factory);
77
}
88
else {
9-
var IosAlertView = factory();
10-
if(typeof window !== 'undefined'){
11-
window.IosAlertView = IosAlertView;
9+
if(typeof window !== 'undefined' && window.Vue){
10+
window.Vue.use(factory());
1211
}
1312
}
1413
})(function (require, exports) {
@@ -63,95 +62,94 @@
6362
};
6463
};
6564

66-
var IosAlertViewComponent = Vue.extend({
67-
template: template,
68-
data: function () {
69-
return {
70-
value: '',
71-
showModal: false
72-
};
73-
},
74-
props: [
75-
'title',
76-
'text',
77-
'input',
78-
'placeholder',
79-
'onClick',
80-
'remindDuration',
81-
'buttons'
82-
],
83-
methods: {
84-
activate: function(){
85-
var self = this;
86-
87-
self._deferred = defer();
88-
89-
self.showModal = true;
90-
91-
// no buttons, remind (ANIMATION_TIME + remindDuration) time, then auto remove
92-
if(!self.buttons || !self.buttons.length){
93-
setTimeout(function(){
94-
self.showModal = false;
95-
self._deferred.resolve();
96-
}, ANIMATION_TIME + self.remindDuration);
97-
}
98-
99-
return self._deferred.promise;
100-
},
101-
onClick: function (button, index) {
102-
var cbkData = {
103-
index: index,
104-
button: button,
105-
value: this.value
65+
return function(Vue, globalOptions){
66+
var IosAlertViewComponent = Vue.extend({
67+
template: template,
68+
data: function () {
69+
return {
70+
value: '',
71+
showModal: false
10672
};
107-
108-
if (typeof button.onClick === 'function') {
109-
button.onClick(cbkData);
110-
}
111-
112-
this._deferred.resolve(cbkData);
113-
this.showModal = false;
11473
},
115-
afterLeave: function () {
116-
this.$destroy();
74+
props: [
75+
'title',
76+
'text',
77+
'input',
78+
'placeholder',
79+
'onClick',
80+
'remindDuration',
81+
'buttons'
82+
],
83+
methods: {
84+
activate: function(){
85+
var self = this;
86+
87+
self._deferred = defer();
88+
89+
self.showModal = true;
90+
91+
// no buttons, remind (ANIMATION_TIME + remindDuration) time, then auto remove
92+
if(!self.buttons || !self.buttons.length){
93+
setTimeout(function(){
94+
self.showModal = false;
95+
self._deferred.resolve();
96+
}, ANIMATION_TIME + self.remindDuration);
97+
}
98+
99+
return self._deferred.promise;
100+
},
101+
onClick: function (button, index) {
102+
var cbkData = {
103+
index: index,
104+
button: button,
105+
value: this.value
106+
};
107+
108+
if (typeof button.onClick === 'function') {
109+
button.onClick(cbkData);
110+
}
111+
112+
this._deferred.resolve(cbkData);
113+
this.showModal = false;
114+
},
115+
afterLeave: function () {
116+
this.$destroy();
117+
}
117118
}
118-
}
119-
});
120-
121-
function getPropsData(options){
122-
options = options || {};
119+
});
123120

124-
var propsData = Vue.util.extend({}, defaults);
121+
function getPropsData(options){
122+
options = options || {};
125123

126-
if (typeof options === 'string') {
127-
propsData[defaults.defaultOption] = options;
128-
} else {
129-
propsData = Vue.util.extend(propsData, options);
130-
}
124+
var propsData = Vue.util.extend({}, defaults);
131125

132-
return propsData;
133-
}
126+
if (typeof options === 'string') {
127+
propsData[defaults.defaultOption] = options;
128+
} else {
129+
propsData = Vue.util.extend(propsData, options);
130+
}
134131

135-
function IosAlertView(options) {
136-
var propsData = getPropsData(options);
132+
return propsData;
133+
}
137134

138-
var instance = new IosAlertViewComponent({propsData: propsData});
135+
function IosAlertView(options) {
136+
var propsData = getPropsData(options);
139137

140-
var mount = document.createElement('div');
141-
mount.id = 'ios-alert-view-' + Date.now();
142-
document.body.appendChild(mount);
138+
var instance = new IosAlertViewComponent({propsData: propsData});
143139

144-
instance.$mount(mount);
140+
var mount = document.createElement('div');
141+
mount.id = 'ios-alert-view-' + Date.now();
142+
document.body.appendChild(mount);
145143

146-
return instance.activate();
147-
}
144+
instance.$mount(mount);
148145

149-
IosAlertView.install = function (Vue, globalOptions) {
146+
return instance.activate();
147+
}
150148

151149
globalOptions = globalOptions || {};
152150

153151
if (typeof globalOptions !== 'object') {
154-
throw 'Expect Object options';
152+
throw new Error('Expect Object options');
155153
}
156154

157155
// override defaults
@@ -228,12 +226,6 @@
228226
var propsData = getPropsData(options);
229227
return IosAlertView(propsData);
230228
};
231-
};
232-
233-
if(typeof window !== 'undefined' && window.Vue){
234-
window.Vue.use(IosAlertView);
235229
}
236-
237-
return IosAlertView;
238230
});
239231

0 commit comments

Comments
 (0)