2014-02-12 10:23:40 +00:00
|
|
|
define [
|
|
|
|
"libs/mustache"
|
|
|
|
], () ->
|
|
|
|
class TabManager
|
|
|
|
templates:
|
|
|
|
tab: $("#tabTemplate").html()
|
|
|
|
content: $("#tabContentTemplate").html()
|
|
|
|
|
|
|
|
constructor: () ->
|
|
|
|
@locked_open = false
|
|
|
|
@locked_closed = false
|
|
|
|
@state = "closed"
|
|
|
|
$("#toolbar").on "mouseenter", () => @onMouseOver()
|
|
|
|
$("#toolbar").on "mouseleave", (e) => @onMouseOut(e)
|
2014-03-20 16:45:39 +00:00
|
|
|
@tabs = []
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
addTab: (options) ->
|
2014-03-20 16:45:39 +00:00
|
|
|
@tabs.push options
|
|
|
|
|
2014-03-21 11:34:16 +00:00
|
|
|
options.show ||= options.id
|
2014-02-12 10:23:40 +00:00
|
|
|
tabEl = $(Mustache.to_html @templates.tab, options)
|
2014-03-21 11:34:16 +00:00
|
|
|
tabEl.find("a").attr("href", "#" + options.show)
|
2014-03-20 16:45:39 +00:00
|
|
|
|
|
|
|
if options.content?
|
|
|
|
contentEl = $(Mustache.to_html @templates.content, options)
|
|
|
|
contentEl.append(options.content)
|
|
|
|
$("#tab-content").append(contentEl)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
if options.active
|
|
|
|
tabEl.addClass("active")
|
2014-03-20 16:45:39 +00:00
|
|
|
contentEl?.addClass("active")
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
if options.after?
|
|
|
|
tabEl.insertAfter($("##{options.after}-tab-li"))
|
|
|
|
else
|
|
|
|
$("#tabs").append(tabEl)
|
2014-03-20 16:45:39 +00:00
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
$("body").scrollTop(0)
|
|
|
|
tabEl.on "shown", () =>
|
|
|
|
$("body").scrollTop(0)
|
2014-03-20 16:45:39 +00:00
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
options.onShown() if options.onShown?
|
2014-03-20 16:45:39 +00:00
|
|
|
for other_tab in @tabs
|
|
|
|
if other_tab.id != options.id and other_tab.active and other_tab.onHidden?
|
|
|
|
other_tab.onHidden()
|
|
|
|
other_tab.active = false
|
|
|
|
options.active = true
|
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
if options.lock
|
|
|
|
@lockOpen()
|
|
|
|
else
|
|
|
|
@unlockOpen()
|
|
|
|
|
|
|
|
if options.contract
|
|
|
|
@contract()
|
|
|
|
|
2014-03-21 11:34:16 +00:00
|
|
|
show: (tab) ->
|
|
|
|
$("##{tab}-tab-li > a").tab("show")
|
2014-03-20 16:45:39 +00:00
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
lockOpen: () ->
|
|
|
|
@locked_open = true
|
|
|
|
$("#toolbar").css({
|
|
|
|
width: 180
|
|
|
|
})
|
|
|
|
|
|
|
|
unlockOpen: () ->
|
|
|
|
@locked_open = false
|
|
|
|
|
|
|
|
contract: () ->
|
|
|
|
$("#toolbar").css({
|
|
|
|
width: 40
|
|
|
|
})
|
|
|
|
# cooldown so we don't immediately reopen
|
|
|
|
original_locked_closed = @locked_closed
|
|
|
|
@locked_closed = true
|
|
|
|
setTimeout () =>
|
|
|
|
@locked_closed = original_locked_closed
|
|
|
|
, 200
|
|
|
|
|
|
|
|
onMouseOver: () ->
|
|
|
|
if !@locked_closed and @state == "closed"
|
|
|
|
@openMenu()
|
|
|
|
|
|
|
|
onMouseOut: (e) ->
|
|
|
|
@cancelOpen()
|
|
|
|
if !@locked_open and @state == "open"
|
|
|
|
@closeMenu()
|
|
|
|
|
|
|
|
cancelOpen: () ->
|
|
|
|
if @openTimeout
|
|
|
|
clearTimeout @openTimeout
|
|
|
|
@state = "closed"
|
|
|
|
|
|
|
|
openMenu: () ->
|
|
|
|
@openTimeout = setTimeout () =>
|
|
|
|
@state = "open"
|
|
|
|
$("#toolbar").animate({
|
|
|
|
width: 180
|
|
|
|
}, "fast")
|
|
|
|
delete @openTimeout
|
|
|
|
, 500
|
|
|
|
|
|
|
|
closeMenu: () ->
|
|
|
|
@state = "closed"
|
|
|
|
$("#toolbar").animate({
|
|
|
|
width: 40
|
|
|
|
}, "fast")
|
|
|
|
|
|
|
|
|