forked from liulangnan/aui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaui-sharebox.js
119 lines (118 loc) · 5.14 KB
/
aui-sharebox.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
/**
* aui-sharebox.js
* @author 流浪男
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function( window, undefined ) {
"use strict";
var auiSharebox = function() {
};
var isShow = false;
auiSharebox.prototype = {
init: function(params,callback){
this.frameBounces = params.frameBounces;
this.col = params.col;
this.buttons = params.buttons;
this.cancelTitle = params.cancelTitle;
this.maskDiv;
this.shareBoxDiv;
var self = this;
self.open(params,callback);
},
open: function(params,callback) {
var shareboxHtml='',buttonsHtml = '';
var self = this;
if(self.shareBoxDiv || !self.buttons)return;
if(!self.maskDiv){
self.maskDiv = document.createElement("div");
self.maskDiv.className = "aui-mask";
document.body.appendChild(self.maskDiv);
}
if(!self.col)self.col = 5;
self.shareBoxDiv = document.createElement("div");
self.shareBoxDiv.className = "aui-sharebox aui-grid";
document.body.appendChild(self.shareBoxDiv);
if(self.buttons && self.buttons.length){
buttonsHtml = '<div class="aui-row aui-row-padded">';
for(var i = 0; i < self.buttons.length;i++){
if(self.col == 5){
buttonsHtml += '<div class="aui-col-5 aui-sharebox-btn">';
}else{
buttonsHtml += '<div class="aui-col-xs-'+(12/self.col)+' aui-sharebox-btn">';
}
if(self.buttons[i].image)buttonsHtml += '<img src="'+self.buttons[i].image+'">';
if(self.buttons[i].text)buttonsHtml += '<div class="aui-grid-label">'+self.buttons[i].text+'</div>';
buttonsHtml += '</div>';
}
buttonsHtml += '</div>';
}
if(self.cancelTitle){
buttonsHtml += '<div class="aui-sharebox-close-btn aui-border-t">'+this.cancelTitle+'</div>';
}
self.shareBoxDiv.innerHTML = buttonsHtml;
var actionsheetHeight = self.shareBoxDiv.offsetHeight;
self.maskDiv.classList.add("aui-mask-in");
self.shareBoxDiv.style.webkitTransform = self.shareBoxDiv.style.transform = "translate3d(0,0,0)";
self.shareBoxDiv.style.opacity = 1;
self.shareBoxDiv.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 shareboxButtons = document.querySelectorAll(".aui-sharebox-btn");
if(shareboxButtons && shareboxButtons.length > 0){
setTimeout(function(){
self.maskDiv.onclick = function(){self.close();return;};
for(var ii = 0; ii < shareboxButtons.length; ii++){
(function(e){
shareboxButtons[e].onclick = function(){
if(self.buttons[e].value){
var _value = self.buttons[e].value;
}else{
var _value = null;
}
if(callback){
callback({
buttonIndex: e+1,
buttonValue:_value
});
};
self.close();
return;
}
})(ii)
}
}, 350)
}
document.querySelector(".aui-sharebox-close-btn").onclick = function(){self.close();return;};
},
close: function(){
var self = this;
if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){
api.setFrameAttr({
bounces:true
});
}
if(self.shareBoxDiv){
var actionsheetHeight = self.shareBoxDiv.offsetHeight;
self.shareBoxDiv.style.webkitTransform = self.shareBoxDiv.style.transform = "translate3d(0,"+actionsheetHeight+"px,0)";
self.maskDiv.style.opacity = 0;
setTimeout(function(){
if(self.maskDiv){
self.maskDiv.parentNode.removeChild(self.maskDiv);
}
self.shareBoxDiv.parentNode.removeChild(self.shareBoxDiv);
self.maskDiv = self.shareBoxDiv = false;
}, 300)
}
}
};
window.auiSharebox = auiSharebox;
})(window);