jquery.modal.dialog.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* ============================================================
  2. * jquery.modal.dialog.js 弹框Plugin
  3. * ============================================================ */
  4. +function ($) {
  5. 'use strict';
  6. var Dialog = function (element, options) {
  7. this.options = this.getOptions(options);
  8. this.$body = $(document.body);
  9. this.$element = $(element);
  10. this.$backdrop = null;
  11. this.$modal = null;
  12. this.$dialog = null;
  13. this.$dialogBox = null;
  14. this.$dialogHeader = null;
  15. this.$dialogTitle = null;
  16. this.$dialogContent = null;
  17. this.$dialogFooter = null;
  18. this.$dismissModal = null;
  19. this.$yesOperation = null;
  20. this.$otherOperation = null;
  21. this.$cancelOperation = null;
  22. this.$closeDiloag = true;
  23. this.$ie = !-[1,];
  24. this.$ie6 = !-[1,]&&!window.XMLHttpRequest;
  25. this.show();
  26. }
  27. Dialog.DEFAULTS = {
  28. backdrop: true,
  29. border: true,
  30. template: [
  31. '<div class="modal">',
  32. '<div class="modal_dialog">',
  33. '<div class="modal_content pie_about">',
  34. '<div class="modal_header">',
  35. '<span class="title modal_title"></span>',
  36. '<span class="max_remind"></span>',
  37. '<a href="javascript:;" class="close J_dismiss_modal"></a>',
  38. '</div>',
  39. '<div class="modal_body"></div>',
  40. '<div class="modal_footer">',
  41. '<div class="res_add_but">',
  42. '<div class="butlist">',
  43. '<input type="button" readonly="readonly" class="btn_blue J_hoverbut btn_100_38 J_dismiss_modal_yes J_btnyes" value="" />',
  44. '</div>',
  45. '<div class="butlist">',
  46. '<input type="button" readonly="readonly" class="btn_lightgray J_hoverbut btn_100_38 J_dismiss_modal J_btncancel" value="" />',
  47. '</div>',
  48. '<div class="clear"></div>',
  49. '</div>',
  50. '</div>',
  51. '<input type="hidden" class="J_btnload" />',
  52. '</div>',
  53. '</div>',
  54. '</div>'
  55. ].join(''),
  56. template3: [
  57. '<div class="modal">',
  58. '<div class="modal_dialog">',
  59. '<div class="modal_content pie_about">',
  60. '<div class="modal_header">',
  61. '<span class="title modal_title"></span>',
  62. '<span class="max_remind"></span>',
  63. '<a href="javascript:;" class="close J_dismiss_modal"></a>',
  64. '</div>',
  65. '<div class="modal_body"></div>',
  66. '<div class="modal_footer">',
  67. '<div class="res_add_but b3">',
  68. '<div class="butlist">',
  69. '<input type="button" readonly="readonly" class="btn_blue J_hoverbut btn_100_38 J_dismiss_modal_yes J_btnyes" value="" />',
  70. '</div>',
  71. '<div class="butlist">',
  72. '<input type="button" readonly="readonly" class="btn_blue J_hoverbut btn_100_38 J_dismiss_modal J_btnother" value="" />',
  73. '</div>',
  74. '<div class="butlist">',
  75. '<input type="button" readonly="readonly" class="btn_lightgray J_hoverbut btn_100_38 J_dismiss_modal J_btncancel" value="" />',
  76. '</div>',
  77. '<div class="clear"></div>',
  78. '</div>',
  79. '</div>',
  80. '<input type="hidden" class="J_btnload" />',
  81. '</div>',
  82. '</div>',
  83. '</div>'
  84. ].join(''),
  85. header: true,
  86. title: '',
  87. content: '',
  88. loading: false,
  89. footer: true,
  90. showFooter: true,
  91. innerPadding: true,
  92. btns: ['确定', '取消'],
  93. btnOne: false,
  94. nobgColor: false,
  95. loadFun: function() {},
  96. yes: function() {},
  97. other: function() {},
  98. cancel: function() {}
  99. }
  100. Dialog.prototype.init = function() {
  101. if (eval(this.options.btnNum) > 2) {
  102. this.$modal = $(this.options.template3).appendTo(this.$body);
  103. } else {
  104. this.$modal = $(this.options.template).appendTo(this.$body);
  105. }
  106. this.$dialog = this.$modal.find('.modal_dialog');
  107. this.$dialogBox = this.$modal.find('.modal_content');
  108. this.$dialogHeader = this.$modal.find('.modal_header');
  109. this.$dialogTitle = this.$modal.find('.modal_title');
  110. this.$dialogContent = this.$modal.find('.modal_body');
  111. this.$dialogFooter = this.$modal.find('.modal_footer');
  112. this.$dismissModal = this.$modal.find('.J_dismiss_modal');
  113. this.$dismissModalYes = this.$modal.find('.J_dismiss_modal_yes');
  114. this.$btnOperation = this.$modal.find('.res_add_but');
  115. this.$yesOperation = this.$modal.find('.J_btnyes');
  116. if (eval(this.options.btnNum) > 2) {
  117. this.$otherOperation = this.$modal.find('.J_btnother');
  118. }
  119. this.$cancelOperation = this.$modal.find('.J_btncancel');
  120. if (!this.options.innerPadding) {
  121. this.$dialogContent.addClass('no_pad');
  122. }
  123. if (!this.options.border) {
  124. this.$dialogBox.addClass('no_pad');
  125. }
  126. if (this.options.nobgColor) {
  127. this.$dialogBox.css('background', 'none');
  128. }
  129. if (this.options.header) {
  130. this.setTitle(this.options.title);
  131. } else {
  132. this.$dialogHeader.remove();
  133. }
  134. if (this.options.loading) {
  135. this.setContent('<div class="ajax_loading"><div class="ajaxloadtxt"></div></div>');
  136. } else {
  137. this.setContent(this.options.content);
  138. }
  139. if (this.options.footer) {
  140. this.setBtns(this.options.btns);
  141. } else {
  142. this.$dialogFooter.remove();
  143. }
  144. if (this.options.showFooter) {
  145. this.$dialogFooter.show();
  146. } else {
  147. this.$dialogFooter.hide();
  148. }
  149. this.operation();
  150. this.btnHover();
  151. this.move();
  152. this.escape();
  153. }
  154. Dialog.prototype.show = function () {
  155. var that = this;
  156. this.backdrop();
  157. this.init();
  158. this.options.loadFun();
  159. this.$dismissModal.on('click', function() {
  160. that.hide();
  161. });
  162. /*
  163. $(document).on('click','.J_dismiss_modal',function(){
  164. $(this).parents().find('.modal').hide();
  165. $(this).parents().find('.modal_backdrop').hide();
  166. //window.location.reload();
  167. });
  168. */
  169. this.$dismissModalYes.on('click', function() {
  170. if (that.$closeDiloag) {
  171. that.hide();
  172. }
  173. });
  174. }
  175. Dialog.prototype.setCloseDialog = function(switchVal) {
  176. eval(switchVal) ? this.$closeDiloag = true : this.$closeDiloag = false;
  177. }
  178. Dialog.prototype.hide = function() {
  179. this.$modal.remove();
  180. if (this.$backdrop) {
  181. this.$backdrop.remove();
  182. }
  183. }
  184. Dialog.prototype.hideDialog = function() {
  185. this.$modal.remove();
  186. }
  187. Dialog.prototype.escape = function() {
  188. var that = this;
  189. $(document).on('keydown', function(event) {
  190. if (event.keyCode == 27) {
  191. that.hide();
  192. }
  193. })
  194. }
  195. Dialog.prototype.move = function() {
  196. var newObj = this.$dialog;
  197. var newTit = this.$dialogHeader;
  198. newTit.mousedown(function(e) {
  199. var offset = newObj.offset();
  200. var x = e.pageX - offset.left;
  201. var y = e.pageY - offset.top;
  202. $(document).bind('mousemove', function(ev) {
  203. newObj.bind('selectstart', function() {
  204. return false;
  205. });
  206. var newx = ev.pageX - x;
  207. var newy = ev.pageY - y;
  208. newObj.css({
  209. 'left': newx + "px",
  210. 'top': newy + "px"
  211. });
  212. });
  213. });
  214. $(document).mouseup(function() {
  215. $(this).unbind("mousemove");
  216. })
  217. }
  218. Dialog.prototype.btnHover = function () {
  219. $(".J_hoverbut").hover(
  220. function() {
  221. $(this).addClass("hover");
  222. },
  223. function() {
  224. $(this).removeClass("hover");
  225. }
  226. );
  227. }
  228. Dialog.prototype.getDefaults = function() {
  229. return Dialog.DEFAULTS
  230. }
  231. Dialog.prototype.getOptions = function (options) {
  232. options = $.extend({}, this.getDefaults(), options)
  233. return options;
  234. }
  235. Dialog.prototype.backdrop = function () {
  236. var that = this;
  237. if (this.options.backdrop) {
  238. this.$backdrop = $(document.createElement('div'))
  239. .addClass('modal_backdrop fade')
  240. .appendTo(this.$body);
  241. /*this.$backdrop.on('click', function() {
  242. that.hide();
  243. });*/
  244. }
  245. }
  246. Dialog.prototype.setTitle = function (title) {
  247. this.$dialogTitle.html(title);
  248. }
  249. Dialog.prototype.setContent = function (content) {
  250. this.$dialogContent.html(content);
  251. this.setPosition();
  252. }
  253. Dialog.prototype.showFooter = function (switchVal) {
  254. if (eval(switchVal)) {
  255. this.$dialogFooter.show();
  256. } else {
  257. this.$dialogFooter.hide();
  258. }
  259. }
  260. Dialog.prototype.setInnerPadding = function (switchVal) {
  261. if (eval(switchVal)) {
  262. this.$dialogContent.removeClass('no_pad');
  263. } else {
  264. this.$dialogContent.addClass('no_pad');
  265. }
  266. }
  267. Dialog.prototype.setBtns = function (btnArr) {
  268. if (this.options.btnOne) {
  269. this.$btnOperation.addClass('btn-one');
  270. this.$cancelOperation.closest('.butlist').remove();
  271. this.$yesOperation.val(btnArr[0]);
  272. } else {
  273. if (eval(this.options.btnNum) > 2) {
  274. this.$yesOperation.val(btnArr[0]);
  275. this.$otherOperation.val(btnArr[1]);
  276. this.$cancelOperation.val(btnArr[2]);
  277. } else {
  278. this.$yesOperation.val(btnArr[0]);
  279. this.$cancelOperation.val(btnArr[1]);
  280. }
  281. }
  282. }
  283. Dialog.prototype.setBtnClass = function (classArr) {
  284. this.$yesOperation.removeClass('btn_100_38').addClass(classArr[0]);
  285. this.$cancelOperation.removeClass('btn_100_38').addClass(classArr[1]);
  286. }
  287. Dialog.prototype.operation = function (content) {
  288. this.$yesOperation.click(this.options.yes);
  289. if (eval(this.options.btnNum) > 2) {
  290. this.$otherOperation.click(this.options.other);
  291. }
  292. this.$cancelOperation.click(this.options.cancel);
  293. }
  294. Dialog.prototype.setPosition = function () {
  295. var that = this;
  296. this.$dialog.css({
  297. left: ($(window).width() - this.$dialog.outerWidth())/2,
  298. top: ($(window).height() - this.$dialog.outerHeight())/2 + $(document).scrollTop()
  299. });
  300. // 处理ie7下头部、底部不能自适应宽度
  301. function isIe(){
  302. return ("ActiveXObject" in window);
  303. }
  304. if (isIe() && window.XMLHttpRequest && document.documentMode == 7) {
  305. this.$dialogHeader.css('width', this.$dialog.outerWidth()-15);
  306. this.$dialogFooter.css('width', this.$dialog.outerWidth()-190);
  307. }
  308. if (this.$ie) {
  309. if (window.PIE) {
  310. $('.pie_about').each(function() {
  311. PIE.attach(this);
  312. });
  313. }
  314. };
  315. if (this.options.backdrop) {
  316. this.$backdrop.addClass('in');
  317. };
  318. // this.$modal.addClass('in');
  319. }
  320. function Plugin(option) {
  321. return new Dialog(this, option);
  322. }
  323. $.fn.dialog = Plugin
  324. $.fn.dialog.Constructor = Dialog
  325. $.fn.dialog.noConflict = function() {
  326. $.fn.dialog = old
  327. return this
  328. }
  329. }(jQuery);