qiaowei | f044a74 | 2019-07-10 16:04:20 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * aui-sharebox.js |
| 3 | * @author 流浪男 |
| 4 | * Licensed under the MIT license. |
| 5 | * http://www.opensource.org/licenses/mit-license.php |
| 6 | */ |
| 7 | (function( window, undefined ) { |
| 8 | "use strict"; |
| 9 | var auiSharebox = function() { |
| 10 | }; |
| 11 | var isShow = false; |
| 12 | auiSharebox.prototype = { |
| 13 | init: function(params,callback){ |
| 14 | this.frameBounces = params.frameBounces; |
| 15 | this.col = params.col; |
| 16 | this.buttons = params.buttons; |
| 17 | this.cancelTitle = params.cancelTitle; |
| 18 | this.maskDiv; |
| 19 | this.shareBoxDiv; |
| 20 | var self = this; |
| 21 | self.open(params,callback); |
| 22 | }, |
| 23 | open: function(params,callback) { |
| 24 | var shareboxHtml='',buttonsHtml = ''; |
| 25 | var self = this; |
| 26 | if(self.shareBoxDiv || !self.buttons)return; |
| 27 | if(!self.maskDiv){ |
| 28 | self.maskDiv = document.createElement("div"); |
| 29 | self.maskDiv.className = "aui-mask"; |
| 30 | document.body.appendChild(self.maskDiv); |
| 31 | } |
| 32 | if(!self.col)self.col = 5; |
| 33 | self.shareBoxDiv = document.createElement("div"); |
| 34 | self.shareBoxDiv.className = "aui-sharebox aui-grid"; |
| 35 | document.body.appendChild(self.shareBoxDiv); |
| 36 | if(self.buttons && self.buttons.length){ |
| 37 | buttonsHtml = '<div class="aui-row aui-row-padded">'; |
| 38 | for(var i = 0; i < self.buttons.length;i++){ |
| 39 | if(self.col == 5){ |
| 40 | buttonsHtml += '<div class="aui-col-5 aui-sharebox-btn">'; |
| 41 | }else{ |
| 42 | buttonsHtml += '<div class="aui-col-xs-'+(12/self.col)+' aui-sharebox-btn">'; |
| 43 | } |
| 44 | if(self.buttons[i].image)buttonsHtml += '<img src="'+self.buttons[i].image+'">'; |
| 45 | if(self.buttons[i].text)buttonsHtml += '<div class="aui-grid-label">'+self.buttons[i].text+'</div>'; |
| 46 | buttonsHtml += '</div>'; |
| 47 | } |
| 48 | buttonsHtml += '</div>'; |
| 49 | } |
| 50 | if(self.cancelTitle){ |
| 51 | buttonsHtml += '<div class="aui-sharebox-close-btn aui-border-t">'+this.cancelTitle+'</div>'; |
| 52 | } |
| 53 | self.shareBoxDiv.innerHTML = buttonsHtml; |
| 54 | var actionsheetHeight = self.shareBoxDiv.offsetHeight; |
| 55 | self.maskDiv.classList.add("aui-mask-in"); |
| 56 | self.shareBoxDiv.style.webkitTransform = self.shareBoxDiv.style.transform = "translate3d(0,0,0)"; |
| 57 | self.shareBoxDiv.style.opacity = 1; |
| 58 | self.shareBoxDiv.addEventListener("touchmove", function(event){ |
| 59 | event.preventDefault(); |
| 60 | }) |
| 61 | self.maskDiv.addEventListener("touchmove", function(event){ |
| 62 | event.preventDefault(); |
| 63 | }) |
| 64 | if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){ |
| 65 | api.setFrameAttr({ |
| 66 | bounces:false |
| 67 | }); |
| 68 | } |
| 69 | var shareboxButtons = document.querySelectorAll(".aui-sharebox-btn"); |
| 70 | if(shareboxButtons && shareboxButtons.length > 0){ |
| 71 | setTimeout(function(){ |
| 72 | self.maskDiv.onclick = function(){self.close();return;}; |
| 73 | for(var ii = 0; ii < shareboxButtons.length; ii++){ |
| 74 | (function(e){ |
| 75 | shareboxButtons[e].onclick = function(){ |
| 76 | if(self.buttons[e].value){ |
| 77 | var _value = self.buttons[e].value; |
| 78 | }else{ |
| 79 | var _value = null; |
| 80 | } |
| 81 | if(callback){ |
| 82 | callback({ |
| 83 | buttonIndex: e+1, |
| 84 | buttonValue:_value |
| 85 | }); |
| 86 | }; |
| 87 | self.close(); |
| 88 | return; |
| 89 | } |
| 90 | })(ii) |
| 91 | } |
| 92 | }, 350) |
| 93 | |
| 94 | } |
| 95 | document.querySelector(".aui-sharebox-close-btn").onclick = function(){self.close();return;}; |
| 96 | }, |
| 97 | close: function(){ |
| 98 | var self = this; |
| 99 | if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){ |
| 100 | api.setFrameAttr({ |
| 101 | bounces:true |
| 102 | }); |
| 103 | } |
| 104 | if(self.shareBoxDiv){ |
| 105 | var actionsheetHeight = self.shareBoxDiv.offsetHeight; |
| 106 | self.shareBoxDiv.style.webkitTransform = self.shareBoxDiv.style.transform = "translate3d(0,"+actionsheetHeight+"px,0)"; |
| 107 | self.maskDiv.style.opacity = 0; |
| 108 | setTimeout(function(){ |
| 109 | if(self.maskDiv){ |
| 110 | self.maskDiv.parentNode.removeChild(self.maskDiv); |
| 111 | } |
| 112 | self.shareBoxDiv.parentNode.removeChild(self.shareBoxDiv); |
| 113 | self.maskDiv = self.shareBoxDiv = false; |
| 114 | }, 300) |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | window.auiSharebox = auiSharebox; |
| 119 | })(window); |