lungo.sugar.growl.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Description or Responsability
  3. *
  4. * @namespace LUNGO.Sugar
  5. * @class Growl
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. LUNGO.Sugar.Growl = (function(lng, undefined) {
  10. var _options = [];
  11. var SELECTOR = {
  12. BODY: 'body',
  13. GROWL: '.growl',
  14. MODAL: '.growl .modal',
  15. NOTIFY: '.growl .notify',
  16. MODAL_HREF: '.growl .modal a'
  17. }
  18. var CSS_CLASS = {
  19. VISIBLE: 'visible',
  20. SHOW: 'show',
  21. HIDE: 'hide',
  22. WORKING: 'working',
  23. INPUT: 'input'
  24. }
  25. var CALLBACK_HIDE = 'LUNGO.Sugar.Growl.hide()';
  26. var MARKUP_GROWL = '<div class="growl"><div class="modal"></div><div class="notify"></div></div>';
  27. var show = function(title, icon, animate, seconds, callback) {
  28. _showGrowlModal(true);
  29. var modal = $(SELECTOR.MODAL);
  30. modal.removeClass(CSS_CLASS.SHOW);
  31. modal.removeClass(CSS_CLASS.INPUT);
  32. modal.html('<span class="big icon ' + icon + '"></span><strong>' + title + '<strong>');
  33. _animate(modal, animate);
  34. modal.show().addClass(CSS_CLASS.SHOW);
  35. _auto_hide(seconds, callback);
  36. };
  37. var hide = function() {
  38. _hide_children();
  39. setTimeout(function() {
  40. $(SELECTOR.GROWL).hide();
  41. }, 100);
  42. };
  43. var notify = function(title, description, icon, type, seconds, callback) {
  44. _showGrowlModal(false);
  45. var notify = $(SELECTOR.NOTIFY);
  46. if (type) {
  47. notify.addClass(type);
  48. }
  49. notify.html('<span class="icon ' + icon + '"></span><strong>' + title + '</strong><br/><em>' + description + '</strong>');
  50. setTimeout(function() {
  51. notify.addClass(CSS_CLASS.SHOW);
  52. }, 300);
  53. _auto_hide(seconds, callback);
  54. };
  55. var option = function(title, options) {
  56. _showGrowlModal(true);
  57. _options = options;
  58. $(SELECTOR.MODAL).removeClass(CSS_CLASS.WORKING).removeClass(CSS_CLASS.SHOW);
  59. var buttons = _createButtons(options);
  60. $(SELECTOR.MODAL).addClass('input').html('<span class="title">' + title + '</span>' + buttons);
  61. $('.growl .modal').show().addClass('show');
  62. };
  63. var _init = function() {
  64. //TODO: Append stylesheet (.less)
  65. $(SELECTOR.BODY).append(MARKUP_GROWL);
  66. _subscribeEvents();
  67. };
  68. var _showGrowlModal = function(modal) {
  69. var growl = $(SELECTOR.GROWL);
  70. if (!growl.is(CSS_CLASS.VISIBLE)) {
  71. growl.show();
  72. }
  73. if (modal) {
  74. growl.addClass('modal');
  75. }
  76. };
  77. var _animate = function(element, animate) {
  78. if (animate) {
  79. element.addClass(CSS_CLASS.WORKING);
  80. }
  81. else {
  82. element.removeClass(CSS_CLASS.WORKING);
  83. }
  84. };
  85. var _createButtons = function(options) {
  86. var buttons = '';
  87. for (var i = 0, len = options.length; i < len; i++) {
  88. buttons += _option_button(options[i].color, 'growl_option_' + i, options[i].icon, options[i].name);
  89. };
  90. buttons += _option_button('red', undefined, 'cancel', 'Cancel');
  91. return buttons;
  92. };
  93. var _option_button = function(color, id, icon, label) {
  94. id = (id !== undefined) ? 'id="' + id + '"' : '';
  95. return '<a href="#" ' + id + ' class="button ' + color + '"><span class="icon ' + icon + '"></span>' + label + '</a>';
  96. };
  97. var _auto_hide = function(seconds, callback) {
  98. if (seconds != undefined && seconds != 0) {
  99. if (callback === undefined) {
  100. callback = CALLBACK_HIDE;
  101. }
  102. setTimeout(callback, seconds * 1000);
  103. }
  104. };
  105. var _hide_children = function() {
  106. _hide_child(SELECTOR.MODAL);
  107. _hide_child(SELECTOR.NOTIFY);
  108. };
  109. var _hide_child = function(selector) {
  110. var child = $(selector);
  111. if (child.hasClass(CSS_CLASS.SHOW)) {
  112. child.removeClass(CSS_CLASS.SHOW);
  113. //child.removeClass(CSS_CLASS.INPUT).html('');
  114. }
  115. };
  116. var _subscribeEvents = function() {
  117. //BINDING EVENT
  118. //CF.app.EVENTS.touch
  119. $(SELECTOR.NOTIFY).bind('click', function() {
  120. $(SELECTOR.NOTIFY).removeClass(CSS_CLASS.SHOW);
  121. });
  122. $('.growl .modal a').live('click', function(event) {
  123. if ($(this).attr('id') !== ''){
  124. id = $(this).attr('id').replace(/growl_option_/g, '');
  125. setTimeout(_options[id].callback, 100);
  126. } else {
  127. event.preventDefault();
  128. hide();
  129. return false;
  130. }
  131. });
  132. };
  133. _init();
  134. return {
  135. show: show,
  136. hide: hide,
  137. notify: notify,
  138. option: option
  139. }
  140. })(LUNGO);