jquery.growl.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. var _createClass = function() {
  2. function defineProperties(target, props) {
  3. for (var i = 0; i < props.length; i++) {
  4. var descriptor = props[i];
  5. descriptor.enumerable = descriptor.enumerable || false;
  6. descriptor.configurable = true;
  7. if ("value" in descriptor) descriptor.writable = true;
  8. Object.defineProperty(target, descriptor.key, descriptor);
  9. }
  10. }
  11. return function(Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; };
  12. }();
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. // Generated by CoffeeScript 2.1.0
  15. (function() {
  16. /*
  17. jQuery Growl
  18. Copyright 2015 Kevin Sylvestre
  19. 1.3.5
  20. */
  21. "use strict";
  22. var $, Animation, Growl;
  23. $ = jQuery;
  24. Animation = function() {
  25. var Animation = function() {
  26. function Animation() {
  27. _classCallCheck(this, Animation);
  28. }
  29. _createClass(Animation, null, [{
  30. key: "transition",
  31. value: function transition($el) {
  32. var el, ref, result, type;
  33. el = $el[0];
  34. ref = this.transitions;
  35. for (type in ref) {
  36. result = ref[type];
  37. if (el.style[type] != null) {
  38. return result;
  39. }
  40. }
  41. }
  42. }]);
  43. return Animation;
  44. }();
  45. ;
  46. Animation.transitions = {
  47. "webkitTransition": "webkitTransitionEnd",
  48. "mozTransition": "mozTransitionEnd",
  49. "oTransition": "oTransitionEnd",
  50. "transition": "transitionend"
  51. };
  52. return Animation;
  53. }();
  54. Growl = function() {
  55. var Growl = function() {
  56. _createClass(Growl, null, [{
  57. key: "growl",
  58. value: function growl() {
  59. var settings = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  60. return new Growl(settings);
  61. }
  62. }]);
  63. function Growl() {
  64. var settings = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  65. _classCallCheck(this, Growl);
  66. this.render = this.render.bind(this);
  67. this.bind = this.bind.bind(this);
  68. this.unbind = this.unbind.bind(this);
  69. this.mouseEnter = this.mouseEnter.bind(this);
  70. this.mouseLeave = this.mouseLeave.bind(this);
  71. this.click = this.click.bind(this);
  72. this.close = this.close.bind(this);
  73. this.cycle = this.cycle.bind(this);
  74. this.waitAndDismiss = this.waitAndDismiss.bind(this);
  75. this.present = this.present.bind(this);
  76. this.dismiss = this.dismiss.bind(this);
  77. this.remove = this.remove.bind(this);
  78. this.animate = this.animate.bind(this);
  79. this.$growls = this.$growls.bind(this);
  80. this.$growl = this.$growl.bind(this);
  81. this.html = this.html.bind(this);
  82. this.content = this.content.bind(this);
  83. this.container = this.container.bind(this);
  84. this.settings = $.extend({}, Growl.settings, settings);
  85. this.initialize(this.settings.location);
  86. this.render();
  87. }
  88. _createClass(Growl, [{
  89. key: "initialize",
  90. value: function initialize(location) {
  91. var id;
  92. id = 'growls-' + location;
  93. return $('body:not(:has(#' + id + '))').append('<div id="' + id + '" />');
  94. }
  95. }, {
  96. key: "render",
  97. value: function render() {
  98. var $growl;
  99. $growl = this.$growl();
  100. this.$growls(this.settings.location).append($growl);
  101. if (this.settings.fixed) {
  102. this.present();
  103. } else {
  104. this.cycle();
  105. }
  106. }
  107. }, {
  108. key: "bind",
  109. value: function bind() {
  110. var $growl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.$growl();
  111. $growl.on("click", this.click);
  112. if (this.settings.delayOnHover) {
  113. $growl.on("mouseenter", this.mouseEnter);
  114. $growl.on("mouseleave", this.mouseLeave);
  115. }
  116. return $growl.on("contextmenu", this.close).find("." + this.settings.namespace + "-close").on("click", this.close);
  117. }
  118. }, {
  119. key: "unbind",
  120. value: function unbind() {
  121. var $growl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.$growl();
  122. $growl.off("click", this.click);
  123. if (this.settings.delayOnHover) {
  124. $growl.off("mouseenter", this.mouseEnter);
  125. $growl.off("mouseleave", this.mouseLeave);
  126. }
  127. return $growl.off("contextmenu", this.close).find("." + this.settings.namespace + "-close").off("click", this.close);
  128. }
  129. }, {
  130. key: "mouseEnter",
  131. value: function mouseEnter(event) {
  132. var $growl;
  133. $growl = this.$growl();
  134. return $growl.stop(true, true);
  135. }
  136. }, {
  137. key: "mouseLeave",
  138. value: function mouseLeave(event) {
  139. return this.waitAndDismiss();
  140. }
  141. }, {
  142. key: "click",
  143. value: function click(event) {
  144. if (this.settings.url != null) {
  145. event.preventDefault();
  146. event.stopPropagation();
  147. return window.open(this.settings.url);
  148. }
  149. }
  150. }, {
  151. key: "close",
  152. value: function close(event) {
  153. var $growl;
  154. event.preventDefault();
  155. event.stopPropagation();
  156. $growl = this.$growl();
  157. return $growl.stop().queue(this.dismiss).queue(this.remove);
  158. }
  159. }, {
  160. key: "cycle",
  161. value: function cycle() {
  162. var $growl;
  163. $growl = this.$growl();
  164. return $growl.queue(this.present).queue(this.waitAndDismiss());
  165. }
  166. }, {
  167. key: "waitAndDismiss",
  168. value: function waitAndDismiss() {
  169. var $growl;
  170. $growl = this.$growl();
  171. return $growl.delay(this.settings.duration).queue(this.dismiss).queue(this.remove);
  172. }
  173. }, {
  174. key: "present",
  175. value: function present(callback) {
  176. var $growl;
  177. $growl = this.$growl();
  178. this.bind($growl);
  179. return this.animate($growl, this.settings.namespace + "-incoming", 'out', callback);
  180. }
  181. }, {
  182. key: "dismiss",
  183. value: function dismiss(callback) {
  184. var $growl;
  185. $growl = this.$growl();
  186. this.unbind($growl);
  187. return this.animate($growl, this.settings.namespace + "-outgoing", 'in', callback);
  188. }
  189. }, {
  190. key: "remove",
  191. value: function remove(callback) {
  192. this.$growl().remove();
  193. return typeof callback === "function" ? callback() : void 0;
  194. }
  195. }, {
  196. key: "animate",
  197. value: function animate($element, name) {
  198. var direction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'in';
  199. var callback = arguments[3];
  200. var transition;
  201. transition = Animation.transition($element);
  202. $element[direction === 'in' ? 'removeClass' : 'addClass'](name);
  203. $element.offset().position;
  204. $element[direction === 'in' ? 'addClass' : 'removeClass'](name);
  205. if (callback == null) {
  206. return;
  207. }
  208. if (transition != null) {
  209. $element.one(transition, callback);
  210. } else {
  211. callback();
  212. }
  213. }
  214. }, {
  215. key: "$growls",
  216. value: function $growls(location) {
  217. var base;
  218. if (this.$_growls == null) {
  219. this.$_growls = [];
  220. }
  221. return (base = this.$_growls)[location] != null ? base[location] : base[location] = $('#growls-' + location);
  222. }
  223. }, {
  224. key: "$growl",
  225. value: function $growl() {
  226. return this.$_growl != null ? this.$_growl : this.$_growl = $(this.html());
  227. }
  228. }, {
  229. key: "html",
  230. value: function html() {
  231. return this.container(this.content());
  232. }
  233. }, {
  234. key: "content",
  235. value: function content() {
  236. return "<div class='" + this.settings.namespace + "-close'>" + this.settings.close + "</div>\n<div class='" + this.settings.namespace + "-title'>" + this.settings.title + "</div>\n<div class='" + this.settings.namespace + "-message'>" + this.settings.message + "</div>";
  237. }
  238. }, {
  239. key: "container",
  240. value: function container(content) {
  241. return "<div class='" + this.settings.namespace + " " + this.settings.namespace + "-" + this.settings.style + " " + this.settings.namespace + "-" + this.settings.size + "'>\n " + content + "\n</div>";
  242. }
  243. }]);
  244. return Growl;
  245. }();
  246. ;
  247. Growl.settings = {
  248. namespace: 'growl',
  249. duration: 3200,
  250. close: "&#215;",
  251. location: "default",
  252. style: "default",
  253. size: "medium",
  254. delayOnHover: true
  255. };
  256. return Growl;
  257. }();
  258. this.Growl = Growl;
  259. $.growl = function() {
  260. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  261. return Growl.growl(options);
  262. };
  263. $.growl.error = function() {
  264. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  265. var settings;
  266. settings = {
  267. title: "Error!",
  268. style: "error"
  269. };
  270. return $.growl($.extend(settings, options));
  271. };
  272. $.growl.notice = function() {
  273. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  274. var settings;
  275. settings = {
  276. title: "Notice!",
  277. style: "notice"
  278. };
  279. return $.growl($.extend(settings, options));
  280. };
  281. $.growl.warning = function() {
  282. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  283. var settings;
  284. settings = {
  285. title: "Warning!",
  286. style: "warning"
  287. };
  288. return $.growl($.extend(settings, options));
  289. };
  290. $.growl.error1 = function() {
  291. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  292. var settings;
  293. settings = {
  294. title: "Error!",
  295. style: "error1"
  296. };
  297. return $.growl($.extend(settings, options));
  298. };
  299. $.growl.notice1 = function() {
  300. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  301. var settings;
  302. settings = {
  303. title: "Notice!",
  304. style: "notice1"
  305. };
  306. return $.growl($.extend(settings, options));
  307. };
  308. $.growl.warning1 = function() {
  309. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  310. var settings;
  311. settings = {
  312. title: "Warning!",
  313. style: "warning1"
  314. };
  315. return $.growl($.extend(settings, options));
  316. };
  317. }).call(this);