qiaowei | f044a74 | 2019-07-10 16:04:20 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * aui-popup.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 auiPopup = function() { |
| 10 | }; |
| 11 | var isShow = false; |
| 12 | auiPopup.prototype = { |
| 13 | init: function(params,callback){ |
| 14 | this.frameBounces = params.frameBounces; |
| 15 | this.location = params.location; |
| 16 | this.buttons = params.buttons; |
| 17 | this.maskDiv; |
| 18 | this.popupDiv; |
| 19 | var self = this; |
| 20 | self.open(params,callback); |
| 21 | }, |
| 22 | open: function(params,callback) { |
| 23 | var buttonsHtml='',locationClass = 'aui-popup-top'; |
| 24 | var self = this; |
| 25 | if(self.popupDiv){ |
| 26 | self.close(); |
| 27 | return; |
| 28 | } |
| 29 | if(!self.maskDiv){ |
| 30 | self.maskDiv = document.createElement("div"); |
| 31 | self.maskDiv.className = "aui-mask"; |
| 32 | document.body.appendChild(self.maskDiv); |
| 33 | } |
| 34 | switch (self.location) { |
| 35 | case "top": |
| 36 | locationClass = 'aui-popup-top'; |
| 37 | break; |
| 38 | case "top-left": |
| 39 | locationClass = 'aui-popup-top-left'; |
| 40 | break; |
| 41 | case "top-right": |
| 42 | locationClass = 'aui-popup-top-right'; |
| 43 | break; |
| 44 | case "bottom": |
| 45 | locationClass = 'aui-popup-bottom'; |
| 46 | break; |
| 47 | case "bottom-left": |
| 48 | locationClass = 'aui-popup-bottom-left'; |
| 49 | break; |
| 50 | case "bottom-right": |
| 51 | locationClass = 'aui-popup-bottom-right'; |
| 52 | break; |
| 53 | default: |
| 54 | locationClass = 'aui-popup-top'; |
| 55 | break; |
| 56 | } |
| 57 | self.popupDiv = document.createElement("div"); |
| 58 | self.popupDiv.className = "aui-popup "+locationClass; |
| 59 | self.popupDiv.innerHTML = '<div class="aui-popup-arrow"></div><div class="aui-popup-content"></div>'; |
| 60 | document.body.appendChild(self.popupDiv); |
| 61 | if(self.buttons && self.buttons.length){ |
| 62 | buttonsHtml += '<ul class="aui-list aui-list-noborder">'; |
| 63 | for(var i = 0; i < self.buttons.length;i++){ |
| 64 | buttonsHtml += '<li class="aui-list-item aui-list-item-middle">'; |
| 65 | buttonsHtml += '<div class="aui-list-item-label-icon"><img src="'+self.buttons[i].image+'"></div>'; |
| 66 | buttonsHtml += '<div class="aui-list-item-inner">'+self.buttons[i].text+'</div>'; |
| 67 | buttonsHtml += '</li>'; |
| 68 | } |
| 69 | buttonsHtml += '</ul>'; |
| 70 | } |
| 71 | document.querySelector(".aui-popup .aui-popup-content").insertAdjacentHTML('beforeend', buttonsHtml); |
| 72 | var actionsheetHeight = document.querySelector(".aui-popup").offsetHeight; |
| 73 | self.maskDiv.classList.add("aui-mask-in"); |
| 74 | self.popupDiv.classList.add("aui-popup-in"); |
| 75 | self.popupDiv.addEventListener("touchmove", function(event){ |
| 76 | event.preventDefault(); |
| 77 | }) |
| 78 | self.maskDiv.addEventListener("touchmove", function(event){ |
| 79 | event.preventDefault(); |
| 80 | }) |
| 81 | if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){ |
| 82 | api.setFrameAttr({ |
| 83 | bounces:false |
| 84 | }); |
| 85 | } |
| 86 | var popupButtons = document.querySelectorAll(".aui-popup .aui-list-item"); |
| 87 | if(popupButtons && popupButtons.length > 0){ |
| 88 | setTimeout(function(){ |
| 89 | self.maskDiv.onclick = function(){self.close();return;}; |
| 90 | for(var ii = 0; ii < popupButtons.length; ii++){ |
| 91 | (function(e){ |
| 92 | popupButtons[e].onclick = function(){ |
| 93 | if(self.buttons[e].value){ |
| 94 | var _value = self.buttons[e].value; |
| 95 | }else{ |
| 96 | var _value = null; |
| 97 | } |
| 98 | if(callback){ |
| 99 | callback({ |
| 100 | buttonIndex: e+1, |
| 101 | buttonTitle: this.textContent, |
| 102 | buttonValue: _value |
| 103 | }); |
| 104 | }; |
| 105 | self.close(); |
| 106 | return; |
| 107 | } |
| 108 | })(ii) |
| 109 | } |
| 110 | }, 350) |
| 111 | } |
| 112 | }, |
| 113 | close: function(){ |
| 114 | var self = this; |
| 115 | if(typeof(api) != 'undefined' && typeof(api) == 'object' && self.frameBounces){ |
| 116 | api.setFrameAttr({ |
| 117 | bounces:true |
| 118 | }); |
| 119 | } |
| 120 | if(self.popupDiv){ |
| 121 | var actionsheetHeight = self.popupDiv.offsetHeight; |
| 122 | self.popupDiv.classList.add("aui-popup-out"); |
| 123 | self.maskDiv.style.opacity = 0; |
| 124 | setTimeout(function(){ |
| 125 | if(self.maskDiv){ |
| 126 | self.maskDiv.parentNode.removeChild(self.maskDiv); |
| 127 | } |
| 128 | self.popupDiv.parentNode.removeChild(self.popupDiv); |
| 129 | self.maskDiv = self.popupDiv = false; |
| 130 | }, 300) |
| 131 | } |
| 132 | } |
| 133 | }; |
| 134 | window.auiPopup = auiPopup; |
| 135 | })(window); |