filter.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. function jVectorMap2GeoIP(cc){
  19. if(cc[0] != "_")
  20. return cc;
  21. else if(cc == "_0")
  22. return "TR"; // Northern Cyprus -> Turkey
  23. else if(cc == "_1")
  24. return "RS"; // Kosovo -> Serbia
  25. else if(cc == "_2")
  26. return "EH"; // Western Sahara <-> Western Sahara
  27. else if(cc == "_3")
  28. return "SO"; // Somaliland -> Somalia
  29. else console.log("Can not map cc '"+cc+"' to GeoIP!");
  30. }
  31. function GeoIP2jVectorMap(cc){
  32. if(cc == "TR")
  33. return ["_0", "TR"]; // Turkey -> Northern Cyprus and Turkey
  34. else if(cc == "RS")
  35. return ["_1", "RS"]; // Serbia -> Kosovo and Serbia
  36. else if(cc == "EH")
  37. return ["_2"]; // Western Sahara <-> Western Sahara
  38. else if(cc == "SO")
  39. return ["_3", "SO"]; // Somalia -> Somaliland and Somalia
  40. else if($.inArray(cc,filter.allCountriesJVectorMap) != -1)
  41. return [cc];
  42. else
  43. return []; // not implemented in jVectorMap
  44. }
  45. /*
  46. * containing all filter options
  47. */
  48. var filter = new function() {
  49. this.initialized = false;
  50. this.disableOnRegionSelected = false;
  51. this.allCountriesJVectorMap = []; // list of all countries from jvectormap, filled later
  52. this.allCountriesGeoIP = Object.keys(countryName); // list of all countries
  53. this.countries = null; // this are the currently selected countries
  54. this.countriesMap = null; // this are the currently selected countries from the map
  55. this.types = null; // stores selected attack types
  56. this.sensors = null; // stores selected sensor types
  57. this.updateCallback = null; // called on each update of a filter selection
  58. this.countrySelectionMap = null; // contains the jvectormap
  59. this.countrySelection; // contains the dropdown for countries
  60. this.authorizedCheckbox; // contains the checkbox
  61. this.attackTypeSelection; // contains the dropdown for attack types
  62. this.sensorTypeSelection; // contains the dropdown for sensor types
  63. this.initialize = function(){
  64. filter.authorizedCheckbox = $('#filterAuthorized');
  65. filter.initCountrySelection();
  66. /* initialize gui */
  67. $("#selectStatsCountries1").click(filter.show);
  68. $("#selectStatsCountries2").click(filter.show);
  69. $("#resetFilter").click(filter.reset);
  70. $("#selectCountriesFinish").click(filter.finish);
  71. filter.initialized = true;
  72. };
  73. this.initCountrySelection = function(){
  74. filter.countrySelectionMap = new jvm.WorldMap({
  75. container: $('#selectCountriesMap'),
  76. map: 'world_mill_en',
  77. backgroundColor: 'navy',
  78. regionsSelectable: true,
  79. // show region label function
  80. onRegionLabelShow: function(e, el, code) {
  81. el.html(el.html());
  82. },
  83. onRegionSelected: filter.clickJVectorMap
  84. });
  85. // resize to prevent the map from showing up tiny
  86. $("#selectCountriesView").on('shown', function(){
  87. $("#selectCountriesView").resize();
  88. });
  89. html = "";
  90. for(var key in countryName){
  91. html += "<option value='"+key+"'>"+countryName[key]+"</option>";
  92. }
  93. $("#selectCountriesSelection").html(html);
  94. filter.countrySelection = $("#selectCountriesSelection").multiselect({
  95. selectedText: "# of # countries selected",
  96. noneSelectedText: "Pick countries manually by clicking on the map or choose them here",
  97. position: {
  98. my: 'left bottom',
  99. at: 'left top'
  100. },
  101. minWidth: 550,
  102. header: false,
  103. click: filter.clickGeoIP
  104. });
  105. // retrieve all countries jvectormap knows
  106. for(var key in filter.countrySelectionMap.regions){
  107. filter.allCountriesJVectorMap.push(key);
  108. }
  109. // select all countries at the beginning
  110. //filter.countries = filter.allCountries;
  111. /* initialize gui */
  112. $("#selectCountriesAll").click(filter.selectAllCountries);
  113. $("#selectCountriesClear").click(filter.clearCountries);
  114. }
  115. // called from main.js on socket event 'setFields'
  116. this.initAttackTypes = function(){
  117. this.attackTypeSelection = $("#filterAttackType").multiselect({
  118. selectedText: "# of # attack types selected",
  119. noneSelectedText: "Filter attack types",
  120. position: {
  121. my: 'left bottom',
  122. at: 'left top'
  123. }
  124. });
  125. };
  126. // called from main.js on socket event 'setSensorTypes'
  127. this.initSensorTypes = function(){
  128. this.sensorTypeSelection = $("#filterSensorType").multiselect({
  129. selectedText: "# of # sensor types selected",
  130. noneSelectedText: "Filter sensor types",
  131. position: {
  132. my: 'left bottom',
  133. at: 'left top'
  134. }
  135. });
  136. };
  137. this.show = function(){
  138. filter.countrySelection.val(filter.countries);
  139. filter.countrySelection.multiselect("refresh");
  140. filter.disableOnRegionSelected = true;
  141. filter.countrySelectionMap.setSelectedRegions(filter.countriesMap);
  142. filter.disableOnRegionSelected = false;
  143. filter.attackTypeSelection.val(filter.types);
  144. filter.attackTypeSelection.multiselect("refresh");
  145. filter.sensorTypeSelection.val(filter.sensors);
  146. filter.sensorTypeSelection.multiselect("refresh");
  147. $("#selectCountriesView").modal("show");
  148. };
  149. this.finish = function(){
  150. filter.countries = filter.countrySelection.val();
  151. filter.countriesMap = filter.countrySelectionMap.getSelectedRegions();
  152. filter.types = filter.attackTypeSelection.val();
  153. filter.sensors = filter.sensorTypeSelection.val();
  154. $("#selectCountriesView").modal("hide");
  155. if(filter.updateCallback)
  156. filter.updateCallback();
  157. else
  158. console.log("filter.updateCallback is null");
  159. };
  160. this.selectAllCountries = function(){
  161. filter.disableOnRegionSelected = true;
  162. filter.countrySelectionMap.setSelectedRegions(filter.allCountriesJVectorMap);
  163. filter.countrySelection.multiselect("checkAll");
  164. filter.disableOnRegionSelected = false;
  165. };
  166. this.clearCountries = function(){
  167. filter.disableOnRegionSelected = true;
  168. filter.countrySelectionMap.clearSelectedRegions();
  169. filter.countrySelection.multiselect("uncheckAll");
  170. filter.disableOnRegionSelected = false;
  171. };
  172. this.reset = function(){
  173. filter.clearCountries();
  174. filter.attackTypeSelection.multiselect("uncheckAll");
  175. filter.sensorTypeSelection.multiselect("uncheckAll");
  176. filter.countrySelection.multiselect("uncheckAll");
  177. filter.authorizedCheckbox.prop("checked", false);
  178. };
  179. this.clickJVectorMap = function(e, cc, isSelected, selectedRegions){
  180. if(filter.disableOnRegionSelected)
  181. return;
  182. var geoipcc = jVectorMap2GeoIP(cc);
  183. filter.updateJVectorMap(geoipcc, isSelected);
  184. filter.updateGeoIP(geoipcc, isSelected);
  185. };
  186. this.clickGeoIP = function(e, ui){
  187. filter.updateJVectorMap(ui.value, ui.checked);
  188. };
  189. this.updateJVectorMap = function(cc, isSelected){
  190. var jVectorMapCCs = GeoIP2jVectorMap(cc);
  191. if(jVectorMapCCs && jVectorMapCCs.length > 0){
  192. var jVectorMapObj = {};
  193. for(var i = 0; i < jVectorMapCCs.length; i++){
  194. var _cc = jVectorMapCCs[i];
  195. jVectorMapObj[_cc] = isSelected;
  196. }
  197. filter.countrySelectionMap.setSelectedRegions(jVectorMapObj);
  198. }
  199. };
  200. this.updateGeoIP = function(cc, isSelected){
  201. var val = filter.countrySelection.val();
  202. if(!val) val = [];
  203. if(isSelected){
  204. val.push(cc);
  205. }
  206. else{
  207. var index = $.inArray(cc, val);
  208. if(index != -1)
  209. val.splice(index, 1);
  210. }
  211. filter.countrySelection.val(val);
  212. filter.countrySelection.multiselect("refresh");
  213. }
  214. this.load = function(cb){
  215. if(!filter.initialized){
  216. setTimeout(function(){filter.load(cb);}, 50); // retry in 50ms
  217. return;
  218. }
  219. if(cb){
  220. filter.updateCallback = cb;
  221. }
  222. else{
  223. filter.updateCallback = null;
  224. }
  225. };
  226. this.unload = function(){
  227. if(!filter.initialized){
  228. setTimeout(filter.unload, 50); // retry in 50ms
  229. return;
  230. }
  231. filter.updateCallback = null;
  232. };
  233. this.getFilter = function(){
  234. var obj = {};
  235. // filter for only authorized when box is checked
  236. if(filter.authorizedCheckbox && filter.authorizedCheckbox.prop('checked'))
  237. obj["authorized"] = true;
  238. if(filter.types && filter.types.length > 0)
  239. obj["types"] = filter.types;
  240. if(filter.sensors && filter.sensors.length > 0)
  241. obj["sensors"] = filter.sensors;
  242. // do not filter countries if none or all are selected
  243. if(filter.countries && filter.countries.length != 0 && filter.countries.length != filter.allCountriesGeoIP.length)
  244. obj["countries"] = filter.countries;
  245. return obj;
  246. };
  247. };
  248. $(function(){
  249. filter.initialize();
  250. });