1 /** @fileoverview Bandejão widget for Janelas Virtuais/TelaSocial
  2 
  3 	@version 1.1
  4 	@author Matheus Martins Teixeira <a href="mailto:mteixeira@grad.icmc.usp.br"><mteixeira@grad.icmc.usp.br></a>
  5  */ 
  6  
  7  /*
  8  * ***** BEGIN LICENSE BLOCK *****
  9  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 10  *
 11  * The contents of this file are subject to the Mozilla Public License Version
 12  * 1.1 (the "License"); you may not use this file except in compliance with
 13  * the License. You may obtain a copy of the License at
 14  * http://www.mozilla.org/MPL/
 15  *
 16  * Software distributed under the License is distributed on an "AS IS" basis,
 17  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 18  * for the specific language governing rights and limitations under the
 19  * License.
 20  *
 21  * The Original Code is TelaSocial
 22  *
 23  * The Initial Developer of the Original Code is Taboca TelaSocial.
 24  * Portions created by the Initial Developer are Copyright (C) 2010
 25  * the Initial Developer. All Rights Reserved.
 26  *
 27  * Contributor(s):
 28  *      Matheus Teixeira <teixeira.mdk@gmail.com>
 29  *
 30  * Alternatively, the contents of this file may be used under the terms of
 31  * either the GNU General Public License Version 2 or later (the "GPL"), or
 32  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 33  * in which case the provisions of the GPL or the LGPL are applicable instead
 34  * of those above. If you wish to allow use of your version of this file only
 35  * under the terms of either the GPL or the LGPL, and not to allow others to
 36  * use your version of this file under the terms of the MPL, indicate your
 37  * decision by deleting the provisions above and replace them with the notice
 38  * and other provisions required by the GPL or the LGPL. If you do not delete
 39  * the provisions above, a recipient may use your version of this file under
 40  * the terms of any one of the MPL, the GPL or the LGPL.
 41  *
 42  * ***** END LICENSE BLOCK ***** */
 43  
 44 function decode(e) {
 45 	return e.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "");
 46 };
 47 
 48 
 49  /**
 50 	Objeto com as funções para o widget bandejão
 51 	@class
 52 	@name bandejao
 53  */
 54 var bandejao = {
 55 	/**
 56 		
 57 		@type 
 58 	*/
 59 	json : null,
 60 	/**
 61 		Url para o rss do cardápio o restaurante universitário
 62 		@type String
 63 	*/
 64 	url : USP_CARDAPIO,
 65 	/**
 66 		Variável que retorna se houveram erros
 67 		@type Boolean
 68 	*/
 69 	error : false,
 70 	/**
 71 		Objeto container para renderização
 72 		@type Dom Object
 73 	*/
 74 	container : null,
 75 	/**
 76 		Inicia o widget
 77 		@public
 78 		@function
 79 	*/
 80 	start : function() {
 81 		bandejao.container = $('#container');
 82 		bandejao.loadMenu();
 83 	},
 84 	/**
 85 		Atualiza o cardápio do restaurante universitário
 86 		@private
 87 		@function
 88 	*/
 89 	loadMenu : function() {
 90 		$.getJSON(bandejao.url).success(function(e) {
 91 			bandejao.processMenu(e.query.results.restaurante);
 92 		}).error(bandejao.error = true);
 93 	},
 94 	/**
 95 		Processa os itens atualizados do cardáio
 96 		@private
 97 		@param {JSON} e Objeto JSON com os itens do cardápio
 98 		@function
 99 	*/
100 	processMenu : function(e) {
101 		if(e[bandejao.weekDate()] != null) {
102 			var menu = new Array(2);
103 			if(e[bandejao.weekDate()].almoco.principal != null) {
104 				var lunch = e[bandejao.weekDate()].almoco;
105 				menu[0] = $('<div>').append('<h2>Almoço</h2><h2>' + decode(lunch.principal) + '</h2><h2>' + decode(lunch.acompanhamento) + '</h2><h2>' + decode(lunch.salada) + '</h2><h2>' + decode(lunch.sobremesa) + '</h2>');
106 			} else {
107 				menu[0] = $('<div>').append('<h2>Almoço</h2><h2>Não terá almoço hoje! :(</h2>');
108 			}
109 			if(e[bandejao.weekDate()].jantar.principal != null) {
110 				var dinner = e[bandejao.weekDate()].jantar;
111 				menu[1] = $('<div>').append('<h2>Janta</h2><h2>' + decode(dinner.principal) + '</h2><h2>' + decode(dinner.acompanhamento) + '</h2><h2>' + decode(dinner.salada) + '</h2><h2>' + decode(dinner.sobremesa) + '</h2>');
112 			} else {
113 				menu[1] = $('<div>').append('<h2>Janta</h2><h2>Não terá janta hoje! :(</h2>');
114 			}
115 			bandejao.container.append('<h1>Bandejão de ' + bandejao.weekDate() + '</h1>').append($('<div>').addClass('col').append(menu[0])).append($('<div>').addClass('col').append(menu[1]));
116 		} else {
117 			bandejao.container.append('<h1>Bandejão de ' + bandejao.weekDate() + '</h1>').append('<h2>Não teremos refeição hoje! :/</h2>');
118 		}
119 		setTimeout(function() {
120 			bandejao.loadMenu()
121 		}, 3 * 60 * 60 * 1000);
122 	},
123 	/**
124 		Retorna o dia da semana referente a data atual
125 		@private
126 		@returns Retorna o dia da semana referente a data atual
127 		@function
128 	*/
129 	weekDate : function() {
130 		var weekDays = new Array('domingo', 'segunda', 'terca', 'quarta', 'quinta', 'sexta', 'sabado');
131 		var today = new Date();
132 		//today.setDate(3);
133 		return weekDays[today.getDay()];
134 	}
135 };
136 
137 $(document).ready(function() {
138 	bandejao.start();
139 })