jquery-confirm.min.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*!
  2. * jquery-confirm v3.3.4 (http://craftpip.github.io/jquery-confirm/)
  3. * Author: Boniface Pereira
  4. * Website: www.craftpip.com
  5. * Contact: hey@craftpip.com
  6. *
  7. * Copyright 2013-2019 jquery-confirm
  8. * Licensed under MIT (https://github.com/craftpip/jquery-confirm/blob/master/LICENSE)
  9. */
  10. (function (factory) {
  11. if (typeof define === "function" && define.amd) {
  12. define(["jquery"], factory);
  13. } else {
  14. if (typeof module === "object" && module.exports) {
  15. module.exports = function (root, jQuery) {
  16. if (jQuery === undefined) {
  17. if (typeof window !== "undefined") {
  18. jQuery = require("jquery");
  19. } else {
  20. jQuery = require("jquery")(root);
  21. }
  22. }
  23. factory(jQuery);
  24. return jQuery;
  25. };
  26. } else {
  27. factory(jQuery);
  28. }
  29. }
  30. })(function ($) {
  31. var w = window;
  32. $.fn.confirm = function (options, option2) {
  33. if (typeof options === "undefined") {
  34. options = {};
  35. }
  36. if (typeof options === "string") {
  37. options = { content: options, title: option2 ? option2 : false };
  38. }
  39. $(this).each(function () {
  40. var $this = $(this);
  41. if ($this.attr("jc-attached")) {
  42. console.warn("jConfirm has already been attached to this element ", $this[0]);
  43. return;
  44. }
  45. $this.on("click", function (e) {
  46. e.preventDefault();
  47. var jcOption = $.extend({}, options);
  48. if ($this.attr("data-title")) {
  49. jcOption.title = $this.attr("data-title");
  50. }
  51. if ($this.attr("data-content")) {
  52. jcOption.content = $this.attr("data-content");
  53. }
  54. if (typeof jcOption.buttons === "undefined") {
  55. jcOption.buttons = {};
  56. }
  57. jcOption["$target"] = $this;
  58. if ($this.attr("href") && Object.keys(jcOption.buttons).length === 0) {
  59. var buttons = $.extend(true, {}, w.jconfirm.pluginDefaults.defaultButtons, (w.jconfirm.defaults || {}).defaultButtons || {});
  60. var firstBtn = Object.keys(buttons)[0];
  61. jcOption.buttons = buttons;
  62. jcOption.buttons[firstBtn].action = function () {
  63. location.href = $this.attr("href");
  64. };
  65. }
  66. jcOption.closeIcon = false;
  67. var instance = $.confirm(jcOption);
  68. });
  69. $this.attr("jc-attached", true);
  70. });
  71. return $(this);
  72. };
  73. $.confirm = function (options, option2) {
  74. if (typeof options === "undefined") {
  75. options = {};
  76. }
  77. if (typeof options === "string") {
  78. options = { content: options, title: option2 ? option2 : false };
  79. }
  80. var putDefaultButtons = !(options.buttons === false);
  81. if (typeof options.buttons !== "object") {
  82. options.buttons = {};
  83. }
  84. if (Object.keys(options.buttons).length === 0 && putDefaultButtons) {
  85. var buttons = $.extend(true, {}, w.jconfirm.pluginDefaults.defaultButtons, (w.jconfirm.defaults || {}).defaultButtons || {});
  86. options.buttons = buttons;
  87. }
  88. return w.jconfirm(options);
  89. };
  90. $.alert = function (options, option2) {
  91. if (typeof options === "undefined") {
  92. options = {};
  93. }
  94. if (typeof options === "string") {
  95. options = { content: options, title: option2 ? option2 : false };
  96. }
  97. var putDefaultButtons = !(options.buttons === false);
  98. if (typeof options.buttons !== "object") {
  99. options.buttons = {};
  100. }
  101. if (Object.keys(options.buttons).length === 0 && putDefaultButtons) {
  102. var buttons = $.extend(true, {}, w.jconfirm.pluginDefaults.defaultButtons, (w.jconfirm.defaults || {}).defaultButtons || {});
  103. var firstBtn = Object.keys(buttons)[0];
  104. options.buttons[firstBtn] = buttons[firstBtn];
  105. }
  106. return w.jconfirm(options);
  107. };
  108. $.dialog = function (options, option2) {
  109. if (typeof options === "undefined") {
  110. options = {};
  111. }
  112. if (typeof options === "string") {
  113. options = { content: options, title: option2 ? option2 : false, closeIcon: function () {} };
  114. }
  115. options.buttons = {};
  116. if (typeof options.closeIcon === "undefined") {
  117. options.closeIcon = function () {};
  118. }
  119. options.confirmKeys = [13];
  120. return w.jconfirm(options);
  121. };
  122. w.jconfirm = function (options) {
  123. if (typeof options === "undefined") {
  124. options = {};
  125. }
  126. var pluginOptions = $.extend(true, {}, w.jconfirm.pluginDefaults);
  127. if (w.jconfirm.defaults) {
  128. pluginOptions = $.extend(true, pluginOptions, w.jconfirm.defaults);
  129. }
  130. pluginOptions = $.extend(true, {}, pluginOptions, options);
  131. var instance = new w.Jconfirm(pluginOptions);
  132. w.jconfirm.instances.push(instance);
  133. return instance;
  134. };
  135. w.Jconfirm = function (options) {
  136. $.extend(this, options);
  137. this._init();
  138. };
  139. w.Jconfirm.prototype = {
  140. _init: function () {
  141. var that = this;
  142. if (!w.jconfirm.instances.length) {
  143. w.jconfirm.lastFocused = $("body").find(":focus");
  144. }
  145. this._id = Math.round(Math.random() * 99999);
  146. this.contentParsed = $(document.createElement("div"));
  147. if (!this.lazyOpen) {
  148. setTimeout(function () {
  149. that.open();
  150. }, 0);
  151. }
  152. },
  153. _buildHTML: function () {
  154. var that = this;
  155. this._parseAnimation(this.animation, "o");
  156. this._parseAnimation(this.closeAnimation, "c");
  157. this._parseBgDismissAnimation(this.backgroundDismissAnimation);
  158. this._parseColumnClass(this.columnClass);
  159. this._parseTheme(this.theme);
  160. this._parseType(this.type);
  161. var template = $(this.template);
  162. template.find(".jconfirm-box").addClass(this.animationParsed).addClass(this.backgroundDismissAnimationParsed).addClass(this.typeParsed);
  163. if (this.typeAnimated) {
  164. template.find(".jconfirm-box").addClass("jconfirm-type-animated");
  165. }
  166. if (this.useBootstrap) {
  167. template.find(".jc-bs3-row").addClass(this.bootstrapClasses.row);
  168. template.find(".jc-bs3-row").addClass("justify-content-md-center justify-content-sm-center justify-content-xs-center justify-content-lg-center");
  169. template.find(".jconfirm-box-container").addClass(this.columnClassParsed);
  170. if (this.containerFluid) {
  171. template.find(".jc-bs3-container").addClass(this.bootstrapClasses.containerFluid);
  172. } else {
  173. template.find(".jc-bs3-container").addClass(this.bootstrapClasses.container);
  174. }
  175. } else {
  176. template.find(".jconfirm-box").css("width", this.boxWidth);
  177. }
  178. if (this.titleClass) {
  179. template.find(".jconfirm-title-c").addClass(this.titleClass);
  180. }
  181. template.addClass(this.themeParsed);
  182. var ariaLabel = "jconfirm-box" + this._id;
  183. template.find(".jconfirm-box").attr("aria-labelledby", ariaLabel).attr("tabindex", -1);
  184. template.find(".jconfirm-content").attr("id", ariaLabel);
  185. if (this.bgOpacity !== null) {
  186. template.find(".jconfirm-bg").css("opacity", this.bgOpacity);
  187. }
  188. if (this.rtl) {
  189. template.addClass("jconfirm-rtl");
  190. }
  191. this.$el = template.appendTo(this.container);
  192. this.$jconfirmBoxContainer = this.$el.find(".jconfirm-box-container");
  193. this.$jconfirmBox = this.$body = this.$el.find(".jconfirm-box");
  194. this.$jconfirmBg = this.$el.find(".jconfirm-bg");
  195. this.$title = this.$el.find(".jconfirm-title");
  196. this.$titleContainer = this.$el.find(".jconfirm-title-c");
  197. this.$content = this.$el.find("div.jconfirm-content");
  198. this.$contentPane = this.$el.find(".jconfirm-content-pane");
  199. this.$icon = this.$el.find(".jconfirm-icon-c");
  200. this.$closeIcon = this.$el.find(".jconfirm-closeIcon");
  201. this.$holder = this.$el.find(".jconfirm-holder");
  202. this.$btnc = this.$el.find(".jconfirm-buttons");
  203. this.$scrollPane = this.$el.find(".jconfirm-scrollpane");
  204. that.setStartingPoint();
  205. this._contentReady = $.Deferred();
  206. this._modalReady = $.Deferred();
  207. this.$holder.css({ "padding-top": this.offsetTop, "padding-bottom": this.offsetBottom });
  208. this.setTitle();
  209. this.setIcon();
  210. this._setButtons();
  211. this._parseContent();
  212. this.initDraggable();
  213. if (this.isAjax) {
  214. this.showLoading(false);
  215. }
  216. $.when(this._contentReady, this._modalReady)
  217. .then(function () {
  218. if (that.isAjaxLoading) {
  219. setTimeout(function () {
  220. that.isAjaxLoading = false;
  221. that.setContent();
  222. that.setTitle();
  223. that.setIcon();
  224. setTimeout(function () {
  225. that.hideLoading(false);
  226. that._updateContentMaxHeight();
  227. }, 100);
  228. if (typeof that.onContentReady === "function") {
  229. that.onContentReady();
  230. }
  231. }, 50);
  232. } else {
  233. that._updateContentMaxHeight();
  234. that.setTitle();
  235. that.setIcon();
  236. if (typeof that.onContentReady === "function") {
  237. that.onContentReady();
  238. }
  239. }
  240. if (that.autoClose) {
  241. that._startCountDown();
  242. }
  243. })
  244. .then(function () {
  245. that._watchContent();
  246. });
  247. if (this.animation === "none") {
  248. this.animationSpeed = 1;
  249. this.animationBounce = 1;
  250. }
  251. this.$body.css(this._getCSS(this.animationSpeed, this.animationBounce));
  252. this.$contentPane.css(this._getCSS(this.animationSpeed, 1));
  253. this.$jconfirmBg.css(this._getCSS(this.animationSpeed, 1));
  254. this.$jconfirmBoxContainer.css(this._getCSS(this.animationSpeed, 1));
  255. },
  256. _typePrefix: "jconfirm-type-",
  257. typeParsed: "",
  258. _parseType: function (type) {
  259. this.typeParsed = this._typePrefix + type;
  260. },
  261. setType: function (type) {
  262. var oldClass = this.typeParsed;
  263. this._parseType(type);
  264. this.$jconfirmBox.removeClass(oldClass).addClass(this.typeParsed);
  265. },
  266. themeParsed: "",
  267. _themePrefix: "jconfirm-",
  268. setTheme: function (theme) {
  269. var previous = this.theme;
  270. this.theme = theme || this.theme;
  271. this._parseTheme(this.theme);
  272. if (previous) {
  273. this.$el.removeClass(previous);
  274. }
  275. this.$el.addClass(this.themeParsed);
  276. this.theme = theme;
  277. },
  278. _parseTheme: function (theme) {
  279. var that = this;
  280. theme = theme.split(",");
  281. $.each(theme, function (k, a) {
  282. if (a.indexOf(that._themePrefix) === -1) {
  283. theme[k] = that._themePrefix + $.trim(a);
  284. }
  285. });
  286. this.themeParsed = theme.join(" ").toLowerCase();
  287. },
  288. backgroundDismissAnimationParsed: "",
  289. _bgDismissPrefix: "jconfirm-hilight-",
  290. _parseBgDismissAnimation: function (bgDismissAnimation) {
  291. var animation = bgDismissAnimation.split(",");
  292. var that = this;
  293. $.each(animation, function (k, a) {
  294. if (a.indexOf(that._bgDismissPrefix) === -1) {
  295. animation[k] = that._bgDismissPrefix + $.trim(a);
  296. }
  297. });
  298. this.backgroundDismissAnimationParsed = animation.join(" ").toLowerCase();
  299. },
  300. animationParsed: "",
  301. closeAnimationParsed: "",
  302. _animationPrefix: "jconfirm-animation-",
  303. setAnimation: function (animation) {
  304. this.animation = animation || this.animation;
  305. this._parseAnimation(this.animation, "o");
  306. },
  307. _parseAnimation: function (animation, which) {
  308. which = which || "o";
  309. var animations = animation.split(",");
  310. var that = this;
  311. $.each(animations, function (k, a) {
  312. if (a.indexOf(that._animationPrefix) === -1) {
  313. animations[k] = that._animationPrefix + $.trim(a);
  314. }
  315. });
  316. var a_string = animations.join(" ").toLowerCase();
  317. if (which === "o") {
  318. this.animationParsed = a_string;
  319. } else {
  320. this.closeAnimationParsed = a_string;
  321. }
  322. return a_string;
  323. },
  324. setCloseAnimation: function (closeAnimation) {
  325. this.closeAnimation = closeAnimation || this.closeAnimation;
  326. this._parseAnimation(this.closeAnimation, "c");
  327. },
  328. setAnimationSpeed: function (speed) {
  329. this.animationSpeed = speed || this.animationSpeed;
  330. },
  331. columnClassParsed: "",
  332. setColumnClass: function (colClass) {
  333. if (!this.useBootstrap) {
  334. console.warn("cannot set columnClass, useBootstrap is set to false");
  335. return;
  336. }
  337. this.columnClass = colClass || this.columnClass;
  338. this._parseColumnClass(this.columnClass);
  339. this.$jconfirmBoxContainer.addClass(this.columnClassParsed);
  340. },
  341. _updateContentMaxHeight: function () {
  342. var height = $(window).height() - (this.$jconfirmBox.outerHeight() - this.$contentPane.outerHeight()) - (this.offsetTop + this.offsetBottom);
  343. this.$contentPane.css({ "max-height": height + "px" });
  344. },
  345. setBoxWidth: function (width) {
  346. if (this.useBootstrap) {
  347. console.warn("cannot set boxWidth, useBootstrap is set to true");
  348. return;
  349. }
  350. this.boxWidth = width;
  351. this.$jconfirmBox.css("width", width);
  352. },
  353. _parseColumnClass: function (colClass) {
  354. colClass = colClass.toLowerCase();
  355. var p;
  356. switch (colClass) {
  357. case "xl":
  358. case "xlarge":
  359. p = "col-md-12";
  360. break;
  361. case "l":
  362. case "large":
  363. p = "col-md-8 col-md-offset-2";
  364. break;
  365. case "m":
  366. case "medium":
  367. p = "col-md-6 col-md-offset-3";
  368. break;
  369. case "s":
  370. case "small":
  371. p = "col-md-4 col-md-offset-4";
  372. break;
  373. case "xs":
  374. case "xsmall":
  375. p = "col-md-2 col-md-offset-5";
  376. break;
  377. default:
  378. p = colClass;
  379. }
  380. this.columnClassParsed = p;
  381. },
  382. initDraggable: function () {
  383. var that = this;
  384. var $t = this.$titleContainer;
  385. this.resetDrag();
  386. if (this.draggable) {
  387. $t.on("mousedown", function (e) {
  388. $t.addClass("jconfirm-hand");
  389. that.mouseX = e.clientX;
  390. that.mouseY = e.clientY;
  391. that.isDrag = true;
  392. });
  393. $(window).on("mousemove." + this._id, function (e) {
  394. if (that.isDrag) {
  395. that.movingX = e.clientX - that.mouseX + that.initialX;
  396. that.movingY = e.clientY - that.mouseY + that.initialY;
  397. that.setDrag();
  398. }
  399. });
  400. $(window).on("mouseup." + this._id, function () {
  401. $t.removeClass("jconfirm-hand");
  402. if (that.isDrag) {
  403. that.isDrag = false;
  404. that.initialX = that.movingX;
  405. that.initialY = that.movingY;
  406. }
  407. });
  408. }
  409. },
  410. resetDrag: function () {
  411. this.isDrag = false;
  412. this.initialX = 0;
  413. this.initialY = 0;
  414. this.movingX = 0;
  415. this.movingY = 0;
  416. this.mouseX = 0;
  417. this.mouseY = 0;
  418. this.$jconfirmBoxContainer.css("transform", "translate(" + 0 + "px, " + 0 + "px)");
  419. },
  420. setDrag: function () {
  421. if (!this.draggable) {
  422. return;
  423. }
  424. this.alignMiddle = false;
  425. var boxWidth = this.$jconfirmBox.outerWidth();
  426. var boxHeight = this.$jconfirmBox.outerHeight();
  427. var windowWidth = $(window).width();
  428. var windowHeight = $(window).height();
  429. var that = this;
  430. var dragUpdate = 1;
  431. if (that.movingX % dragUpdate === 0 || that.movingY % dragUpdate === 0) {
  432. if (that.dragWindowBorder) {
  433. var leftDistance = windowWidth / 2 - boxWidth / 2;
  434. var topDistance = windowHeight / 2 - boxHeight / 2;
  435. topDistance -= that.dragWindowGap;
  436. leftDistance -= that.dragWindowGap;
  437. if (leftDistance + that.movingX < 0) {
  438. that.movingX = -leftDistance;
  439. } else {
  440. if (leftDistance - that.movingX < 0) {
  441. that.movingX = leftDistance;
  442. }
  443. }
  444. if (topDistance + that.movingY < 0) {
  445. that.movingY = -topDistance;
  446. } else {
  447. if (topDistance - that.movingY < 0) {
  448. that.movingY = topDistance;
  449. }
  450. }
  451. }
  452. that.$jconfirmBoxContainer.css("transform", "translate(" + that.movingX + "px, " + that.movingY + "px)");
  453. }
  454. },
  455. _scrollTop: function () {
  456. if (typeof pageYOffset !== "undefined") {
  457. return pageYOffset;
  458. } else {
  459. var B = document.body;
  460. var D = document.documentElement;
  461. D = D.clientHeight ? D : B;
  462. return D.scrollTop;
  463. }
  464. },
  465. _watchContent: function () {
  466. var that = this;
  467. if (this._timer) {
  468. clearInterval(this._timer);
  469. }
  470. var prevContentHeight = 0;
  471. this._timer = setInterval(function () {
  472. if (that.smoothContent) {
  473. var contentHeight = that.$content.outerHeight() || 0;
  474. if (contentHeight !== prevContentHeight) {
  475. prevContentHeight = contentHeight;
  476. }
  477. var wh = $(window).height();
  478. var total = that.offsetTop + that.offsetBottom + that.$jconfirmBox.height() - that.$contentPane.height() + that.$content.height();
  479. if (total < wh) {
  480. that.$contentPane.addClass("no-scroll");
  481. } else {
  482. that.$contentPane.removeClass("no-scroll");
  483. }
  484. }
  485. }, this.watchInterval);
  486. },
  487. _overflowClass: "jconfirm-overflow",
  488. _hilightAnimating: false,
  489. highlight: function () {
  490. this.hiLightModal();
  491. },
  492. hiLightModal: function () {
  493. var that = this;
  494. if (this._hilightAnimating) {
  495. return;
  496. }
  497. that.$body.addClass("hilight");
  498. var duration = parseFloat(that.$body.css("animation-duration")) || 2;
  499. this._hilightAnimating = true;
  500. setTimeout(function () {
  501. that._hilightAnimating = false;
  502. that.$body.removeClass("hilight");
  503. }, duration * 1000);
  504. },
  505. _bindEvents: function () {
  506. var that = this;
  507. this.boxClicked = false;
  508. this.$scrollPane.click(function (e) {
  509. if (!that.boxClicked) {
  510. var buttonName = false;
  511. var shouldClose = false;
  512. var str;
  513. if (typeof that.backgroundDismiss === "function") {
  514. str = that.backgroundDismiss();
  515. } else {
  516. str = that.backgroundDismiss;
  517. }
  518. if (typeof str === "string" && typeof that.buttons[str] !== "undefined") {
  519. buttonName = str;
  520. shouldClose = false;
  521. } else {
  522. if (typeof str === "undefined" || !!str === true) {
  523. shouldClose = true;
  524. } else {
  525. shouldClose = false;
  526. }
  527. }
  528. if (buttonName) {
  529. var btnResponse = that.buttons[buttonName].action.apply(that);
  530. shouldClose = typeof btnResponse === "undefined" || !!btnResponse;
  531. }
  532. if (shouldClose) {
  533. that.close();
  534. } else {
  535. that.hiLightModal();
  536. }
  537. }
  538. that.boxClicked = false;
  539. });
  540. this.$jconfirmBox.click(function (e) {
  541. that.boxClicked = true;
  542. });
  543. var isKeyDown = false;
  544. $(window).on("jcKeyDown." + that._id, function (e) {
  545. if (!isKeyDown) {
  546. isKeyDown = true;
  547. }
  548. });
  549. $(window).on("keyup." + that._id, function (e) {
  550. if (isKeyDown) {
  551. that.reactOnKey(e);
  552. isKeyDown = false;
  553. }
  554. });
  555. $(window).on("resize." + this._id, function () {
  556. that._updateContentMaxHeight();
  557. setTimeout(function () {
  558. that.resetDrag();
  559. }, 100);
  560. });
  561. },
  562. _cubic_bezier: "0.36, 0.55, 0.19",
  563. _getCSS: function (speed, bounce) {
  564. return {
  565. "-webkit-transition-duration": speed / 1000 + "s",
  566. "transition-duration": speed / 1000 + "s",
  567. "-webkit-transition-timing-function": "cubic-bezier(" + this._cubic_bezier + ", " + bounce + ")",
  568. "transition-timing-function": "cubic-bezier(" + this._cubic_bezier + ", " + bounce + ")",
  569. };
  570. },
  571. _setButtons: function () {
  572. var that = this;
  573. var total_buttons = 0;
  574. if (typeof this.buttons !== "object") {
  575. this.buttons = {};
  576. }
  577. $.each(this.buttons, function (key, button) {
  578. total_buttons += 1;
  579. if (typeof button === "function") {
  580. that.buttons[key] = button = { action: button };
  581. }
  582. that.buttons[key].text = button.text || key;
  583. that.buttons[key].btnClass = button.btnClass || "btn-default";
  584. that.buttons[key].action = button.action || function () {};
  585. that.buttons[key].keys = button.keys || [];
  586. that.buttons[key].isHidden = button.isHidden || false;
  587. that.buttons[key].isDisabled = button.isDisabled || false;
  588. $.each(that.buttons[key].keys, function (i, a) {
  589. that.buttons[key].keys[i] = a.toLowerCase();
  590. });
  591. var button_element = $('<button type="button" class="btn"></button>')
  592. .html(that.buttons[key].text)
  593. .addClass(that.buttons[key].btnClass)
  594. .prop("disabled", that.buttons[key].isDisabled)
  595. .css("display", that.buttons[key].isHidden ? "none" : "")
  596. .click(function (e) {
  597. e.preventDefault();
  598. var res = that.buttons[key].action.apply(that, [that.buttons[key]]);
  599. that.onAction.apply(that, [key, that.buttons[key]]);
  600. that._stopCountDown();
  601. if (typeof res === "undefined" || res) {
  602. that.close();
  603. }
  604. });
  605. that.buttons[key].el = button_element;
  606. that.buttons[key].setText = function (text) {
  607. button_element.html(text);
  608. };
  609. that.buttons[key].addClass = function (className) {
  610. button_element.addClass(className);
  611. };
  612. that.buttons[key].removeClass = function (className) {
  613. button_element.removeClass(className);
  614. };
  615. that.buttons[key].disable = function () {
  616. that.buttons[key].isDisabled = true;
  617. button_element.prop("disabled", true);
  618. };
  619. that.buttons[key].enable = function () {
  620. that.buttons[key].isDisabled = false;
  621. button_element.prop("disabled", false);
  622. };
  623. that.buttons[key].show = function () {
  624. that.buttons[key].isHidden = false;
  625. button_element.css("display", "");
  626. };
  627. that.buttons[key].hide = function () {
  628. that.buttons[key].isHidden = true;
  629. button_element.css("display", "none");
  630. };
  631. that["$_" + key] = that["$$" + key] = button_element;
  632. that.$btnc.append(button_element);
  633. });
  634. if (total_buttons === 0) {
  635. this.$btnc.hide();
  636. }
  637. if (this.closeIcon === null && total_buttons === 0) {
  638. this.closeIcon = true;
  639. }
  640. if (this.closeIcon) {
  641. if (this.closeIconClass) {
  642. var closeHtml = '<i class="' + this.closeIconClass + '"></i>';
  643. this.$closeIcon.html(closeHtml);
  644. }
  645. this.$closeIcon.click(function (e) {
  646. e.preventDefault();
  647. var buttonName = false;
  648. var shouldClose = false;
  649. var str;
  650. if (typeof that.closeIcon === "function") {
  651. str = that.closeIcon();
  652. } else {
  653. str = that.closeIcon;
  654. }
  655. if (typeof str === "string" && typeof that.buttons[str] !== "undefined") {
  656. buttonName = str;
  657. shouldClose = false;
  658. } else {
  659. if (typeof str === "undefined" || !!str === true) {
  660. shouldClose = true;
  661. } else {
  662. shouldClose = false;
  663. }
  664. }
  665. if (buttonName) {
  666. var btnResponse = that.buttons[buttonName].action.apply(that);
  667. shouldClose = typeof btnResponse === "undefined" || !!btnResponse;
  668. }
  669. if (shouldClose) {
  670. that.close();
  671. }
  672. });
  673. this.$closeIcon.show();
  674. } else {
  675. this.$closeIcon.hide();
  676. }
  677. },
  678. setTitle: function (string, force) {
  679. force = force || false;
  680. if (typeof string !== "undefined") {
  681. if (typeof string === "string") {
  682. this.title = string;
  683. } else {
  684. if (typeof string === "function") {
  685. if (typeof string.promise === "function") {
  686. console.error("Promise was returned from title function, this is not supported.");
  687. }
  688. var response = string();
  689. if (typeof response === "string") {
  690. this.title = response;
  691. } else {
  692. this.title = false;
  693. }
  694. } else {
  695. this.title = false;
  696. }
  697. }
  698. }
  699. if (this.isAjaxLoading && !force) {
  700. return;
  701. }
  702. this.$title.html(this.title || "");
  703. this.updateTitleContainer();
  704. },
  705. setIcon: function (iconClass, force) {
  706. force = force || false;
  707. if (typeof iconClass !== "undefined") {
  708. if (typeof iconClass === "string") {
  709. this.icon = iconClass;
  710. } else {
  711. if (typeof iconClass === "function") {
  712. var response = iconClass();
  713. if (typeof response === "string") {
  714. this.icon = response;
  715. } else {
  716. this.icon = false;
  717. }
  718. } else {
  719. this.icon = false;
  720. }
  721. }
  722. }
  723. if (this.isAjaxLoading && !force) {
  724. return;
  725. }
  726. this.$icon.html(this.icon ? '<i class="' + this.icon + '"></i>' : "");
  727. this.updateTitleContainer();
  728. },
  729. updateTitleContainer: function () {
  730. if (!this.title && !this.icon) {
  731. this.$titleContainer.hide();
  732. } else {
  733. this.$titleContainer.show();
  734. }
  735. },
  736. setContentPrepend: function (content, force) {
  737. if (!content) {
  738. return;
  739. }
  740. this.contentParsed.prepend(content);
  741. },
  742. setContentAppend: function (content) {
  743. if (!content) {
  744. return;
  745. }
  746. this.contentParsed.append(content);
  747. },
  748. setContent: function (content, force) {
  749. force = !!force;
  750. var that = this;
  751. if (content) {
  752. this.contentParsed.html("").append(content);
  753. }
  754. if (this.isAjaxLoading && !force) {
  755. return;
  756. }
  757. this.$content.html("");
  758. this.$content.append(this.contentParsed);
  759. setTimeout(function () {
  760. that.$body.find("input[autofocus]:visible:first").focus();
  761. }, 100);
  762. },
  763. loadingSpinner: false,
  764. showLoading: function (disableButtons) {
  765. this.loadingSpinner = true;
  766. this.$jconfirmBox.addClass("loading");
  767. if (disableButtons) {
  768. this.$btnc.find("button").prop("disabled", true);
  769. }
  770. },
  771. hideLoading: function (enableButtons) {
  772. this.loadingSpinner = false;
  773. this.$jconfirmBox.removeClass("loading");
  774. if (enableButtons) {
  775. this.$btnc.find("button").prop("disabled", false);
  776. }
  777. },
  778. ajaxResponse: false,
  779. contentParsed: "",
  780. isAjax: false,
  781. isAjaxLoading: false,
  782. _parseContent: function () {
  783. var that = this;
  784. var e = "&nbsp;";
  785. if (typeof this.content === "function") {
  786. var res = this.content.apply(this);
  787. if (typeof res === "string") {
  788. this.content = res;
  789. } else {
  790. if (typeof res === "object" && typeof res.always === "function") {
  791. this.isAjax = true;
  792. this.isAjaxLoading = true;
  793. res.always(function (data, status, xhr) {
  794. that.ajaxResponse = { data: data, status: status, xhr: xhr };
  795. that._contentReady.resolve(data, status, xhr);
  796. if (typeof that.contentLoaded === "function") {
  797. that.contentLoaded(data, status, xhr);
  798. }
  799. });
  800. this.content = e;
  801. } else {
  802. this.content = e;
  803. }
  804. }
  805. }
  806. if (typeof this.content === "string" && this.content.substr(0, 4).toLowerCase() === "url:") {
  807. this.isAjax = true;
  808. this.isAjaxLoading = true;
  809. var u = this.content.substring(4, this.content.length);
  810. $.get(u)
  811. .done(function (html) {
  812. that.contentParsed.html(html);
  813. })
  814. .always(function (data, status, xhr) {
  815. that.ajaxResponse = { data: data, status: status, xhr: xhr };
  816. that._contentReady.resolve(data, status, xhr);
  817. if (typeof that.contentLoaded === "function") {
  818. that.contentLoaded(data, status, xhr);
  819. }
  820. });
  821. }
  822. if (!this.content) {
  823. this.content = e;
  824. }
  825. if (!this.isAjax) {
  826. this.contentParsed.html(this.content);
  827. this.setContent();
  828. that._contentReady.resolve();
  829. }
  830. },
  831. _stopCountDown: function () {
  832. clearInterval(this.autoCloseInterval);
  833. if (this.$cd) {
  834. this.$cd.remove();
  835. }
  836. },
  837. _startCountDown: function () {
  838. var that = this;
  839. var opt = this.autoClose.split("|");
  840. if (opt.length !== 2) {
  841. console.error("Invalid option for autoClose. example 'close|10000'");
  842. return false;
  843. }
  844. var button_key = opt[0];
  845. var time = parseInt(opt[1]);
  846. if (typeof this.buttons[button_key] === "undefined") {
  847. console.error("Invalid button key '" + button_key + "' for autoClose");
  848. return false;
  849. }
  850. var seconds = Math.ceil(time / 1000);
  851. this.$cd = $('<span class="countdown"> (' + seconds + ")</span>").appendTo(this["$_" + button_key]);
  852. this.autoCloseInterval = setInterval(function () {
  853. that.$cd.html(" (" + (seconds -= 1) + ") ");
  854. if (seconds <= 0) {
  855. that["$$" + button_key].trigger("click");
  856. that._stopCountDown();
  857. }
  858. }, 1000);
  859. },
  860. _getKey: function (key) {
  861. switch (key) {
  862. case 192:
  863. return "tilde";
  864. case 13:
  865. return "enter";
  866. case 16:
  867. return "shift";
  868. case 9:
  869. return "tab";
  870. case 20:
  871. return "capslock";
  872. case 17:
  873. return "ctrl";
  874. case 91:
  875. return "win";
  876. case 18:
  877. return "alt";
  878. case 27:
  879. return "esc";
  880. case 32:
  881. return "space";
  882. }
  883. var initial = String.fromCharCode(key);
  884. if (/^[A-z0-9]+$/.test(initial)) {
  885. return initial.toLowerCase();
  886. } else {
  887. return false;
  888. }
  889. },
  890. reactOnKey: function (e) {
  891. var that = this;
  892. var a = $(".jconfirm");
  893. if (a.eq(a.length - 1)[0] !== this.$el[0]) {
  894. return false;
  895. }
  896. var key = e.which;
  897. if (this.$content.find(":input").is(":focus") && /13|32/.test(key)) {
  898. return false;
  899. }
  900. var keyChar = this._getKey(key);
  901. if (keyChar === "esc" && this.escapeKey) {
  902. if (this.escapeKey === true) {
  903. this.$scrollPane.trigger("click");
  904. } else {
  905. if (typeof this.escapeKey === "string" || typeof this.escapeKey === "function") {
  906. var buttonKey;
  907. if (typeof this.escapeKey === "function") {
  908. buttonKey = this.escapeKey();
  909. } else {
  910. buttonKey = this.escapeKey;
  911. }
  912. if (buttonKey) {
  913. if (typeof this.buttons[buttonKey] === "undefined") {
  914. console.warn("Invalid escapeKey, no buttons found with key " + buttonKey);
  915. } else {
  916. this["$_" + buttonKey].trigger("click");
  917. }
  918. }
  919. }
  920. }
  921. }
  922. $.each(this.buttons, function (key, button) {
  923. if (button.keys.indexOf(keyChar) !== -1) {
  924. that["$_" + key].trigger("click");
  925. }
  926. });
  927. },
  928. setDialogCenter: function () {
  929. console.info("setDialogCenter is deprecated, dialogs are centered with CSS3 tables");
  930. },
  931. _unwatchContent: function () {
  932. clearInterval(this._timer);
  933. },
  934. close: function (onClosePayload) {
  935. var that = this;
  936. if (typeof this.onClose === "function") {
  937. this.onClose(onClosePayload);
  938. }
  939. this._unwatchContent();
  940. $(window).unbind("resize." + this._id);
  941. $(window).unbind("keyup." + this._id);
  942. $(window).unbind("jcKeyDown." + this._id);
  943. if (this.draggable) {
  944. $(window).unbind("mousemove." + this._id);
  945. $(window).unbind("mouseup." + this._id);
  946. this.$titleContainer.unbind("mousedown");
  947. }
  948. that.$el.removeClass(that.loadedClass);
  949. $("body").removeClass("jconfirm-no-scroll-" + that._id);
  950. that.$jconfirmBoxContainer.removeClass("jconfirm-no-transition");
  951. setTimeout(function () {
  952. that.$body.addClass(that.closeAnimationParsed);
  953. that.$jconfirmBg.addClass("jconfirm-bg-h");
  954. var closeTimer = that.closeAnimation === "none" ? 1 : that.animationSpeed;
  955. setTimeout(function () {
  956. that.$el.remove();
  957. var l = w.jconfirm.instances;
  958. var i = w.jconfirm.instances.length - 1;
  959. for (i; i >= 0; i--) {
  960. if (w.jconfirm.instances[i]._id === that._id) {
  961. w.jconfirm.instances.splice(i, 1);
  962. }
  963. }
  964. if (!w.jconfirm.instances.length) {
  965. if (that.scrollToPreviousElement && w.jconfirm.lastFocused && w.jconfirm.lastFocused.length && $.contains(document, w.jconfirm.lastFocused[0])) {
  966. var $lf = w.jconfirm.lastFocused;
  967. if (that.scrollToPreviousElementAnimate) {
  968. var st = $(window).scrollTop();
  969. var ot = w.jconfirm.lastFocused.offset().top;
  970. var wh = $(window).height();
  971. if (!(ot > st && ot < st + wh)) {
  972. var scrollTo = ot - Math.round(wh / 3);
  973. $("html, body").animate({ scrollTop: scrollTo }, that.animationSpeed, "swing", function () {
  974. $lf.focus();
  975. });
  976. } else {
  977. $lf.focus();
  978. }
  979. } else {
  980. $lf.focus();
  981. }
  982. w.jconfirm.lastFocused = false;
  983. }
  984. }
  985. if (typeof that.onDestroy === "function") {
  986. that.onDestroy();
  987. }
  988. }, closeTimer * 0.4);
  989. }, 50);
  990. return true;
  991. },
  992. open: function () {
  993. if (this.isOpen()) {
  994. return false;
  995. }
  996. this._buildHTML();
  997. this._bindEvents();
  998. this._open();
  999. return true;
  1000. },
  1001. setStartingPoint: function () {
  1002. var el = false;
  1003. if (this.animateFromElement !== true && this.animateFromElement) {
  1004. el = this.animateFromElement;
  1005. w.jconfirm.lastClicked = false;
  1006. } else {
  1007. if (w.jconfirm.lastClicked && this.animateFromElement === true) {
  1008. el = w.jconfirm.lastClicked;
  1009. w.jconfirm.lastClicked = false;
  1010. } else {
  1011. return false;
  1012. }
  1013. }
  1014. if (!el) {
  1015. return false;
  1016. }
  1017. var offset = el.offset();
  1018. var iTop = el.outerHeight() / 2;
  1019. var iLeft = el.outerWidth() / 2;
  1020. iTop -= this.$jconfirmBox.outerHeight() / 2;
  1021. iLeft -= this.$jconfirmBox.outerWidth() / 2;
  1022. var sourceTop = offset.top + iTop;
  1023. sourceTop = sourceTop - this._scrollTop();
  1024. var sourceLeft = offset.left + iLeft;
  1025. var wh = $(window).height() / 2;
  1026. var ww = $(window).width() / 2;
  1027. var targetH = wh - this.$jconfirmBox.outerHeight() / 2;
  1028. var targetW = ww - this.$jconfirmBox.outerWidth() / 2;
  1029. sourceTop -= targetH;
  1030. sourceLeft -= targetW;
  1031. if (Math.abs(sourceTop) > wh || Math.abs(sourceLeft) > ww) {
  1032. return false;
  1033. }
  1034. this.$jconfirmBoxContainer.css("transform", "translate(" + sourceLeft + "px, " + sourceTop + "px)");
  1035. },
  1036. _open: function () {
  1037. var that = this;
  1038. if (typeof that.onOpenBefore === "function") {
  1039. that.onOpenBefore();
  1040. }
  1041. this.$body.removeClass(this.animationParsed);
  1042. this.$jconfirmBg.removeClass("jconfirm-bg-h");
  1043. this.$body.focus();
  1044. that.$jconfirmBoxContainer.css("transform", "translate(" + 0 + "px, " + 0 + "px)");
  1045. setTimeout(function () {
  1046. that.$body.css(that._getCSS(that.animationSpeed, 1));
  1047. that.$body.css({ "transition-property": that.$body.css("transition-property") + ", margin" });
  1048. that.$jconfirmBoxContainer.addClass("jconfirm-no-transition");
  1049. that._modalReady.resolve();
  1050. if (typeof that.onOpen === "function") {
  1051. that.onOpen();
  1052. }
  1053. that.$el.addClass(that.loadedClass);
  1054. }, this.animationSpeed);
  1055. },
  1056. loadedClass: "jconfirm-open",
  1057. isClosed: function () {
  1058. return !this.$el || this.$el.parent().length === 0;
  1059. },
  1060. isOpen: function () {
  1061. return !this.isClosed();
  1062. },
  1063. toggle: function () {
  1064. if (!this.isOpen()) {
  1065. this.open();
  1066. } else {
  1067. this.close();
  1068. }
  1069. },
  1070. };
  1071. w.jconfirm.instances = [];
  1072. w.jconfirm.lastFocused = false;
  1073. w.jconfirm.pluginDefaults = {
  1074. template:
  1075. '<div class="jconfirm"><div class="jconfirm-bg jconfirm-bg-h"></div><div class="jconfirm-scrollpane"><div class="jconfirm-row"><div class="jconfirm-cell"><div class="jconfirm-holder"><div class="jc-bs3-container"><div class="jc-bs3-row"><div class="jconfirm-box-container jconfirm-animated"><div class="jconfirm-box" role="dialog" aria-labelledby="labelled" tabindex="-1"><div class="jconfirm-closeIcon">&times;</div><div class="jconfirm-title-c"><span class="jconfirm-icon-c"></span><span class="jconfirm-title"></span></div><div class="jconfirm-content-pane"><div class="jconfirm-content"></div></div><div class="jconfirm-buttons"></div><div class="jconfirm-clear"></div></div></div></div></div></div></div></div></div></div>',
  1076. title: "Hello",
  1077. titleClass: "",
  1078. type: "default",
  1079. typeAnimated: true,
  1080. draggable: true,
  1081. dragWindowGap: 15,
  1082. dragWindowBorder: true,
  1083. animateFromElement: true,
  1084. alignMiddle: true,
  1085. smoothContent: true,
  1086. content: "Are you sure to continue?",
  1087. buttons: {},
  1088. defaultButtons: { ok: { action: function () {} }, close: { action: function () {} } },
  1089. contentLoaded: function () {},
  1090. icon: "",
  1091. lazyOpen: false,
  1092. bgOpacity: null,
  1093. theme: "light",
  1094. animation: "scale",
  1095. closeAnimation: "scale",
  1096. animationSpeed: 400,
  1097. animationBounce: 1,
  1098. escapeKey: true,
  1099. rtl: false,
  1100. container: "body",
  1101. containerFluid: false,
  1102. backgroundDismiss: false,
  1103. backgroundDismissAnimation: "shake",
  1104. autoClose: false,
  1105. closeIcon: null,
  1106. closeIconClass: false,
  1107. watchInterval: 100,
  1108. columnClass: "col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1",
  1109. boxWidth: "50%",
  1110. scrollToPreviousElement: true,
  1111. scrollToPreviousElementAnimate: true,
  1112. useBootstrap: true,
  1113. offsetTop: 40,
  1114. offsetBottom: 40,
  1115. bootstrapClasses: { container: "container", containerFluid: "container-fluid", row: "row" },
  1116. onContentReady: function () {},
  1117. onOpenBefore: function () {},
  1118. onOpen: function () {},
  1119. onClose: function () {},
  1120. onDestroy: function () {},
  1121. onAction: function () {},
  1122. };
  1123. var keyDown = false;
  1124. $(window).on("keydown", function (e) {
  1125. if (!keyDown) {
  1126. var $target = $(e.target);
  1127. var pass = false;
  1128. if ($target.closest(".jconfirm-box").length) {
  1129. pass = true;
  1130. }
  1131. if (pass) {
  1132. $(window).trigger("jcKeyDown");
  1133. }
  1134. keyDown = true;
  1135. }
  1136. });
  1137. $(window).on("keyup", function () {
  1138. keyDown = false;
  1139. });
  1140. w.jconfirm.lastClicked = false;
  1141. $(document).on("mousedown", "button, a, [jc-source]", function () {
  1142. w.jconfirm.lastClicked = $(this);
  1143. });
  1144. });