mui.popup.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Popup(alert,confirm,prompt)
  3. * @param {Object} $
  4. * @param {Object} window
  5. * @param {Object} document
  6. */
  7. (function($, window, document) {
  8. var CLASS_POPUP = $.className('popup');
  9. var CLASS_POPUP_BACKDROP = $.className('popup-backdrop');
  10. var CLASS_POPUP_IN = $.className('popup-in');
  11. var CLASS_POPUP_OUT = $.className('popup-out');
  12. var CLASS_POPUP_INNER = $.className('popup-inner');
  13. var CLASS_POPUP_TITLE = $.className('popup-title');
  14. var CLASS_POPUP_TEXT = $.className('popup-text');
  15. var CLASS_POPUP_INPUT = $.className('popup-input');
  16. var CLASS_POPUP_BUTTONS = $.className('popup-buttons');
  17. var CLASS_POPUP_BUTTON = $.className('popup-button');
  18. var CLASS_POPUP_BUTTON_BOLD = $.className('popup-button-bold');
  19. var CLASS_POPUP_BACKDROP = $.className('popup-backdrop');
  20. var CLASS_ACTIVE = $.className('active');
  21. var popupStack = [];
  22. var backdrop = (function() {
  23. var element = document.createElement('div');
  24. element.classList.add(CLASS_POPUP_BACKDROP);
  25. element.addEventListener($.EVENT_MOVE, $.preventDefault);
  26. element.addEventListener('webkitTransitionEnd', function() {
  27. if (!this.classList.contains(CLASS_ACTIVE)) {
  28. element.parentNode && element.parentNode.removeChild(element);
  29. }
  30. });
  31. return element;
  32. }());
  33. var createInput = function(placeholder) {
  34. return '<div class="' + CLASS_POPUP_INPUT + '"><input type="text" autofocus placeholder="' + (placeholder || '') + '"/></div>';
  35. };
  36. var createInner = function(message, title, extra) {
  37. return '<div class="' + CLASS_POPUP_INNER + '"><div class="' + CLASS_POPUP_TITLE + '">' + title + '</div><div class="' + CLASS_POPUP_TEXT + '">' + message.replace(/\r\n/g, "<br/>").replace(/\n/g, "<br/>") + '</div>' + (extra || '') + '</div>';
  38. };
  39. var createButtons = function(btnArray) {
  40. var length = btnArray.length;
  41. var btns = [];
  42. for (var i = 0; i < length; i++) {
  43. btns.push('<span class="' + CLASS_POPUP_BUTTON + (i === length - 1 ? (' ' + CLASS_POPUP_BUTTON_BOLD) : '') + '">' + btnArray[i] + '</span>');
  44. }
  45. return '<div class="' + CLASS_POPUP_BUTTONS + '">' + btns.join('') + '</div>';
  46. };
  47. var createPopup = function(html, callback) {
  48. var popupElement = document.createElement('div');
  49. popupElement.className = CLASS_POPUP;
  50. popupElement.innerHTML = html;
  51. var removePopupElement = function() {
  52. popupElement.parentNode && popupElement.parentNode.removeChild(popupElement);
  53. popupElement = null;
  54. };
  55. popupElement.addEventListener($.EVENT_MOVE, $.preventDefault);
  56. popupElement.addEventListener('webkitTransitionEnd', function(e) {
  57. if (popupElement && e.target === popupElement && popupElement.classList.contains(CLASS_POPUP_OUT)) {
  58. removePopupElement();
  59. }
  60. });
  61. popupElement.style.display = 'block';
  62. document.body.appendChild(popupElement);
  63. popupElement.offsetHeight;
  64. popupElement.classList.add(CLASS_POPUP_IN);
  65. if (!backdrop.classList.contains(CLASS_ACTIVE)) {
  66. backdrop.style.display = 'block';
  67. document.body.appendChild(backdrop);
  68. backdrop.offsetHeight;
  69. backdrop.classList.add(CLASS_ACTIVE);
  70. }
  71. var btns = $.qsa('.' + CLASS_POPUP_BUTTON, popupElement);
  72. var input = popupElement.querySelector('.' + CLASS_POPUP_INPUT + ' input');
  73. var popup = {
  74. element: popupElement,
  75. close: function(index, animate) {
  76. if (popupElement) {
  77. var result = callback && callback({
  78. index: index || 0,
  79. value: input && input.value || ''
  80. });
  81. if (result === false) { //返回false则不关闭当前popup
  82. return;
  83. }
  84. if (animate !== false) {
  85. popupElement.classList.remove(CLASS_POPUP_IN);
  86. popupElement.classList.add(CLASS_POPUP_OUT);
  87. } else {
  88. removePopupElement();
  89. }
  90. popupStack.pop();
  91. //如果还有其他popup,则不remove backdrop
  92. if (popupStack.length) {
  93. popupStack[popupStack.length - 1]['show'](animate);
  94. } else {
  95. backdrop.classList.remove(CLASS_ACTIVE);
  96. }
  97. }
  98. }
  99. };
  100. var handleEvent = function(e) {
  101. popup.close(btns.indexOf(e.target));
  102. };
  103. $(popupElement).on('tap', '.' + CLASS_POPUP_BUTTON, handleEvent);
  104. if (popupStack.length) {
  105. popupStack[popupStack.length - 1]['hide']();
  106. }
  107. popupStack.push({
  108. close: popup.close,
  109. show: function(animate) {
  110. popupElement.style.display = 'block';
  111. popupElement.offsetHeight;
  112. popupElement.classList.add(CLASS_POPUP_IN);
  113. },
  114. hide: function() {
  115. popupElement.style.display = 'none';
  116. popupElement.classList.remove(CLASS_POPUP_IN);
  117. }
  118. });
  119. return popup;
  120. };
  121. var createAlert = function(message, title, btnValue, callback, type) {
  122. if (typeof message === 'undefined') {
  123. return;
  124. } else {
  125. if (typeof title === 'function') {
  126. callback = title;
  127. type = btnValue;
  128. title = null;
  129. btnValue = null;
  130. } else if (typeof btnValue === 'function') {
  131. type = callback;
  132. callback = btnValue;
  133. btnValue = null;
  134. }
  135. }
  136. if (!$.os.plus || type === 'div') {
  137. return createPopup(createInner(message, title || '提示') + createButtons([btnValue || '确定']), callback);
  138. }
  139. return plus.nativeUI.alert(message, callback, title || '提示', btnValue || '确定');
  140. };
  141. var createConfirm = function(message, title, btnArray, callback, type) {
  142. if (typeof message === 'undefined') {
  143. return;
  144. } else {
  145. if (typeof title === 'function') {
  146. callback = title;
  147. type = btnArray;
  148. title = null;
  149. btnArray = null;
  150. } else if (typeof btnArray === 'function') {
  151. type = callback;
  152. callback = btnArray;
  153. btnArray = null;
  154. }
  155. }
  156. if (!$.os.plus || type === 'div') {
  157. return createPopup(createInner(message, title || '提示') + createButtons(btnArray || ['取消', '确认']), callback);
  158. }
  159. return plus.nativeUI.confirm(message, callback, title, btnArray || ['取消', '确认']);
  160. };
  161. var createPrompt = function(message, placeholder, title, btnArray, callback, type) {
  162. if (typeof message === 'undefined') {
  163. return;
  164. } else {
  165. if (typeof placeholder === 'function') {
  166. callback = placeholder;
  167. type = title;
  168. placeholder = null;
  169. title = null;
  170. btnArray = null;
  171. } else if (typeof title === 'function') {
  172. callback = title;
  173. type = btnArray;
  174. title = null;
  175. btnArray = null;
  176. } else if (typeof btnArray === 'function') {
  177. type = callback;
  178. callback = btnArray;
  179. btnArray = null;
  180. }
  181. }
  182. if (!$.os.plus || type === 'div') {
  183. return createPopup(createInner(message, title || '提示', createInput(placeholder)) + createButtons(btnArray || ['取消', '确认']), callback);
  184. }
  185. return plus.nativeUI.prompt(message, callback, title || '提示', placeholder, btnArray || ['取消', '确认']);
  186. };
  187. var closePopup = function() {
  188. if (popupStack.length) {
  189. popupStack[popupStack.length - 1]['close']();
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. };
  195. var closePopups = function() {
  196. while (popupStack.length) {
  197. popupStack[popupStack.length - 1]['close']();
  198. }
  199. };
  200. $.closePopup = closePopup;
  201. $.closePopups = closePopups;
  202. $.alert = createAlert;
  203. $.confirm = createConfirm;
  204. $.prompt = createPrompt;
  205. })(mui, window, document);