config.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // This is free software, licensed under the Apache License, Version 2.0
  2. 'use strict';
  3. 'require view';
  4. 'require fs';
  5. 'require ui';
  6. 'require uci';
  7. 'require form';
  8. 'require tools.widgets as widgets';
  9. var isReadonlyView = !L.hasViewPermission() || null;
  10. return view.extend({
  11. handleDeleteModal: function(m, iface, ev) {
  12. L.showModal(_('Delete interface <em>%h</em>').format(iface), [
  13. E('p', _('The interface will be removed from the database permanently. This cannot be undone.')),
  14. E('div', { 'class': 'right' }, [
  15. E('div', {
  16. 'class': 'btn',
  17. 'click': L.hideModal
  18. }, _('Cancel')),
  19. ' ',
  20. E('div', {
  21. 'class': 'btn cbi-button-negative',
  22. 'click': ui.createHandlerFn(this, 'handleDelete', m, iface)
  23. }, _('Delete'))
  24. ])
  25. ]);
  26. },
  27. handleDelete: function(m, iface, ev) {
  28. return fs.exec('/usr/bin/vnstat', ['--remove', '-i', iface, '--force'])
  29. .then(L.bind(m.render, m))
  30. .catch(function(e) {
  31. ui.addNotification(null, E('p', e.message));
  32. })
  33. .finally(L.hideModal);
  34. },
  35. render: function() {
  36. var m, s, o;
  37. m = new form.Map('vnstat', _('vnStat'), _('vnStat is a network traffic monitor for Linux that keeps a log of network traffic for the selected interface(s).'));
  38. s = m.section(form.TypedSection, 'vnstat', _('Interfaces'));
  39. s.anonymous = true;
  40. s.addremove = false;
  41. o = s.option(widgets.DeviceSelect, 'interface', _('Monitor interfaces'), _('The selected interfaces are automatically added to the vnStat database upon startup.'));
  42. o.rmempty = true;
  43. o.multiple = true;
  44. o.noaliases = true;
  45. o.nobridges = false;
  46. o.noinactive = false;
  47. o.nocreate = false;
  48. o = s.option(form.DummyValue, '_database');
  49. o.load = function(section_id) {
  50. return fs.exec('/usr/bin/vnstat', ['--dbiflist', '1']).then(L.bind(function(result) {
  51. var databaseInterfaces = [];
  52. if (result.code == 0 && result.stdout) {
  53. databaseInterfaces = result.stdout.trim().split('\n');
  54. }
  55. var configInterfaces = uci.get_first('vnstat', 'vnstat', 'interface') || [];
  56. this.interfaces = databaseInterfaces.filter(function(iface) {
  57. return configInterfaces.indexOf(iface) == -1;
  58. });
  59. }, this));
  60. };
  61. o.render = L.bind(function(view, section_id) {
  62. var table = E('table', { 'class': 'table' }, [
  63. E('tr', { 'class': 'tr table-titles' }, [
  64. E('th', { 'class': 'th' }, _('Interface')),
  65. E('th', { 'class': 'th right' }, _('Delete'))
  66. ])
  67. ]);
  68. var rows = [];
  69. for (var i = 0; i < this.interfaces.length; i++) {
  70. rows.push([
  71. this.interfaces[i],
  72. E('button', {
  73. 'class': 'btn cbi-button-remove',
  74. 'click': ui.createHandlerFn(view, 'handleDeleteModal', m, this.interfaces[i]),
  75. 'disabled': isReadonlyView
  76. }, [ _('Delete…') ])
  77. ]);
  78. }
  79. cbi_update_table(table, rows, E('em', _('No unconfigured interfaces found in database.')));
  80. return E([], [
  81. E('h3', _('Unconfigured interfaces')),
  82. E('div', { 'class': 'cbi-section-descr' },
  83. _('These interfaces are present in the vnStat database, but are not configured above.')),
  84. table
  85. ]);
  86. }, o, this);
  87. return m.render();
  88. }
  89. });