aui-toast.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * aui-popup.js
  3. * @author 流浪男
  4. * @todo more things to abstract, e.g. Loading css etc.
  5. * Licensed under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. */
  8. (function( window, undefined ) {
  9. "use strict";
  10. var auiToast = function() {
  11. // this.create();
  12. };
  13. var isShow = false;
  14. auiToast.prototype = {
  15. create: function(params,callback) {
  16. var self = this;
  17. var toastHtml = '';
  18. switch (params.type) {
  19. case "success":
  20. var iconHtml = '<i class="aui-iconfont aui-icon-correct"></i>';
  21. break;
  22. case "fail":
  23. var iconHtml = '<i class="aui-iconfont aui-icon-close"></i>';
  24. break;
  25. case "custom":
  26. var iconHtml = params.html;
  27. break;
  28. case "loading":
  29. var iconHtml = '<div class="aui-toast-loading"></div>';
  30. break;
  31. }
  32. var titleHtml = params.title ? '<div class="aui-toast-content">'+params.title+'</div>' : '';
  33. toastHtml = '<div class="aui-toast">'+iconHtml+titleHtml+'</div>';
  34. if(document.querySelector(".aui-toast"))return;
  35. document.body.insertAdjacentHTML('beforeend', toastHtml);
  36. var duration = params.duration ? params.duration : "2000";
  37. self.show();
  38. if(params.type == 'loading'){
  39. if(callback){
  40. callback({
  41. status: "success"
  42. });
  43. };
  44. }else{
  45. setTimeout(function(){
  46. self.hide();
  47. }, duration)
  48. }
  49. },
  50. show: function(){
  51. var self = this;
  52. document.querySelector(".aui-toast").style.display = "block";
  53. document.querySelector(".aui-toast").style.marginTop = "-"+Math.round(document.querySelector(".aui-toast").offsetHeight/2)+"px";
  54. if(document.querySelector(".aui-toast"))return;
  55. },
  56. hide: function(){
  57. var self = this;
  58. if(document.querySelector(".aui-toast")){
  59. document.querySelector(".aui-toast").parentNode.removeChild(document.querySelector(".aui-toast"));
  60. }
  61. },
  62. remove: function(){
  63. if(document.querySelector(".aui-dialog"))document.querySelector(".aui-dialog").parentNode.removeChild(document.querySelector(".aui-dialog"));
  64. if(document.querySelector(".aui-mask")){
  65. document.querySelector(".aui-mask").classList.remove("aui-mask-out");
  66. }
  67. return true;
  68. },
  69. success: function(params,callback){
  70. var self = this;
  71. params.type = "success";
  72. return self.create(params,callback);
  73. },
  74. fail: function(params,callback){
  75. var self = this;
  76. params.type = "fail";
  77. return self.create(params,callback);
  78. },
  79. custom:function(params,callback){
  80. var self = this;
  81. params.type = "custom";
  82. return self.create(params,callback);
  83. },
  84. loading:function(params,callback){
  85. var self = this;
  86. params.type = "loading";
  87. return self.create(params,callback);
  88. }
  89. };
  90. window.auiToast = auiToast;
  91. })(window);