overleaf/services/web/public/coffee/ide/TabManager.coffee

110 lines
2.3 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05: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)
@tabs = []
2014-02-12 05:23:40 -05:00
addTab: (options) ->
@tabs.push options
options.show ||= options.id
2014-02-12 05:23:40 -05:00
tabEl = $(Mustache.to_html @templates.tab, options)
tabEl.find("a").attr("href", "#" + options.show)
if options.content?
contentEl = $(Mustache.to_html @templates.content, options)
contentEl.append(options.content)
$("#tab-content").append(contentEl)
2014-02-12 05:23:40 -05:00
if options.active
tabEl.addClass("active")
contentEl?.addClass("active")
2014-02-12 05:23:40 -05:00
if options.after?
tabEl.insertAfter($("##{options.after}-tab-li"))
else
$("#tabs").append(tabEl)
2014-02-12 05:23:40 -05:00
$("body").scrollTop(0)
tabEl.on "shown", () =>
$("body").scrollTop(0)
2014-02-12 05:23:40 -05:00
options.onShown() if options.onShown?
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 05:23:40 -05:00
if options.lock
@lockOpen()
else
@unlockOpen()
if options.contract
@contract()
show: (tab) ->
$("##{tab}-tab-li > a").tab("show")
2014-02-12 05:23:40 -05: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")