/app/assets/javascripts/application.js

https://github.com/chriskottom/mission_of_mercy · JavaScript · 153 lines · 119 code · 27 blank · 7 comment · 21 complexity · 891e141e7298d5c925382a48584ebe09 MD5 · raw file

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