Created
November 5, 2012 15:03
-
-
Save lcruz/4017627 to your computer and use it in GitHub Desktop.
Generic picker for Android and iOs
This file contains 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
var Picker = require("/commons/picker"); | |
var oPicker = new Picker(); | |
// | |
// create base UI tab and root window | |
// | |
var win = Titanium.UI.createWindow({ | |
title:'Ejemplo', | |
backgroundColor:'#fff' | |
}); | |
oPicker.addToWindow(win); | |
oPicker.addEventListener("cancel", function() { | |
oPicker.hide(); | |
}); | |
var button = Ti.UI.createButton({ title: 'Mostrar' }); | |
button.addEventListener("click", function() { | |
oPicker.show(); | |
}); | |
win.add(button); | |
win.open(); |
This file contains 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 Picker() { | |
var self = this; | |
this.eventListeners = []; | |
/* Contenedor para el picker */ | |
this.picker_view = Ti.UI.createView({ | |
layout: 'vertical', | |
height:300 | |
}); | |
var label = Ti.UI.createLabel({ text:'test'}); | |
this.picker_view.add(label); | |
this.picker = Titanium.UI.createPicker({ | |
useSpinner : true, | |
backgroundColor:"#0C0" | |
}); | |
var picker_data = [ | |
Titanium.UI.createPickerRow({title:'John'}), | |
Titanium.UI.createPickerRow({title:'Alex'}), | |
Titanium.UI.createPickerRow({title:'Marie'}), | |
Titanium.UI.createPickerRow({title:'Eva'}), | |
Titanium.UI.createPickerRow({title:'James'}) | |
]; | |
this.picker.add(picker_data); | |
this.picker_view.add(this.picker); | |
if (Ti.Platform.osname == 'android') { | |
var opt = Ti.UI.createOptionDialog(); | |
//opt.androidView = this.picker_view; | |
opt.options = ['Confirm', 'Help', 'Cancel','Confirm', 'Help', 'Cancel','Confirm', 'Help', 'Cancel','Confirm', 'Help', 'Cancel'], | |
opt.buttonNames = ['Cancel','Done']; | |
opt.cancel = 0; | |
opt.addEventListener('click', function (event) { | |
if (!event || !event.index) { | |
self.fireEvent("cancel"); | |
return; | |
} | |
if (1 == event.index) { | |
self.fireEvent("done"); | |
//field.value = picker.getSelectedRow(0).title | |
} | |
}); | |
this.opt = opt; | |
} else { | |
var cancel = Titanium.UI.createButton({ | |
title:'Cancelar', | |
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED | |
}); | |
var done = Titanium.UI.createButton({ | |
title:'Listo', | |
style:Titanium.UI.iPhone.SystemButtonStyle.DONE | |
}); | |
done.addEventListener("click", function(e) { | |
self.fireEvent("done"); | |
}); | |
cancel.addEventListener("click", function(e) { | |
self.fireEvent("cancel"); | |
}); | |
var spacer = Titanium.UI.createButton({ | |
systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE | |
}); | |
var toolbar = Titanium.UI.iOS.createToolbar({ | |
top:0, | |
items:[cancel,spacer,done] | |
}); | |
this.picker_view.add(toolbar); | |
} | |
} | |
/** | |
* Muestra el picker | |
*/ | |
Picker.prototype.show = function() { | |
Ti.API.info("Mostrando"); | |
if (Ti.Platform.osname == 'android') { | |
this.opt.show(); | |
} else { | |
var slide_in = Titanium.UI.createAnimation({bottom:0}); | |
this.picker_view.animate(slide_in); | |
} | |
} | |
Picker.prototype.hide = function() { | |
if (Ti.Platform.osname == 'android') { | |
this.opt.hide(); | |
} else { | |
var slide_out = Titanium.UI.createAnimation({bottom:-251}) | |
this.picker_view.animate(slide_out); | |
} | |
} | |
Picker.prototype.addEventListener = function(eventName, handler) { | |
this.eventListeners.push({ 'eventName' : eventName, 'handler' : handler }); | |
} | |
Picker.prototype.addToWindow = function(win) { | |
Ti.API.info("Agregando"); | |
if (Ti.Platform.osname != 'android') { | |
win.add(this.picker_view); | |
} | |
} | |
Picker.prototype.fireEvent = function(eventName) { | |
Ti.API.info("Fire event:" + eventName); | |
for (var i = 0; i < this.eventListeners.length; i++) { | |
Ti.API.info(this.eventListeners[i]['eventName'] + "===" + eventName); | |
var eventListener = this.eventListeners[i]; | |
if (eventListener['eventName'] == eventName) { | |
eventListener['handler'].call(); | |
} | |
} | |
} | |
module.exports = Picker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment