Skip to content

Instantly share code, notes, and snippets.

@gaustin
Created March 3, 2010 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaustin/320990 to your computer and use it in GitHub Desktop.
Save gaustin/320990 to your computer and use it in GitHub Desktop.
McAdmin.browseController = SC.TreeController.create(
/** @scope McAdmin.browseController.prototype */ {
allowsMultipleSelection: NO,
content: null,
count: 0,
countBinding: "*arrangedObjects.length",
displayItem: function() {
var list = McAdmin.browsePage.getPath('treeView.contentView'),
selectedItem = list.selection.firstObject(),
itemView = SC.LabelView.design({
escapeHTML: NO,
classNames: 'instructions-tab',
value: "<p>This is a test of the dynamic view broadcast system. This is only a test.</p>"
});
McAdmin.viewRawItemController.set('nowShowing', McAdmin.mainPage.getPath('instructions'));
}
});
// ==========================================================================
// Project: MetaCollection Admin
// MetaCollection Admin is a web-based tool for managing a MetaCollection installation.
//
// This project is hosted at Google Code at http://code.google.com/p/metacollection-admin/
//
// This project is licensed under the BSD license. See the License.txt file for more information.
// ==========================================================================
/*globals McAdmin */
sc_require('models/item');
McAdmin.ROOT_ITEM_QUERY = SC.Query.local(McAdmin.Item);
/** @class
(Document Your Data Source Here)
@extends SC.DataSource
*/
McAdmin.ItemDataSource = SC.DataSource.extend(
/** @scope McAdmin.ItemDataSource.prototype */ {
// ..........................................................
// QUERY SUPPORT
//
fetch: function(store, query) {
var propfindRequestData = '<?xml version="1.0"?><D:propfind xmlns:D="DAV:" xmlns:mc="uri://MetaCollection/v2"><D:prop><D:displayname/><mc:mimetype/><mc:resourceprovider/><mc:version/><mc:published/><mc:name/><mc:id/></D:prop></D:propfind>';
// TODO: Add handlers to fetch data for specific queries.
// call store.dataSourceDidFetchQuery(query) when done.
if (query === McAdmin.ROOT_ITEM_QUERY) {
// TODO: When loading an item tree we should use a remote query and skip/take items
var req = SC.Request.create()
.set('address', McAdmin.COLLECTION_URL)
.set('type', 'PROPFIND').set('body', propfindRequestData)
.xml()
.notify(this, 'didFetchItems', store, query)
req.isAsynchronous = NO,
req.send();
return YES;
} else if (query.partial) {
var req = SC.Request.create()
.set('address', query.href ? McAdmin.COLLECTION_URL + query.href : McAdmin.COLLECTION_URL)
.set('type', 'PROPFIND').set('body', propfindRequestData)
.xml()
.notify(this, 'didFetchItems', store, query)
req.isAsynchronous = NO,
req.send();
return YES;
}
return NO ; // return YES if you handled the query
},
didFetchItems: function(response, store, query) {
if (SC.ok(response)) {
store.loadRecords(McAdmin.Item, this.makeTreeItemList(response.get('body')));
store.dataSourceDidFetchQuery(query);
} else store.dataSourceDidErrorQuery(query, response);
},
makeTreeItemList: function(xmlResponses) {
var xmlRecords = xmlResponses.getElementsByTagName('D:response'),
records = [];
for(i = 0; i < xmlRecords.length; i++) {
var item = this.mungePropfind(xmlRecords[i])
records.push(this.rawItemToTreeItemChild(item));
}
return records;
},
rawItemToTreeItemChild: function(item) {
item.treeItemIsExpanded = NO;
item.title = item.name;
item.count = item.mimetype == "application/x-folder" ? 5 : 0;
if (item.mimetype == "application/x-folder") {
item.treeItemChildren = function(){
var ret = [],
inStore = McAdmin.store.find(McAdmin.Item, {guid: item.guid });
if (this.item.get('treeItemIsExpanded') && inStore.isError()) {
console.log("This href: " + this.item.get('href') + " - href:" + item.href);
var query = SC.Query.local(McAdmin.Item, {
href: this.item.get('href'),
partial: YES
}),
children = McAdmin.store.find(query);
item.count = children.length;
ret = children.slice(1, children.length);
}
return null;
}.property().cacheable()
}
return item;
},
mungePropfind: function(xmlRecord) {
var mungedItem = SC.Object.create({
href: xmlRecord.getElementsByTagName('D:href')[0].firstChild.nodeValue,
name: xmlRecord.getElementsByTagName('mc:name')[0].firstChild.nodeValue,
version: xmlRecord.getElementsByTagName('mc:version')[0].firstChild.nodeValue,
guid: xmlRecord.getElementsByTagName('mc:id')[0].firstChild.nodeValue,
mimetype: xmlRecord.getElementsByTagName('mc:mimetype')[0].firstChild.nodeValue
});
return mungedItem;
}
// snip a bunch of methods I'm not using
}) ;
McAdmin.Item = SC.Record.extend(
/** @scope McAdmin.Item.prototype */ {
href: SC.Record.attr(String),
name: SC.Record.attr(String),
guid: SC.Record.attr(String),
mimetype: SC.Record.attr(String),
version: SC.Record.attr(Number),
properties: SC.Record.attr(Array),
relationships: SC.Record.attr(Array),
primaryKey: "href",
children: SC.Record.toMany('McAdmin.Item', {inverse: "parent", isMaster: YES }),
parent: SC.Record.toOne('McAdmin.Item', {inverse: "children", isMaster: NO })
});
McAdmin.main = function main() {
McAdmin.getPath('mainPage.mainPane').append();
var itemRecords = McAdmin.store.find(McAdmin.ROOT_ITEM_QUERY),
itemRecordsLength = itemRecords.length();
var rootItemNode = SC.Object.create({
treeItemIsExpanded: YES,
title: "Inventory",
treeItemChildren: itemRecords,
count: itemRecords.length(),
});
McAdmin.browseController.set('content', rootItemNode);
};
function main() { McAdmin.main(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment