MCPcopy
hub / github.com/dcloudio/mui / createPopup

Function createPopup

js/mui.popup.js:50–124  ·  view source on GitHub ↗
(html, callback)

Source from the content-addressed store, hash-verified

48 };
49
50 var createPopup = function(html, callback) {
51 var popupElement = document.createElement('div');
52 popupElement.className = CLASS_POPUP;
53 popupElement.innerHTML = html;
54 var removePopupElement = function() {
55 popupElement.parentNode && popupElement.parentNode.removeChild(popupElement);
56 popupElement = null;
57 };
58 popupElement.addEventListener($.EVENT_MOVE, $.preventDefault);
59 popupElement.addEventListener('webkitTransitionEnd', function(e) {
60 if (popupElement && e.target === popupElement && popupElement.classList.contains(CLASS_POPUP_OUT)) {
61 removePopupElement();
62 }
63 });
64 popupElement.style.display = 'block';
65 document.body.appendChild(popupElement);
66 popupElement.offsetHeight;
67 popupElement.classList.add(CLASS_POPUP_IN);
68
69 if (!backdrop.classList.contains(CLASS_ACTIVE)) {
70 backdrop.style.display = 'block';
71 document.body.appendChild(backdrop);
72 backdrop.offsetHeight;
73 backdrop.classList.add(CLASS_ACTIVE);
74 }
75 var btns = $.qsa('.' + CLASS_POPUP_BUTTON, popupElement);
76 var input = popupElement.querySelector('.' + CLASS_POPUP_INPUT + ' input');
77 var popup = {
78 element: popupElement,
79 close: function(index, animate) {
80 if (popupElement) {
81 var result = callback && callback({
82 index: index || 0,
83 value: input && input.value || ''
84 });
85 if (result === false) { //返回false则不关闭当前popup
86 return;
87 }
88 if (animate !== false) {
89 popupElement.classList.remove(CLASS_POPUP_IN);
90 popupElement.classList.add(CLASS_POPUP_OUT);
91 } else {
92 removePopupElement();
93 }
94 popupStack.pop();
95 //如果还有其他popup,则不remove backdrop
96 if (popupStack.length) {
97 popupStack[popupStack.length - 1]['show'](animate);
98 } else {
99 backdrop.classList.remove(CLASS_ACTIVE);
100 }
101 }
102 }
103 };
104 var handleEvent = function(e) {
105 popup.close(btns.indexOf(e.target));
106 };
107 $(popupElement).on('tap', '.' + CLASS_POPUP_BUTTON, handleEvent);

Callers 3

createAlertFunction · 0.70
createConfirmFunction · 0.70
createPromptFunction · 0.70

Calls 3

callbackFunction · 0.85
removePopupElementFunction · 0.70
$Function · 0.70

Tested by

no test coverage detected