PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/bitbucket/src/main/resources/js/bitbucket.js

https://bitbucket.org/mjensen/jira-bitbucket-connector
JavaScript | 256 lines | 211 code | 40 blank | 5 comment | 3 complexity | 66ca8500975c41138195c39413e97dc1 MD5 | raw file
  1. Bitbucket = {};
  2. Bitbucket.Repositories = function($)
  3. {
  4. var projectOptions;
  5. var loadRepository = function(id, success, error) {
  6. $.ajax({
  7. url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
  8. type: "GET",
  9. contentType: 'application/json',
  10. accepts: 'application/json',
  11. success: success,
  12. error: error
  13. });
  14. };
  15. var createRepositoryPopup = function(o) {
  16. var changeTimeoutId;
  17. var popup;
  18. var options = o;
  19. var defaults = {
  20. mode: "add",
  21. save: function() {}
  22. };
  23. $.extend({}, defaults, options);
  24. var create = function create_repository_popup() {
  25. popup = new AJS.Dialog({width:500, height:180, id: options.mode+"-repository-dialog",
  26. closeOnOutsideClick: false});
  27. popup.addPage("page-start");
  28. popup.gotoPage(0);
  29. popup.addHeader(AJS.I18n.getText("bitbucket."+options.mode+".title"), "dialog-title");
  30. popup.addPanel(AJS.I18n.getText("bitbucket."+options.mode+".title"),
  31. "<form id='"+options.mode+"-repository-form' class='aui' action=''>" +
  32. "<input name='id' type='hidden'>"+
  33. "<fieldset>" +
  34. "<legend class='assistive'>" + AJS.I18n.getText("bitbucket."+options.mode+".description") + "</legend>" +
  35. "<div class='field-group'><label for='projectKey'>" + AJS.I18n.getText("bitbucket.field.projectKey") + ":</label>" +
  36. "<select name='projectKey' size='1'>"+projectOptions+"</select></div>" +
  37. "<div class='field-group'><label for='uri'>" + AJS.I18n.getText("bitbucket.field.uri") + ":</label>" +
  38. "<input name='uri' size='30'></div>" +
  39. "</fieldset>" +
  40. "</form>");
  41. popup.addButton(AJS.I18n.getText("save.name"), function() {
  42. options.save();
  43. }, options.mode + "-repository-start-next-button");
  44. popup.addCancel(AJS.I18n.getText("cancel.name"), function() {
  45. popup.remove();
  46. });
  47. popup.gotoPage(0);
  48. };
  49. return {
  50. open: function open_repository_popup(f) {
  51. $.ajax({
  52. url: contextPath + "/rest/api/2.0.alpha1/project",
  53. type: "GET",
  54. dataType: 'json',
  55. contentType: 'application/json',
  56. accepts: 'application/json',
  57. success: function(msg) {
  58. if(!projectOptions) {
  59. var numProjects=msg.length;
  60. for (var i = 0; i < numProjects; ++i)
  61. projectOptions += "<option value='"+msg[i]['key']+"'>"+msg[i]['name']+"</option>";
  62. }
  63. create();
  64. popup.show();
  65. $("#"+options.mode+"-repository-projectKey").focus();
  66. $("form#"+options.mode+"-repository-form select[name='projectKey']").focus();
  67. f && f();
  68. },
  69. error: function(jqXHR) {
  70. alert("Could not load project list.");
  71. popup.remove();
  72. }
  73. });
  74. },
  75. close: function close_repository_popup() {
  76. popup.remove();
  77. }
  78. };
  79. };
  80. return {
  81. add: function bitbucket_create_repository() {
  82. var addRepositoryPopup = createRepositoryPopup({
  83. mode: "add",
  84. save: function() {
  85. var repository = {
  86. projectKey: $("#add-repository-form select[name='projectKey']").val(),
  87. uri: $("#add-repository-form input[name='uri']").val()
  88. };
  89. $.ajax({
  90. url: contextPath + "/rest/bitbucket/1.0/repository",
  91. data: JSON.stringify(repository),
  92. type: "POST",
  93. dataType: 'json',
  94. contentType: 'application/json',
  95. accepts: 'application/json',
  96. success: function(msg) {
  97. alert("Repository added.");
  98. addRepositoryPopup.close();
  99. window.location.reload();
  100. },
  101. error: function(jqXHR) {
  102. alert("Repository not added.");
  103. addRepositoryPopup.close();
  104. }
  105. })
  106. }
  107. });
  108. addRepositoryPopup.open();
  109. },
  110. sync: function bitbucket_sync_repository() {
  111. },
  112. edit: function bitbucket_edit_repository(id) {
  113. var editRepositoryPopup = createRepositoryPopup({
  114. mode: "edit",
  115. save: function() {
  116. var repository = {
  117. projectKey: $("#edit-repository-form select[name='projectKey']").val(),
  118. uri: $("#edit-repository-form input[name='uri']").val()
  119. };
  120. $.ajax({
  121. url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
  122. data: JSON.stringify(repository),
  123. type: "PUT",
  124. dataType: 'json',
  125. contentType: 'application/json',
  126. accepts: 'application/json',
  127. success: function(msg) {
  128. alert("Repository updated.");
  129. editRepositoryPopup.close();
  130. window.location.reload();
  131. },
  132. error: function(jqXHR) {
  133. alert("Repository not updated.");
  134. editRepositoryPopup.close();
  135. }
  136. })
  137. }
  138. });
  139. loadRepository(id,
  140. function(repository) {
  141. editRepositoryPopup.open(function() {
  142. $("#edit-repository-form select[name='projectKey']").val(repository.projectKey);
  143. $("#edit-repository-form input[name='uri']").val(repository.uri);
  144. });
  145. },
  146. function(jqXHR) {
  147. alert("Could not load repository details.");
  148. }
  149. );
  150. },
  151. remove: function bitbucket_delete_repository(id) {
  152. loadRepository(id,
  153. function(repository) {
  154. var deleteRepositoryPopup =
  155. new AJS.Dialog({width:500, height:200, id:"delete-repository-dialog", closeOnOutsideClick: false});
  156. deleteRepositoryPopup.addHeader(AJS.I18n.getText("bitbucket.delete.title"), "dialog-title");
  157. deleteRepositoryPopup.addPanel(AJS.I18n.getText("bitbucket.delete.title"),
  158. "<form id='delete-repository-form' class='aui' action=''>" +
  159. "<fieldset>" +
  160. "<legend class='assistive'>" + AJS.I18n.getText("bitbucket.delete.description") + "</legend>" +
  161. "<div class='field-group'><label>" + AJS.I18n.getText("bitbucket.field.projectKey") + ":</label>" + repository.projectKey + "</div>"+
  162. "<div class='field-group'><label>" + AJS.I18n.getText("bitbucket.field.url") + ":</label>" + repository.uri + "</div>"+
  163. "</fieldset>" +
  164. "</form>");
  165. deleteRepositoryPopup.addButton(AJS.I18n.getText("delete.name"), function() {
  166. $.ajax({
  167. url: contextPath + "/rest/bitbucket/1.0/repository/"+id,
  168. type: "DELETE",
  169. dataType: 'json',
  170. contentType: 'application/json',
  171. accepts: 'application/json',
  172. success: function(msg) {
  173. alert("Repository deleted.");
  174. deleteRepositoryPopup.remove();
  175. window.location.reload();
  176. },
  177. error: function(jqXHR) {
  178. alert("Repository not deleted.");
  179. deleteRepositoryPopup.remove();
  180. }
  181. })
  182. });
  183. deleteRepositoryPopup.addCancel(AJS.I18n.getText("cancel.name"), function() {
  184. deleteRepositoryPopup.remove();
  185. });
  186. deleteRepositoryPopup.show();
  187. },
  188. function(jqXHR) {
  189. alert("Could not load repository details.");
  190. }
  191. );
  192. }
  193. }
  194. }(AJS.$);
  195. AJS.toInit(function($)
  196. {
  197. $("#bitbucket-add").click(function(e) {
  198. e.preventDefault();
  199. Bitbucket.Repositories.add();
  200. });
  201. // $("a.bitbucket-sync").click(function(e) {
  202. // e.preventDefault();
  203. // var repository = $(this).parents("tr").find("a.repository");
  204. // Bitbucket.Repositories.sync(repository.attr("data-project"),repository.attr("data-name"));
  205. // });
  206. $("a.bitbucket-edit").click(function(e) {
  207. e.preventDefault();
  208. var repository = $(this).parents("tr").find("a.repository");
  209. Bitbucket.Repositories.edit(repository.attr("data-id"));
  210. });
  211. $("a.bitbucket-delete").click(function(e) {
  212. e.preventDefault();
  213. var repository = $(this).parents("tr").find("a.repository");
  214. Bitbucket.Repositories.remove(repository.attr("data-id"));
  215. });
  216. });