You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

196 lines
7.0 KiB

  1. /**
  2. * @author Will Steinmetz
  3. *
  4. * jQuery notification plug-in inspired by the notification style of Windows 8
  5. *
  6. * Copyright (c)2013, Will Steinmetz
  7. * Licensed under the BSD license.
  8. * http://opensource.org/licenses/BSD-3-Clause
  9. */
  10. ;(function($) {
  11. var settings = {
  12. life: 10000,
  13. theme: 'teal',
  14. sticky: false,
  15. verticalEdge: 'right',
  16. horizontalEdge: 'top',
  17. zindex: 1100
  18. };
  19. var methods = {
  20. init: function(message, options) {
  21. return this.each(function() {
  22. var $this = $(this),
  23. data = $this.data('notific8');
  24. $this.data('notific8', {
  25. target: $this,
  26. settings: {},
  27. message: ""
  28. });
  29. data = $this.data('notific8');
  30. data.message = message;
  31. // apply the options
  32. $.extend(data.settings, settings, options);
  33. // add the notification to the stack
  34. methods._buildNotification($this);
  35. });
  36. },
  37. /**
  38. * Destroy the notification
  39. */
  40. destroy: function($this) {
  41. var data = $this.data('notific8');
  42. $(window).unbind('.notific8');
  43. $this.removeData('notific8');
  44. },
  45. /**
  46. * Build the notification and add it to the screen's stack
  47. */
  48. _buildNotification: function($this) {
  49. var data = $this.data('notific8'),
  50. notification = $('<div />'),
  51. num = Number($('body').attr('data-notific8s'));
  52. num++;
  53. notification.addClass('jquery-notific8-notification').addClass(data.settings.theme);
  54. notification.attr('id', 'jquery-notific8-notification-' + num);
  55. $('body').attr('data-notific8s', num);
  56. // check for a heading
  57. if (data.settings.hasOwnProperty('heading') && (typeof data.settings.heading == "string")) {
  58. notification.append($('<div />').addClass('jquery-notific8-heading').html(data.settings.heading));
  59. }
  60. // check if the notification is supposed to be sticky
  61. if (data.settings.sticky) {
  62. var close = $('<div />').addClass('jquery-notific8-close-sticky').append(
  63. $('<span />').html('close x')
  64. );
  65. close.click(function(event) {
  66. notification.animate({width: 'hide'}, {
  67. duration: 'fast',
  68. complete: function() {
  69. notification.remove();
  70. }
  71. });
  72. });
  73. notification.append(close);
  74. notification.addClass('sticky');
  75. }
  76. // otherwise, put the normal close button up that is only display
  77. // when the notification is hovered over
  78. else {
  79. var close = $('<div />').addClass('jquery-notific8-close').append(
  80. $('<span />').html('X')
  81. );
  82. close.click(function(event) {
  83. notification.animate({width: 'hide'}, {
  84. duration: 'fast',
  85. complete: function() {
  86. notification.remove();
  87. }
  88. });
  89. });
  90. notification.append(close);
  91. }
  92. // add the message
  93. notification.append($('<div />').addClass('jquery-notific8-message').html(data.message));
  94. // add the notification to the stack
  95. $('.jquery-notific8-container.' + data.settings.verticalEdge + '.' + data.settings.horizontalEdge).append(notification);
  96. // slide the message onto the screen
  97. notification.animate({width: 'show'}, {
  98. duration: 'fast',
  99. complete: function() {
  100. if (!data.settings.sticky) {
  101. (function(n, l) {
  102. setTimeout(function() {
  103. n.animate({width: 'hide'}, {
  104. duration: 'fast',
  105. complete: function() {
  106. n.remove();
  107. }
  108. });
  109. }, l);
  110. })(notification, data.settings.life);
  111. }
  112. data.settings = {};
  113. }
  114. });
  115. },
  116. /**
  117. * Set up the configuration settings
  118. */
  119. configure: function(options) {
  120. $.extend(settings, options);
  121. },
  122. /**
  123. * Set up the z-index
  124. */
  125. zindex: function(zindex) {
  126. settings.zindex = zindex;
  127. }
  128. };
  129. // wrapper since this plug-in is called without selecting an item first
  130. $.notific8 = function(message, options) {
  131. switch (message) {
  132. case 'configure':
  133. case 'config':
  134. return methods.configure.apply(this, [options]);
  135. break;
  136. case 'zindex':
  137. return methods.zindex.apply(this, [options]);
  138. break;
  139. default:
  140. if (typeof options == 'undefined') {
  141. options = {};
  142. }
  143. // make sure that the stack containers exist
  144. if ($('.jquery-notific8-container').size() === 0) {
  145. var $body = $('body');
  146. $body.attr('data-notific8s', 0);
  147. $body.append($('<div />').addClass('jquery-notific8-container').addClass('top').addClass('right'));
  148. $body.append($('<div />').addClass('jquery-notific8-container').addClass('top').addClass('left'));
  149. $body.append($('<div />').addClass('jquery-notific8-container').addClass('bottom').addClass('right'));
  150. $body.append($('<div />').addClass('jquery-notific8-container').addClass('bottom').addClass('left'));
  151. $('.jquery-notific8-container').css('z-index', settings.zindex);
  152. }
  153. // make sure the edge settings exist
  154. if ((!options.hasOwnProperty('verticalEdge')) || ((options.verticalEdge.toLowerCase() != 'right') && (options.verticalEdge.toLowerCase() != 'left'))) {
  155. options.verticalEdge = settings.verticalEdge;
  156. }
  157. if ((!options.hasOwnProperty('horizontalEdge')) || ((options.horizontalEdge.toLowerCase() != 'top') && (options.horizontalEdge.toLowerCase() != 'bottom'))) {
  158. options.horizontalEdge = settings.horizontalEdge;
  159. }
  160. options.verticalEdge = options.verticalEdge.toLowerCase();
  161. options.horizontalEdge = options.horizontalEdge.toLowerCase();
  162. //display the notification in the right corner
  163. $('.jquery-notific8-container.' + options.verticalEdge + '.' + options.horizontalEdge).notific8(message, options);
  164. break;
  165. }
  166. };
  167. // plugin setup
  168. $.fn.notific8 = function(message, options) {
  169. if (typeof message == "string") {
  170. return methods.init.apply(this, arguments);
  171. } else {
  172. $.error('jQuery.notific8 takes a string message as the first parameter');
  173. }
  174. };
  175. })(jQuery);