diff --git a/web/app/assets/javascripts/createSession.js b/web/app/assets/javascripts/createSession.js index 1345b3b7d..29e52754a 100644 --- a/web/app/assets/javascripts/createSession.js +++ b/web/app/assets/javascripts/createSession.js @@ -7,81 +7,19 @@ var logger = context.JK.logger; var rest = context.JK.Rest(); var realtimeMessaging = context.JK.JamServer; - var friendSelectorDialog = null; var invitationDialog = null; - var autoComplete = null; - var userNames = []; - var userIds = []; - var userPhotoUrls = []; + var inviteMusiciansDialog = null; var MAX_GENRES = 1; - var selectedFriendIds = {}; var sessionSettings = {}; function beforeShow(data) { - userNames = []; - userIds = []; - userPhotoUrls = []; + inviteMusiciansDialog.afterShow(data); context.JK.GenreSelectorHelper.render('#create-session-genre'); resetForm(); } function afterShow(data) { - friendSelectorDialog.setCallback(friendSelectorCallback); - - var friends = rest.getFriends({ id: context.JK.currentUserId }) - .done(function(friends) { - $.each(friends, function() { - userNames.push(this.name); - userIds.push(this.id); - userPhotoUrls.push(this.photo_url); - }); - - var autoCompleteOptions = { - lookup: { suggestions: userNames, data: userIds }, - onSelect: addInvitation - }; - - $('#friend-input').attr("placeholder", "Type a friend\'s name").prop('disabled', false); - - if (!autoComplete) { - autoComplete = $('#friend-input').autocomplete(autoCompleteOptions); - } - else { - autoComplete.setOptions(autoCompleteOptions); - } - - $(".autocomplete").width("150px"); - }) - .fail(function() { - $('#friend-input').attr("placeholder", "Unable to lookup friends"); - app.ajaxError(arguments); - }); - } - - function friendSelectorCallback(newSelections) { - var keys = Object.keys(newSelections); - for (var i=0; i < keys.length; i++) { - addInvitation(newSelections[keys[i]].userName, newSelections[keys[i]].userId); - } - } - - function addInvitation(value, data) { - if ($('#selected-friends div[user-id=' + data + ']').length === 0) { - var template = $('#template-added-invitation').html(); - var invitationHtml = context.JK.fillTemplate(template, {userId: data, userName: value}); - $('#selected-friends').append(invitationHtml); - $('#friend-input').select(); - selectedFriendIds[data] = true; - } - else { - $('#friend-input').select(); - context.alert('Invitation already exists for this musician.'); - } - } - - function removeInvitation(evt) { - delete selectedFriendIds[$(evt.currentTarget).parent().attr('user-id')]; - $(evt.currentTarget).closest('.invitation').remove(); + inviteMusiciansDialog.afterShow(data); } function resetForm() { @@ -248,49 +186,12 @@ return false; } - function createInvitations(sessionId, onComplete) { - var callCount = 0; - var totalInvitations = 0; - $('#selected-friends .invitation').each(function(index, invitation) { - callCount++; - totalInvitations++; - var invite_id = $(invitation).attr('user-id'); - var invite = { - music_session: sessionId, - receiver: invite_id - }; - $.ajax({ - type: "POST", - url: "/api/invitations", - data: invite - }).done(function(response) { - callCount--; - }).fail(app.ajaxError); - }); - // TODO - this is the second time I've used this pattern. - // refactor to make a common utility for this. - function checker() { - if (callCount === 0) { - onComplete(); - } else { - context.setTimeout(checker, 10); - } - } - checker(); - return totalInvitations; - } - function events() { $('#create-session-form').on('submit', submitForm); $('#btn-create-session').on("click", submitForm); - $('#selected-friends').on("click", ".invitation a", removeInvitation); $('#musician-access').change(toggleMusicianAccess); $('#fan-access').change(toggleFanAccess); - $('#btn-choose-friends').click(function() { - friendSelectorDialog.showDialog(selectedFriendIds); - }); - $('div[layout-id="createSession"] .btn-email-invitation').click(function() { invitationDialog.showEmailDialog(); }); @@ -373,39 +274,10 @@ }); } - function searchFriends(query) { - if (query.length < 2) { - $('#friend-search-results').empty(); - return; - } - var url = "/api/search?query=" + query + "&userId=" + context.JK.currentUserId; - $.ajax({ - type: "GET", - url: url, - success: friendSearchComplete - }); - } - - function friendSearchComplete(response) { - // reset search results each time - $('#friend-search-results').empty(); - - // loop through each - $.each(response.friends, function() { - // only show friends who are musicians - if (this.musician === true) { - var template = $('#template-friend-search-results').html(); - var searchResultHtml = context.JK.fillTemplate(template, {userId: this.id, name: this.first_name + ' ' + this.last_name}); - $('#friend-search-results').append(searchResultHtml); - $('#friend-search-results').attr('style', 'display:block'); - } - }); - } - - function initialize(invitationDialogInstance, friendSelectorDialogInstance) { - friendSelectorDialog = friendSelectorDialogInstance; + function initialize(invitationDialogInstance, inviteMusiciansDialogInstance) { invitationDialog = invitationDialogInstance; - $('#create-session-invite-musicians').append(context.JK.fillTemplate($('#template-invite-session-musicians').html(), {})); + inviteMusiciansDialog = inviteMusiciansDialogInstance; + $('#create-session-invite-musicians').append(inviteMusiciansDialog.friendSelectorHTML); events(); loadBands(); loadSessionSettings(); @@ -420,8 +292,6 @@ this.submitForm = submitForm; this.validateForm = validateForm; this.loadBands = loadBands; - this.searchFriends = searchFriends; - this.addInvitation = addInvitation; return this; }; diff --git a/web/app/assets/javascripts/inviteMusicians.js b/web/app/assets/javascripts/inviteMusicians.js new file mode 100644 index 000000000..bb3d45b2e --- /dev/null +++ b/web/app/assets/javascripts/inviteMusicians.js @@ -0,0 +1,173 @@ +(function(context,$) { + + "use strict"; + + context.JK = context.JK || {}; + context.JK.InviteMusiciansDialog = function(app) { + var logger = context.JK.logger; + var userNames = []; + var userIds = []; + var userPhotoUrls = []; + var friendSelectorDialog = null; + var friendSelectorHTML = null; + var selectedFriendIds = {}; + var autoComplete = null; + var rest = context.JK.Rest(); + var selectedIds = {}; + var newSelections = {}; + var mySaveCallback; + + this. beforeShow = function(data) { + userNames = []; + userIds = []; + userPhotoUrls = []; + } + + this.afterShow = function(data) { + friendSelectorDialog.setCallback(friendSelectorCallback); + + var friends = rest.getFriends({ id: context.JK.currentUserId }) + .done(function(friends) { + $.each(friends, function() { + userNames.push(this.name); + userIds.push(this.id); + userPhotoUrls.push(this.photo_url); + }); + + var autoCompleteOptions = { + lookup: { suggestions: userNames, data: userIds }, + onSelect: addInvitation + }; + + $('#friend-input').attr("placeholder", "Type a friend\'s name").prop('disabled', false); + + if (!autoComplete) { + autoComplete = $('#friend-input').autocomplete(autoCompleteOptions); + } + else { + autoComplete.setOptions(autoCompleteOptions); + } + + $(".autocomplete").width("150px"); + }) + .fail(function() { + $('#friend-input').attr("placeholder", "Unable to lookup friends"); + app.ajaxError(arguments); + }); + } + + function friendSelectorCallback(newSelections) { + var keys = Object.keys(newSelections); + for (var i=0; i < keys.length; i++) { + addInvitation(newSelections[keys[i]].userName, newSelections[keys[i]].userId); + } + } + + function addInvitation(value, data) { + if ($('#selected-friends div[user-id=' + data + ']').length === 0) { + var template = $('#template-added-invitation').html(); + var invitationHtml = context.JK.fillTemplate(template, {userId: data, userName: value}); + $('#selected-friends').append(invitationHtml); + $('#friend-input').select(); + selectedFriendIds[data] = true; + } + else { + $('#friend-input').select(); + context.alert('Invitation already exists for this musician.'); + } + } + + function removeInvitation(evt) { + delete selectedFriendIds[$(evt.currentTarget).parent().attr('user-id')]; + $(evt.currentTarget).closest('.invitation').remove(); + } + + function createInvitations(sessionId, onComplete) { + var callCount = 0; + var totalInvitations = 0; + $('#selected-friends .invitation').each(function(index, invitation) { + callCount++; + totalInvitations++; + var invite_id = $(invitation).attr('user-id'); + var invite = { + music_session: sessionId, + receiver: invite_id + }; + $.ajax({ + type: "POST", + url: "/api/invitations", + data: invite + }).done(function(response) { + callCount--; + }).fail(app.ajaxError); + }); + // TODO - this is the second time I've used this pattern. + // refactor to make a common utility for this. + function checker() { + if (callCount === 0) { + onComplete(); + } else { + context.setTimeout(checker, 10); + } + } + checker(); + return totalInvitations; + } + + function searchFriends(query) { + if (query.length < 2) { + $('#friend-search-results').empty(); + return; + } + var url = "/api/search?query=" + query + "&userId=" + context.JK.currentUserId; + $.ajax({ + type: "GET", + url: url, + success: friendSearchComplete + }); + } + + function friendSearchComplete(response) { + // reset search results each time + $('#friend-search-results').empty(); + + // loop through each + $.each(response.friends, function() { + // only show friends who are musicians + if (this.musician === true) { + var template = $('#template-friend-search-results').html(); + var searchResultHtml = context.JK.fillTemplate(template, {userId: this.id, name: this.first_name + ' ' + this.last_name}); + $('#friend-search-results').append(searchResultHtml); + $('#friend-search-results').attr('style', 'display:block'); + } + }); + } + + function events() { + $('#btn-choose-friends').click(function() { + friendSelectorDialog.showDialog(selectedFriendIds); + }); + $('#selected-friends').on("click", ".invitation a", removeInvitation); + } + + function showDialog(ids) { + } + + this.initialize = function(friendSelectorDialogInstance) { + friendSelectorDialog = friendSelectorDialogInstance; + friendSelectorHTML = context.JK.fillTemplate($('#template-session-invite-musicians').html()); + events(); + }; + + this.setCallback = function(callback) { + mySaveCallback = callback; + } + + this.searchFriends = searchFriends; + this.addInvitation = addInvitation; + + this.showDialog = showDialog; + return this; + }; + +})(window,jQuery); \ No newline at end of file diff --git a/web/app/assets/javascripts/session.js b/web/app/assets/javascripts/session.js index 4245dbee8..311134271 100644 --- a/web/app/assets/javascripts/session.js +++ b/web/app/assets/javascripts/session.js @@ -14,6 +14,8 @@ var addNewGearDialog; var localRecordingsDialog = null; var recordingFinishedDialog = null; + var friendSelectorDialog = null; + var inviteMusicianDialog = null; var screenActive = false; var currentMixerRangeMin = null; var currentMixerRangeMax = null; diff --git a/web/app/views/clients/_inviteSessionMusicians.html.erb b/web/app/views/clients/_inviteSessionMusicians.html.erb new file mode 100644 index 000000000..c47606211 --- /dev/null +++ b/web/app/views/clients/_inviteSessionMusicians.html.erb @@ -0,0 +1,26 @@ + +
+
+
+
+
+ CANCEL  +
+
+ INVITE +
+
+ + + diff --git a/web/app/views/clients/_session.html.erb b/web/app/views/clients/_session.html.erb index 86b69b577..f4f90c341 100644 --- a/web/app/views/clients/_session.html.erb +++ b/web/app/views/clients/_session.html.erb @@ -178,15 +178,4 @@ - + diff --git a/web/app/views/clients/index.html.erb b/web/app/views/clients/index.html.erb index bbd434dbf..da5464594 100644 --- a/web/app/views/clients/index.html.erb +++ b/web/app/views/clients/index.html.erb @@ -105,6 +105,9 @@ var friendSelectorDialog = new JK.FriendSelectorDialog(JK.app); friendSelectorDialog.initialize(); + var inviteMusiciansDialog = new JK.InviteMusiciansDialog(JK.app); + inviteMusiciansDialog.initialize(friendSelectorDialog); + var userDropdown = new JK.UserDropdown(JK.app); JK.UserDropdown = userDropdown; userDropdown.initialize(invitationDialog); @@ -148,7 +151,7 @@ JK.Banner.initialize(); var createSessionScreen = new JK.CreateSessionScreen(JK.app); - createSessionScreen.initialize(invitationDialog, friendSelectorDialog); + createSessionScreen.initialize(invitationDialog, inviteMusiciansDialog); var bandSetupScreen = new JK.BandSetupScreen(JK.app); bandSetupScreen.initialize(invitationDialog, friendSelectorDialog);