Skip to content

Instantly share code, notes, and snippets.

@iskugor
Created December 16, 2011 11:40
Show Gist options
  • Save iskugor/1485751 to your computer and use it in GitHub Desktop.
Save iskugor/1485751 to your computer and use it in GitHub Desktop.
Titanium insert after functionality
var win = Ti.UI.createWindow({
backgroundColor: '#39f',
layout: 'vertical'
});
var wrapper = Ti.UI.createView({
layout: 'vertical'
});
var label1 = Ti.UI.createLabel({
text: 'Label 1'
});
label1.toString = function() {
return this.text;
};
var label2 = Ti.UI.createLabel({
text: 'Label 2'
});
label2.toString = function() {
return this.text;
};
var label3 = Ti.UI.createLabel({
text: 'Label 3'
});
label3.toString = function() {
return this.text;
};
var label4 = Ti.UI.createLabel({
text: 'Label 4'
});
label4.toString = function() {
return this.text;
};
wrapper.add(label1);
wrapper.add(label2);
wrapper.add(label3);
win.add(wrapper);
//Ti.API.debug(label1.parent);
function insertAfter(parent, element, after) {
Ti.API.info('Insert after');
Ti.API.debug(parent);
Ti.API.debug(element);
Ti.API.debug(after);
Ti.API.debug(parent.children);
Ti.API.debug(parent.children.length);
Ti.API.info('after');
Ti.API.debug(element);
Ti.API.debug(after);
var afterIndex = -1;
var children = parent.children.slice(0);
Ti.API.info('children');
Ti.API.debug(children.join(', '));
for (var i = 0; i < children.length; ++i) {
if (after === children[i]) {
afterIndex = i;
Ti.API.info('After index: ' + afterIndex);
}
parent.remove(children[i]);
}
Ti.API.debug(parent.children.join(', '));
if (afterIndex >= 0) {
Ti.API.info('children splice');
var tmp = children.slice(0, afterIndex + 1);
Ti.API.debug(tmp.join(', '));
tmp.push(element);
Ti.API.debug(tmp.join(', '));
var tmp2 = children.slice(afterIndex + 1);
Ti.API.debug(tmp2.join(', '));
children = tmp.concat(tmp2);
Ti.API.debug(children.join(', '));
}
for (i = 0; i < children.length; ++i) {
Ti.API.info('children add');
Ti.API.debug(children[i]);
parent.add(children[i]);
}
}
setTimeout(function() { insertAfter(wrapper, label4, label2); }, 3000);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment