mrbsection.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. function mrbSectionInit(ctx){
  2. $('#latest-builds').on('click', '.cancel-build-btn', function(e){
  3. e.stopImmediatePropagation();
  4. e.preventDefault();
  5. var url = $(this).data('request-url');
  6. var buildReqIds = $(this).data('buildrequest-id');
  7. libtoaster.cancelABuild(url, buildReqIds, function () {
  8. window.location.reload();
  9. }, null);
  10. });
  11. $('#latest-builds').on('click', '.rebuild-btn', function(e){
  12. e.stopImmediatePropagation();
  13. e.preventDefault();
  14. var url = $(this).data('request-url');
  15. var target = $(this).data('target');
  16. libtoaster.startABuild(url, target, function(){
  17. window.location.reload();
  18. }, null);
  19. });
  20. // cached version of buildData, so we can determine whether a build has
  21. // changed since it was last fetched, and update the DOM appropriately
  22. var buildData = {};
  23. // returns the cached version of this build, or {} is there isn't a cached one
  24. function getCached(build) {
  25. return buildData[build.id] || {};
  26. }
  27. // returns true if a build's state changed to "Succeeded" or "Failed"
  28. // from some other value
  29. function buildFinished(build) {
  30. var cached = getCached(build);
  31. return cached.state &&
  32. cached.state !== build.state &&
  33. (build.state == 'Succeeded' || build.state == 'Failed' ||
  34. build.state == 'Cancelled');
  35. }
  36. // returns true if the state changed
  37. function stateChanged(build) {
  38. var cached = getCached(build);
  39. return (cached.state !== build.state);
  40. }
  41. // returns true if the complete_percentage changed
  42. function progressChanged(build) {
  43. var cached = getCached(build);
  44. return (cached.tasks_complete_percentage !== build.tasks_complete_percentage);
  45. }
  46. function refreshMostRecentBuilds(){
  47. libtoaster.getMostRecentBuilds(
  48. libtoaster.ctx.mostRecentBuildsUrl,
  49. // success callback
  50. function (data) {
  51. var build;
  52. var tmpl;
  53. var container;
  54. var selector;
  55. var colourClass;
  56. var elements;
  57. // classes on the parent which signify the build state and affect
  58. // the colour of the container for the build
  59. var buildStateClasses = 'alert-info alert-success alert-danger';
  60. for (var i = 0; i < data.length; i++) {
  61. build = data[i];
  62. if (buildFinished(build)) {
  63. // a build finished: reload the whole page so that the build
  64. // shows up in the builds table
  65. window.location.reload();
  66. }
  67. else if (stateChanged(build)) {
  68. // update the whole template
  69. tmpl = $.templates("#build-template");
  70. html = tmpl.render(build);
  71. selector = '[data-latest-build-result="' + build.id + '"] ' +
  72. '[data-role="build-status-container"]';
  73. container = $(selector);
  74. container.html(html);
  75. // style the outermost container for this build to reflect
  76. // the new build state (red, green, blue);
  77. // NB class set here should be in buildStateClasses
  78. colourClass = 'alert-info';
  79. if (build.state == 'Succeeded') {
  80. colourClass = 'alert-success';
  81. }
  82. else if (build.state == 'Failed') {
  83. colourClass = 'alert-danger';
  84. }
  85. elements = $('[data-latest-build-result="' + build.id + '"]');
  86. elements.removeClass(buildStateClasses);
  87. elements.addClass(colourClass);
  88. }
  89. else if (progressChanged(build)) {
  90. // update the progress text
  91. selector = '#build-pc-done-' + build.id;
  92. $(selector).html(build.tasks_complete_percentage);
  93. // update the progress bar
  94. selector = '#build-pc-done-bar-' + build.id;
  95. $(selector).width(build.tasks_complete_percentage + '%');
  96. }
  97. buildData[build.id] = build;
  98. }
  99. },
  100. // fail callback
  101. function (data) {
  102. console.error(data);
  103. }
  104. );
  105. }
  106. window.setInterval(refreshMostRecentBuilds, 1000);
  107. refreshMostRecentBuilds();
  108. }