Skip to content

Instantly share code, notes, and snippets.

@clseva
Last active December 10, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clseva/4501042 to your computer and use it in GitHub Desktop.
Save clseva/4501042 to your computer and use it in GitHub Desktop.
AGCalendar example
/**
* Calendar Module for Titanium
* Read the documentation for more information
*/
// Start off by creating an instance of the module.
Titanium.Calendar = Ti.Calendar = require('ag.calendar');
// Set EventKit as our datasource
Ti.Calendar.dataSource("coredata");
// Create a window to hold our calendar
var window = Ti.UI.createWindow({
title: "Calendar",
backgroundColor: "#fff",
modal:true
});
// Now, create the calendar view and enable swipe-to-delete
// by using editable: true
var calendarView = Ti.Calendar.createView({
top:0,
editable: true,
color:"white"
});
// Button to hide our calendar
var options = Ti.UI.createButton({title:"Options"});
// Eventlistener
options.addEventListener("click", function() {
var dialog = Ti.UI.createOptionDialog({
options: ['Set custom date', 'Delete all events', 'Cancel'],
title: 'Calendar options',
cancel: 2
});
dialog.addEventListener("click", function(e) {
if (e.index == 0) {
var d = new Date();
d.setDate(d.getDate()+3);
calendarView.selectDate(d);
} else if(e.index == 1) {
Ti.API.info(Ti.Calendar.ds);
if (Ti.Calendar.ds == "coredata") {
Ti.API.info("Deleting all events...");
Ti.Calendar.deleteAllEvents();
} else {
alert("This function is only available while using CoreData as your datasource.");
return;
}
}
});
dialog.show();
});
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
// Button to select todays date
var todayButton = Ti.UI.createButton({title:"Today"});
// Eventlistener
todayButton.addEventListener("click", function() {
calendarView.selectTodaysDate();
});
calendarView.addEventListener("date:clicked", function(e) {
var date_clicked = new Date(e.event.date);
Ti.API.info("Date clicked: "+monthNames[date_clicked.getMonth()]+" "+date_clicked.getDate()+".");
});
calendarView.addEventListener("date:longpress", function(e) {
var date_clicked = new Date(e.event.date);
// e.event.date time fix. Always returns 23:59.
date_clicked.setHours(new Date().getHours(), new Date().getMinutes(), new Date().getSeconds());
var dialog = Ti.UI.createAlertDialog({
message: "Would you like to add a new event on "+monthNames[date_clicked.getMonth()]+" "+date_clicked.getDate()+". ?",
buttonNames: ['Yeah!', 'Cancel'],
cancel: 1,
title: 'New event'
});
var endDate = new Date(e.event.date);
// e.event.date time fix. Always returns 23:59.
endDate.setHours(new Date().getHours()+3, new Date().getMinutes(), new Date().getSeconds());
Ti.API.info("Start date: " + date_clicked);
Ti.API.info("End date: " + endDate);
dialog.addEventListener('click', function(e){
if (e.index == 0) {
Ti.Calendar.addEvent({
title: "Added event",
startDate: date_clicked,
endDate: endDate,
location: "At home",
identifier: Ti.Calendar.identifier
});
setTimeout(function() {
calendarView.selectTodaysDate();
},1000);
}
});
dialog.show();
});
calendarView.addEventListener("month:next", function() {
Ti.API.info("Moving to next month");
});
calendarView.addEventListener("month:previous", function() {
Ti.API.info("Moving to previous month");
});
// Now, let's create an eventlistener to get the data we want
// from our calendar. This event will fire when a user touches
// an event in the tableview.
calendarView.addEventListener('event:clicked', function(e) {
var event = e.event;
// Log event details
Ti.API.info(JSON.stringify(event));
// Event dates is returned as strings.
// To convert the string to a Date() object, input
// the string in a Date([string]) as shown below.
var toDateObj = new Date(event.startDate);
// Now you can trigger all date functions
// http://www.w3schools.com/jsref/jsref_obj_date.asp
// This example utilizes the toUTCString to show date according to universal time
Ti.API.info("This event will start: "+toDateObj.toUTCString());
});
// Add everything to our window and open it.
window.setLeftNavButton(options);
window.setRightNavButton(todayButton);
window.add(calendarView);
window.open({animated: false});
// There seems to be an issue with the Kal library
// When 3 or more events are added it duplicates itself.
// This fix takes care of it while im figuring out whats wrong.
setTimeout(function() {
calendarView.selectTodaysDate();
},1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment