|
/** |
|
* Icon module |
|
* app/lib/icon.js |
|
* |
|
* Inserts icons into Labels, and Buttons from an IcoMoon font icon set |
|
* |
|
* Inspired by: https://github.com/MattMcFarland/com.mattmcfarland.fontawesome |
|
*/ |
|
|
|
const fontFamily = 'icomoon'; |
|
const defaultSize = OS_IOS ? '24pt' : 24; |
|
let iosSystem = {}; |
|
if (OS_IOS) iosSystem = { |
|
share: Ti.UI.iOS.SystemButton.ACTION, |
|
}; |
|
|
|
/** |
|
* Read selection.json from IcoMoon to auto-generate char mapping |
|
*/ |
|
const config = '/fonts/selection.json'; |
|
const file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, config); |
|
const selection = JSON.parse(file.read().text); |
|
//console.log(selection); |
|
|
|
const charMap = selection.icons.reduce((obj, icon) => { |
|
const { name, code } = icon.properties; |
|
obj[name] = String.fromCodePoint(code); |
|
return obj; |
|
}, {}); |
|
//console.log('font icons', charMap); |
|
|
|
function getIconChar(name) { |
|
//console.log('geIconChar', name); |
|
if (charMap.hasOwnProperty(name)) { |
|
return charMap[name]; |
|
} |
|
|
|
return name; |
|
} |
|
|
|
function setIconFont(orig, size) { |
|
const font = orig || {}; |
|
|
|
font.fontWeight = 'normal'; |
|
font.fontFamily = fontFamily; |
|
font.fontSize = size || font.fontSize || defaultSize; |
|
|
|
return font; |
|
} |
|
|
|
function addIconToString(string, icon) { |
|
if (string && string.trim() !== '') { |
|
return icon + ' ' + string; |
|
} |
|
|
|
return icon; |
|
} |
|
|
|
function replaceIconInString(string, oldIcon, newIcon) { |
|
let newString = string.replace(oldIcon, newIcon); |
|
|
|
if (newString == string) { |
|
newString = addIconToString(string, newIcon); |
|
} |
|
|
|
return newString; |
|
} |
|
|
|
function setIcon(icon) { |
|
const prevIcon = `${this.iconChar}`; |
|
//console.log('setIcon', this); |
|
if (this.hasOwnProperty('text')) { // Label |
|
//console.log('Label.setIcon:', icon); |
|
this.iconChar = getIconChar(icon); |
|
this.text = replaceIconInString(this.text, prevIcon, this.iconChar); |
|
return; |
|
} |
|
else if (this.hasOwnProperty('title')) { // Button |
|
//console.log('Button.setIcon:', icon); |
|
this.iconChar = getIconChar(icon); |
|
this.title = replaceIconInString(this.title, prevIcon, this.iconChar); |
|
return; |
|
} |
|
} |
|
|
|
/** |
|
* Adds icon to a Label |
|
*/ |
|
function createLabelIcon(args, $ = null) { |
|
//console.log('Creating label icon:', args.icon); |
|
args.text = args.text || ''; |
|
|
|
if (args.icon) { |
|
args.iconChar = getIconChar(args.icon); |
|
args.text = addIconToString(args.text, args.iconChar); |
|
} |
|
|
|
args.font = setIconFont(args.font, args.iconSize); |
|
args.verticalAlign = Ti.UI.TEXT_VERTICAL_ALIGNMENT_TOP; |
|
args.classes = args.classes ? args.classes : []; |
|
if (typeof args.classes === 'string') { |
|
args.classes = [args.classes]; |
|
} |
|
args.classes.push('icon'); |
|
|
|
const $l = Ti.UI.createLabel(args); |
|
$l.setIcon = setIcon; |
|
|
|
if ($) { |
|
const style = $.createStyle({ |
|
classes: args.classes, |
|
apiName: 'Label', |
|
}); |
|
$l.applyProperties(style); |
|
} |
|
|
|
return $l; |
|
} |
|
|
|
/** |
|
* Adds icon to a Button |
|
*/ |
|
function createButtonIcon(args, $ = null) { |
|
//console.log('Creating button icon:', args); |
|
args.title = args.title || ''; |
|
|
|
if (args.icon) { |
|
if (OS_IOS && args.iosSystem && iosSystem.hasOwnProperty(args.icon)) { |
|
args.systemButton = iosSystem[args.icon]; |
|
} |
|
else { |
|
args.iconChar = getIconChar(args.icon); |
|
|
|
//args.title = addIconToString(args.title, args.iconChar); |
|
args.title = args.iconChar; |
|
} |
|
} |
|
|
|
if (OS_IOS && !args.style) { |
|
args.style = Ti.UI.iOS.SystemButtonStyle.PLAIN; |
|
} |
|
|
|
args.font = setIconFont(args.font, args.iconSize); |
|
args.classes = args.classes ? args.classes : []; |
|
if (typeof args.classes === 'string') { |
|
args.classes = [args.classes]; |
|
} |
|
args.classes.push('icon'); |
|
|
|
const $b = Ti.UI.createButton(args); |
|
$b.setIcon = setIcon; |
|
|
|
if ($) { |
|
const style = $.createStyle({ |
|
classes: args.classes, |
|
apiName: 'Button', |
|
}); |
|
$b.applyProperties(style); |
|
} |
|
|
|
return $b; |
|
} |
|
|
|
/** |
|
* Adds icon to a Toggle Button |
|
* |
|
* NOTE: this probably needs some love |
|
*/ |
|
function createSwitchIcon(args) { |
|
//console.log('Creating switch icon:', args); |
|
if (args.icon || (args.iconOn && args.iconOff)) { |
|
_.defaults(args, { |
|
value: false, |
|
color: 'white', |
|
//tintColor: 'blue', |
|
titleOn: 'On', |
|
titleOff: 'Off', |
|
}); |
|
let vArgs = args; |
|
_.defaults(vArgs, { |
|
layout: 'horizontal', |
|
height: Ti.UI.SIZE, |
|
width: Ti.UI.SIZE, |
|
//color: null, |
|
//tintColor: 'blue', |
|
}); |
|
let $v = Ti.UI.createView(vArgs); |
|
|
|
let iArgs = { |
|
font: args.font, |
|
iconSize: args.iconSize, |
|
color: args.value ? args.tintColor : args.color, |
|
classes: ['icon', 'iconLeft'], |
|
right: 5, |
|
//touchEnabled: false, |
|
}; |
|
if (args.iconOff && args.iconOn) { |
|
iArgs.icon = args.value ? args.iconOn : args.iconOff; |
|
} |
|
else { |
|
iArgs.icon = args.icon; |
|
} |
|
let $i = createLabelIcon(iArgs); |
|
$i.setIcon = setIcon; |
|
|
|
let $l = Ti.UI.createLabel({ |
|
font: args.font, |
|
color: args.value ? args.tintColor : args.color, |
|
text: args.value ? args.titleOn : args.titleOff, |
|
//touchEnabled: false, |
|
}); |
|
|
|
$v.add([$i, $l]); |
|
$v.icon = $i; |
|
$v.label = $l; |
|
|
|
$v.setValue = function(val) { |
|
$v.value = val; |
|
|
|
$v.icon.color = $v.label.color = $v.value ? $v.tintColor : $v.color; |
|
$v.label.text = $v.value ? $v.titleOn : $v.titleOff; |
|
|
|
if ($v.iconOn && $v.iconOff) { |
|
//Ti.API.debug('Updating icon: '+($v.value ? $v.iconOn : $v.iconOff)); |
|
$v.icon.setIcon($v.value ? $v.iconOn : $v.iconOff); |
|
} |
|
}; |
|
|
|
$v.addEventListener('click', function(e) { |
|
$v.setValue(!$v.value); |
|
}); |
|
|
|
$v.setIcon = $i.setIcon; |
|
|
|
return $v; |
|
} |
|
|
|
return Ti.UI.createSwitch(args); |
|
} |
|
|
|
exports.fontFamily = fontFamily; |
|
exports.defaultSize = defaultSize; |
|
exports.charMap = charMap; |
|
exports.getIconChar = getIconChar; |
|
exports.createLabel = createLabelIcon; |
|
exports.createButton = createButtonIcon; |
|
exports.createSwitch = createSwitchIcon; |