-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabmanager.coffee
71 lines (58 loc) · 1.89 KB
/
tabmanager.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
defaultTabUrls = ['/', '/orders', '/directory', '/suppliers']
if Meteor.isClient
Template.registerHelper "isDefaultTab", (url) -> url in defaultTabUrls
last_visited_tabs = ReactiveVar []
visitTab = (url) ->
tabs = last_visited_tabs.get()
if url in tabs
tabs.remove(url)
tabs.push url
last_visited_tabs.set tabs
class TabManager
constructor: (@collection, @icons) ->
@registeredTemplates = []
registerTemplate: (name) ->
if name and name not in @registeredTemplates
@registeredTemplates.push name
collection = @collection
getKey = ->
if FlowRouter
url = FlowRouter.current().path
else
url = Iron.Location.get().path
key = "#{Meteor.userId()}:#{url}"
# Register hook which will save state into database each time it will change.
# Hook is provided by `nous:state` package.
Template[name].onStateUpdated ->
key = getKey()
collection.update key, $set: state: @state.toJSON()
# Register hook which will restore saved state from database.
Template[name].onStateRequested ->
key = getKey()
collection.findOne(key)?.state
createTab: (template_name, title) ->
if FlowRouter
url = FlowRouter.current().path
else
url = Iron.Location.get().path
Session.set 'currentTab', url
visitTab(url)
key = "#{Meteor.userId()}:#{url}"
if @collection.find(key).count() is 0
@collection.insert
_id: key
url: url
title: title
route: template_name
@registerTemplate template_name
removeTab: (id) ->
visited_tabs = last_visited_tabs.get()
tab = @collection.findOne(id)
if tab
@collection.remove id
visited_tabs.remove tab.url
last_visited_tabs.set visited_tabs
lastVisitedTab: ->
tabs = last_visited_tabs.get()
tabs[tabs.length - 1]
getIcon: (route) -> @icons[route] ? 'fa-file'