luci2: implement Class.require() and Class.instantiate()
[project/luci2/ui.git] / luci2 / htdocs / luci2 / view / system.startup.js
1 L.ui.view.extend({
2         title: L.tr('Startup'),
3
4         getRcLocal: L.rpc.declare({
5                 object: 'luci2.system',
6                 method: 'rclocal_get',
7                 expect: { data: '' }
8         }),
9
10         setRcLocal: L.rpc.declare({
11                 object: 'luci2.system',
12                 method: 'rclocal_set',
13                 params: [ 'data' ]
14         }),
15
16         execute: function() {
17                 var self = this;
18                 var redraw = function() { return self.execute(); };
19                 var allow_write = self.options.acls.startup;
20
21                 return $.when(
22                         L.system.initList().then(function(list) {
23                                 /* filter init scripts with no start prio */
24                                 for (var i = 0; i < list.length; i++)
25                                 {
26                                         if (typeof(list[i].start) != 'undefined')
27                                                 continue;
28
29                                         list.splice(i--, 1);
30                                 }
31
32                                 var initTable = new L.ui.table({
33                                         columns: [ {
34                                                 caption: L.tr('Start priority'),
35                                                 key:     'start'
36                                         }, {
37                                                 caption: L.tr('Initscript'),
38                                                 key:     'name'
39                                         }, {
40                                                 key:     'enabled',
41                                                 format:  function(v, n) {
42                                                         return [
43                                                                 $('<div />')
44                                                                         .addClass('btn-group pull-right')
45                                                                         .append($('<button />')
46                                                                                 .attr('disabled', !allow_write)
47                                                                                 .attr('name', list[n].name)
48                                                                                 .addClass('btn btn-sm')
49                                                                                 .addClass(v ? 'btn-success' : 'btn-danger')
50                                                                                 .text(v ? L.trc('Init script state', 'Enabled') : L.trc('Init script state', 'Disabled'))
51                                                                                 .click(function() {
52                                                                                         L.ui.loading(true);
53                                                                                         if (v)
54                                                                                                 L.system.initDisable(this.getAttribute('name')).then(redraw);
55                                                                                         else
56                                                                                                 L.system.initEnable(this.getAttribute('name')).then(redraw);
57                                                                                 }))
58                                                                         .append($('<button />')
59                                                                                 .addClass('btn btn-primary btn-sm dropdown-toggle')
60                                                                                 .attr('data-toggle', 'dropdown')
61                                                                                 .attr('disabled', !allow_write)
62                                                                                 .text(L.tr('Action…')))
63                                                                         .append($('<ul />')
64                                                                                 .addClass('dropdown-menu pull-right')
65                                                                                 .append($('<li />')
66                                                                                         .append($('<a />')
67                                                                                                 .attr('href', '#')
68                                                                                                 .text(L.tr('Reload'))
69                                                                                                 .click(function(ev) { L.system.initReload(v).then(redraw); ev.preventDefault(); })))
70                                                                                 .append($('<li />')
71                                                                                         .append($('<a />')
72                                                                                                 .attr('href', '#')
73                                                                                                 .text(L.tr('Restart'))
74                                                                                                 .click(function(ev) { L.system.initRestart(v).then(redraw); ev.preventDefault(); })))
75                                                                                 .append($('<li />')
76                                                                                         .append($('<a />')
77                                                                                                 .attr('href', '#')
78                                                                                                 .text(L.tr('Stop'))
79                                                                                                 .click(function(ev) { L.system.initStop(v).then(redraw); ev.preventDefault(); }))))
80                                                         ];
81                                                 }
82                                         } ]
83                                 });
84
85                                 initTable.rows(list);
86                                 initTable.insertInto('#init_table');
87
88                                 L.ui.loading(false);
89                         }),
90                         self.getRcLocal().then(function(data) {
91                                 $('textarea').val(data).attr('disabled', !allow_write);
92                                 $('input.cbi-button-save').attr('disabled', !allow_write).click(function() {
93                                         var data = ($('textarea').val() || '').replace(/\r/g, '').replace(/\n?$/, '\n');
94                                         L.ui.loading(true);
95                                         self.setRcLocal(data).then(function() {
96                                                 $('textarea').val(data);
97                                                 L.ui.loading(false);
98                                         });
99                                 });
100                         })
101                 );
102         }
103 });