c87c1c0e6c0cc401be29121b702e21d419231065.svn-base 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Created with Sublime Text 2.
  3. * license: http://www.lovewebgames.com/jsmodule/index.html
  4. * User: 田想兵
  5. * Date: 2015-03-16
  6. * Time: 20:27:54
  7. * Contact: 55342775@qq.com
  8. */
  9. ;
  10. (function(root, factory) {
  11. //amd
  12. if (typeof define === 'function' && define.amd) {
  13. define(['$'], factory);
  14. } else if (typeof exports === 'object') { //umd
  15. module.exports = factory();
  16. } else {
  17. root.Dialog = factory(window.Zepto || window.jQuery || $);
  18. }
  19. })(this, function($) {
  20. $.fn.Dialog = function(settings) {
  21. var list = [];
  22. $(this).each(function() {
  23. var dialog = new Dialog();
  24. var options = $.extend({
  25. trigger: $(this)
  26. }, settings);
  27. dialog.init(options);
  28. list.push(dialog);
  29. });
  30. return list;
  31. };
  32. $.Dialog = function(settings) {
  33. if (settings.type === "alert") {
  34. var alert = new Dialog();
  35. var html = '<div class="ui-alert-title">' + settings.content + '</div>';
  36. var action = '';
  37. if (settings.button) {
  38. if (typeof settings.button == 'boolean') {
  39. settings.button = '确定';
  40. };
  41. action = '<p class="ui-dialog-action"><button class="ui-alert-submit js-dialog-close">' + settings.button + '</button></p>';
  42. } else if (!settings.timer) {
  43. settings.timer = 3000;
  44. }
  45. html += action;
  46. var alertOptions = $.extend({
  47. target: html,
  48. animate: true,
  49. show: true,
  50. mask: true,
  51. className: "ui-alert",
  52. afterHide: function(c) {
  53. this.dispose();
  54. settings.callback && settings.callback();
  55. }
  56. }, settings);
  57. alert.init(alertOptions);
  58. if (settings.timer) {
  59. setTimeout(function() {
  60. alert.dispose();
  61. settings.callback && settings.callback();
  62. }, settings.timer);
  63. }
  64. alert.touch(alert.mask, function() {
  65. alert.hide();
  66. settings.callback && settings.callback();
  67. });
  68. }
  69. if (settings.type === "confirm") {
  70. var dialog = new Dialog();
  71. var html = '<div class="ui-confirm-title">' + settings.content + '</div>';
  72. var action = '';
  73. if (!settings.buttons) {
  74. settings.buttons = [{
  75. 'yes': '确定'
  76. }, {
  77. 'no': '取消'
  78. }];
  79. };
  80. var btnstr = '';
  81. for (var i = 0, l = settings.buttons.length; i < l; i++) {
  82. var item = settings.buttons[i];
  83. if (item.yes) {
  84. btnstr += '<td><button class="ui-confirm-submit " data-type="yes">' + item.yes + '</button></td>';
  85. }
  86. if (item.no) {
  87. btnstr += '<td><button class="ui-confirm-no" data-type="no">' + item.no + '</button></td>';
  88. }
  89. if (item.close) {
  90. btnstr += '<td><button class="ui-confirm-close js-dialog-close" data-type="close">' + item.close + '</button></td>';
  91. }
  92. }
  93. action = '<table class="ui-dialog-action"><tr>' + btnstr + '</tr></table>';
  94. html += action;
  95. var options = $.extend({
  96. target: html,
  97. animate: true,
  98. show: true,
  99. fixed:true,
  100. mask: true,
  101. className: "ui-alert",
  102. afterHide: function(c) {
  103. this.dispose();
  104. },
  105. beforeShow: function(c) {
  106. dialog.touch($('.ui-confirm-submit', c), function() {
  107. settings.callback && settings.callback.call(dialog, 'yes', c);
  108. });
  109. dialog.touch($('.ui-confirm-no', c), function() {
  110. settings.callback && settings.callback.call(dialog, 'no', c);
  111. });
  112. dialog.touch($('.ui-confirm-close', c), function() {
  113. settings.callback && settings.callback.call(dialog, 'close', c);
  114. });
  115. }
  116. }, settings);
  117. dialog.init(options);
  118. }
  119. };
  120. /*alert*/
  121. $.alert = function(content, button, callback, timer, settings) {
  122. var options = {};
  123. var defaults = {
  124. zIndex: 100,
  125. type: 'alert'
  126. };
  127. if (typeof content == 'object') {
  128. options = $.extend(defaults, content);
  129. } else {
  130. options = $.extend(defaults, {
  131. content: content,
  132. button: button,
  133. timer: timer,
  134. callback: callback
  135. });
  136. }
  137. $.Dialog($.extend(options, settings));
  138. }
  139. /*
  140. buttons :[{yes:"确定"},{no:'取消'},{close:'关闭'}]
  141. */
  142. $.confirm = function(content, buttons, callback, settings) {
  143. var options = {};
  144. var defaults = {
  145. zIndex: 100,
  146. type: 'confirm'
  147. };
  148. if (typeof content == 'object') {
  149. options = $.extend(defaults, content);
  150. } else {
  151. options = $.extend(defaults, {
  152. content: content,
  153. buttons: buttons,
  154. callback: callback
  155. });
  156. }
  157. $.Dialog($.extend(options, settings));
  158. }
  159. var Dialog = function() {
  160. var rnd = Math.random().toString().replace('.', '');
  161. this.id = 'dialog_' + rnd;
  162. this.settings = {};
  163. this.settings.closeTpl = $('<span class="ui-dialog-close js-dialog-close">x</span>');
  164. this.settings.titleTpl = $('<div class="ui-dialog-title"></div>');
  165. this.timer = null;
  166. this.showed = false;
  167. this.mask = $();
  168. }
  169. Dialog.prototype = {
  170. init: function(settings) {
  171. var _this = this;
  172. this.settings = $.extend({
  173. fixed: false//是否固定位置,
  174. }, this.settings, settings);
  175. if (this.settings.mask) {
  176. this.mask = $('<div class="ui-dialog-mask"/>');
  177. $('body').append(this.mask);
  178. }
  179. $('body').append('<div class="ui-dialog" id="' + this.id + '"></div>');
  180. this.dialogContainer = $('#' + this.id);
  181. var zIndex = this.settings.zIndex || 10;
  182. this.dialogContainer.css({
  183. 'zIndex': zIndex
  184. });
  185. if (this.settings.className) {
  186. this.dialogContainer.addClass(this.settings.className);
  187. };
  188. this.mask.css({
  189. 'zIndex': zIndex - 1
  190. });
  191. if (this.settings.closeTpl) {
  192. this.dialogContainer.append(this.settings.closeTpl);
  193. }
  194. if (this.settings.title) {
  195. this.dialogContainer.append(this.settings.titleTpl);
  196. this.settings.titleTpl.html(this.settings.title);
  197. }
  198. this.bindEvent();
  199. if (this.settings.show) {
  200. this.show();
  201. }
  202. },
  203. touch: function(obj, fn) {
  204. var move;
  205. $(obj).on('click', click);
  206. function click(e) {
  207. return fn.call(this, e);
  208. }
  209. $(obj).on('touchmove', function(e) {
  210. move = true;
  211. }).on('touchend', function(e) {
  212. e.preventDefault();
  213. if (!move) {
  214. var returnvalue = fn.call(this, e, 'touch');
  215. if (!returnvalue) {
  216. e.preventDefault();
  217. e.stopPropagation();
  218. }
  219. }
  220. move = false;
  221. });
  222. },
  223. bindEvent: function() {
  224. var _this = this;
  225. if (this.settings.trigger) {
  226. $(this.settings.trigger).click(function() {
  227. _this.show()
  228. });
  229. _this.touch($(this.settings.trigger), function() {
  230. _this.show()
  231. });
  232. };
  233. $(this.dialogContainer).on('click', '.js-dialog-close', function() {
  234. _this.hide();
  235. return false;
  236. })
  237. // $(window).resize(function() {
  238. // _this.setPosition();
  239. // });
  240. // $(window).scroll(function() {
  241. // _this.setPosition();
  242. // })
  243. $(document).keydown(function(e) {
  244. if (e.keyCode === 27 && _this.showed) {
  245. _this.hide();
  246. }
  247. });
  248. $(this.dialogContainer).on('hide', function() {
  249. _this.hide();
  250. })
  251. },
  252. dispose: function() {
  253. this.dialogContainer.remove();
  254. this.mask.remove();
  255. this.timer && clearInterval(this.timer);
  256. },
  257. hide: function() {
  258. var _this = this;
  259. if (_this.settings.beforeHide) {
  260. _this.settings.beforeHide.call(_this, _this.dialogContainer);
  261. }
  262. this.showed = false;
  263. this.mask.hide();
  264. this.timer && clearInterval(this.timer);
  265. if (this.settings.animate) {
  266. this.dialogContainer.removeClass('zoomIn').addClass("zoomOut");
  267. setTimeout(function() {
  268. _this.dialogContainer.hide();
  269. if (typeof _this.settings.target === "object") {
  270. $('body').append(_this.dialogContainer.hide());
  271. }
  272. if (_this.settings.afterHide) {
  273. _this.settings.afterHide.call(_this, _this.dialogContainer);
  274. }
  275. }, 500);
  276. } else {
  277. this.dialogContainer.hide();
  278. if (typeof this.settings.target === "object") {
  279. $('body').append(this.dialogContainer)
  280. }
  281. if (this.settings.afterHide) {
  282. this.settings.afterHide.call(this, this.dialogContainer);
  283. }
  284. }
  285. },
  286. show: function() {
  287. if (typeof this.settings.target === "string") {
  288. if (/^(\.|\#\w+)/gi.test(this.settings.target)) {
  289. this.dailogContent = $(this.settings.target);
  290. } else {
  291. this.dailogContent = $('<div>' + this.settings.target + '</div>')
  292. }
  293. } else {
  294. this.dailogContent = this.settings.target;
  295. }
  296. this.mask.show();
  297. this.dailogContent.show();
  298. this.height = this.settings.height || 'auto' //this.dialogContainer.height();
  299. this.width = this.settings.width || 'auto' //this.dialogContainer.width();
  300. this.dialogContainer.append(this.dailogContent).show().css({
  301. height: this.height,
  302. width: this.width
  303. });
  304. if (this.settings.beforeShow) {
  305. this.settings.beforeShow.call(this, this.dialogContainer);
  306. }
  307. this.showed = true;
  308. $(this.settings.trigger).blur();
  309. this.setPosition();
  310. var _this = this;
  311. // $.alert(this.settings.clientWidth)
  312. this.timer && clearInterval(this.timer);
  313. if (this.settings.fixed) {
  314. this.timer = setInterval(function() {
  315. _this.setPosition();
  316. }, 1000);
  317. }
  318. if (this.settings.animate) {
  319. this.dialogContainer.addClass('zoomIn').removeClass('zoomOut').addClass('animated');
  320. }
  321. },
  322. setPosition: function() {
  323. if (this.showed) {
  324. var _this = this;
  325. this.dialogContainer.show();
  326. this.height = this.settings.height;
  327. this.width = this.settings.width;
  328. if (isNaN(this.height)) {
  329. this.height = (this.dialogContainer.outerHeight && this.dialogContainer.outerHeight()) || this.dialogContainer.height();
  330. }
  331. if (isNaN(this.width)) {
  332. this.width = (this.dialogContainer.outerWidth && this.dialogContainer.outerWidth()) || this.dialogContainer.width();
  333. }
  334. var clientHeight = this.settings.clientHeight || document.documentElement.clientHeight || document.body.clientHeight;
  335. var clientWidth = this.settings.clientWidth || document.documentElement.clientWidth || document.body.clientWidth;
  336. var ml = this.width / 2;
  337. var mt = this.height / 2;
  338. var left = clientWidth / 2 - ml;
  339. var top = clientHeight / 2 - mt;
  340. left = Math.floor(Math.max(0, left));
  341. top = Math.floor(Math.max(0, top));
  342. var position = 'absolute';
  343. if(_this.settings.fixed){
  344. position='fixed';
  345. }
  346. _this.dialogContainer.css({
  347. position: position,
  348. top: top,
  349. left: left
  350. });
  351. }
  352. }
  353. }
  354. return Dialog;
  355. });