index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. (function (window, $) {
  2. var $command = $('#command'),
  3. $params = $('#paramsContainer');
  4. App.prototype.init = function showConfig () {
  5. var app = this;
  6. $command = $('#command').val('');
  7. $params = $('#paramsContainer');
  8. $('#sendForm').submit(function (e) {
  9. var data = {},
  10. params = $params.find('.param-input');
  11. e.preventDefault();
  12. e.stopPropagation();
  13. data.q = $command.val();
  14. if (!data.q) {
  15. $params.addClass('new-message');
  16. return;
  17. }
  18. $params.removeClass('new-message');
  19. params.each(function () {
  20. var param = $(this),
  21. val = param.val(),
  22. name = param.attr('name');
  23. if (val !== '') {
  24. data.args = data.args || {};
  25. data.args[name] = param.attr('type') === 'number' ? parseInt(val, 10) : val;
  26. }
  27. });
  28. app.send('/cjdns', data, function (res) {
  29. //$command.val('');
  30. //$param.val('');
  31. });
  32. });
  33. this.getMethods();
  34. };
  35. App.prototype.getMethods = function getMethods () {
  36. var app = this,
  37. $methods = $('#methods'),
  38. paramTmpl = app.render([
  39. '<div><label>',
  40. '<span class="span3">%required%%name%%requiredClose%</span>',
  41. '<input class="param-input" name="%name%" value="" type="%type%" class="input-medium" placeholder="%paramtype%" />',
  42. '</label></div>'].join('')),
  43. paramTypes = {
  44. 'String': 'text',
  45. 'Int': 'number'
  46. };
  47. app.send('/methods', function (resp) {
  48. var methods = resp.methods,
  49. methodsSelect = [],
  50. methodParams = {};
  51. if (methods) {
  52. methodsSelect = methods.map(function (method, i) {
  53. var param,
  54. params = [];
  55. methodParams[method.name] = [];
  56. for (param in method.params) {
  57. if (method.params.hasOwnProperty(param)) {
  58. params.push('<em>' + method.params[param].type + '</em> ' + param + (method.params[param].required == 1 ? ', <em>required</em>' : ''));
  59. methodParams[method.name].push({
  60. name: param,
  61. type: method.params[param].type,
  62. required: method.params[param].required == 1
  63. });
  64. }
  65. }
  66. return {
  67. id: method.name,
  68. text: method.name,
  69. params: (params.join('<br />') || '<em>no params</em>')
  70. };
  71. });
  72. }
  73. function format (item) {
  74. return [
  75. '<div><strong>',
  76. item.id,
  77. '</strong></div>',
  78. '<div class="select-offset">',
  79. item.params,
  80. '</div>'
  81. ].join('');
  82. }
  83. $command.select2({
  84. data: methodsSelect,
  85. formatResult: format,
  86. formatSelection: format
  87. }).on('change', function (e) {
  88. var method = e.val,
  89. params = methodParams[method],
  90. paramsHtml = [];
  91. $params.removeClass('new-message');
  92. $params.empty();
  93. params.forEach(function (param) {
  94. paramsHtml.push(paramTmpl({
  95. name: param.name,
  96. type: paramTypes[param.type] || 'text',
  97. paramtype: param.type,
  98. required: param.required ? '<strong title="required">' : '',
  99. requiredClose: param.required ? '*</strong>' : ''
  100. }));
  101. });
  102. paramsHtml = paramsHtml.length > 0 ? paramsHtml.join('') : 'No params accepted';
  103. $params.html(paramsHtml);
  104. }).select2('enable', true);
  105. });
  106. };
  107. new App();
  108. } (window, jQuery, App));