File: 0.01.1a/js/alerts.js (View as HTML)

  1: /* -------------------------------------------------------------
  2: This file is part of FreeDESK
  3: 
  4: FreeDESK is (C) Copyright 2012 David Cutting
  5: 
  6: FreeDESK is free software: you can redistribute it and/or modify
  7: it under the terms of the GNU General Public License as published by
  8: the Free Software Foundation, either version 3 of the License, or
  9: (at your option) any later version.
 10: 
 11: FreeDESK is distributed in the hope that it will be useful,
 12: but WITHOUT ANY WARRANTY; without even the implied warranty of
 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14: GNU General Public License for more details.
 15: 
 16: You should have received a copy of the GNU General Public License
 17: along with FreeDESK.  If not, see www.gnu.org/licenses
 18: 
 19: For more information see www.purplepixie.org/freedesk/
 20: -------------------------------------------------------------- */
 21: 
 22: /**
 23:  * The alert pane manager for FreeDESK
 24: **/
 25: function FreeDESK_AlertPane()
 26: {
 27: 	this.container = document.getElementById('alert_pane');
 28: 	
 29: 	this.random = function()
 30: 	{
 31: 		var len=32;
 32: 		var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
 33: 		var randomstring = "";
 34: 		for (var i=0; i<len; ++i)
 35: 		{
 36: 			var randchar = Math.floor(Math.random() * chars.length);
 37: 			randomstring += chars.substring(randchar, randchar+1);
 38: 		}
 39: 		return randomstring;
 40: 	}
 41: 	
 42: 	// Add an alert
 43: 	// text: text to appear
 44: 	// level: 0 green, 1 (default) yellow, 2 red
 45: 	// autoRemove: seconds before autoRemove (0 = keep forever), default 5s
 46: 	this.add = function(text, level, autoRemove)
 47: 	{
 48: 		if (level == undefined)
 49: 			var level = 1;
 50: 		if (autoRemove == undefined)
 51: 			var autoRemove = 5;
 52: 			
 53: 		if (this.container == null) // not initialised at startup
 54: 			this.container = document.getElementById('alert_pane');
 55: 		var div = document.createElement('div');
 56: 		var id = "alert_"+this.random();
 57: 		div.setAttribute('id', id);
 58: 		div.className = "alert_"+level;
 59: 		
 60: 		var content = document.createElement('span');
 61: 		content.innerHTML = text;
 62: 		
 63: 		var close = document.createElement('span');
 64: 		close.className = "alertclose";
 65: 		close.innerHTML = "<a href=\"#\" onclick=\"Alerts.close('"+id+"');\">X</a>";
 66: 		
 67: 		div.appendChild(content);
 68: 		div.appendChild(close);
 69: 		
 70: 		this.container.appendChild(div);
 71: 		
 72: 		if (autoRemove > 0)
 73: 		{
 74: 			setTimeout(function(){Alerts.close(id); }, autoRemove*1000);
 75: 		}
 76: 	}
 77: 	
 78: 	this.close = function(id)
 79: 	{
 80: 		var child = document.getElementById(id);
 81: 		if (child != undefined)
 82: 			this.container.removeChild(child);
 83: 	}
 84: 	
 85: 	this.clear = function()
 86: 	{
 87: 		if (this.container.hasChildNodes())
 88: 		{
 89: 			while(this.container.childNodes.length >= 1)
 90: 				this.container.removeChild(this.container.firstChild);
 91: 		}
 92: 	}
 93: 	
 94: 	this.randomTest = function()
 95: 	{
 96: 		var lvl = Math.floor(Math.random() * 3);
 97: 		var txt = "Hello "+this.random()+ " "+lvl;
 98: 		this.add(txt, lvl);
 99: 	}
100: }
101: 
102: Alerts = new FreeDESK_AlertPane();
103: 
104: