mirror of
https://github.com/LdesignMedia/moodle-availability_ipaddress.git
synced 2026-10-22 19:18:06 +02:00
Update #86c46bd0u - Add support for predefined IP addresses/ranges
This commit is contained in:
1 parent
26669e73f1
commit
fe3a5b2302
16 files changed
+1435
-52
No files matched your search
+80
-10
@@ -43,9 +43,11 @@ M.availability_ipaddress.form = Y.Object(M.core_availability.plugin);
|
||||
* @method initInner
|
||||
* @param {Array} param Array of objects
|
||||
*/
|
||||
M.availability_ipaddress.form.initInner = function() {
|
||||
M.availability_ipaddress.form.initInner = function(param) {
|
||||
"use strict";
|
||||
Y.log('M.availability_ipaddress 1.10');
|
||||
// Store predefined ranges from backend.
|
||||
this.predefinedRanges = param || [];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -80,24 +82,68 @@ M.availability_ipaddress.form.getValue = function(field, node) {
|
||||
*/
|
||||
M.availability_ipaddress.form.getNode = function(json) {
|
||||
"use strict";
|
||||
var html, node, root, id;
|
||||
var html, node, root, id, selectId, i, range;
|
||||
|
||||
// Make sure we work with unique id.
|
||||
id = 'ipaddresses' + M.availability_ipaddress.form.instId;
|
||||
selectId = 'predefined' + M.availability_ipaddress.form.instId;
|
||||
M.availability_ipaddress.form.instId += 1;
|
||||
|
||||
// Create HTML structure.
|
||||
html = '';
|
||||
html += '<span class="availability-group"><label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('title', 'availability_ipaddress') + ' </span></label>';
|
||||
html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="' + id + '">';
|
||||
node = Y.Node.create('<span class="form-inline">' + html + '</span>');
|
||||
html = '<div class="availability-ipaddress-container">';
|
||||
|
||||
// Add predefined ranges if available.
|
||||
if (this.predefinedRanges && this.predefinedRanges.length > 0) {
|
||||
html += '<div class="availability-group">';
|
||||
html += '<label><span class="p-r-1">' +
|
||||
M.util.get_string('use_predefined', 'availability_ipaddress') + '</span></label>';
|
||||
html += '<select name="predefined_ranges" id="' + selectId +
|
||||
'" multiple="multiple" class="form-control" style="min-height: 100px;">';
|
||||
|
||||
for (i = 0; i < this.predefinedRanges.length; i++) {
|
||||
range = this.predefinedRanges[i];
|
||||
html += '<option value="' + range.id + '" data-ipaddresses="' + Y.Escape.html(range.ipaddresses) + '">';
|
||||
html += Y.Escape.html(range.name);
|
||||
html += '</option>';
|
||||
}
|
||||
|
||||
html += '</select>';
|
||||
html += '</div>';
|
||||
|
||||
html += '<div class="availability-group" style="margin-top: 10px;">';
|
||||
html += '<label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('custom_ipaddress', 'availability_ipaddress') + '</span></label>';
|
||||
} else {
|
||||
html += '<div class="availability-group">';
|
||||
html += '<label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('title', 'availability_ipaddress') + '</span></label>';
|
||||
}
|
||||
|
||||
html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="' +
|
||||
id + '" class="form-control">';
|
||||
html += '</div>';
|
||||
html += '</div>';
|
||||
|
||||
node = Y.Node.create('<div>' + html + '</div>');
|
||||
|
||||
// Set initial values, if specified.
|
||||
if (json.ipaddresses !== undefined) {
|
||||
node.one('input[name=ipaddresses]').set('value', json.ipaddresses);
|
||||
}
|
||||
|
||||
// Set selected predefined ranges if specified.
|
||||
if (json.predefined_ranges !== undefined && this.predefinedRanges && this.predefinedRanges.length > 0) {
|
||||
var select = node.one('select[name=predefined_ranges]');
|
||||
if (select) {
|
||||
json.predefined_ranges.forEach(function(rangeId) {
|
||||
var option = select.one('option[value="' + rangeId + '"]');
|
||||
if (option) {
|
||||
option.set('selected', true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Add event handlers (first time only).
|
||||
if (!M.availability_ipaddress.form.addedEvents) {
|
||||
M.availability_ipaddress.form.addedEvents = true;
|
||||
@@ -106,6 +152,11 @@ M.availability_ipaddress.form.getNode = function(json) {
|
||||
// Trigger the updating of the hidden availability data whenever the ipaddress field changes.
|
||||
M.core_availability.form.update();
|
||||
}, '.availability_ipaddress input[name=ipaddresses]');
|
||||
|
||||
root.delegate('change', function() {
|
||||
// Trigger the updating when predefined ranges are selected.
|
||||
M.core_availability.form.update();
|
||||
}, '.availability_ipaddress select[name=predefined_ranges]');
|
||||
}
|
||||
|
||||
return node;
|
||||
@@ -120,6 +171,10 @@ M.availability_ipaddress.form.getNode = function(json) {
|
||||
M.availability_ipaddress.validateIpaddress = function(ipaddresses) {
|
||||
'use strict';
|
||||
Y.log(ipaddresses);
|
||||
// Return true for empty string - it's valid to have no custom IPs
|
||||
if (!ipaddresses || ipaddresses.trim() === '') {
|
||||
return true;
|
||||
}
|
||||
ipaddresses = ipaddresses.split(',');
|
||||
for (var i in ipaddresses) {
|
||||
|
||||
@@ -134,7 +189,7 @@ M.availability_ipaddress.validateIpaddress = function(ipaddresses) {
|
||||
var ipv4Regex = new RegExp(
|
||||
'^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)' +
|
||||
'(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}-' +
|
||||
'([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]){1}$',
|
||||
'(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$',
|
||||
'gm'
|
||||
);
|
||||
|
||||
@@ -178,6 +233,20 @@ M.availability_ipaddress.form.fillValue = function(value, node) {
|
||||
// with the structure used in the __construct and save functions
|
||||
// within condition.php.
|
||||
value.ipaddresses = this.getValue('ipaddresses', node);
|
||||
|
||||
// Get selected predefined ranges.
|
||||
var select = node.one('select[name=predefined_ranges]');
|
||||
if (select) {
|
||||
var selectedRanges = [];
|
||||
select.get('options').each(function(option) {
|
||||
if (option.get('selected')) {
|
||||
selectedRanges.push(parseInt(option.get('value')));
|
||||
}
|
||||
});
|
||||
if (selectedRanges.length > 0) {
|
||||
value.predefined_ranges = selectedRanges;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -190,8 +259,9 @@ M.availability_ipaddress.form.fillErrors = function(errors, node) {
|
||||
var value = {};
|
||||
this.fillValue(value, node);
|
||||
|
||||
// Basic ipaddresses checks.
|
||||
if (M.availability_ipaddress.validateIpaddress(value.ipaddresses) === false) {
|
||||
// Basic ipaddresses checks - only validate if not empty.
|
||||
if (value.ipaddresses && value.ipaddresses.trim() !== '' &&
|
||||
M.availability_ipaddress.validateIpaddress(value.ipaddresses) === false) {
|
||||
errors.push('availability_ipaddress:error_ipaddress');
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
YUI.add("moodle-availability_ipaddress-form",function(e,d){M.availability_ipaddress=M.availability_ipaddress||{},M.availability_ipaddress.v4="(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}",M.availability_ipaddress.v6="^((?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,2}|:)|(?:[a-fA-F\\d]{1,4}:){4}(?:(:[a-fA-F\\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,3}|:)|(?:[a-fA-F\\d]{1,4}:){3}(?:(:[a-fA-F\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,4}|:)|(?:[a-fA-F\\d]{1,4}:){2}(?:(:[a-fA-F\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,5}|:)|(?:[a-fA-F\\d]{1,4}:){1}(?:(:[a-fA-F\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,6}|:)|(?::((?::[a-fA-F\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,7}|:)))(%[0-9a-zA-Z]{1,})?",M.availability_ipaddress.form=e.Object(M.core_availability.plugin),M.availability_ipaddress.form.initInner=function(){},M.availability_ipaddress.form.getValue=function(d,a){"use strict";d=a.one("input[name="+d+"]").get("value");return M.availability_ipaddress.validateIpaddress(d),d},M.availability_ipaddress.form.getNode=function(d){"use strict";var a,i="ipaddresses"+M.availability_ipaddress.form.instId;return M.availability_ipaddress.form.instId+=1,a="",a+='<span class="availability-group"><label for="'+i+'"><span class="p-r-1">'+M.util.get_string("title","availability_ipaddress")+" </span></label>",i=e.Node.create('<span class="form-inline">'+(a+='<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="'+i+'">')+"</span>"),d.ipaddresses!==undefined&&i.one("input[name=ipaddresses]").set("value",d.ipaddresses),M.availability_ipaddress.form.addedEvents||(M.availability_ipaddress.form.addedEvents=!0,e.one(".availability-field").delegate("valuechange",function(){M.core_availability.form.update()},".availability_ipaddress input[name=ipaddresses]")),i},M.availability_ipaddress.validateIpaddress=function(d){"use strict";for(var a in d=d.split(","))if(!(new RegExp(/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/gm).test(d[a])||new RegExp("^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}-([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]){1}$","gm").test(d[a])||new RegExp(M.availability_ipaddress.v6).test(d[a])||new RegExp("^(?:".concat(M.availability_ipaddress.v4+"\\/(3[0-2]|[12]?[0-9])|(1\\*)",")|(?:").concat(M.availability_ipaddress.v6+"\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])",")?\\/gm")).test(d[a])))return!1;return!0},M.availability_ipaddress.form.fillValue=function(d,a){d.ipaddresses=this.getValue("ipaddresses",a)},M.availability_ipaddress.form.fillErrors=function(d,a){"use strict";var i={};this.fillValue(i,a),!1===M.availability_ipaddress.validateIpaddress(i.ipaddresses)&&d.push("availability_ipaddress:error_ipaddress")}},"@VERSION@",{requires:["base","node","event","moodle-core_availability-form"]});
|
||||
YUI.add("moodle-availability_ipaddress-form",function(r,d){M.availability_ipaddress=M.availability_ipaddress||{},M.availability_ipaddress.v4="(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])){3}",M.availability_ipaddress.v6="^((?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-fA-F\\d]{1,4}|:)|(?:[a-fA-F\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,2}|:)|(?:[a-fA-F\\d]{1,4}:){4}(?:(:[a-fA-F\\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,3}|:)|(?:[a-fA-F\\d]{1,4}:){3}(?:(:[a-fA-F\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,4}|:)|(?:[a-fA-F\\d]{1,4}:){2}(?:(:[a-fA-F\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,5}|:)|(?:[a-fA-F\\d]{1,4}:){1}(?:(:[a-fA-F\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(:[a-fA-F\\d]{1,4}){1,6}|:)|(?::((?::[a-fA-F\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,7}|:)))(%[0-9a-zA-Z]{1,})?",M.availability_ipaddress.form=r.Object(M.core_availability.plugin),M.availability_ipaddress.form.initInner=function(d){"use strict";this.predefinedRanges=d||[]},M.availability_ipaddress.form.getValue=function(d,a){"use strict";d=a.one("input[name="+d+"]").get("value");return M.availability_ipaddress.validateIpaddress(d),d},M.availability_ipaddress.form.getNode=function(d){"use strict";var a,e,i,s,t="ipaddresses"+M.availability_ipaddress.form.instId,l="predefined"+M.availability_ipaddress.form.instId;if(M.availability_ipaddress.form.instId+=1,a='<div class="availability-ipaddress-container">',this.predefinedRanges&&0<this.predefinedRanges.length){for(a=(a+='<div class="availability-group">')+('<label><span class="p-r-1">'+M.util.get_string("use_predefined","availability_ipaddress")+"</span></label>")+('<select name="predefined_ranges" id="'+l+'" multiple="multiple" class="form-control" style="min-height: 100px;">'),e=0;e<this.predefinedRanges.length;e++)a=(a+='<option value="'+(i=this.predefinedRanges[e]).id+'" data-ipaddresses="'+r.Escape.html(i.ipaddresses)+'">')+r.Escape.html(i.name)+"</option>";a=(a=(a+="</select>")+"</div>"+'<div class="availability-group" style="margin-top: 10px;">')+('<label for="'+t+'"><span class="p-r-1">'+M.util.get_string("custom_ipaddress","availability_ipaddress")+"</span></label>")}else a=(a+='<div class="availability-group">')+'<label for="'+t+'"><span class="p-r-1">'+M.util.get_string("title","availability_ipaddress")+"</span></label>";return l=r.Node.create("<div>"+(a=(a+='<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="'+t+'" class="form-control">')+"</div>"+"</div>")+"</div>"),d.ipaddresses!==undefined&&l.one("input[name=ipaddresses]").set("value",d.ipaddresses),d.predefined_ranges!==undefined&&this.predefinedRanges&&0<this.predefinedRanges.length&&(s=l.one("select[name=predefined_ranges]"))&&d.predefined_ranges.forEach(function(d){d=s.one('option[value="'+d+'"]');d&&d.set("selected",!0)}),M.availability_ipaddress.form.addedEvents||(M.availability_ipaddress.form.addedEvents=!0,(t=r.one(".availability-field")).delegate("valuechange",function(){M.core_availability.form.update()},".availability_ipaddress input[name=ipaddresses]"),t.delegate("change",function(){M.core_availability.form.update()},".availability_ipaddress select[name=predefined_ranges]")),l},M.availability_ipaddress.validateIpaddress=function(d){"use strict";if(!d||""===d.trim())return!0;for(var a in d=d.split(","))if(!(new RegExp(/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/gm).test(d[a])||new RegExp("^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}-(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$","gm").test(d[a])||new RegExp(M.availability_ipaddress.v6).test(d[a])||new RegExp("^(?:".concat(M.availability_ipaddress.v4+"\\/(3[0-2]|[12]?[0-9])|(1\\*)",")|(?:").concat(M.availability_ipaddress.v6+"\\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])",")?\\/gm")).test(d[a])))return!1;return!0},M.availability_ipaddress.form.fillValue=function(d,a){var e;d.ipaddresses=this.getValue("ipaddresses",a),(a=a.one("select[name=predefined_ranges]"))&&(e=[],a.get("options").each(function(d){d.get("selected")&&e.push(parseInt(d.get("value")))}),0<e.length&&(d.predefined_ranges=e))},M.availability_ipaddress.form.fillErrors=function(d,a){"use strict";var e={};this.fillValue(e,a),e.ipaddresses&&""!==e.ipaddresses.trim()&&!1===M.availabiLine truncated
|
||||
+80
-10
@@ -43,8 +43,10 @@ M.availability_ipaddress.form = Y.Object(M.core_availability.plugin);
|
||||
* @method initInner
|
||||
* @param {Array} param Array of objects
|
||||
*/
|
||||
M.availability_ipaddress.form.initInner = function() {
|
||||
M.availability_ipaddress.form.initInner = function(param) {
|
||||
"use strict";
|
||||
// Store predefined ranges from backend.
|
||||
this.predefinedRanges = param || [];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -77,24 +79,68 @@ M.availability_ipaddress.form.getValue = function(field, node) {
|
||||
*/
|
||||
M.availability_ipaddress.form.getNode = function(json) {
|
||||
"use strict";
|
||||
var html, node, root, id;
|
||||
var html, node, root, id, selectId, i, range;
|
||||
|
||||
// Make sure we work with unique id.
|
||||
id = 'ipaddresses' + M.availability_ipaddress.form.instId;
|
||||
selectId = 'predefined' + M.availability_ipaddress.form.instId;
|
||||
M.availability_ipaddress.form.instId += 1;
|
||||
|
||||
// Create HTML structure.
|
||||
html = '';
|
||||
html += '<span class="availability-group"><label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('title', 'availability_ipaddress') + ' </span></label>';
|
||||
html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="' + id + '">';
|
||||
node = Y.Node.create('<span class="form-inline">' + html + '</span>');
|
||||
html = '<div class="availability-ipaddress-container">';
|
||||
|
||||
// Add predefined ranges if available.
|
||||
if (this.predefinedRanges && this.predefinedRanges.length > 0) {
|
||||
html += '<div class="availability-group">';
|
||||
html += '<label><span class="p-r-1">' +
|
||||
M.util.get_string('use_predefined', 'availability_ipaddress') + '</span></label>';
|
||||
html += '<select name="predefined_ranges" id="' + selectId +
|
||||
'" multiple="multiple" class="form-control" style="min-height: 100px;">';
|
||||
|
||||
for (i = 0; i < this.predefinedRanges.length; i++) {
|
||||
range = this.predefinedRanges[i];
|
||||
html += '<option value="' + range.id + '" data-ipaddresses="' + Y.Escape.html(range.ipaddresses) + '">';
|
||||
html += Y.Escape.html(range.name);
|
||||
html += '</option>';
|
||||
}
|
||||
|
||||
html += '</select>';
|
||||
html += '</div>';
|
||||
|
||||
html += '<div class="availability-group" style="margin-top: 10px;">';
|
||||
html += '<label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('custom_ipaddress', 'availability_ipaddress') + '</span></label>';
|
||||
} else {
|
||||
html += '<div class="availability-group">';
|
||||
html += '<label for="' + id + '"><span class="p-r-1">' +
|
||||
M.util.get_string('title', 'availability_ipaddress') + '</span></label>';
|
||||
}
|
||||
|
||||
html += '<input type="text" placeholder="192.168.178.1,231.54.211.0/20,231.3.56.211" name="ipaddresses" id="' +
|
||||
id + '" class="form-control">';
|
||||
html += '</div>';
|
||||
html += '</div>';
|
||||
|
||||
node = Y.Node.create('<div>' + html + '</div>');
|
||||
|
||||
// Set initial values, if specified.
|
||||
if (json.ipaddresses !== undefined) {
|
||||
node.one('input[name=ipaddresses]').set('value', json.ipaddresses);
|
||||
}
|
||||
|
||||
// Set selected predefined ranges if specified.
|
||||
if (json.predefined_ranges !== undefined && this.predefinedRanges && this.predefinedRanges.length > 0) {
|
||||
var select = node.one('select[name=predefined_ranges]');
|
||||
if (select) {
|
||||
json.predefined_ranges.forEach(function(rangeId) {
|
||||
var option = select.one('option[value="' + rangeId + '"]');
|
||||
if (option) {
|
||||
option.set('selected', true);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Add event handlers (first time only).
|
||||
if (!M.availability_ipaddress.form.addedEvents) {
|
||||
M.availability_ipaddress.form.addedEvents = true;
|
||||
@@ -103,6 +149,11 @@ M.availability_ipaddress.form.getNode = function(json) {
|
||||
// Trigger the updating of the hidden availability data whenever the ipaddress field changes.
|
||||
M.core_availability.form.update();
|
||||
}, '.availability_ipaddress input[name=ipaddresses]');
|
||||
|
||||
root.delegate('change', function() {
|
||||
// Trigger the updating when predefined ranges are selected.
|
||||
M.core_availability.form.update();
|
||||
}, '.availability_ipaddress select[name=predefined_ranges]');
|
||||
}
|
||||
|
||||
return node;
|
||||
@@ -116,6 +167,10 @@ M.availability_ipaddress.form.getNode = function(json) {
|
||||
*/
|
||||
M.availability_ipaddress.validateIpaddress = function(ipaddresses) {
|
||||
'use strict';
|
||||
// Return true for empty string - it's valid to have no custom IPs
|
||||
if (!ipaddresses || ipaddresses.trim() === '') {
|
||||
return true;
|
||||
}
|
||||
ipaddresses = ipaddresses.split(',');
|
||||
for (var i in ipaddresses) {
|
||||
|
||||
@@ -129,7 +184,7 @@ M.availability_ipaddress.validateIpaddress = function(ipaddresses) {
|
||||
var ipv4Regex = new RegExp(
|
||||
'^(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)' +
|
||||
'(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}-' +
|
||||
'([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]){1}$',
|
||||
'(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$',
|
||||
'gm'
|
||||
);
|
||||
|
||||
@@ -168,6 +223,20 @@ M.availability_ipaddress.form.fillValue = function(value, node) {
|
||||
// with the structure used in the __construct and save functions
|
||||
// within condition.php.
|
||||
value.ipaddresses = this.getValue('ipaddresses', node);
|
||||
|
||||
// Get selected predefined ranges.
|
||||
var select = node.one('select[name=predefined_ranges]');
|
||||
if (select) {
|
||||
var selectedRanges = [];
|
||||
select.get('options').each(function(option) {
|
||||
if (option.get('selected')) {
|
||||
selectedRanges.push(parseInt(option.get('value')));
|
||||
}
|
||||
});
|
||||
if (selectedRanges.length > 0) {
|
||||
value.predefined_ranges = selectedRanges;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -180,8 +249,9 @@ M.availability_ipaddress.form.fillErrors = function(errors, node) {
|
||||
var value = {};
|
||||
this.fillValue(value, node);
|
||||
|
||||
// Basic ipaddresses checks.
|
||||
if (M.availability_ipaddress.validateIpaddress(value.ipaddresses) === false) {
|
||||
// Basic ipaddresses checks - only validate if not empty.
|
||||
if (value.ipaddresses && value.ipaddresses.trim() !== '' &&
|
||||
M.availability_ipaddress.validateIpaddress(value.ipaddresses) === false) {
|
||||
errors.push('availability_ipaddress:error_ipaddress');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user