Project

General

Profile

1 4046 berkley
var fullDocId;
2
3
//log the user in
4 3998 berkley
function login()
5
{
6
  var user = document.getElementById("un").value;
7
  var org = document.getElementById("org").value;
8
  var pass = document.getElementById("pw").value;
9
  var ldapUsername = 'uid=' + user + ',o=' + org + ',dc=ecoinformatics,dc=org';
10
11
  $.get("metacat", {username: ldapUsername, password: pass, action:"login", qformat:"xml"},
12
    function(data) {
13
      //alert('user ' + ldapUsername + ' logged in.  data:' + data);
14
      if(data.indexOf('<sessionId>') != -1)
15
      { //login successful
16
        //alert('user logged in');
17
        slideUp("#loginformdiv");
18
        setCookie("sms-login", true);
19
        setCookie("sms-user", user);
20
        setCookie("sms-org", org);
21
        setLoginHeader(true);
22 4012 berkley
        checkLogin();
23 3998 berkley
      }
24
      else
25
      { //login not successful
26
        alert('Sorry, your login failed.  Please try again.  If you need a username, please go to http://knb.ecoinformatics.org.');
27
        setCookie("sms-login", false);
28
      }
29
30
    }, "XML");
31
}
32
33 4046 berkley
//update the users status on the page
34 4002 berkley
function updateStatus()
35
{
36
  var url = window.location.href;
37
  if(url.indexOf('docid') != -1)
38
  { //if there's a docid in the url, set the cookie
39
    var docid = url.substring(url.indexOf("docid=") + 6, url.indexOf("&status"));
40
    var docidcookie = getCookie("sms-lastdocid");
41
    if(docid != docidcookie)
42
    { //set the cookie for next time
43
      setCookie("sms-lastdocid", docid);
44
      //slideDown("#uploadstatus");
45
      $('#uploadstatus').css("display", "block");
46
    }
47
    else
48
    { //hide the status
49
      $('#uploadstatus').css("display", "none");
50
      //slideUp("#uploadstatus");
51
    }
52
  }
53
54
}
55
56 4046 berkley
//set the header when the user logs in
57 3998 berkley
function setLoginHeader(loggedin)
58
{
59
  if(loggedin)
60
  {
61 4002 berkley
    updateStatus();
62 3998 berkley
    var user = getCookie("sms-user");
63
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">"
64 4012 berkley
      + user + " Logged In <a href=\"javascript:logout()\" style=\"font-size:70%\">[logout]</a></h2>");
65 3998 berkley
    slideUp("#loginformdiv");
66
    $('#maindiv').css("display", "block");
67
    $('#bottomimg').css("bottom", "0px");
68 4046 berkley
    getId();
69 3998 berkley
  }
70
  else
71
  {
72
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">"
73
      + "Please Log In</h2>");
74
    slideDown("#loginformdiv");
75
    $('#maindiv').css("display", "none");
76
    $('#bottomimg').css("bottom", "15px");
77
  }
