Pietro
Pietro

Reputation: 1835

Jira Gadget Plugin - Javascript DRY

I see in the gadget xml file, where I write the view part to create the chart that is always the same code for every gadget, is it possible to make more clear? like creating an external Javascript file to import? Probably the problem of an external file is to get the data.

here is the code:

view: {
                        enableReload: true,
                        onResizeReload: true,
                        onResizeAdjustHeight: true,
                        template: function (args) {
                            var gadget = this;
                            gadget.getView().empty();
                            gadget.projectOrFilterName = args.chart.filterName;

                            var container = AJS.$("<div id='chart_div'/>");
                            gadget.getView().append(container);

                            if(args.chart.data && args.chart.data.length > 0) {
                                var data = new google.visualization.DataTable();
                                data.addColumn('string', this.getMsg("gadget.user.activity.time.label"));
                                data.addColumn('number', this.getMsg("gadget.user.activity.issues.solving.label"));
                                data.addColumn('number', this.getMsg("gadget.user.activity.sum.label"));
                                data.addRows(args.chart.data);

                                switch(gadget.getPref("chartType"))
                                {
                                    case "ColumnChart":
                                        var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
                                        break;
                                    case "AreaChart":
                                        var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
                                        break;
                                    case "LineChart":
                                        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
                                        break;
                                }

                                var width = gadgets.window.getViewportDimensions().width;
                                var height = width * 3/4;
                                chart.draw(
                                    data,
                                    {width: width, height: height,
                                    curveType: "function",
                                    legend: 'bottom',
                                    backgroundColor:{fill:'#DDE8FF'},
                                    vAxis: {  textStyle:{fontSize:10},
                                            baseline:0,
                                            baselineColor:'#9F0000'},
                                    hAxis: {textStyle:{fontSize:10},
                                            slantedText:'true',
                                            slantedTextAngle:45},
                                    colors:['#566D7E','orange'],
                                    fontName:'Trebuchet MS',
                                    pointSize: 2
                                });
                            }
                            else {
                                gadget.getView().append("<p>No Data available</p>");
                            }

                            gadget.resize();
                        },

Upvotes: 0

Views: 1435

Answers (1)

Fabien
Fabien

Reputation: 105

You should include your own JS in your atlassian-plugin.xml like that:

  <!-- GADGET WEB RESOURCES -->
  <web-resource key="resources">
    <resource type="download" name="javascript/common.js" location="javascript/MyCustomLibrary.js">
      <property key="content-type" value="text/javascript"/>
    </resource>
    <!-- ... -->
  </web-resource>

And, then, add it in your plugin.xml

#requireResource("plugin-key:resources")

And, finally, call your JS function from your gadget object view part:

CustomFunction(gadget, data);

Hoping to have helped you, Regards.

Upvotes: 1

Related Questions