From: Paul Donald Date: Fri, 17 Oct 2025 22:41:15 +0000 (+0200) Subject: luci-app-lldpd: ES6 treatment X-Git-Url: http://git.openwrt.org/?a=commitdiff_plain;h=764a94b59cd748024fdac8ea3163ff3196a5064c;p=project%2Fluci.git luci-app-lldpd: ES6 treatment Signed-off-by: Paul Donald --- diff --git a/applications/luci-app-lldpd/htdocs/luci-static/resources/lldpd.js b/applications/luci-app-lldpd/htdocs/luci-static/resources/lldpd.js index 7b0121e548..8695fc8c55 100644 --- a/applications/luci-app-lldpd/htdocs/luci-static/resources/lldpd.js +++ b/applications/luci-app-lldpd/htdocs/luci-static/resources/lldpd.js @@ -47,10 +47,10 @@ const etitle = _('enable filter'); const ptitle = _('keep only one protocol'); const ntitle = _('keep only one neighbor'); -var cbiFilterSelect = form.Value.extend({ +const cbiFilterSelect = form.Value.extend({ __name__: 'CBI.LLDPD.FilterSelect', - __init__: function() { + __init__() { this.super('__init__', arguments); this.selected = null; @@ -80,11 +80,11 @@ var cbiFilterSelect = form.Value.extend({ }, /** @private */ - handleRowClick: function(section_id, ev) { - var row = ev.currentTarget; - var tbody = row.parentNode; - var selected = row.getAttribute('data-filter'); - var input = tbody.querySelector('[id="' + this.cbid(section_id) + '-' + selected + '"]'); + handleRowClick(section_id, ev) { + const row = ev.currentTarget; + const tbody = row.parentNode; + const selected = row.getAttribute('data-filter'); + const input = tbody.querySelector('[id="' + this.cbid(section_id) + '-' + selected + '"]'); this.selected = selected; @@ -96,12 +96,12 @@ var cbiFilterSelect = form.Value.extend({ row.classList.add('lldpd-filter-selected'); }, - formvalue: function(section_id) { + formvalue(section_id) { return this.selected || this.cfgvalue(section_id); }, - renderFrame: function(section_id, in_table, option_index, nodes) { - var tmp = this.description; + renderFrame(section_id, in_table, option_index, nodes) { + const tmp = this.description; // Prepend description with table legend this.description = @@ -110,7 +110,7 @@ var cbiFilterSelect = form.Value.extend({ '
  • ' + 'N — ' + ntitle + '
  • ' + '' + this.description; - var rendered = this.super('renderFrame', arguments); + const rendered = this.super('renderFrame', arguments); // Restore original description this.description = tmp; @@ -118,17 +118,17 @@ var cbiFilterSelect = form.Value.extend({ return rendered; }, - renderWidget: function(section_id, option_index, cfgvalue) { + renderWidget(section_id, option_index, cfgvalue) { //default value is "15" - rows are zero based - var selected = parseInt(cfgvalue) || 15; + const selected = parseInt(cfgvalue) || 15; - var tbody = []; + const tbody = []; - var renderFilterVal = L.bind(function(row, col) { + const renderFilterVal = L.bind(function(row, col) { return this.filterVal[row][col] ? '✔' : ''; }, this); - for (var i = 0; i < this.filterVal.length; i++) { + for (let i = 0; i < this.filterVal.length; i++) { tbody.push(E('tr', { 'class': ((selected == i) ? 'lldpd-filter-selected' : ''), 'click': L.bind(this.handleRowClick, this, section_id), @@ -155,7 +155,7 @@ var cbiFilterSelect = form.Value.extend({ ])); }; - var table = E('table', { 'class': 'lldpd-filter', 'id': this.cbid(section_id) }, [ + const table = E('table', { 'class': 'lldpd-filter', 'id': this.cbid(section_id) }, [ E('thead', {}, [ E('tr', {}, [ E('th', { 'rowspan': 2 }), @@ -179,14 +179,14 @@ var cbiFilterSelect = form.Value.extend({ }, }); -var CBIMultiIOSelect = form.MultiValue.extend({ +const CBIMultiIOSelect = form.MultiValue.extend({ __name__: 'CBI.MultiIOSelect', - renderWidget: function(section_id, option_index, cfgvalue) { - var value = (cfgvalue != null) ? cfgvalue : this.default ? this.default : '', + renderWidget(section_id, option_index, cfgvalue) { + const value = (cfgvalue != null) ? cfgvalue : this.default ? this.default : '', choices = this.transformChoices() ? this.transformChoices() : ''; - var widget = new ui.Dropdown(L.toArray(value), choices, { + const widget = new ui.Dropdown(L.toArray(value), choices, { id: this.cbid(section_id), sort: this.keylist, multiple: true, @@ -204,7 +204,7 @@ var CBIMultiIOSelect = form.MultiValue.extend({ function init() { return new Promise(function(resolveFn, rejectFn) { - var data = session.getLocalData('luci-app-lldpd'); + let data = session.getLocalData('luci-app-lldpd'); if (data !== null) { return resolveFn(); } diff --git a/applications/luci-app-lldpd/htdocs/luci-static/resources/view/lldpd/config.js b/applications/luci-app-lldpd/htdocs/luci-static/resources/view/lldpd/config.js index 0f34e109f3..60afb11980 100644 --- a/applications/luci-app-lldpd/htdocs/luci-static/resources/view/lldpd/config.js +++ b/applications/luci-app-lldpd/htdocs/luci-static/resources/view/lldpd/config.js @@ -18,8 +18,8 @@ const callInitList = rpc.declare({ method: 'getInitList', params: [ 'name' ], expect: { '': {} }, - filter: function(res) { - for (var k in res) + filter(res) { + for (let k in res) return +res[k].enabled; return null; } @@ -32,7 +32,7 @@ const callInitAction = rpc.declare({ expect: { result: false } }); -var usage = _('See syntax here.').format('href=https://lldpd.github.io/usage.html target="_blank"'); +const usage = _('See syntax here.').format('href=https://lldpd.github.io/usage.html target="_blank"'); const validateioentries = function(section_id, value) { if (value) { @@ -40,8 +40,8 @@ const validateioentries = function(section_id, value) { const a = value.split(' '); const noex = a.filter(el=> !el.startsWith('!')); const ex = a.filter(el=> el.startsWith('!') && !el.startsWith('!!')); - for (var i of noex) { - for (var e of ex) { + for (let i of noex) { + for (let e of ex) { if ('!'+i == e){ return emsg.format(i, e); } @@ -52,24 +52,24 @@ const validateioentries = function(section_id, value) { }; return L.view.extend({ - __init__: function() { + __init__() { this.super('__init__', arguments); // Inject CSS - var head = document.getElementsByTagName('head')[0]; - var css = E('link', { 'href': + const head = document.getElementsByTagName('head')[0]; + const css = E('link', { 'href': L.resource('lldpd/lldpd.css') + '?v=#PKG_VERSION', 'rel': 'stylesheet' }); head.appendChild(css); }, - load: function() { + load() { return Promise.all([ + network.getDevices(), callInitList('lldpd'), lldpd.init(), uci.load('lldpd'), - network.getDevices() ]); }, @@ -80,10 +80,8 @@ return L.view.extend({ // ----------------------------------------------------------------------------------------- /** @private */ - populateBasicOptions: function(s, tab, data) { - var o; - var serviceEnabled = data[0]; - var net_devices = data[3]; + populateBasicOptions(s, tab, [net_devices, serviceEnabled]) { + let o; // Service enable/disable o = s.taboption(tab, form.Flag, 'enabled', _('Enable service')); @@ -99,13 +97,13 @@ return L.view.extend({ if (value == '1') { // Enable and start - callInitAction('lldpd', 'enable').then(function() { + callInitAction('lldpd', 'enable').then(() => { return callInitAction('lldpd', 'start'); }); } else { // Stop and disable - callInitAction('lldpd', 'stop').then(function() { + callInitAction('lldpd', 'stop').then(() => { return callInitAction('lldpd', 'disable'); }); } @@ -128,7 +126,7 @@ return L.view.extend({ /* This function returns the value for a specified key. Used to fill various location fields derived from an lldpd location config string */ function getLocationValueFromConfString(_key) { - var inStr = this ? this.section.formvalue('config', 'lldp_location'): + let inStr = this ? this.section.formvalue('config', 'lldp_location'): uci.get('lldpd', 'config', 'lldp_location'); inStr = inStr ? inStr: ''; @@ -152,17 +150,17 @@ return L.view.extend({ }; function write_lldp_location() { - var _input = this ? this.section.formvalue('config', '_lldp_location_type'): + const _input = this ? this.section.formvalue('config', '_lldp_location_type'): '1'; if(_input){ if (_input == '1') { /* location coordinate latitude 48.85667N longitude 2.2014E altitude 117.47 m datum WGS84 */ - var lat = this.section.formvalue('config', '_coordinate_lat'), - lon = this.section.formvalue('config', '_coordinate_lon'), - alt = this.section.formvalue('config', '_coordinate_alt'), - dat = this.section.formvalue('config', '_coordinate_dat'); + const lat = this.section.formvalue('config', '_coordinate_lat'); + const lon = this.section.formvalue('config', '_coordinate_lon'); + const alt = this.section.formvalue('config', '_coordinate_alt'); + const dat = this.section.formvalue('config', '_coordinate_dat'); if(lat && lon && dat) { uci.set('lldpd', 'config', 'lldp_location', 'coordinate latitude ' + lat + @@ -174,12 +172,12 @@ return L.view.extend({ else if (_input == '2') { /* location address country US street "Commercial Road" city "Roseville" */ - var cc = this.section.formvalue('config', '_civic_cc'), - city = this.section.formvalue('config', '_civic_city'), - str = this.section.formvalue('config', '_civic_str'), - bldg = this.section.formvalue('config', '_civic_bldg'), - nmbr = this.section.formvalue('config', '_civic_nmbr'), - zip = this.section.formvalue('config', '_civic_zip'); + const cc = this.section.formvalue('config', '_civic_cc'); + const city = this.section.formvalue('config', '_civic_city'); + const str = this.section.formvalue('config', '_civic_str'); + const bldg = this.section.formvalue('config', '_civic_bldg'); + const nmbr = this.section.formvalue('config', '_civic_nmbr'); + const zip = this.section.formvalue('config', '_civic_zip'); uci.set('lldpd', 'config', 'lldp_location', 'address country ' + cc.toUpperCase() @@ -191,7 +189,7 @@ return L.view.extend({ } else if (_input == '3') { /* location elin 12345 */ - var elin = this.section.formvalue('config', '_elin'); + const elin = this.section.formvalue('config', '_elin'); if(elin) uci.set('lldpd', 'config', 'lldp_location', 'elin ' + elin); } @@ -228,9 +226,9 @@ return L.view.extend({ o.rmempty = true; o.write = function(section_id, value) { if (value) { - const words = value.trim().split(/\s+/), - regex = /^coordinate|^address|^elin/; - var start; + const words = value.trim().split(/\s+/); + const regex = /^coordinate|^address|^elin/; + let start; words.forEach(w=>{ if (w.match(regex)) start = w; }); @@ -241,9 +239,9 @@ return L.view.extend({ }; o.validate = function(section_id, value) { if (value) { - const words = value.trim().split(/\s+/), - regex = /^coordinate|^address|^elin/; - var _eval = _("Must contain: 'coordinate ...', 'address ...' or 'elin ...'"); + const words = value.trim().split(/\s+/); + const regex = /^coordinate|^address|^elin/; + let _eval = _("Must contain: 'coordinate ...', 'address ...' or 'elin ...'"); words.forEach(w=>{ if (w.match(regex)) _eval = true; }); @@ -259,7 +257,7 @@ return L.view.extend({ o.datatype = "maxlength(20)"; o.validate = function(section_id, value) { if (!value) return true; - var valid = _('valid syntax: 0 .. 90.000[N|S]'); + let valid = _('valid syntax: 0 .. 90.000[N|S]'); valid = (parseFloat(value) >= 0 && parseFloat(value) <= 90) ? /^-?\d+(?:\.\d+)?[NnSs]$/.test(value) ? true : valid : valid; return valid; @@ -275,7 +273,7 @@ return L.view.extend({ o.datatype = "maxlength(20)"; o.validate = function(section_id, value) { if (!value) return true; - var valid = _('valid syntax: 0 .. 180.000[E|W]'); + let valid = _('valid syntax: 0 .. 180.000[E|W]'); valid = (parseFloat(value) >= 0 && parseFloat(value) <= 180) ? /^-?\d+(?:\.\d+)?[WwEe]$/.test(value) ? true : valid : valid; return valid; @@ -285,15 +283,15 @@ return L.view.extend({ } o.write = write_lldp_location; - const min_alt = -100000.00, - max_alt = 42849672.95; + const min_alt = -100000.00; + const max_alt = 42849672.95; o = s.taboption(tab, form.Value, '_coordinate_alt', _('Altitude'), '%f .. %f [m|f]'.format(min_alt, max_alt)); o.depends({ '_lldp_location_type' : '1'}); o.datatype = 'maxlength(20)'; o.validate = function(section_id, value) { if (!value) return true; - var valid = _('valid syntax: %f .. %f [mf]').format(min_alt, max_alt); + let valid = _('valid syntax: %f .. %f [mf]').format(min_alt, max_alt); valid = (parseFloat(value) >= min_alt && parseFloat(value) <= max_alt) ? /^-?\d+(?:\.\d+)?\ [mf]$/.test(value) ? true : valid : valid; return valid; @@ -324,7 +322,7 @@ return L.view.extend({ o.placeholder = 'EU'; o.validate = function(section_id, value) { if(!value) return true; - var valid = _('Two character CC required'); + let valid = _('Two character CC required'); valid = (value.length == 2) ? /^[A-Z]{2}$/i.test(value) ? true : valid : valid; return valid; @@ -516,9 +514,8 @@ return L.view.extend({ // ----------------------------------------------------------------------------------------- /** @private */ - populateIfacesOptions: function(s, tab, data) { - var o; - var net_devices = data[3]; + populateIfacesOptions(s, tab, [net_devices, serviceEnabled]) { + let o; // Interfaces to listen on // This value: lldpd.init handles as a list value, and produces a CSV for lldpd.conf: 'configure system interface pattern' @@ -564,8 +561,8 @@ return L.view.extend({ // ----------------------------------------------------------------------------------------- /** @private */ - populateAdvancedOptions: function(s, tab, data) { - var o; + populateAdvancedOptions(s, tab, {net_devices, serviceEnabled}) { + let o; // SNMP agentX socket /* **Note**: The init file tests for SNMP support, so agentxsocket is at worst inert. */ @@ -691,11 +688,11 @@ return L.view.extend({ // ----------------------------------------------------------------------------------------- /** @private */ - populateProtocolsOptions: function(s, tab, data) { - var o; + populateProtocolsOptions(s, tab, {net_devices, serviceEnabled}) { + let o; o = s.taboption(tab, form.SectionValue, '_protocols', form.TypedSection, 'lldpd'); - var ss = o.subsection; + let ss = o.subsection; ss.anonymous = true; ss.addremove = false; @@ -835,7 +832,7 @@ return L.view.extend({ // ----------------------------------------------------------------------------------------- /** @private */ - populateExtraTLVOptions: function(s, tab, data) { + populateExtraTLVOptions(s, tab, [net_devices, serviceEnabled]) { let o, ss, oo; ///// Custom TLV @@ -853,7 +850,7 @@ return L.view.extend({ ss.addbtntitle = _('Add Custom TLV', 'lldpd Custom TLV'); oo = ss.option(lldpd.CBIMultiIOSelect, 'ports', _('Network Interface(s)')); - data[3].forEach(nd => { + net_devices.forEach(nd => { oo.value(nd.getName()); oo.value('!'+nd.getName()); }); @@ -990,8 +987,8 @@ return L.view.extend({ }, /** @private */ - populateOptions: function(s, data) { - var o; + populateOptions(s, data) { + let o; s.tab('basic', _('Basic Settings')); this.populateBasicOptions(s, 'basic', data); @@ -1009,8 +1006,8 @@ return L.view.extend({ this.populateProtocolsOptions(s, 'protocols', data); }, - render: function(data) { - var m, s; + render(data) { + let m, s; m = new form.Map('lldpd', _('LLDPd Settings'), _('LLDPd is an implementation of IEEE 802.1ab') + ' ' +