Нет описания

jquery.jquery-confirm.min.js 36KB

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