/public/javascripts/application.js

https://github.com/sandal/mission_of_mercy · JavaScript · 148 lines · 119 code · 26 blank · 3 comment · 21 complexity · c84af8550c5af5db58c5e77a45204cdc MD5 · raw file

  1. // Setup the MoM Namespace
  2. var MoM = MoM ? MoM : new Object();
  3. MoM.setupNamespace = function(namespace){
  4. if(MoM[namespace] == undefined)
  5. MoM[namespace] = {}
  6. }
  7. MoM.init = function(auth_token){
  8. MoM.AuthToken = auth_token;
  9. MoM.Support.startPolling();
  10. jQuery(function($) {
  11. $('img.help').click(function(e){
  12. helpBox = $(e.target).attr('data-help');
  13. $('#' + helpBox).slideToggle();
  14. });
  15. $('div.help-box img.close').click(function(e){
  16. $(e.target).parent().slideUp();
  17. });
  18. });
  19. }
  20. MoM.disableEnterKey = function(){
  21. $(document).observe('keypress', function(e) {
  22. if(e.keyCode == 13) e.stop();
  23. });
  24. }
  25. MoM.openInBackground = function(url){
  26. window.open(url); self.focus();
  27. }
  28. // Support
  29. MoM.setupNamespace("Support");
  30. // Toggle to disable support
  31. MoM.Support.Enabled = true;
  32. MoM.Support.Interval = 20;
  33. MoM.Support.startPolling = function (){
  34. new PeriodicalExecuter(MoM.Support.checkForRequests, MoM.Support.Interval);
  35. }
  36. MoM.Support.checkForRequests = function (executer){
  37. if(MoM.Support.Enabled){
  38. jQuery.getJSON('/active_support_requests.json',
  39. { authenticity_token: MoM.AuthToken},
  40. function(data){
  41. MoM.Support.processRequests(data);
  42. }
  43. );
  44. }
  45. else
  46. executer.stop();
  47. }
  48. MoM.Support.processRequests = function(data){
  49. if(data.requests.length > 0){
  50. var text = "Help needed at " + data.requests;
  51. var options = {
  52. 'icon': '/images/activebar/icon.png',
  53. 'button': '/images/activebar/close.png',
  54. 'highlight': 'InfoBackground',
  55. 'border': 'InfoBackground'
  56. }
  57. if(jQuery.fn.activebar.container != null){
  58. jQuery('.content',jQuery.fn.activebar.container).html(text);
  59. jQuery.fn.activebar.updateBar(options);
  60. if(!jQuery.fn.activebar.container.is(':visible'))
  61. jQuery.fn.activebar.show();
  62. }
  63. else
  64. {
  65. jQuery('<div></div>').html(text).activebar(options);
  66. }
  67. }
  68. else if(data.help_requested){
  69. MoM.Support.showSupportRequested(data.request_id);
  70. }
  71. else
  72. {
  73. if(jQuery.fn.activebar && ($('help_link') && $('help_link').visible()) || $('help_link') == null)
  74. jQuery.fn.activebar.hide();
  75. }
  76. }
  77. MoM.Support.showSupportRequested = function(id){
  78. MoM.Support.Enabled = false;
  79. if($('help_link') != null) $('help_link').hide();
  80. var text = "Help is on the way. <span style='float:right;'> Click here to cancel your request:</span>";
  81. var closeCallback = function(){
  82. jQuery.ajax({
  83. type: "PUT",
  84. url: '/support_requests/' + id,
  85. dataType: "script",
  86. data: {authenticity_token: MoM.AuthToken}
  87. });
  88. };
  89. var options = {
  90. 'icon': '/images/activebar/icon.png',
  91. 'button': '/images/activebar/close.png',
  92. 'highlight': 'none repeat scroll 0 0 #d23234',
  93. 'background': 'none repeat scroll 0 0 #d23234',
  94. 'border': '#d23234',
  95. 'fontColor': 'white',
  96. onClose: closeCallback
  97. }
  98. if(jQuery.fn.activebar.container != null){
  99. jQuery('.content',jQuery.fn.activebar.container).html(text);
  100. jQuery.fn.activebar.updateBar(options);
  101. if(!jQuery.fn.activebar.container.is(':visible'))
  102. jQuery.fn.activebar.show();
  103. }
  104. else
  105. {
  106. jQuery('<div></div>').html(text).activebar(options);
  107. }
  108. }
  109. MoM.Support.startStatusPolling = function(){
  110. new PeriodicalExecuter(MoM.Support.checkForStatusRequests, 15);
  111. }
  112. MoM.Support.checkForStatusRequests = function (executer){
  113. jQuery.ajax({
  114. url: '/status',
  115. timeout: 2000,
  116. dataType: "script",
  117. error: function(){
  118. $(document.body).addClassName('red')
  119. jQuery('#requests').html("<h1>Error connecting to server</h1>");
  120. }
  121. });
  122. }