luci2: add network protocol support extensions
[project/luci2/ui.git] / luci2 / htdocs / luci2 / proto / static.js
1 L.NetworkModel.Protocol.extend({
2         protocol:    'static',
3         description: L.tr('Static address'),
4         tunnel:      false,
5         virtual:     false,
6
7         _ev_broadcast: function(ev)
8         {
9                 var self = ev.data.self;
10                 var sid = ev.data.sid;
11
12                 var i = ($('#' + self.section.id('field', sid, 'ipaddr')).val() || '').split(/\./);
13                 var m = ($('#' + self.section.id('field', sid, 'netmask') + ' select').val() || '').split(/\./);
14
15                 var I = 0;
16                 var M = 0;
17
18                 for (var n = 0; n < 4; n++)
19                 {
20                         i[n] = parseInt(i[n]);
21                         m[n] = parseInt(m[n]);
22
23                         if (isNaN(i[n]) || i[n] < 0 || i[n] > 255 ||
24                                 isNaN(m[n]) || m[n] < 0 || m[n] > 255)
25                                 return;
26
27                         I |= (i[n] << ((3 - n) * 8));
28                         M |= (m[n] << ((3 - n) * 8));
29                 }
30
31                 var B = I | ~M;
32
33                 $('#' + self.section.id('field', sid, 'broadcast'))
34                         .attr('placeholder', '%d.%d.%d.%d'.format(
35                                 (B >> 24) & 0xFF, (B >> 16) & 0xFF,
36                                 (B >>  8) & 0xFF, (B >>  0) & 0xFF
37                         ));
38         },
39
40         populateForm: function(section, iface)
41         {
42                 var device = L.NetworkModel.getDeviceByInterface(iface);
43
44                 section.taboption('general', L.cbi.InputValue, 'ipaddr', {
45                         caption:  L.tr('IPv4 address'),
46                         datatype: 'ip4addr',
47                         optional: true
48                 }).on('blur validate', this._ev_broadcast);
49
50                 section.taboption('general', L.cbi.ComboBox, 'netmask', {
51                         caption:  L.tr('IPv4 netmask'),
52                         datatype: 'ip4addr',
53                         optional: true
54                 }).on('blur validate', this._ev_broadcast)
55                         .value('255.255.255.0')
56                         .value('255.255.0.0')
57                         .value('255.0.0.0');
58
59                 section.taboption('general', L.cbi.InputValue, 'broadcast', {
60                         caption:  L.tr('IPv4 broadcast'),
61                         datatype: 'ip4addr',
62                         optional: true
63                 });
64
65                 section.taboption('general', L.cbi.InputValue, 'gateway', {
66                         caption:  L.tr('IPv4 gateway'),
67                         datatype: 'ip4addr',
68                         optional: true
69                 });
70
71                 section.taboption('general', L.cbi.DynamicList, 'dns', {
72                         caption:  L.tr('DNS servers'),
73                         datatype: 'ipaddr',
74                         optional: true
75                 });
76
77
78                 section.taboption('ipv6', L.cbi.ComboBox, 'ip6assign', {
79                         caption:     L.tr('IPv6 assignment length'),
80                         description: L.tr('Assign a part of given length of every public IPv6-prefix to this interface'),
81                         datatype:    'max(64)',
82                         optional:    true
83                 }).value('', L.tr('disabled')).value('64');
84
85                 var ip6hint = section.taboption('ipv6', L.cbi.InputValue, 'ip6hint', {
86                         caption:     L.tr('IPv6 assignment hint'),
87                         description: L.tr('Assign prefix parts using this hexadecimal subprefix ID for this interface'),
88                         optional:    true
89                 });
90
91                 for (var i = 33; i <= 64; i++)
92                         ip6hint.depends('ip6assign', i);
93
94                 section.taboption('ipv6', L.cbi.InputValue, 'ip6addr', {
95                         caption:     L.tr('IPv6 address'),
96                         datatype:    'ip6addr',
97                         optional:    true
98                 }).depends('ip6assign', false);
99
100                 section.taboption('ipv6', L.cbi.InputValue, 'ip6gw', {
101                         caption:     L.tr('IPv6 gateway'),
102                         datatype:    'ip6addr',
103                         optional:    true
104                 }).depends('ip6assign', false);
105
106                 section.taboption('ipv6', L.cbi.InputValue, 'ip6prefix', {
107                         caption:     L.tr('IPv6 routed prefix'),
108                         description: L.tr('Public prefix routed to this device for distribution to clients'),
109                         datatype:    'ip6addr',
110                         optional:    true
111                 }).depends('ip6assign', false);
112         }
113 });