Project

General

Profile

1
var fullDocId;
2

    
3
//log the user in 
4
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
        checkLogin();
23
      }
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
//update the users status on the page
34
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
//set the header when the user logs in
57
function setLoginHeader(loggedin)
58
{
59
  if(loggedin)
60
  {
61
    updateStatus();
62
    var user = getCookie("sms-user");
63
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">" 
64
      + user + " Logged In <a href=\"javascript:logout()\" style=\"font-size:70%\">[logout]</a></h2>");
65
    slideUp("#loginformdiv");
66
    $('#maindiv').css("display", "block");
67
    $('#bottomimg').css("bottom", "0px");
68
    getId();
69
  }
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
//log the user out.
81
function logout()
82
{
83
  $.get("metacat", {action:"logout", qformat:"xml"});
84
  setLoginHeader(false);
85
  setCookie("sms-login", false);
86
}
87

    
88
//make sure the user is logged in.
89
function checkLogin()
90
{
91
  var currentTab = getCookie("sms-current-tab");
92
  
93
  if(getCookie("sms-login") == "true")
94
  {
95
    setLoginHeader(true);
96
    showDatasets();
97
    if(currentTab == null || currentTab == 'search')
98
    {
99
      showSearchPane();
100
    }
101
    else
102
    {
103
      if(currentTab == 'browse')
104
      {
105
        showBrowsePane();
106
      }
107
      else if(currentTab == 'upload')
108
      {
109
        showUploadPane();
110
      }
111
    }
112
  }
113
  else
114
  {
115
    setLoginHeader(false);
116
  }
117
}
118

    
119
//search the document base in metacat
120
function search()
121
{
122
  var searchval = document.getElementById("searchtextbox").value
123
  var url = '/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0';
124
  setCookie("sms-searchval", searchval);
125
  setCookie("sms-pagestart", 0);
126
  reloadSearchContent(url);
127
}
128

    
129
//show all of the datasets in metacat
130
function showDatasets()
131
{
132
  var searchval = getCookie('sms-searchval');
133
  if(searchval == null || searchval == '')
134
  {
135
    searchval = '%25';
136
  }
137
  var currentTab = getCookie('sms-current-tab');
138
  var page;
139
  if(currentTab == null)
140
  {
141
    page = 0;
142
  }
143
  else if(currentTab == 'search' || currentTab == 'upload')
144
  {
145
    page = getCookie('sms-search-pagestart');
146
  }
147
  else
148
  {
149
    page = getCookie('sms-browse-pagestart');
150
    setCookie('sms-browse-content-loaded', 'true');
151
    searchval = '%25';
152
  }
153
  
154
  if(page)
155
  {
156
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=' + page);
157
  }
158
  else
159
  {
160
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0');
161
  }
162
}
163

    
164
//reload the search result table
165
function reloadSearchContent(url)
166
{
167
  var table;
168
  var div;
169
  var page = url.substring(url.indexOf('pagestart=') + 10, url.length);
170
  var currentTab = getCookie('sms-current-tab');
171
  if(currentTab == null || currentTab == 'search' || currentTab == 'upload')
172
  {
173
    table = '#searchresulttable';
174
    div = '#searchresultdiv'
175
    setCookie("sms-search-pagestart", page);
176
  }
177
  else if(currentTab == 'browse')
178
  {
179
    table = '#browseresulttable';
180
    div = '#browseresultdiv'
181
    setCookie("sms-browse-pagestart", page);
182
  }
183
  
184
  $(table).load(url);
185
}
186

    
187
//upload a file to metacat
188
function uploadfile()
189
{
190
  if(getCookie("sms-login") != "true")
191
  {
192
    alert('You cannot upload.  You are not logged in.');
193
    return;
194
  }
195
  if(!checkId(true))
196
  { //make sure the id is valid
197
    alert('The ID prefix you chose is not valid.  The prefix must be a string of alpha characters only.');
198
  }
199
  else
200
  {
201
    if(document.getElementById("datafile").value == null || document.getElementById("datafile").value == "")
202
    {
203
      alert('You must choose a file to upload.');
204
      return;
205
    }
206
    getId(true, true, true);
207
  }
208
}
209

    
210
//make a document public
211
function makepublic(docid)
212
{
213
  $.get("/sms/metacat?action=setaccess&docid=" + docid + 
214
        "&principal=public&permission=read&permType=allow&permOrder=allowFirst",
215
        function(data) {
216
          if(data.indexOf("<success>") != -1)
217
          {
218
            slideUp("#uploadstatus");
219
            $("#uploadstatus").html('<p>The document with id ' + 
220
              '<a href="/sms/metacat?action=read&docid=' + docid + '&qformat=sms">' + docid + 
221
              '</a> is now publicly readable.</p>');
222
            slideDown("#uploadstatus");
223
            //alert('success: ' + "#makepublic");
224
            //slideUp("#makepublic");
225
          }
226
          else
227
          {
228
            alert('The access control changes for ' + docid + ' failed.  It is not publicly readable.');
229
          }
230
        }, "XML");
231
}
232

    
233
//get the next id and put it in the id text boxes
234
function getId(setFields, setForm, submitForm)
235
{
236
  if(setFields == null)
237
  {
238
    setFields = true;
239
  }
240
  
241
  if(setForm == null)
242
  {
243
    setForm = false;
244
  }
245
  
246
  if(submitForm == null)
247
  {
248
    submitForm = false;
249
  }
250
  
251
  var scopeStr = document.getElementById("docidfamily").value;
252
  //var scopeStr = $('#docidfamily').value;
253
  if(scopeStr == '' || scopeStr == null)
254
  {
255
    scopeStr = "sms";
256
  }
257
  
258
  $.get("metacat", {action:"getlastdocid", scope:scopeStr}, 
259
      function(data)
260
      {
261
        var docid = data.substring(data.indexOf("<docid>") + 7, data.indexOf("</docid>"));
262
        var nextid;
263
        if(docid == 'null')
264
        {
265
          nextid = 1;
266
        }
267
        else
268
        {
269
          nextid = docid.substring(docid.indexOf(".") + 1, docid.lastIndexOf("."));
270
          nextid++;
271
          //nextid = scopeStr + nextid + ".1"; 
272
        }
273
        //$('#docidtextfield').val(nextid);
274
        if(setFields)
275
        {
276
          $('#docidfamily').val(scopeStr);
277
          $('#docidnumber').val(nextid);
278
          $('#docidrevision').val("1");
279
        }
280
        fullDocId = scopeStr + "." + nextid + ".1";
281
        //alert('fullDocId: ' + fullDocId);
282
        if(setForm)
283
        {
284
          //alert('setting docid to ' + fullDocId);
285
          $('#docid').val(fullDocId);
286
        }
287
        
288
        if(submitForm)
289
        {
290
          $("form").submit();
291
        }
292
      }, 
293
      "XML");
294
}
295

    
296
//check for a valid docid
297
function checkId(setForm)
298
{
299
  if(setForm == null)
300
  {
301
    setForm = false;
302
  }
303
  getId(false, setForm, false);
304
  var scopeStr = document.getElementById("docidfamily").value;
305
  var numberStr = document.getElementById("docidnumber").value;
306
  var userDocid = scopeStr + "." + numberStr; 
307
  
308
  //fullDocId is a global var that gets set by getId()
309
  var nextnum = fullDocId.substring(fullDocId.indexOf(".") + 1, fullDocId.lastIndexOf("."));
310
  var regexp = "[^[[a-z]|[A-Z]]+]"; //search for anything thats not an alpha 
311
  var re = new RegExp(regexp);
312
  var match = re.test(scopeStr);
313
  if(match)
314
  { //if it matches, reject
315
    return false;
316
  }
317
  
318
  return true;
319
}
320

    
321
//show the search tab
322
function showSearchPane()
323
{
324
  setCookie('sms-current-tab', 'search');
325
  //hide all, then slide down the search pane
326
  $('#uploaddiv').fadeOut("slow");
327
  $('#browseresultdiv').fadeOut("slow");
328
  $('#searchdiv').fadeIn("slow");
329
  switchTabs('search');
330
}
331

    
332
//show the upload tab
333
function showUploadPane()
334
{
335
  setCookie('sms-current-tab', 'upload');
336
  //hide all, then slide down the upload pane
337
  $('#searchdiv').fadeOut("slow");
338
  $('#browseresultdiv').fadeOut("slow");
339
  $('#uploaddiv').fadeIn("slow");
340
  $('#uploadetabimg').hide();
341
  $('#uploadtabimgsel').show();
342
  switchTabs('upload');
343
}
344

    
345
//show the browse tab
346
function showBrowsePane()
347
{
348
  setCookie('sms-current-tab', 'browse');
349
  var contentLoaded = getCookie('sms-browse-content-loaded');
350
  if(!contentLoaded)
351
  {
352
    reloadSearchContent('/sms/metacat?action=query&anyfield=%25&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0');
353
  }
354
  //hide all, then slide down the browse pane
355
  $('#searchdiv').fadeOut("slow");
356
  $('#uploaddiv').fadeOut("slow");
357
  $('#browseresultdiv').fadeIn("slow");
358
  $('#browsetabimg').hide();
359
  $('#browsetabimgsel').show();
360
  switchTabs('browse');
361
}
362

    
363
function switchTabs(tab)
364
{
365
  if(tab == 'browse')
366
  {
367
    $('#searchtabimg').show();
368
    $('#uploadtabimg').show();
369
    $('#browsetabimg').hide();
370
    
371
    $('#uploadtabimgsel').hide();
372
    $('#browsetabimgsel').show();
373
    $('#searchtabimgsel').hide();
374
  }
375
  else if(tab == 'search')
376
  {
377
    $('#searchtabimg').hide();
378
    $('#uploadtabimg').show();
379
    $('#browsetabimg').show();
380
    
381
    $('#uploadtabimgsel').hide();
382
    $('#browsetabimgsel').hide();
383
    $('#searchtabimgsel').show();
384
  }
385
  else if(tab == 'upload')
386
  {
387
    $('#searchtabimg').show();
388
    $('#uploadtabimg').hide();
389
    $('#browsetabimg').show();
390
    
391
    $('#uploadtabimgsel').show();
392
    $('#browsetabimgsel').hide();
393
    $('#searchtabimgsel').hide();
394
  }
395
}
396

    
397
//slide an element up
398
function slideUp(id)
399
{
400
  $(id).slideUp("slow");
401
}
402

    
403
//slide and element down
404
function slideDown(id)
405
{
406
  $(id).slideDown("slow");
407
}
408

    
409
//set a cookie
410
function setCookie( name, value, expires, path, domain, secure ) 
411
{
412
  // set time, it's in milliseconds
413
  var today = new Date();
414
  today.setTime( today.getTime() );
415
  
416
  /*
417
  if the expires variable is set, make the correct 
418
  expires time, the current script below will set 
419
  it for x number of days, to make it for hours, 
420
  delete * 24, for minutes, delete * 60 * 24
421
  */
422
  if ( expires )
423
  {
424
    expires = expires * 1000 * 60 * 60 * 24;
425
  }
426
  var expires_date = new Date( today.getTime() + (expires) );
427
  
428
  document.cookie = name + "=" +escape( value ) +
429
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
430
  ( ( path ) ? ";path=" + path : "" ) + 
431
  ( ( domain ) ? ";domain=" + domain : "" ) +
432
  ( ( secure ) ? ";secure" : "" );
433
}
434

    
435
//get a cookie
436
function getCookie( check_name ) {
437
	// first we'll split this cookie up into name/value pairs
438
	// note: document.cookie only returns name=value, not the other components
439
	var a_all_cookies = document.cookie.split( ';' );
440
	var a_temp_cookie = '';
441
	var cookie_name = '';
442
	var cookie_value = '';
443
	var b_cookie_found = false; // set boolean t/f default f
444
	
445
	for ( i = 0; i < a_all_cookies.length; i++ )
446
	{
447
		// now we'll split apart each name=value pair
448
		a_temp_cookie = a_all_cookies[i].split( '=' );
449
		
450
		// and trim left/right whitespace while we're at it
451
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
452
	
453
		// if the extracted name matches passed check_name
454
		if ( cookie_name == check_name )
455
		{
456
			b_cookie_found = true;
457
			// we need to handle case where cookie has no value but exists (no = sign, that is):
458
			if ( a_temp_cookie.length > 1 )
459
			{
460
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
461
			}
462
			// note that in cases where cookie is initialized but no value, null is returned
463
			return cookie_value;
464
			break;
465
		}
466
		a_temp_cookie = null;
467
		cookie_name = '';
468
	}
469
	if ( !b_cookie_found )
470
	{
471
		return null;
472
	}
473
}		
(22-22/23)