Advertisement
private775

SharePoint: colorize the calendar view on SP2010 (JS)

Jun 4th, 2015
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. if(typeof acme === "undefined"){
  3.     acme = {};
  4. }
  5.  
  6. if(typeof acme.CAL === "undefined"){
  7.     acme.CAL = {};
  8. }
  9.  
  10. acme.CAL.Initialize = function(){
  11.     SP.UI.ApplicationPages.CalendarService.prototype.$7a_1_OLD__ = SP.UI.ApplicationPages.CalendarService.prototype.$7a_1;
  12.  
  13.     SP.UI.ApplicationPages.CalendarService.prototype.$7a_1 = function($p0, $p1) {
  14.         this.$7a_1_OLD__($p0, $p1);
  15.         window.setTimeout(acme.CAL.Colorize, 100);
  16.     };
  17. };
  18.  
  19. acme.CAL.Colorize = function(){
  20.     $("div.ms-acal-rootdiv").find("div.ms-acal-item").each(function(index){
  21.         var self = this;
  22.         var title = $(self).attr("title");
  23.         var colors = acme.CAL.GetColors(title);
  24.        
  25.         $(self).css("background-color",colors.BGColor);
  26.    
  27.         $(self).find("div").each(function(index){
  28.             $(this).css('color', '');
  29.             var style = $(this).attr("style");
  30.             var newStyle = "color: " + colors.FGColor + " !important; " + (style || "");
  31.             $(this).attr("style", newStyle);
  32.         });
  33.        
  34.         $(self).find("a").each(function(index){
  35.             $(this).css('color', '');
  36.             var style = $(this).attr("style");
  37.             var newStyle = "color: " + colors.FGColor + " !important; " + (style || "");
  38.             $(this).attr("style", newStyle);
  39.         });
  40.     });
  41. };
  42.  
  43. acme.CAL.GetColors = function(title){
  44.     var allColors = [
  45.         { "BGColor": "red", "FGColor": "yellow" },
  46.         { "BGColor": "pink", "FGColor": "darkred" },
  47.         { "BGColor": "yellow", "FGColor": "red" },
  48.         { "BGColor": "green", "FGColor": "white" },
  49.         { "BGColor": "blue", "FGColor": "yellow" },
  50.         { "BGColor": "orange", "FGColor": "darkred" },
  51.         { "BGColor": "black", "FGColor": "white" },
  52.         { "BGColor": "darkred", "FGColor": "yellow" },
  53.         { "BGColor": "darkgray", "FGColor": "yellow" },
  54.         { "BGColor": "aqua", "FGColor": "darkred" },
  55.         { "BGColor": "fuchsia", "FGColor": "white" },
  56.         { "BGColor": "cyan", "FGColor": "white" },
  57.         { "BGColor": "magenta", "FGColor": "white" }
  58.     ];
  59.  
  60.     var CurrentColorId = 0;
  61.     var patt = /^\s*(\d{1,2}):\d{1,2}\s*[ap]m/i;
  62.     if(patt.test(title)){
  63.         var mm = title.match(patt)[1];
  64.         CurrentColorId = Number(mm);
  65.     }
  66.    
  67.     var len = allColors.length;
  68.     var color = allColors[CurrentColorId % len];
  69.     return color;
  70. };
  71.  
  72. ExecuteOrDelayUntilScriptLoaded(acme.CAL.Initialize, "SP.UI.ApplicationPages.Calendar.js");
  73. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement