Rock.Web.UI.Controls.FlotChart.RegisterJavaScript C# (CSharp) Method

RegisterJavaScript() protected method

Registers the java script.
protected RegisterJavaScript ( ) : void
return void
        protected virtual void RegisterJavaScript()
        {
            var metric = new Rock.Model.MetricService( new Rock.Data.RockContext() ).Get( this.MetricId ?? 0 );

            // setup Rest URL parameters
            if ( metric != null )
            {
                RegisterJavaScriptForMetric( metric );
            }
            else
            {
                _hfRestUrlParams.Value = string.Empty;
            }

            var scriptFormat = new StringBuilder();

            if ( !string.IsNullOrWhiteSpace( DataSourceUrl ) )
            {
                string restUrl = this.ResolveUrl( this.DataSourceUrl );
                _hfRestUrl.Value = restUrl;

                scriptFormat.Append( @"
            var restUrl_{0} = $('#{0} .js-rest-url').val() + $('#{0} .js-rest-url-params').val();

            $.ajax({{
                url: restUrl_{0},
                dataType: 'json',
                contentType: 'application/json'
            }})
            .done( function (chartData) {{
            " );
            }
            else
            {
                scriptFormat.Append( @"
            $(function() {{
                var chartData = {5};
            ");
            }

            scriptFormat.Append( @"
                var chartOptions = {1};
                var plotSelector = '#{0} .js-chart-placeholder';
                var yaxisLabelText = $('#{0} .js-yaxis-value').val();
                var getSeriesUrl = $('#{0} .js-seriesname-url').val();
                var combineValues = {4};
            ");

            if ( this.GetType() == typeof( PieChart ) )
            {
                scriptFormat.Append( @"
                Rock.controls.charts.plotPieChartData(chartData, chartOptions, plotSelector, getSeriesUrl);

            " );
            }
            else if ( this.GetType() == typeof( BarChart ) )
            {
                scriptFormat.Append( @"
                Rock.controls.charts.plotBarChartData(chartData, chartOptions, plotSelector, getSeriesUrl);
            " );
            }
            else
            {
                scriptFormat.Append( @"
                Rock.controls.charts.plotChartData(chartData, chartOptions, plotSelector, yaxisLabelText, getSeriesUrl, combineValues);
            ");
            }

            scriptFormat.Append( @"
                // plothover script (tooltip script)
                {2}

                // plotclick script
                {3}
            ");
            if ( !string.IsNullOrWhiteSpace( DataSourceUrl ) )
            {
                scriptFormat.Append( @"
            }})
            .fail(function (jqXHR, textStatus, errorThrown) {{
                //debugger
            ");
            }

            scriptFormat.Append( @"
            }});
            " );

            string chartOptionsJson = JsonConvert.SerializeObject( this.Options, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore } );

            _hbChartOptions.Text = "<div style='white-space: pre; max-height: 120px; overflow-y:scroll' Font-Names='Consolas' Font-Size='8'><br />" + chartOptionsJson + "</div>";

            var seriesPartitionNameUrl = this.SeriesPartitionNameUrl;
            if ( this.MetricId.HasValue )
            {
                seriesPartitionNameUrl = seriesPartitionNameUrl ?? "~/api/MetricValues/GetSeriesPartitionName/";
                seriesPartitionNameUrl = this.ResolveUrl( seriesPartitionNameUrl.EnsureTrailingForwardslash() + this.MetricId + "/" );
            }

            if ( !string.IsNullOrWhiteSpace( seriesPartitionNameUrl ) )
            {
                _hfSeriesPartitionNameUrl.Value = seriesPartitionNameUrl;
            }
            else
            {
                _hfSeriesPartitionNameUrl.Value = null;
            }

            string tooltipScript = ShowTooltip ? string.Format( "Rock.controls.charts.bindTooltip('{0}', {1})", this.ClientID, this.TooltipFormatter ?? "null" ) : null;
            string chartClickScript = GetChartClickScript();
            string chartData = string.IsNullOrEmpty( ChartData ) ? "[]" : ChartData;

            string script = string.Format(
                scriptFormat.ToString(),
                this.ClientID,      // {0}
                chartOptionsJson,   // {1}
                tooltipScript,      // {2}
                chartClickScript,   // {3}
                this.CombineValues.ToTrueFalse().ToLower(),  // {4}
                chartData );        // {5}

            ScriptManager.RegisterStartupScript( this, this.GetType(), "flot-chart-script_" + this.ClientID, script, true );
        }