PageRenderTime 36ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/queenAdmin/assets/js/plugins/jquery-gritter/jquery.gritter.custom.js

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