Skip to content

Instantly share code, notes, and snippets.

@falkolab
Created November 27, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save falkolab/c373afd660c6aa1f932b to your computer and use it in GitHub Desktop.
Save falkolab/c373afd660c6aa1f932b to your computer and use it in GitHub Desktop.
list view databinding helper
exports.populateCallLog = function(listview, items) {
var helper = require('listViewHelper');
return helper.populate(listview, items, false /*no merge*/, function sectionGenerator(lv, items) {
var moment = require("alloy/moment");
var headerViewCtrl = Alloy.createController('modules/call/log/pickerSectionHeaderView');
var dateTitle,
item = items[0],
utcTime = new Date(),
timeZone = utcTime.getTimezoneOffset(),
datetime = moment(item.properties.timestamp);
var dayDiff = moment().diff(datetime, 'hours');
if (dayDiff <= 24) {
dateTitle = L("Today");
} else if (dayDiff > 24 && dayDiff <= 24*2) {
dateTitle = L("Yesterday");
} else {
dateTitle = datetime.zone(timeZone).format('L');
}
headerViewCtrl.updateViews({
"#dateTitle" : {
text : dateTitle
}
});
section = Ti.UI.createListSection({
headerView : headerViewCtrl.getView(),
footerView: Alloy.createController('modules/call/log/pickerSectionFooterView').getView()
});
headerViewCtrl = null;
return section;
}, null, function sortItemIteratee(value) {
return -value.properties.timestamp;
}, null);
};
exports.populate = function(listview, items, merge,
sectionGenerator, groupByIteratee, sortItemsIteratee, sortSectionsIteratee) {
if (!_.isFunction(sortItemsIteratee)) {
sortItemsIteratee = function defaultSortItemsIteratee(value) {
return -parseInt(value.properties.itemId);
};
}
if (!_.isFunction(sortSectionsIteratee)) {
sortSectionsIteratee = function defaultSortSectionsIteratee(value) {
return -parseInt(value.__groupKey);
};
}
if (!_.isFunction(groupByIteratee)) {
groupByIteratee = function defaultGroupByIteratee(item) {
return item.properties.groupKey;
};
}
var sections = _.chain(items).groupBy(groupByIteratee).pairs().map(function(pairs) {
var section;
var items = pairs[1], groupKey = pairs[0];
if(merge) {
section = _.findWhere(listview.sections, {
__groupKey : groupKey
});
if (section) {
var newItems = section.items.concat(items);
newItems = _.sortBy(newItems, sortItemsIteratee);
section.items = newItems;
} else {
section = sectionGenerator(listview, items);
section.__groupKey = groupKey;
section.setItems(_.sortBy(items, sortItemsIteratee));
var appendSection,
insertAtIndex;
if (!listview.sections.length) {
appendSection = true;
} else {
// определим куда надо вставить секцию
var tmpSections = _.clone(listview.sections);
tmpSections.push(section);
var keys = _.chain(tmpSections)
.sortBy(sortSectionsIteratee)
.map(_.property('__groupKey'))
.value();
tmpSections = null;
insertAtIndex = keys.indexOf(section.__groupKey);
appendSection = keys[keys.length - 1] == section.__groupKey;
}
if (appendSection) {
listview.appendSection(section);
} else {
listview.insertSectionAt(insertAtIndex, section);
}
}
} else {
section = sectionGenerator(listview, items);
section.__groupKey = groupKey;
section.setItems(items);
return section;
}
}).value();
if(!merge) {
Ti.API.info('>>> sections', sections.length);
listview.sections = sections;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment