mid.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 'use strict';
  2. 'require uci';
  3. 'require view';
  4. 'require rpc';
  5. 'require ui';
  6. return view.extend({
  7. callGetJsonStatus: rpc.declare({
  8. object: 'olsrinfo',
  9. method: 'getjsondata',
  10. params: ['otable', 'v4_port', 'v6_port'],
  11. }),
  12. fetch_jsoninfo: function (otable) {
  13. var jsonreq4 = '';
  14. var jsonreq6 = '';
  15. var v4_port = parseInt(uci.get('olsrd', 'olsrd_jsoninfo', 'port') || '') || 9090;
  16. var v6_port = parseInt(uci.get('olsrd6', 'olsrd_jsoninfo', 'port') || '') || 9090;
  17. var json;
  18. var self = this;
  19. return new Promise(function (resolve, reject) {
  20. L.resolveDefault(self.callGetJsonStatus(otable, v4_port, v6_port), {})
  21. .then(function (res) {
  22. json = res;
  23. jsonreq4 = JSON.parse(json.jsonreq4);
  24. jsonreq6 = json.jsonreq6 !== '' ? JSON.parse(json.jsonreq6) : [];
  25. var jsondata4 = {};
  26. var jsondata6 = {};
  27. var data4 = [];
  28. var data6 = [];
  29. var has_v4 = false;
  30. var has_v6 = false;
  31. if (jsonreq4 === '' && jsonreq6 === '') {
  32. window.location.href = 'error_olsr';
  33. reject([null, 0, 0, true]);
  34. return;
  35. }
  36. if (jsonreq4 !== '') {
  37. has_v4 = true;
  38. jsondata4 = jsonreq4 || {};
  39. if (otable === 'status') {
  40. data4 = jsondata4;
  41. } else {
  42. data4 = jsondata4[otable] || [];
  43. }
  44. for (var i = 0; i < data4.length; i++) {
  45. data4[i]['proto'] = '4';
  46. }
  47. }
  48. if (jsonreq6 !== '') {
  49. has_v6 = true;
  50. jsondata6 = jsonreq6 || {};
  51. if (otable === 'status') {
  52. data6 = jsondata6;
  53. } else {
  54. data6 = jsondata6[otable] || [];
  55. }
  56. for (var j = 0; j < data6.length; j++) {
  57. data6[j]['proto'] = '6';
  58. }
  59. }
  60. for (var k = 0; k < data6.length; k++) {
  61. data4.push(data6[k]);
  62. }
  63. resolve([data4, has_v4, has_v6, false]);
  64. })
  65. .catch(function (err) {
  66. console.error(err);
  67. reject([null, 0, 0, true]);
  68. });
  69. });
  70. },
  71. action_mid: function () {
  72. var self = this;
  73. return new Promise(function (resolve, reject) {
  74. self
  75. .fetch_jsoninfo('mid')
  76. .then(function ([data, has_v4, has_v6, error]) {
  77. if (error) {
  78. reject(error);
  79. }
  80. function compare(a, b) {
  81. if (a.proto === b.proto) {
  82. return a.main.ipAddress < b.main.ipAddress;
  83. } else {
  84. return a.proto < b.proto;
  85. }
  86. }
  87. data.sort(compare);
  88. var result = { mids: data, has_v4: has_v4, has_v6: has_v6 };
  89. resolve(result);
  90. })
  91. .catch(function (err) {
  92. reject(err);
  93. });
  94. });
  95. },
  96. render: function () {
  97. var mids_res;
  98. var has_v4;
  99. var has_v6;
  100. return this.action_mid()
  101. .then(function (result) {
  102. mids_res = result.mids;
  103. has_v4 = result.has_v4;
  104. has_v6 = result.has_v6;
  105. var table = E('div', { 'class': 'table cbi-section-table' }, [
  106. E('div', { 'class': 'tr cbi-section-table-titles' }, [E('div', { 'class': 'th cbi-section-table-cell' }, _('OLSR node')), E('div', { class: 'th cbi-section-table-cell' }, _('Secondary OLSR interfaces'))]),
  107. ]);
  108. var i = 1;
  109. for (var k = 0; k < mids_res.length; k++) {
  110. var mid = mids_res[k];
  111. var aliases = '';
  112. for (var j = 0; j < mid.aliases.length; j++) {
  113. var v = mid.aliases[j];
  114. var sep = aliases === '' ? '' : ', ';
  115. aliases = v.ipAddress + sep + aliases;
  116. }
  117. var host = mid.main.ipAddress;
  118. if (mid.proto === '6') {
  119. host = '[' + mid.main.ipAddress + ']';
  120. }
  121. var tr = E('div', { 'class': 'tr cbi-section-table-row cbi-rowstyle-' + i + ' proto-' + mid.proto }, [
  122. E('div', { 'class': 'td cbi-section-table-cell left' }, [E('a', { 'href': 'http://' + host + '/cgi-bin-status.html' }, mid.main.ipAddress)]),
  123. E('div', { 'class': 'td cbi-section-table-cell left' }, aliases),
  124. ]);
  125. table.appendChild(tr);
  126. i = (i % 2) + 1;
  127. }
  128. var fieldset = E('fieldset', { 'class': 'cbi-section' }, [E('legend', {}, _('Overview of known multiple interface announcements')), table]);
  129. var h2 = E('h2', { 'name': 'content' }, _('Active MID announcements'));
  130. var divToggleButtons = E('div', { 'id': 'togglebuttons' });
  131. var statusOlsrCommonJs = null;
  132. if (has_v4 && has_v6) {
  133. statusOlsrCommonJs = E('script', { 'type': 'text/javascript', 'src': L.resource('common/common_js.js') });
  134. }
  135. var result = E([], {}, [h2, divToggleButtons, fieldset, statusOlsrCommonJs]);
  136. return result;
  137. })
  138. .catch(function (error) {
  139. console.error(error);
  140. });
  141. },
  142. handleSaveApply: null,
  143. handleSave: null,
  144. handleReset: null,
  145. });