Login   Register  
Icontem

File: app.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Arturs Sosins  >  Titanium Countdown  >  app.js  >  Download  
File: app.js
Role: Example script
Content type: text/plain
Description: Example using with 3 different countdowns
Class: Titanium Countdown
Show a time countdown in Titanium label objects
Author: By
Last change:
Date: 2012-02-11 04:45
Size: 5,898 bytes
 

Contents

Class file image Download
/*************************************************************
 * This script is developed by Arturs Sosins aka ar2rsawseen, http://appcodingeasy.com/
 * Feel free to distribute and modify code, but keep reference to its creator
 *
 * Coutndown class displays updating countdown using provided left time or target timestamp.
 * It is possible to display countdown in different time units and formats by providing
 * label objects for specified time units
 *
 * For more information, examples and online documentation visit: 
 * http://appcodingeasy.com/Titanium-Mobile/Any-format-countdown
**************************************************************/
Titanium.UI.setBackgroundColor('#000');

//create window
var win = Titanium.UI.createWindow({  
    title:'Countdown Main',
    modal: true,
    exitOnClose: true
});

//create label function
var createLabel = function(y, text){
	text = (typeof text == "undefined") ? "" : text;
	return Titanium.UI.createLabel({
		color:'#fff',
		text:text,
		top: y,
		font:{fontSize:20,fontFamily:'Helvetica Neue'},
		textAlign:'center',
		width:'auto'
	});
};

//for button generation
var createButton = function(text, bottom, callback){
 	var button = Ti.UI.createButton({
    	height:50,
    	width:"50%",
    	color:'#000',
    	title: text,
    	font: {
    	    fontSize:14,
    	    fontFamily:'Helvetica Neue',
   	     	fontWeight:'bold'
  	  	},
  	  	left:"25%",
  	  	right:"25%",
  	  	bottom:bottom
	});
	button.addEventListener('click', callback);
	return button;
};

function win1()
{
	//create window
	var win1 = Titanium.UI.createWindow({  
	    title:'Countdown1',
	    backgroundColor: "#000",
	    modal: true
	});
	//year label
	var year = createLabel(20,"There are {y} years and");
	win1.add(year);

	//months label
	var month = createLabel(50,"{m} months");
	win1.add(month);

	//months label
	var week = createLabel(80,"{w} weeks");
	win1.add(week);

	//months label
	var day = createLabel(110,"{d} days");
	win1.add(day);

	//months label
	var hour = createLabel(140,"{h} hours");
	win1.add(hour);

	//months label
	var min = createLabel(170,"{i} minutes");
	win1.add(min);

	//months label
	var sec = createLabel(200,"{s} seconds");
	win1.add(sec);

	//create countdown
	var countdown = require('countdown');
	var cd = new countdown({
		//provide time
		//here for example 2 years
		year: 2,
		sec: 5,
		//provide lables for outputing
		//each specific time unit
		label_year: year,//years
		label_month: month,//months
		label_week: week,//weeks
		label_day: day,//days
		label_hour: hour,//hours
		label_min: min,//minutes
		label_sec: sec,//seconds
		//on end event callback
		onend: function(){alert("That's it");}
	});
	

	//create button to start countdown
	win1.add(createButton("Start", 150, function(){
		cd.start();
	}));
	
	//create button to stop countdown
	win1.add(createButton("Stop", 100, function(){
		cd.stop();
	}));
	
	//create back button
	win1.add(createButton("Back", 50, function(){
		win1.close();
	}));
	
	win1.open();
}

function win2()
{
	//create window
	var win2 = Titanium.UI.createWindow({  
	    title:'Countdown2',
	    backgroundColor: "#000",
	    modal: true
	});
	
	//months label
	var month = createLabel(20,"{m} months");
	win2.add(month);

	//months label
	var day = createLabel(50,"{d} days");
	win2.add(day);

	//months label
	var hour = createLabel(80,"{h} hours");
	win2.add(hour);

	//months label
	var min = createLabel(110,"{i} minutes");
	win2.add(min);

	//months label
	var sec = createLabel(140,"{s} seconds");
	win2.add(sec);

	//create countdown
	var countdown = require('countdown');
	var cd = new countdown({
		//provide time
		//here for example 2 years
		year: 2,
		sec: 5,
		//provide lables for outputing
		//each specific time unit
		label_month: month,//months
		label_day: day,//days
		label_hour: hour,//hours
		label_min: min,//minutes
		label_sec: sec,//seconds
		//on end event callback
		onend: function(){alert("That's it");}
	});
	

	//create button to start countdown
	win2.add(createButton("Start", 150, function(){
		cd.start();
	}));
	
	//create button to stop countdown
	win2.add(createButton("Stop", 100, function(){
		cd.stop();
	}));
	
	//create back button
	win2.add(createButton("Back", 50, function(){
		win2.close();
	}));
	
	win2.open();
}

function win3()
{
	//create window
	var win3 = Titanium.UI.createWindow({  
	    title:'Countdown2',
	    backgroundColor: "#000",
	    modal: true
	});
	
	//months label
	var month = createLabel(20,"{m} months");
	win3.add(month);

	//months label
	var day = createLabel(50,"{d} days");
	win3.add(day);

	//months label
	var hour = createLabel(80,"{h} hours");
	win3.add(hour);

	//months label
	var min = createLabel(110,"{i} minutes");
	win3.add(min);

	//months label
	var sec = createLabel(140,"{s} seconds");
	win3.add(sec);

	//create countdown
	var countdown = require('countdown');
	var cd = new countdown({
		//provide timestampe in future
		time: Math.round(new Date(2012,11,21,0,0,0).getTime() / 1000),
		//provide lables for outputing
		//each specific time unit
		label_month: month,//months
		label_day: day,//days
		label_hour: hour,//hours
		label_min: min,//minutes
		label_sec: sec,//seconds
		//on end event callback
		onend: function(){alert("That's it");}
	});
	

	//create button to start countdown
	win3.add(createButton("Start", 150, function(){
		cd.start();
	}));
	
	//create button to stop countdown
	win3.add(createButton("Stop", 100, function(){
		cd.stop();
	}));
	
	//create back button
	win3.add(createButton("Back", 50, function(){
		win3.close();
	}));
	
	win3.open();
}

//open first countdown
win.add(createButton("Countdown 1", 300, function(){
	win1();
}));

//open second countdown
win.add(createButton("Countdown 2", 250, function(){
	win2();
}));

//open third countdown
win.add(createButton("Countdown 3", 200, function(){
	win3();
}));

//open window
win.open();