menu.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var help = false; // states whether help is active
  19. var live = true; // states whether live or database view is active
  20. var trigger = "hover"; // states whether the help popovers will show on hover (or click -> on click|focus -> on focus)
  21. var currentView = 0;
  22. var VIEWS = {
  23. MAP: 1,
  24. STREETMAP: 2,
  25. GLOBE: 3,
  26. TABLE: 4,
  27. STATISTICS: 5
  28. };
  29. /*
  30. set content of leftContent to div with given id and hide other divs
  31. */
  32. function setLeftContent(id) {
  33. $("#leftWin").children("div").each(function(n, el) { // for each direct children
  34. if(el.id == id)
  35. $(el).show();
  36. else
  37. $(el).hide();
  38. }
  39. );
  40. }
  41. function updateMainWidth() {
  42. $("[LeftWinAdd], [LeftWinRemove]").each(function(i, el) {
  43. var leftWinAdd = $(el).attr("LeftWinAdd");
  44. var leftWinRemove = $(el).attr("LeftWinRemove");
  45. if ($("#leftWin").is(":visible")) {
  46. // remove center margin of divs
  47. toggleCenterDivs();
  48. // remove class we should remove and add the class we should add
  49. $(el).removeClass(leftWinRemove);
  50. $(el).addClass(leftWinAdd);
  51. } else {
  52. // reverse above
  53. $(el).removeClass(leftWinAdd);
  54. $(el).addClass(leftWinRemove);
  55. toggleCenterDivs();
  56. }
  57. });
  58. }
  59. function updateWins(leftContent, leftVisible, toggleVisible, filterUpdateCallback) {
  60. setLeftContent(leftContent);
  61. if (leftVisible)
  62. $("#leftWin").show();
  63. else
  64. $("#leftWin").hide();
  65. if (toggleVisible) {
  66. //$("#toggleView").show();
  67. $("#liveView").removeClass("disabled");
  68. $("#dbView").removeClass("disabled");
  69. } else {
  70. //$("#toggleView").hide();
  71. $("#liveView").addClass("disabled");
  72. $("#dbView").addClass("disabled");
  73. }
  74. // TODO: was geschickteres überlegen als an zwei stellen den filter callback für die Datenbankansicht zu setzen
  75. if(filterUpdateCallback){
  76. filter.load(filterUpdateCallback);
  77. }
  78. else{
  79. filter.unload();
  80. }
  81. updateMainWidth();
  82. }
  83. // show extra borders for elements if help is active to be able to see which elements to hover
  84. function updateHelpElements() {
  85. $("[onHelpActive]").each(function(i, el) {
  86. var onHelp = $(el).attr("onHelpActive");
  87. if (help) {
  88. // add the class we should add
  89. $(el).addClass(onHelp);
  90. } else {
  91. // reverse above
  92. $(el).removeClass(onHelp);
  93. }
  94. });
  95. }
  96. /*
  97. * menu functionality (navbar and toggle buttons)
  98. * includes: make navbar buttons toggleable (except help and about as they do not (yet) change the side content)
  99. */
  100. function updateMenu(tab){
  101. // "2D View" entry chosen via hash, show 2D map
  102. if (tab == 'map') {
  103. currentView = VIEWS.MAP;
  104. world.showMap();
  105. // if there is no attack data show an info alert
  106. if (!world.jvmHasMarker())
  107. showInfoNoData();
  108. // if there is attack data remove the info alert
  109. else
  110. $("#tableWaitingAlert").remove();
  111. // set help if active
  112. if (help)
  113. $("#helpEntry").addClass("active");
  114. // resize map before showing it to prevent it from showing up tiny
  115. $("#map").resize();
  116. // set left window and toogleLive
  117. updateWins("dbWin", !live, true, !live && requestAttackUpdate);
  118. $("#advMarkerInfo").removeClass("disabled");
  119. $("#resetMap").removeClass("disabled");
  120. }
  121. // "Street View" entry chosen via hash, show streetmap
  122. else if (tab == 'streetmap') {
  123. currentView = VIEWS.STREETMAP;
  124. world.showStreetmap();
  125. // if there is no attack data show an info alert
  126. if (!world.stMapHasMarker())
  127. showInfoNoData();
  128. // if there is attack data remove the info alert
  129. else
  130. $("#tableWaitingAlert").remove();
  131. if (help)
  132. $("#helpEntry").addClass("active");
  133. updateWins("dbWin", !live, true, !live && requestAttackUpdate);
  134. $("#advMarkerInfo").removeClass("disabled");
  135. $("#resetMap").removeClass("disabled");
  136. }
  137. // "3D View" entry chosen via hash, show 3D map
  138. else if (tab == 'globe') {
  139. currentView = VIEWS.GLOBE;
  140. world.showGlobe();
  141. // if there is no attack data show an info alert
  142. if (!world.globeHasMarker())
  143. showInfoNoData();
  144. // if there is attack data remove the info alert
  145. else
  146. $("#tableWaitingAlert").remove();
  147. if (help)
  148. $("#helpEntry").addClass("active");
  149. updateWins("dbWin", !live, true, !live && requestAttackUpdate);
  150. $("#advMarkerInfo").removeClass("disabled");
  151. $("#resetMap").removeClass("disabled");
  152. }
  153. // "Table View" entry chosen via hash, show table
  154. else if (tab == 'table') {
  155. currentView = VIEWS.TABLE;
  156. world.leaveMap();
  157. if (help)
  158. $("#helpEntry").addClass("active");
  159. // if there is no attack data show an info alert
  160. if ($("#table .dataTables_empty").length > 0)
  161. showInfoNoData();
  162. // if there is attack data remove the info alert
  163. else
  164. $("#tableWaitingAlert").remove();
  165. updateWins("dbWin", !live, true, !live && requestAttackUpdate);
  166. $("#advMarkerInfo").addClass("disabled");
  167. $("#resetMap").removeClass("disabled");
  168. world.resize();
  169. }
  170. // "Statistics" entry chosen via hash, show statistics
  171. else if (tab == 'stats') {
  172. currentView = VIEWS.STATISTICS;
  173. world.leaveMap();
  174. // remove alert
  175. $("#tableWaitingAlert").remove();
  176. if (help)
  177. $("#helpEntry").addClass("active");
  178. updateWins("statsWin", true, false, filterUpdateStats);
  179. var chartsCreated = createCharts();
  180. // reload charts, the filter could have been changed on another tab
  181. if (!chartsCreated)
  182. filterUpdateStats();
  183. $("#advMarkerInfo").addClass("disabled");
  184. $("#resetMap").addClass("disabled");
  185. }
  186. else console.log('menue: unknown tab: ' + tab);
  187. };
  188. $(function () {
  189. // "Help" entry chosen in menu -> toggle entry tab, show help popovers
  190. $('#helpEntry a').click(function (e) {
  191. e.preventDefault();
  192. if (help) {
  193. help = false;
  194. $("#helpEntry").removeClass("active");
  195. hideHelpPopovers();
  196. } else {
  197. help = true;
  198. $("#helpEntry").addClass("active");
  199. showHelpPopovers();
  200. $('#helpEntry').popover('show');
  201. }
  202. updateHelpElements();
  203. });
  204. // "Live View" entry chosen in toggle button menu -> switch to live view, hide side/database window
  205. $('#liveView').click(function (e) {
  206. if (!$(this).hasClass("disabled")) {
  207. var settings = $("#attackTable").dataTable().fnSettings();
  208. settings.oFeatures.bServerSide = false;
  209. $("#attackTable").dataTable()._fnClearTable(settings);
  210. live = true;
  211. $(leftWin).hide();
  212. setLeftContent("dbWin");
  213. filter.unload();
  214. updateMainWidth();
  215. if (help)
  216. showHelpPopovers();
  217. // stop loading data if loading is in progress
  218. world.finishLoading(false);
  219. $("#liveView").addClass("active");
  220. $("#dbView").removeClass("active");
  221. world.resize();
  222. }
  223. });
  224. // "Database View" entry chosen in toggle button menu -> switch to database view, show side/database window
  225. $('#dbView').click(function (e) {
  226. if (!$(this).hasClass("disabled")) {
  227. live = false;
  228. $(leftWin).show();
  229. setLeftContent("dbWin");
  230. filter.load(requestAttackUpdate);
  231. updateMainWidth();
  232. if (help)
  233. showHelpPopovers();
  234. $("#dbView").addClass("active");
  235. $("#liveView").removeClass("active");
  236. world.resize();
  237. }
  238. });
  239. // "Advanced Marker Information" entry chosen in menu -> show more information on hover over markers
  240. $('#advMarkerInfo').click(function (e) {
  241. if (!$(this).hasClass("disabled")) {
  242. if (advInfo == true) {
  243. advInfo = false;
  244. $("#advMarkerInfo").removeClass("active");
  245. } else {
  246. advInfo = true;
  247. $("#advMarkerInfo").addClass("active");
  248. }
  249. }
  250. });
  251. $('#resetMap').click(function (e) {
  252. if (!$(this).hasClass("disabled")) {
  253. refreshMap();
  254. }
  255. });
  256. });
  257. // show "no data" alert
  258. function showInfoNoData() {
  259. // show an alert if none exists
  260. if (!$("#tableWaitingAlert").length > 0) {
  261. showalert("tableWaitingAlert", "Waiting for data...", "Once attacks occur or are requested in the dabase view they will show up below. To display attacks on both maps and the globe you need to load them separately (i.e. click their menu entry once). Otherwise they will stay empty even if attacks occur until you load them.", "info", false);
  262. }
  263. }
  264. // reset map and table
  265. function refreshMap() {
  266. // show "no data" alert if map, globe or table is active
  267. if (!$("#stats").is(":visible"))
  268. showInfoNoData();
  269. // reset maps and table
  270. world.reset();
  271. world.resetTable();
  272. }
  273. // allow keyboard control in about view (modal)
  274. // based on http://stackoverflow.com/questions/16718443/use-keyboard-to-scroll-in-twitters-bootstrap-2-x-modal-popup
  275. $(function () {
  276. $('#aboutView').keydown(function(e) {
  277. // scroll up and down in the modal
  278. var speed = 50; // scroll speed
  279. if (e.keyCode == 40) {
  280. e.preventDefault();
  281. $('.modal-body').scrollTop($('.modal-body').scrollTop() + speed);
  282. }
  283. if (e.keyCode == 38) {
  284. e.preventDefault();
  285. $('.modal-body').scrollTop($('.modal-body').scrollTop() - speed);
  286. }
  287. });
  288. // Disable key control of the maps/globe if modal is active
  289. $('#aboutView').on('show', function(e) {world.toggleEnabled();});
  290. $('#aboutView').on('hide', function(e) {world.toggleEnabled();});
  291. });