PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/jquery-noty/2.0.1/jquery.noty.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 475 lines | 335 code | 112 blank | 28 comment | 73 complexity | 0af80643230c65ce948f2e5cb391d3d8 MD5 | raw file
  1. /**
  2. * noty - jQuery Notification Plugin v2.0.1
  3. * Contributors: https://github.com/needim/noty/graphs/contributors
  4. *
  5. * Examples and Documentation - http://needim.github.com/noty/
  6. *
  7. * Licensed under the MIT licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. **/
  11. if (typeof Object.create !== 'function') {
  12. Object.create = function (o) {
  13. function F() {};
  14. F.prototype = o;
  15. return new F();
  16. };
  17. };
  18. ;(function($) {
  19. var NotyObject = {
  20. init: function(options) {
  21. // Mix in the passed in options with the default options
  22. this.options = $.extend({}, $.noty.defaults, options);
  23. this.options.layout = (this.options.custom) ? $.noty.layouts['inline'] : $.noty.layouts[this.options.layout];
  24. this.options.theme = $.noty.themes[this.options.theme];
  25. delete options.layout, delete options.theme;
  26. this.options = $.extend({}, this.options, this.options.layout.options);
  27. this.options.id = 'noty_' + (new Date().getTime() * Math.floor(Math.random()* 1000000));
  28. this.options = $.extend({}, this.options, options);
  29. // Build the noty dom initial structure
  30. this._build();
  31. // return this so we can chain/use the bridge with less code.
  32. return this;
  33. }, // end init
  34. _build: function() {
  35. // Generating noty bar
  36. var $bar = $('<div class="noty_bar"/>').attr('id', this.options.id);
  37. $bar.append(this.options.template).find('.noty_text').html(this.options.text);
  38. this.$bar = (this.options.layout.parent.object !== null) ? $(this.options.layout.parent.object).css(this.options.layout.parent.css).append($bar) : $bar;
  39. // Set buttons if available
  40. if (this.options.buttons) {
  41. // If we have button disable closeWith & timeout options
  42. this.options.closeWith = [], this.options.timeout = false;
  43. var $buttons = $('<div/>').addClass('noty_buttons');
  44. (this.options.layout.parent.object !== null) ? this.$bar.find('.noty_bar').append($buttons) : this.$bar.append($buttons);
  45. var self = this;
  46. $.each(this.options.buttons, function(i, button) {
  47. var $button = $('<button/>').addClass((button.addClass) ? button.addClass : 'gray').html(button.text)
  48. .appendTo(self.$bar.find('.noty_buttons'))
  49. .bind('click', function(e) { if ($.isFunction(button.onClick)) { button.onClick.call($button, self); } });
  50. });
  51. }
  52. // For easy access
  53. this.$message = this.$bar.find('.noty_message');
  54. this.$closeButton = this.$bar.find('.noty_close');
  55. this.$buttons = this.$bar.find('.noty_buttons');
  56. $.noty.store[this.options.id] = this; // store noty for api
  57. }, // end _build
  58. show: function() {
  59. var self = this;
  60. $(self.options.layout.container.selector).append(self.$bar);
  61. self.options.theme.style.apply(self);
  62. ($.type(self.options.layout.css) === 'function') ? this.options.layout.css.apply(self.$bar) : self.$bar.css(this.options.layout.css || {});
  63. self.$bar.addClass(self.options.layout.addClass);
  64. self.options.layout.container.style.apply($(self.options.layout.container.selector));
  65. self.options.theme.callback.onShow.apply(this);
  66. if ($.inArray('click', self.options.closeWith) > -1)
  67. self.$bar.css('cursor', 'pointer').one('click', function() { self.close(); });
  68. if ($.inArray('hover', self.options.closeWith) > -1)
  69. self.$bar.one('mouseenter', function() { self.close(); });
  70. if ($.inArray('button', self.options.closeWith) > -1)
  71. self.$closeButton.one('click', function() { self.close(); });
  72. if ($.inArray('button', self.options.closeWith) == -1)
  73. self.$closeButton.remove();
  74. if (self.options.callback.onShow)
  75. self.options.callback.onShow.apply(self);
  76. self.$bar.animate(
  77. self.options.animation.open,
  78. self.options.animation.speed,
  79. self.options.animation.easing,
  80. function() {
  81. if (self.options.callback.afterShow) self.options.callback.afterShow.apply(self);
  82. self.shown = true;
  83. });
  84. // If noty is have a timeout option
  85. if (self.options.timeout)
  86. self.$bar.delay(self.options.timeout).promise().done(function() { self.close(); });
  87. return this;
  88. }, // end show
  89. close: function() {
  90. if (this.closed) return;
  91. var self = this;
  92. if (!this.shown) { // If we are still waiting in the queue just delete from queue
  93. $.each($.noty.queue, function(i, n) {
  94. if (n.options.id == self.options.id) {
  95. $.noty.queue.splice(i, 1);
  96. }
  97. });
  98. return;
  99. }
  100. self.$bar.addClass('i-am-closing-now');
  101. if (self.options.callback.onClose) { self.options.callback.onClose.apply(self); }
  102. self.$bar.clearQueue().stop().animate(
  103. self.options.animation.close,
  104. self.options.animation.speed,
  105. self.options.animation.easing,
  106. function() { if (self.options.callback.afterClose) self.options.callback.afterClose.apply(self); })
  107. .promise().done(function() {
  108. // Modal Cleaning
  109. if (self.options.modal) {
  110. $.notyRenderer.setModalCount(-1);
  111. if ($.notyRenderer.getModalCount() == 0) $('.noty_modal').fadeOut('fast', function() { $(this).remove(); });
  112. }
  113. // Layout Cleaning
  114. $.notyRenderer.setLayoutCountFor(self, -1);
  115. if ($.notyRenderer.getLayoutCountFor(self) == 0) $(self.options.layout.container.selector).remove();
  116. self.$bar.remove();
  117. self.$bar = null;
  118. self.closed = true;
  119. delete $.noty.store[self.options.id]; // deleting noty from store
  120. self.options.theme.callback.onClose.apply(self);
  121. if (!self.options.dismissQueue) {
  122. // Queue render
  123. $.noty.ontap = true;
  124. $.notyRenderer.render();
  125. }
  126. });
  127. }, // end close
  128. setText: function(text) {
  129. if (!this.closed) {
  130. this.options.text = text;
  131. this.$bar.find('.noty_text').html(text);
  132. }
  133. return this;
  134. },
  135. setType: function(type) {
  136. if (!this.closed) {
  137. this.options.type = type;
  138. this.options.theme.style.apply(this);
  139. this.options.theme.callback.onShow.apply(this);
  140. }
  141. return this;
  142. },
  143. closed: false,
  144. shown: false
  145. }; // end NotyObject
  146. $.notyRenderer = {};
  147. $.notyRenderer.init = function(options) {
  148. // Renderer creates a new noty
  149. var notification = Object.create(NotyObject).init(options);
  150. (notification.options.force) ? $.noty.queue.unshift(notification) : $.noty.queue.push(notification);
  151. $.notyRenderer.render();
  152. return ($.noty.returns == 'object') ? notification : notification.options.id;
  153. };
  154. $.notyRenderer.render = function() {
  155. var instance = $.noty.queue[0];
  156. if ($.type(instance) === 'object') {
  157. if (instance.options.dismissQueue) {
  158. $.notyRenderer.show($.noty.queue.shift());
  159. } else {
  160. if ($.noty.ontap) {
  161. $.notyRenderer.show($.noty.queue.shift());
  162. $.noty.ontap = false;
  163. }
  164. }
  165. } else {
  166. $.noty.ontap = true; // Queue is over
  167. }
  168. };
  169. $.notyRenderer.show = function(notification) {
  170. if (notification.options.modal) {
  171. $.notyRenderer.createModalFor(notification);
  172. $.notyRenderer.setModalCount(+1);
  173. }
  174. // Where is the container?
  175. if ($(notification.options.layout.container.selector).length == 0) {
  176. if (notification.options.custom) {
  177. notification.options.custom.append($(notification.options.layout.container.object).addClass('i-am-new'));
  178. } else {
  179. $('body').append($(notification.options.layout.container.object).addClass('i-am-new'));
  180. }
  181. } else {
  182. $(notification.options.layout.container.selector).removeClass('i-am-new');
  183. }
  184. $.notyRenderer.setLayoutCountFor(notification, +1);
  185. notification.show();
  186. };
  187. $.notyRenderer.createModalFor = function(notification) {
  188. if ($('.noty_modal').length == 0)
  189. $('<div/>').addClass('noty_modal').data('noty_modal_count', 0).css(notification.options.theme.modal.css).prependTo($('body')).fadeIn('fast');
  190. };
  191. $.notyRenderer.getLayoutCountFor = function(notification) {
  192. return $(notification.options.layout.container.selector).data('noty_layout_count') || 0;
  193. };
  194. $.notyRenderer.setLayoutCountFor = function(notification, arg) {
  195. return $(notification.options.layout.container.selector).data('noty_layout_count', $.notyRenderer.getLayoutCountFor(notification) + arg);
  196. };
  197. $.notyRenderer.getModalCount = function() {
  198. return $('.noty_modal').data('noty_modal_count') || 0;
  199. };
  200. $.notyRenderer.setModalCount = function(arg) {
  201. return $('.noty_modal').data('noty_modal_count', $.notyRenderer.getModalCount() + arg);
  202. };
  203. // This is for custom container
  204. $.fn.noty = function(options) {
  205. return this.each(function() {
  206. options.custom = $(this);
  207. return $.notyRenderer.init(options);
  208. });
  209. };
  210. $.noty = {};
  211. $.noty.queue = [];
  212. $.noty.ontap = true;
  213. $.noty.layouts = {};
  214. $.noty.themes = {};
  215. $.noty.returns = 'object';
  216. $.noty.store = {};
  217. $.noty.get = function(id) {
  218. return $.noty.store.hasOwnProperty(id) ? $.noty.store[id] : false;
  219. };
  220. $.noty.close = function(id) {
  221. return $.noty.get(id) ? $.noty.get(id).close() : false;
  222. };
  223. $.noty.setText = function(id, text) {
  224. return $.noty.get(id) ? $.noty.get(id).setText(text) : false;
  225. };
  226. $.noty.setType = function(id, type) {
  227. return $.noty.get(id) ? $.noty.get(id).setType(type) : false;
  228. };
  229. $.noty.clearQueue = function() {
  230. $.noty.queue = [];
  231. };
  232. $.noty.closeAll = function() {
  233. $.noty.clearQueue();
  234. $.each($.noty.store, function(id, noty) {
  235. noty.close();
  236. });
  237. };
  238. var windowAlert = window.alert;
  239. $.noty.consumeAlert = function(options) {
  240. window.alert = function(text) {
  241. if (options)
  242. options.text = text;
  243. else
  244. options = {text:text};
  245. $.notyRenderer.init(options);
  246. };
  247. };
  248. $.noty.stopConsumeAlert = function(){
  249. window.alert = windowAlert;
  250. };
  251. $.noty.defaults = {
  252. layout: 'top',
  253. theme: 'default',
  254. type: 'alert',
  255. text: '',
  256. dismissQueue: true,
  257. template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
  258. animation: {
  259. open: {height: 'toggle'},
  260. close: {height: 'toggle'},
  261. easing: 'swing',
  262. speed: 500
  263. },
  264. timeout: false,
  265. force: false,
  266. modal: false,
  267. closeWith: ['click'],
  268. callback: {
  269. onShow: function() {},
  270. afterShow: function() {},
  271. onClose: function() {},
  272. afterClose: function() {}
  273. },
  274. buttons: false
  275. };
  276. $(window).resize(function() {
  277. $.each($.noty.layouts, function(index, layout) {
  278. layout.container.style.apply($(layout.container.selector));
  279. });
  280. });
  281. })(jQuery);
  282. // Helpers
  283. function noty(options) {
  284. // This is for BC - Will be deleted on v2.2.0
  285. var using_old = 0
  286. , old_to_new = {
  287. 'animateOpen': 'animation.open',
  288. 'animateClose': 'animation.close',
  289. 'easing': 'animation.easing',
  290. 'speed': 'animation.speed',
  291. 'onShow': 'callback.onShow',
  292. 'onShown': 'callback.afterShow',
  293. 'onClose': 'callback.onClose',
  294. 'onClosed': 'callback.afterClose'
  295. }
  296. $.each(options, function(key, value) {
  297. if (old_to_new[key]) {
  298. using_old++;
  299. var _new = old_to_new[key].split('.');
  300. if (!options[_new[0]]) options[_new[0]] = {};
  301. options[_new[0]][_new[1]] = (value) ? value : function() {};
  302. delete options[key];
  303. }
  304. });
  305. if (!options.closeWith) {
  306. options.closeWith = $.noty.defaults.closeWith;
  307. }
  308. if (options.hasOwnProperty('closeButton')) {
  309. using_old++;
  310. if (options.closeButton) options.closeWith.push('button');
  311. delete options.closeButton;
  312. }
  313. if (options.hasOwnProperty('closeOnSelfClick')) {
  314. using_old++;
  315. if (options.closeOnSelfClick) options.closeWith.push('click');
  316. delete options.closeOnSelfClick;
  317. }
  318. if (options.hasOwnProperty('closeOnSelfOver')) {
  319. using_old++;
  320. if (options.closeOnSelfOver) options.closeWith.push('hover');
  321. delete options.closeOnSelfOver;
  322. }
  323. if (options.hasOwnProperty('custom')) {
  324. using_old++;
  325. if (options.custom.container != 'null') options.custom = options.custom.container;
  326. }
  327. if (options.hasOwnProperty('cssPrefix')) {
  328. using_old++;
  329. delete options.cssPrefix;
  330. }
  331. if (options.theme == 'noty_theme_default') {
  332. using_old++;
  333. options.theme = 'default';
  334. }
  335. if (!options.hasOwnProperty('dismissQueue')) {
  336. if (options.layout == 'topLeft'
  337. || options.layout == 'topRight'
  338. || options.layout == 'bottomLeft'
  339. || options.layout == 'bottomRight') {
  340. options.dismissQueue = true;
  341. } else {
  342. options.dismissQueue = false;
  343. }
  344. }
  345. if (options.buttons) {
  346. $.each(options.buttons, function(i, button) {
  347. if (button.click) {
  348. using_old++;
  349. button.onClick = button.click;
  350. delete button.click;
  351. }
  352. if (button.type) {
  353. using_old++;
  354. button.addClass = button.type;
  355. delete button.type;
  356. }
  357. });
  358. }
  359. if (using_old) console.warn('You are using noty v2 with v1.x.x options. @deprecated until v2.2.0 - Please update your options.');
  360. // console.log(options);
  361. // End of the BC
  362. return jQuery.notyRenderer.init(options);
  363. }