/bitbucket/src/main/resources/js/bitbucket.js
JavaScript | 256 lines | 211 code | 40 blank | 5 comment | 3 complexity | 66ca8500975c41138195c39413e97dc1 MD5 | raw file
- Bitbucket = {};
- Bitbucket.Repositories = function($)
- {
- var projectOptions;
- var loadRepository = function(id, success, error) {
- $.ajax({
- url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
- type: "GET",
- contentType: 'application/json',
- accepts: 'application/json',
- success: success,
- error: error
- });
- };
- var createRepositoryPopup = function(o) {
- var changeTimeoutId;
- var popup;
- var options = o;
- var defaults = {
- mode: "add",
- save: function() {}
- };
- $.extend({}, defaults, options);
- var create = function create_repository_popup() {
- popup = new AJS.Dialog({width:500, height:180, id: options.mode+"-repository-dialog",
- closeOnOutsideClick: false});
- popup.addPage("page-start");
- popup.gotoPage(0);
- popup.addHeader(AJS.I18n.getText("bitbucket."+options.mode+".title"), "dialog-title");
- popup.addPanel(AJS.I18n.getText("bitbucket."+options.mode+".title"),
- "<form id='"+options.mode+"-repository-form' class='aui' action=''>" +
- "<input name='id' type='hidden'>"+
- "<fieldset>" +
- "<legend class='assistive'>" + AJS.I18n.getText("bitbucket."+options.mode+".description") + "</legend>" +
- "<div class='field-group'><label for='projectKey'>" + AJS.I18n.getText("bitbucket.field.projectKey") + ":</label>" +
- "<select name='projectKey' size='1'>"+projectOptions+"</select></div>" +
- "<div class='field-group'><label for='uri'>" + AJS.I18n.getText("bitbucket.field.uri") + ":</label>" +
- "<input name='uri' size='30'></div>" +
- "</fieldset>" +
- "</form>");
- popup.addButton(AJS.I18n.getText("save.name"), function() {
- options.save();
- }, options.mode + "-repository-start-next-button");
- popup.addCancel(AJS.I18n.getText("cancel.name"), function() {
- popup.remove();
- });
- popup.gotoPage(0);
- };
- return {
- open: function open_repository_popup(f) {
- $.ajax({
- url: contextPath + "/rest/api/2.0.alpha1/project",
- type: "GET",
- dataType: 'json',
- contentType: 'application/json',
- accepts: 'application/json',
- success: function(msg) {
- if(!projectOptions) {
- var numProjects=msg.length;
- for (var i = 0; i < numProjects; ++i)
- projectOptions += "<option value='"+msg[i]['key']+"'>"+msg[i]['name']+"</option>";
- }
- create();
- popup.show();
- $("#"+options.mode+"-repository-projectKey").focus();
- $("form#"+options.mode+"-repository-form select[name='projectKey']").focus();
- f && f();
- },
- error: function(jqXHR) {
- alert("Could not load project list.");
- popup.remove();
- }
- });
- },
- close: function close_repository_popup() {
- popup.remove();
- }
- };
- };
- return {
- add: function bitbucket_create_repository() {
- var addRepositoryPopup = createRepositoryPopup({
- mode: "add",
- save: function() {
- var repository = {
- projectKey: $("#add-repository-form select[name='projectKey']").val(),
- uri: $("#add-repository-form input[name='uri']").val()
- };
- $.ajax({
- url: contextPath + "/rest/bitbucket/1.0/repository",
- data: JSON.stringify(repository),
- type: "POST",
- dataType: 'json',
- contentType: 'application/json',
- accepts: 'application/json',
- success: function(msg) {
- alert("Repository added.");
- addRepositoryPopup.close();
- window.location.reload();
- },
- error: function(jqXHR) {
- alert("Repository not added.");
- addRepositoryPopup.close();
- }
- })
- }
- });
- addRepositoryPopup.open();
- },
- sync: function bitbucket_sync_repository() {
- },
- edit: function bitbucket_edit_repository(id) {
- var editRepositoryPopup = createRepositoryPopup({
- mode: "edit",
- save: function() {
- var repository = {
- projectKey: $("#edit-repository-form select[name='projectKey']").val(),
- uri: $("#edit-repository-form input[name='uri']").val()
- };
- $.ajax({
- url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
- data: JSON.stringify(repository),
- type: "PUT",
- dataType: 'json',
- contentType: 'application/json',
- accepts: 'application/json',
- success: function(msg) {
- alert("Repository updated.");
- editRepositoryPopup.close();
- window.location.reload();
- },
- error: function(jqXHR) {
- alert("Repository not updated.");
- editRepositoryPopup.close();
- }
- })
- }
- });
- loadRepository(id,
- function(repository) {
- editRepositoryPopup.open(function() {
- $("#edit-repository-form select[name='projectKey']").val(repository.projectKey);
- $("#edit-repository-form input[name='uri']").val(repository.uri);
- });
- },
- function(jqXHR) {
- alert("Could not load repository details.");
- }
- );
- },
- remove: function bitbucket_delete_repository(id) {
- loadRepository(id,
- function(repository) {
- var deleteRepositoryPopup =
- new AJS.Dialog({width:500, height:200, id:"delete-repository-dialog", closeOnOutsideClick: false});
- deleteRepositoryPopup.addHeader(AJS.I18n.getText("bitbucket.delete.title"), "dialog-title");
- deleteRepositoryPopup.addPanel(AJS.I18n.getText("bitbucket.delete.title"),
- "<form id='delete-repository-form' class='aui' action=''>" +
- "<fieldset>" +
- "<legend class='assistive'>" + AJS.I18n.getText("bitbucket.delete.description") + "</legend>" +
- "<div class='field-group'><label>" + AJS.I18n.getText("bitbucket.field.projectKey") + ":</label>" + repository.projectKey + "</div>"+
- "<div class='field-group'><label>" + AJS.I18n.getText("bitbucket.field.url") + ":</label>" + repository.uri + "</div>"+
- "</fieldset>" +
- "</form>");
- deleteRepositoryPopup.addButton(AJS.I18n.getText("delete.name"), function() {
- $.ajax({
- url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
- type: "DELETE",
- dataType: 'json',
- contentType: 'application/json',
- accepts: 'application/json',
- success: function(msg) {
- alert("Repository deleted.");
- deleteRepositoryPopup.remove();
- window.location.reload();
- },
- error: function(jqXHR) {
- alert("Repository not deleted.");
- deleteRepositoryPopup.remove();
- }
- })
- });
- deleteRepositoryPopup.addCancel(AJS.I18n.getText("cancel.name"), function() {
- deleteRepositoryPopup.remove();
- });
- deleteRepositoryPopup.show();
- },
- function(jqXHR) {
- alert("Could not load repository details.");
- }
- );
- }
- }
- }(AJS.$);
- AJS.toInit(function($)
- {
- $("#bitbucket-add").click(function(e) {
- e.preventDefault();
- Bitbucket.Repositories.add();
- });
- // $("a.bitbucket-sync").click(function(e) {
- // e.preventDefault();
- // var repository = $(this).parents("tr").find("a.repository");
- // Bitbucket.Repositories.sync(repository.attr("data-project"),repository.attr("data-name"));
- // });
- $("a.bitbucket-edit").click(function(e) {
- e.preventDefault();
- var repository = $(this).parents("tr").find("a.repository");
- Bitbucket.Repositories.edit(repository.attr("data-id"));
- });
- $("a.bitbucket-delete").click(function(e) {
- e.preventDefault();
- var repository = $(this).parents("tr").find("a.repository");
- Bitbucket.Repositories.remove(repository.attr("data-id"));
- });
- });