php-censor/public/assets/js/app.js

442 lines
13 KiB
JavaScript
Raw Normal View History

2016-07-19 20:28:11 +02:00
var PHPCensor = {
intervals: {},
widgets: {},
init: function () {
$(document).ready(function () {
// Update latest builds every 5 seconds:
2016-07-19 20:28:11 +02:00
PHPCensor.getBuilds();
PHPCensor.intervals.getBuilds = setInterval(PHPCensor.getBuilds, 5000);
// Update latest project builds every 10 seconds:
2016-07-21 17:20:34 +02:00
if (typeof PROJECT_ID != 'undefined') {
2016-07-19 20:28:11 +02:00
PHPCensor.intervals.getProjectBuilds = setInterval(PHPCensor.getProjectBuilds, 10000);
}
});
$(window).on('builds-updated', function (e, data) {
2016-07-19 20:28:11 +02:00
PHPCensor.updateHeaderBuilds(data);
});
},
getBuilds: function () {
$.ajax({
url: APP_URL + 'build/ajax-queue',
success: function (data) {
$(window).trigger('builds-updated', [data]);
},
2016-07-19 20:28:11 +02:00
error: PHPCensor.handleFailedAjax
});
},
getProjectBuilds: function () {
$.ajax({
url: APP_URL + 'project/ajax-builds/' + PROJECT_ID + '?branch=' + PROJECT_BRANCH + '&environment=' + PROJECT_ENVIRONMENT + '&per_page=' + PER_PAGE + '&page=' + PAGE,
success: function (data) {
$('#latest-builds').html(data);
},
2016-07-19 20:28:11 +02:00
error: PHPCensor.handleFailedAjax
});
},
updateHeaderBuilds: function (data) {
2016-07-21 19:02:11 +02:00
$('.app-pending-list').empty();
$('.app-running-list').empty();
if (!data.pending.count) {
2016-07-21 19:02:11 +02:00
$('.app-pending').hide();
} else {
2016-07-21 19:02:11 +02:00
$('.app-pending').show();
$('.app-pending .header').text(Lang.get('n_builds_pending', data.pending.count));
$.each(data.pending.items, function (idx, build) {
2016-07-21 19:02:11 +02:00
$('.app-pending-list').append(build.header_row);
});
}
if (!data.running.count) {
2016-07-21 19:02:11 +02:00
$('.app-running').hide();
} else {
2016-07-21 19:02:11 +02:00
$('.app-running').show();
$('.app-running .header').text(Lang.get('n_builds_running', data.running.count));
$.each(data.running.items, function (idx, build) {
2016-07-21 19:02:11 +02:00
$('.app-running-list').append(build.header_row);
});
}
},
get: function (uri, success) {
$.ajax({
2016-07-21 17:20:34 +02:00
url: window.APP_URL + uri,
success: function (data) {
success();
},
2016-07-19 20:28:11 +02:00
error: PHPCensor.handleFailedAjax
});
},
handleFailedAjax: function (xhr) {
if (xhr.status == 401) {
2016-07-21 17:20:34 +02:00
window.location.href = window.APP_URL + 'session/login';
}
}
};
2016-07-19 20:28:11 +02:00
PHPCensor.init();
function handleFailedAjax(xhr) {
2016-07-19 20:28:11 +02:00
PHPCensor.handleFailedAjax(xhr);
}
/**
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
* for the details of code below
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {
},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
/**
* Used for delete buttons in the system, just to prevent accidental clicks.
*/
function confirmDelete(url, reloadAfter) {
2016-07-19 20:28:11 +02:00
var dialog = new PHPCensorConfirmDialog({
2017-09-17 06:22:05 +02:00
title: Lang.get('confirm_title'),
message: Lang.get('confirm_message'),
confirmBtnCaption: Lang.get('confirm_ok'),
2017-09-17 06:22:05 +02:00
cancelBtnCaption: Lang.get('confirm_cancel'),
/*
confirm-btn click handler
*/
confirmed: function (e) {
var dialog = this;
e.preventDefault();
/*
Call delete URL
*/
$.ajax({
url: url,
success: function (data) {
if (reloadAfter) {
dialog.onClose = function () {
window.location.reload();
};
}
dialog.showStatusMessage(Lang.get('confirm_success'), 500);
},
error: function (data) {
dialog.showStatusMessage(Lang.get('confirm_failed') + data.statusText);
if (data.status == 401) {
handleFailedAjax(data);
}
}
});
}
});
dialog.show();
return dialog;
}
/**
2016-07-19 20:28:11 +02:00
* PHPCensorConfirmDialog constructor options object
* @type {{message: string, title: string, confirmBtnCaption: string, cancelBtnCaption: string, confirmed: Function}}
*/
2016-07-19 20:28:11 +02:00
var PHPCensorConfirmDialogOptions = {
message: 'Are you sure?',
title: 'Confirmation',
confirmBtnCaption: 'Ok',
cancelBtnCaption: 'Cancel',
confirmed: function (e) {
this.close();
}
};
2016-07-19 20:28:11 +02:00
var PHPCensorConfirmDialog = Class.extend({
/**
* @private
* @var {bool} Determines whether the dialog has been confirmed
*/
confirmed: false,
/**
2016-07-19 20:28:11 +02:00
* @param {PHPCensorConfirmDialogOptions} options
*/
init: function (options) {
2016-07-19 20:28:11 +02:00
options = options ? $.extend(PHPCensorConfirmDialogOptions, options) : PHPCensorConfirmDialogOptions;
if (!$('#confirm-dialog').length) {
/*
Add the dialog html to a page on first use. No need to have it there before first use.
*/
$('body').append(
'<div class="modal fade" id="confirm-dialog">'
2017-01-13 19:01:41 +01:00
+ '<div class="modal-dialog">'
+ '<div class="modal-content">'
+ '<div class="modal-header">'
+ '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'
+ '<h4 class="modal-title"></h4>'
+ '</div>'
+ '<div class="modal-body">'
+ '<p></p>'
+ '</div>'
+ '<div class="modal-footer">'
+ '<button id="confirm-cancel" type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>'
+ '<button id="confirm-ok" type="button" class="btn btn-danger"></button>'
+ '</div>'
+ '</div>'
+ '</div>'
+ '</div>'
);
}
/*
Define dialog controls
*/
this.$dialog = $('#confirm-dialog');
2017-01-13 19:01:41 +01:00
this.$cancelBtn = this.$dialog.find('#confirm-cancel');
this.$confirmBtn = this.$dialog.find('#confirm-ok');
this.$title = this.$dialog.find('h4.modal-title');
this.$body = this.$dialog.find('div.modal-body');
/*
Initialize its values
*/
2016-07-19 20:28:11 +02:00
this.$title.html(options.title ? options.title : PHPCensorConfirmDialogOptions.title);
this.$body.html(options.message ? options.message : PHPCensorConfirmDialogOptions.message);
this.$confirmBtn.html(
2016-07-19 20:28:11 +02:00
options.confirmBtnCaption ? options.confirmBtnCaption : PHPCensorConfirmDialogOptions.confirmBtnCaption
);
this.$cancelBtn.html(
2016-07-19 20:28:11 +02:00
options.cancelBtnCaption ? options.cancelBtnCaption : PHPCensorConfirmDialogOptions.cancelBtnCaption
);
/*
Events
*/
this.confirmBtnClick = options.confirmed;
/*
Re-bind handlers
*/
this.$confirmBtn.unbind('click');
this.$confirmBtn.click(this.onConfirm.bind(this));
this.$confirmBtn.unbind('hidden.bs.modal');
/*
Bind the close event of the dialog to the set of onClose* methods
*/
2017-09-17 06:22:05 +02:00
this.$dialog.on('hidden.bs.modal', function () {
this.onClose()
}.bind(this));
this.$dialog.on('hidden.bs.modal', function () {
if (this.confirmed) {
this.onCloseConfirmed();
} else {
this.onCloseCanceled();
}
}.bind(this));
/*
Restore state if was changed previously
*/
this.$cancelBtn.show();
this.$confirmBtn.show();
this.confirmed = false;
},
/**
* Show dialog
*/
show: function () {
this.$dialog.modal('show');
},
/**
* Hide dialog
*/
close: function () {
this.$dialog.modal('hide');
},
onConfirm: function (e) {
this.confirmed = true;
$(this).attr('disabled', 'disabled');
this.confirmBtnClick(e);
},
/**
* Called only when confirmed dialog was closed
*/
2017-09-17 06:22:05 +02:00
onCloseConfirmed: function () {
},
/**
* Called only when canceled dialog was closed
*/
2017-09-17 06:22:05 +02:00
onCloseCanceled: function () {
},
/**
* Called always when the dialog was closed
*/
2017-09-17 06:22:05 +02:00
onClose: function () {
},
showStatusMessage: function (message, closeTimeout) {
this.$confirmBtn.hide();
this.$cancelBtn.hide();
/*
Status message
*/
this.$body.html(message);
if (closeTimeout) {
window.setTimeout(function () {
/*
Hide the dialog
*/
this.close();
}.bind(this), closeTimeout);
}
}
});
/**
* Used to initialise the project form:
*/
2017-09-17 06:22:05 +02:00
function setupProjectForm() {
$('.github-container').hide();
2017-09-17 06:22:05 +02:00
$('#element-reference').change(function () {
var el = $(this);
var val = el.val();
var type = $('#element-type').val();
var acceptable = {
'github': {
'ssh': /git\@github\.com\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'git': /git\:\/\/github.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/github\.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
},
'bitbucket': {
'ssh': /git\@bitbucket\.org\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/[a-zA-Z0-9_\-]+\@bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'anon': /https\:\/\/bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
}
};
2017-09-17 06:22:05 +02:00
if (acceptable[type] !== undefined) {
for (var i in acceptable[type]) {
if (val.match(acceptable[type][i])) {
el.val(val.replace(acceptable[type][i], '$1'));
}
}
}
});
2017-09-17 06:22:05 +02:00
$('#element-type').change(function () {
if ($(this).val() == 'github') {
$('#loading').show();
$.ajax({
dataType: "json",
url: window.APP_URL + 'project/ajax-github-repositories',
success: function (data) {
$('#loading').hide();
if (data && data.repos) {
$('#element-github').empty();
for (var i in data.repos) {
var name = data.repos[i];
$('#element-github').append($('<option></option>').text(name).val(name));
}
$('.github-container').slideDown();
}
},
error: handleFailedAjax
});
} else {
$('.github-container').slideUp();
}
$('#element-reference').trigger('change');
});
2017-09-17 06:22:05 +02:00
$('#element-github').change(function () {
var val = $('#element-github').val();
2017-09-17 06:22:05 +02:00
if (val != 'choose') {
$('#element-type').val('github');
$('#element-reference').val(val);
$('label[for=element-reference]').hide();
$('label[for=element-type]').hide();
$('#element-reference').hide();
$('#element-token').val(window.github_token);
$('#element-title').val(val);
} else {
$('label[for=element-reference]').show();
$('label[for=element-type]').show();
$('#element-reference').show();
$('#element-type').show();
$('#element-reference').val('');
$('#element-token').val('');
}
});
}
2014-12-04 12:14:04 +01:00
var Lang = {
get: function () {
2017-09-17 06:22:05 +02:00
var args = Array.prototype.slice.call(arguments);
;
2014-12-04 12:14:04 +01:00
var string = args.shift();
2016-07-21 17:20:34 +02:00
if (STRINGS[string]) {
args.unshift(STRINGS[string]);
2014-12-04 12:14:04 +01:00
return sprintf.apply(sprintf[0], args);
}
return string;
2014-12-04 12:14:04 +01:00
}
};