You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

256 lines
6.7 KiB

  1. /*
  2. * 使用说明:
  3. * window.wxc.Pop(popHtml, [type], [options])
  4. * popHtml:html字符串
  5. * type:window.wxc.xcConfirm.typeEnum集合中的元素
  6. * options:扩展对象
  7. * 用法:
  8. * 1. window.wxc.xcConfirm("我是弹窗<span>lalala</span>");
  9. * 2. window.wxc.xcConfirm("成功","success");
  10. * 3. window.wxc.xcConfirm("请输入","input",{onOk:function(){}})
  11. * 4. window.wxc.xcConfirm("自定义",{title:"自定义"})
  12. */
  13. (function($){
  14. window.wxc = window.wxc || {};
  15. window.wxc.xcConfirm = function(popHtml, type, options) {
  16. var btnType = window.wxc.xcConfirm.btnEnum;
  17. var eventType = window.wxc.xcConfirm.eventEnum;
  18. var popType = {
  19. info: {
  20. title: "信息",
  21. icon: "0 0",//蓝色i
  22. btn: btnType.ok
  23. },
  24. success: {
  25. title: "成功",
  26. icon: "0 -48px",//绿色对勾
  27. btn: btnType.ok
  28. },
  29. error: {
  30. title: "错误",
  31. icon: "-48px -48px",//红色叉
  32. btn: btnType.ok
  33. },
  34. confirm: {
  35. title: "提示",
  36. icon: "-48px 0",//黄色问号
  37. btn: btnType.okcancel
  38. },
  39. warning: {
  40. title: "警告",
  41. icon: "0 -96px",//黄色叹号
  42. btn: btnType.okcancel
  43. },
  44. input: {
  45. title: "输入",
  46. icon: "",
  47. btn: btnType.ok
  48. },
  49. custom: {
  50. title: "",
  51. icon: "",
  52. btn: btnType.ok
  53. }
  54. };
  55. var itype = type ? type instanceof Object ? type : popType[type] || {} : {};//格式化输入的参数:弹窗类型
  56. var config = $.extend(true, {
  57. //属性
  58. title: "", //自定义的标题
  59. icon: "", //图标
  60. btn: btnType.ok, //按钮,默认单按钮
  61. //事件
  62. onOk: $.noop,//点击确定的按钮回调
  63. onCancel: $.noop,//点击取消的按钮回调
  64. onClose: $.noop//弹窗关闭的回调,返回触发事件
  65. }, itype, options);
  66. var $txt = $("<p>").html(popHtml);//弹窗文本dom
  67. var $tt = $("<span>").addClass("tt").text(config.title);//标题
  68. var icon = config.icon;
  69. var $icon = icon ? $("<div>").addClass("bigIcon").css("backgroundPosition",icon) : "";
  70. var btn = config.btn;//按钮组生成参数
  71. var popId = creatPopId();//弹窗索引
  72. var $box = $("<div>").addClass("xcConfirm");//弹窗插件容器
  73. var $layer = $("<div>").addClass("xc_layer fadeIn animated infinite");//遮罩层
  74. var $popBox = $("<div>").addClass("popBox bounceInDown animated infinite");//弹窗盒子
  75. var $ttBox = $("<div>").addClass("ttBox");//弹窗顶部区域
  76. var $txtBox = $("<div>").addClass("txtBox");//弹窗内容主体区
  77. var $btnArea = $("<div>").addClass("btnArea");//按钮区域
  78. var $ok = $("<a>").addClass("sgBtn btn btn-primary").addClass("ok").text("确定");//确定按钮
  79. var $cancel = $("<a>").addClass("sgBtn").addClass("cancel btn btn-default").text("取消");//取消按钮
  80. var $input = $("<input>").addClass("inputBox form-control");//输入框
  81. var $clsBtn = $("<a>").addClass("clsBtn");//关闭按钮
  82. //建立按钮映射关系
  83. var btns = {
  84. ok: $ok,
  85. cancel: $cancel
  86. };
  87. init();
  88. function init(){
  89. //处理特殊类型input
  90. if(popType["input"] === itype){
  91. $txt.append($input);
  92. }
  93. creatDom();
  94. bind();
  95. }
  96. function creatDom(){
  97. $popBox.append(
  98. $ttBox.append(
  99. $clsBtn
  100. ).append(
  101. $tt
  102. )
  103. ).append(
  104. $txtBox.append($icon).append($txt)
  105. ).append(
  106. $btnArea.append(creatBtnGroup(btn))
  107. );
  108. $box.attr("id", popId).append($layer).append($popBox);
  109. $("body").append($box);
  110. function removeClass_fadeIn(){
  111. $('.xc_layer').removeClass("fadeIn animated infinite");
  112. }
  113. function removeClass_bounceInDown(){
  114. $('.popBox').removeClass("bounceInDown animated infinite");
  115. }
  116. setTimeout(removeClass_fadeIn, 590);
  117. setTimeout(removeClass_bounceInDown, 590);
  118. }
  119. function bind(){
  120. //点击确认按钮
  121. $ok.click(doOk);
  122. //键盘事件
  123. $(window).bind("keydown", function(e){
  124. //回车键触发确认按钮事件
  125. if(e.keyCode == 13 || e.keyCode == 108) {
  126. if($("#" + popId).length == 1){
  127. doOk();
  128. }
  129. }
  130. //ESC键触发确认按钮事件
  131. if(e.keyCode == 27){
  132. if($('#' + popId).length == 1){
  133. doClose();
  134. }
  135. }
  136. });
  137. //点击取消按钮
  138. $cancel.click(doCancel);
  139. //点击关闭按钮
  140. $clsBtn.click(doClose);
  141. }
  142. //确认按钮事件
  143. function doOk(){
  144. var $o = $(this);
  145. var v = $.trim($input.val());
  146. if ($input.is(":visible"))
  147. config.onOk(v);
  148. else
  149. config.onOk();
  150. $(".xc_layer").addClass("inFade animated infinite");
  151. $(".popBox").addClass("bounceDownIn animated infinite");
  152. function removeClass_bounceDownIn(){
  153. $('.xc_layer').removeClass("inFade animated infinite");
  154. $('.popBox').removeClass("bounceDownIn animated infinite");
  155. $("#" + popId).remove();
  156. }
  157. setTimeout(removeClass_bounceDownIn, 590);
  158. config.onClose(eventType.ok);
  159. }
  160. //取消按钮事件
  161. function doCancel(){
  162. var $o = $(this);
  163. config.onCancel();
  164. $(".xc_layer").addClass("inFade animated infinite");
  165. $(".popBox").addClass("bounceDownIn animated infinite");
  166. function removeClass_bounceDownIn(){
  167. $('.xc_layer').removeClass("inFade animated infinite");
  168. $('.popBox').removeClass("bounceDownIn animated infinite");
  169. $("#" + popId).remove();
  170. }
  171. setTimeout(removeClass_bounceDownIn, 590);
  172. config.onClose(eventType.cancel);
  173. }
  174. //关闭按钮事件
  175. function doClose(){
  176. $(".xc_layer").addClass("inFade animated infinite");
  177. $(".popBox").addClass("bounceDownIn animated infinite");
  178. function removeClass_bounceDownIn(){
  179. $('.xc_layer').removeClass("inFade animated infinite");
  180. $('.popBox').removeClass("bounceDownIn animated infinite");
  181. $("#" + popId).remove();
  182. }
  183. setTimeout(removeClass_bounceDownIn, 590);
  184. config.onClose(eventType.close);
  185. $(window).unbind("keydown");
  186. }
  187. //生成按钮组
  188. function creatBtnGroup(tp){
  189. var $bgp = $("<div>").addClass("btnGroup");
  190. $.each(btns, function(i, n){
  191. if( btnType[i] == (tp & btnType[i]) ){
  192. $bgp.append(n);
  193. }
  194. });
  195. return $bgp;
  196. }
  197. //重生popId,防止id重复
  198. function creatPopId(){
  199. var i = "pop_" + (new Date()).getTime()+parseInt(Math.random()*100000);//弹窗索引
  200. if($("#" + i).length > 0){
  201. return creatPopId();
  202. }else{
  203. return i;
  204. }
  205. }
  206. };
  207. //按钮类型
  208. window.wxc.xcConfirm.btnEnum = {
  209. ok: parseInt("0001",2), //确定按钮
  210. cancel: parseInt("0010",2), //取消按钮
  211. okcancel: parseInt("0011",2) //确定&&取消
  212. };
  213. //触发事件类型
  214. window.wxc.xcConfirm.eventEnum = {
  215. ok: 1,
  216. cancel: 2,
  217. close: 3
  218. };
  219. //弹窗类型
  220. window.wxc.xcConfirm.typeEnum = {
  221. info: "info",
  222. success: "success",
  223. error:"error",
  224. confirm: "confirm",
  225. warning: "warning",
  226. input: "input",
  227. custom: "custom"
  228. };
  229. })(jQuery);