78
}
79
80 4046 berkley
//log the user out.
81 3998 berkley
function logout()
82
{
83
  $.get("metacat", {action:"logout", qformat:"xml"});
84
  setLoginHeader(false);
85
  setCookie("sms-login", false);
86
}
87
88 4077 berkley
//make sure the given docid is public then call checkLogin()
89
function checkPublicAccess(docid)
90
{
91
  var lastpublicaccess = getCookie("sms-last-public-access");
92
  if(lastpublicaccess == null || lastpublicaccess != docid)
93
  { //only make it public if it wasn't already made public
94
    makepublic(docid);
95
  }
96
  checkLogin();
97
}
98
99 4046 berkley
//make sure the user is logged in.
100 3998 berkley
function checkLogin()
101
{
102 4076 berkley
  var currentTab = getCookie("sms-current-tab");
103
104 3998 berkley
  if(getCookie("sms-login") == "true")
105
  {
106
    setLoginHeader(true);
107 4005 berkley
    showDatasets();
108 4076 berkley
    if(currentTab == null || currentTab == 'search')
109
    {
110
      showSearchPane();
111
    }
112
    else
113
    {
114
      if(currentTab == 'browse')
115
      {
116
        showBrowsePane();
117
      }
118
      else if(currentTab == 'upload')
119
      {
120
        showUploadPane();
121
      }
122
    }
123 3998 berkley
  }
124
  else
125
  {
126
    setLoginHeader(false);
127
  }
128
}
129
130 4046 berkley
//search the document base in metacat
131 4012 berkley
function search()
132
{
133
  var searchval = document.getElementById("searchtextbox").value
134 4014 berkley
  var url = '/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0';
135
  setCookie("sms-searchval", searchval);
136 4032 berkley
  setCookie("sms-pagestart", 0);
137 4076 berkley
  reloadSearchContent(url);
138 4012 berkley
}
139
140 4046 berkley
//show all of the datasets in metacat
141 4005 berkley
function showDatasets()
142
{
143 4014 berkley
  var searchval = getCookie('sms-searchval');
144 4032 berkley
  if(searchval == null || searchval == '')
145 4014 berkley
  {
146
    searchval = '%25';
147
  }
148 4076 berkley
  var currentTab = getCookie('sms-current-tab');
149
  var page;
150
  if(currentTab == null)
151
  {
152
    page = 0;
153
  }
154
  else if(currentTab == 'search' || currentTab == 'upload')
155
  {
156
    page = getCookie('sms-search-pagestart');
157
  }
158
  else
159
  {
160
    page = getCookie('sms-browse-pagestart');
161
    setCookie('sms-browse-content-loaded', 'true');
162
    searchval = '%25';
163
  }
164
165 4013 berkley
  if(page)
166
  {
167 4076 berkley
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=' + page);
168 4013 berkley
  }
169
  else
170
  {
171 4076 berkley
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0');
172 4013 berkley
  }
173 4005 berkley
}
174
175 4046 berkley
//reload the search result table
176 4076 berkley
function reloadSearchContent(url)
177 4005 berkley
{
178 4076 berkley
  var table;
179
  var div;
180 4013 berkley
  var page = url.substring(url.indexOf('pagestart=') + 10, url.length);
181 4076 berkley
  var currentTab = getCookie('sms-current-tab');
182
  if(currentTab == null || currentTab == 'search' || currentTab == 'upload')
183
  {
184
    table = '#searchresulttable';
185
    div = '#searchresultdiv'
186
    setCookie("sms-search-pagestart", page);
187
  }
188
  else if(currentTab == 'browse')
189
  {
190
    table = '#browseresulttable';
191
    div = '#browseresultdiv'
192
    setCookie("sms-browse-pagestart", page);
193
  }
194
195
  $(table).load(url);
196 4005 berkley
}
197
198 4046 berkley
//upload a file to metacat
199 3998 berkley
function uploadfile()
200
{
201
  if(getCookie("sms-login") != "true")
202
  {
203
    alert('You cannot upload.  You are not logged in.');
204
    return;
205
  }
206 4046 berkley
  if(!checkId(true))
207
  { //make sure the id is valid
208
    alert('The ID prefix you chose is not valid.  The prefix must be a string of alpha characters only.');
209
  }
210
  else
211
  {
212
    if(document.getElementById("datafile").value == null || document.getElementById("datafile").value == "")
213
    {
214
      alert('You must choose a file to upload.');
215
      return;
216
    }
217
    getId(true, true, true);
218
  }
219 3998 berkley
}
220
221 4046 berkley
//make a document public
222 4032 berkley
function makepublic(docid)
223
{
224
  $.get("/sms/metacat?action=setaccess&docid=" + docid +
225
        "&principal=public&permission=read&permType=allow&permOrder=allowFirst",
226
        function(data) {
227
          if(data.indexOf("<success>") != -1)
228
          {
229
            slideUp("#uploadstatus");
230
            $("#uploadstatus").html('<p>The document with id ' +
231
              '<a href="/sms/metacat?action=read&docid=' + docid + '&qformat=sms">' + docid +
232
              '</a> is now publicly readable.</p>');
233
            slideDown("#uploadstatus");
234 4077 berkley
            setCookie("sms-last-public-access", docid);
235 4032 berkley
          }
236
          else
237
          {
238
            alert('The access control changes for ' + docid + ' failed.  It is not publicly readable.');
239
          }
240
        }, "XML");
241
}
242
243 4046 berkley
//get the next id and put it in the id text boxes
244
function getId(setFields, setForm, submitForm)
245
{
246
  if(setFields == null)
247
  {
248
    setFields = true;
249
  }
250
251
  if(setForm == null)
252
  {
253
    setForm = false;
254
  }
255
256
  if(submitForm == null)
257
  {
258
    submitForm = false;
259
  }
260
261
  var scopeStr = document.getElementById("docidfamily").value;
262
  //var scopeStr = $('#docidfamily').value;
263
  if(scopeStr == '' || scopeStr == null)
264
  {
265
    scopeStr = "sms";
266
  }
267
268
  $.get("metacat", {action:"getlastdocid", scope:scopeStr},
269
      function(data)
270
      {
271
        var docid = data.substring(data.indexOf("<docid>") + 7, data.indexOf("</docid>"));
272
        var nextid;
273
        if(docid == 'null')
274
        {
275
          nextid = 1;
276
        }
277
        else
278
        {
279
          nextid = docid.substring(docid.indexOf(".") + 1, docid.lastIndexOf("."));
280
          nextid++;
281
          //nextid = scopeStr + nextid + ".1";
282
        }
283
        //$('#docidtextfield').val(nextid);
284
        if(setFields)
285
        {
286
          $('#docidfamily').val(scopeStr);
287
          $('#docidnumber').val(nextid);
288
          $('#docidrevision').val("1");
289
        }
290
        fullDocId = scopeStr + "." + nextid + ".1";
291
        //alert('fullDocId: ' + fullDocId);
292
        if(setForm)
293
        {
294
          //alert('setting docid to ' + fullDocId);
295
          $('#docid').val(fullDocId);
296
        }
297
298
        if(submitForm)
299
        {
300
          $("form").submit();
301
        }
302
      },
303
      "XML");
304
}
305
306
//check for a valid docid
307
function checkId(setForm)
308
{
309
  if(setForm == null)
310
  {
311
    setForm = false;
312
  }
313
  getId(false, setForm, false);
314
  var scopeStr = document.getElementById("docidfamily").value;
315
  var numberStr = document.getElementById("docidnumber").value;
316
  var userDocid = scopeStr + "." + numberStr;
317
318
  //fullDocId is a global var that gets set by getId()
319
  var nextnum = fullDocId.substring(fullDocId.indexOf(".") + 1, fullDocId.lastIndexOf("."));
320
  var regexp = "[^[[a-z]|[A-Z]]+]"; //search for anything thats not an alpha
321
  var re = new RegExp(regexp);
322
  var match = re.test(scopeStr);
323
  if(match)
324
  { //if it matches, reject
325
    return false;
326
  }
327
328
  return true;
329
}
330
331
//show the search tab
332
function showSearchPane()
333
{
334 4076 berkley
  setCookie('sms-current-tab', 'search');
335 4046 berkley
  //hide all, then slide down the search pane
336 4077 berkley
  $('#uploaddiv').hide();
337
  $('#browseresultdiv').hide();
338 4046 berkley
  $('#searchdiv').fadeIn("slow");
339
  switchTabs('search');
340
}
341
342
//show the upload tab
343
function showUploadPane()
344
{
345 4076 berkley
  setCookie('sms-current-tab', 'upload');
346 4046 berkley
  //hide all, then slide down the upload pane
347 4077 berkley
  $('#searchdiv').hide();
348
  $('#browseresultdiv').hide();
349 4046 berkley
  $('#uploaddiv').fadeIn("slow");
350
  $('#uploadetabimg').hide();
351
  $('#uploadtabimgsel').show();
352
  switchTabs('upload');
353
}
354
355
//show the browse tab
356
function showBrowsePane()
357
{
358 4076 berkley
  setCookie('sms-current-tab', 'browse');
359 4077 berkley
  var page = getCookie('sms-browse-pagestart');
360
  if(!page)
361
  {
362
    page = 0;
363
  }
364 4076 berkley
  var contentLoaded = getCookie('sms-browse-content-loaded');
365
  if(!contentLoaded)
366
  {
367 4077 berkley
    reloadSearchContent('/sms/metacat?action=query&anyfield=%25&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=' + page);
368 4076 berkley
  }
369 4046 berkley
  //hide all, then slide down the browse pane
370 4077 berkley
  $('#searchdiv').hide();
371
  $('#uploaddiv').hide();
372 4076 berkley
  $('#browseresultdiv').fadeIn("slow");
373 4046 berkley
  $('#browsetabimg').hide();
374
  $('#browsetabimgsel').show();
375
  switchTabs('browse');
376
}
377
378 4077 berkley
//switch to a given tab
379 4046 berkley
function switchTabs(tab)
380
{
381
  if(tab == 'browse')
382
  {
383
    $('#searchtabimg').show();
384
    $('#uploadtabimg').show();
385
    $('#browsetabimg').hide();
386
387
    $('#uploadtabimgsel').hide();
388
    $('#browsetabimgsel').show();
389
    $('#searchtabimgsel').hide();
390
  }
391
  else if(tab == 'search')
392
  {
393
    $('#searchtabimg').hide();
394
    $('#uploadtabimg').show();
395
    $('#browsetabimg').show();
396
397
    $('#uploadtabimgsel').hide();
398
    $('#browsetabimgsel').hide();
399
    $('#searchtabimgsel').show();
400
  }
401
  else if(tab == 'upload')
402
  {
403
    $('#searchtabimg').show();
404
    $('#uploadtabimg').hide();
405
    $('#browsetabimg').show();
406
407
    $('#uploadtabimgsel').show();
408
    $('#browsetabimgsel').hide();
409
    $('#searchtabimgsel').hide();
410
  }
411
}
412
413
//slide an element up
414 3998 berkley
function slideUp(id)
415
{
416
  $(id).slideUp("slow");
417
}
418
419 4046 berkley
//slide and element down
420 3998 berkley
function slideDown(id)
421
{
422
  $(id).slideDown("slow");
423
}
424
425 4046 berkley
//set a cookie
426 3998 berkley
function setCookie( name, value, expires, path, domain, secure )
427
{
428
  // set time, it's in milliseconds
429
  var today = new Date();
430
  today.setTime( today.getTime() );
431
432
  /*
433
  if the expires variable is set, make the correct
434
  expires time, the current script below will set
435
  it for x number of days, to make it for hours,
436
  delete * 24, for minutes, delete * 60 * 24
437
  */
438
  if ( expires )
439
  {
440
    expires = expires * 1000 * 60 * 60 * 24;
441
  }
442
  var expires_date = new Date( today.getTime() + (expires) );
443
444
  document.cookie = name + "=" +escape( value ) +
445
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
446
  ( ( path ) ? ";path=" + path : "" ) +
447
  ( ( domain ) ? ";domain=" + domain : "" ) +
448
  ( ( secure ) ? ";secure" : "" );
449
}
450
451 4046 berkley
//get a cookie
452 3998 berkley
function getCookie( check_name ) {
453
	// first we'll split this cookie up into name/value pairs
454
	// note: document.cookie only returns name=value, not the other components
455
	var a_all_cookies = document.cookie.split( ';' );
456
	var a_temp_cookie = '';
457
	var cookie_name = '';
458
	var cookie_value = '';
459
	var b_cookie_found = false; // set boolean t/f default f
460
461
	for ( i = 0; i < a_all_cookies.length; i++ )
462
	{
463
		// now we'll split apart each name=value pair
464
		a_temp_cookie = a_all_cookies[i].split( '=' );
465
466
		// and trim left/right whitespace while we're at it
467
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
468
469
		// if the extracted name matches passed check_name
470
		if ( cookie_name == check_name )
471
		{
472
			b_cookie_found = true;
473
			// we need to handle case where cookie has no value but exists (no = sign, that is):
474
			if ( a_temp_cookie.length > 1 )
475
			{
476
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
477
			}
478
			// note that in cases where cookie is initialized but no value, null is returned
479
			return cookie_value;
480
			break;
481
		}
482
		a_temp_cookie = null;
483
		cookie_name = '';
484
	}
485
	if ( !b_cookie_found )
486
	{
487
		return null;
488
	}
489
}