This repository was archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
157 lines (151 loc) · 5.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// main.js
var React = require('react');
var ReactDOM = require('react-dom');
var $ = require('jquery');
var Nav = require('react-bootstrap/lib/Nav');
var Navbar = require('react-bootstrap/lib/Navbar');
var NavbarBrand = require('react-bootstrap/lib/NavbarBrand');
var NavbarHeader = require('react-bootstrap/lib/NavbarHeader');
var NavbarToggle = require('react-bootstrap/lib/NavbarToggle');
var NavbarBrand = require('react-bootstrap/lib/NavbarBrand');
var NavItem = require('react-bootstrap/lib/NavItem');
var MenuItem = require('react-bootstrap/lib/MenuItem');
var Panel = require('react-bootstrap/lib/Panel');
var Table = require('react-bootstrap/lib/Table');
var Grid = require('react-bootstrap/lib/Grid');
var Row = require('react-bootstrap/lib/Row');
var Col = require('react-bootstrap/lib/Col');
var Glyphicon = require('react-bootstrap/lib/Glyphicon');
var PrintJob = React.createClass({
render: function() {
var printerJobs = this.props.queue.map(function(job, i) {
return (
<tr key={i}>
<td>{job.queue_pos}</td>
<td>{job.user_id}</td>
<td>{job.filename}</td>
</tr>
);
});
return (
<Table fill striped>
<thead>
<tr>
<th className="table-queuepos">#</th>
<th className="table-username">Username</th>
<th className="table-filename">Filename</th>
</tr>
</thead>
<tbody>
{printerJobs}
</tbody>
</Table>
);
}
});
var PrinterPanel = React.createClass({
getDefaultProps: function() {
return {
data: []
};
},
render: function() {
var printerNodes = this.props.data.map(function(printer) {
var style = (function() {
if (printer.state === 1) {
return "success"
}
else if (printer.state === 2) {
return "info"
}
else if (printer.state === 3) {
return "warning"
}
else if (printer.state === 4) {
return "danger"
}
}).call(this);
var glyphState = (function() {
if (printer.state === 1) {
return "ok-sign"
}
else if (printer.state === 2) {
return "file"
}
else if (printer.state === 3) {
return "exclamation-sign"
}
else if (printer.state === 4) {
return "remove-sign"
}
}).call(this);
var title = ( <h3>{printer.name}<Glyphicon className="pull-right" glyph={glyphState} /></h3> );
return (
<Col md={4} key={printer.name}>
<Panel header={title} footer={printer.error} bsStyle={style}>
{function() {
if (printer.queue.length != 0) {
return <PrintJob fill queue={printer.queue} />
}
else {
return <p>Queue is currently empty.</p>
}
}.call(this)}
</Panel>
</Col>
);
});
return (
<div className="printerPanel">
{printerNodes}
</div>
);
}
});
var PrinterPage = React.createClass({
loadPrinterData: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
success: function(data) {
data.printers.sort(function (x, y) {
return y.queue.length - x.queue.length;
});
this.setState({data: data});
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentDidMount: function() {
this.loadPrinterData();
setInterval(this.loadPrinterData, this.props.pollInterval);
},
render: function() {
return (
<div className="mainContent">
<Navbar fluid>
<NavbarHeader>
<NavbarBrand>
<a href="#"><strong>RPI PrinterQ</strong></a>
</NavbarBrand>
</NavbarHeader>
<Nav pullRight>
<NavItem><strong>Last updated: {this.state.data.last_updated}</strong></NavItem>
</Nav>
</Navbar>
<Grid fluid>
<Row>
<PrinterPanel data={this.state.data.printers} />
</Row>
</Grid>
</div>
);
}
});
ReactDOM.render(
<PrinterPage url="/static/json/printers_data.json" pollInterval={20000} />,
document.getElementById('container')
);