/** * BuboBox form callback module * * @author Wim Mostmans (wim@bubobox.com) * @contributor Gert Sallaerts (gert@bubobox.com) * * @license BSD * * Copyright (c) 2014 BuboBox */ window.bbbx_modules = window.bbbx_modules || []; // Make modules array if not yet exists window.bbbx_modules.push(function(sandbox, $) { var NAME = 'bbbx-login-stack'; var settings = $(bbbx_login_stack_tag).data(); var formId = 'bbbx-login-stack-form'; /** * Add a stack item to the recorder/player popup * name can be pre/post or name of a stack item + pre/post * For example "form.pre" or "terms.post" */ function addItem( e ) { // Check if intro text is configured var intro = ''; if( settings.hasOwnProperty('intro') && settings.intro !== '' ) { intro = '

' + settings.intro + '


'; } e.stack.itemReady({ name: 'pre', view: { title: 'Login', body: intro + '
', footer: '' } }); } function verify( e ) { // Get credentials from the form var credentials = $('#' + formId).serialize(), username = $('#' + formId + ' input[name=username]').val(); // Set button to grey to give the user a visual cue that something is happening e.origin.addClass('bbbx-button-grey'); sandbox.api.validation.custom( // The data you want to validate { username: username }, // The rules to apply { username: [ 'required', 'unique_field' ] }, // Callback function (response) { if (response.valid) { // If the username has not yet participated, we will try to authenticate $.post(settings.endpoint, credentials).done(function( response ) { // Enable login button again e.origin.removeClass('bbbx-button-grey'); if( response.status === 'error' ) { hasError( true ); $('#' + formId + ' #bbbx-username').focus(); // Set focus on the input field } else { hasError( false ); // If response contains meta add it to the current user if( response.hasOwnProperty( 'meta' ) ) { for( key in response.meta ) { sandbox.setMeta( key, response.meta[ key ] ); } } sandbox.publish('stack.next', {origin: $('#' + formId)}); } }); } else { // The username has already participated, we will not try to authenticate hasError(true); } }, // Error handler (if something goes wrong with the API request) function (response) { console.error(response); } ); } function hasError( error ) { var inputs = $('#' + formId).find('input'); if( error === true ) { inputs.addClass('bbbx-error'); } else { inputs.removeClass('bbbx-error'); } } var exports = { NAME: NAME, /** * Module is registered inside the BuboBox widget */ init: function() { console.error('Custom module initialized'); }, /** * All modules are loaded */ ready: function() { console.error('Custom module ready, all other modules are also loaded at this point'); }, /** * Add listeners for certain actions that happen * in the BuboBox widgets */ bind: function() { sandbox.subscribe('stack.add.pre', addItem); sandbox.subscribe('bbbx-login-stack.login', verify); }, /** * Remove listeners we create in the bind function */ unbind: function() { sandbox.unsubscribe(['stack.add.pre', addItem]); sandbox.unsubscribe(['bbbx-login-stack.login', verify]); }, /** * Clean up all stuff this module has created * - DOM elements * - Bindings */ destroy: function() { this.unbind(); // remove some left over DOM elments } }; return exports; }); // Store the current script tag reference var bbbx_login_stack_tag = document.currentScript;