-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathember-query.coffee
278 lines (210 loc) · 7.88 KB
/
ember-query.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Em.QueryLocation = Em.HistoryLocation.extend
initState: ->
location = @get('location')
state = location.pathname + location.search
@set('history', window.history)
@replaceState state
queryString: (url) ->
url.split("?")[1] or ""
queryHash: ->
url = @newURL || @get('location.search')
$.deparam @queryString(url)
willChangeURL: (url) ->
@set 'fullURL', url
@newURL = url
alreadyHasParams: (params) ->
@toQueryString(@queryHash()) is @toQueryString(params)
toQueryString: (params) ->
$.param(params)
.replace(/%5B/g, "[")
.replace(/%5D/g, "]")
.replace(/%2C/g, ",")
getURL: ->
@_super() + window.location.search
setURL: (url) ->
@_super(url)
@set 'fullURL', url
@newURL = undefined
replaceURL: (url) ->
@_super(url)
@set 'fullURL', url
@newURL = undefined
replaceQueryParams: (params) ->
@doUpdateQueryParams params, @replaceURL.bind(@)
setQueryParams: (params) ->
@doUpdateQueryParams params, @setURL.bind(@)
doUpdateQueryParams: (params, callback) ->
newPath = @get('location.pathname')
query = @toQueryString(params)
newPath += "?#{query}" unless Em.isEmpty(query)
callback newPath
Em.Location.registerImplementation('query', Em.QueryLocation)
Em.ControllerMixin.reopen
transitionParams: (newParams) ->
@get('target').transitionParams(newParams)
transitionToRouteWithParams: (args...) ->
@get('target').transitionToRouteWithParams args...
transitionAllParams: (args...) ->
@get('target').transitionAllParams args...
replaceQueryParams: (args...) ->
@get('target').replaceQueryParams args...
currentParams: ->
@container.lookup('router:main').paramsFromRoutes()
init: (args...) ->
@_super(args...)
if @observeParams?
for param in @observeParams
@addObserver param, =>
Em.run.once =>
@container.lookup('router:main').serializeParams()
Em.Router.reopen
# Hijacking updateUrl is used because redefining
# doTransition is not possible as it's a function in
# a closure and not available to any accessible
# object. This in fact applies to all of the overrides in
# the startRouting method below, which just exist to
# override some functions after they have been defined
init: (args...) ->
Ember.assert("You are using a version of ember that is too old for ember-query to work with.", @_setupRouter)
@_super(args...)
hijackUpdateUrlParams: null
_setupRouter: (router, location) ->
@_super(router, location)
defaultUpdateURL = router.updateURL
@router.updateURL = (url) =>
if @hijackUpdateUrlParams?
qs = @location.toQueryString(@hijackUpdateUrlParams)
url += "?#{qs}" unless Em.isEmpty qs
@hijackUpdateUrlParams = null
defaultUpdateURL url
@location.willChangeURL url
defaultHandleURL = @router.handleURL
@router.handleURL = (url) =>
@location.willChangeURL url
defaultHandleURL.call(@router, url).then =>
@router.currentHandlerInfos.forEach (handlerInfo) =>
handlerInfo.handler.deserializeParams(@queryParams())
defaultRecognize = @router.recognizer.recognize
@router.recognizer.recognize = (path) =>
if /\?/.test path
path = path.split("?")[0]
defaultRecognize.call @router.recognizer, path
fullURLBinding: 'location.fullURL'
didTransition: (infos) ->
@_super(infos)
Em.run.next =>
@replaceQueryParams @paramsFromRoutes()
@notifyPropertyChange('url')
currentRoute: ->
@router.currentHandlerInfos.get('lastObject').handler
queryParams: ->
@get('location').queryHash()
# Merge passed params into current params
transitionParams: (newParams) ->
params = @location.queryHash()
for own key, value of newParams
if value?
params[key] = value
else
delete params[key]
@transitionAllParams params
# replace all current params with passed params
transitionAllParams: (params) ->
return if @router.isLoading
return if @location.alreadyHasParams params
@location.setQueryParams params
if @get('namespace').LOG_TRANSITIONS
Em.Logger.log 'Transitioned query params', params
# call deserialize for all routes in current tree
@router.currentHandlerInfos.forEach (info) =>
info.handler.deserializeParams params, info.handler.defaultController()
# call setupController for bottom route
controller = @currentRoute().defaultController()
model = @currentRoute().currentModel
@currentRoute().setupController controller, model, params
@notifyPropertyChange 'url'
# update the current query params without adding
# a state to the history
replaceQueryParams: (params) ->
@location.replaceQueryParams params
transitionToRouteWithParams: (args...) ->
name = args[0]
unless @router.hasRoute(name)
name = args[0] = args[0] + '.index'
@hijackUpdateUrlParams = args.pop()
@replaceQueryParams @hijackUpdateUrlParams
Ember.assert "The route #{name} was not found",
@router.hasRoute(name)
@router.transitionTo args...
isLoaded: ->
@router.currentHandlerInfos?
serializeParams: ->
return unless @isLoaded()
@transitionAllParams @paramsFromRoutes()
queryStringFromRoutes: ->
@get('location').toQueryString @paramsFromRoutes()
paramsFromRoutes: ->
params = {}
@router.currentHandlerInfos.forEach (info) ->
handler = info.handler
newParams = handler.serializeParams handler.defaultController()
Em.merge params, newParams
params
Em.Route.reopen
deserializeParams: Em.K
serializeParams: -> {}
queryParams: ->
@get('router').queryParams()
transitionParams: (newParams) ->
@get('router').transitionParams(newParams)
transitionToRouteWithParams: (args...) ->
@get('router').transitionToRouteWithParams args...
replaceQueryParams: (params) ->
@get('router').replaceQueryParams params
defaultController: -> @controllerFor @routeName
setup: (context) ->
controllerName = @controllerName or @routeName
controller = @controllerFor(controllerName, true)
controller = @generateController(controllerName, context) unless controller
# Assign the route's controller so that it can more easily be
# referenced in event handlers
@controller = controller
if @setupControllers
Ember.deprecate "Ember.Route.setupControllers is deprecated. Please use Ember.Route.setupController(controller, model) instead."
@setupControllers controller, context, @queryParams()
else
@setupController controller, context, @queryParams()
@deserializeParams @queryParams(), controller
if @renderTemplates
Ember.deprecate "Ember.Route.renderTemplates is deprecated. Please use Ember.Route.renderTemplate(controller, model) instead."
@renderTemplates context
else
@renderTemplate controller, context
Em.LinkView.reopen
_invoke: (event) ->
return @_super(event) unless @get('query')?
return true unless Em.ViewUtils.isSimpleClick(event)
event.preventDefault()
event.stopPropagation() if @bubbles is false
return false if @get('_isDisabled')
if @get "loading"
Ember.Logger.warn "This linkTo is in an inactive loading state because at least one of its parameters' presently has a null/undefined value, or the provided route name is invalid."
return false
router = @get "router"
routeArgs = @get "routeArgs"
if @get("replace")
throw "replace not implemented with query params"
else
params = $.deparam @get('query')
route = @get 'namedRoute'
router.transitionToRouteWithParams routeArgs.concat(params)...
href: (->
return @_super() unless @get('query')?
router = @get('router')
routeArgs = @get('routeArgs')
path = if routeArgs?
router.generate.apply(router, routeArgs)
else
get(this, 'loadingHref');
"#{path}?#{@get('query')}"
).property('routeArgs')