aboutsummaryrefslogtreecommitdiff
path: root/static/mauve_utils.js.old
blob: 2a14dc216fd6335e7e6d47f30596645f66f074a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// rather simple first stab at automating image rollovers - any image with
// a class of auto_hover will set its source to be the original name + _hover.png
// when rolled over, and back again when the mouse moves away.
//
// need to initialise by calling addAutoHover() after document has loaded.
//
function addAutoHover() {
  $$('img.auto_hover').each(function(image) {
    image.observe('mouseover', function(event) { 
      image.src = image.src.gsub(".png", "_hover.png");
    });
    image.observe('mouseout', function(event) {
      image.src = image.src.gsub("_hover.png", ".png");
    });
    preload = new Image();
    preload.src = image.src.gsub(".png", "_hover.png");
  });
};

function addRefresh() {
  updater1 = new Ajax.PeriodicalUpdater("alert_summary", "/_alert_summary",  
    { method: 'get', frequency: 120 });
  updater2 = new Ajax.PeriodicalUpdater("alert_counts", "/_alert_counts",  
    { method: 'get', frequency: 120 });
}

// Pop up the big white box at the top when something goes wrong, scroll so 
// user can see it.
//
function reportError(message) {
  $('errors_list').insert('<li>'+message+'</li>');
  $('errors').show();
  $('errors').scrollTo();
}
// Hide the big white box again
//
function clearErrors() { $('errors').hide(); }

// Wrapper around reportError to report an error in updating a particular
// alert.
//
function acknowledgeFailed(id, message) {
  if (message) 
    reportError("<strong>Couldn't update alert "+id+":</strong> "+message);
  else
    reportError("<strong>Couldn't update alert "+id+"</strong>");
}

// Updates the page from a JSON representation of a particular alert.
//
function updateAlert(alert) {
  var strip = $('alert_'+alert.id);
  if (!strip) {
    reportError("Alert "+id+" not rendered - bug?");
    return;
  }
  
  image = strip.down(".acknowledge img");
  image.src = alert.acknowledged_at ? 
    "/images/acknowledge_acknowledged.png" :
    "/images/acknowledge_unacknowledged.png"
  
  if (strip.down(".source"))
    strip.down(".source").update(alert.source);
  if (strip.down(".subject"))
    strip.down(".subject").update(alert.subject);
  if (strip.down(".summary") && strip.down(".summary").down())
    strip.down(".summary").down().update(alert.summary);
  strip.next().update(alert.detail);
  
  if (alert.acknowledged_at)
    strip.next().hide();
}

// called when user hits the acknowledge button for an alert - makes a callback
// to the server to communicate the change, and updates the button 
// appropriately.
//
function toggleAcknowledge(id) {
  updater = new Ajax.Request('/alert/'+id+'/acknowledge', { 
  
    method: 'post',
    
    // ignored by server, see http://www.ruby-forum.com/topic/162976 for why
    postBody: 'x',
    
    onFailure: function(xhr) { acknowledgeFailed(id, "Failure - "+xhr.statusText); },
    
    onException: function(xhr, ex) { acknowledgeFailed(id, Dumper(ex)); },
    
    onSuccess: function(xhr) {
      if (xhr.status == 200) {
        content_type = xhr.getResponseHeader("Content-Type");
        if (content_type != "application/json") {
          acknowledgeFailed(id, "Got "+content_type+" not application/json from server");
        } else {
          updateAlert(xhr.responseText.evalJSON());
        }
      } else {
        acknowledgeFailed(id, "Connection problem");
      }
    }
  });
};


// Controls the showing of details on alerts.
function toggleDetailView(id) {
  updater = new Ajax.Request('/alert/'+id+'/toggleDetailView', {
    method: 'post',
    postBody: 'x',
    onFailure: function(xhr) { acknowledgeFailed(id, "Failure - "+xhr.statusText); },
    onException: function(xhr, ex) { acknowledgeFailed(id, Dumper(ex)); },
    onSuccess: function(xhr) {
      if (xhr.status == 200) {
        content_type = xhr.getResponseHeader("Content-Type");
        if (content_type != "application/json") {
          acknowledgeFailed(id, "Got "+content_type+" not application/json from server");
        } else {
          //updateAlert(xhr.responseText.evalJSON());
        }
      } else {
        acknowledgeFailed(id, "Connection problem");
      }
    }
  });
}


// Controls the showing of folding on alerts.
function toggleFoldingView(subject) {
  updater = new Ajax.Request('/alert/fold/'+subject, {
    method: 'post',
    postBody: 'x',
    onFailure: function(xhr) { acknowledgeFailed(subject, "Failure - "+xhr.statusText); },
    onException: function(xhr, ex) { acknowledgeFailed(subject, Dumper(ex)); },
    onSuccess: function(xhr) {
      if (xhr.status == 200) {
        content_type = xhr.getResponseHeader("Content-Type");
        if (content_type != "application/json") {
          acknowledgeFailed(subject, "Got "+content_type+" not application/json from server");
        } else {
          //updateAlert(xhr.responseText.evalJSON());
        }
      } else {
        acknowledgeFailed(subject, "Connection problem");
      }
    }
  });
}