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
  if(getCookie("sms-login") == "true")
92
  {
93
    setLoginHeader(true);
94
    showDatasets();
95
    showSearchPane();
96
  }
97
  else
98
  {
99
    setLoginHeader(false);
100
  }
101
}
102

    
103
//search the document base in metacat
104
function search()
105
{
106
  var searchval = document.getElementById("searchtextbox").value
107
  var url = '/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0';
108
  setCookie("sms-searchval", searchval);
109
  setCookie("sms-pagestart", 0);
110
  reloadSearchContent(url);
111
}
112

    
113
//show all of the datasets in metacat
114
function showDatasets()
115
{
116
  var searchval = getCookie('sms-searchval');
117
  if(searchval == null || searchval == '')
118
  {
119
    searchval = '%25';
120
  }
121
  var page = getCookie('sms-pagestart');
122
  if(page)
123
  {
124
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=' + page);
125
  }
126
  else
127
  {
128
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0');
129
  }
130
}
131

    
132
//reload the search result table
133
function reloadSearchContent(url)
134
{
135
  var page = url.substring(url.indexOf('pagestart=') + 10, url.length);
136
  setCookie("sms-pagestart", page);
137
  $("#searchresulttable").load(url);
138
}
139

    
140
//upload a file to metacat
141
function uploadfile()
142
{
143
  if(getCookie("sms-login") != "true")
144
  {
145
    alert('You cannot upload.  You are not logged in.');
146
    return;
147
  }
148
  if(!checkId(true))
149
  { //make sure the id is valid
150
    alert('The ID prefix you chose is not valid.  The prefix must be a string of alpha characters only.');
151
  }
152
  else
153
  {
154
    if(document.getElementById("datafile").value == null || document.getElementById("datafile").value == "")
155
    {
156
      alert('You must choose a file to upload.');
157
      return;
158
    }
159
    getId(true, true, true);
160
  }
161
}
162

    
163
//make a document public
164
function makepublic(docid)
165
{
166
  $.get("/sms/metacat?action=setaccess&docid=" + docid + 
167
        "&principal=public&permission=read&permType=allow&permOrder=allowFirst",
168
        function(data) {
169
          if(data.indexOf("<success>") != -1)
170
          {
171
            slideUp("#uploadstatus");
172
            $("#uploadstatus").html('<p>The document with id ' + 
173
              '<a href="/sms/metacat?action=read&docid=' + docid + '&qformat=sms">' + docid + 
174
              '</a> is now publicly readable.</p>');
175
            slideDown("#uploadstatus");
176
            //alert('success: ' + "#makepublic");
177
            //slideUp("#makepublic");
178
          }
179
          else
180
          {
181
            alert('The access control changes for ' + docid + ' failed.  It is not publicly readable.');
182
          }
183
        }, "XML");
184
}
185

    
186
//get the next id and put it in the id text boxes
187
function getId(setFields, setForm, submitForm)
188
{
189
  if(setFields == null)
190
  {
191
    setFields = true;
192
  }
193
  
194
  if(setForm == null)
195
  {
196
    setForm = false;
197
  }
198
  
199
  if(submitForm == null)
200
  {
201
    submitForm = false;
202
  }
203
  
204
  var scopeStr = document.getElementById("docidfamily").value;
205
  //var scopeStr = $('#docidfamily').value;
206
  if(scopeStr == '' || scopeStr == null)
207
  {
208
    scopeStr = "sms";
209
  }
210
  
211
  $.get("metacat", {action:"getlastdocid", scope:scopeStr}, 
212
      function(data)
213
      {
214
        var docid = data.substring(data.indexOf("<docid>") + 7, data.indexOf("</docid>"));
215
        var nextid;
216
        if(docid == 'null')
217
        {
218
          nextid = 1;
219
        }
220
        else
221
        {
222
          nextid = docid.substring(docid.indexOf(".") + 1, docid.lastIndexOf("."));
223
          nextid++;
224
          //nextid = scopeStr + nextid + ".1"; 
225
        }
226
        //$('#docidtextfield').val(nextid);
227
        if(setFields)
228
        {
229
          $('#docidfamily').val(scopeStr);
230
          $('#docidnumber').val(nextid);
231
          $('#docidrevision').val("1");
232
        }
233
        fullDocId = scopeStr + "." + nextid + ".1";
234
        //alert('fullDocId: ' + fullDocId);
235
        if(setForm)
236
        {
237
          //alert('setting docid to ' + fullDocId);
238
          $('#docid').val(fullDocId);
239
        }
240
        
241
        if(submitForm)
242
        {
243
          $("form").submit();
244
        }
245
      }, 
246
      "XML");
247
}
248

    
249
//check for a valid docid
250
function checkId(setForm)
251
{
252
  if(setForm == null)
253
  {
254
    setForm = false;
255
  }
256
  getId(false, setForm, false);
257
  var scopeStr = document.getElementById("docidfamily").value;
258
  var numberStr = document.getElementById("docidnumber").value;
259
  var userDocid = scopeStr + "." + numberStr; 
260
  
261
  //fullDocId is a global var that gets set by getId()
262
  var nextnum = fullDocId.substring(fullDocId.indexOf(".") + 1, fullDocId.lastIndexOf("."));
263
  var regexp = "[^[[a-z]|[A-Z]]+]"; //search for anything thats not an alpha 
264
  var re = new RegExp(regexp);
265
  var match = re.test(scopeStr);
266
  if(match)
267
  { //if it matches, reject
268
    return false;
269
  }
270
  
271
  return true;
272
}
273

    
274
//show the search tab
275
function showSearchPane()
276
{
277
  //hide all, then slide down the search pane
278
  $('#uploaddiv').fadeOut("slow");
279
  $('#searchresultdiv').fadeOut("slow");
280
  $('#searchdiv').fadeIn("slow");
281
  switchTabs('search');
282
}
283

    
284
//show the upload tab
285
function showUploadPane()
286
{
287
  //hide all, then slide down the upload pane
288
  $('#searchdiv').fadeOut("slow");
289
  $('#searchresultdiv').fadeOut("slow");
290
  $('#uploaddiv').fadeIn("slow");
291
  $('#uploadetabimg').hide();
292
  $('#uploadtabimgsel').show();
293
  switchTabs('upload');
294
}
295

    
296
//show the browse tab
297
function showBrowsePane()
298
{
299
  //hide all, then slide down the browse pane
300
  $('#searchdiv').fadeOut("slow");
301
  $('#uploaddiv').fadeOut("slow");
302
  $('#searchresultdiv').fadeIn("slow");
303
  $('#browsetabimg').hide();
304
  $('#browsetabimgsel').show();
305
  switchTabs('browse');
306
}
307

    
308
function switchTabs(tab)
309
{
310
  if(tab == 'browse')
311
  {
312
    $('#searchtabimg').show();
313
    $('#uploadtabimg').show();
314
    $('#browsetabimg').hide();
315
    
316
    $('#uploadtabimgsel').hide();
317
    $('#browsetabimgsel').show();
318
    $('#searchtabimgsel').hide();
319
  }
320
  else if(tab == 'search')
321
  {
322
    $('#searchtabimg').hide();
323
    $('#uploadtabimg').show();
324
    $('#browsetabimg').show();
325
    
326
    $('#uploadtabimgsel').hide();
327
    $('#browsetabimgsel').hide();
328
    $('#searchtabimgsel').show();
329
  }
330
  else if(tab == 'upload')
331
  {
332
    $('#searchtabimg').show();
333
    $('#uploadtabimg').hide();
334
    $('#browsetabimg').show();
335
    
336
    $('#uploadtabimgsel').show();
337
    $('#browsetabimgsel').hide();
338
    $('#searchtabimgsel').hide();
339
  }
340
}
341

    
342
//slide an element up
343
function slideUp(id)
344
{
345
  $(id).slideUp("slow");
346
}
347

    
348
//slide and element down
349
function slideDown(id)
350
{
351
  $(id).slideDown("slow");
352
}
353

    
354
//set a cookie
355
function setCookie( name, value, expires, path, domain, secure ) 
356
{
357
  // set time, it's in milliseconds
358
  var today = new Date();
359
  today.setTime( today.getTime() );
360
  
361
  /*
362
  if the expires variable is set, make the correct 
363
  expires time, the current script below will set 
364
  it for x number of days, to make it for hours, 
365
  delete * 24, for minutes, delete * 60 * 24
366
  */
367
  if ( expires )
368
  {
369
    expires = expires * 1000 * 60 * 60 * 24;
370
  }
371
  var expires_date = new Date( today.getTime() + (expires) );
372
  
373
  document.cookie = name + "=" +escape( value ) +
374
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
375
  ( ( path ) ? ";path=" + path : "" ) + 
376
  ( ( domain ) ? ";domain=" + domain : "" ) +
377
  ( ( secure ) ? ";secure" : "" );
378
}
379

    
380
//get a cookie
381
function getCookie( check_name ) {
382
	// first we'll split this cookie up into name/value pairs
383
	// note: document.cookie only returns name=value, not the other components
384
	var a_all_cookies = document.cookie.split( ';' );
385
	var a_temp_cookie = '';
386
	var cookie_name = '';
387
	var cookie_value = '';
388
	var b_cookie_found = false; // set boolean t/f default f
389
	
390
	for ( i = 0; i < a_all_cookies.length; i++ )
391
	{
392
		// now we'll split apart each name=value pair
393
		a_temp_cookie = a_all_cookies[i].split( '=' );
394
		
395
		// and trim left/right whitespace while we're at it
396
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
397
	
398
		// if the extracted name matches passed check_name
399
		if ( cookie_name == check_name )
400
		{
401
			b_cookie_found = true;
402
			// we need to handle case where cookie has no value but exists (no = sign, that is):
403
			if ( a_temp_cookie.length > 1 )
404
			{
405
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
406
			}
407
			// note that in cases where cookie is initialized but no value, null is returned
408
			return cookie_value;
409
			break;
410
		}
411
		a_temp_cookie = null;
412
		cookie_name = '';
413
	}
414
	if ( !b_cookie_found )
415
	{
416
		return null;
417
	}
418
}		
(22-22/23)