|
1 |
| -$( document ).ready(function() { |
2 |
| - $('.badge').click(function(){ |
3 |
| - $('.badge-markdown').toggle(); |
| 1 | +$(document).ready(function() { |
| 2 | + $(".pact-badge").click(function() { |
| 3 | + $(".pact-badge-markdown").toggle(); |
4 | 4 | });
|
| 5 | + |
| 6 | + $(".more-options") |
| 7 | + .materialMenu("init", { |
| 8 | + position: "overlay", |
| 9 | + animationSpeed: 1, |
| 10 | + items: [ |
| 11 | + // { |
| 12 | + // type: "normal", |
| 13 | + // text: "View in API Browser", |
| 14 | + // click: function(openMenuElement) { |
| 15 | + // window.location.href = openMenuElement.data().apiBrowserUrl; |
| 16 | + // } |
| 17 | + // }, |
| 18 | + // { |
| 19 | + // type: "normal", |
| 20 | + // text: "View Matrix", |
| 21 | + // click: function(openMenuElement) { |
| 22 | + // window.location.href = openMenuElement.data().matrixUrl; |
| 23 | + // } |
| 24 | + // }, |
| 25 | + { |
| 26 | + type: "normal", |
| 27 | + text: "Delete ...", |
| 28 | + click: function(openMenuElement) { |
| 29 | + promptToDeleteResource(openMenuElement.data().pactUrl, createDeletionConfirmationText(openMenuElement.data())) |
| 30 | + } |
| 31 | + } |
| 32 | + ] |
| 33 | + }) |
| 34 | + .click(function() { |
| 35 | + $(this).materialMenu("open"); |
| 36 | + }); |
5 | 37 | });
|
| 38 | + |
| 39 | +function createDeletionConfirmationText(data) { |
| 40 | + return `Do you wish to delete the pact for version ${data.consumerVersionNumber} of ${data.consumerName}?`; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +function confirmDeleteResource( |
| 45 | + confirmationText, |
| 46 | + confirmCallbak, |
| 47 | + cancelCallback |
| 48 | +) { |
| 49 | + $.confirm({ |
| 50 | + title: "Confirm!", |
| 51 | + content: confirmationText, |
| 52 | + buttons: { |
| 53 | + delete: { |
| 54 | + text: "DELETE", |
| 55 | + btnClass: "alert alert-danger", |
| 56 | + keys: ["enter", "shift"], |
| 57 | + action: confirmCallbak |
| 58 | + }, |
| 59 | + cancel: cancelCallback |
| 60 | + } |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function promptToDeleteResource(deletionUrl, confirmationText) { |
| 65 | + const cancel = function() {}; |
| 66 | + const confirm = function() { |
| 67 | + deleteResource( |
| 68 | + deletionUrl, |
| 69 | + handleDeletionSuccess, |
| 70 | + handleDeletionFailure |
| 71 | + ); |
| 72 | + }; |
| 73 | + |
| 74 | + confirmDeleteResource( |
| 75 | + confirmationText, |
| 76 | + confirm, |
| 77 | + cancel |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +function handleDeletionSuccess(responseBody) { |
| 82 | + if(responseBody._links['pb:latest-pact-version']) { |
| 83 | + $.confirm({ |
| 84 | + title: "Pact deleted", |
| 85 | + content: "Where to next?", |
| 86 | + buttons: { |
| 87 | + latest: { |
| 88 | + text: "Latest pact", |
| 89 | + keys: ["enter", "shift"], |
| 90 | + action: function() { window.location.href = responseBody._links['pb:latest-pact-version'].href } |
| 91 | + }, |
| 92 | + home: { |
| 93 | + text: "Home", |
| 94 | + action: function() { window.location.href = "/"; } |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + } else { |
| 100 | + window.location.href = "/"; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function handleDeletionFailure(response) { |
| 105 | + let errorMessage = null; |
| 106 | + |
| 107 | + if (response.error && response.error.message && response.error.reference) { |
| 108 | + errorMessage = |
| 109 | + "<p>Could not delete resources due to error: " + |
| 110 | + response.error.message + |
| 111 | + "</p><p>Error reference: " + |
| 112 | + response.error.reference + "</p>"; |
| 113 | + } else { |
| 114 | + errorMessage = |
| 115 | + "Could not delete resources due to error: " + JSON.stringify(response); |
| 116 | + } |
| 117 | + |
| 118 | + $.alert({ |
| 119 | + title: 'Error', |
| 120 | + content: errorMessage, |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +function deleteResource(url, successCallback, errorCallback) { |
| 125 | + $.ajax({ |
| 126 | + url: url, |
| 127 | + dataType: "json", |
| 128 | + type: "delete", |
| 129 | + accepts: { |
| 130 | + text: "application/hal+json" |
| 131 | + }, |
| 132 | + success: function(data, textStatus, jQxhr) { |
| 133 | + successCallback(data); |
| 134 | + }, |
| 135 | + error: function(jqXhr, textStatus, errorThrown) { |
| 136 | + errorCallback(jqXhr.responseJSON); |
| 137 | + } |
| 138 | + }); |
| 139 | +} |
0 commit comments