/** * Javascript for the session settings dialog. */ (function(context,$) { context.JK = context.JK || {}; context.JK.SessionSettingsDialog = function(app, sessionScreen) { var logger = context.JK.logger; var $dialog; var rest = new JK.Rest(); function beforeShow(data) { context.JK.GenreSelectorHelper.render('#session-settings-genre'); $dialog = $('[layout-id="session-settings"]'); var currentSession = sessionScreen.getCurrentSession(); context.JK.GenreSelectorHelper.setSelectedGenres('#session-settings-genre', currentSession.genres); // dynamic object binding to form. // TODO: Generalize, test and bundle with formToObject // It's the other direction... given an objects -- set the form's // inputs/values to the corresponding object properties. var skip = [ 'band_id', // not used 'user_id', // not used 'participants', // has its own API 'invitations', // has its own API 'join_requests', // has its own API 'genres' // handled specifically ]; var radioButtons = [ 'approval_required', 'fan_chat' ]; $.each(_.keys(currentSession), function(index,propName) { if (context._.contains(skip, propName)) { return true; // "continue" } var isRadio = (context._.contains(radioButtons, propName)); var desiredValue = null; if ($.isArray(currentSession[propName])) { desiredValue = currentSession[propName].join(','); } else { desiredValue = currentSession[propName]; } desiredValue = String(desiredValue); var inputSelector = '[name="' + propName + '"]'; var $input = []; // radio buttons must be handled differently if (isRadio) { inputSelector += '[value="' + desiredValue + '"]'; $(inputSelector).removeAttr('checked'); $input = $(inputSelector, $dialog); $input.prop('checked', true).change(); } else { $input = $(inputSelector, $dialog); $input.val(desiredValue).change(); } }); } function musicianAccessChanged(evt) { $dialog = $('[layout-id="session-settings"]'); var hasMusicianAccess = $('select[name="musician_access"]', $dialog).val(); // string hasMusicianAccess = context.JK.stringToBool(hasMusicianAccess); if (hasMusicianAccess) { $('input[name="approval_required"]', $dialog).removeAttr("disabled"); } else { $('input[name="approval_required"]', $dialog).attr("disabled", "disabled"); } } function fanAccessChanged(evt) { $dialog = $('[layout-id="session-settings"]'); var hasFanAccess = $('select[name="fan_access"]', $dialog).val(); // string hasFanAccess = context.JK.stringToBool(hasFanAccess); if (hasFanAccess) { $('input[name="fan_chat"]', $dialog).removeAttr("disabled"); } else { $('input[name="fan_chat"]', $dialog).attr("disabled", "disabled"); } } function saveSettings(evt) { var newSessionInfo = $('#session-settings-dialog').formToObject(); var id = newSessionInfo.id; delete newSessionInfo.id; if (typeof newSessionInfo.genres === "string") { newSessionInfo.genres = [newSessionInfo.genres]; } rest.updateSession(id, newSessionInfo, settingsSaved); } function settingsSaved(response) { // No response returned from this call. 204. sessionScreen.refreshCurrentSession(); app.layout.closeDialog('session-settings'); } function events() { $('#session-settings-dialog-submit').on('click', saveSettings); $('#session-settings-dialog select[name="fan_access"]').on('change', fanAccessChanged); $('#session-settings-dialog select[name="musician_access"]').on('change', musicianAccessChanged); } this.initialize = function() { events(); var dialogBindings = { 'beforeShow': beforeShow }; app.bindDialog('session-settings', dialogBindings); }; }; })(window,jQuery);