Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Run user select Js only where needed
Transform current implementation into regular Coffescript classes
so that the same call method can be reused on the dispatcher
as for other classes.
  • Loading branch information
cirosantilli committed Oct 22, 2014
1 parent c3b81e5 commit c507e90
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
10 changes: 9 additions & 1 deletion app/assets/javascripts/dispatcher.js.coffee
Expand Up @@ -67,6 +67,7 @@ class Dispatcher
new TeamMembers()
when 'groups:members'
new GroupMembers()
new UsersSelect()
when 'projects:tree:show'
new TreeView()
shortcut_handler = new ShortcutsNavigation()
Expand All @@ -81,11 +82,17 @@ class Dispatcher
shortcut_handler = true

switch path.first()
when 'admin' then new Admin()
when 'admin'
new Admin()
switch path[1]
when 'groups'
new UsersSelect()
when 'dashboard'
shortcut_handler = new ShortcutsDashboardNavigation()
when 'projects'
switch path[1]
when 'issues', 'merge_requests'
new ProjectUsersSelect()
when 'wikis'
new Wikis()
shortcut_handler = new ShortcutsNavigation()
Expand All @@ -94,6 +101,7 @@ class Dispatcher
shortcut_handler = new ShortcutsNavigation()
when 'team_members', 'deploy_keys', 'hooks', 'services', 'protected_branches'
shortcut_handler = new ShortcutsNavigation()
new UsersSelect()


# If we haven't installed a custom shortcut handler, install the default one
Expand Down
19 changes: 9 additions & 10 deletions app/assets/javascripts/project_users_select.js.coffee
@@ -1,6 +1,6 @@
@projectUsersSelect =
init: ->
$('.ajax-project-users-select').each (i, select) ->
class @ProjectUsersSelect
constructor: ->
$('.ajax-project-users-select').each (i, select) =>
project_id = $(select).data('project-id') || $('body').data('project-id')

$(select).select2
Expand Down Expand Up @@ -28,14 +28,16 @@
Api.user(id, callback)


formatResult: projectUsersSelect.projectUserFormatResult
formatSelection: projectUsersSelect.projectUserFormatSelection
formatResult: (args...) =>
@formatResult(args...)
formatSelection: (args...) =>
@formatSelection(args...)
dropdownCssClass: "ajax-project-users-dropdown"
dropdownAutoWidth: true
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
m

projectUserFormatResult: (user) ->
formatResult: (user) ->
if user.avatar_url
avatar = user.avatar_url
else
Expand All @@ -52,8 +54,5 @@
<div class='user-username'>#{user.username}</div>
</div>"

projectUserFormatSelection: (user) ->
formatSelection: (user) ->
user.name

$ ->
projectUsersSelect.init()
53 changes: 28 additions & 25 deletions app/assets/javascripts/users_select.js.coffee
@@ -1,5 +1,30 @@
$ ->
userFormatResult = (user) ->
class @UsersSelect
constructor: ->
$('.ajax-users-select').each (i, select) =>
$(select).select2
placeholder: "Search for a user"
multiple: $(select).hasClass('multiselect')
minimumInputLength: 0
query: (query) ->
Api.users query.term, (users) ->
data = { results: users }
query.callback(data)

initSelection: (element, callback) ->
id = $(element).val()
if id isnt ""
Api.user(id, callback)


formatResult: (args...) =>
@formatResult(args...)
formatSelection: (args...) =>
@formatSelection(args...)
dropdownCssClass: "ajax-users-dropdown"
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
m

formatResult: (user) ->
if user.avatar_url
avatar = user.avatar_url
else
Expand All @@ -11,27 +36,5 @@ $ ->
<div class='user-username'>#{user.username}</div>
</div>"

userFormatSelection = (user) ->
formatSelection: (user) ->
user.name

$('.ajax-users-select').each (i, select) ->
$(select).select2
placeholder: "Search for a user"
multiple: $(select).hasClass('multiselect')
minimumInputLength: 0
query: (query) ->
Api.users query.term, (users) ->
data = { results: users }
query.callback(data)

initSelection: (element, callback) ->
id = $(element).val()
if id isnt ""
Api.user(id, callback)


formatResult: userFormatResult
formatSelection: userFormatSelection
dropdownCssClass: "ajax-users-dropdown"
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
m

0 comments on commit c507e90

Please sign in to comment.