Code ExtJS Sencha for the GreveApp


Ext.onReady(function(){

Ext.QuickTips.init();

Ext.define("HAGreve",{
extend:'Ext.data.Model',

fields : [{
name : 'Descricao',
mapping : 'description'
},{
name: 'Link',
mapping:'source_link'
},{
name:'TodoDia',
mapping:'all_day'
},{
name:'Cancelada',
mapping:'canceled'
},{
name : 'Companhia',
mapping : "company['name']"
},{
name : 'Data_Início',
mapping : 'start_date'
},{
name : 'Data_Fim',
mapping : 'end_date'
}]
});

var store = Ext.create("Ext.data.Store",{
model:'HAGreve',
autoLoad:true,
proxy:{
type:'jsonp',
callbackKey: 'callback',
url:'http://hagreve.com/api/v2/strikes',
headers:{'Accept':'application/json'
},
reader: {
type: 'json',
root: 'root'
}
}
});

var grid = Ext.create('Ext.grid.Panel',{
store:store,
width:750,
scope:this,
id:'gridHaGreve',
columnLines:true,
//bbar:['<em>&copy; 2014 '+ Ext.String.format('<a href="http://www.vidalnelson.no.sapo.pt" target="_blank" title="Nelson Silva">Nelson Silva</a>')+'</em>',
//     {xtype:'tbfill'}],

columns:[{
text:'Empresa',
dataIndex:'Companhia',
flex:0.8,
align:'left',
style: 'text-align:center',
renderer:getEmpresa.greve
},{
text:'Descrição',
dataIndex:'Descricao',
flex:2,
align:'left',
style: 'text-align:center',
renderer:addTool.tip
},{
text:'Data Início',
dataIndex:'Data_Início',
flex:1,
align:'left',
style: 'text-align:center',
renderer:checkDate.fire
},{
text:'Data de Fim',
dataIndex:'Data_Fim',
flex:1,
align:'left',
style: 'text-align:center',
},{
text:'Fonte',
dataIndex:'Link',
flex:0.5,
align:'left',
style: 'text-align:center',
renderer:link.redirect
},{
text:'Duração',
dataIndex:'TodoDia',
flex:0.6,
align:'left',
style: 'text-align:center',
renderer:duration
},{
text:'Cancelada',
dataIndex:'Cancelada',
flex:0.8,
align:'left',
style: 'text-align:center',
renderer:canceled
}]

});

Ext.window.Window.override({
animateTarget: Ext.getDoc(),
maximize: function(){
this.callParent([true]);
},
restore: function(){
this.callParent([true]);
}
});

var win = Ext.create('Ext.window.Window',{
layout:'fit',
id:"Win",
title:'HÁ Greve?',
iconCls:'icon-greve',
border:false,
constrain: true,
height:250,
autoShow:true,
draggable:true,
resizable:false,
items:[grid],
tools:[{
type:'refresh',
handler:function(event, toolEl, panelHeader){
store.reload();
}
}]

}).show();

});

var addTool = addTool || {};
addTool={
tip:function(value, metadata){
metadata.tdAttr = 'data-qtip="' + value + '"';
return value;
}
}

var link = link || {};
link = {
redirect:function(value, metadata){
var lnk = Ext.String.format('<a href="{0}" target="_blank">{1}</a>', value, '<img src="img/arrow.png" />');
//metadata.tdAttr = 'style="background:url(img/arrow.png) no-repeat 2px 1px; padding-left: 16px;"';
return lnk;
}
}

var getEmpresa =  getEmpresa || {};

getEmpresa.greve = function(value, meta){

switch(value){
case 'CP':
meta.tdCls = 'CP-cell';
return value = 'CP';
break;

case 'Refer':
meta.tdCls = 'CP-cell';
return value = 'Refer';
break;

case 'Fertagus':
meta.tdCls = 'CP-cell';
return value = 'Fertagus';
break;

case 'Soflusa':
meta.tdCls = 'Soft-cell';
return value = 'Soflusa';
break;

case 'STCP':
meta.tdCls = 'stcp-cell';
return value = 'STCP';
break;

case 'T.C.Barreiro':
meta.tdCls = 'undef-cell';
return value = 'T.C.Barreiro';
break;

case 'Carris':
meta.tdCls = 'Carris-cell';
return value = 'Carris';
break;

case 'Transportes Urbanos de Braga':
meta.tdCls = 'tub-icon';
return  value = 'T.U.Braga';
break;

case 'Rodoviária do Tejo':
meta.tdCls = 'rtejo-icon';
return  value = 'R.Tejo';
break;

case 'Transportes Sul do Tejo':
meta.tdCls = 'tst-icon';
return  value ='TST';
break;

case 'Aviação':
meta.tdCls = 'airplane-icon';
return  value ='Aviação';
break;

default:
meta.tdCls = 'undef-cell';
return value;
}
}

var checkDate = checkDate || {};

checkDate.fire = function(values){
var dts = [];
for(var i in values){
dts[i] = values.slice(0,10);
}
for (var x in dts){
if(checkDate.tomorrowDate() == dts[x]){
var wind = Ext.getCmp('Win').setTitle('<marquee style="color:yellow;height:20; width:200" scrollamout="200">\
Amanhã Há Greve ' + dts[x]+ '</marquee>');
}
else if(checkDate.today() == dts[x]){
var wind = Ext.getCmp('Win').setTitle('<marquee style="color:yellow;height:20; width:200" scrollamout="200">\
Hoje Há Greve ' + dts[x] + '</marquee>');
}
}
return values;
}

checkDate.tomorrowDate = function(){
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();

day = checkDate.leftZero(day);
month = checkDate.leftZero(month);

var tmdate = year + "-" + month +"-"+day;

return tmdate;
}

checkDate.today = function(){
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();

day      = checkDate.leftZero(day);
month = checkDate.leftZero(month);

var tmdate = year + "-" + month + "-" +day;

return tmdate;
}

checkDate.leftZero = function(zero){
zero < 10 ? zero = '0' + zero : zero;
return zero;
}

var canceled = function(value, metadata){

var valcolors = [];

if(value === true){
value = 'Sim';
valcolors.push('sim');

}else if(value === false){
value = 'Não';
valcolors.push('nao');
}
valcolors.forEach(function(val){
if(val == 'nao'){
metadata.tdAttr = 'style="background-color:red;color:black;"';
}else if(val == 'sim'){
metadata.tdAttr = 'style="background-color:green;color:black;"';
}
});
return value;
}

var duration = function(value, metadata){
if(value === true){
value = "Todo o dia";
}else{
value = "Parcial";
metadata.tdAttr = 'style="background-color:yellow;color:black;"';
}
return value;
}
// this is for the one that bring all data from hagreve?/short date with no hours
// if needed for future releases
var oldDate = function(value, metadata){
var year = value.slice(0,10);
if(year < 2014){
metadata.tdAttr = 'style="background-color:yellow;color:black;"';
}
return value;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s