Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurel Schwitter committed Jan 25, 2018
0 parents commit a1b3635
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>

<html>

<head>
<title>ColoredCalendar</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.js" type="text/javascript"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="colorcalendar.js" type="text/javascript"></script>
</head>

<body>
<div class="container">
<h1>Get your Color</h1>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2"><span>Enter your event title</span></div>
<div class="col-sm-10"><input type="text" id="testInput" /></div>
</div>
</div>
</div>
<script type="text/javascript">
$("#testInput").keydown(function(){
var a = GetColorCodeFromCategory(GetName($(this).val().toString()));
$("body").css("background-color", "rgb("+ a + ")");
$("h1, span").css("color", GetForegroundColor(a));
})
</script>
</body>

</html>
41 changes: 41 additions & 0 deletions Test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<! DOCTYPE html>

<html>

<head>
<title>Test</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.js" type="text/javascript"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<script src="colorcalendar.js" type="text/javascript"></script>
</head>

<body>
<div class="container">
<h1>Test</h1>
<div class="container-fluid">
<div class="row"><input type="text" id="testInput" />
</div>
<div class="row">

</div>
</div>
</div>
<script type="text/javascript">
$("#testInput").keydown(function(){
var a = GetColorCodeFromCategory(GetName($(this).val().toString()));
$("body").css("background-color", "rgb("+ a + ")");
})
</script>
</body>

</html>
118 changes: 118 additions & 0 deletions colorcalendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Creator: Aurel Schwitter
Version: 1.0
Date: 26.01.2017
Integrate to Sharepoint:
1) Upload to public Document library
2) Copy url of uploaded Document
3) Add Script editor on specified Page
3.1) Paste following
<script src="{url to script}" type="text/javascript"></script>
*/


// Editable elements
// Separators separating Name and Text
var COL_CAL_SEPARATORS = [ ":", "-", " "];
// Random values for different colors
var CAL_RANDOM = 1.4;
var CAL_RANDOM_2 = 5;


// Check for Query
if (typeof $ == undefined){
throw "jQuery needed";
} else {
// Load WaitForTheGodDamnSP() when Page is ready
_spBodyOnLoadFunctionNames.push("WaitForTheGodDamnSP");
}

// Waits for Sharepoint to load everything (1000ms)
function WaitForTheGodDamnSP (){
LoadSodByKey("SP.UI.ApplicationPages.Calendar.js", function () {
window.setTimeout(ColorCalendar, 1000);
});
}

// Colores the calendar
function ColorCalendar() {
// Get all calendar items
var $calitems = $(".ms-acal-item");

// check if any exists
if ($calitems.length > 0) {
$calitems.each(function (i) {
var $box = $(this);
var $text = $box.find("a");
var text = $text[0].innerHTML;

// Is calendar already colored?
if ( text.indexOf(">") == -1){

// Get color from Name
var colors = GetColorCodeFromCategory(GetName(text));
// Convert color array to rgb(R,G,B)
var rgbcolors = "rgb(" + colors + ")";

// calculate black or white Text
var fcolor = GetForegroundColor(colors);
// Set foreground color
$box.css("background-color", rgbcolors);
$box.find("div, a").wrapInner("<span style=\"color:"+fcolor+"\"></span>");

}
});

}
// rerun this function in 2s
window.setTimeout(ColorCalendar, 1000);
};

// Generate number from string
function HashCode (str) {
var hash = 0, i, chr, len;
if (str.length === 0) return hash;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

// Get Name from category
function GetName(originalText) {
var res = "";
COL_CAL_SEPARATORS.forEach(function(e) {
if (originalText.indexOf(e) > -1){
res = originalText.split(e)[0].trim();
return false;
}
});
return res === "" ? originalText : res;
}

function GetColorCodeFromCategory(category) {
if (category === undefined) return [0,0,0];
var bgcolor = null;
var fgcolor = null;

var hash = HashCode(category.trim());
hash = hash * CAL_RANDOM;

// Convert to positive number
// Randomize negative numbers
// Remove any decimal points
hash = Math.floor(hash < 0 ? Math.abs(hash / CAL_RANDOM_2) : hash);

// Convert decimal to r,g,b array
return [(hash & 0xff0000) >> 16, (hash & 0x00ff00) >> 8, (hash & 0x0000ff)];

}

function GetForegroundColor (rgb){
if (rgb.length != 3) return "#000000 !important";
var rounded = Math.round((rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000);
return rounded > 150 ? "#000000 !important" : "#ffffff !important";
}
Binary file added colorcalendar.zip
Binary file not shown.

0 comments on commit a1b3635

Please sign in to comment.