/******************************************************************************
* DatePicker
* ============================================================================
* Author: Julio Cesar Serrano Ortuno
* Creation date: November, 22 of 2014
* Last update: November, 23 of 2014
* ============================================================================
* Simple date picker for DroidScript
* ****************************************************************************/
Date.prototype.daysInMonth = function () {
return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate();
}
/*** DatePicker class ***/
function DatePicker() {
this.text = {}; //Recipient for result date
this.month = 0; //Date
this.year = 0;
this.day = 0;
this.init = DatePicker_init;
this.getDatePicker = DatePicker_get;
this.fill = DatePicker_fill;
this.createDialog = DatePicker_createDialog;
this.init();
}
/*** Creates the date picker dialog ***/
function DatePicker_init() {
this.dlg = this.createDialog();
}
/*** Shows the date picker ***/
// Receives a date and a Text object which is going to receive the selected date
function DatePicker_get(d, textobj) {
this.month = d.getMonth();
this.year = d.getFullYear();
this.day = d.getDate();
this.text = textobj;
this.fill();
this.dlg.Show();
}
/*** Fills the date picker dialog ***/
//Fills the dialog from the date provided on get method
function DatePicker_fill() {
// This can be adapted to your locale
var months = ["Enero","Febrero","Marzo","Abril","Mayo","Junio",
"Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
// Hides the days layout
this.callay.SetVisibility("Hide");
//Get layout objects
var lo = this.layObjects;
//Sets the month and the year
lo.tmonthyear.SetText(""+months[this.month]+" "+this.year);
//Get the first day of the month
var firstday = new Date();
firstday.setDate(1);
firstday.setMonth(this.month);
firstday.setFullYear(this.year);
//Get the number of cells we have to pass to start putting the day numbers
//Y start the week on monday, so the adjustment I make
var dow = firstday.getDay()-2;
if (dow == -2) dow = 5;
//Erase all days
lo.cells.forEach(function (item) {
item.SetText("");
item.SetBackColor("#00ffffff");
});
//Set the days of this month
for (var i = 1; i <= firstday.daysInMonth(); i++) {
lo.cells[i+dow].SetText(""+i);
if (i == this.day) {
//Current established day gets remarked
lo.cells[i+dow].SetBackColor("#ff00ACE0");
} else {
lo.cells[i+dow].SetBackColor("#ff555555");
}
}
//Shows the day numbers layout
this.callay.SetVisibility("Show");
}
/*** Creates the dialog ***/
function DatePicker_createDialog() {
var lo = {}; //Layout objects that are to be modified from other methods
//are stored in this.layObjects.
//"lo" will be stored there later on this very method.
var dlg = app.CreateDialog("Seleccionar Fecha");
dlg.Hide();
var lay = app.CreateLayout("Linear", "VCenter,FillXY");
lay.SetBackColor("#ffffffff");
dlg.AddLayout(lay);
var lmonth = app.CreateLayout("Linear", "Horizontal,FillX");
lmonth.SetMargins(0,0.02,0,0.02);
lay.AddChild(lmonth);
var prev = app.CreateImage("Img/prev.png",0.1,0.06);
prev.datepicker = this;
prev.SetOnTouchUp( prevHandler );
lmonth.AddChild(prev);
lo.tmonthyear = app.CreateText("Noviembre 2014",0.52,0.09,"Center");
lo.tmonthyear.SetTextSize(16);
lo.tmonthyear.SetPadding(0,0.014,0,0);
lmonth.AddChild(lo.tmonthyear);
var next = app.CreateImage("Img/next.png",0.1,0.06);
next.datepicker = this;
next.SetOnTouchUp( nextHandler );
lmonth.AddChild(next);
var lweek = app.CreateLayout("Linear","Horizontal, FillX");
lay.AddChild(lweek);
var lun = app.CreateText("L",0.1,0.06,"Center");
lun.SetMargins(0.004,0,0.004,0);
lweek.AddChild(lun);
var mar = app.CreateText("M",0.1,0.06,"Center");
mar.SetMargins(0.004,0,0.004,0);
lweek.AddChild(mar);
var mie = app.CreateText("X",0.1,0.06,"Center");
mie.SetMargins(0.004,0,0.004,0);
lweek.AddChild(mie);
var jue = app.CreateText("J",0.1,0.06,"Center");
jue.SetMargins(0.004,0,0.004,0);
lweek.AddChild(jue);
var vie = app.CreateText("V",0.1,0.06,"Center");
vie.SetMargins(0.004,0,0.004,0);
lweek.AddChild(vie);
var sab = app.CreateText("S",0.1,0.06,"Center");
sab.SetMargins(0.004,0,0.004,0);
lweek.AddChild(sab);
var dom = app.CreateText("D",0.1,0.06,"Center");
dom.SetMargins(0.004,0,0.004,0);
lweek.AddChild(dom);
this.callay = app.CreateLayout("Linear","VCenter,FillXY");
lay.AddChild(this.callay);
lo.cells = [];
for (var i = 0; i < 6; i++) {
var linelay = app.CreateLayout("Linear","Horizontal,FillX");
this.callay.AddChild(linelay);
for (var j = 0; j < 7; j++) {
var cell = app.CreateText("31",0.1,0.06);
cell.SetTextColor("#ffffffff");
cell.SetMargins(0.004,0.004,0.004,0.004);
cell.SetPadding(0,0.015,0,0);
cell.datepicker = this;
cell.SetOnTouchUp( dayHandler );
linelay.AddChild(cell);
lo.cells.push(cell);
}
}
this.layObjects = lo;
return dlg;
}
/*** Event handlers ***/
/*** Goes to the previous month ***/
function prevHandler() {
if (this.datepicker.month == 0) {
this.datepicker.month = 11;
this.datepicker.year--;
} else {
this.datepicker.month--;
}
this.datepicker.fill();
}
/*** Goes to the next month ***/
function nextHandler() {
if (this.datepicker.month == 11) {
this.datepicker.month = 0;
this.datepicker.year++;
} else {
this.datepicker.month++;
}
this.datepicker.fill();
}
/*** Handles when the user clicks on a day ***/
function dayHandler() {
var t = this.GetText().trim();
if (t.length == 0) return;
var n = parseInt(t);
var str = n+"/"+(this.datepicker.month+1)+"/"+this.datepicker.year;
this.datepicker.text.SetText(str);
this.datepicker.dlg.Hide();
}var datepicker = new DatePicker();
datepicker.getDatePicker(new Date(Date.now()),txtRecipient);
/****************************************************************************** * DatePicker * ============================================================================ * Author: Julio Cesar Serrano Ortuno * Creation date: November, 22 of 2014 * Last update: November, 26 of 2014 * ============================================================================ * Simple date picker for DroidScript * ****************************************************************************/ Date.prototype.daysInMonth = function () { return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate();}
var lastev=[]; lastev.action="Up"; lastev.x=0; lastev.y=0;
/*** DatePicker class ***/function DatePicker() { DPTHIS = this; this.text = {}; //Recipient for result date this.month = 0; //Date this.year = 0; this.day = 0; this.init = DatePicker_init; this.getDatePicker = DatePicker_get; this.fill = DatePicker_fill; this.createDialog = DatePicker_createDialog; this.getDateRect = DatePicker_getDateRect; this.getDateFromCoords = DatePicker_getDateFromCoords; //Paint the calendar and gets an array with date rects for helping //Click detection later. this.calimg.SetColor("#ffffffff"); var rects = []; for (var i = 1; i <= firstday.daysInMonth(); i++) { var rect = this.getDateRect(i,dow); this.calimg.SetPaintColor("#ff555555"); this.calimg.DrawRectangle(rect.x,rect.y,rect.tx,rect.ty); this.calimg.SetPaintColor("#ffffffff"); this.calimg.SetTextSize(16); this.calimg.DrawText(lpad(""+i,2),rect.x+0.03,rect.y+0.1); rects[i-1] = rect; } this.rects = rects; this.calimg = app.CreateImage(null,0.756,0.475); this.calimg.SetOnTouch( calimg_OnTouch ); this.callay.AddChild(this.calimg); this.layObjects = lo; return dlg;}
/*** Given a date returns a DateRect for representation purposes ***/function DatePicker_getDateRect(day, dow) { var r = new DateRect(0,0); var padding = r.padding; var width = r.width, height = r.height; var target = day+dow; var row = Math.floor(target / 7); var col = Math.floor(target % 7); var x = padding+((padding*2)*col)+(width*col); var y = padding+((padding*2)*row)+(height*row); return new DateRect(x, y);}
/*** Gets a date from given coordinates ***/function DatePicker_getDateFromCoords(x, y) { var rects = this.rects; var date = -1; rects.some( function (rect, index) { if (rect.inside(x, y)) { date = index+1; return true; } }); return date;}
// Used for date representation coordinates storagefunction DateRect(x,y) { this.padding = 0.0065 this.x = x; this.y = y; this.width = 0.130; this.height = 0.14; this.tx = x+this.width; this.ty = y+this.height; this.inside = function (x, y) { return (x >= this.x && x <= this.tx && y >= this.y && y <= this.ty); };}
/*** Event handlers ***/
/*** Goes to the previous month ***/function prevHandler() { if (this.datepicker.month == 0) { this.datepicker.month = 11; this.datepicker.year--; } else { this.datepicker.month--; } this.datepicker.fill();}
/*** Goes to the next month ***/function nextHandler() { if (this.datepicker.month == 11) { this.datepicker.month = 0; this.datepicker.year++; } else { this.datepicker.month++; } this.datepicker.fill();}
var moves = [];
/*** Handles Multitouch over the calendar ***/function calimg_OnTouch(ev) { //When the action was Move, we take account of all moves if (ev.action == "Move") { moves.push(ev.X); } // Handle user interaction // We wait until TouchUp if (ev.action == "Up") { if (lastev.action == "Down") { // In the rare case the user doesn't move // Get the signaled day and retrn var date = DPTHIS.getDateFromCoords(ev.X, ev.Y); if (date != -1) { var str = lpad(date,2)+"/"+lpad(DPTHIS.month+1,2)+"/"+DPTHIS.year; DPTHIS.text.SetText(str); DPTHIS.dlg.Hide(); } } else if (lastev.action == "Move") { // Last event was move, so we have to analize the move app.ShowDebug("Move: "+moves[0]+" / "+moves[1]+":"+moves.length); if (Math.abs(moves[0]-moves[moves.length-1]) <= 0.1) { // Discard insignificant moves // And take like Down var date = DPTHIS.getDateFromCoords(ev.X, ev.Y); if (date != -1) { var str = lpad(date,2)+"/"+lpad(DPTHIS.month+1,2)+"/"+DPTHIS.year; DPTHIS.text.SetText(str); DPTHIS.dlg.Hide(); } } else { // Handle the move if (moves[0] < moves[1]) { //To right if (DPTHIS.month == 0) { DPTHIS.month = 11; DPTHIS.year--; } else { DPTHIS.month--; } DPTHIS.fill(); } else if (moves[0] > moves[1]) { //To left if (DPTHIS.month == 11) { DPTHIS.month = 0; DPTHIS.year++; } else { DPTHIS.month++; } DPTHIS.fill(); } } // The move has been completed, so we init the moves array moves = []; } } // Get record of this event for later comparation if(ev.action != ""){ lastev.action = ev.action lastev.x = ev.x[0]; lastev.y = ev.y[0] } }//Fills with zeroes by left
function lpad(value, padding, c) {
c = c || "0";
var zeroes = new Array(padding+1).join(c);
return (zeroes + value).slice(-padding);
}And you whats the BEST PART?
Your small gesture of support will keep DroidScript alive. And that small team will continue to provide updates and bug fixes. And make what was a good tool that makes android app into THE BEST TOOL for app making.
Wait there's more....
AND YOU WILL BE PROUD THAT YOU HAVE CONTRIBUTED TO MAKE IT POSSIBLE.
Enter code here...
//app.LoadScript("DatePicker.js",showDP);;-)
It does have floating action buttons but they are nothing like chat heads.
They are the sort of button you get to create a new email in the Gmail app on recent versions of Android.
If you want to create transparent apps, I think you need a Premium subscription.
They are useful to float over other apps it is true. I keep one of mine available all the time to call from a notification I leave in the tray.