Initial commit of LuCI2
[project/luci2/ui.git] / luci2 / htdocs / luci2 / view / system.startup.js
1 L.ui.view.extend({
2     title: L.tr('Startup'),
3     execute: function() {
4         var allow_write = this.options.acls.startup;
5
6         return $.when(
7             L.system.initList(function(list) {
8                 /* filter init scripts with no start prio */
9                 for (var i = 0; i < list.length; i++)
10                 {
11                     if (typeof(list[i].start) != 'undefined')
12                         continue;
13
14                     list.splice(i--, 1);
15                 }
16
17                 var initTable = new L.ui.table({
18                     columns: [ {
19                         caption: L.tr('Start priority'),
20                         key:     'start'
21                     }, {
22                         caption: L.tr('Initscript'),
23                         key:     'name',
24                         width:   '90%'
25                     }, {
26                         caption: L.trc('Init script table heading', 'Enable'),
27                         key:     'enabled',
28                         format: function(v, n) {
29                             return $('<button />')
30                                 .attr('disabled', !allow_write)
31                                 .attr('name', list[n].name)
32                                 .addClass('cbi-button')
33                                 .addClass(v ? 'cbi-button-apply' : 'cbi-button-reset')
34                                 .text(v ? L.trc('Init script state', 'Enabled') : L.trc('Init script state', 'Disabled'))
35                                 .click(function() {
36                                     L.ui.loading(true);
37                                     if (v)
38                                         L.system.initDisable(this.getAttribute('name')).then(renderInitlist);
39                                     else
40                                         L.system.initEnable(this.getAttribute('name')).then(renderInitlist);
41                                 });
42                         }
43                     }, {
44                         caption: L.trc('Init script table heading', 'Restart'),
45                         key:     'enabled',
46                         format: function(v, n) {
47                             return $('<button />')
48                                 .attr('disabled', !allow_write)
49                                 .attr('name', list[n].name)
50                                 .addClass('cbi-button')
51                                 .addClass('cbi-button-reload')
52                                 .text(L.trc('Init script action', 'Restart'))
53                                 .click(function() {
54                                     L.ui.loading(true);
55                                     L.system.initRestart(this.getAttribute('name')).then(renderInitlist)
56                                 });
57                         }
58                     }, {
59                         caption: L.trc('Init script table heading', 'Stop'),
60                         key:     'enabled',
61                         format: function(v, n) {
62                             return $('<button />')
63                                 .attr('disabled', !allow_write)
64                                 .attr('name', list[n].name)
65                                 .addClass('cbi-button')
66                                 .addClass('cbi-button-remove')
67                                 .text(L.trc('Init script action', 'Stop'))
68                                 .click(function() {
69                                     L.ui.loading(true);
70                                     L.system.initStop(this.getAttribute('name')).then(renderInitlist)
71                                 });
72                         }
73                     } ]
74                 });
75
76                 initTable.rows(list);
77                 initTable.insertInto('#init_table');
78
79                 L.ui.loading(false);
80             }),
81             L.system.getRcLocal(function(data) {
82                 $('#maps').accordion({ heightStyle: 'content' });
83
84                 $('textarea').val(data).attr('disabled', !allow_write);
85                 $('input.cbi-button-save').attr('disabled', !allow_write).click(function() {
86                     var data = ($('textarea').val() || '').replace(/\r/g, '').replace(/\n?$/, '\n');
87                     L.ui.loading(true);
88                     L.system.setRcLocal(data, function() {
89                         $('textarea').val(data);
90                         L.ui.loading(false);
91                     });
92                 });
93             })
94         );
95     }
96 });