123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- function mrbSectionInit(ctx){
- $('#latest-builds').on('click', '.cancel-build-btn', function(e){
- e.stopImmediatePropagation();
- e.preventDefault();
- var url = $(this).data('request-url');
- var buildReqIds = $(this).data('buildrequest-id');
- libtoaster.cancelABuild(url, buildReqIds, function () {
- window.location.reload();
- }, null);
- });
- $('#latest-builds').on('click', '.rebuild-btn', function(e){
- e.stopImmediatePropagation();
- e.preventDefault();
- var url = $(this).data('request-url');
- var target = $(this).data('target');
- libtoaster.startABuild(url, target, function(){
- window.location.reload();
- }, null);
- });
- // cached version of buildData, so we can determine whether a build has
- // changed since it was last fetched, and update the DOM appropriately
- var buildData = {};
- // returns the cached version of this build, or {} is there isn't a cached one
- function getCached(build) {
- return buildData[build.id] || {};
- }
- // returns true if a build's state changed to "Succeeded" or "Failed"
- // from some other value
- function buildFinished(build) {
- var cached = getCached(build);
- return cached.state &&
- cached.state !== build.state &&
- (build.state == 'Succeeded' || build.state == 'Failed' ||
- build.state == 'Cancelled');
- }
- // returns true if the state changed
- function stateChanged(build) {
- var cached = getCached(build);
- return (cached.state !== build.state);
- }
- // returns true if the complete_percentage changed
- function progressChanged(build) {
- var cached = getCached(build);
- return (cached.tasks_complete_percentage !== build.tasks_complete_percentage);
- }
- function refreshMostRecentBuilds(){
- libtoaster.getMostRecentBuilds(
- libtoaster.ctx.mostRecentBuildsUrl,
- // success callback
- function (data) {
- var build;
- var tmpl;
- var container;
- var selector;
- var colourClass;
- var elements;
- // classes on the parent which signify the build state and affect
- // the colour of the container for the build
- var buildStateClasses = 'alert-info alert-success alert-danger';
- for (var i = 0; i < data.length; i++) {
- build = data[i];
- if (buildFinished(build)) {
- // a build finished: reload the whole page so that the build
- // shows up in the builds table
- window.location.reload();
- }
- else if (stateChanged(build)) {
- // update the whole template
- tmpl = $.templates("#build-template");
- html = tmpl.render(build);
- selector = '[data-latest-build-result="' + build.id + '"] ' +
- '[data-role="build-status-container"]';
- container = $(selector);
- container.html(html);
- // style the outermost container for this build to reflect
- // the new build state (red, green, blue);
- // NB class set here should be in buildStateClasses
- colourClass = 'alert-info';
- if (build.state == 'Succeeded') {
- colourClass = 'alert-success';
- }
- else if (build.state == 'Failed') {
- colourClass = 'alert-danger';
- }
- elements = $('[data-latest-build-result="' + build.id + '"]');
- elements.removeClass(buildStateClasses);
- elements.addClass(colourClass);
- }
- else if (progressChanged(build)) {
- // update the progress text
- selector = '#build-pc-done-' + build.id;
- $(selector).html(build.tasks_complete_percentage);
- // update the progress bar
- selector = '#build-pc-done-bar-' + build.id;
- $(selector).width(build.tasks_complete_percentage + '%');
- }
- buildData[build.id] = build;
- }
- },
- // fail callback
- function (data) {
- console.error(data);
- }
- );
- }
- window.setInterval(refreshMostRecentBuilds, 1000);
- refreshMostRecentBuilds();
- }
|