Project

General

Profile

Download (4.15 KB) Statistics
| Branch: | Tag: | Revision:
1
/*jslint browser: true, eqeqeq: true, undef: true */
2
/*global jQuery */
3
/******************************************************************************
4
Lines above are for jslint, the JavaScript verifier.  http://www.jslint.com/
5
******************************************************************************/
6

    
7
/* MIT-licensed code from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some */
8
/* (C) 2007 Mozilla Developer Network and/or Jeff Walden */
9
if (!Array.prototype.some) {
10
	Array.prototype.some = function(fun /*, thisp */) {
11
		"use strict";
12
		if (!this) {
13
			throw new TypeError();
14
		}
15
		var t = Object(this);
16
		var len = t.length >>> 0;
17
		if (typeof fun !== "function") {
18
			throw new TypeError();
19
		}
20
		var thisp = arguments[1];
21
		for (var i = 0; i < len; i++) {
22
			if (i in t && fun.call(thisp, t[i], i, t)) {
23
				return true;
24
			}
25
		}
26
		return false;
27
	};
28
}
29

    
30
(function ($) {
31
	// --------------------------------------------------------------------
32
	// find pairs of <input class='ipv4v6'> (textbox for IPv4 or IPv6 addr)
33
	// and <select class='ipv4v6'> (dropdown for # bits in CIDR) and
34
	// activate behavior that restricts options in the <select> when an
35
	// ipv4 address is typed in the <input>.
36
	// --------------------------------------------------------------------
37
	var _ipv4v6ify = function (input1, input2) {
38
		var options = Array.prototype.slice.call(input2.options, 0);
39
		var has_128  = options.some(function (x) { return parseInt(x.value, 10) === 128; });
40
		var has_0    = options.some(function (x) { return parseInt(x.value, 10) === 0; });
41
		var max_ipv6 = has_128 ? 128 : 127;
42
		var min_ipv6 = has_0 ? 0 : 1;
43
		var max_ipv4 = has_128 ? 32 : 31;
44
		var min_ipv4 = has_0 ? 0 : 1;
45
		var was_ipv4 = undefined;
46
		var is_ipv4  = undefined;
47
		var restrict_bits_to_ipv4 = function () {
48
			input2.options.length = 0;
49
			for (var i = 0; i < options.length; i += 1) {
50
				var val = parseInt(options[i].value, 10);
51
				if (val >= min_ipv4 && val <= max_ipv4) {
52
					input2.options.add(options[i]);
53
				}
54
			}
55
		};
56
		var unrestrict_bits = function () {
57
			input2.options.length = 0;
58
			for (var i = 0; i < options.length; i += 1) {
59
				input2.options.add(options[i]);
60
			}
61
		};
62
		var onchange_handler = function () {
63
			was_ipv4 = is_ipv4;
64
			is_ipv4  = /\./.test(input1.value) && !/\:/.test(input1.value);
65
			// handle state transitions to gracefully change the
66
			// value in the dropdown.	
67
			var bits = parseInt($(input2).val(), 10);
68
			if (was_ipv4 === false && is_ipv4 === true) {
69
				restrict_bits_to_ipv4();
70
				/* min_ipv4 -> min_ipv4 */
71
				/*   ...    ->   ...    */
72
				/* max_ipv4 -> max_ipv4 */
73
				/*   ...    ->   ...    */
74
				/* max_ipv6 -> max_ipv4 */
75
				if (bits < min_ipv4) {
76
					$(input2).val(min_ipv4);
77
				}
78
				else if (bits < max_ipv4) {
79
					$(input2).val(bits);
80
				}
81
				else {
82
					$(input2).val(max_ipv4);
83
				}
84
			}
85
			else if (was_ipv4 === true && is_ipv4 === false) {
86
				unrestrict_bits();
87
				/* min_ipv4 -> min_ipv4 */
88
				/*   ...    ->   ...    */
89
				/* max_ipv4 -> max_ipv4 */
90
				if (bits < min_ipv4) {
91
					$(input2).val(min_ipv6);
92
				}
93
				else if (bits < max_ipv4) {
94
					$(input2).val(bits);
95
				}
96
				else {
97
					$(input2).val(max_ipv6);
98
				}
99
			}
100
			else if (was_ipv4 === undefined && is_ipv4 === true) {
101
				// initial value is an ipv4 address
102
				restrict_bits_to_ipv4();
103
				/* min_ipv4 -> min_ipv4 */
104
				/*   ...    ->   ...    */
105
				/* max_ipv4 -> max_ipv4 */
106
				/*   ...    ->   ...    */
107
				/* max_ipv6 -> max_ipv4 */
108
				if (bits < min_ipv4) {
109
					$(input2).val(min_ipv4);
110
				}
111
				else if (bits < max_ipv4) {
112
					$(input2).val(bits);
113
				}
114
				else {
115
					$(input2).val(max_ipv4);
116
				}
117
			}
118
		};
119
		$(input1).unbind("change").bind("change", onchange_handler).trigger("change");
120
	};
121
	$.fn.extend({
122
		"ipv4v6ify": function () {
123
			return this.each(function () {
124
				var inputs, i, input1, input2;
125
				inputs = $(this).find(":input.ipv4v6").toArray();
126
				for (i = 0; i < inputs.length - 1; i += 1) {
127
					input1 = inputs[i];
128
					input2 = inputs[i + 1];
129
					if (input1.type === "text" && input2.type === "select-one") {
130
						_ipv4v6ify(input1, input2);
131
					}
132
				}
133
			});
134
		}
135
	});
136
	$(function () {
137
		$(document).ipv4v6ify();
138
	});
139
})(jQuery);
140

    
(9-9/17)