Skip to content

Commit 444a068

Browse files
committed
Expose URL param API to populate editor
This change exposes an API though URL params making it easier to publish links to prepopulate the editor. The URL param API looks like this: `cmd` - the command to use. Could be `play` or `help` or anything we decide to support. `arg` - Zero or more of these args. Current implementation: - `cmd` - This command will be prefixed by the current users config `cmdchar` (which defaults to `:`). The special case here being `cmd=cypher`. In that case, the command is stripped and not prefixed by anything. - `arg` - They will be inserted in the order they appear in the URI and separated by a space. This does **NOT** execute the command automatically. It adds content to the editor, the user must execute the command manually. Examples: Link to external guide: `http://localhost:7474/?cmd=play&arg=http://guides.neo4j.com/reco` Link to cypher query: `http://localhost:7474/?cmd=cypher&arg=RETURN&arg=1`
1 parent f547c0d commit 444a068

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

app/scripts/controllers/Main.coffee

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ angular.module('neo4jApp.controllers')
170170

171171
.run([
172172
'$rootScope'
173-
($scope) ->
173+
'Utils'
174+
'Settings'
175+
'Editor'
176+
($scope, Utils, Settings, Editor) ->
174177
$scope.unauthorized = yes
178+
179+
if cmdParam = Utils.getUrlParam('cmd', window.location.href)
180+
if cmdParam[0] is 'cypher'
181+
cmdCommand = ''
182+
else
183+
cmdCommand = "#{Settings.cmdchar}#{cmdParam[0]} "
184+
cmdArgs = Utils.getUrlParam('arg', decodeURIComponent(window.location.href)) || []
185+
Editor.setContent(cmdCommand + cmdArgs.join(' '))
175186
])

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)