@ -8,125 +8,99 @@ define([
var module = angular . module ( 'kibana.services' ) ;
module . service ( 'filterSrv' , function ( dashboard , $rootScope , $timeout , $routeParams ) {
module . factory ( 'filterSrv' , function ( dashboard , $rootScope , $timeout , $routeParams ) {
// defaults
var _d = {
list : [ ] ,
time : { }
} ;
// Save a reference to this
var self = this ;
var result = {
_updateTemplateData : function ( initial ) {
this . _filterTemplateData = { } ;
_ . each ( this . list , function ( filter ) {
if ( initial ) {
var urlValue = $routeParams [ filter . name ] ;
if ( urlValue ) {
filter . current = { text : urlValue , value : urlValue } ;
}
}
if ( ! filter . current || ! filter . current . value ) {
return ;
}
this . _filterTemplateData [ filter . name ] = filter . current . value ;
} ) ;
} ,
filterOptionSelected : function ( option ) {
this . current = option ;
this . _updateTemplateData ( ) ;
} ,
add : function ( filter ) {
this . list . push ( filter ) ;
} ,
applyFilterToTarget : function ( target ) {
if ( target . indexOf ( '[[' ) === - 1 ) {
return target ;
}
return _ . template ( target , this . _filterTemplateData , this . templateSettings ) ;
} ,
setTime : function ( time ) {
_ . extend ( this . time , time ) ;
// disable refresh if we have an absolute time
if ( time . to !== 'now' ) {
this . old _refresh = this . dashboard . refresh ;
dashboard . set _interval ( false ) ;
return ;
}
if ( this . old _refresh && this . old _refresh !== this . dashboard . refresh ) {
dashboard . set _interval ( this . old _refresh ) ;
this . old _refresh = null ;
}
} ,
timeRange : function ( parse ) {
var _t = this . time ;
if ( _ . isUndefined ( _t ) || _ . isUndefined ( _t . from ) ) {
return false ;
}
if ( parse === false ) {
return {
from : _t . from ,
to : _t . to
} ;
} else {
var _from = _t . from ;
var _to = _t . to || new Date ( ) ;
return {
from : kbn . parseDate ( _from ) ,
to : kbn . parseDate ( _to )
} ;
}
} ,
removeFilter : function ( filter , dashboard ) {
this . list = _ . without ( this . list , filter ) ;
} ,
init : function ( dashboard ) {
_ . defaults ( this , _d ) ;
this . dashboard = dashboard ;
this . templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g } ;
if ( dashboard && dashboard . services && dashboard . services . filter ) {
// compatiblity hack
this . time = dashboard . services . filter . time ;
}
// Call this whenever we need to reload the important stuff
this . init = function ( ) {
dashboard . current . services . filter = dashboard . current . services . filter || { } ;
_ . defaults ( dashboard . current . services . filter , _d ) ;
self . list = dashboard . current . services . filter . list ;
self . time = dashboard . current . services . filter . time ;
self . templateSettings = {
interpolate : /\[\[([\s\S]+?)\]\]/g ,
} ;
if ( self . list . length ) {
this . _updateTemplateData ( true ) ;
}
} ;
this . _updateTemplateData = function ( initial ) {
self . _filterTemplateData = { } ;
_ . each ( self . list , function ( filter ) {
if ( initial ) {
var urlValue = $routeParams [ filter . name ] ;
if ( urlValue ) {
filter . current = { text : urlValue , value : urlValue } ;
}
}
if ( ! filter . current || ! filter . current . value ) {
return ;
}
self . _filterTemplateData [ filter . name ] = filter . current . value ;
} ) ;
} ;
this . filterOptionSelected = function ( filter , option ) {
filter . current = option ;
this . _updateTemplateData ( ) ;
dashboard . refresh ( ) ;
} ;
this . add = function ( filter ) {
self . list . push ( filter ) ;
} ;
this . applyFilterToTarget = function ( target ) {
if ( target . indexOf ( '[[' ) === - 1 ) {
return target ;
}
return _ . template ( target , self . _filterTemplateData , self . templateSettings ) ;
} ;
this . remove = function ( filter ) {
self . list = dashboard . current . services . filter . list = _ . without ( self . list , filter ) ;
if ( ! $rootScope . $$phase ) {
$rootScope . $apply ( ) ;
}
$timeout ( function ( ) {
dashboard . refresh ( ) ;
} , 0 ) ;
} ;
this . setTime = function ( time ) {
_ . extend ( self . time , time ) ;
// disable refresh if we have an absolute time
if ( time . to !== 'now' ) {
self . old _refresh = dashboard . current . refresh ;
dashboard . set _interval ( false ) ;
}
else if ( self . old _refresh && self . old _refresh !== dashboard . current . refresh ) {
dashboard . set _interval ( self . old _refresh ) ;
self . old _refresh = null ;
}
$timeout ( function ( ) {
dashboard . refresh ( ) ;
} , 0 ) ;
} ;
this . timeRange = function ( parse ) {
var _t = self . time ;
if ( _ . isUndefined ( _t ) ) {
return false ;
}
if ( parse === false ) {
return {
from : _t . from ,
to : _t . to
} ;
} else {
var
_from = _t . from ,
_to = _t . to || new Date ( ) ;
return {
from : kbn . parseDate ( _from ) ,
to : kbn . parseDate ( _to )
} ;
}
} ;
// Now init
self . init ( ) ;
} ;
return result ;
} ) ;
} ) ;
} ) ;