Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Created July 2, 2010 17:11
Show Gist options
  • Save joegaudet/461623 to your computer and use it in GitHub Desktop.
Save joegaudet/461623 to your computer and use it in GitHub Desktop.
// ==========================================================================
// Project: DarkHorse - The PC Browser targeted face of the Matygo Web App
// Copyright: ©2010 Matygo Educational Incorporated
// ==========================================================================
/*globals DarkHorse */
/** @class
(Document Your Controller Here)
@extends SC.Object
*/
DarkHorse.ScrollListController = SC.TreeController.extend({
groupKey: '',
groupCount: 0,
treeItemIsGrouped: YES,
treeItemIsExpanded: YES,
treeItemChildrenKey: 'treeItemChildren',
treeItemIsExpandedKey: 'treeItemIsExpanded',
length: 0,
ContentItem: SC.Object.extend({
treeItemIsExpanded: YES,
treeItemIsGrouped: YES,
isGroupRow: YES,
title: "Root",
isEnabled: NO,
isSelectable: NO,
treeItemChildren: []
}),
summary: function() {
var len = this.get('length'),
ret;
if (len && len > 0) {
ret = len === 1 ? "1 " + this._singleName: ("%@ " + this._pluralName).fmt(len);
} else ret = "No " + this._pluralName;
return ret;
}.property('length').cacheable(),
arrangedObjects: function() {
var ret, content = this.get('content');
content = this.filterContent(content);
content = this.groupContent(content);
if (content) {
ret = SC.TreeItemObserver.create({
item: content,
delegate: this
});
} else ret = null; // empty!
this._sctc_arrangedObjects = ret;
if(ret){
this.set('length',ret.get('length') - this.get('groupCount'));
}
return ret;
}.property('groupKey','filterFunction').cacheable(),
filterContent: function(content){
if(content){
content = content.filter(this.filterFunction,this);
}
return content;
},
filterFunction: function(item){
return YES;
},
groupContent: function(content) {
if (!content) return;
var groups = [];
var courseController = this;
var groupKey = this.groupKey;
this.set('groupCount',0);
content.forEach(function(contentItem) {
var group, groupName = contentItem.get(groupKey); // get the group we are in
for (var j = 0; j < groups.length; j++) {
if (groups[j].get(groupKey) === groupName) {
group = groups[j];
break;
}
}
if(!group) {
group = courseController.ContentItem.create({
treeItemChildren: []
});
this.incrementProperty('groupCount');
group.set(groupKey,groupName);
group.set('title',groupName);
groups.push(group);
}
contentItem.set('treeItemIsGrouped',YES);
group.treeItemChildren.push(contentItem);
},
this);
var courseItems = this.ContentItem.create({
treeItemChildren: groups
});
return courseItems;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment