Compare commits

...

9 Commits

2 changed files with 76 additions and 32 deletions

View File

@ -41,7 +41,8 @@ Strolch = {
// do nothing
},
hasAuthToken: function () {
return this.getCookie("strolch.authorization") !== "";
var cookie = this.getCookie("strolch.authorization");
return cookie != null && cookie !== "";
},
getUserConfig: function () {
return this.props.userConfig;
@ -64,13 +65,13 @@ Strolch = {
if (userConfig && this.isNotEmptyString(userConfig.locale)) {
userLocale = userConfig.locale;
if (userLocale.length > 2) {
userLocale = userLocale.substr(0, 2).toLowerCase();
userLocale = userLocale.substring(0, 2).toLowerCase();
}
} else {
if (navigator.languages && navigator.languages.length > 0) {
userLocale = navigator.languages[0].substr(0, 2).toLowerCase();
userLocale = navigator.languages[0].substring(0, 2).toLowerCase();
} else if (navigator.language) {
userLocale = navigator.language.substr(0, 2).toLowerCase();
userLocale = navigator.language.substring(0, 2).toLowerCase();
} else {
userLocale = "en";
}
@ -160,11 +161,34 @@ Strolch = {
/*
* Utils
*/
clearQueryParams: function () {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2) return;
document.location.hash = hashParts[0];
},
getQueryParamsAsString: function () {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2) return '';
return hashParts[1];
return decodeURIComponent(hashParts[1]);
},
getQueryParamsAsObject: function () {
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2) return {};
var result = {};
var queryParams = hashParts[1];
var queryArr = queryParams.split('&');
for (var i = 0; i < queryArr.length; i++) {
var queryParam = queryArr[i].split('=');
if (queryParam.length !== 2) continue;
result[decodeURIComponent(queryParam[0])] = decodeURIComponent(queryParam[1]);
}
return result;
},
getQueryParamValue: function (paramName) {
@ -178,8 +202,8 @@ Strolch = {
for (var i = 0; i < queryArr.length; i++) {
var queryParam = queryArr[i].split('=');
if (queryParam.length !== 2) continue;
var name = queryParam[0];
var value = queryParam[1];
var name = decodeURIComponent(queryParam[0]);
var value = decodeURIComponent(queryParam[1]);
if (name === paramName && this.isNotEmptyString(value)) {
return value;
}
@ -188,25 +212,27 @@ Strolch = {
return null;
},
setQueryParamValue: function (paramName, paramValue) {
var encodedName = encodeURIComponent(paramName);
var encodedValue = this.isEmptyString(paramValue) ? null : encodeURIComponent(paramValue);
var hash = document.location.hash;
var hashParts = hash.split('?');
if (hashParts.length !== 2) {
if (this.isNotEmptyString(paramValue)) {
if (encodedValue != null) {
if (hash.endsWith("/"))
document.location.hash = hash + '?' + paramName + '=' + paramValue;
document.location.hash = hash + '?' + encodedName + '=' + encodedValue;
else
document.location.hash = hash + '/?' + paramName + '=' + paramValue;
document.location.hash = hash + '/?' + encodedName + '=' + encodedValue;
} else if (hash.endsWith("/")) {
document.location.hash = hash.substr(0, hash.length - 2);
document.location.hash = hash.substring(0, hash.length - 2);
}
return;
}
if (this.isEmptyString(hashParts[1])) {
if (this.isNotEmptyString(paramValue)) {
document.location.hash = hashParts[0] + '?' + paramName + '=' + paramValue;
if (encodedValue != null) {
document.location.hash = hashParts[0] + '?' + encodedName + '=' + encodedValue;
}
return;
}
@ -220,7 +246,7 @@ Strolch = {
for (var i = 0; i < queryArr.length; i++) {
var query = queryArr[i];
var queryParam = query.split('=');
if (queryParam.length !== 2 || queryParam[0] !== paramName) {
if (queryParam.length !== 2 || queryParam[0] !== encodedName) {
if (!first)
hash += '&';
first = false;
@ -228,27 +254,27 @@ Strolch = {
continue;
}
if (this.isNotEmptyString(paramValue)) {
if (encodedValue != null) {
if (!first)
hash += '&';
first = false;
hash += paramName + '=' + paramValue;
hash += encodedName + '=' + encodedValue;
}
set = true;
}
if (!set && this.isNotEmptyString(paramValue)) {
if (!set && encodedValue != null) {
if (!first)
hash += '&';
hash += paramName + '=' + paramValue;
hash += encodedName + '=' + encodedValue;
}
if (hash.charAt(hash.length - 1) === "?") {
if (hash.charAt(hash.length - 2) === "/")
hash = hash.substr(0, hash.length - 2);
hash = hash.substring(0, hash.length - 2);
else
hash = hash.substr(0, hash.length - 1);
hash = hash.substring(0, hash.length - 1);
}
document.location.hash = hash
@ -267,7 +293,7 @@ Strolch = {
return c.substring(name.length, c.length);
}
}
return "";
return null;
},
setCookie: function (cname, cvalue, expiration) {
console.log("Setting cookie " + cname);
@ -281,7 +307,7 @@ Strolch = {
expires = expiration.toUTCString();
} else if (typeof expiration == "number") {
var d = new Date();
d.setTime(d.getTime() + (validDays * 24 * 60 * 60 * 1000));
d.setTime(d.getTime() + (expiration * 24 * 60 * 60 * 1000));
expires = d.toUTCString();
} else {
var d = new Date();
@ -289,11 +315,11 @@ Strolch = {
expires = d.toUTCString();
}
document.cookie = cname + "=" + cvalue + ";expires=" + expires + ";path=/";
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/;SameSite=Strict;domain=" + document.domain;
},
deleteCookie: function (cname) {
console.log("Deleting cookie " + cname);
document.cookie = cname + '=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = cname + "=; Path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;SameSite=Strict;domain=" + document.domain;
},
uuid: function () {
@ -314,11 +340,13 @@ Strolch = {
},
isFloat: function (val) {
return Number(parseFloat(val)) === val;
// noinspection EqualityComparisonWithCoercionJS
return parseFloat(val).toString() == val;
},
isInteger: function (val) {
return Number(parseInt(val)) === val;
// noinspection EqualityComparisonWithCoercionJS
return parseInt(val).toString() == val;
},
isDate: function (val) {
@ -450,11 +478,15 @@ Strolch = {
return date.toLocaleDateString('de-CH')
},
toLocalDateTime: function (val) {
toLocalDateTime: function (val, withSeconds) {
if (this.isEmptyString(val) || val === '-') return '-';
var date = new Date(val);
if (this.props.locale != null) return date.toLocaleDateString(this.props.locale) + ' ' + date.toLocaleTimeString(this.props.locale);
return date.toLocaleDateString('de-CH') + ' ' + date.toLocaleTimeString('de-CH');
var locale = this.props.locale != null ? this.props.locale : "de-CH";
if (withSeconds) {
return date.toLocaleDateString(locale) + ' ' + date.toLocaleTimeString(locale);
} else {
return date.toLocaleDateString(locale) + ' ' + this.getTimeString(date);
}
},
toDateTime: function (val) {
@ -500,7 +532,10 @@ Strolch = {
// gets the calendar date as displayed in the UI
getDateString: function (datetime, addCentury) {
if (typeof (datetime) === 'string') {
if (datetime == null)
return "";
if (typeof (datetime) == 'string') {
datetime = new Date(datetime);
}
@ -516,6 +551,15 @@ Strolch = {
// gets the date of a date string from getDateString()
getDate: function (datetimeString) {
if (datetimeString == null || datetimeString.length === "")
return null;
if (datetimeString.indexOf("T") === 10) {
return new Date(datetimeString.substring(0, 10));
} else if (datetimeString.length === 10 && datetimeString.indexOf("-") === 4) {
return new Date(datetimeString);
}
var splitString = datetimeString.split(".");
if (splitString.length !== 3) return null;

View File

@ -1,7 +1,7 @@
{
"name": "strolchjs",
"version": "0.4.2",
"main": "strolch.js",
"version": "0.5.3",
"main": "Strolch.js",
"ignore": [
"**/.*",
"node_modules",