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.
 
 
 
 

222 lines
5.2 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");//遮罩层
  74. var $popBox = $("<div>").addClass("popBox");//弹窗盒子
  75. var $ttBox = $("<div>").addClass("ttBox");//弹窗顶部区域
  76. var $txtBox = $("<div>").addClass("txtBox");//弹窗内容主体区
  77. var $btnArea = $("<div>").addClass("btnArea");//按钮区域
  78. var $ok = $("<a>").addClass("sgBtn").addClass("ok").text("确定");//确定按钮
  79. var $cancel = $("<a>").addClass("sgBtn").addClass("cancel").text("取消");//取消按钮
  80. var $input = $("<input>").addClass("inputBox");//输入框
  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. }
  111. function bind(){
  112. //点击确认按钮
  113. $ok.click(doOk);
  114. //键盘事件
  115. $(window).bind("keydown", function(e){
  116. //回车键触发确认按钮事件
  117. if(e.keyCode == 13 || e.keyCode == 108) {
  118. if($("#" + popId).length == 1){
  119. doOk();
  120. }
  121. }
  122. //ESC键触发确认按钮事件
  123. if(e.keyCode == 27){
  124. if($('#' + popId).length == 1){
  125. doClose();
  126. }
  127. }
  128. });
  129. //点击取消按钮
  130. $cancel.click(doCancel);
  131. //点击关闭按钮
  132. $clsBtn.click(doClose);
  133. }
  134. //确认按钮事件
  135. function doOk(){
  136. var $o = $(this);
  137. var v = $.trim($input.val());
  138. if ($input.is(":visible"))
  139. config.onOk(v);
  140. else
  141. config.onOk();
  142. $("#" + popId).remove();
  143. config.onClose(eventType.ok);
  144. }
  145. //取消按钮事件
  146. function doCancel(){
  147. var $o = $(this);
  148. config.onCancel();
  149. $("#" + popId).remove();
  150. config.onClose(eventType.cancel);
  151. }
  152. //关闭按钮事件
  153. function doClose(){
  154. $("#" + popId).remove();
  155. config.onClose(eventType.close);
  156. $(window).unbind("keydown");
  157. }
  158. //生成按钮组
  159. function creatBtnGroup(tp){
  160. var $bgp = $("<div>").addClass("btnGroup");
  161. $.each(btns, function(i, n){
  162. if( btnType[i] == (tp & btnType[i]) ){
  163. $bgp.append(n);
  164. }
  165. });
  166. return $bgp;
  167. }
  168. //重生popId,防止id重复
  169. function creatPopId(){
  170. var i = "pop_" + (new Date()).getTime()+parseInt(Math.random()*100000);//弹窗索引
  171. if($("#" + i).length > 0){
  172. return creatPopId();
  173. }else{
  174. return i;
  175. }
  176. }
  177. };
  178. //按钮类型
  179. window.wxc.xcConfirm.btnEnum = {
  180. ok: parseInt("0001",2), //确定按钮
  181. cancel: parseInt("0010",2), //取消按钮
  182. okcancel: parseInt("0011",2) //确定&&取消
  183. };
  184. //触发事件类型
  185. window.wxc.xcConfirm.eventEnum = {
  186. ok: 1,
  187. cancel: 2,
  188. close: 3
  189. };
  190. //弹窗类型
  191. window.wxc.xcConfirm.typeEnum = {
  192. info: "info",
  193. success: "success",
  194. error:"error",
  195. confirm: "confirm",
  196. warning: "warning",
  197. input: "input",
  198. custom: "custom"
  199. };
  200. })(jQuery);