All files / src/methods/social index.js

37.5% Statements 6/16
50% Branches 6/12
50% Functions 2/4
37.5% Lines 6/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96      1x                                                                                                       1x           1x       1x 1x   1x                                                      
import config from '../../config';
import Method from '../Method';
 
const formModel = {
    addFacebookIdentityButton: 'add_fb_identity',
    facebookDisconnectButton: 'fb_disconnect'
};
 
/**
 * @memberOf Useris
 * @function social
 *
 * @description Validate and expose function for social account
 *
 * It must be used with the **newtonsmartstatussocial** template;
 *
 * @param {Object} options Options object
 * @param {Object} [options.element=false] The div element where the html's tmpl will be injected
 * @param {String} [options.access_token=''] **Optionals** if true,
 * the tmpl calls will be called with the params access_token filled
 * @param {Boolean} [options.doNotLoad=false] **Optionals** if true,
 * this params will tell useris to not load the template (It doesn't make the apicall)
 * @param {object} callbackOnject Object containg all the callbacks
 * @param {function} [callbackObject.onTemplateLoaded='no-default']
 * Callback executed when the template has been loaded.
 * @param {function} [callbackObject.onFacebookRegisterSuccess='no-default']
 * Callback executed when the template has been loaded.
 * @param {function} [callbackObject.onFacebookDisconnect='no-default']
 * Callback executed when the template has been loaded.
 *
 * @example
 *  Useris.social({
 *      element: document.getElementById('elementId'),
 *      access_token: 'abcdefgh1234567890'
 *  }, {
 *      onTemplateLoaded: function(tmplInfo){
 *          console.log('Template loaded', tmplInfo);
 *      },
 *      onFacebookRegisterSuccess: function(){
 *          //facebook login code
 *          facebookLogin();
 *      },
 *      onFacebookDisconnect: function(){
 *          //facebook disconnect code
 *          facebookDisconnect();
 *      }
 *  });
 *
 *  @returns {object}
 *  <pre>
 *      {}
 *  </pre>
 */
export default class Social extends Method {
    init = (options) => {
        this.smartsectionObj = {
            subsection: 'newtonsmartstatussocial',
            loadForm: options.element || false,
            access_token: options.access_token || '',
            doNotLoad: options.doNotLoad || false
        };
        this.initUserView();
    };
 
    attachEvents = () => {
        let addFacebookIdentityButton = false;
        let facebookDisconnectButton = false;
 
        Iif (config.get('identityFbEnabled')) {
            addFacebookIdentityButton = this.UserViewInstance
                .getElement(formModel.addFacebookIdentityButton);
            facebookDisconnectButton = this.UserViewInstance
                .getElement(formModel.facebookDisconnectButton);
 
            if (addFacebookIdentityButton) {
                this.UserViewInstance.attachEvent(addFacebookIdentityButton, {
                    eventName: 'click',
                    event: (e) => {
                        e.preventDefault();
                        this.callback('onFacebookRegisterSuccess');
                    }
                });
            }
 
            if (facebookDisconnectButton) {
                this.UserViewInstance.attachEvent(facebookDisconnectButton, {
                    eventName: 'click',
                    event: (e) => {
                        e.preventDefault();
                        this.callback('onFacebookDisconnect');
                    }
                });
            }
        }
    }
}