1 /** @fileoverview Agenda widget for Janelas Virtuais/TelaSocial
  2 	
  3 	@version 1.0
  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  *      @author Marcio Galli   <mgalli@taboca.com>
 29  *      @author Rafael Sartori <faelsartori@gmail.com>
 30  *      @author Matheus Teixeira <teixeira.mdk@gmail.com>
 31  *
 32  * Alternatively, the contents of this file may be used under the terms of
 33  * either the GNU General Public License Version 2 or later (the "GPL"), or
 34  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 35  * in which case the provisions of the GPL or the LGPL are applicable instead
 36  * of those above. If you wish to allow use of your version of this file only
 37  * under the terms of either the GPL or the LGPL, and not to allow others to
 38  * use your version of this file under the terms of the MPL, indicate your
 39  * decision by deleting the provisions above and replace them with the notice
 40  * and other provisions required by the GPL or the LGPL. If you do not delete
 41  * the provisions above, a recipient may use your version of this file under
 42  * the terms of any one of the MPL, the GPL or the LGPL.
 43  *
 44  * ***** END LICENSE BLOCK ***** */
 45 
 46  /**
 47 	Objeto com as funções para o widget agenda
 48 	@class
 49 	@name agenda
 50  */
 51 var agenda = {
 52 	/** 
 53 	Array com as url's para os eventos
 54 	@type array
 55 	*/
 56 	urlFeed : ICMC_EVENTOS,
 57 	/**
 58 	Lista dos eventos do feed rss 
 59 	@type array
 60 	*/
 61 	feedList : null,
 62 	/**
 63 	Lista dos eventos no widget
 64 	@type array
 65 	*/
 66 	feedEvents : null,
 67 	/** 
 68 	Lista dos defesas de teses no widget 
 69 	@type array
 70 	*/
 71 	feedTheses : null,
 72 	/** 
 73 	Objeto que irá conter os eventos
 74 	@type DOM Object
 75 	*/
 76 	container : null,
 77 	/** 
 78 	Número máximo de itens renderizados (tamanho da tela/tamanho de um item)
 79 	@type int
 80 	*/
 81 	maxItens : window.innerHeight / 150,
 82 	/** 
 83 	Tempo para atualização dos feeds. 
 84 	@type int
 85 	*/
 86 	timeRefresh : 90, // in seconds
 87 	/** 
 88 	Erros do widget 
 89 	@type Array
 90 	*/
 91 	status : Array(0, 0), /* 0=emptyorerror, 1=loaded, 2=processed */
 92 	/**
 93 		Inicializa o widget agenda
 94 		@param {DOM Object} obj Container dos eventos
 95 		@public
 96 		@function
 97 	*/
 98 	init : function(obj) {
 99 		agenda.container = $(obj);
100 		setTimeout(function() {
101 			agenda.start();
102 		}, 100);
103 		var refresh = setInterval(function() {
104 			agenda.start();
105 		}, agenda.timeRefresh * 1000);
106 	},
107 	/**
108 		Inicia o widget
109 		O método {@link agenda.init} chama está função
110 		@private
111 		@function
112 	*/
113 	start : function() {
114 		agenda.container.html('');
115 		$('<input>').attr('type', 'hidden').attr('id', 'hascontent').attr('id', 'hascontent').appendTo($('body'));
116 		agenda.feedList = new Array();
117 		agenda.feedEvents = null;
118 		agenda.feedTheses = null;
119 		agenda.status = new Array(0, 0);
120 		setTimeout(function() {
121 			agenda.updateFeed()
122 		}, 100);
123 	},
124 	/**
125 		Atualiza a lista de itens do rss para o widget.
126 		O método {@link agenda.start} chama está função
127 		@private
128 		@function
129 	*/
130 	updateFeed : function() {
131 		$('#hascontent').attr('value', '0');
132 		if(agenda.status[0] == 0)
133 			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select * from rss where url=\"" + agenda.urlFeed[0] + "\"&format=json").success(function(e) {
134 				agenda.feedEvents = e.query.results.item;
135 				agenda.status[0] = 1;
136 			}).error(agenda.status[0] = 0);
137 
138 		if(agenda.status[1] == 0)
139 			$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select * from rss where url=\"" + agenda.urlFeed[1] + "\"&format=json").success(function(e) {
140 				agenda.feedTheses = e.query.results.item;
141 				agenda.status[1] = 1;
142 			}).error(agenda.status[1] = 0);
143 
144 		setTimeout(function() {
145 			if(agenda.status[0] == 1 && agenda.status[1] == 1){
146 				agenda.process();
147 			}else
148 				agenda.updateFeed();
149 		}, 200);
150 	},
151 	/**
152 		Processa os itens atualizados na lista de eventos e cria um arrayList pré formatado para os eventos.
153 		O método {@link agenda.updateFeed} chama está função.
154 		@private
155 		@function
156 	*/
157 	process : function() {
158 		agenda.status[0] = 2;
159 		for(var i = 0; agenda.feedEvents != null && i < agenda.feedEvents.length; i++) {
160 			agenda.feedEvents[i].type = 'event';
161 			agenda.feedEvents[i].pubDate = new Date(agenda.feedEvents[i].pubDate);
162 			agenda.feedList.push(agenda.feedEvents[i]);
163 		}
164 
165 		agenda.status[1] = 2;
166 		for(var i = 0; agenda.feedTheses != null && i < agenda.feedTheses.length; i++) {
167 			agenda.feedTheses[i].type = 'theses';
168 			agenda.feedTheses[i].pubDate = new Date(agenda.feedTheses[i].pubDate);
169 			agenda.feedList.push(agenda.feedTheses[i]);
170 		}
171 		agenda.render();
172 	},
173 	/**
174 		Renderiza os objetos no container do widget. Limite dado por {@link agenda.maxItens}
175 		Após processar os itens, o método {@link agenda.process} chama está função
176 		@private
177 		@function
178 	*/
179 	render : function() {
180 		agenda.container.html('');
181 		$('#hascontent').attr('value', '1');
182 		agenda.feedList.sort(agenda.compareDate);
183 		for(var i = 0; i < agenda.feedList.length && i < agenda.maxItens; i++) {
184 			agenda.container.append(agenda.printItem(agenda.feedList[i]));
185 		}
186 	},
187 	/**
188 		Função auxiliar para ordenação dos eventos por data.
189 		@param {DOM Object} a Item para comparação
190 		@param {DOM Object} b Item para comparação
191 		@returns {int} 0 se igual, 1 se a é mais recente e -1 se a é mais antigo.
192 		@private
193 		@function
194 	*/
195 	compareDate : function(a, b) {
196 		if(a.pubDate < b.pubDate)
197 			return -1;
198 		else if(a.pubDate > b.pubDate)
199 			return 1;
200 		else
201 			return 0;
202 	},
203 	/**
204 		Função auxiliar que retorna uma string com a formatação de data e hora para pt-br.
205 		@param {Object} data Objeto a ser formatado
206 		@returns {String} Data formatada para pt-br. Ex. Domingo, 2 de Janeiro ás 12:00h
207 		@private
208 		@function
209 	*/
210 	formatDate : function(data) {
211 		var months = Array('Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
212 		var weekDay = Array('Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado');
213 		var time = (data.getHours() < 10 ? '0' + data.getHours() : data.getHours()) + ":" + (data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes());
214 
215 		return (weekDay[data.getDay()] + ", " + data.getDate() + ' de ' + months[data.getMonth()] + ' ás ' + time + 'h');
216 	},
217 	/**
218 		Formata os objetos para códigos HTML para renderização.
219 		O método {@link agenda.render} chama esse método no momento da renderização.
220 		@param {Object} obj Objeto para codificação
221 		@returns {DOM Object} Objeto DOM para ser renderizado.
222 		@private
223 		@function
224 	*/
225 	printItem : function(obj) {
226 		if(obj.type == 'event')
227 			var bg = 'bg3';
228 		else
229 			var bg = 'bg2';
230 		var item = $('<div>').addClass('item').addClass(bg);
231 		var title = $('<div>').addClass('line').addClass('title').html('<span>' + obj.title + '</span>');
232 		var author = $('<div>').addClass('line').addClass('column0').html('<sub>Palestrante</sub><span>' + obj.author + '</span>');
233 		var datetime = $('<div>').addClass('line').addClass('column1').html('<sub>Data - Horário</sub><span>' + agenda.formatDate(obj.pubDate) + '</span>');
234 		var category = $('<div>').addClass('line').addClass('column0').html('<sub>Categoria</sub><span>' + obj.category + '</span>');
235 		var local = $('<div>').addClass('line').addClass('column1').html('<sub>Local</sub><span>' + obj.local + '</span>');
236 		item.append(title).append(author).append(datetime).append(category).append(local);
237 
238 		return item;
239 	},
240 }
241