summaryrefslogtreecommitdiffstats
path: root/lib/toaster/toastergui/static/js/mrbsection.js
blob: 09117e1daf3d8ff7851d63b996e371bf6ce156fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function mrbSectionInit(ctx){

  var projectBuilds;

  if (ctx.mrbType === 'project')
    projectBuilds = true;

  $(".cancel-build-btn").click(function(e){
    e.preventDefault();

    var url = $(this).data('request-url');
    var buildReqIds = $(this).data('buildrequest-id');
    var banner = $(this).parents(".alert");

    banner.find(".progress-info").fadeOut().promise().done(function(){
      $("#cancelling-msg-" + buildReqIds).show();
      console.log("cancel build");
      libtoaster.cancelABuild(url, buildReqIds, function(){
        if (projectBuilds == false){
          /* the all builds page is not 'self updating' like thei
           * project Builds
           */
          window.location.reload();
        }
      }, null);
    });
  });

  $(".run-again-btn").click(function(e){
    e.preventDefault();

    var url = $(this).data('request-url');
    var target = $(this).data('target');

    libtoaster.startABuild(url, target, function(){
      window.location.reload();
    }, null);
  });


  var progressTimer;

  if (projectBuilds === true){
    progressTimer = window.setInterval(function() {
      libtoaster.getProjectInfo(libtoaster.ctx.projectPageUrl,
        function(prjInfo){
          /* These two are needed because a build can be 100% and still
           * in progress due to the fact that the % done is updated at the
           * start of a task so it can be doing the last task at 100%
           */
          var inProgress = 0;
          var allPercentDone = 0;
          if (prjInfo.builds.length === 0)
            return

          for (var i in prjInfo.builds){
            var build = prjInfo.builds[i];

            if (build.outcome === "In Progress" ||
               $(".progress .bar").length > 0){
              /* Update the build progress */
              var percentDone;

              if (build.outcome !== "In Progress"){
                /* We have to ignore the value when it's Succeeded because it
                *   goes back to 0
                */
                percentDone = 100;
              } else {
                percentDone = build.percentDone;
                inProgress++;
              }

              $("#build-pc-done-" + build.id).text(percentDone);
              $("#build-pc-done-title-" + build.id).attr("title", percentDone);
              $("#build-pc-done-bar-" + build.id).css("width",
                String(percentDone) + "%");

              allPercentDone += percentDone;
            }
          }

          if (allPercentDone === (100 * prjInfo.builds.length) && !inProgress)
            window.location.reload();

          /* Our progress bar is not still showing so shutdown the polling. */
          if ($(".progress .bar").length === 0)
            window.clearInterval(progressTimer);

      });
    }, 1500);
  }
}