var win=Ti.UI.createWindow();
var db=require('mydata/DataBase');
var _moment=require('ui/moment');
win.title='Bloc de Notas';
var portada=Ti.UI.createTableView({
backgroundColor:'grey',
width:'100%',height:'100%',
top:0
});
var boton=Ti.UI.createButton({
title:'Nueva Nota',
left:4, bottom:10,
width:'98%', height:38,
borderRadius:6,
borderWidth:2,
borderColor:'#13E339',
backgroundColor:'#2403F8',
font:{fontSize:16, fontWeight:'bold'}
});
win.add(portada);
win.add(boton);
//creamos la nota
boton.addEventListener('click', function(e){
var self=Ti.UI.createWindow({
title:'Nueva Nota',
width:'100%', height:'100%',
backgroundColor:'white'
});
var view=Ti.UI.createView({
width:'100%', height:'100%',
backgroundColor:'#DFECE1',
borderRadius:6,
borderWidth:6,
borderColor:'#5BC606',
});
var titulo=Ti.UI.createTextField({
hintText:'Introduce un Titulo...',
hintTextColor:'red',
top:16, left:12, color:'blue',
font:{fontSize:24}
});
var txtarea=Ti.UI.createTextArea({
width:'92%', top:65,
bottom:50, color:'#000',
borderColor:'blue'
});
var guarda=Ti.UI.createButton({
title:'guardar',
width:160, height:32, bottom:16,
borderRadius:6,
borderWidth:6,
backgroundColor:'#1309EA'
});
guarda.addEventListener('click', function(e){
var current_date = new Date();
var modified_date= _moment(current_date).format("MMM Do YYYY, h:mm:ss a");
var mycolor= getRandomColor();
if (txtarea.value.length < 1){
guardanotas(txtarea, self);
}else if(titulo.value.length< 1){
guardanotas(titulo, self);
}else {
var db=Ti.Database.open('cuentas');
var res=db.execute('INSERT INTO notes(title, nota, fecha, color)VALUES (?,?,?,?)', titulo.value, txtarea.value, modified_date, mycolor);
db.close();
carganotas();
self.close();
}
});
function guardanotas(txtarea, self){
var dialog=Ti.UI.createAlertDialog({
message:'Introducca titulo y texto, por favor...!!!',
ok:'OK',
title:'Campo vacio.'
});
dialog.show();
}
view.add(titulo);
view.add(txtarea);
view.add(guarda);
self.add(view);
self.open({'modal':true});
});
function carganotas(){
var db=Ti.Database.open('cuentas');
var query='SELECT * FROM notes ORDER BY fecha DESC';
var res=db.execute(query);
var filas= [];
while(res.isValidRow()){
var id_nota=res.fieldByName('id_nota');
var fecha_txt=res.fieldByName('fecha');
var nota_txt= res.fieldByName('nota');
var myTitle= res.fieldByName('title');
var myColor= res.fieldByName('color');
var publica = Ti.UI.createLabel({
objName:'publica',
text : '>>>>>>>>>>>>>desliza la nota para compartir...>>>>>>>>>>>>>>>',
top : 0,
width : '100%',
height : 28,
color : '#000',
font : {fontSize : 12, fontWeight : 'bold' },
touchEnabled : false
});
var elimina = Ti.UI.createLabel({
objName:'elimina',
text : '<<<<<<<<<<<<desliza la nota para borrar<<<<<<<<<',
textColor:'blue',
top : 5,
width : '90%',
height : 28,
color : '#000',
font : {fontSize : 12, fontWeight : 'bold',fontColor:'blue' },
touchEnabled : false
});
var id_label=Ti.UI.createLabel({
objName:'id_label',
text:id_nota,
top:5, color:'#000',
width:'90%', height:18,
font:{fontSize:16, fontWeight:'bold'},
touchEnabled:false
});
var fecha_label=Ti.UI.createLabel({
objName:'fecha_label',
text:fecha_txt,
color:'#000', top:5,
width:'90%', height:Ti.UI.SIZE,
font:{fontSize:18, fontWeight:'bold'}
});
var labelTitle=Ti.UI.createLabel({
objName:'labelTitle',
text:myTitle,
color:'#000', top:5,
width:'90%',
font:{fontSize:18, fontWeight:'bold'},
touchEnable:false
});
var nota_label=Ti.UI.createLabel({
objName:'nota_label',
text:nota_txt,
color:'#333', top:5,
width:'90%', height:28,
font:{fontSize:18, fontWeight:'bold'},
touchEnable:false
});
var newview=Ti.UI.createView({
objName:'newview',
layout:"vertical",
width:'100%', height:200,
touchEnabled:false
});
// newview.add(comparte);
newview.add(elimina);
newview.add(publica);
newview.add(id_label);
newview.add(fecha_label);
newview.add(labelTitle);
newview.add(nota_label);
var tablas=Ti.UI.createTableViewRow({
selectedColor:"black",
backgroundColor: myColor,
id_nota:id_nota,
height:200,
touchEnabled:false
});
tablas.add(newview);
filas.push(tablas);
res.next();
}
res.close();
db.close();
portada.setData(filas);
}
carganotas();
function onClickItem(e){
// CREO UN IF POR SI TENGO BOTON DE COMPARTIR
/*if(e.source.objName=='comparte'){
//COMPARTE
/ alert('COMPARTE CIRUELO');
/}else{*/
//EDITAR NOTA
console.log(e.rowData.id_nota);
var db=Ti.Database.open('cuentas');
var query='SELECT * FROM notes WHERE id_nota='+e.rowData.id_nota;
var res=db.execute(query);
while(res.isValidRow()){
var nota_id= res.fieldByName('id_nota');
var fecha_txt= res.fieldByName('fecha');
var nota_txt= res.fieldByName('nota');
var win_row=Ti.UI.createWindow({
title:'Nueva Nota',
width:'100%', height:'100%',
backgroundColor:'white'
});
var view_row = Ti.UI.createView({
width:'100%', height:'110%',
backgroundColor:'white',
borderWidth:6,
borderColor:'#BFC9CA',
borderRadius:6,
});
var txtarea= Ti.UI.createTextArea({
color:"#000", top:65,
width:'92%', bottom:60,
});
txtarea.addEventListener('change', function(e){
});
txtarea.value = nota_txt;
var actualiza = Ti.UI.createButton({
title:'guardar',
width:160, height:32,
bottom:20,
borderWidth:6,
borderRadius:6,
backgroundColor:'#1309EA'
});
actualiza.addEventListener('click', function(e){
if(txtarea.value.length < 1){
guardanotas(txtarea, win_row);
}else{
var db=Ti.Database.open('cuentas');
var query='UPDATE notes SET nota="' + txtarea.value+'" WHERE id_nota=' + nota_id;
var res= db.execute(query);
db.close();
carganotas();
win_row.close();
}
});
win_row.add(view_row);
view_row.add(txtarea);
view_row.add(actualiza);
win_row.open({modal:true});
console.log(fecha_txt + " " + nota_txt);
res.next();
}
// }
};
var db=Ti.Database.open('cuentas');
var filas = [];
portada.addEventListener("click", onClickItem);
//Borrar las notas deslizando
portada.addEventListener('swipe', function(note){
console.log(JSON.stringify(note));
if (note.direction==='left'){
var dialog=Ti.UI.createAlertDialog({
cancel:0,
buttonNames: ['Cancel','Aceptar'],
message:'Si Aceptas, se borrara la ultima nota...!!',
title:'Deseas borrar...???'
});
dialog.addEventListener('click', function(e) {
if (e.index == 1) {
var db=Ti.Database.open('cuentas');
//var query='DELETE FROM notes';
var query='DELETE FROM notes WHERE id_nota='+note.row.id_nota;
portada.deleteRow(note.index);
db.execute(query);
db.close();
}
});
dialog.show();
}
});
//funcion de compartir
portada.addEventListener('swipe', function(e){
if (e.direction==='right'){
var win = Ti.UI.createWindow({
backgroundColor : 'white'
});
win.open();
var fb = require('facebook');
fb.initialize();
// Create a Button.
var share = Ti.UI.createButton({
backgroundImage: "descarga.jpg",
width:180, height:52,
//height : Ti.UI.SIZE,
//width : Ti.UI.SIZE,
});
//Agregar a la vista principal.
win.add(share);
var webLink ='https://paseandoporlanubedelared.blogspot.com.es/';
//Escuchar eventos de clic
var db=Ti.Database.open('cuentas');
share.addEventListener('click', function(){
fb.presentShareDialog({
link : webLink,
title :'title text',
description : 'nota text',
picture : 'http://i64.tinypic.com/miic6a.jpg',
mode : fb.SHARE_DIALOG_MODE_AUTOMATIC
});
});
fb.addEventListener('shareCompleted', function(e){
if (e.success){
Ti.API.info('Se ha completado la solicitud de recurso compartido.');
} else {
Ti.API.info('No se pudo compartir.');
}
});
}
});
function getRandomColor(){
var letters = '0123456789ABCDEF';
var color='#';
for(var i = 0; i < 6; i++){
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
portada.setData(filas);
carganotas();
db.close();
win.open();