5951e92e51773b552c27d3fc7110a44beae995b1
[oweals/luci.git] /
1 'use strict';
2 'require form';
3 'require uci';
4 'require ui';
5 'require shadowsocks-libev as ss';
6
7 var conf = 'shadowsocks-libev';
8
9 return L.view.extend({
10         render: function() {
11                 var m, s, o;
12
13                 m = new form.Map(conf, _('Remote Servers'),
14                         _('Definition of remote shadowsocks servers.  \
15                                 Disable any of them will also disable instances referring to it.'));
16
17                 s = m.section(form.GridSection, 'server');
18                 s.addremove = true;
19                 s.handleLinkImport = function() {
20                         var textarea = new ui.Textarea();
21                         ui.showModal(_('Import Links'), [
22                                 textarea.render(),
23                                 E('div', { class: 'right' }, [
24                                         E('button', {
25                                                 class: 'btn',
26                                                 click: ui.hideModal
27                                         }, [ _('Cancel') ]),
28                                         ' ',
29                                         E('button', {
30                                                 class: 'btn cbi-button-action',
31                                                 click: ui.createHandlerFn(this, function() {
32                                                         textarea.getValue().split('\n').forEach(function(s) {
33                                                                 var config = ss.parse_uri(s);
34                                                                 if (config) {
35                                                                         var tag = config[1];
36                                                                         if (tag && !tag.match(/^[a-zA-Z0-9_]+$/)) tag = null;
37                                                                         var sid = uci.add(conf, 'server', tag);
38                                                                         config = config[0];
39                                                                         Object.keys(config).forEach(function(k) {
40                                                                                 uci.set(conf, sid, k, config[k]);
41                                                                         });
42                                                                 }
43                                                         });
44                                                         return uci.save()
45                                                                 .then(L.bind(this.map.load, this.map))
46                                                                 .then(L.bind(this.map.reset, this.map))
47                                                                 .then(L.ui.hideModal)
48                                                                 .catch(function() {});
49                                                 })
50                                         }, [ _('Import') ])
51                                 ])
52                         ]);
53                 };
54                 s.renderSectionAdd = function(extra_class) {
55                         var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments);
56                         el.appendChild(E('button', {
57                                 'class': 'cbi-button cbi-button-add',
58                                 'title': _('Import Links'),
59                                 'click': ui.createHandlerFn(this, 'handleLinkImport')
60                         }, [ _('Import Links') ]));
61                         return el;
62                 };
63
64                 o = s.option(form.Flag, 'disabled', _('Disable'));
65                 o.editable = true;
66
67                 ss.options_server(s);
68
69                 return m.render();
70         },
71         addFooter: function() {
72                 var p = '#edit=';
73                 if (location.hash.indexOf(p) === 0) {
74                         var section_id = location.hash.substring(p.length);
75                         var editBtn = document.querySelector('#cbi-shadowsocks-libev-' + section_id + ' button.cbi-button-edit');
76                         if (editBtn)
77                                 editBtn.click();
78                 }
79                 return this.super('addFooter', arguments);
80         }
81 });