汇联通执法队后台管理系统
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.

jquery-confirm.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*!
  2. * jquery-confirm v2.5.1 (http://craftpip.github.io/jquery-confirm/)
  3. * Author: Boniface Pereira
  4. * Website: www.craftpip.com
  5. * Contact: hey@craftpip.com
  6. *
  7. * Copyright 2013-2015 jquery-confirm
  8. * Licensed under MIT (https://github.com/craftpip/jquery-confirm/blob/master/LICENSE)
  9. */
  10. if (typeof jQuery === 'undefined') {
  11. throw new Error('jquery-confirm requires jQuery');
  12. }
  13. var jconfirm, Jconfirm;
  14. (function ($) {
  15. "use strict";
  16. $.fn.confirm = function (options, option2) {
  17. if (typeof options === 'undefined') options = {};
  18. if (typeof options === 'string')
  19. options = {
  20. content: options,
  21. title: (option2) ? option2 : false
  22. };
  23. /*
  24. * Alias of $.confirm to emulate native confirm()
  25. */
  26. $(this).each(function () {
  27. var $this = $(this);
  28. $this.on('click', function (e) {
  29. e.preventDefault();
  30. var jcOption = $.extend({}, options);
  31. if ($this.attr('data-title'))
  32. jcOption['title'] = $this.attr('data-title');
  33. if ($this.attr('data-content'))
  34. jcOption['content'] = $this.attr('data-content');
  35. jcOption['$target'] = $this;
  36. if ($this.attr('href') && !options['confirm'])
  37. jcOption['confirm'] = function () {
  38. location.href = $this.attr('href');
  39. };
  40. $.confirm(jcOption);
  41. });
  42. });
  43. return $(this);
  44. };
  45. $.confirm = function (options, option2) {
  46. if (typeof options === 'undefined') options = {};
  47. if (typeof options === 'string')
  48. options = {
  49. content: options,
  50. title: (option2) ? option2 : false
  51. };
  52. /*
  53. * Alias of jconfirm
  54. */
  55. return jconfirm(options);
  56. };
  57. $.alert = function (options, option2) {
  58. if (typeof options === 'undefined') options = {};
  59. if (typeof options === 'string')
  60. options = {
  61. content: options,
  62. title: (option2) ? option2 : false
  63. };
  64. /*
  65. * Alias of jconfirm
  66. */
  67. options.cancelButton = false;
  68. return jconfirm(options);
  69. };
  70. $.dialog = function (options, option2) {
  71. if (typeof options === 'undefined') options = {};
  72. if (typeof options === 'string')
  73. options = {
  74. content: options,
  75. title: (option2) ? option2 : false
  76. };
  77. /*
  78. * Alias of jconfirm
  79. */
  80. options.cancelButton = false;
  81. options.confirmButton = false;
  82. options.confirmKeys = [13];
  83. return jconfirm(options);
  84. };
  85. jconfirm = function (options) {
  86. if (typeof options === 'undefined') options = {};
  87. /*
  88. * initial function for calling.
  89. */
  90. if (jconfirm.defaults) {
  91. /*
  92. * Merge global defaults with plugin defaults
  93. */
  94. $.extend(jconfirm.pluginDefaults, jconfirm.defaults);
  95. }
  96. /*
  97. * merge options with plugin defaults.
  98. */
  99. var options = $.extend({}, jconfirm.pluginDefaults, options);
  100. return new Jconfirm(options);
  101. };
  102. Jconfirm = function (options) {
  103. /*
  104. * constructor function Jconfirm,
  105. * options = user options.
  106. */
  107. $.extend(this, options);
  108. this._init();
  109. };
  110. Jconfirm.prototype = {
  111. _init: function () {
  112. var that = this;
  113. this._rand = Math.round(Math.random() * 99999);
  114. this._buildHTML();
  115. this._bindEvents();
  116. setTimeout(function () {
  117. that.open();
  118. that._watchContent();
  119. }, 0);
  120. },
  121. _buildHTML: function () {
  122. var that = this;
  123. /*
  124. * Prefixing animations.
  125. */
  126. this.animation = 'anim-' + this.animation.toLowerCase();
  127. this.closeAnimation = 'anim-' + this.closeAnimation.toLowerCase();
  128. this.theme = 'jconfirm-' + this.theme.toLowerCase();
  129. if (this.animation == 'anim-none')
  130. this.animationSpeed = 0;
  131. this._lastFocused = $('body').find(':focus');
  132. /*
  133. * Append html.
  134. */
  135. this.$el = $(this.template).appendTo(this.container).addClass(this.theme);
  136. this.$el.find('.jconfirm-box-container').addClass(this.columnClass);
  137. this.$el.find('.jconfirm-bg').css(this._getCSS(this.animationSpeed, 1));
  138. this.$el.find('.jconfirm-bg').css('opacity', this.opacity);
  139. this.$b = this.$el.find('.jconfirm-box').css(this._getCSS(this.animationSpeed, this.animationBounce)).addClass(this.animation);
  140. this.$body = this.$b; // alias
  141. /*
  142. * Add rtl class if rtl option has selected
  143. */
  144. if (this.rtl)
  145. this.$el.addClass("rtl");
  146. this._contentReady = $.Deferred();
  147. this._modalReady = $.Deferred();
  148. /*
  149. * Setup title contents
  150. */
  151. this.$title = this.$el.find('.title');
  152. this.contentDiv = this.$el.find('div.content');
  153. this.$content = this.contentDiv; // alias
  154. this.$contentPane = this.$el.find('.content-pane');
  155. this.$icon = this.$el.find('.icon-c');
  156. this.$closeIcon = this.$el.find('.closeIcon');
  157. this.$contentPane.css(this._getCSS(this.animationSpeed, 1));
  158. this.setTitle();
  159. this.setIcon();
  160. this._setButtons();
  161. if (this.closeIconClass)
  162. this.$closeIcon.html('<i class="' + this.closeIconClass + '"></i>');
  163. that._contentHash = this._hash(that.$content.html());
  164. $.when(this._contentReady, this._modalReady).then(function () {
  165. that.setContent();
  166. that.setTitle();
  167. that.setIcon();
  168. });
  169. this._getContent();
  170. this._imagesLoaded();
  171. if (this.autoClose)
  172. this._startCountDown();
  173. },
  174. _unwatchContent: function () {
  175. clearInterval(this._timer);
  176. },
  177. _hash: function () {
  178. return $.Taiji.base64.decode((encodeURIComponent(this.$content.html())));
  179. },
  180. _watchContent: function () {
  181. var that = this;
  182. this._timer = setInterval(function () {
  183. var now = that._hash(that.$content.html());
  184. if (that._contentHash != now) {
  185. that._contentHash = now;
  186. that.setDialogCenter();
  187. that._imagesLoaded();
  188. }
  189. }, this.watchInterval);
  190. },
  191. _bindEvents: function () {
  192. var that = this;
  193. var boxClicked = false;
  194. this.$el.find('.jconfirm-scrollpane').click(function (e) {
  195. // ignore propagated clicks
  196. if (!boxClicked) {
  197. // background clicked
  198. if (that.backgroundDismiss) {
  199. that.cancel();
  200. that.close();
  201. } else {
  202. that.$b.addClass('hilight');
  203. setTimeout(function () {
  204. that.$b.removeClass('hilight');
  205. }, 800);
  206. }
  207. }
  208. boxClicked = false;
  209. });
  210. this.$el.find('.jconfirm-box').click(function (e) {
  211. boxClicked = true;
  212. });
  213. if (this.$confirmButton) {
  214. this.$confirmButton.click(function (e) {
  215. e.preventDefault();
  216. var r = that.confirm(that.$b);
  217. that._stopCountDown();
  218. that.onAction('confirm');
  219. if (typeof r === 'undefined' || r)
  220. that.close();
  221. });
  222. }
  223. if (this.$cancelButton) {
  224. this.$cancelButton.click(function (e) {
  225. e.preventDefault();
  226. var r = that.cancel(that.$b);
  227. that._stopCountDown();
  228. that.onAction('cancel');
  229. if (typeof r === 'undefined' || r)
  230. that.close();
  231. });
  232. }
  233. if (this.$closeButton) {
  234. this.$closeButton.click(function (e) {
  235. e.preventDefault();
  236. that._stopCountDown();
  237. that.cancel();
  238. that.onAction('close');
  239. that.close();
  240. });
  241. }
  242. if (this.keyboardEnabled) {
  243. setTimeout(function () {
  244. $(window).on('keyup.' + this._rand, function (e) {
  245. that.reactOnKey(e);
  246. });
  247. }, 500);
  248. }
  249. $(window).on('resize.' + this._rand, function () {
  250. that.setDialogCenter(true);
  251. });
  252. },
  253. _getCSS: function (speed, bounce) {
  254. return {
  255. '-webkit-transition-duration': speed / 1000 + 's',
  256. 'transition-duration': speed / 1000 + 's',
  257. '-webkit-transition-timing-function': 'cubic-bezier(.36,1.1,.2, ' + bounce + ')',
  258. 'transition-timing-function': 'cubic-bezier(.36,1.1,.2, ' + bounce + ')'
  259. };
  260. },
  261. _imagesLoaded: function () {
  262. var that = this;
  263. $.each(this.$content.find('img:not(.loaded)'), function (i, a) {
  264. var interval = setInterval(function () {
  265. var h = $(a).css('height');
  266. if (h !== '0px') {
  267. $(a).addClass('loaded');
  268. that.setDialogCenter();
  269. clearInterval(interval);
  270. }
  271. }, 40);
  272. })
  273. },
  274. _setButtons: function () {
  275. /*
  276. * Settings up buttons
  277. */
  278. this.$btnc = this.$el.find('.buttons');
  279. if (this.confirmButton && $.trim(this.confirmButton) !== '') {
  280. this.$confirmButton = $('<button type="button" class="btn">' + this.confirmButton + '</button>').appendTo(this.$btnc).addClass(this.confirmButtonClass);
  281. }
  282. if (this.cancelButton && $.trim(this.cancelButton) !== '') {
  283. this.$cancelButton = $('<button type="button" class="btn">' + this.cancelButton + '</button>').appendTo(this.$btnc).addClass(this.cancelButtonClass);
  284. }
  285. if (!this.confirmButton && !this.cancelButton) {
  286. this.$btnc.hide();
  287. }
  288. if (!this.confirmButton && !this.cancelButton && this.closeIcon === null) {
  289. this.$closeButton = this.$b.find('.closeIcon').show();
  290. }
  291. if (this.closeIcon === true) {
  292. this.$closeButton = this.$b.find('.closeIcon').show();
  293. }
  294. },
  295. setTitle: function (string) {
  296. this.title = (typeof string !== 'undefined') ? string : this.title;
  297. this.$title.html(this.title || '');
  298. },
  299. setIcon: function (iconClass) {
  300. this.title = (typeof string !== 'undefined') ? iconClass : this.title;
  301. this.$icon.html(this.icon ? '<i class="' + this.icon + '"></i>' : '');
  302. },
  303. setContent: function (string) {
  304. // only set the content on the modal.
  305. var that = this;
  306. this.content = (typeof string == 'undefined') ? this.content : string;
  307. if (this.content == '') {
  308. this.$content.html(this.content);
  309. this.$contentPane.hide();
  310. } else {
  311. this.$content.html(this.content);
  312. this.$contentPane.show();
  313. }
  314. if (this.$content.hasClass('loading')) {
  315. this.$content.removeClass('loading');// it was loading via ajax.
  316. this.$btnc.find('button').prop('disabled', false);
  317. }
  318. },
  319. _getContent: function (string) {
  320. // get content from remote & stuff.
  321. var that = this;
  322. string = (string) ? string : this.content;
  323. this._isAjax = false;
  324. /*
  325. * Set content.
  326. */
  327. if (!this.content) { // if the content is falsy
  328. this.content = '';
  329. this.setContent(this.content);
  330. this._contentReady.reject();
  331. } else if (typeof this.content === 'string') {
  332. if (this.content.substr(0, 4).toLowerCase() === 'url:') {
  333. this._isAjax = true;
  334. this.$content.addClass('loading');
  335. this.$btnc.find('button').prop('disabled', true);
  336. var url = this.content.substring(4, this.content.length);
  337. $.get(url).done(function (html) {
  338. that.content = html;
  339. that._contentReady.resolve();
  340. }).always(function (data, status, xhr) {
  341. if (typeof that.contentLoaded === 'function')
  342. that.contentLoaded(data, status, xhr);
  343. });
  344. } else {
  345. this.setContent(this.content);
  346. this._contentReady.reject();
  347. }
  348. } else if (typeof this.content === 'function') {
  349. this.$content.addClass('loading');
  350. this.$btnc.find('button').attr('disabled', 'disabled');
  351. var promise = this.content(this);
  352. if (typeof promise !== 'object') {
  353. console.error('The content function must return jquery promise.');
  354. } else if (typeof promise.always !== 'function') {
  355. console.error('The object returned is not a jquery promise.');
  356. } else {
  357. this._isAjax = true;
  358. promise.always(function (data, status) {
  359. that._contentReady.resolve();
  360. });
  361. }
  362. } else {
  363. console.error('Invalid option for property content, passed: ' + typeof this.content);
  364. }
  365. this.setDialogCenter();
  366. },
  367. _stopCountDown: function () {
  368. clearInterval(this.timerInterval);
  369. if (this.$cd)
  370. this.$cd.remove();
  371. },
  372. _startCountDown: function () {
  373. var opt = this.autoClose.split('|');
  374. if (/cancel/.test(opt[0]) && this.type === 'alert') {
  375. return false;
  376. } else if (/confirm|cancel/.test(opt[0])) {
  377. this.$cd = $('<span class="countdown">').appendTo(this['$' + opt[0] + 'Button']);
  378. var that = this;
  379. that.$cd.parent().click();
  380. var time = opt[1] / 1000;
  381. this.timerInterval = setInterval(function () {
  382. that.$cd.html(' (' + (time -= 1) + ')');
  383. if (time === 0) {
  384. that.$cd.html('');
  385. that.$cd.parent().trigger('click');
  386. clearInterval(that.timerInterval);
  387. }
  388. }, 1000);
  389. } else {
  390. console.error('Invalid option ' + opt[0] + ', must be confirm/cancel');
  391. }
  392. },
  393. reactOnKey: function key(e) {
  394. /*
  395. * prevent keyup event if the dialog is not last!
  396. */
  397. var a = $('.jconfirm');
  398. if (a.eq(a.length - 1)[0] !== this.$el[0])
  399. return false;
  400. var key = e.which;
  401. // Do not react if Enter/Space is pressed on input elements
  402. if (this.contentDiv.find(':input').is(':focus') && /13|32/.test(key))
  403. return false;
  404. if ($.inArray(key, this.cancelKeys) !== -1) {
  405. /*
  406. * Cancel key pressed.
  407. */
  408. if (this.$cancelButton) {
  409. this.$cancelButton.click();
  410. } else {
  411. this.close();
  412. }
  413. }
  414. if ($.inArray(key, this.confirmKeys) !== -1) {
  415. /*
  416. * Confirm key pressed.
  417. */
  418. if (this.$confirmButton) {
  419. this.$confirmButton.click();
  420. }
  421. }
  422. },
  423. setDialogCenter: function () {
  424. if (this.$contentPane.css('display') == 'none') {
  425. var contentHeight = 0;
  426. var paneHeight = 0;
  427. } else {
  428. var contentHeight = this.$content.outerHeight();
  429. var paneHeight = this.$contentPane.height();
  430. if (paneHeight == 0)
  431. paneHeight = contentHeight;
  432. }
  433. var off = 100;
  434. var w = this.$content.outerWidth();
  435. //var s = '-clip-path: inset(0px 0px '+contentHeight+'px 0px);' +
  436. // 'clip-path: inset(0px 0px '+contentHeight+'px 0px)';
  437. this.$content.css({
  438. 'clip': 'rect(0px ' + (off + w) + 'px ' + contentHeight + 'px -' + off + 'px)'
  439. });
  440. this.$contentPane.css({
  441. 'height': contentHeight
  442. });
  443. var windowHeight = $(window).height();
  444. var boxHeight = this.$b.outerHeight() - paneHeight + contentHeight;
  445. var topMargin = (windowHeight - boxHeight) / 2;
  446. var minMargin = 100;
  447. if (boxHeight > (windowHeight - minMargin)) {
  448. var style = {
  449. 'margin-top': minMargin / 2,
  450. 'margin-bottom': minMargin / 2
  451. }
  452. $('body').addClass('jconfirm-noscroll');
  453. } else {
  454. var style = {
  455. 'margin-top': topMargin
  456. }
  457. $('body').removeClass('jconfirm-noscroll');
  458. }
  459. this.$b.css(style);
  460. },
  461. close: function () {
  462. var that = this;
  463. if (this.isClosed())
  464. return false;
  465. if (typeof this.onClose === 'function')
  466. this.onClose();
  467. this._unwatchContent();
  468. that._lastFocused.focus();
  469. //this.observer.disconnect();
  470. /*
  471. unbind the window resize & keyup event.
  472. */
  473. $(window).unbind('resize.' + this._rand);
  474. if (this.keyboardEnabled)
  475. $(window).unbind('keyup.' + this._rand);
  476. that.$el.find('.jconfirm-bg').removeClass('seen');
  477. $('body').removeClass('jconfirm-noscroll');
  478. this.$b.addClass(this.closeAnimation);
  479. var closeTimer = (this.closeAnimation == 'anim-none') ? 0 : this.animationSpeed;
  480. setTimeout(function () {
  481. that.$el.remove();
  482. }, closeTimer * 25 / 100);
  483. jconfirm.record.closed += 1;
  484. jconfirm.record.currentlyOpen -= 1;
  485. return true;
  486. },
  487. open: function () {
  488. var that = this;
  489. if (this.isClosed())
  490. return false;
  491. that.$el.find('.jconfirm-bg').addClass('seen');
  492. this.$b.removeClass(this.animation);
  493. this.$b.find('input[autofocus]:visible:first').focus();
  494. jconfirm.record.opened += 1;
  495. jconfirm.record.currentlyOpen += 1;
  496. if (typeof this.onOpen === 'function')
  497. this.onOpen();
  498. var jcr = 'jconfirm-box' + this._rand;
  499. this.$b.attr('aria-labelledby', jcr).attr('tabindex', -1).focus();
  500. if (this.$title)
  501. this.$title.attr('id', jcr); else if (this.$content)
  502. this.$content.attr('id', jcr);
  503. setTimeout(function () {
  504. that.$b.css({
  505. 'transition-property': that.$b.css('transition-property') + ', margin'
  506. });
  507. that._modalReady.resolve();
  508. }, this.animationSpeed);
  509. return true;
  510. },
  511. isClosed: function () {
  512. return this.$el.css('display') === '';
  513. }
  514. };
  515. jconfirm.pluginDefaults = {
  516. template: '<div class="jconfirm"><div class="jconfirm-bg"></div><div class="jconfirm-scrollpane"><div class="container"><div class="row"><div class="jconfirm-box-container"><div class="jconfirm-box" role="dialog" aria-labelledby="labelled" tabindex="-1"><div class="closeIcon">&times;</div><div class="title-c"><span class="icon-c"></span><span class="title"></span></div><div class="content-pane"><div class="content"></div></div><div class="buttons"></div><div class="jquery-clear"></div></div></div></div></div></div></div>',
  517. title: 'Hello',
  518. content: 'Are you sure to continue?',
  519. contentLoaded: function () {
  520. },
  521. icon: '',
  522. opacity: 0.2,
  523. confirmButton: 'Okay',
  524. cancelButton: 'Close',
  525. confirmButtonClass: 'btn-default',
  526. cancelButtonClass: 'btn-default',
  527. theme: 'white',
  528. animation: 'zoom',
  529. closeAnimation: 'scale',
  530. animationSpeed: 500,
  531. animationBounce: 1.2,
  532. keyboardEnabled: false,
  533. rtl: false,
  534. confirmKeys: [13], // ENTER key
  535. cancelKeys: [27], // ESC key
  536. container: 'body',
  537. confirm: function () {
  538. },
  539. cancel: function () {
  540. },
  541. backgroundDismiss: false,
  542. autoClose: false,
  543. closeIcon: null,
  544. closeIconClass: false,
  545. watchInterval: 100,
  546. columnClass: 'col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1',
  547. onOpen: function () {
  548. },
  549. onClose: function () {
  550. },
  551. onAction: function () {
  552. }
  553. };
  554. jconfirm.record = {
  555. opened: 0,
  556. closed: 0,
  557. currentlyOpen: 0
  558. };
  559. })(jQuery);