Created
February 23, 2011 10:17
-
-
Save sr3d/840244 to your computer and use it in GitHub Desktop.
my secret toolbelt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function log(object) { | |
Ti.API.debug(object); | |
if(logger) { | |
logger.log(object); | |
}; | |
}; | |
var indWin = null; | |
var actInd = null; | |
function showIndicator() { | |
indWin = Titanium.UI.createWindow({ height:150, width:150 }); | |
var indView = Titanium.UI.createView({ height:150, width:150, backgroundColor:'#000', borderRadius:10, opacity:0.9 }); | |
actInd = Titanium.UI.createActivityIndicator({ style:Titanium.UI.iPhone.ActivityIndicatorStyle.BIG, height:30, width:30 }); | |
indWin.add(indView); | |
indWin.add(actInd); | |
indWin.open(); | |
actInd.show(); | |
}; | |
function hideIndicator() { | |
actInd.hide(); | |
indWin.close({opacity:0,duration:100}); | |
}; | |
// This utility function creates the query string | |
// to be appended to the base URI of the YQL Web | |
// service. | |
function toQueryString(obj) { | |
var parts = []; | |
for(var each in obj) { | |
if (obj.hasOwnProperty(each)) { | |
parts.push(encodeURIComponent(each) + '=' + obj[each]); | |
}; | |
}; | |
return parts.join('&'); | |
}; | |
/* Wrapper on top of the HTTP client */ | |
function Request(url, options) { | |
if( !url ) { throw "Invalid URL"; }; | |
options = _.extend({ | |
method: 'GET', | |
async: true, | |
evalJSON: true, | |
parameters: {}, | |
headers: [], // [ [key, value], [key, value] ] | |
onSuccess: function(){}, | |
onError: function(response){ log('Error Requesting'); log(response.responseText); } | |
}, options || {} ); | |
options.method = options.method.toUpperCase(); | |
if(!options.async) { Ti.App.fireEvent('app:show_indicator'); }; | |
/* construct the GET URL */ | |
if( options.method == 'GET' ) { | |
url = url + (/\?/.test(url) ? '' : '?') + toQueryString(options.parameters); | |
}; | |
log( options.method + ' ' + url + ' [' + toQueryString(options.parameters) + ']' ); | |
var request = Ti.Network.createHTTPClient(); | |
request.open(options.method, url, options.async); | |
request.onload = function() { | |
if(!options.async) { Ti.App.fireEvent('app:hide_indicator'); }; // remove the indicator a bit | |
try { | |
// log(this.responseText); | |
if( options.evalJSON ) { | |
this.responseJSON = JSON.parse(this.responseText); | |
}; | |
options.onSuccess(this); // normalize to make it a bit more like Prototype | |
} catch(ex) { | |
log(ex); | |
}; | |
// this.release(); | |
}; | |
request.onerror = function() { | |
if(!options.async) { Ti.App.fireEvent('app:hide_indicator'); }; | |
options.onError(); | |
}; | |
for(var i = 0; i < options.headers.length; i++ ) { | |
request.setRequestHeader(options.headers[i][0], options.headers[i][1]); | |
}; | |
if( options.method == 'GET' ) { | |
request.send(); | |
} else { | |
request.send(options.parameters); | |
}; | |
}; | |
var DB = { | |
get: function(key, defaultValue, prefix) { | |
var value = JSON.parse(Ti.App.Properties.getString( (prefix || 'user_') + key ) ); | |
return (value != null) ? value : defaultValue; | |
} | |
,set: function(key, value, prefix ) { Ti.App.Properties.setString( (prefix || 'user_') + key, JSON.stringify(value)); } | |
,remove: function(key, prefix) { Ti.App.Properties.removeProperty( (prefix || 'user_') + key ); } | |
/* drop all keys beginning with prefix */ | |
,drop: function(prefix) { | |
if(!prefix) { prefix = 'user_'; }; | |
var properties = Ti.App.Properties.listProperties(); | |
for (var i=0; i < properties.length; i++) { | |
if(properties[i].indexOf(prefix) == 0) { | |
Ti.App.Properties.removeProperty(properties[i]); | |
}; | |
}; | |
} | |
,debug: function() { | |
var properties = Ti.App.Properties.listProperties(); | |
for (var i=0; i < properties.length; i++) { | |
log(properties[i] + ':' + Ti.App.Properties.getString( properties[i] ) ); | |
} | |
} | |
}; | |
String.prototype.strip = function() { | |
return this.replace(/^\s+|\s+$/g,""); | |
}; | |
String.prototype.unescapeQuote = function() { | |
return this.replace(/"/g, '"'); | |
}; | |
/* 2010-09-28T19:40:28-05:00 */ | |
Date.prototype.toRailsISOString = function() { | |
function f(n){return n<10? '0' + n : n;}; | |
var offset = -this.getTimezoneOffset(); | |
offset = ( offset > 0 ? '+' : '-' ) + f(Math.abs(Math.floor(offset/60))) + ':' + f( offset % 60 ); | |
return this.getFullYear()+'-'+ | |
f(this.getMonth()+1) +'-'+ | |
f(this.getDate())+ 'T'+ | |
f(this.getHours())+ ':'+ | |
f(this.getMinutes())+ ':'+ | |
f(this.getSeconds()) + offset; | |
}; | |
// http://braxtoninaustin.com/2011/02/converting-a-number-to-currency-with-javascript/ | |
function numberToCurrency(num) { | |
num = num.toString(); //.replace(/\$|\,/g, ''); | |
if (isNaN(num)) num = "0"; | |
sign = (num == (num = Math.abs(num))); | |
num = Math.floor(num * 100 + 0.50000000001); | |
cents = num % 100; | |
num = Math.floor(num / 100).toString(); | |
if (cents < 10) {cents = '0' + cents;}; | |
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) { | |
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3)); | |
}; | |
return (((sign) ? '' : '-') + '$' + num + '.' + cents); | |
// return "$ " + num.toLocaleString().split(".")[0] + "." + num.toFixed(2).split(".")[1]; | |
// var s = Math.ceil(number*100); | |
// return "$" + number.toFixed(2); | |
}; | |
var NO_SELECTION = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE; | |
var DEFAULT_SELECTION = Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE; | |
var COUNTRIES = [["Afghanistan","AF"],["Aland Islands","AX"],["Albania","AL"], | |
["Algeria","DZ"],["American Samoa","AS"],["Andorra","AD"],["Angola","AO"],["Anguilla","AI"],["Antarctica","AQ"],["Antigua and Barbuda","AG"],["Argentina","AR"],["Armenia","AM"],["Aruba","AW"],["Australia","AU"],["Austria","AT"],["Azerbaijan","AZ"],["Bahamas","BS"],["Bahrain","BH"],["Bangladesh","BD"],["Barbados","BB"],["Belarus","BY"],["Belgium","BE"],["Belize","BZ"],["Benin","BJ"],["Bermuda","BM"],["Bhutan","BT"],["Bolivia","BO"],["Bosnia and Herzegovina","BA"],["Botswana","BW"],["Bouvet Island","BV"],["Brazil","BR"],["British Indian Ocean Territory","IO"],["Brunei Darussalam","BN"],["Bulgaria","BG"],["Burkina Faso","BF"],["Burundi","BI"],["Cambodia","KH"],["Cameroon","CM"],["Canada","CA"],["Cape Verde","CV"],["Cayman Islands","KY"],["Central African Republic","CF"],["Chad","TD"],["Chile","CL"], | |
["China","CN"],["Christmas Island","CX"],["Cocos (Keeling) Islands","CC"],["Colombia","CO"],["Comoros","KM"],["Congo","CG"],["Congo, the Democratic Republic of the","CD"],["Cook Islands","CK"],["Costa Rica","CR"],["Cote D'ivoire","CI"],["Croatia","HR"],["Cuba","CU"],["Cyprus","CY"],["Czech Republic","CZ"],["Denmark","DK"],["Djibouti","DJ"],["Dominica","DM"],["Dominican Republic","DO"],["Ecuador","EC"],["Egypt","EG"],["El Salvador","SV"],["Equatorial Guinea","GQ"],["Eritrea","ER"],["Estonia","EE"],["Ethiopia","ET"],["Falkland Islands (Malvinas)","FK"],["Faroe Islands","FO"],["Fiji","FJ"],["Finland","FI"],["France","FR"], | |
["French Guiana","GF"],["French Polynesia","PF"],["French Southern Territories","TF"],["Gabon","GA"],["Gambia","GM"],["Georgia","GE"],["Germany","DE"],["Ghana","GH"],["Gibraltar","GI"],["Greece","GR"],["Greenland","GL"],["Grenada","GD"],["Guadeloupe","GP"],["Guam","GU"],["Guatemala","GT"],["Guernsey","GG"],["Guinea","GN"],["Guinea-Bissau","GW"],["Guyana","GY"],["Haiti","HT"],["Heard Island and Mcdonald Islands","HM"],["Holy See (Vatican City State)","VA"],["Honduras","HN"],["Hong Kong","HK"],["Hungary","HU"],["Iceland","IS"],["India","IN"],["Indonesia","ID"],["Iran, Islamic Republic of","IR"],["Iraq","IQ"],["Ireland","IE"],["Isle of Man","IM"],["Israel","IL"],["Italy","IT"],["Jamaica","JM"],["Japan","JP"],["Jersey","JE"],["Jordan","JO"],["Kazakhstan","KZ"],["Kenya","KE"],["Kiribati","KI"], | |
["Korea, Democratic People's Republic of","KP"],["Korea, Republic of","KR"],["Kuwait","KW"],["Kyrgyzstan","KG"],["Lao People's Democratic Republic","LA"],["Latvia","LV"],["Lebanon","LB"],["Lesotho","LS"],["Liberia","LR"],["Libyan Arab Jamahiriya","LY"],["Liechtenstein","LI"],["Lithuania","LT"],["Luxembourg","LU"],["Macao","MO"],["Macedonia, the Former Yugoslav Republic of","MK"],["Madagascar","MG"],["Malawi","MW"],["Malaysia","MY"],["Maldives","MV"],["Mali","ML"],["Malta","MT"],["Marshall Islands","MH"],["Martinique","MQ"],["Mauritania","MR"],["Mauritius","MU"],["Mayotte","YT"],["Mexico","MX"],["Micronesia, Federated States of","FM"],["Moldova, Republic of","MD"],["Monaco","MC"],["Mongolia","MN"],["Montserrat","MS"],["Morocco","MA"],["Mozambique","MZ"],["Myanmar","MM"],["Namibia","NA"], | |
["Nauru","NR"],["Nepal","NP"],["Netherlands","NL"],["Netherlands Antilles","AN"],["New Caledonia","NC"],["New Zealand","NZ"],["Nicaragua","NI"],["Niger","NE"],["Nigeria","NG"],["Niue","NU"],["Norfolk Island","NF"],["Northern Mariana Islands","MP"],["Norway","NO"],["Oman","OM"],["Pakistan","PK"],["Palau","PW"],["Palestinian Territory, Occupied","PS"],["Panama","PA"],["Papua New Guinea","PG"],["Paraguay","PY"],["Peru","PE"],["Philippines","PH"],["Pitcairn","PN"],["Poland","PL"],["Portugal","PT"],["Puerto Rico","PR"],["Qatar","QA"],["Reunion","RE"],["Romania","RO"],["Russian Federation","RU"],["Rwanda","RW"],["Saint Helena","SH"],["Saint Kitts and Nevis","KN"],["Saint Lucia","LC"],["Saint Pierre and Miquelon","PM"],["Saint Vincent and the Grenadines","VC"],["Samoa","WS"],["San Marino","SM"], | |
["Sao Tome and Principe","ST"],["Saudi Arabia","SA"],["Senegal","SN"],["Serbia and Montenegro","CS"],["Seychelles","SC"],["Sierra Leone","SL"],["Singapore","SG"],["Slovakia","SK"],["Slovenia","SI"],["Solomon Islands","SB"],["Somalia","SO"],["South Africa","ZA"],["South Georgia and the South Sandwich Islands","GS"],["Spain","ES"],["Sri Lanka","LK"],["Sudan","SD"],["Suriname","SR"],["Svalbard and Jan Mayen","SJ"],["Swaziland","SZ"],["Sweden","SE"],["Switzerland","CH"],["Syrian Arab Republic","SY"],["Taiwan, Province of China","TW"],["Tajikistan","TJ"],["Tanzania, United Republic of","TZ"],["Thailand","TH"],["Timor-Leste","TL"],["Togo","TG"],["Tokelau","TK"],["Tonga","TO"],["Trinidad and Tobago","TT"], | |
["Tunisia","TN"],["Turkey","TR"],["Turkmenistan","TM"],["Turks and Caicos Islands","TC"],["Tuvalu","TV"],["Uganda","UG"],["Ukraine","UA"],["United Arab Emirates","AE"],["United Kingdom","GB"],["United States","US"],["United States Minor Outlying Islands","UM"],["Uruguay","UY"],["Uzbekistan","UZ"],["Vanuatu","VU"],["Venezuela","VE"],["Viet Nam","VN"],["Virgin Islands, British","VG"],["Virgin Islands, U.S.","VI"],["Wallis and Futuna","WF"],["Western Sahara","EH"],["Yemen","YE"],["Zambia","ZM"],["Zimbabwe","ZW"]]; | |
/* Timezone Handling */ | |
var TIMEZONES = [["International Date Line West","GMT-11:00","(GMT-11:00) International Date Line West"],["Midway Island","GMT-11:00","(GMT-11:00) Midway Island"],["Samoa","GMT-11:00","(GMT-11:00) Samoa"],["Hawaii","GMT-10:00","(GMT-10:00) Hawaii"],["Alaska","GMT-09:00","(GMT-09:00) Alaska"],["Pacific Time (US & Canada)","GMT-08:00) Pacific Time (US & Canada","(GMT-08:00) Pacific Time (US & Canada)"],["Tijuana","GMT-08:00","(GMT-08:00) Tijuana"],["Arizona","GMT-07:00","(GMT-07:00) Arizona"],["Chihuahua","GMT-07:00","(GMT-07:00) Chihuahua"],["Mazatlan","GMT-07:00","(GMT-07:00) Mazatlan"],["Mountain Time (US & Canada)","GMT-07:00) Mountain Time (US & Canada","(GMT-07:00) Mountain Time (US & Canada)"],["Central America","GMT-06:00","(GMT-06:00) Central America"], | |
["Central Time (US & Canada)","GMT-06:00) Central Time (US & Canada","(GMT-06:00) Central Time (US & Canada)"],["Guadalajara","GMT-06:00","(GMT-06:00) Guadalajara"],["Mexico City","GMT-06:00","(GMT-06:00) Mexico City"],["Monterrey","GMT-06:00","(GMT-06:00) Monterrey"],["Saskatchewan","GMT-06:00","(GMT-06:00) Saskatchewan"],["Bogota","GMT-05:00","(GMT-05:00) Bogota"],["Eastern Time (US & Canada)","GMT-05:00) Eastern Time (US & Canada","(GMT-05:00) Eastern Time (US & Canada)"],["Indiana (East)","GMT-05:00) Indiana (East","(GMT-05:00) Indiana (East)"],["Lima","GMT-05:00","(GMT-05:00) Lima"],["Quito","GMT-05:00","(GMT-05:00) Quito"],["Caracas","GMT-04:30","(GMT-04:30) Caracas"],["Atlantic Time (Canada)","GMT-04:00) Atlantic Time (Canada","(GMT-04:00) Atlantic Time (Canada)"],["Georgetown","GMT-04:00","(GMT-04:00) Georgetown"], | |
["La Paz","GMT-04:00","(GMT-04:00) La Paz"],["Santiago","GMT-04:00","(GMT-04:00) Santiago"],["Newfoundland","GMT-03:30","(GMT-03:30) Newfoundland"],["Brasilia","GMT-03:00","(GMT-03:00) Brasilia"],["Buenos Aires","GMT-03:00","(GMT-03:00) Buenos Aires"],["Greenland","GMT-03:00","(GMT-03:00) Greenland"],["Mid-Atlantic","GMT-02:00","(GMT-02:00) Mid-Atlantic"],["Azores","GMT-01:00","(GMT-01:00) Azores"],["Cape Verde Is.","GMT-01:00","(GMT-01:00) Cape Verde Is."],["Casablanca","GMT+00:00","(GMT+00:00) Casablanca"],["Dublin","GMT+00:00","(GMT+00:00) Dublin"],["Edinburgh","GMT+00:00","(GMT+00:00) Edinburgh"],["Lisbon","GMT+00:00","(GMT+00:00) Lisbon"],["London","GMT+00:00","(GMT+00:00) London"],["Monrovia","GMT+00:00","(GMT+00:00) Monrovia"],["UTC","GMT+00:00","(GMT+00:00) UTC"],["Amsterdam","GMT+01:00","(GMT+01:00) Amsterdam"], | |
["Belgrade","GMT+01:00","(GMT+01:00) Belgrade"],["Berlin","GMT+01:00","(GMT+01:00) Berlin"],["Bern","GMT+01:00","(GMT+01:00) Bern"],["Bratislava","GMT+01:00","(GMT+01:00) Bratislava"],["Brussels","GMT+01:00","(GMT+01:00) Brussels"],["Budapest","GMT+01:00","(GMT+01:00) Budapest"],["Copenhagen","GMT+01:00","(GMT+01:00) Copenhagen"],["Ljubljana","GMT+01:00","(GMT+01:00) Ljubljana"],["Madrid","GMT+01:00","(GMT+01:00) Madrid"],["Paris","GMT+01:00","(GMT+01:00) Paris"],["Prague","GMT+01:00","(GMT+01:00) Prague"],["Rome","GMT+01:00","(GMT+01:00) Rome"],["Sarajevo","GMT+01:00","(GMT+01:00) Sarajevo"],["Skopje","GMT+01:00","(GMT+01:00) Skopje"],["Stockholm","GMT+01:00","(GMT+01:00) Stockholm"],["Vienna","GMT+01:00","(GMT+01:00) Vienna"],["Warsaw","GMT+01:00","(GMT+01:00) Warsaw"],["West Central Africa","GMT+01:00","(GMT+01:00) West Central Africa"], | |
["Zagreb","GMT+01:00","(GMT+01:00) Zagreb"],["Athens","GMT+02:00","(GMT+02:00) Athens"],["Bucharest","GMT+02:00","(GMT+02:00) Bucharest"],["Cairo","GMT+02:00","(GMT+02:00) Cairo"],["Harare","GMT+02:00","(GMT+02:00) Harare"],["Helsinki","GMT+02:00","(GMT+02:00) Helsinki"],["Istanbul","GMT+02:00","(GMT+02:00) Istanbul"],["Jerusalem","GMT+02:00","(GMT+02:00) Jerusalem"],["Kyiv","GMT+02:00","(GMT+02:00) Kyiv"],["Minsk","GMT+02:00","(GMT+02:00) Minsk"],["Pretoria","GMT+02:00","(GMT+02:00) Pretoria"],["Riga","GMT+02:00","(GMT+02:00) Riga"],["Sofia","GMT+02:00","(GMT+02:00) Sofia"],["Tallinn","GMT+02:00","(GMT+02:00) Tallinn"],["Vilnius","GMT+02:00","(GMT+02:00) Vilnius"],["Baghdad","GMT+03:00","(GMT+03:00) Baghdad"],["Kuwait","GMT+03:00","(GMT+03:00) Kuwait"],["Moscow","GMT+03:00","(GMT+03:00) Moscow"],["Nairobi","GMT+03:00","(GMT+03:00) Nairobi"], | |
["Riyadh","GMT+03:00","(GMT+03:00) Riyadh"],["St. Petersburg","GMT+03:00","(GMT+03:00) St. Petersburg"],["Volgograd","GMT+03:00","(GMT+03:00) Volgograd"],["Tehran","GMT+03:30","(GMT+03:30) Tehran"],["Abu Dhabi","GMT+04:00","(GMT+04:00) Abu Dhabi"],["Baku","GMT+04:00","(GMT+04:00) Baku"],["Muscat","GMT+04:00","(GMT+04:00) Muscat"],["Tbilisi","GMT+04:00","(GMT+04:00) Tbilisi"],["Yerevan","GMT+04:00","(GMT+04:00) Yerevan"],["Kabul","GMT+04:30","(GMT+04:30) Kabul"],["Ekaterinburg","GMT+05:00","(GMT+05:00) Ekaterinburg"],["Islamabad","GMT+05:00","(GMT+05:00) Islamabad"],["Karachi","GMT+05:00","(GMT+05:00) Karachi"],["Tashkent","GMT+05:00","(GMT+05:00) Tashkent"],["Chennai","GMT+05:30","(GMT+05:30) Chennai"],["Kolkata","GMT+05:30","(GMT+05:30) Kolkata"],["Mumbai","GMT+05:30","(GMT+05:30) Mumbai"],["New Delhi","GMT+05:30","(GMT+05:30) New Delhi"], | |
["Sri Jayawardenepura","GMT+05:30","(GMT+05:30) Sri Jayawardenepura"],["Kathmandu","GMT+05:45","(GMT+05:45) Kathmandu"],["Almaty","GMT+06:00","(GMT+06:00) Almaty"],["Astana","GMT+06:00","(GMT+06:00) Astana"],["Dhaka","GMT+06:00","(GMT+06:00) Dhaka"],["Novosibirsk","GMT+06:00","(GMT+06:00) Novosibirsk"],["Rangoon","GMT+06:30","(GMT+06:30) Rangoon"],["Bangkok","GMT+07:00","(GMT+07:00) Bangkok"],["Hanoi","GMT+07:00","(GMT+07:00) Hanoi"],["Jakarta","GMT+07:00","(GMT+07:00) Jakarta"],["Krasnoyarsk","GMT+07:00","(GMT+07:00) Krasnoyarsk"],["Beijing","GMT+08:00","(GMT+08:00) Beijing"],["Chongqing","GMT+08:00","(GMT+08:00) Chongqing"],["Hong Kong","GMT+08:00","(GMT+08:00) Hong Kong"],["Irkutsk","GMT+08:00","(GMT+08:00) Irkutsk"],["Kuala Lumpur","GMT+08:00","(GMT+08:00) Kuala Lumpur"],["Perth","GMT+08:00","(GMT+08:00) Perth"],["Singapore","GMT+08:00","(GMT+08:00) Singapore"], | |
["Taipei","GMT+08:00","(GMT+08:00) Taipei"],["Ulaan Bataar","GMT+08:00","(GMT+08:00) Ulaan Bataar"],["Urumqi","GMT+08:00","(GMT+08:00) Urumqi"],["Osaka","GMT+09:00","(GMT+09:00) Osaka"],["Sapporo","GMT+09:00","(GMT+09:00) Sapporo"],["Seoul","GMT+09:00","(GMT+09:00) Seoul"],["Tokyo","GMT+09:00","(GMT+09:00) Tokyo"],["Yakutsk","GMT+09:00","(GMT+09:00) Yakutsk"],["Adelaide","GMT+09:30","(GMT+09:30) Adelaide"],["Darwin","GMT+09:30","(GMT+09:30) Darwin"],["Brisbane","GMT+10:00","(GMT+10:00) Brisbane"],["Canberra","GMT+10:00","(GMT+10:00) Canberra"],["Guam","GMT+10:00","(GMT+10:00) Guam"],["Hobart","GMT+10:00","(GMT+10:00) Hobart"],["Melbourne","GMT+10:00","(GMT+10:00) Melbourne"],["Port Moresby","GMT+10:00","(GMT+10:00) Port Moresby"],["Sydney","GMT+10:00","(GMT+10:00) Sydney"],["Vladivostok","GMT+10:00","(GMT+10:00) Vladivostok"],["Kamchatka","GMT+11:00","(GMT+11:00) Kamchatka"], | |
["Magadan","GMT+11:00","(GMT+11:00) Magadan"],["New Caledonia","GMT+11:00","(GMT+11:00) New Caledonia"],["Solomon Is.","GMT+11:00","(GMT+11:00) Solomon Is."],["Auckland","GMT+12:00","(GMT+12:00) Auckland"],["Fiji","GMT+12:00","(GMT+12:00) Fiji"],["Marshall Is.","GMT+12:00","(GMT+12:00) Marshall Is."],["Wellington","GMT+12:00","(GMT+12:00) Wellington"],["Nuku'alofa","GMT+13:00","(GMT+13:00) Nuku'alofa"]]; | |
// The different reminders for the Task | |
var REMINDER_TYPES = [ null, "15 Mins", "30 mins", "1 Hour", "2 Hours", "1 Day", "2 Days" ]; | |
// script by Josh Fraser (http://www.onlineaspect.com) | |
//http://onlineaspect.com/examples/timezone/detect_timezone.js | |
var TimeZone = { | |
getTimeZoneIndex: function(){ | |
var timezone = this.calculate_time_zone(); | |
for(var i = 0; i< TIMEZONES.length; i++) { | |
if( TIMEZONES[i][1].indexOf(timezone) != -1) { | |
return i; | |
}; | |
}; | |
return 0; | |
} | |
,calculate_time_zone: function () { | |
var rightNow = new Date(); | |
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // jan 1st | |
var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st | |
var temp = jan1.toGMTString(); | |
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); | |
temp = june1.toGMTString(); | |
var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)); | |
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60); | |
var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60); | |
var dst; | |
if (std_time_offset == daylight_time_offset) { | |
dst = "0"; // daylight savings time is NOT observed | |
} else { | |
// positive is southern, negative is northern hemisphere | |
var hemisphere = std_time_offset - daylight_time_offset; | |
if (hemisphere >= 0) { | |
std_time_offset = daylight_time_offset; | |
} | |
dst = "1"; // daylight savings time is observed | |
}; | |
return this.convert(std_time_offset); | |
} | |
,convert: function(value) { | |
var hours = parseInt(value); | |
value -= parseInt(value); | |
value *= 60; | |
var mins = parseInt(value); | |
value -= parseInt(value); | |
value *= 60; | |
var secs = parseInt(value); | |
var display_hours = hours; | |
// handle GMT case (00:00) | |
if (hours == 0) { | |
display_hours = "00"; | |
} else if (hours > 0) { | |
// add a plus sign and perhaps an extra 0 | |
display_hours = (hours < 10) ? "+0"+hours : "+"+hours; | |
} else { | |
// add an extra 0 if needed | |
display_hours = (hours > -10) ? "-0"+Math.abs(hours) : hours; | |
} | |
mins = (mins < 10) ? "0"+mins : mins; | |
return display_hours+":"+mins; | |
} | |
}; | |
var listenOnce = function(event, handler) { | |
var handlerWrapper = function(e) { | |
handler(e); | |
Ti.App.removeEventListener(event, handlerWrapper); | |
}; | |
Ti.App.addEventListener(event, handlerWrapper); | |
}; | |
var listenButUnregisterWhenClose = function( win, hash ) { | |
_.each(hash, function(handler, event) { | |
Ti.App.addEventListener(event, handler); | |
win.addEventListener('close', function() { | |
log('unregistering' + event); | |
Ti.App.removeEventListener(event, handler); // make sure we unlisten on the way out | |
}); | |
}); | |
}; | |
var listenToDefaultEvents = function(win) { | |
listenButUnregisterWhenClose(win, { | |
'app:views:updateColors' : function(event) { | |
win.barColor = App.styles.barColor; | |
} | |
}); | |
}; | |
function confirmAndCall(number) { | |
var confirmationDialog = Ti.UI.createOptionDialog({ | |
title: 'What do you want to do?', | |
options: ['Call','Send SMS', 'Cancel'], | |
destructive: 2, | |
cancel: 2 | |
}); | |
confirmationDialog.addEventListener('click', function(e) { | |
switch(e.index) { | |
case 0: | |
Ti.Platform.openURL('tel:' + number); | |
break; | |
case 1: | |
Ti.Platform.openURL('sms:' + number); | |
break; | |
}; | |
}); | |
confirmationDialog.show(); | |
}; | |
var cachedFiles = {}; | |
/* load a file and cache it so next time we don't have to re-evaluate again */ | |
function load(path) { | |
if(!cachedFiles[path] || DEBUG ){ | |
log('caching ' + path ); | |
cachedFiles[path] = require(path); | |
}; | |
return cachedFiles[path]; | |
}; | |
/* return an intermediate page in simulator so that | |
we can re-load the top-level pages dynamically | |
*/ | |
function rootLoad(path) { | |
if(DEBUG) { | |
return { | |
createWindow: function() { | |
var win = createWindow({title: 'Landing Page'}); | |
var tableView = Ti.UI.createTableView({hasChild: true, style: Ti.UI.iPhone.TableViewStyle.GROUPED}); | |
tableView.setData([{title: 'Open Page'}]); | |
tableView.addEventListener('click', function(e) { | |
currentTabGroup.activeTab.open( load(path).createWindow() ); | |
}); | |
win.add(tableView); | |
listenToDefaultEvents(win); | |
return win; | |
} | |
}; | |
} else { | |
return load(path); | |
}; | |
}; | |
/* create a window */ | |
function createWindow(options) { | |
options = _.extend({ | |
title: 'Window', | |
barColor: App.styles.barColor | |
}, options || {}); | |
var win = Ti.UI.createWindow(options); | |
// for regular window, listen to the default events | |
if(!options.tabBarHidden || options.listenToDefaultEvents) { | |
listenToDefaultEvents(win); | |
}; | |
return win; | |
}; | |
/* Create a new window */ | |
function createWebView(options) { | |
options = _.extend({ | |
title: 'Web', | |
url: '', | |
controlHidden: false, | |
tabBarHidden: false | |
}, options || {} ); | |
var win = Ti.UI.createWindow({ title: options.title, barColor: App.styles.barColor, tabBarHidden: options.tabBarHidden }); | |
win.orientationModes = [ Ti.UI.PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT ]; | |
var webView = Ti.UI.createWebView( {top: 0}); | |
webView.url = options.url; | |
if(!options.controlHidden) { | |
var nav = Ti.UI.createButtonBar({ labels:['Back', 'Reload', 'Forward'], backgroundColor: App.styles.tabbedBarColor }); | |
var flexSpace = Ti.UI.createButton({ systemButton: Ti.UI.iPhone.SystemButton.FLEXIBLE_SPACE }); | |
win.setToolbar([flexSpace,nav,flexSpace]); | |
nav.addEventListener('click',function(e) { | |
switch(e.index) { | |
case 0: | |
webView.goBack(); | |
break; | |
case 1: | |
webView.reload(); | |
break; | |
case 2: | |
webView.goForward(); | |
break; | |
}; | |
}); | |
}; | |
win.add(webView); | |
win.leftNavButton = Ti.UI.createLabel({}); | |
var closeBtn = Ti.UI.createButton({ title: 'Close' }); | |
closeBtn.addEventListener('click', function(e) { win.close() } ); | |
win.rightNavButton = closeBtn; | |
return win; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment