Skip to content

Instantly share code, notes, and snippets.

@itspriddle
Created October 27, 2010 05:08
Show Gist options
  • Select an option

  • Save itspriddle/648492 to your computer and use it in GitHub Desktop.

Select an option

Save itspriddle/648492 to your computer and use it in GitHub Desktop.
/**
* Titanium Mobile Utility Functions
*
* @author Joshua Priddle <[email protected]>
* @version 0.0.0
*/
var $ = {};
/**
* Shorthand for Ti.API logger methods
*
* JSON.stringifys message if it is not a string
*/
$.log = function(message, level) {
if (typeof message != "string") {
message = JSON.stringify(message);
}
Ti.API[level || 'info'](message);
};
// --------------------------------------------------------------------
/**
* Fire an alert, should be used for debugging only
*
* JSON.stringifys message if it is not a string
*/
$.alert = function(message) {
if (typeof message != "string") {
message = JSON.stringify(message);
}
alert(message);
};
// --------------------------------------------------------------------
/**
* Iterates over an object invoking callback for each member
*
* callback: function(value, key) {}
*/
$.each = function(object, callback, context) {
for (var key in object) {
if (object[key]) {
callback.call(context, object[key], key, object);
}
}
};
// --------------------------------------------------------------------
/**
* Iterates over an object returning callback for each member
*
* callback: function(value, key) { return "Value is " + value; }
*/
$.map = function(object, callback, context) {
var out = [];
for (var key in object) {
if (object[key]) {
out.push(callback.call(context, object[key], key, object));
}
}
return out;
};
// --------------------------------------------------------------------
/**
* Extends destination with source's attributes
*/
$.extend = function(destination, source) {
for (var prop in source) {
if (source[prop]) {
destination[prop] = source[prop];
}
}
return destination;
};
// --------------------------------------------------------------------
/**
* Simple key/value store using Ti.App.Properties
*
* Set config:
* $.config('user', {username: 'itspriddle'})
*
* Get config:
* $.config('user')
*
* Delete config:
* $.config('user', null);
*/
$.config = function(key, value) {
if (arguments.length == 2) {
if (value === null) {
Ti.App.Properties.removeProperty(key);
} else {
Ti.App.Properties.setString(key, JSON.stringify(value));
}
} else if (arguments.length == 1) {
var data = Ti.App.Properties.getString(key, false);
return data && JSON.parse(data) || undefined;
}
};
// --------------------------------------------------------------------
/**
* Detect simulators
*
* If callback is supplied and we're using the simulator, execute/return it
* Otherwise return true or false if were using the simulator
*/
$.development = function(callback) {
var sim = Ti.Platform.model == 'google_sdk' || Ti.Platform.model == 'Simulator';
if (sim && callback) {
return callback();
} else {
return sim;
}
};
// --------------------------------------------------------------------
/**
* Detect production (basically the opposite of $.development)
*/
$.production = function(callback) {
var production = ! $.development();
if (production && callback) {
return callback();
} else {
return production;
}
};
// --------------------------------------------------------------------
/**
* Detect iphone
*
* If callback is supplied and this is iphone, execute and return it
* Otherwise return true if iphone or false
*/
$.iphone = function(callback) {
var iphone = Ti.Platform.osname == 'iphone';
if (iphone && callback) {
return callback();
} else {
return iphone;
}
};
// --------------------------------------------------------------------
/**
* Detect ipad
*
* If callback is supplied and this is ipad, execute and return it
* Otherwise return true if ipad or false
*/
$.ipad = function(callback) {
var ipad = Ti.Platform.osname == 'ipad';
if (ipad && callback) {
return callback();
} else {
return ipad;
}
};
// --------------------------------------------------------------------
/**
* Detect android
*
* If callback is supplied and this is android, execute and return it
* Otherwise return true if android or false
*/
$.android = function(callback) {
var android = Ti.Platform.osname == 'android';
if (android && callback) {
return callback();
} else {
return android;
}
};
// --------------------------------------------------------------------
/**
* strftime, based on http://github.com/github/jquery-relatize_date
*/
$.strftime = function(date, format) {
var shortDays = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var shortMonths = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
var day = date.getDay(), month = date.getMonth();
var hours = date.getHours(), minutes = date.getMinutes();
var pad = function(num) {
var string = num.toString(10);
return new Array((2 - string.length) + 1).join('0') + string;
};
return format.replace(/\%([aAbBcdHImMpSwyY])/g, function(part) {
var out = null;
switch (part[1]) {
case 'a': out = shortDays[day]; break;
case 'A': out = days[day]; break;
case 'b': out = shortMonths[month]; break;
case 'B': out = months[month]; break;
case 'c': out = date.toString(); break;
case 'd': out = pad(date.getDate()); break;
case 'H': out = pad(hours); break;
case 'I': out = pad((hours + 12) % 12); break;
case 'm': out = pad(month + 1); break;
case 'M': out = pad(minutes); break;
case 'p': out = hours > 12 ? 'PM' : 'AM'; break;
case 'S': out = pad(date.getSeconds()); break;
case 'w': out = day; break;
case 'y': out = pad(date.getFullYear() % 100); break;
case 'Y': out = date.getFullYear().toString(); break;
}
return out;
});
};
// --------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment