Lungo.Notification.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Notification system in CSS3
  3. *
  4. * @namespace Lungo
  5. * @class Notification
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. Lungo.Notification = (function(lng, undefined) {
  10. var _options = [];
  11. var _el = null;
  12. var _window = null;
  13. var DELAY_TIME = 1;
  14. var ANIMATION_MILISECONDS = 200;
  15. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  16. var BINDING = lng.Constants.BINDING;
  17. var SELECTOR = {
  18. BODY: 'body',
  19. NOTIFICATION: '.notification',
  20. MODAL: '.notification .window',
  21. MODAL_HREF: '.notification .window a',
  22. WINDOW_CLOSABLE: '.notification > .url .close',
  23. CONFIRM_BUTTONS: '.notification .confirm a.button'
  24. };
  25. var STYLE = {
  26. MODAL: 'modal',
  27. VISIBLE: 'visible',
  28. SHOW: 'show',
  29. WORKING: 'working',
  30. INPUT: 'input'
  31. };
  32. var CALLBACK_HIDE = 'Lungo.Notification.hide()';
  33. var MARKUP_NOTIFICATION = '<div class="notification"><div class="window"></div></div>';
  34. /**
  35. *
  36. */
  37. var show = function(title, description, icon, animate, seconds, callback) {
  38. _new_instance(true, animate);
  39. _show(_markup(title, description, icon));
  40. _hide(seconds, callback);
  41. };
  42. /**
  43. *
  44. */
  45. var hide = function() {
  46. _window.removeClass('show');
  47. setTimeout(function() {
  48. _el.style('display', 'none').removeClass('url').removeClass('confirm').removeClass('modal');
  49. }, ANIMATION_MILISECONDS - 50);
  50. };
  51. /**
  52. *
  53. */
  54. var confirm = function(options) {
  55. _options = options;
  56. _new_instance(true);
  57. var markup = '<p>' + _markup(options.title, options.description, options.icon) + '</p><hr/>';
  58. markup += _button_markup(options.accept, 'accept');
  59. markup += _button_markup(options.cancel, 'cancel');
  60. _window.addClass('special confirm');
  61. _show(markup);
  62. };
  63. /**
  64. *
  65. */
  66. var alert = function(title, description, icon, seconds, callback) {
  67. _notify(title, description, icon, 'alert', seconds, callback);
  68. };
  69. /**
  70. *
  71. */
  72. var success = function(title, description, icon, seconds, callback) {
  73. _notify(title, description, icon, 'success', seconds, callback);
  74. };
  75. /**
  76. *
  77. */
  78. var error = function(title, description, icon, seconds, callback) {
  79. _notify(title, description, icon, 'error', seconds, callback);
  80. };
  81. /**
  82. *
  83. */
  84. var _notify = function(title, description, icon, type, seconds, callback) {
  85. _new_instance(false);
  86. _window.addClass(type || 'info').addClass('special notify');
  87. _show(_markup(title, description, icon));
  88. _hide(seconds, callback);
  89. };
  90. /**
  91. *
  92. */
  93. var html = function(markup, closable) {
  94. _new_instance(true);
  95. _window.addClass('url');
  96. markup += (closable) ? '<span class="icon close"></span>' : '';
  97. _show(markup);
  98. };
  99. /**
  100. *
  101. */
  102. var loading = function() {
  103. _new_instance(true);
  104. var data_loading = lng.Attributes.Data.Loading.html;
  105. var html_binded = data_loading.replace(BINDING.START + BINDING.KEY + BINDING.END, 'icon loading white');
  106. _show(html_binded);
  107. };
  108. var _init = function() {
  109. lng.dom(SELECTOR.BODY).append(MARKUP_NOTIFICATION);
  110. _el = lng.dom(SELECTOR.NOTIFICATION);
  111. _window = _el.children('.window');
  112. _subscribeEvents();
  113. };
  114. var _new_instance = function(modal, animate) {
  115. _el.style('display') === 'none' && _el.show();
  116. modal && _el.addClass(STYLE.MODAL) || _el.removeClass(STYLE.MODAL);
  117. _window.removeClass(STYLE.SHOW).removeClass(STYLE.WORKING);
  118. _window.removeClass('url').removeClass('notify').removeClass('confirm').removeClass('special').removeClass('working');
  119. _window.removeClass('error').removeClass('alert').removeClass('success');
  120. if (animate) {
  121. _window.addClass(STYLE.WORKING);
  122. }
  123. };
  124. var _show = function(html) {
  125. _window.html(html);
  126. setTimeout(function() {
  127. _window.addClass(STYLE.SHOW);
  128. }, DELAY_TIME);
  129. };
  130. var _hide = function(seconds, callback) {
  131. if (seconds !== undefined && seconds !== 0) {
  132. var miliseconds = seconds * 1000;
  133. setTimeout(function() {
  134. hide();
  135. // if (callback) callback.apply(callback);
  136. if (callback) setTimeout(callback, ANIMATION_MILISECONDS);
  137. }, miliseconds);
  138. }
  139. };
  140. var _markup = function(title, description, icon) {
  141. description = !description ? "&nbsp;" : description;
  142. return '<span class="icon ' + icon + '"></span><strong>' + title + '</strong><small>' + description + '</small>';
  143. };
  144. var _button_markup = function(options, callback) {
  145. return '<a href="#" data-callback="' + callback + '" class="button ' + options.color + '" data-icon="' + options.icon + '">' + options.label + '</a>';
  146. };
  147. var _subscribeEvents = function() {
  148. _window.tap(function(event) {
  149. if (_window.hasClass('notify')) {
  150. hide();
  151. }
  152. });
  153. lng.dom(SELECTOR.CONFIRM_BUTTONS).tap(function(event) {
  154. var button = lng.dom(this);
  155. var callback = _options[button.data('callback')].callback;
  156. if (callback) callback.call(callback);
  157. hide();
  158. });
  159. lng.dom(SELECTOR.WINDOW_CLOSABLE).tap( hide );
  160. };
  161. _init();
  162. return {
  163. show: show,
  164. hide: hide,
  165. error: error,
  166. alert: alert,
  167. success: success,
  168. confirm: confirm,
  169. html: html,
  170. loading: loading
  171. };
  172. })(Lungo);