File: datatable-ipsorting.js

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (105 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * This is a pluginf for the Spyrmedia Datatables.
 * This plugins sort the columns containing IP Addresses.
 * Thanks to: https://datatables.net/plug-ins/sorting/ip-address
 */
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "ip-address-pre": function ( a ) {
        var i, item;
        var m, n, t;
        var x, xa;

        if (!a) {
            return 0
        }

        a = a.replace(/<[\s\S]*?>/g, "");
        //IPv4:Port
                t = a.split(":");
                if (t.length == 2){
                        m = t[0].split(".");
                }
                else {
                        m = a.split(".");
                }
        n = a.split(":");
        x = "";
        xa = "";

        if (m.length == 4) {
            // IPV4
            for(i = 0; i < m.length; i++) {
                item = m[i];

                if(item.length == 1) {
                    x += "00" + item;
                }
                else if(item.length == 2) {
                    x += "0" + item;
                }
                else {
                    x += item;
                }
            }
        }
        else if (n.length > 0) {
            // IPV6
            var count = 0;
            for(i = 0; i < n.length; i++) {
                item = n[i];

                if (i > 0) {
                    xa += ":";
                }

                if(item.length === 0) {
                    count += 0;
                }
                else if(item.length == 1) {
                    xa += "000" + item;
                    count += 4;
                }
                else if(item.length == 2) {
                    xa += "00" + item;
                    count += 4;
                }
                else if(item.length == 3) {
                    xa += "0" + item;
                    count += 4;
                }
                else {
                    xa += item;
                    count += 4;
                }
            }

            // Padding the ::
            n = xa.split(":");
            var paddDone = 0;

            for (i = 0; i < n.length; i++) {
                item = n[i];

                if (item.length === 0 && paddDone === 0) {
                    for (var padding = 0 ; padding < (32-count) ; padding++) {
                        x += "0";
                        paddDone = 1;
                    }
                }
                else {
                    x += item;
                }
            }
        }

        return x;
    },

    "ip-address-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "ip-address-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});