noty.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*!
  2. @package noty - jQuery Notification Plugin
  3. @version version: 2.4.0
  4. @contributors https://github.com/needim/noty/graphs/contributors
  5. @documentation Examples and Documentation - http://needim.github.com/noty/
  6. @license Licensed under the MIT licenses: http://www.opensource.org/licenses/mit-license.php
  7. */
  8. if (typeof Object.create !== 'function') {
  9. Object.create = function (o) {
  10. function F() {
  11. }
  12. F.prototype = o;
  13. return new F();
  14. };
  15. }
  16. var NotyObject = {
  17. init: function (options) {
  18. // Mix in the passed in options with the default options
  19. this.options = $.extend({}, $.noty.defaults, options);
  20. this.options.layout = (this.options.custom) ? $.noty.layouts['inline'] : $.noty.layouts[this.options.layout];
  21. if ($.noty.themes[this.options.theme]) {
  22. this.options.theme = $.noty.themes[this.options.theme];
  23. if (this.options.theme.template)
  24. this.options.template = this.options.theme.template;
  25. if (this.options.theme.animation)
  26. this.options.animation = this.options.theme.animation;
  27. }
  28. else {
  29. this.options.themeClassName = this.options.theme;
  30. }
  31. this.options = $.extend({}, this.options, this.options.layout.options);
  32. if (this.options.id) {
  33. if ($.noty.store[this.options.id]) {
  34. return $.noty.store[this.options.id];
  35. }
  36. } else {
  37. this.options.id = 'noty_' + (new Date().getTime() * Math.floor(Math.random() * 1000000));
  38. }
  39. // Build the noty dom initial structure
  40. this._build();
  41. // return this so we can chain/use the bridge with less code.
  42. return this;
  43. }, // end init
  44. _build: function () {
  45. // Generating noty bar
  46. var $bar = $('<div class="noty_bar noty_type_' + this.options.type + '"></div>').attr('id', this.options.id);
  47. $bar.append(this.options.template).find('.noty_text').html(this.options.text);
  48. this.$bar = (this.options.layout.parent.object !== null) ? $(this.options.layout.parent.object).css(this.options.layout.parent.css).append($bar) : $bar;
  49. if (this.options.themeClassName)
  50. this.$bar.addClass(this.options.themeClassName).addClass('noty_container_type_' + this.options.type);
  51. // Set buttons if available
  52. if (this.options.buttons) {
  53. var $buttons;
  54. // Try find container for buttons in presented template, and create it if not found
  55. if (this.$bar.find('.noty_buttons').length > 0) {
  56. $buttons = this.$bar.find('.noty_buttons');
  57. } else {
  58. $buttons = $('<div/>').addClass('noty_buttons');
  59. (this.options.layout.parent.object !== null) ? this.$bar.find('.noty_bar').append($buttons) : this.$bar.append($buttons);
  60. }
  61. var self = this;
  62. $.each(this.options.buttons, function (i, button) {
  63. var $button = $('<button/>').addClass((button.addClass) ? button.addClass : 'gray').html(button.text).attr('id', button.id ? button.id : 'button-' + i)
  64. .attr('title', button.title)
  65. .appendTo($buttons)
  66. .on('click', function (event) {
  67. if ($.isFunction(button.onClick)) {
  68. button.onClick.call($button, self, event);
  69. }
  70. });
  71. });
  72. } else {
  73. // If buttons is not available, then remove containers if exist
  74. this.$bar.find('.noty_buttons').remove();
  75. }
  76. if (this.options.progressBar && this.options.timeout) {
  77. var $progressBar = $('<div/>').addClass('noty_progress_bar');
  78. (this.options.layout.parent.object !== null) ? this.$bar.find('.noty_bar').append($progressBar) : this.$bar.append($progressBar);
  79. }
  80. // For easy access
  81. this.$message = this.$bar.find('.noty_message');
  82. this.$closeButton = this.$bar.find('.noty_close');
  83. this.$buttons = this.$bar.find('.noty_buttons');
  84. this.$progressBar = this.$bar.find('.noty_progress_bar');
  85. $.noty.store[this.options.id] = this; // store noty for api
  86. }, // end _build
  87. show: function () {
  88. var self = this;
  89. (self.options.custom) ? self.options.custom.find(self.options.layout.container.selector).append(self.$bar) : $(self.options.layout.container.selector).append(self.$bar);
  90. if (self.options.theme && self.options.theme.style)
  91. self.options.theme.style.apply(self);
  92. ($.type(self.options.layout.css) === 'function') ? this.options.layout.css.apply(self.$bar) : self.$bar.css(this.options.layout.css || {});
  93. self.$bar.addClass(self.options.layout.addClass);
  94. self.options.layout.container.style.apply($(self.options.layout.container.selector), [self.options.within]);
  95. self.showing = true;
  96. if (self.options.theme && self.options.theme.style)
  97. self.options.theme.callback.onShow.apply(this);
  98. if ($.inArray('click', self.options.closeWith) > -1)
  99. self.$bar.css('cursor', 'pointer').on('click', function (evt) {
  100. self.stopPropagation(evt);
  101. if (self.options.callback.onCloseClick) {
  102. self.options.callback.onCloseClick.apply(self);
  103. }
  104. self.close();
  105. });
  106. if ($.inArray('hover', self.options.closeWith) > -1)
  107. self.$bar.one('mouseenter', function () {
  108. self.close();
  109. });
  110. if ($.inArray('button', self.options.closeWith) > -1)
  111. self.$closeButton.one('click', function (evt) {
  112. self.stopPropagation(evt);
  113. self.close();
  114. });
  115. if ($.inArray('button', self.options.closeWith) == -1)
  116. self.$closeButton.remove();
  117. if (self.options.callback.beforeShow)
  118. self.options.callback.beforeShow.apply(self);
  119. if (typeof self.options.animation.open == 'string') {
  120. self.animationTypeOpen = 'css';
  121. self.$bar.css('min-height', self.$bar.innerHeight());
  122. self.$bar.on('click', function (e) {
  123. self.wasClicked = true;
  124. });
  125. self.$bar.show();
  126. if (self.options.callback.onShow)
  127. self.options.callback.onShow.apply(self);
  128. self.$bar.addClass(self.options.animation.open).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
  129. if (self.options.callback.afterShow) self.options.callback.afterShow.apply(self);
  130. self.showing = false;
  131. self.shown = true;
  132. self.bindTimeout();
  133. if (self.hasOwnProperty('wasClicked')) {
  134. self.$bar.off('click', function (e) {
  135. self.wasClicked = true;
  136. });
  137. self.close();
  138. }
  139. });
  140. } else if (typeof self.options.animation.open == 'object' && self.options.animation.open == null) {
  141. self.animationTypeOpen = 'none';
  142. self.showing = false;
  143. self.shown = true;
  144. self.$bar.show();
  145. self.bindTimeout();
  146. if (self.options.callback.onShow)
  147. self.options.callback.onShow.apply(self);
  148. self.$bar.queue(function () {
  149. if (self.options.callback.afterShow)
  150. self.options.callback.afterShow.apply(self);
  151. });
  152. } else {
  153. self.animationTypeOpen = 'anim';
  154. if (self.options.callback.onShow)
  155. self.options.callback.onShow.apply(self);
  156. self.$bar.animate(
  157. self.options.animation.open,
  158. self.options.animation.speed,
  159. self.options.animation.easing,
  160. function () {
  161. if (self.options.callback.afterShow) self.options.callback.afterShow.apply(self);
  162. self.showing = false;
  163. self.shown = true;
  164. self.bindTimeout();
  165. });
  166. }
  167. return this;
  168. }, // end show
  169. bindTimeout: function() {
  170. var self = this;
  171. // If noty is have a timeout option
  172. if (self.options.timeout) {
  173. if (self.options.progressBar && self.$progressBar) {
  174. self.$progressBar.css({
  175. transition: 'all 100ms linear'
  176. });
  177. self.progressPercentage = (self.$progressBar.width() / (self.options.timeout / 100));
  178. self.intervalId = setInterval(function() {
  179. self.$progressBar.width((self.$progressBar.width() - self.progressPercentage));
  180. }, 100);
  181. }
  182. self.queueClose(self.options.timeout);
  183. self.$bar.on('mouseenter', self.dequeueClose.bind(self));
  184. self.$bar.on('mouseleave', self.queueClose.bind(self, self.options.timeout));
  185. }
  186. },
  187. dequeueClose: function () {
  188. if (this.intervalId) {
  189. clearInterval(this.intervalId);
  190. this.$progressBar.css('width', '100%');
  191. this.intervalId = null;
  192. }
  193. if (!this.closeTimer) return;
  194. clearTimeout(this.closeTimer);
  195. this.closeTimer = null;
  196. },
  197. queueClose: function (timeout) {
  198. var self = this;
  199. if (!self.intervalId && self.options.progressBar) {
  200. self.intervalId = setInterval(function() {
  201. self.$progressBar.width((self.$progressBar.width() - self.progressPercentage));
  202. }, 100);
  203. }
  204. if (this.closeTimer) return;
  205. self.closeTimer = window.setTimeout(function () {
  206. self.close();
  207. }, timeout);
  208. return self.closeTimer
  209. },
  210. close: function () {
  211. if (this.intervalId) {
  212. clearInterval(this.intervalId);
  213. this.intervalId = null;
  214. if (this.$progressBar) {
  215. this.$progressBar.css('width', '0%');
  216. }
  217. }
  218. if (this.closeTimer) this.dequeueClose();
  219. if (this.closed) return;
  220. if (this.$bar && this.$bar.hasClass('i-am-closing-now')) return;
  221. var self = this;
  222. if (this.showing && (this.animationTypeOpen == 'anim' || this.animationTypeOpen == 'none')) {
  223. self.$bar.queue(
  224. function () {
  225. self.close.apply(self);
  226. }
  227. );
  228. return;
  229. } else if (this.showing && this.animationTypeOpen == 'css') {
  230. self.$bar.on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
  231. self.close();
  232. });
  233. }
  234. if (!this.shown && !this.showing) { // If we are still waiting in the queue just delete from queue
  235. var queue = [];
  236. $.each($.noty.queue, function (i, n) {
  237. if (n.options.id != self.options.id) {
  238. queue.push(n);
  239. }
  240. });
  241. $.noty.queue = queue;
  242. return;
  243. }
  244. self.$bar.addClass('i-am-closing-now');
  245. if (self.options.callback.onClose) {
  246. self.options.callback.onClose.apply(self);
  247. }
  248. if (typeof self.options.animation.close == 'string') {
  249. self.$bar.removeClass(self.options.animation.open).addClass(self.options.animation.close).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function () {
  250. if (self.options.callback.afterClose) self.options.callback.afterClose.apply(self);
  251. self.closeCleanUp();
  252. });
  253. } else if (typeof self.options.animation.close == 'object' && self.options.animation.close == null) {
  254. self.$bar.dequeue().hide(0, function() {
  255. if (self.options.callback.afterClose) self.options.callback.afterClose.apply(self);
  256. self.closeCleanUp();
  257. });
  258. } else {
  259. self.$bar.clearQueue().stop().animate(
  260. self.options.animation.close,
  261. self.options.animation.speed,
  262. self.options.animation.easing,
  263. function () {
  264. if (self.options.callback.afterClose) self.options.callback.afterClose.apply(self);
  265. })
  266. .promise().done(function () {
  267. self.closeCleanUp();
  268. });
  269. }
  270. }, // end close
  271. closeCleanUp: function () {
  272. var self = this;
  273. // Modal Cleaning
  274. if (self.options.modal) {
  275. $.notyRenderer.setModalCount(-1);
  276. if ($.notyRenderer.getModalCount() == 0 && !$.noty.queue.length) $('.noty_modal').fadeOut(self.options.animation.fadeSpeed, function () {
  277. $(this).remove();
  278. });
  279. }
  280. // Layout Cleaning
  281. $.notyRenderer.setLayoutCountFor(self, -1);
  282. if ($.notyRenderer.getLayoutCountFor(self) == 0) $(self.options.layout.container.selector).remove();
  283. // Make sure self.$bar has not been removed before attempting to remove it
  284. if (typeof self.$bar !== 'undefined' && self.$bar !== null) {
  285. if (typeof self.options.animation.close == 'string') {
  286. self.$bar.css('transition', 'all 10ms ease').css('border', 0).css('margin', 0).height(0);
  287. self.$bar.one('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', function () {
  288. self.$bar.remove();
  289. self.$bar = null;
  290. self.closed = true;
  291. if (self.options.theme.callback && self.options.theme.callback.onClose) {
  292. self.options.theme.callback.onClose.apply(self);
  293. }
  294. self.handleNext();
  295. });
  296. } else {
  297. self.$bar.remove();
  298. self.$bar = null;
  299. self.closed = true;
  300. self.handleNext();
  301. }
  302. } else {
  303. self.handleNext();
  304. }
  305. }, // end close clean up
  306. handleNext: function () {
  307. var self = this;
  308. delete $.noty.store[self.options.id]; // deleting noty from store
  309. if (self.options.theme.callback && self.options.theme.callback.onClose) {
  310. self.options.theme.callback.onClose.apply(self);
  311. }
  312. if (!self.options.dismissQueue) {
  313. // Queue render
  314. $.noty.ontap = true;
  315. $.notyRenderer.render();
  316. }
  317. if (self.options.maxVisible > 0 && self.options.dismissQueue) {
  318. $.notyRenderer.render();
  319. }
  320. },
  321. setText: function (text) {
  322. if (!this.closed) {
  323. this.options.text = text;
  324. this.$bar.find('.noty_text').html(text);
  325. }
  326. return this;
  327. },
  328. setType: function (type) {
  329. if (!this.closed) {
  330. this.options.type = type;
  331. this.options.theme.style.apply(this);
  332. this.options.theme.callback.onShow.apply(this);
  333. }
  334. return this;
  335. },
  336. setTimeout: function (time) {
  337. if (!this.closed) {
  338. var self = this;
  339. this.options.timeout = time;
  340. self.$bar.delay(self.options.timeout).promise().done(function () {
  341. self.close();
  342. });
  343. }
  344. return this;
  345. },
  346. stopPropagation: function (evt) {
  347. evt = evt || window.event;
  348. if (typeof evt.stopPropagation !== "undefined") {
  349. evt.stopPropagation();
  350. }
  351. else {
  352. evt.cancelBubble = true;
  353. }
  354. },
  355. closed : false,
  356. showing: false,
  357. shown : false
  358. }; // end NotyObject
  359. $.notyRenderer = {};
  360. $.notyRenderer.init = function (options) {
  361. // Renderer creates a new noty
  362. var notification = Object.create(NotyObject).init(options);
  363. if (notification.options.killer)
  364. $.noty.closeAll();
  365. (notification.options.force) ? $.noty.queue.unshift(notification) : $.noty.queue.push(notification);
  366. $.notyRenderer.render();
  367. return ($.noty.returns == 'object') ? notification : notification.options.id;
  368. };
  369. $.notyRenderer.render = function () {
  370. var instance = $.noty.queue[0];
  371. if ($.type(instance) === 'object') {
  372. if (instance.options.dismissQueue) {
  373. if (instance.options.maxVisible > 0) {
  374. if ($(instance.options.layout.container.selector + ' > li').length < instance.options.maxVisible) {
  375. $.notyRenderer.show($.noty.queue.shift());
  376. }
  377. else {
  378. }
  379. }
  380. else {
  381. $.notyRenderer.show($.noty.queue.shift());
  382. }
  383. }
  384. else {
  385. if ($.noty.ontap) {
  386. $.notyRenderer.show($.noty.queue.shift());
  387. $.noty.ontap = false;
  388. }
  389. }
  390. }
  391. else {
  392. $.noty.ontap = true; // Queue is over
  393. }
  394. };
  395. $.notyRenderer.show = function (notification) {
  396. if (notification.options.modal) {
  397. $.notyRenderer.createModalFor(notification);
  398. $.notyRenderer.setModalCount(+1);
  399. }
  400. // Where is the container?
  401. if (notification.options.custom) {
  402. if (notification.options.custom.find(notification.options.layout.container.selector).length == 0) {
  403. notification.options.custom.append($(notification.options.layout.container.object).addClass('i-am-new'));
  404. }
  405. else {
  406. notification.options.custom.find(notification.options.layout.container.selector).removeClass('i-am-new');
  407. }
  408. }
  409. else {
  410. if ($(notification.options.layout.container.selector).length == 0) {
  411. $('body').append($(notification.options.layout.container.object).addClass('i-am-new'));
  412. }
  413. else {
  414. $(notification.options.layout.container.selector).removeClass('i-am-new');
  415. }
  416. }
  417. $.notyRenderer.setLayoutCountFor(notification, +1);
  418. notification.show();
  419. };
  420. $.notyRenderer.createModalFor = function (notification) {
  421. if ($('.noty_modal').length == 0) {
  422. var modal = $('<div/>').addClass('noty_modal').addClass(notification.options.theme).data('noty_modal_count', 0);
  423. if (notification.options.theme.modal && notification.options.theme.modal.css)
  424. modal.css(notification.options.theme.modal.css);
  425. modal.prependTo($('body')).fadeIn(notification.options.animation.fadeSpeed);
  426. if ($.inArray('backdrop', notification.options.closeWith) > -1)
  427. modal.on('click', function () {
  428. $.noty.closeAll();
  429. });
  430. }
  431. };
  432. $.notyRenderer.getLayoutCountFor = function (notification) {
  433. return $(notification.options.layout.container.selector).data('noty_layout_count') || 0;
  434. };
  435. $.notyRenderer.setLayoutCountFor = function (notification, arg) {
  436. return $(notification.options.layout.container.selector).data('noty_layout_count', $.notyRenderer.getLayoutCountFor(notification) + arg);
  437. };
  438. $.notyRenderer.getModalCount = function () {
  439. return $('.noty_modal').data('noty_modal_count') || 0;
  440. };
  441. $.notyRenderer.setModalCount = function (arg) {
  442. return $('.noty_modal').data('noty_modal_count', $.notyRenderer.getModalCount() + arg);
  443. };
  444. // This is for custom container
  445. $.fn.noty = function (options) {
  446. options.custom = $(this);
  447. return $.notyRenderer.init(options);
  448. };
  449. $.noty = {};
  450. $.noty.queue = [];
  451. $.noty.ontap = true;
  452. $.noty.layouts = {};
  453. $.noty.themes = {};
  454. $.noty.returns = 'object';
  455. $.noty.store = {};
  456. $.noty.get = function (id) {
  457. return $.noty.store.hasOwnProperty(id) ? $.noty.store[id] : false;
  458. };
  459. $.noty.close = function (id) {
  460. return $.noty.get(id) ? $.noty.get(id).close() : false;
  461. };
  462. $.noty.setText = function (id, text) {
  463. return $.noty.get(id) ? $.noty.get(id).setText(text) : false;
  464. };
  465. $.noty.setType = function (id, type) {
  466. return $.noty.get(id) ? $.noty.get(id).setType(type) : false;
  467. };
  468. $.noty.clearQueue = function () {
  469. $.noty.queue = [];
  470. };
  471. $.noty.closeAll = function () {
  472. $.noty.clearQueue();
  473. $.each($.noty.store, function (id, noty) {
  474. noty.close();
  475. });
  476. };
  477. var windowAlert = window.alert;
  478. $.noty.consumeAlert = function (options) {
  479. window.alert = function (text) {
  480. if (options)
  481. options.text = text;
  482. else
  483. options = {text: text};
  484. $.notyRenderer.init(options);
  485. };
  486. };
  487. $.noty.stopConsumeAlert = function () {
  488. window.alert = windowAlert;
  489. };
  490. $.noty.defaults = {
  491. layout : 'topRight',
  492. theme : 'relax',
  493. type : 'alert',
  494. text : '',
  495. progressBar : false,
  496. dismissQueue: true,
  497. template : '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
  498. animation : {
  499. open : {height: 'toggle'},
  500. close : {height: 'toggle'},
  501. easing : 'swing',
  502. speed : 500,
  503. fadeSpeed: 'fast'
  504. },
  505. timeout : false,
  506. force : false,
  507. modal : false,
  508. maxVisible : 5,
  509. killer : false,
  510. closeWith : ['click'],
  511. callback : {
  512. beforeShow : function () {
  513. },
  514. onShow : function () {
  515. },
  516. afterShow : function () {
  517. },
  518. onClose : function () {
  519. },
  520. afterClose : function () {
  521. },
  522. onCloseClick: function () {
  523. }
  524. },
  525. buttons : false
  526. };
  527. $(window).on('resize', function () {
  528. $.each($.noty.layouts, function (index, layout) {
  529. layout.container.style.apply($(layout.container.selector));
  530. });
  531. });
  532. // Helpers
  533. window.noty = function noty(options) {
  534. return $.notyRenderer.init(options);
  535. };