Skip to content

Commit 9ae4659

Browse files
committed
Merge pull request #153 from oskarhane/url-param-api
Expose URL param API to populate editor
2 parents f8af943 + ae601c9 commit 9ae4659

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

app/scripts/controllers/Main.coffee

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ angular.module('neo4jApp.controllers')
178178

179179
.run([
180180
'$rootScope'
181-
($scope) ->
181+
'Utils'
182+
'Settings'
183+
'Editor'
184+
($scope, Utils, Settings, Editor) ->
182185
$scope.unauthorized = yes
186+
187+
if cmdParam = Utils.getUrlParam('cmd', window.location.href)
188+
return unless cmdParam[0] is 'play'
189+
cmdCommand = "#{Settings.cmdchar}#{cmdParam[0]} "
190+
cmdArgs = Utils.getUrlParam('arg', decodeURIComponent(window.location.href)) || []
191+
Editor.setContent(cmdCommand + cmdArgs.join(' '))
183192
])

lib/helpers.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,11 @@ class neo.helpers
168168
flat
169169
, [])
170170

171+
@getUrlParam = (name, theLocation) ->
172+
return no unless theLocation
173+
out = []
174+
re = new RegExp('[\\?&]' + name + '=([^&#]*)', 'g')
175+
while (results = re.exec(theLocation)) isnt null
176+
out.push(results[1]) if results and results[1]
177+
return undefined if not out.length
178+
out

test/spec/other/utils.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,24 @@ describe 'Utils', () ->
151151
it 'should flatten nested arrays', ->
152152
t1 = [1, [2], [[3], 'hello', {k: 1}]]
153153
expect(JSON.stringify(Utils.flattenArray(t1))).toBe(JSON.stringify([1, 2, 3, 'hello', {k: 1}]))
154+
155+
it 'should read URL params correctly', ->
156+
urls = [
157+
{location: 'http://neo4j.com/?param=1', paramName: 'param', expect: '1'},
158+
{location: 'http://neo4j.com/?param2=2&param=1', paramName: 'param', expect: '1'},
159+
{location: 'http://neo4j.com/?param=', paramName: 'param', expect: undefined},
160+
{location: 'http://neo4j.com/', paramName: 'param', expect: undefined}
161+
]
162+
urls.forEach((tCase) ->
163+
res = Utils.getUrlParam tCase.paramName, tCase.location
164+
val = if res then res[0] else res
165+
expect(val).toBe(tCase.expect)
166+
)
167+
168+
it 'should read URL array params correctly', ->
169+
single = 'http://neo4j.com/?cmd=play&arg=cypher'
170+
expect(Utils.getUrlParam('arg', single)[0]).toBe('cypher')
171+
172+
multi = 'http://neo4j.com/?cmd=play&arg=cypher&arg=hello'
173+
expect(Utils.getUrlParam('arg', multi)[0]).toBe('cypher')
174+
expect(Utils.getUrlParam('arg', multi)[1]).toBe('hello')

0 commit comments

Comments
 (0)