Created
August 14, 2012 19:34
-
-
Save grantges/3352035 to your computer and use it in GitHub Desktop.
Utility Library for Appcelerator Titanium
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
//Shortcuts | |
var $P = Ti.App.Properties; | |
var $F = Ti.Filesystem; | |
// PLATFORM RELATED Functions | |
exports.osname = Ti.Platform.osname; | |
exports.iOS = function() { return (Ti.Platform.osname === 'ipad' || Ti.Platform.osname ==='iphone')}; | |
exports.android = function() { return Ti.Platform.osname === 'android' } | |
exports.isTablet = function(){ | |
if(Ti.Platform.osname ==='ipad') return true; | |
if(Ti.Platform.osname === 'android'){ | |
if(exports.screenHeight > 800 || exports.screenWidth > 800) return true; | |
} | |
return false; | |
} | |
exports.screenHeight = function() { | |
return Ti.Platform.displayCaps.platformHeight; | |
}; | |
exports.screenWidth = function() { | |
return Ti.Platform.displayCaps.platformWidth; | |
}; | |
/** | |
* require - abstracts the require attribute to account for proper format and platform | |
* @param {String} cat - Category of the module / file being requested; Conforms to the root folder name (Ex. ui) | |
* @param {String} mod - Module or filename minus the .js extension | |
*/ | |
exports.require = function(cat, mod){ | |
var deviceType = (exports.isTablet()) ? 'tablet' : 'handheld', | |
modPath = null; | |
// First see if we might have a winner in device / platform specific branch | |
modPath = exports.getModulePath(cat+Ti.Filesystem.separator+deviceType+Ti.Filesystem.separator + exports.osname + Ti.Filesystem.separator +mod+'.js'); | |
if(modPath===null) { // Then try to see if it might be just a device speicifc file | |
modPath = exports.getModulePath(cat+Ti.Filesystem.separator+deviceType+Ti.Filesystem.separator + mod+'.js'); | |
if(modPath === null) { // OK maybe not - is it common across types and platforms? | |
modPath = exports.getModulePath(cat+ Ti.Filesystem.separator + 'common' + Ti.Filesystem.separator + mod+'.js'); | |
if(modPath === null) { // Well last resort try the initial category folder | |
modPath = exports.getModulePath(cat+Ti.Filesystem.separator+mod+'.js'); | |
} | |
} | |
} | |
return (modPath) ? require(modPath) : null; | |
} | |
exports.getModulePath = function(path) { | |
var f = Ti.Filesystem.getFile(path); | |
return ( f.exists() ) ? (path.replace('.js','')) : null; | |
} | |
exports.findFile = function(dir, name) { | |
var root = Ti.Filesystem.getFile(dir); | |
var files = root.getDirectoryListing(); | |
var result = null; | |
if( files.length ) { | |
for(var i=0, l= files.length; i < l; i++) { | |
var f = Ti.Filesystem.getFile(root.getNativePath()+Ti.Filesystem.separator+files[i]); | |
if(files[i] === name) { | |
if(f.exists()) { result = f.getNativePath();} | |
} | |
else { | |
if(isDirectory(f)) { | |
result = exports.findFile(dir+Ti.Filesystem.separator+files[i], name); | |
} | |
} | |
if(result !== null) | |
return result; | |
} | |
} | |
return result; | |
} | |
exports.isDirectory = function(file) { | |
if(Ti.Platform.osname === 'ipad' || Ti.Platform.osname === 'iphone') { | |
return file.nativePath.lastIndexOf('/') === file.nativePath.length-1; | |
} | |
return file.isDirectory(); | |
}; | |
exports.mixin = function (target, source) { | |
// Credit to Appcelerator's TiBountyHunter code | |
var name, s, i; | |
for (name in source) { | |
if (source.hasOwnProperty(name)) { | |
s = source[name]; | |
if(!(name in target) || (target[name] !== s)) { | |
target[name] = s; | |
} | |
} | |
} | |
return target; // Object | |
} | |
exports.assignCallbacks = function(target, source){ | |
var evt; | |
if(source.callbacks) { | |
for(evt in source){ | |
target.addEventListener(evt, source[evt]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment