// * Add initialize() handler
document.observe("dom:loaded", function() { try {Application.initialize()} catch(e) { alert('Error when initializing application! \n' + e); } });
Application = {
initialize : function(options) {
this.options = options;
this.window = document.viewport.getDimensions();
this.TreeView.initialize();
// if (DEBUG) { setTimeout( 'TreeViewTest.run()', 1000 ) }
},
TreeView : {
initialize : function() {
// console.profile()
this.container = $('treeview');
this.json = null;
this.__load_folders();
this.__add_folders_overlay_element();
// console.profileEnd()
},
__load_folders : function() {
new Ajax.Request('/folders.json', {
method : 'get',
parameters : 'authenticity_token='+authenticity_token,
onSuccess : function(transport) {
Application.TreeView.json = transport.responseJSON;
Application.TreeView.__load_dom_elements(); }
})
},
__load_dom_elements : function() {
this.container.select('li').each( function(element) { new Application.TreeView.Node(element, null) } );
},
__add_folders_overlay_element : function() {
if (!this.container) return false
this.overlay = new Element('id',
{ 'id':'folders_overlay',
'class':'rounded_10',
'style': 'width:'+$('content').getWidth()+'px; height:'+($('left').getHeight()+40)+'px;' }).setOpacity(0.6).hide()
this.container.insert({'after':this.overlay})
this.container.observe(
'mouseenter', function(event) { Application.TreeView.overlay.setStyle({'display':'block'})}
)
this.overlay.observe(
'mouseleave', function(event) { Application.TreeView.overlay.hide()}
)
},
Node: Class.create({
initialize : function(element, attributes) {
if (element) {
var element = this.element = element;
var id = this.id = this.__get_folder_id();
} else {
var element = this.element = this.__create_element(attributes);
var id = this.id = attributes.id;
}
this.expanded = false
this.url = Application.TreeView.json.select( function(n) { return n.id == id } ).first().url
this.children = Application.TreeView.json.select( function(n) { return n.parent_id == id } )
this.tree = new Element('ul', { 'class' : 'tree' })
this.tree.hide()
this.element.insert(this.tree)
this.__add_handlers(this)
if (this.children.size() > 0) { this.element.addClassName('has_children') }
if (document.location.href == this.url) { this.element.addClassName('active') }
return this.element
},
__create_element : function(attributes) {
return new Element('li', { 'id':'treeview_folder_'+attributes.id }).update('<a href="'+attributes.href+'">' + attributes.name + '</a>');
},
__get_folder_id : function() {
return parseInt( this.element.identify().gsub(/treeview_folder_/, '') );
},
__add_handlers : function(instance) {
instance.element.observe(
'mouseenter',
function(event) {
instance.expanded = true
instance.__show_children()
}).observe(
'mouseleave',
function(event) {
instance.expanded = false
window.setTimeout( function() { if (!instance.expanded) instance.__hide_children() }, 500 );
}).observe(
'mouseover',
function(event) {
}
)
},
__show_children : function() {
// console.info('Showing children', this.children)
this.children.each( function(node) {
if (!$('treeview_folder_'+node.id)) {
this.tree.insert( new Application.TreeView.Node( null, {'id':node.id, 'name':node.name, 'href':node.url} ).element ) }
this.tree.show()
}.bind(this) )
},
__hide_children : function() {
Effect.Fade(this.tree, {duration:0.2})
}
})
}
}