FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
tools.js
1 /*
2  Copyright (C) 2014, Siemens AG
3  Author: Johannes Najjar
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 function setCookie(cookieName, cookieValue, exdays) {
20  exdays = typeof exdays !== 'undefined' ? exdays : 1;
21  var date = new Date();
22  date.setTime(date.getTime() + (exdays * 24 * 60 * 60 * 1000));
23  var expires = "expires=" + date.toUTCString();
24  document.cookie = cookieName + "=" + cookieValue + "; " + expires;
25 }
26 
27 function getCookie(cookieName) {
28  var name = cookieName + "=";
29  var allCookies = document.cookie.split(';');
30  for (var i = 0; i < allCookies.length; i++) {
31  var theCookie = allCookies[i];
32  while (theCookie.charAt(0) == ' ') {
33  theCookie = theCookie.substring(1);
34  }
35  if (theCookie.indexOf(name) != -1) {
36  return theCookie.substring(name.length, theCookie.length);
37  }
38  }
39  return "";
40 }
41 
42 function setOption(name, value) {
43  setCookie("option." + name, value, 1);
44 }
45 
46 function getOption(name) {
47  return getCookie("option." + name);
48 }
49 
50 function getOptionDefaultTrue(name) {
51  var theCookie = getCookie("option." + name);
52  if (theCookie === "") {
53  return true;
54  }
55  else {
56  return theCookie === "true";
57  }
58 }
59 
60 function failed(jqXHR, textStatus, error) {
61  var err = textStatus + ", " + error;
62  console.log("Request Failed: " + err);
63  respondTxt = jqXHR.responseText;
64  if (respondTxt.indexOf('Module unavailable or your login session timed out.') != 0) {
65  $('#dmessage').append(respondTxt);
66  $('#dmessage').css('display', 'block');
67  }
68  else if (confirm("You are not logged in. Go to login page?")) {
69  window.location.href = "?mod=auth";
70  }
71 }
72 
73 function rmDefaultText(caller, dflt) {
74  if ($(caller).val() == dflt) {
75  $(caller).val('');
76  }
77 }
78 
79 function sortList(selector)
80 {
81  var options = $(selector);
82  var arr = options.map(function(_, o) { return { t: $(o).text(), v: o.value }; }).get();
83  arr.sort(function(o1, o2) { return o1.t.toLowerCase() > o2.t.toLowerCase() ? 1 : o1.t.toLowerCase() < o2.t.toLowerCase() ? -1 : 0; });
84  options.each(function(i, o) {
85  o.value = arr[i].v;
86  $(o).text(arr[i].t);
87  });
88 }