Controller 		= window.require 'spine/controller'
Activator 		= window.require 'spine/controller/activator'
Activateable 	= window.require 'spine/controller/activateable'
Popup			= require '../../editor/popup'

$body = $ 'body'

class NavItem extends Controller
	className : 'browser-nav-item'
	@include Activateable

	constructor:(options)->
		super
		if @item.root then @el.attr('title',@item.navTitle).addClass 'root' else @el.html(@item.name)
		@$bind 'click', =>
			return if @isActive()
			@parent().trigger 'select', @item
			@active().el.nextAll().each (i,el)->$(el).controller().release()

class NavBar extends Activator
	className : 'browser-nav'
	constructor: ->
		super

		return unless @disconnectBtn

		@popup = new Popup
			title: 'Sure about disconnecting?'
			confirmBtn: 'Yes, Disconnect'
			cancelBtn: 'Cancel'
			confirmBtnClass: 'green-button button--uc no-icons'
			cancelBtnClass: 'dim-button button--uc no-icons'
		@append @logout=$("<div class='browser-logout' title='Disconnect from server' data-icon='↯'></div>")

		@logout.bind 'click', (e) =>
			do e.stopPropagation
			@popup.pop @el.closest('body'), =>
				(@$trigger 'logout').trigger 'logout'
				@children (i) -> i.release()

	addItem: (item) ->
		@children (child) ->
			unless child.item.root or ///^#{child.item.path.replace /\//ig, '\/'}(\/|$)///.test item.path
				child.release()
		@append child = new NavItem {item}
		child.active()

module.exports = NavBar
