-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAresHtml.py
559 lines (445 loc) · 15.8 KB
/
AresHtml.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
""" Main module for the HTML wrappers
The htmlId is generated by the report layer to ensure that there is no overlap between the ID
This will also help on having comprehensive ID
Please make sure that all the CSS information are defined in a CSS class
"""
INDENT = ' '
class HtmlItem(object):
"""
Main Abstract class for the Html objects
Alias will be used by the Ares Report Interface to map the name to the class.
This alias will also be used in the HTML ID
"""
alias = None
jsEvent = None
def __init__(self, htmlId, cssCls=None):
""" Get the html object ID and store the CSS Class if passed """
self.__htmlId = htmlId
self.cssCls = cssCls
@property
def htmlId(self):
if self.alias is not None:
return "%s_%s" % (self.alias, self.__htmlId)
return self.__htmlId
def html(self):
"""
"""
raise NotImplementedError('subclasses must override html()!')
def js(self, evenType, jsDef):
"""
Common implementation to add javascript callback functions
This javascript wrapper include on purpose a defined set of javascript methods in order to control the calls
If some Ajax / DB calls are required, users will have to directly defined those items when they are writing
the python report
"""
if not evenType in getattr(self, 'jQueryEvent', []):
# This is a check to control the number of events per class
# Also because some of them might require specific display
print('Do not use any Ajax call or bespoke methods here')
print('In the function is not implemented yet please have a look at the call in AreHtml.py')
raise Exception('%s not defined for this %s!' % (evenType, self.__class__))
if self.jsEvent is None:
self.jsEvent = [(evenType, jsDef)]
def jsVal(self):
""" Return the Javascript Value """
return '%s.val()' % self.jsRef()
def jsRef(self):
""" Function to return the Jquery reference to the Html object """
if self.alias is None:
raise Exception('No valid Alias defined for %s!' % self.__class__)
return '$("#%s")' % self.htmlId
def jsOnLoad(self):
""" Functions which need to be run in the header """
pass
class Table(HtmlItem):
""" Wrapper for the HTML table
the cssCls class will be added to the table.
If some style is needed at row or column level this has to be done in the CSS Style sheet.
"""
headers = None
vals = None
alias = 'table'
def __init__(self, htmlId, cols, values, cssCls=None):
""" Set the content of the table """
super(Table, self).__init__(htmlId) # To get the HTML Id
self.headers = cols
self.vals = values
def html(self):
""" Return the HTML object for the table """
item = ['<table class="table">']
item.append('%s<thead><tr>' % INDENT)
for header in self.headers:
item.append('%s%s<th>%s</th>' % (INDENT, INDENT, header))
item.append('%s</tr></thead>' % INDENT)
for row in self.vals:
item.append("%s<tr>" % INDENT)
for val in row:
item.append("%s%s<td>%s</td>" % (INDENT, INDENT, val))
item.append("%s</tr>" % INDENT)
item.append('</table>')
return "\n".join(item)
class List(HtmlItem):
"""
"""
val = None
alias = 'list'
def __init__(self, htmlId, values):
"""
"""
super(List, self).__init__(htmlId) # To get the HTML Id
self.val = values
def html(self):
"""
"""
item = ['<ul class="list-group">']
for label, cnt in self.val:
item.append('%s<li class="list-group-item">%s<span class="badge">%s</span></li>' % (INDENT, label, cnt))
item.append('</ul>')
return "\n".join(item)
class DropDown(HtmlItem):
""" Wrapper for a Dropdowm HTML object
"""
val = None
title = None
jQueryEvent = ['click']
alias = 'dropDown'
def __init__(self, htmlId, title, values):
"""
"""
super(DropDown, self).__init__(htmlId) # To get the HTML Id
self.val = values
self.title = title # The default value
def html(self):
"""
"""
item = ['<div class="dropdown" id="%s">' % self.htmlId]
item.append('<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">%s<span class="caret"></span></button>' % self.title)
item.append('<ul class="dropdown-menu">')
for val in self.val:
item.append('%s<li><a href="#%s">%s</a></li>' % (INDENT, val[0], val[1]))
item.append('</ul>')
item.append('</div>')
return "\n".join(item)
def jsRef(self):
""" Function to return the Jquery reference to the Html object
For example to get the selected item from a javascript call back function
- alert($(this).text())
"""
return '$("#%s .dropdown-menu li")' % self.htmlId
class Select(HtmlItem):
"""
Basic wrapper to the Select HTML Tag
https://silviomoreto.github.io/bootstrap-select/examples/
For example to get a change on the Select Box Item in the Javascript call back method
- alert($(this).val()) ;
TODO: Extend the python object to handle multi select and all the cool features
"""
val = None
jQueryEvent = ['click', 'change']
alias = 'select'
def __init__(self, htmlId, values):
"""
Values should be list of tuple and the tuple should be a label and a list
for example:
[('Nodes', ['GBC', 'BNPPAR']), ]
"""
super(Select, self).__init__(htmlId) # To get the HTML Id
self.val = values
def html(self):
"""
"""
item = ['<select class="selectpicker" id="%s">' % self.htmlId]
for group, vals in self.val:
item.append('%s<optgroup label="%s">' % (INDENT, group))
for v in vals:
item.append('%s%s<option>%s</option>' % (INDENT, INDENT, v))
item.append('%s</optgroup>' % INDENT)
item.append('</select>')
return "\n".join(item)
class Div(HtmlItem):
""" Wrapper for a simple DIV tag
"""
val = None
cls = None
jQueryEvent = ['drop']
alias = 'div'
def __init__(self, htmlId, value, cssCls=None):
""" Set the div value """
super(Div, self).__init__(htmlId) # To get the HTML Id
self.val = value
self.cls = cssCls
def html(self):
""" Return the HMTL object of for div """
if self.cls is not None:
return '<div id="%s" class="%s">%s</div>' % (self.htmlId, self.cls, self.val)
return '<div id="%s">%s</div>' % (self.htmlId, self.val)
def JsVal(self):
""" Return the Javascript Value """
return '$("#%s").html()' % self.htmlId
class Container(Div):
""" Wrapper for a simple DIV container
"""
cls = 'container'
htmlObj = None
alias = 'container'
def __init__(self, htmlId, htmlObj, cssCls=None):
""" Set the div value """
super(Div, self).__init__(htmlId) # To get the HTML Id
self.htmlObj = htmlObj
if cssCls is not None:
self.cls = cssCls
def html(self):
""" Return the HMTL object of for div """
self.val = self.htmlObj.html()
return super(Container, self).html()
class Split(Div):
""" Wrapper for a bootstrap Grid
"""
cls = "container-fluid"
htmlObjs = None
alias = 'grid'
def __init__(self, htmlId, htmlObjLeft, htmlObjRight, cssCls=None):
""" Set the div value """
super(Div, self).__init__(htmlId) # To get the HTML Id
self.htmlObjs = [htmlObjLeft, htmlObjRight]
if cssCls is not None:
self.cls = cssCls
def html(self):
""" """
res = ['<div id="%s" class="%s">' % (self.htmlId, self.cls)]
res.append('%s<div class="row">' % INDENT)
for htmObj in self.htmlObjs:
res.append('%s%s<div class="col-lg-6">' % (INDENT, INDENT))
res.append('%s%s%s' % (INDENT, INDENT, htmObj.html()))
res.append('%s%s</div>' % (INDENT, INDENT))
res.append('%s</div>' % INDENT)
res.append('</div>')
return "\n".join(res)
class Graph(HtmlItem):
""" Wrapper to create a graph container """
dim = None
cssCls = 'span4'
def __init__(self, htmlId, width, height, withSgv=True, cssCls=None):
""" Store the HTML object dimension """
super(Graph, self).__init__(htmlId) # To get the HTML Id
self.dim = (width, height)
self.withSgv = withSgv
if cssCls is not None:
self.cssCls = cssCls
def html(self):
""" Return the Graph container for D£ and DVD3 """
if self.withSgv:
return '<div id="chart%s" class="%s">\n<svg width="%s" height="%s"></svg>\n</div>\n' % (self.htmlId, self.cssCls, self.dim[0], self.dim[1])
return '<div id="chart%s" class="%s"></div>\n' % (self.htmlId, self.cssCls)
def jsRef(self):
""" Function to return the Jquery reference to the Html object """
if self.withSgv:
return '$("#chart%s svg")' % self.htmlId
return '$("#chart%s")' % self.htmlId
class NestedTable(Table):
"""
"""
def html(self):
""" Return the HTML object for the table """
item = ['<table class="table">']
item.append('%s<thead><tr>' % INDENT)
for header in self.headers:
item.append('%s%s<th>%s</th>' % (INDENT, INDENT, header))
item.append('%s</tr></thead>' % INDENT)
for row in self.vals:
item.append("%s<tr>" % INDENT)
for val in row:
item.append("%s%s<td>%s</td>" % (INDENT, INDENT, val.html()))
item.append("%s</tr>" % INDENT)
item.append('</table>')
return "\n".join(item)
class Button(HtmlItem):
"""
"""
val = None
cssCls = None
jQueryEvent = ['click']
alias = 'button'
def __init__(self, htmlId, value, cssCls=None):
"""
"""
super(Button, self).__init__(htmlId) # To get the HTML Id
self.val = value
self.cssCls = cssCls
def html(self):
"""
"""
if self.cssCls is not None:
return '<button id="%s" type="button" class="btn %s">%s</button>' % (self.htmlId, self.cssCls, self.val)
return '<button id="%s" type="button" class="btn">%s</button>' % (self.htmlId, self.val)
class A(HtmlItem):
""" Wrapper for a Anchor HTML tag """
val, link = None, None
alias = 'anchor'
def __init__(self, htmlId, value, link, cssCls=None):
""" Set the div value """
super(A, self).__init__(htmlId) # To get the HTML Id
self.val = value
self.link = link
self.cssCls = cssCls
def html(self):
""" Return the HMTL object of for div """
if self.cssCls is not None:
return '<a href="%s" class="%s">%s</a>' % (self.link, self.val, self.cssCls)
return '<a href="%s">%s</a>' % (self.link, self.val)
class Text(HtmlItem):
"""
"""
cssCls = None
val = None
alias = 'text'
def __init__(self, htmlId, value, cssCls=None):
""" """
super(Text, self).__init__(htmlId) # To get the HTML Id
self.val = value
if cssCls is not None:
self.cssCls = cssCls
def html(self):
""" """
if self.cssCls is not None:
return '<font id="%s" class="%s">%s</font>' % (self.htmlId, self.cssCls, self.val)
return '<font id="%s">%s</font>' % (self.htmlId, self.val)
class Code(Text):
""" """
cssCls = ''
alias = 'code'
def html(self):
""" """
if self.cssCls is not None:
return '<pre><code id="%s" class="%s">%s</code></pre>' % (self.htmlId, self.cssCls, self.val)
return '<pre><code id="%s">%s</code></pre>' % (self.htmlId, self.val)
class Paragraph(HtmlItem):
"""
"""
cssCls = None
val = None
htmlObjs = None
alias = 'paragraph'
def __init__(self, htmlId, value, htmlObjs=None, cssCls=None):
""" """
super(Paragraph, self).__init__(htmlId) # To get the HTML Id
self.val = value
self.cssCls = cssCls
self.htmlObjs = htmlObjs
def html(self):
""" Return the HTML string for a paragraph including or not some other html object """
# For this object we can have a list of Text objects
pVal = self.val
if self.htmlObjs is not None:
for i, htmlObj in enumerate(self.htmlObjs):
pVal = pVal.replace("{%s}" % i, htmlObj.html())
if self.cssCls is not None:
return '<p id="%s" class="%s">%s</p>' % (self.htmlId, self.cssCls, pVal)
return '<p id="%s">%s</p>' % (self.htmlId, pVal)
class Input(HtmlItem):
"""
"""
val = None
name = None
alias = 'input'
jQueryEvent = ['blur']
def __init__(self, htmlId, name, value):
""" """
super(Input, self).__init__(htmlId) # To get the HTML Id
self.name = name
self.val = value
def html(self):
""" """
return '<input id="%s" type="text" name="%s" value="%s">' % (self.htmlId, self.name, self.val)
class TextArea(HtmlItem):
"""
"""
alias = 'textarea'
jsclick = False
def html(self):
""" Return the item with a text area and a button """
if not self.jsclick:
print('The jsRef method will return the value of the textarea')
raise Exception('The click method had to be defined for TextArea')
item = ['<div class="input-group">']
item.append('%s<textarea class="form-control custom-control" rows="3" style="resize:none" id="%s"></textarea>' % (INDENT, self.htmlId))
item.append('%s<span class="input-group-btn"><button class="btn btn-primary" id="%s_button"><span>Send</span></button></span>)' % (INDENT, self.htmlId))
item.append('</div>')
return "\n".join(item)
def jsVal(self):
""" Return the Javascript Value """
return '$("#%s").val()' % self.htmlId
def jsRef(self):
""" Function to return the Jquery reference to the Html object """
return '$("#%s_button")' % self.htmlId
def click(self, jsAction):
"""
"""
if self.jsEvent is None:
self.jsEvent = [('click', jsAction)]
else:
self.jsEvent.append(('click', jsAction))
self.jsclick = True
class Title(HtmlItem):
""" Wrapper for the HTML header tags
Tooltips functionality will require jquery-ui.js
"""
cssCls = None
val = None
alias = 'title'
def __init__(self, htmlId, dim, value, tooltips=False, cssCls=None):
""" Instanciate the object, define the level and add the class """
super(Title, self).__init__(htmlId) # To get the HTML Id
self.val = value
self.cssCls = cssCls
self.dim = dim
self.tooltips = tooltips
def html(self):
""" Return a header HTML Tag """
if self.cssCls is not None:
return '<H%s id="%s" class="%s">%s</H%s>' % (self.dim, self.htmlId, self.cssCls, self.val, self.dim)
return '<H%s id="%s">%s</H%s>' % (self.dim, self.htmlId, self.val, self.dim)
def jsOnLoad(self):
if self.tooltips:
return "$( document ).tooltip();"
class Modal(HtmlItem):
""" Wrapper to a simple model view """
class DatePicker(HtmlItem):
""" Wrapper to a Jquery Date picker object
This module will require jquery-ui.js to run correctly
"""
cssCls = None
val = None
alias = 'date'
def jsOnLoad(self):
return "%s.datepicker();" % self.jsRef
class DropZone(HtmlItem):
"""
"""
cssCls = None
val = None
alias = 'dropZone'
jsEvent = [('dragover', '''
event.originalEvent.stopPropagation();
event.originalEvent.preventDefault();
event.originalEvent.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
'''),
('drop', '''
event.originalEvent.stopPropagation();
event.originalEvent.preventDefault();
var files = event.originalEvent.dataTransfer.files; // FileList object.
//files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'</li>');
}
$('#list').html('<ul>' + output.join('') + '</ul>');
'''),
]
def html(self):
""" Return the Drop Zone component """
item = ['<div id="%s">Drop files here</div><output id="list"></output>' % self.htmlId]
return "\n".join(item)