|
/* |
|
* uiBlockers |
|
* Description: adds the possibility of blocking the UI on specific regions, mantaining separate |
|
* runningCalls counters for each of the registered regions. |
|
* |
|
*/ |
|
|
|
function uiBlockers ( config ){ |
|
|
|
// Utils |
|
var isString = _.isString, |
|
isEmpty = _.isEmpty, |
|
bind = _.bind; |
|
|
|
|
|
var blockers = { |
|
'': { calls: 0, selector: 'body' } |
|
}; |
|
|
|
|
|
function normalizeSelector ( selector ){ |
|
return ( isString( selector ) && !isEmpty( selector ) ) ? selector : 'body'; |
|
} |
|
|
|
function hasBlocker ( name ){ |
|
return !!blockers[name]; |
|
} |
|
|
|
function addBlocker ( name, selector ){ |
|
if ( _.isUndefined( selector ) ){ |
|
selector = name; |
|
} |
|
var out = false; |
|
if ( !hasBlocker( name ) ){ |
|
out = blockers[name] = { calls: 0, selector: normalizeSelector( selector ) }; |
|
} |
|
return out; |
|
} |
|
|
|
function getSelector ( name ){ |
|
return ( blockers[name] && blockers[name].selector ); |
|
} |
|
|
|
function incCalls ( name ){ |
|
if ( hasBlocker(name) ){ |
|
blockers[name].calls = ( blockers[name].calls || 0 ) + 1; |
|
} |
|
} |
|
function decCalls ( name ){ |
|
if ( hasBlocker(name) ){ |
|
blockers[name].calls = Math.max( ( blockers[name].calls || 0 ) - 1, 0 ); |
|
} |
|
} |
|
function resetCalls( name ){ |
|
if ( hasBlocker(name) ){ |
|
blockers[name].calls = 0; |
|
} |
|
} |
|
function getCalls( name ){ |
|
return ( blockers[name] && blockers[name].calls ); |
|
} |
|
|
|
function showProgressIndicator ( name ) { |
|
if( $.blockUI ){ |
|
var selector = getSelector( name ); |
|
if ( selector == 'body'){ |
|
Dashboards.blockUIwithDrag(); |
|
} else { |
|
$(selector).block(); |
|
} |
|
} |
|
} |
|
|
|
function hideProgressIndicator ( name ) { |
|
if( $.unblockUI ){ |
|
var selector = getSelector( name ); |
|
if ( selector == 'body'){ |
|
$.unblockUI(); |
|
} else { |
|
$(selector).unblock(); |
|
} |
|
} |
|
Dashboards.showErrorTooltip(); |
|
} |
|
|
|
function resetRunningCalls ( name ){ |
|
resetCalls( name ); |
|
setTimeout( function(){ |
|
hideProgressIndicator( name ); |
|
}, 10); |
|
} |
|
|
|
function incrementRunningCalls ( name ) { |
|
incCalls( name ) ; |
|
showProgressIndicator( name ); |
|
} |
|
|
|
function decrementRunningCalls ( name ) { |
|
decCalls( name ) ; |
|
setTimeout( function(){ |
|
if( getCalls( name ) <= 0 ){ |
|
hideProgressIndicator( name ); |
|
resetCalls( name ); // Just in case |
|
} |
|
},10); |
|
} |
|
|
|
|
|
function getBlock ( comp, name ) { |
|
return bind(function() { |
|
if(!this.isRunning){ |
|
incrementRunningCalls( name ); |
|
this.isRunning = true; |
|
} |
|
}, comp); |
|
} |
|
|
|
function getUnblock ( comp, name ) { |
|
return bind(function(){ |
|
if(this.isRunning){ |
|
decrementRunningCalls( name ); |
|
this.isRunning = false; |
|
} |
|
}, comp); |
|
} |
|
|
|
|
|
return { |
|
addBlocker: addBlocker, |
|
showProgressIndicator: showProgressIndicator, |
|
hideProgressIndicator: hideProgressIndicator, |
|
resetRunningCalls: resetRunningCalls, |
|
getRunningCalls: getCalls, |
|
incrementRunningCalls: incrementRunningCalls, |
|
decrementRunningCalls: decrementRunningCalls, |
|
getBlock: getBlock, |
|
getUnblock: getUnblock |
|
|
|
}; |
|
|
|
} |