PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Bespin/BespinEmbedded/plugins/thirdparty/gritter/index.js

https://bitbucket.org/raffel/node-host
JavaScript | 399 lines | 195 code | 88 blank | 116 comment | 43 complexity | 03cefed0bbc0e4855207ce32f6cee21b MD5 | raw file
  1. /*
  2. * Gritter for jQuery
  3. * http://www.boedesign.com/
  4. *
  5. * Copyright (c) 2009 Jordan Boesch
  6. * Dual licensed under the MIT and GPL licenses.
  7. *
  8. * Date: December 1, 2009
  9. * Version: 1.6
  10. */
  11. var $ = require('jquery').$;
  12. /**
  13. * Set it up as an object under the jQuery namespace
  14. */
  15. $.gritter = {};
  16. /**
  17. * Set up global options that the user can over-ride
  18. */
  19. $.gritter.options = {
  20. fade_in_speed: 'medium', // how fast notifications fade in
  21. fade_out_speed: 1000, // how fast the notices fade out
  22. time: 6000 // hang on the screen for...
  23. }
  24. /**
  25. * Add a gritter notification to the screen
  26. * @see Gritter#add();
  27. */
  28. $.gritter.add = function(params){
  29. try {
  30. return Gritter.add(params || {});
  31. } catch(e) {
  32. var err = 'Gritter Error: ' + e;
  33. (typeof(console) != 'undefined' && console.error) ?
  34. console.error(err, params) :
  35. alert(err);
  36. }
  37. }
  38. /**
  39. * Remove a gritter notification from the screen
  40. * @see Gritter#removeSpecific();
  41. */
  42. $.gritter.remove = function(id, params){
  43. Gritter.removeSpecific(id, params || {});
  44. }
  45. /**
  46. * Remove all notifications
  47. * @see Gritter#stop();
  48. */
  49. $.gritter.removeAll = function(params){
  50. Gritter.stop(params || {});
  51. }
  52. /**
  53. * Big fat Gritter object
  54. * @constructor (not really since it's object literal)
  55. */
  56. var Gritter = {
  57. // Public - options to over-ride with $.gritter.options in "add"
  58. fade_in_speed: '',
  59. fade_out_speed: '',
  60. time: '',
  61. // Private - no touchy the private parts
  62. _custom_timer: 0,
  63. _item_count: 0,
  64. _is_setup: 0,
  65. _tpl_close: '<div class="gritter-close"></div>',
  66. _tpl_item: '<div id="gritter-item-[[number]]" class="gritter-item-wrapper [[item_class]]" style="display:none"><div class="gritter-top"></div><div class="gritter-item">[[image]]<div class="[[class_name]]"><span class="gritter-title">[[username]]</span><p>[[text]]</p></div><div style="clear:both"></div></div><div class="gritter-bottom"></div></div>',
  67. _tpl_wrap: '<div id="gritter-notice-wrapper"></div>',
  68. /**
  69. * Add a gritter notification to the screen
  70. * @param {Object} params The object that contains all the options for drawing the notification
  71. * @return {Integer} The specific numeric id to that gritter notification
  72. */
  73. add: function(params){
  74. // We might have some issues if we don't have a title or text!
  75. if(!params.title || !params.text){
  76. throw 'You need to fill out the first 2 params: "title" and "text"';
  77. }
  78. // Check the options and set them once
  79. if(!this._is_setup){
  80. this._runSetup();
  81. }
  82. // Basics
  83. var user = params.title,
  84. text = params.text,
  85. image = params.image || '',
  86. sticky = params.sticky || false,
  87. item_class = params.class_name || '',
  88. time_alive = params.time || '';
  89. this._verifyWrapper();
  90. this._item_count++;
  91. var number = this._item_count,
  92. tmp = this._tpl_item;
  93. // Assign callbacks
  94. $(['before_open', 'after_open', 'before_close', 'after_close']).each(function(i, val){
  95. Gritter['_' + val + '_' + number] = ($.isFunction(params[val])) ? params[val] : function(){}
  96. });
  97. // Reset
  98. this._custom_timer = 0;
  99. // A custom fade time set
  100. if(time_alive){
  101. this._custom_timer = time_alive;
  102. }
  103. var image_str = (image != '') ? '<img src="' + image + '" class="gritter-image" />' : '',
  104. class_name = (image != '') ? 'gritter-with-image' : 'gritter-without-image';
  105. // String replacements on the template
  106. tmp = this._str_replace(
  107. ['[[username]]', '[[text]]', '[[image]]', '[[number]]', '[[class_name]]', '[[item_class]]'],
  108. [user, text, image_str, this._item_count, class_name, item_class], tmp
  109. );
  110. this['_before_open_' + number]();
  111. $('#gritter-notice-wrapper').append(tmp);
  112. var item = $('#gritter-item-' + this._item_count);
  113. item.fadeIn(this.fade_in_speed, function(){
  114. Gritter['_after_open_' + number]($(this));
  115. });
  116. if(!sticky){
  117. this._setFadeTimer(item, number);
  118. }
  119. // Bind the hover/unhover states
  120. $(item).bind('mouseenter mouseleave', function(event){
  121. if(event.type == 'mouseenter'){
  122. if(!sticky){
  123. Gritter._restoreItemIfFading($(this), number);
  124. }
  125. }
  126. else {
  127. if(!sticky){
  128. Gritter._setFadeTimer($(this), number);
  129. }
  130. }
  131. Gritter._hoverState($(this), event.type);
  132. });
  133. return number;
  134. },
  135. /**
  136. * If we don't have any more gritter notifications, get rid of the wrapper using this check
  137. * @private
  138. * @param {Integer} unique_id The ID of the element that was just deleted, use it for a callback
  139. * @param {Object} e The jQuery element that we're going to perform the remove() action on
  140. */
  141. _countRemoveWrapper: function(unique_id, e){
  142. // Remove it then run the callback function
  143. e.remove();
  144. this['_after_close_' + unique_id](e);
  145. // Check if the wrapper is empty, if it is.. remove the wrapper
  146. if($('.gritter-item-wrapper').length == 0){
  147. $('#gritter-notice-wrapper').remove();
  148. }
  149. },
  150. /**
  151. * Fade out an element after it's been on the screen for x amount of time
  152. * @private
  153. * @param {Object} e The jQuery element to get rid of
  154. * @param {Integer} unique_id The id of the element to remove
  155. * @param {Object} params An optional list of params to set fade speeds etc.
  156. * @param {Boolean} unbind_events Unbind the mouseenter/mouseleave events if they click (X)
  157. */
  158. _fade: function(e, unique_id, params, unbind_events){
  159. var params = params || {},
  160. fade = (typeof(params.fade) != 'undefined') ? params.fade : true;
  161. fade_out_speed = params.speed || this.fade_out_speed;
  162. this['_before_close_' + unique_id](e);
  163. // If this is true, then we are coming from clicking the (X)
  164. if(unbind_events){
  165. e.unbind('mouseenter mouseleave');
  166. }
  167. // Fade it out or remove it
  168. if(fade){
  169. e.animate({
  170. opacity: 0
  171. }, fade_out_speed, function(){
  172. e.animate({ height: 0 }, 300, function(){
  173. Gritter._countRemoveWrapper(unique_id, e);
  174. })
  175. })
  176. }
  177. else {
  178. this._countRemoveWrapper(unique_id, e);
  179. }
  180. },
  181. /**
  182. * Perform actions based on the type of bind (mouseenter, mouseleave)
  183. * @private
  184. * @param {Object} e The jQuery element
  185. * @param {String} type The type of action we're performing: mouseenter or mouseleave
  186. */
  187. _hoverState: function(e, type){
  188. // Change the border styles and add the (X) close button when you hover
  189. if(type == 'mouseenter'){
  190. e.addClass('hover');
  191. var find_img = e.find('img');
  192. // Insert the close button before what element
  193. (find_img.length) ?
  194. find_img.before(this._tpl_close) :
  195. e.find('span').before(this._tpl_close);
  196. // Clicking (X) makes the perdy thing close
  197. e.find('.gritter-close').click(function(){
  198. var unique_id = e.attr('id').split('-')[2];
  199. Gritter.removeSpecific(unique_id, {}, e, true);
  200. });
  201. }
  202. // Remove the border styles and (X) close button when you mouse out
  203. else {
  204. e.removeClass('hover');
  205. e.find('.gritter-close').remove();
  206. }
  207. },
  208. /**
  209. * Remove a specific notification based on an ID
  210. * @param {Integer} unique_id The ID used to delete a specific notification
  211. * @param {Object} params A set of options passed in to determine how to get rid of it
  212. * @param {Object} e The jQuery element that we're "fading" then removing
  213. * @param {Boolean} unbind_events If we clicked on the (X) we set this to true to unbind mouseenter/mouseleave
  214. */
  215. removeSpecific: function(unique_id, params, e, unbind_events){
  216. if(!e){
  217. var e = $('#gritter-item-' + unique_id);
  218. }
  219. // We set the fourth param to let the _fade function know to
  220. // unbind the "mouseleave" event. Once you click (X) there's no going back!
  221. this._fade(e, unique_id, params || {}, unbind_events);
  222. },
  223. /**
  224. * If the item is fading out and we hover over it, restore it!
  225. * @private
  226. * @param {Object} e The HTML element to remove
  227. * @param {Integer} unique_id The ID of the element
  228. */
  229. _restoreItemIfFading: function(e, unique_id){
  230. clearTimeout(this['_int_id_' + unique_id]);
  231. e.stop().css({ opacity: '' });
  232. },
  233. /**
  234. * Setup the global options - only once
  235. * @private
  236. */
  237. _runSetup: function(){
  238. for(opt in $.gritter.options){
  239. this[opt] = $.gritter.options[opt];
  240. }
  241. this._is_setup = 1;
  242. },
  243. /**
  244. * Set the notification to fade out after a certain amount of time
  245. * @private
  246. * @param {Object} item The HTML element we're dealing with
  247. * @param {Integer} unique_id The ID of the element
  248. */
  249. _setFadeTimer: function(e, unique_id){
  250. var timer_str = (this._custom_timer) ? this._custom_timer : this.time;
  251. this['_int_id_' + unique_id] = setTimeout(function(){
  252. Gritter._fade(e, unique_id);
  253. }, timer_str);
  254. },
  255. /**
  256. * Bring everything to a halt
  257. * @param {Object} params A list of callback functions to pass when all notifications are removed
  258. */
  259. stop: function(params){
  260. // callbacks (if passed)
  261. var before_close = ($.isFunction(params.before_close)) ? params.before_close : function(){};
  262. var after_close = ($.isFunction(params.after_close)) ? params.after_close : function(){};
  263. var wrap = $('#gritter-notice-wrapper');
  264. before_close(wrap);
  265. wrap.fadeOut(function(){
  266. $(this).remove();
  267. after_close();
  268. });
  269. },
  270. /**
  271. * An extremely handy PHP function ported to JS, works well for templating
  272. * @private
  273. * @param {String/Array} search A list of things to search for
  274. * @param {String/Array} replace A list of things to replace the searches with
  275. * @return {String} sa The output
  276. */
  277. _str_replace: function(search, replace, subject, count){
  278. var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
  279. f = [].concat(search),
  280. r = [].concat(replace),
  281. s = subject,
  282. ra = r instanceof Array, sa = s instanceof Array;
  283. s = [].concat(s);
  284. if(count){
  285. this.window[count] = 0;
  286. }
  287. for(i = 0, sl = s.length; i < sl; i++){
  288. if(s[i] === ''){
  289. continue;
  290. }
  291. for (j = 0, fl = f.length; j < fl; j++){
  292. temp = s[i] + '';
  293. repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
  294. s[i] = (temp).split(f[j]).join(repl);
  295. if(count && s[i] !== temp){
  296. this.window[count] += (temp.length-s[i].length) / f[j].length;
  297. }
  298. }
  299. }
  300. return sa ? s : s[0];
  301. },
  302. /**
  303. * A check to make sure we have something to wrap our notices with
  304. * @private
  305. */
  306. _verifyWrapper: function(){
  307. if($('#gritter-notice-wrapper').length == 0){
  308. $('body').append(this._tpl_wrap);
  309. }
  310. }
  311. }