From: Calin Crisan Date: Tue, 24 Feb 2015 15:12:36 +0000 (+0200) Subject: added support for regex validators in additional config X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=4346cbc29548c0c9ec42faa6f8da28539e8a9048;p=motioneye-debian added support for regex validators in additional config --- diff --git a/motioneye.py b/motioneye.py index 9648fd3..915ba8b 100755 --- a/motioneye.py +++ b/motioneye.py @@ -148,9 +148,9 @@ def _test_requirements(): print('SMB_SHARES require root privileges') return False -# if settings.ENABLE_REBOOT: # TODO -# print('reboot requires root privileges') -# return False + if settings.ENABLE_REBOOT: + print('reboot requires root privileges') + return False try: import tornado # @UnusedImport diff --git a/static/js/main.js b/static/js/main.js index 4cce2c3..9b8691e 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -515,6 +515,27 @@ function initUI() { return true; }, ''); + $('tr[validate] input[type=text]').each(function () { + var $this = $(this); + var $tr = $this.parent().parent(); + var required = $tr.attr('required'); + var validate = $tr.attr('validate'); + if (!validate) { + return; + } + + makeCustomValidator($this, function (value) { + if (!value && required) { + return 'this field is required'; + } + + if (!value.toLowerCase().match(new RegExp(validate))) { + return 'enter a valid value'; + } + + return true; + }, ''); + }); /* input value processors */ makeStrippedInput($('tr[strip=true] input[type=text]')); @@ -557,9 +578,8 @@ function initUI() { var $tr = $(this); var depends = $tr.attr('depends').split(' '); depends.forEach(function (depend) { - if (depend.charAt(0) == '!') { - depend = depend.substring(1); - } + depend = depend.split('=')[0]; + depend = depend.replace(new RegExp('[^a-zA-Z0-9_]', 'g'), ''); if (depend in seenDependNames) { return; @@ -850,11 +870,10 @@ function updateConfigUi() { var depends = $tr.attr('depends').split(' '); var conditionOk = true; depends.every(function (depend) { - var neg = false; - if (depend.charAt(0) == '!') { - neg = true; - depend = depend.substring(1); - } + var neg = depend.indexOf('!') >= 0; + var parts = depend.split('='); + var boolCheck = parts.length == 1; + depend = parts[0].replace(new RegExp('[^a-zA-Z0-9_$]', 'g'), ''); var control = $('#' + depend + 'Entry, #' + depend + 'Select, #' + depend + 'Slider'); var val = false; @@ -867,17 +886,25 @@ function updateConfigUi() { val = control.get(0).checked; } } - - val = Boolean(val); - if (neg) { - val = !val; + + if (boolCheck) { + if (neg) { + val = !val; + } + + if (!val) { + conditionOk = false; + return false; + } } - - if (!val) { - conditionOk = false; - return false; + else { /* comparison */ + var equal = parts[parts.length - 1] == val; + if (equal == neg) { + conditionOk = false; + return false; + } } - + return true; }); diff --git a/templates/main.html b/templates/main.html index 9e1aa01..b711918 100644 --- a/templates/main.html +++ b/templates/main.html @@ -14,7 +14,8 @@ {% if config.get('ticks') %}ticks="{{config['ticks']}}"{% endif %} {% if config.get('ticksnum') is not none %}ticksnum="{{config['ticksnum']}}"{% endif %} {% if config.get('decimals') is not none %}decimals="{{config['decimals']}}"{% endif %} - {% if config.get('unit') %}unit="{{config['unit']}}"{% endif %}> + {% if config.get('unit') %}unit="{{config['unit']}}"{% endif %} + {% if config.get('validate') %}validate="{{config['validate']}}"{% endif %}> {% if config['type'] == 'separator' %}
{% else %}