aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/static/js/statpage.js
blob: be2948c41c533bfe00164f99c229cb728a5a435d (plain) (blame)
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
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$(function() {
  $('.title').click(function() {
    var id = $(this).parent().attr("id")
    toggleId(id);
    // $(this).parent().children('.statbody').toggle();
  });
  $('.statbody').each(function() {
    if (isVisible($(this).parent().attr("id"))) {
      $(this).show();
    }
  });
  var cat = window.location.hash.slice(1);
  if (! cat) {
    cat = "health";
  }
  $('.navlink').click(function() {
    var cat = $(this).attr("id").slice(4);
    showCategory(cat);
  });
  showCategory(cat);
});

function showCategory(cat) {
  $('#fragment').val(cat);
  $('.categorywrapper').each(function() {
    var localCat = $(this).attr("id").slice(3);
    if (localCat == cat) {
      $('#link'+localCat).parent().addClass("selected");
      $(this).show();
    } else {
      $('#link'+localCat).parent().removeClass("selected");
      $(this).hide();
    }
  })
}

function formChanged() {
  document.forms[0].submit();
}

if (! String.prototype.startsWith) {
  String.prototype.startsWith = function(s) {
    if (this.length < s.length) { return false; }
    return this.substr(0, s.length) == s;
  }  
}

if (! String.prototype.trim) {
  String.prototype.trim = function() {
    var firstNonSpace;
    for (var i = 0; i < this.length; ++i) {
      if (this[i] != ' ') {
        firstNonSpace = i;
        break;
      }
    }
    var s = this;
    if (firstNonSpace) {
      s = this.substr(firstNonSpace);
    }
    var lastNonSpace;
    for (var i = this.length-1; i >= 0; --i) {
      if (this[i] != ' ') {
        lastNonSpace = i;
        break;
      }
    }
    if (lastNonSpace !== undefined) {
      s = s.substr(0, lastNonSpace+1);
    }
    return s;
  }  
}

if (! Array.prototype.contains) {
  Array.prototype.contains = function(obj) {
    for (var i = 0; i < this.length; ++i) {
      if (this[i] == obj) return true;
    }
    return false;
  }  
}

if (! Array.prototype.first) {
  Array.prototype.first = function(f) {
    for (var i = 0; i < this.length; ++i) {
      if (f(this[i])) {
        return this[i];
      }
    }
  }  
}

var cookieprefix = "visiblestats="

function statsCookieValue() {
  return (document.cookie.split(";").map(function(s) { return s.trim() }).first(function(str) { 
    return str.startsWith(cookieprefix); 
  }) || cookieprefix).split("=")[1];
}

function isVisible(id) {
  var cookieValue = statsCookieValue();
  return ! (cookieValue.split("-").contains(id));
}

function rememberHidden(id) {
  if (! isVisible(id)) { return; }
  document.cookie = cookieprefix+
    statsCookieValue().split("-").concat([id]).join("-");
}

function rememberVisible(id) {
  if (isVisible(id)) { return; }
  document.cookie = cookieprefix+
    statsCookieValue().split("-").filter(function(obj) { return obj != id }).join("-");
}

function toggleId(id) {
  var body = $('#'+id).children('.statbody');
  body.toggle();
  if (body.is(":visible")) {
    rememberVisible(id);
  } else {
    rememberHidden(id);
  }
}