This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
layout.coffee
243 lines (193 loc) · 7.22 KB
/
layout.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict'
_ = require 'underscore'
Backbone = require 'backbone'
View = require './view'
EventBroker = require '../lib/event_broker'
utils = require '../lib/utils'
mediator = require '../mediator'
# Shortcut to access the DOM manipulation library.
{$} = Backbone
module.exports = class Layout extends View
# Bind to document body by default.
el: 'body'
# Override default view behavior, we don’t want document.body to be removed.
keepElement: true
# The site title used in the document title.
# This should be set in your app-specific Application class
# and passed as an option.
title: ''
# Regions
# -------
# Collection of registered regions; all view regions are collected here.
globalRegions: null
listen:
'beforeControllerDispose mediator': 'scroll'
constructor: (options = {}) ->
@globalRegions = []
@title = options.title
@regions = options.regions if options.regions
@settings = _.defaults options,
titleTemplate: (data) ->
st = if data.subtitle then "#{data.subtitle} \u2013 " else ''
st + data.title
openExternalToBlank: false
routeLinks: 'a, .go-to'
skipRouting: '.noscript'
# Per default, jump to the top of the page.
scrollTo: [0, 0]
mediator.setHandler 'region:show', @showRegion, this
mediator.setHandler 'region:register', @registerRegionHandler, this
mediator.setHandler 'region:unregister', @unregisterRegionHandler, this
mediator.setHandler 'region:find', @regionByName, this
mediator.setHandler 'adjustTitle', @adjustTitle, this
super
# Set the app link routing.
@startLinkRouting() if @settings.routeLinks
# Controller startup and disposal
# -------------------------------
# Handler for the global beforeControllerDispose event.
scroll: ->
# Reset the scroll position.
to = @settings.scrollTo
if to and typeof to is 'object'
[x, y] = to
window.scrollTo x, y
# Handler for the global dispatcher:dispatch event.
# Change the document title to match the new controller.
# Get the title from the title property of the current controller.
adjustTitle: (subtitle = '') ->
title = @settings.titleTemplate {@title, subtitle}
document.title = title
@publishEvent 'adjustTitle', subtitle, title
title
# Automatic routing of internal links
# -----------------------------------
startLinkRouting: ->
route = @settings.routeLinks
@delegate 'click', route, @openLink if route
stopLinkRouting: ->
route = @settings.routeLinks
@undelegate 'click', route if route
isExternalLink: (link) ->
return false unless utils.matchesSelector link, 'a, area'
return true if link.hasAttribute 'download'
# IE 9-11 resolve href but do not populate protocol, host etc.
# Reassigning href helps. See #878 issue for details.
link.href += '' unless link.host
{target} = link
target is '_blank' or
link.rel is 'external' or
link.origin isnt location.origin or
(target is '_parent' and parent isnt self) or
(target is '_top' and top isnt self)
# Handle all clicks on A elements and try to route them internally.
openLink: (event) =>
return if utils.modifierKeyPressed event
el = if $ then event.currentTarget else event.delegateTarget
# Get the href and perform checks on it.
href = el.getAttribute('href') or el.getAttribute('data-href')
# Basic href checks.
# Technically an empty string is a valid relative URL
# but it doesn’t make sense to route it.
return if not href or
# Exclude fragment links.
href[0] is '#'
# Apply skipRouting option.
{skipRouting} = @settings
switch typeof skipRouting
when 'function'
return unless skipRouting href, el
when 'string'
return if utils.matchesSelector el, skipRouting
# Handle external links.
if @isExternalLink el
if @settings.openExternalToBlank
# Open external links normally in a new tab.
event.preventDefault()
@openWindow href
return
# Pass to the router, try to route the path internally.
utils.redirectTo url: href
# Prevent default handling if the URL could be routed.
event.preventDefault()
# Handle all browsing context resources
openWindow: (href) ->
window.open href
# Region management
# -----------------
# Handler for `!region:register`.
# Register a single view region or all regions exposed.
registerRegionHandler: (instance, name, selector) ->
if name?
@registerGlobalRegion instance, name, selector
else
@registerGlobalRegions instance
# Registering one region bound to a view.
registerGlobalRegion: (instance, name, selector) ->
# Remove the region if there was already one registered perhaps by
# a base class.
@unregisterGlobalRegion instance, name
# Place this region registration into the regions array.
@globalRegions.unshift {instance, name, selector}
# Triggered by view; passed in the regions hash.
# Simply register all regions exposed by it.
registerGlobalRegions: (instance) ->
# Regions can be be extended by subclasses, so we need to check the
# whole prototype chain for matching regions. Regions registered by the
# more-derived class overwrites the region registered by the less-derived
# class.
for version in utils.getAllPropertyVersions instance, 'regions'
for name, selector of version
@registerGlobalRegion instance, name, selector
# Return nothing.
return
# Handler for `!region:unregister`.
# Unregisters single named region or all view regions.
unregisterRegionHandler: (instance, name) ->
if name?
@unregisterGlobalRegion instance, name
else
@unregisterGlobalRegions instance
# Unregisters a specific named region from a view.
unregisterGlobalRegion: (instance, name) ->
cid = instance.cid
@globalRegions = (region for region in @globalRegions when (
region.instance.cid isnt cid or region.name isnt name
))
# When views are disposed; remove all their registered regions.
unregisterGlobalRegions: (instance) ->
@globalRegions = (region for region in @globalRegions when (
region.instance.cid isnt instance.cid
))
# Returns the region by its name, if found.
regionByName: (name) ->
for reg in @globalRegions when reg.name is name and not reg.instance.stale
return reg
# When views are instantiated and request for a region assignment;
# attempt to fulfill it.
showRegion: (name, instance) ->
# Find an appropriate region.
region = @regionByName name
# Assert that we got a valid region.
throw new Error "No region registered under #{name}" unless region
# Apply the region selector.
instance.container = if region.selector is ''
if $
region.instance.$el
else
region.instance.el
else
if region.instance.noWrap
region.instance.container.find region.selector
else
region.instance.find region.selector
# Disposal
# --------
dispose: ->
return if @disposed
# Stop routing links.
@stopLinkRouting()
# Remove all regions and document title setting.
delete this[prop] for prop in ['globalRegions', 'title', 'route']
mediator.removeHandlers this
super