Skip to content

Instantly share code, notes, and snippets.

@ray-peters
Last active September 8, 2015 15:52
Show Gist options
  • Save ray-peters/228b825d3fb4be6b22e1 to your computer and use it in GitHub Desktop.
Save ray-peters/228b825d3fb4be6b22e1 to your computer and use it in GitHub Desktop.
Patient Zero
// HOW TO USE: Click start from scratch and be looking at the "Add Block" box
// CONFIGURATIONS
var SPLIT_TEST_INDEX = 0;
var optEditor = ontraport.window.panel.currentPane.components.editor_component,
optEditorModel = optEditor.options.model,
BlockStylesModel = ontraport.Models.Objects.getModel( 131 ),
BlockTypes = ontraport.Models.Objects.getModel( 132 ),
_def = $.Deferred();
var ROTATE_INTERVAL = 500;
$( "<div />" ).ontraport_components_dialog_loading_dialog( {
"deferred": _def,
"theme": "blue",
"loadingPhrase": $l( "Building Patient Zero" )
} );
$.when( BlockTypes.getList( { "range": 99999 } ) )
.done( function( blockTypesResponse ) {
var nextBlockIndex = 0,
optEditorModelType = optEditorModel.attr( "type" ),
mapBlockIDtoRotateCount = {};
function rotateBlock( id ) {
optEditor.options.uiState.attr( "currentSelectedBlock", id );
var retDef = $.Deferred(),
count = mapBlockIDtoRotateCount[ id ];
console.log( "Starting to rotate: ", id );
var rotateIt = function( id, rotateCount ) {
var _count = rotateCount;
return function(){
console.log( "\t\t...again..." );
optEditorModel.rotateBlockInstance( SPLIT_TEST_INDEX, id, 1 );
if ( --_count > 0 ) {
setTimeout( function(){
rotateIt();
}, ( ROTATE_INTERVAL / 2 ) );
} else {
console.log( "\tDONE!" );
retDef.resolve();
}
}
}( id, count );
rotateIt();
return retDef;
}
var blockRotationHandler = function(){
console.log( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
console.log( "!!!! handling block rotations !!!!" );
console.log( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
var blocksToRotate = Object.keys( mapBlockIDtoRotateCount ),
index = 0;
function _helper() {
var ret = rotateBlock( blocksToRotate[ index ] );
index++;
if ( blocksToRotate[ index ] ) {
ret.done( function(){
setTimeout( _helper, ROTATE_INTERVAL );
} );
} else {
ret.done( function(){
setTimeout( function(){
console.log( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
console.log( "!!!!!!!!!!!!!! DONE !!!!!!!!!!!!!!" );
console.log( "!!!!!!!!!!!!!! DONE !!!!!!!!!!!!!!" );
console.log( "!!!!!!!!!!!!!! DONE !!!!!!!!!!!!!!" );
console.log( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
_def.resolve();
}, ROTATE_INTERVAL * 3 );
} );
}
};
_helper();
};
function loadNextBlock( arr ) {
if ( arr.length === 0 ) {
// We're done; Show the block list
optEditor.options.uiState.attr( "editMode", null );
setTimeout( blockRotationHandler, 1000 );
return;
}
var blockType = _.first( arr );
optEditorModel.getBlocksOfType( {
"block_type_id": blockType.id,
"type": optEditorModelType,
"range": 9999
} ).done( function( data ) {
var _dataBlocks = data.data.blocks;
// add a block for every block style
if ( _dataBlocks ) {
for ( var i = 0, l = _dataBlocks.length; i < l; ++i ) {
optEditorModel.addBlock( SPLIT_TEST_INDEX, blockType.id, nextBlockIndex, blockType.name );
if ( i > 0 ) {
mapBlockIDtoRotateCount[ optEditorModel.resource[ SPLIT_TEST_INDEX ].blocks[ nextBlockIndex ].id ] = i;
}
nextBlockIndex++;
}
}
console.log( blockType.name + " creating " + l + " blocks!" );
setTimeout( function(){
loadNextBlock( _.rest( arr ) );
}, ROTATE_INTERVAL );
} );
}
loadNextBlock( blockTypesResponse.data );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment