locker-factory
Version:
Content Locker UI Builder
184 lines (148 loc) • 4.94 kB
JavaScript
var Dispatcher = require( './Dispatcher' ),
EventEmitter = require( 'events' ).EventEmitter,
Constants = require( './constants/Constants' ),
Assign = require( 'object-assign' ),
_campaigns = [],
_startIndex = 0,
_offset = 0,
_completionUrl = '';
var Store = Assign( {}, EventEmitter.prototype, {
getCampaigns: function() {
return _campaigns;
},
getStartIndex: function() {
return _startIndex;
},
getOffset: function() {
return _offset;
},
getCompletionUrl: function() {
return _completionUrl;
},
// SETTERS
setCampaigns: function( campaigns ) {
_campaigns = campaigns;
},
setStartIndex: function( idx ) {
_startIndex = idx;
},
setOffset: function( offset ) {
_offset = offset;
},
setCompletionUrl: function( url ) {
if( url.length && url.indexOf( 'http' ) == -1 ) {
url = window.location.protocol + '//' + url;
}
_completionUrl = url;
},
// GET CAMPAIGNS
emitGetCampaigns: function() {
this.emit( Constants.actions.GET_CAMPAIGNS );
},
addGetCampaignsListener: function( callback ) {
this.on( Constants.actions.GET_CAMPAIGNS, callback );
},
removeGetCampaignsListener: function( callback ) {
this.removeListener( Constants.actions.GET_CAMPAIGNS, callback );
},
// CLOSE LOCKER
emitCloseLocker: function() {
this.emit( Constants.actions.CLOSE_LOCKER );
},
addCloseLockerListener: function( callback ) {
this.on( Constants.actions.CLOSE_LOCKER, callback );
},
removeCloseLockerListener: function( callback ) {
this.removeListener( Constants.actions.CLOSE_LOCKER, callback );
},
// BLOCK LOCKER
emitBlockLocker: function() {
this.emit( Constants.actions.BLOCK_LOCKER );
},
addBlockLockerListener: function( callback ) {
this.on( Constants.actions.BLOCK_LOCKER, callback );
},
removeBlockLockerListener: function( callback ) {
this.removeListener( Constants.actions.BLOCK_LOCKER, callback );
},
// SUCCESSFULL AJAX RESPONSE
emitRequestSuccess: function() {
this.emit( Constants.request.SUCCESS );
},
addRequestSuccessListener: function( callback ) {
this.on( Constants.request.SUCCESS, callback );
},
removeRequestSuccessListener: function( callback ) {
this.removeListener( Constants.request.SUCCESS, callback );
},
// TIMEOUT AJAX RESPONSE
emitRequestTimeout: function( response ) {
_timeoutResponse = {
message: response.statusText
};
this.emit( Constants.request.TIMEOUT );
},
addRequestTimeoutListener: function( callback ) {
this.on( Constants.request.TIMEOUT, callback );
},
removeRequestTimeoutListener: function( callback ) {
this.removeListener( Constants.actions.TIMEOUT, callback );
},
// ERROR AJAX RESPONSE
emitRequestError: function( response ) {
var message = ( response.statusText ? response.statusText : response.message );
_errorResponse = {
code: response.status,
message: message
};
this.emit( Constants.request.ERROR );
},
addRequestErrorListener: function( callback ) {
this.on( Constants.request.ERROR, callback );
},
removeRequestErrorListener: function( callback ) {
this.removeListener( Constants.actions.ERROR, callback );
}
} );
Dispatcher.register( function( action ) {
switch( action.action ) {
// GET LOCKER CAMPAIGNS
case Constants.actions.GET_CAMPAIGNS:
switch( action.responseType ) {
case Constants.request.PENDING:
break;
case Constants.request.ERROR:
Store.emitRequestError( action.response );
break;
case Constants.request.TIMEOUT:
Store.emitRequestTimeout( action.response );
break;
default:
var campaigns = JSON.parse( action.response.text );
// need to handle campaigns array containing single string value representing a completion url
if( campaigns.length ) {
if( campaigns[ 0 ].completionUrl ) {
// we have a url, close the locker
// this emit will trigger the closeLockerCallback function passed to the locker factory as a prop
Store.setCompletionUrl( campaigns[ 0 ].completionUrl );
Store.emitCloseLocker();
} else {
// we have campaigns
Store.setCampaigns( campaigns );
Store.emitGetCampaigns();
}
} else if( Store.getCampaigns().length == 0 ) {
// no campaigns in ajax response and no campaigns in store from previous request, block content
Store.emitBlockLocker();
} else {
Store.setCampaigns( [] );
Store.emitGetCampaigns();
}
break;
}
break;
default:
// do nothing
}
} );
module.exports = Store;