libraryJQueryGrowl

Hosted version of Kevin Sylvestre's growl library

当前为 2018-04-04 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/40055/263232/libraryJQueryGrowl.js

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