Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix for Titanium SDK bug TC-5848 (Button 9-patch)
/*
Created by: Andrey Tkachenko falko.lab@gmail.com
Source: https://gist.github.com/falkolab/c56585874223216e85d3
This is fix code for https://jira.appcelerator.org/browse/TIMOB-20357
!!! You must use only $.addListener for fixed button NOT button.addEventListener
`win` argument can be omited if controller root is Window already.
example:
require('fix_timob20357').fix('myButton', $);
*/
exports.fix = function(buttonId, $, win) {
if(!OS_ANDROID) return;
if(!_.isFunction($.addListener)) {
Ti.API.error("Can't apply fix tc5848 for this Alloy version");
return;
}
var button = $.getView(buttonId);
if(!button) {
// button id can be changed, need exception
throw "Can't find button: " + buttonId;
//Ti.API.error("Can't find button:", buttonId);
return;
}
if(button.apiName !== 'Ti.UI.Button' || !_.has(button._properties, 'backgroundImage')) {
// not need fix;
return;
}
win = win || $.getView();
if(win.apiName !== 'Ti.UI.Window') {
Ti.API.error("Can't apply fix without window");
return;
}
// cleanup button
var parent = button.getParent();
$.removeView(buttonId);
var listeners = $.getListener(button);
$.removeListener(button);
parent.remove(button);
var delayedProperties = _.pick(button._properties, 'backgroundColor', 'backgroundImage');
// create clone of button
button = Ti.UI.createButton(_.omit(button._properties, 'backgroundColor', 'backgroundImage'));
$[buttonId] = button;
$.__views[buttonId] = button;
parent.add(button);
_.each(listeners, function(evt) {
$.addListener(button, evt.type, evt.handler);
});
if(/* isWindowOpen */ !!win.activity.actionBar) {
button.applyProperties(delayedProperties);
} else {
win.addEventListener('open', function applyFix(evt) {
evt.source.removeEventListener(evt.type, applyFix);
button.applyProperties(delayedProperties);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment