Passed
Pull Request — master (#5574)
by David
09:40
created

more_text_script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
// This file is part of BOINC.
3
// https://boinc.berkeley.edu
4
// Copyright (C) 2024 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// show potentially long text with More/Less buttons
20
21
// output text; if significantly more than $nchars,
22
// show the first part and a More/Less button that toggles the remainder
23
//
24
// $text can't contain HTML tags (else would have to do lots of parsing)
25
//
26
function show_text_more($text, $nchars) {
27
    static $count = 0;
28
29
    $n = strlen($text);
30
    if ($n < $nchars) {
31
        echo $text;
32
        return;
33
    }
34
    // find where to break
35
    $b = strpos($text, ' ', $nchars);
36
    if ($b === false) {
37
        echo $text;
38
        return;
39
    }
40
41
    // don't break if tail is short
42
    if (($n - $b) < 40) {
43
        echo $text;
44
        return;
45
    }
46
47
    if ($count == 0) {
48
        more_text_script();
49
    }
50
    echo substr($text, 0, $b);
51
    echo sprintf('<span id="dots_%d">...</span>', $count);
52
    echo sprintf('<span id="more_%d">', $count);
53
    echo substr($text, $b);
54
    echo '</span>';
55
    echo sprintf(
56
        '<a onclick="toggle_more(\'dots_%d\', \'more_%d\', \'btn_%d\')" id="btn_%d"> (more)</a>',
57
        $count, $count, $count, $count
58
    );
59
    echo sprintf('
60
        <script>document.getElementById("more_%d").style.display = "none";</script>
61
        ',
62
        $count
63
    );
64
    $count++;
65
}
66
67
function more_text_script() {
68
    echo '
69
<script>
70
function toggle_more(dots_id, more_id, btn_id) {
71
    var dots = document.getElementById(dots_id);
72
    var moreText = document.getElementById(more_id);
73
    var btnText = document.getElementById(btn_id);
74
75
    if (dots.style.display === "none") {
76
        dots.style.display = "inline";
77
        btnText.innerHTML = " (more)";
78
        moreText.style.display = "none";
79
    } else {
80
        dots.style.display = "none";
81
        btnText.innerHTML = " (less)";
82
        moreText.style.display = "inline";
83
    }
84
}
85
</script>
86
';
87
}
88
89
?>
90