The goal is to list out some data, but the methods vary on options, and whether the user is on a touch device.
This is my current function declaration. Very non-DRY. Values of isTouch
and headlinesOnly
don't change during loop, so I don't want to make if
tests inside the loop.
Edit: This is primarily meant for a memory-sensitive environment which, when not handled properly, reboots to safe mode, not a regular browser. (But I'm making it for both.)
var makeElement;
if (isTouch) {
if (headlinesOnly) {
makeElement = function(item, top, left) {
var a = document.createElement("a");
a.setAttribute('ontouchstart', 'itemTouchStart()');
a.setAttribute('ontouchmove', 'itemTouchMove()');
a.setAttribute('ontouchend', 'itemTouchEnd(\'' + item.link + '\')');
a.style.top = top + 'px';
a.style.left = left + 'px';
a.innerHTML = '<h1>' + item.title + '</h1>';
return a;
};
}
else {
makeElement = function(item, top, left) {
var a = document.createElement("a");
a.setAttribute('ontouchstart', 'itemTouchStart()');
a.setAttribute('ontouchmove', 'itemTouchMove()');
a.setAttribute('ontouchend', 'itemTouchEnd(\'' + item.link + '\')');
a.style.top = top + 'px';
a.style.left = left + 'px';
a.innerHTML = '<h1>' + item.title + '</h1>'
+ '<div>' + item.content + '</h1>';
return a;
};
}
var dragging;
window['itemTouchStart'] = function() {
dragging = false;
};
window['itemTouchMove'] = function() {
dragging = true;
};
window['itemTouchEnd'] = function(url) {
if (dragging) return;
if (cycript) {
tryOpenUrl(url);
}
else {
window.open(url, "_blank");
}
};
}
else {
if (headlinesOnly) {
makeElement = function(item, top, left) {
var a = document.createElement("a");
a.setAttribute('onmouseup', 'itemMouseUp(\'' + item.link + '\')');
a.style.top = top + 'px';
a.style.left = left + 'px';
a.innerHTML = '<h1>' + item.title + '</h1>';
return a;
};
}
else {
makeElement = function(item, top, left) {
var a = document.createElement("a");
a.setAttribute('onmouseup', 'itemMouseUp(\'' + item.link + '\')');
a.style.top = top + 'px';
a.style.left = left + 'px';
a.innerHTML = '<h1>' + item.title + '</h1>'
+ '<div>' + item.content + '</h1>';
return a;
};
}
window['itemMouseUp'] = function(url) {
window.open(url, "_blank");
};
}
var item = myCollection.shift();
container.appendChild(makeElement(item));
var columnWidth = container.childNodes[0].offsetWidth;
var containerWidth = container.offsetWidth;
var numberOfColumns = containerWidth / columnWidth;
var columns = [];
for (var i = 0; i < numberOfColumns; i++) {
this.columns.push(0);
}
while (item = myCollection.shift()) {
var sh = getShortestColumn();
var itemElement = makeElement(item, sh.height, sh.index * columnWidth);
container.appendChild(itemElement);
columns[sh.index] += itemElement.offsetHeight;
}