-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodebookConcertina.php
139 lines (122 loc) · 6.59 KB
/
CodebookConcertina.php
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
<?php
/**
* REDCap External Module: Codebook Concertina
* Expand and collapse the codebook table rows for the fields of each instrument
* @author Luke Stevens, Murdoch Children's Research Institute
*/
namespace MCRI\CodebookConcertina;
use ExternalModules\AbstractExternalModule;
use REDCap;
class CodebookConcertina extends AbstractExternalModule
{
const DEFAULT_TEXT_SHOW = 'Expand';
const DEFAULT_TEXT_SHOW_ALL = 'Expand all instruments';
const DEFAULT_TEXT_HIDE = 'Collapse';
const DEFAULT_TEXT_HIDE_ALL = 'Collapse all instruments';
const DEFAULT_VISIBILITY = '1';
public function __construct() {
parent::__construct();
$this->disableUserBasedSettingPermissions();
}
public function redcap_every_page_top($project_id) {
if (isset($project_id) && intval($project_id)>0 && PAGE==='Design/data_dictionary_codebook.php') {
$btnTextShow = $this->getShowButtonText();
$btnTextShowAll = $this->getShowAllButtonText();
$btnTextHide = $this->getHideButtonText();
$btnTextHideAll = $this->getHideAllButtonText();
$defaultVisibility = $this->getDefaultVisibility();
// add js to page to add toggle buttons and hide/show
?>
<script type='text/javascript'>
(function(window, document, $) {
$(document).ready(function() {
var defaultVisibility = <?php echo $defaultVisibility;?>;
var icons = ['down', 'up'];
var btnLbl = ['<?php echo $btnTextShow;?>', '<?php echo $btnTextHide;?>'];
var btnLblAll = ['<?php echo $btnTextShowAll;?>', '<?php echo $btnTextHideAll;?>'];
var currentForm = '';
function btnLblText(visibility) {
return '<span class="glyphicon glyphicon-chevron-'+icons[visibility]+'"></span> '+btnLbl[visibility];
}
function btnLblAllText(visibility) {
return '<span class="glyphicon glyphicon-chevron-'+icons[visibility]+'"></span> '+btnLblAll[visibility];
}
var toggleRows = function() {
var $this = $(this);
var toggleForm = this.id;
var visible = btnLbl.indexOf($this.text().trim()); // visible when button says "Collapse"
if (visible) {
// collapse and switch button lbl to "Expand" when expanded
$this.html(btnLblText(0));
$('table.ReportTableWithBorder tr.'+toggleForm).hide();
} else {
// expand and switch button lbl to "Collapse" when collapsed
$this.html(btnLblText(1));
$('table.ReportTableWithBorder tr.'+toggleForm).show();
}
};
var toggleAllRows = function() {
var $this = $(this);
var toggleType = btnLblAll.indexOf($this.text().trim());
// trigger click on all buttons with text corresponding to the visibility e.g. if Collapse all, all the Collapse buttons
$('table.ReportTableWithBorder button.toggle-rows:contains("'+btnLbl[toggleType]+'")').trigger('click');
$this.html(btnLblAllText((toggleType)?0:1));
};
$('table.ReportTableWithBorder:first > tbody > tr').each(function() { // main table.ReportTableWithBorder trs only - ignore table.ReportTableWithBorder subtables for sql fields
var rowTDs = $(this).find('td');
if (rowTDs.length===0) {
// this is the th row - do nothing
} else if (rowTDs.length===1) {
// this is a form header row
// - extract the form ref from the final span (<span style="margin-left:10px;color:#444;">(instrument_name)</span>
// - add toggle button
currentForm = $(rowTDs[0]).find('span').last().html().replace('(','').replace(')','');
$('<button type="button" id="toggle-'+currentForm+'" class="btn btn-xs btn-primary toggle-rows" style="float:right;" data-toggle="button">'+btnLblText(defaultVisibility)+'</button>')
.on('click', toggleRows)
.appendTo(rowTDs[0]);
} else {
// this is a variable's tr
// - add a class to target with the toggle
// - hide if default is hidden
$(this).addClass("toggle-"+currentForm);
if (!defaultVisibility) { $(this).hide(); }
}
});
$('<button type="button" id="toggle-all-forms" class="btn btn-xs btn-primary" style="float:right;margin:5px;" data-toggle="button">'+btnLblAllText(defaultVisibility)+'</button>')
.on('click', toggleAllRows)
.insertBefore('table.ReportTableWithBorder:first');
});
})(window, document, jQuery);
</script>
<?php
}
}
protected function getDefaultVisibility() {
$defaultVisibility = $this->getProjectSetting('default-visibility');
if ($defaultVisibility!='0' && $defaultVisibility!='1') {
$defaultVisibility = self::DEFAULT_VISIBILITY;
$this->setProjectSetting('default-visibility', $defaultVisibility);
}
return $defaultVisibility;
}
protected function getShowButtonText() {
return $this->getButtonText('button-text-show', self::DEFAULT_TEXT_SHOW);
}
protected function getShowAllButtonText() {
return $this->getButtonText('button-text-show-all', self::DEFAULT_TEXT_SHOW_ALL);
}
protected function getHideButtonText() {
return $this->getButtonText('button-text-hide', self::DEFAULT_TEXT_HIDE);
}
protected function getHideAllButtonText() {
return $this->getButtonText('button-text-hide-all', self::DEFAULT_TEXT_HIDE_ALL);
}
protected function getButtonText($setting, $default) {
$btnText = $this->getProjectSetting($setting);
if ($btnText=='') {
$this->setProjectSetting($setting, $default);
$btnText = $default;
}
return REDCap::escapeHtml($btnText);
}
}