Lungo.Notification.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 [data-action=close], .notification > .error, .notification > .success',
  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, icon, seconds, callback) {
  38. var markup;
  39. if (title !== undefined) {
  40. markup = _markup(title, null, icon);
  41. } else {
  42. var data_loading = lng.Attributes.loading.html;
  43. markup = data_loading.replace(BINDING.START + BINDING.KEY + BINDING.END, 'icon white');
  44. }
  45. _show(markup, 'growl');
  46. _hide(seconds, callback);
  47. };
  48. /**
  49. *
  50. */
  51. var hide = function() {
  52. _window.removeClass('show');
  53. setTimeout(function() {
  54. _el.style('display', 'none').removeClass('html').removeClass('confirm').removeClass('notify').removeClass('growl');
  55. }, ANIMATION_MILISECONDS - 50);
  56. };
  57. /**
  58. *
  59. */
  60. var confirm = function(options) {
  61. _options = options;
  62. var markup = _markup(options.title, options.description, options.icon);
  63. markup += _button_markup(options.accept, 'accept');
  64. markup += _button_markup(options.cancel, 'cancel');
  65. _show(markup, 'confirm');
  66. };
  67. /**
  68. *
  69. */
  70. var success = function(title, description, icon, seconds, callback) {
  71. _notify(title, description, icon, 'success', seconds, callback);
  72. };
  73. /**
  74. *
  75. */
  76. var error = function(title, description, icon, seconds, callback) {
  77. _notify(title, description, icon, 'error', seconds, callback);
  78. };
  79. /**
  80. *
  81. */
  82. var _notify = function(title, description, icon, stylesheet, seconds, callback) {
  83. _show(_markup(title, description, icon), stylesheet);
  84. if (seconds) {
  85. _hide(seconds, callback);
  86. }
  87. };
  88. /**
  89. *
  90. */
  91. var html = function(markup, button) {
  92. markup += (button) ? '<a href="#" class="button large anchor" data-action="close">' + button + '</a>' : '';
  93. _show(markup, 'html');
  94. };
  95. var _init = function() {
  96. lng.dom(SELECTOR.BODY).append(MARKUP_NOTIFICATION);
  97. _el = lng.dom(SELECTOR.NOTIFICATION);
  98. _window = _el.children('.window');
  99. _subscribeEvents();
  100. };
  101. var _show = function(html, stylesheet) {
  102. _el.show();
  103. _window.removeClass(STYLE.SHOW);
  104. _window.removeClass('error').removeClass('success').removeClass('html').removeClass('growl');
  105. _window.addClass(stylesheet);
  106. _window.html(html);
  107. setTimeout(function() {
  108. _window.addClass(STYLE.SHOW);
  109. }, DELAY_TIME);
  110. };
  111. var _hide = function(seconds, callback) {
  112. if (seconds !== undefined && seconds !== 0) {
  113. var miliseconds = seconds * 1000;
  114. setTimeout(function() {
  115. hide();
  116. // if (callback) callback.apply(callback);
  117. if (callback) setTimeout(callback, ANIMATION_MILISECONDS);
  118. }, miliseconds);
  119. }
  120. };
  121. var _markup = function(title, description, icon) {
  122. description = !description ? "&nbsp;" : description;
  123. return '<span class="icon ' + icon + '"></span><strong class="text bold">' + title + '</strong><small>' + description + '</small>';
  124. };
  125. var _button_markup = function(options, callback) {
  126. return '<a href="#" data-callback="' + callback + '" class="button anchor large text thin">' + options.label + '</a>';
  127. };
  128. var _subscribeEvents = function() {
  129. lng.dom(SELECTOR.CONFIRM_BUTTONS).tap(function(event) {
  130. var button = lng.dom(this);
  131. var callback = _options[button.data('callback')].callback;
  132. if (callback) callback.call(callback);
  133. hide();
  134. });
  135. lng.dom(SELECTOR.WINDOW_CLOSABLE).tap( hide );
  136. };
  137. _init();
  138. return {
  139. show: show,
  140. hide: hide,
  141. error: error,
  142. success: success,
  143. confirm: confirm,
  144. html: html
  145. };
  146. })(Lungo);