aui-popup-new.js
5.53 KB
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
/**
* aui-popup.js
* @author 流浪男
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function( window, undefined ) {
"use strict";
var auiPopup = function() {
};
var isShow = false;
auiPopup.prototype = {
init: function(params,callback){
this.frameBounces = params.frameBounces;
this.location = params.location;
this.buttons = params.buttons;
this.maskDiv;
this.popupDiv;
var self = this;
self.open(params,callback);
},
open: function(params,callback) {
var buttonsHtml='',locationClass = 'aui-popup-top';
var self = this;
if(self.popupDiv){
self.close();
return;
}
if(!self.maskDiv){
self.maskDiv = document.createElement("div");
self.maskDiv.className = "aui-mask";
document.body.appendChild(self.maskDiv);
}
switch (self.location) {
case "top":
locationClass = 'aui-popup-top';
break;
case "top-left":
locationClass = 'aui-popup-top-left';
break;
case "top-right":
locationClass = 'aui-popup-top-right';
break;
case "bottom":
locationClass = 'aui-popup-bottom';
break;
case "bottom-left":
locationClass = 'aui-popup-bottom-left';
break;
case "bottom-right":
locationClass = 'aui-popup-bottom-right';
break;
default:
locationClass = 'aui-popup-top';
break;
}
self.popupDiv = document.createElement("div");
self.popupDiv.className = "aui-popup "+locationClass;
self.popupDiv.innerHTML = '<div class="aui-popup-arrow"></div><div class="aui-popup-content"></div>';
document.body.appendChild(self.popupDiv);
if(self.buttons && self.buttons.length){
buttonsHtml += '<div class="aui-list">';
for(var i = 0; i < self.buttons.length;i++){
buttonsHtml += '<div class="aui-list-item">';
buttonsHtml += '<div class="aui-list-item-left"><img src="'+self.buttons[i].image+'"></div>';
buttonsHtml += '<div class="aui-list-item-center">'+self.buttons[i].text+'</div>';
buttonsHtml += '</div>';
}
buttonsHtml += '</ul>';
}
document.querySelector(".aui-popup .aui-popup-content").insertAdjacentHTML('beforeend', buttonsHtml);
var actionsheetHeight = document.querySelector(".aui-popup").offsetHeight;
self.maskDiv.classList.add("aui-mask-in");
self.popupDiv.classList.add("aui-popup-in");
self.popupDiv.addEventListener("touchmove", function(event){
event.preventDefault();
})
self.maskDiv.addEventListener("touchmove", function(event){
event.preventDefault();
})
if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){
api.setFrameAttr({
bounces:false
});
}
var popupButtons = document.querySelectorAll(".aui-popup .aui-list-item");
if(popupButtons && popupButtons.length > 0){
setTimeout(function(){
self.maskDiv.onclick = function(){self.close();return;};
for(var ii = 0; ii < popupButtons.length; ii++){
(function(e){
popupButtons[e].onclick = function(){
if(self.buttons[e].value){
var _value = self.buttons[e].value;
}else{
var _value = null;
}
if(callback){
callback({
buttonIndex: e+1,
buttonTitle: this.textContent,
buttonValue: _value
});
};
self.close();
return;
}
})(ii)
}
}, 350)
}
},
close: function(){
var self = this;
if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){
api.setFrameAttr({
bounces:true
});
}
if(self.popupDiv){
var actionsheetHeight = self.popupDiv.offsetHeight;
self.popupDiv.classList.add("aui-popup-out");
self.maskDiv.style.opacity = 0;
setTimeout(function(){
if(self.maskDiv){
self.maskDiv.parentNode.removeChild(self.maskDiv);
}
self.popupDiv.parentNode.removeChild(self.popupDiv);
self.maskDiv = self.popupDiv = false;
}, 300)
}
}
};
window.auiPopup = auiPopup;
})(window);