|
| 1 | +var canvasHoverController = (function() { |
| 2 | + |
| 3 | + var isInNavigation = false; |
| 4 | + |
| 5 | + function initialize(setupConfig){ |
| 6 | + application.transferConfigParams(setupConfig, controllerConfig); |
| 7 | + var cssLink = document.createElement("link"); |
| 8 | + cssLink.type = "text/css"; |
| 9 | + cssLink.rel = "stylesheet"; |
| 10 | + cssLink.href = "scripts/CanvasHover/ho.css"; |
| 11 | + document.getElementsByTagName("head")[0].appendChild(cssLink); |
| 12 | + } |
| 13 | + |
| 14 | + //config parameters |
| 15 | + var controllerConfig = { |
| 16 | + hoverColor: "darkred", |
| 17 | + showQualifiedName : false, |
| 18 | + showVersion : false, |
| 19 | + showIssues : false |
| 20 | + }; |
| 21 | + |
| 22 | + function activate(){ |
| 23 | + |
| 24 | + actionController.actions.mouse.hover.subscribe(handleOnMouseEnter); |
| 25 | + actionController.actions.mouse.unhover.subscribe(handleOnMouseLeave); |
| 26 | + |
| 27 | + createTooltipContainer(); |
| 28 | + |
| 29 | + events.hovered.on.subscribe(onEntityHover); |
| 30 | + events.hovered.off.subscribe(onEntityUnhover); |
| 31 | + } |
| 32 | + |
| 33 | + function reset(){ |
| 34 | + var hoveredEntities = events.hovered.getEntities(); |
| 35 | + |
| 36 | + hoveredEntities.forEach(function(hoveredEntity){ |
| 37 | + var unHoverEvent = { |
| 38 | + sender: canvasHoverController, |
| 39 | + entities: [hoveredEntity] |
| 40 | + }; |
| 41 | + |
| 42 | + events.hovered.off.publish(unHoverEvent); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + function createTooltipContainer(){ |
| 47 | + |
| 48 | + var canvas = document.getElementById("canvas"); |
| 49 | + |
| 50 | + var tooltipDivElement = document.createElement("DIV"); |
| 51 | + tooltipDivElement.id = "tooltip"; |
| 52 | + |
| 53 | + var namePElement = document.createElement("P"); |
| 54 | + namePElement.id = "tooltipName"; |
| 55 | + tooltipDivElement.appendChild(namePElement); |
| 56 | + |
| 57 | + if(controllerConfig.showQualifiedName) { |
| 58 | + var qualifiedNamePElement = document.createElement("P"); |
| 59 | + qualifiedNamePElement.id = "tooltipQualifiedName"; |
| 60 | + tooltipDivElement.appendChild(qualifiedNamePElement); |
| 61 | + } |
| 62 | + |
| 63 | + if(controllerConfig.showVersion) { |
| 64 | + var versionPElement = document.createElement("P"); |
| 65 | + versionPElement.id = "tooltipVersion"; |
| 66 | + tooltipDivElement.appendChild(versionPElement); |
| 67 | + } |
| 68 | + if(controllerConfig.showIssues) { |
| 69 | + var openIssuesPElement = document.createElement("P"); |
| 70 | + openIssuesPElement.id = "tooltipOpenIssues"; |
| 71 | + tooltipDivElement.appendChild((openIssuesPElement)); |
| 72 | + |
| 73 | + var closedIssuesPElement = document.createElement("P"); |
| 74 | + closedIssuesPElement.id = "tooltipClosedIssues"; |
| 75 | + tooltipDivElement.appendChild((closedIssuesPElement)); |
| 76 | + |
| 77 | + var openSecurityIssuesPElement = document.createElement("P"); |
| 78 | + openSecurityIssuesPElement.id = "tooltipOpenSecurityIssues"; |
| 79 | + tooltipDivElement.appendChild((openSecurityIssuesPElement)); |
| 80 | + |
| 81 | + var closedSecurityIssuesPElement = document.createElement("P"); |
| 82 | + closedSecurityIssuesPElement.id = "tooltipClosedSecurityIssues"; |
| 83 | + tooltipDivElement.appendChild((closedSecurityIssuesPElement)); |
| 84 | + } |
| 85 | + canvas.appendChild(tooltipDivElement); |
| 86 | + } |
| 87 | + |
| 88 | + function handleOnMousedown(canvasEvent) { |
| 89 | + isInNavigation = true; |
| 90 | + } |
| 91 | + |
| 92 | + function handleOnMouseup(canvasEvent) { |
| 93 | + isInNavigation = false; |
| 94 | + } |
| 95 | + |
| 96 | + function handleOnMouseEnter(eventObject) { |
| 97 | + if(isInNavigation){ |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + var entity = model.getEntityById(eventObject.target.id); |
| 102 | + if(entity === undefined){ |
| 103 | + entity = eventObject.target.id; |
| 104 | + events.log.error.publish({ text: "Entity of partID " + multipartEvent.partID + " not in model data."}); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + var applicationEvent = { |
| 109 | + sender : canvasHoverController, |
| 110 | + entities : [entity], |
| 111 | + /*posX : multipartEvent.layerX, |
| 112 | + posY : multipartEvent.layerY*/ |
| 113 | + }; |
| 114 | + events.hovered.on.publish(applicationEvent); |
| 115 | + } |
| 116 | + |
| 117 | + function handleOnMouseLeave(eventObject) { |
| 118 | + var entity = model.getEntityById(eventObject.target.id); |
| 119 | + if(entity === undefined){ |
| 120 | + events.log.error.publish({ text: "Entity of partID " + multipartEvent.partID + " not in model data."}); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + var applicationEvent = { |
| 125 | + sender : canvasHoverController, |
| 126 | + entities : [entity] |
| 127 | + }; |
| 128 | + |
| 129 | + events.hovered.off.publish(applicationEvent); |
| 130 | + } |
| 131 | + |
| 132 | + function onEntityHover(applicationEvent) { |
| 133 | + var entity = applicationEvent.entities[0]; |
| 134 | + |
| 135 | + if(entity === undefined){ |
| 136 | + events.log.error.publish({ text: "Entity is not defined"}); |
| 137 | + } |
| 138 | + |
| 139 | + if(entity.isTransparent === true) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + if(entity.type === "text"){ |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if(entity.marked && entity.selected){ |
| 148 | + canvasManipulator.unhighlightEntities([entity]); |
| 149 | + } else { |
| 150 | + canvasManipulator.highlightEntities([entity], controllerConfig.hoverColor); |
| 151 | + } |
| 152 | + |
| 153 | + $("#tooltipName").text(getTooltipName(entity)); |
| 154 | + if(controllerConfig.showQualifiedName) { |
| 155 | + $("#tooltipQualifiedName").text(entity.qualifiedName); |
| 156 | + } |
| 157 | + if(controllerConfig.showVersion) { |
| 158 | + $("#tooltipVersion").text("Version: " + entity.version); |
| 159 | + } |
| 160 | + if(controllerConfig.showIssues) { |
| 161 | + let openIssuesSelector = $('#tooltipOpenIssues'); |
| 162 | + let closedIssuesSelector = $('#tooltipClosedIssues'); |
| 163 | + let openSecurityIssuesSelector = $('#tooltipOpenSecurityIssues'); |
| 164 | + let closedSecurityIssuesSelector = $('#tooltipClosedSecurityIssues'); |
| 165 | + if (entity.type === "Namespace") { |
| 166 | + openIssuesSelector.css("display", "none"); |
| 167 | + closedIssuesSelector.css("display", "none"); |
| 168 | + openSecurityIssuesSelector.css("display", "none"); |
| 169 | + closedSecurityIssuesSelector.css("display", "none"); |
| 170 | + } else { |
| 171 | + openIssuesSelector.text("Open Issues: " + entity.numberOfOpenIssues); |
| 172 | + closedIssuesSelector.text("Closed Issues: " + entity.numberOfClosedIssues); |
| 173 | + openSecurityIssuesSelector.text("Open Security Issues: " + entity.numberOfOpenSecurityIssues); |
| 174 | + closedSecurityIssuesSelector.text("Closed Security Issues: " + entity.numberOfClosedSecurityIssues); |
| 175 | + openIssuesSelector.css("display", "block"); |
| 176 | + closedIssuesSelector.css("display", "block"); |
| 177 | + openSecurityIssuesSelector.css("display", "block"); |
| 178 | + closedSecurityIssuesSelector.css("display", "block"); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + var tooltip = $("#tooltip"); |
| 183 | + tooltip.css("top", applicationEvent.posY + 50 + "px"); |
| 184 | + tooltip.css("left", applicationEvent.posX + 50 + "px"); |
| 185 | + tooltip.css("display", "block"); |
| 186 | + } |
| 187 | + |
| 188 | + function onEntityUnhover(applicationEvent) { |
| 189 | + var entity = applicationEvent.entities[0]; |
| 190 | + |
| 191 | + if(entity.marked && entity.selected){ |
| 192 | + canvasManipulator.highlightEntities([entity], controllerConfig.hoverColor); |
| 193 | + } else { |
| 194 | + if(!entity.selected){ |
| 195 | + canvasManipulator.unhighlightEntities([entity]); |
| 196 | + } |
| 197 | + if(entity.type === "Namespace"){ |
| 198 | + canvasManipulator.unhighlightEntities([entity]); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + $("#tooltip").css("display", "none"); |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + function getTooltipName(entity) { |
| 207 | + if(entity.type === "Method") { |
| 208 | + return entity.type + ": " + entity.signature; |
| 209 | + } |
| 210 | + |
| 211 | + if (entity.type === "Namespace") { |
| 212 | + return "Package: " + entity.name; |
| 213 | + } |
| 214 | + |
| 215 | + return entity.type + ": " + entity.name; |
| 216 | + } |
| 217 | + |
| 218 | + return { |
| 219 | + initialize: initialize, |
| 220 | + activate: activate, |
| 221 | + reset: reset, |
| 222 | + handleOnMouseEnter: handleOnMouseEnter, |
| 223 | + handleOnMouseLeave: handleOnMouseLeave |
| 224 | + }; |
| 225 | +})(); |
0 commit comments