aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/pro/pro_padlist.js
blob: 73b179c4ea463a4d33a0c44eed320205a8bc0c0f (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * 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.
 */

import("funhtml.*");
import("jsutils.*");
import("stringutils");

import("etherpad.utils.*");
import("etherpad.helpers");
import("etherpad.pad.padutils");
import("etherpad.collab.collab_server");

import("etherpad.pro.pro_accounts");

function _getColumnMeta() {
  // returns map of {id --> {
  //    title,
  //    sortFn(a,b),
  //    render(p)
  // }

  function _dateNum(d) {
    if (!d) {
      return 0;
    }
    return -1 * (+d);
  }


  var cols = {};

  function addAvailableColumn(id, cdata) {
    if (!cdata.render) {
      cdata.render = function(p) {
        return p[id];
      };
    }
    if (!cdata.cmpFn) {
      cdata.cmpFn = function(a,b) {
        return cmp(a[id], b[id]);
      };
    }
    cdata.id = id;
    cols[id] = cdata;
  }

  addAvailableColumn('public', {
    title: "",
    render: function(p) {
      // TODO: implement an icon with hover text that says public vs.
      // private
      return "";
    },
    cmpFn: function(a,b) {
      return 0; // not sort-able
    }
  });
  addAvailableColumn('secure', {
    title: "",
    render: function(p) {
      if (p.password) {
        return IMG({src: '/static/img/may09/padlock.gif'});
      } else {
        return "";
      }
    },
    cmpFn: function(a,b) {
      return cmp(a.password, b.password);
    }
  });
  addAvailableColumn('title', {
    title: "Title",
    render: function(p) {
      var t = padutils.getProDisplayTitle(p.localPadId, p.title);
      return A({href: "/"+p.localPadId}, t);
    },
    sortFn: function(a, b) {
      return cmp(padutils.getProDisplayTitle(a.localPadId, a.title),
                 padutils.getProDisplayTitle(b.localPadId, b.title));
    }
  });
  addAvailableColumn('creatorId', {
    title: "Creator",
    render: function(p) {
      return pro_accounts.getFullNameById(p.creatorId);
    },
    sortFn: function(a, b) {
      return cmp(pro_accounts.getFullNameById(a.creatorId),
                 pro_accounts.getFullNameById(b.creatorId));
    }
  });
  addAvailableColumn('createdDate', {
    title: "Created",
    render: function(p) {
      return timeAgo(p.createdDate);
    },
    sortFn: function(a, b) {
      return cmp(_dateNum(a.createdDate), _dateNum(b.createdDate));
    }
  });
  addAvailableColumn('lastEditorId', {
    title: "Last Editor",
    render: function(p) {
      if (p.lastEditorId) {
        return pro_accounts.getFullNameById(p.lastEditorId);
      } else {
        return "";
      }
    },
    sortFn: function(a, b) {
      var a_ = a.lastEditorId ? pro_accounts.getFullNameById(a.lastEditorId) : "ZZZZZZZZZZ";
      var b_ = b.lastEditorId ? pro_accounts.getFullNameById(b.lastEditorId) : "ZZZZZZZZZZ";
      return cmp(a_, b_);
    }
  });

  addAvailableColumn('editors', {
    title: "Editors",
    render: function(p) {
      var editors = [];
      p.proAttrs.editors.forEach(function(editorId) {
        editors.push([editorId, pro_accounts.getFullNameById(editorId)]);
      });
      editors.sort(function(a,b) { return cmp(a[1], b[1]); });
      var sp = SPAN();
      for (var i = 0; i < editors.length; i++) {
        if (i > 0) {
          sp.push(", ");
        }
        sp.push(A({href: "/ep/padlist/edited-by?editorId="+editors[i][0]}, editors[i][1]));
      }
      return sp;
    }
  });

  addAvailableColumn('lastEditedDate', {
    title: "Last Edited",
    render: function(p) {
      if (p.lastEditedDate) {
        return timeAgo(p.lastEditedDate);
      } else {
        return "never";
      }
    },
    sortFn: function(a,b) {
      return cmp(_dateNum(a.lastEditedDate), _dateNum(b.lastEditedDate));
    }
  });
  addAvailableColumn('localPadId', {
    title: "Path",
  });
  addAvailableColumn('actions', {
    title: "",
    render: function(p) {
      return DIV({className: "gear-drop", id: "pad-gear-"+p.id}, "  ");
    }
  });

  addAvailableColumn('connectedUsers', {
    title: "Connected Users",
    render: function(p) {
      var names = [];
      padutils.accessPadLocal(p.localPadId, function(pad) {
        var userList = collab_server.getConnectedUsers(pad);
        userList.forEach(function(u) {
          if (collab_server.translateSpecialKey(u.specialKey) != 'invisible') {
            // excludes etherpad admin user
            names.push(u.name);
          }
        });
      });
      return names.join(", ");
    }
  });

  return cols;
}

function _sortPads(padList) {
  var meta = _getColumnMeta();
  var sortId = _getCurrentSortId();
  var reverse = false;
  if (sortId.charAt(0) == '-') {
    reverse = true;
    sortId = sortId.slice(1);
  }
  padList.sort(function(a,b) { return cmp(a.localPadId, b.localPadId); });
  padList.sort(function(a,b) { return meta[sortId].sortFn(a, b); });
  if (reverse) { padList.reverse(); }
}

function _addClientVars(padList) {
  var padTitles = {}; // maps localPadId -> title
  var localPadIds = {}; // maps padmetaId -> localPadId
  padList.forEach(function(p) {
    padTitles[p.localPadId] = stringutils.toHTML(padutils.getProDisplayTitle(p.localPadId, p.title));
    localPadIds[p.id] = p.localPadId;
  });
  helpers.addClientVars({ 
    padTitles: padTitles,
    localPadIds: localPadIds
  });
}

function _getCurrentSortId() {
  return request.params.sortBy || "lastEditedDate";
}

function _renderColumnHeader(m) {
  var sp = SPAN();
  var sortBy = _getCurrentSortId();
  if (m.sortFn) {
    var d = {sortBy: m.id};
    var arrow = "";
    if (sortBy == m.id) {
      d.sortBy = ("-"+m.id);
      arrow = html("&#8595;");
    }
    if (sortBy == ("-"+m.id)) {
      arrow = html("&#8593;");
    }
    sp.push(arrow, " ", A({href: qpath(d)}, m.title));
  } else {
    sp.push(m.title);
  }
  return sp;
}

function renderPadList(padList, columnIds, limit) {
  _sortPads(padList);
  _addClientVars(padList);

  if (limit && (limit < padList.length)) {
    padList = padList.slice(0,limit);
  }

  var showSecurityInfo = false;
  padList.forEach(function(p) {
    if (p.password && p.password.length > 0) { showSecurityInfo = true; }
  });
  if (!showSecurityInfo && (columnIds[0] == 'secure')) {
    columnIds.shift();
  }

  var columnMeta = _getColumnMeta();

  var t = TABLE({id: "padtable", cellspacing:"0", cellpadding:"0"});
  var toprow = TR({className: "toprow"});
  columnIds.forEach(function(cid) { 
    toprow.push(TH(_renderColumnHeader(columnMeta[cid])));
  });
  t.push(toprow);

  padList.forEach(function(p) {
    // Note that this id is always numeric, and is the actual
    // canonical padmeta id.
    var row = TR({id: 'padmeta-'+p.id});
    var first = true;
    for (var i = 0; i < columnIds.length; i++) {
      var cid = columnIds[i];
      var m = columnMeta[cid];
      var classes = cid;
      if (i == 0) {
        classes += (" first");
      }
      if (i == (columnIds.length - 1)) {
        classes += (" last");
      }
      row.push(TD({className: classes}, m.render(p)));
    }
    t.push(row);
  });

  return t;
}