slitaz-dev-tools rev 200
tazwikiss: add calc plugin
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun Nov 17 13:32:25 2013 +0000 (2013-11-17) |
parents | 37cc4a18435c |
children | 4a724f94f49f |
files | tazwikiss/rootfs/var/www/wiki/pages/AideCalc.txt tazwikiss/rootfs/var/www/wiki/pages/HelpCalc.txt tazwikiss/rootfs/var/www/wiki/plugins/wkp_Admin.sh tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.css tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.sh |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tazwikiss/rootfs/var/www/wiki/pages/AideCalc.txt Sun Nov 17 13:32:25 2013 +0000 1.3 @@ -0,0 +1,26 @@ 1.4 +!Aide sur le tableur CSV 1.5 +!!Creation 1.6 +Les tableaux sont créés à partir de données CSV avec au moins 2 lignes de 2 colonnes : 1.7 +* Les lignes CSV doivent se terminer avec un ''';''' 1.8 +* Les lignes CSV ont 2 ''';''' ou plus 1.9 +* Chaque ligne CSV a le même nombre de ''';''' 1.10 +* Chaque case est accessible par une lettre et un nombre, et peut contenir 1.11 +** Du texte 1.12 +** Un nombre 1.13 +** Une expression ''javascript'' débutant par '''=''' 1.14 +!!Usage 1.15 +* Déplacer la souris au dessus des cases pour voir le contenu 1.16 +* Cliquer et modifier les case blanches 1.17 +** Cliquer en dehors de la case pour tout recalculer 1.18 +* Cliquer sur une case grise pour voir le CSV à jour 1.19 +!!Exemples 1.20 +{{Pommes;12;=parseInt((100*B1)/B3)+'%'; 1.21 +Poires;7;=parseInt((100*B2)/B3)+"%"; 1.22 +Total :;=B1+B2;;}} 1.23 +Pommes;12;=parseInt((100*B1)/B3)+'%'; 1.24 +Poires;7;=parseInt((100*B2)/B3)+"%"; 1.25 +Total :;=B1+B2;; 1.26 +{{Un nombre;sa racine carré; 1.27 +2;=(A2<0)?alert('A2 < 0 !'):Math.sqrt(A2);}} 1.28 +Un nombre;sa racine carré; 1.29 +2;=(A2<0)?alert('A2 < 0 !'):Math.sqrt(A2);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tazwikiss/rootfs/var/www/wiki/pages/HelpCalc.txt Sun Nov 17 13:32:25 2013 +0000 2.3 @@ -0,0 +1,26 @@ 2.4 +!Help on the CSV spreadsheet 2.5 +!!Creation 2.6 +Spreadsheets are created from inlined CSV data with at least 2 rows and 2 lines : 2.7 +* The CSV lines must end with a ''';''' 2.8 +* The CSV lines have 2 or more ''';''' 2.9 +* Each CSV line have the same count of ''';''' 2.10 +* Each cell is addressed by a letter and a number, and may hold 2.11 +** Some text 2.12 +** A number 2.13 +** A ''javascript'' formula starting with '''=''' 2.14 +!!Usage 2.15 +* Move the mouse over the cells to see the contents 2.16 +* click and modify the white cells 2.17 +** click anywhere outside the cell to recompute everything 2.18 +* click to a gray cell to see the CSV update 2.19 +!!Examples 2.20 +{{Apples;12;=parseInt((100*B1)/B3)+'%'; 2.21 +Pears;7;=parseInt((100*B2)/B3)+"%"; 2.22 +Total :;=B1+B2;;}} 2.23 +Apples;12;=parseInt((100*B1)/B3)+'%'; 2.24 +Pears;7;=parseInt((100*B2)/B3)+"%"; 2.25 +Total :;=B1+B2;; 2.26 +{{A number;its square root; 2.27 +2;=(A2<0)?alert('A2 < 0 !'):Math.sqrt(A2);}} 2.28 +A number;its square root; 2.29 +2;=(A2<0)?alert('A2 < 0 !'):Math.sqrt(A2);
3.1 --- a/tazwikiss/rootfs/var/www/wiki/plugins/wkp_Admin.sh Sat Sep 21 22:03:00 2013 +0200 3.2 +++ b/tazwikiss/rootfs/var/www/wiki/plugins/wkp_Admin.sh Sun Nov 17 13:32:25 2013 +0000 3.3 @@ -92,7 +92,12 @@ 3.4 eval $(grep ^description= $i) 3.5 alt="$(grep ^description_$lang= $i)" 3.6 [ -n "$alt" ] && eval $(echo "$alt" | sed 's/_..=/=/') 3.7 + help= 3.8 + eval $(grep ^help= $i) 3.9 + alt="$(grep ^help_$lang= $i)" 3.10 + [ -n "$alt" ] && eval $(echo "$alt" | sed 's/_..=/=/') 3.11 name="$(basename $i .sh)" 3.12 + [ -n "$help" ] && description=" <a href='?page=$help'>$description</a>" 3.13 ;; 3.14 Locales) 3.15 j=${i#config-}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.css Sun Nov 17 13:32:25 2013 +0000 4.3 @@ -0,0 +1,31 @@ 4.4 +.bordercalc { 4.5 + background-color: #ccc; 4.6 + font-weight: bold; 4.7 + text-align: center; 4.8 + border: 1px solid #999; 4.9 + padding: 0; 4.10 +} 4.11 + 4.12 +.inputcalc { 4.13 + border: none; 4.14 + width: 80px; 4.15 + font-size: 14px; 4.16 + padding: 2px; 4.17 +} 4.18 + 4.19 +.inputcalc:hover { 4.20 + background-color: #eee; 4.21 +} 4.22 + 4.23 +.inputcalc:focus { 4.24 + background-color: #ccf; 4.25 +} 4.26 + 4.27 +.tablecalc { 4.28 + border-collapse: collapse; 4.29 +} 4.30 + 4.31 +.cellcalc { 4.32 + border: 1px solid #999; 4.33 + padding: 0; 4.34 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.sh Sun Nov 17 13:32:25 2013 +0000 5.3 @@ -0,0 +1,156 @@ 5.4 +plugin="Calc" 5.5 +description_fr="Tableur format CSV" 5.6 +description="CSV format spreadsheet" 5.7 +help_fr="AideCalc" 5.8 +help="HelpCalc" 5.9 + 5.10 +case "$1" in 5.11 +showhead) cat <<EOT 5.12 +<!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára --> 5.13 +<script type="text/javascript"> 5.14 +function csv(id,rows,cols) { 5.15 + var data = ""; 5.16 + for (var i=1; i<=rows; i++) { 5.17 + for (var j=1; j<=cols; j++) { 5.18 + var letter = String.fromCharCode("A".charCodeAt(0)+j-1); 5.19 + data += document.getElementById(id+letter+i).title+';'; 5.20 + } 5.21 + data += "\\\\n"; 5.22 + } 5.23 + alert(data); 5.24 +} 5.25 + 5.26 +var DATA={}; 5.27 +function buildCalc(id, rows, cols) { 5.28 + DATA[id] = {}; 5.29 + for (var i=0; i<=rows; i++) { 5.30 + var row = document.getElementById(id).insertRow(-1); 5.31 + for (var j=0; j<=cols && j<=26; j++) { 5.32 + var letter = String.fromCharCode("A".charCodeAt(0)+j-1); 5.33 + var cell = row.insertCell(-1); 5.34 + if (i&&j) { 5.35 + cell.className = "cellcalc"; 5.36 + cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>"; 5.37 + } 5.38 + else { 5.39 + cell.className = "bordercalc"; 5.40 + cell.title = "Show CSV"; 5.41 + cell.onclick = function(){csv(id,rows,cols);}; 5.42 + cell.innerHTML = (i||j) ? i||letter : "√"; 5.43 + } 5.44 + } 5.45 + } 5.46 +} 5.47 + 5.48 +function getWidth(s) 5.49 +{ 5.50 + var e = document.getElementById("widthcalc"); 5.51 + e.innerHTML = s+" :"; 5.52 + return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth; 5.53 +} 5.54 + 5.55 +function setCell(e, v) 5.56 +{ 5.57 + e.style.width = getWidth(v)+"px"; 5.58 + e.style.textAlign = 5.59 + (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right"; 5.60 + e.title = v; 5.61 +} 5.62 +</script> 5.63 +<span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span> 5.64 +EOT 5.65 + exit 0 ;; 5.66 +showtail) cat <<EOT 5.67 +<script type="text/javascript"> 5.68 +var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc")); 5.69 +INPUTS.forEach(function(elm) { 5.70 + elm.onfocus = function(e) { 5.71 + e.target.value = e.target.title || ""; 5.72 + }; 5.73 + elm.onblur = function(e) { 5.74 + setCell(e.target, e.target.value); 5.75 + computeAll(); 5.76 + }; 5.77 + var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4); 5.78 + var getter = function() { 5.79 + var value = elm.title || ""; 5.80 + if (value.charAt(0) == "=") 5.81 + with (DATA[calcid]) return eval(value.substring(1)); 5.82 + else return isNaN(parseFloat(value)) ? value : parseFloat(value); 5.83 + }; 5.84 + Object.defineProperty(DATA[calcid], cellid, {get:getter}); 5.85 + Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter}); 5.86 +}); 5.87 +(window.computeAll = function() { 5.88 + INPUTS.forEach(function(elm) { 5.89 + var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4); 5.90 + try { elm.value = DATA[calcid][cellid]; } catch(e) {} }); 5.91 +})(); 5.92 +</script> 5.93 +EOT 5.94 + exit 0 ;; 5.95 +esac 5.96 + 5.97 +formatEnd() 5.98 +{ 5.99 +CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh ' 5.100 +function showcalc() 5.101 +{ 5.102 + if (lines > 1 && rows > 1) { 5.103 + id="C" (100+cnt++) 5.104 + print "<noscript><u>Enable javascript to see the spreadsheet " id "</u></noscript>" 5.105 + print "<table id=\"" id "\" class=\"tablecalc\"></table>" 5.106 + print "<script type=\"text/javascript\">" 5.107 + print "buildCalc(\"" id "\"," lines "," rows ");" 5.108 + for (i = 1; i <= lines; i++) { 5.109 + gsub("<","<",line[i]) 5.110 + for (j = 1; j < split(line[i],tmp,";"); j++) { 5.111 + if (tmp[j] == "") continue 5.112 + gsub("\"","\\\\\"",tmp[j]) 5.113 + s = "setCell(document.getElementById(\"" id 5.114 + c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1) 5.115 + print s c i "\"), \"" tmp[j] "\")"; 5.116 + } 5.117 + } 5.118 + print "</script>" 5.119 + } 5.120 + else for (i = 1; i <= lines; i++) print line[i] 5.121 + rows = lines = gotcalc = 0 5.122 +} 5.123 +{ 5.124 + if (/;<br \/>$/) { 5.125 + gotcalc = 1 5.126 + if (!headdone) { 5.127 + headdone = 1 5.128 + showtail = 1 5.129 + system("/bin/sh " prg " showhead") 5.130 + #print "system(" prg " showhead)" 5.131 + } 5.132 + line[++lines] = $0 5.133 + gsub("<","<",$0) 5.134 + i = split($0,tmp,";")-1 5.135 + if (lines == 1) rows = i 5.136 + if (i != rows) rows = -1 5.137 + } 5.138 + else { 5.139 + if (gotcalc) showcalc() 5.140 + print 5.141 + } 5.142 +} 5.143 +END { 5.144 + if (gotcalc) showcalc() 5.145 + if (showtail) system("/bin/sh " prg " showtail") 5.146 +} 5.147 +' <<EOT 5.148 +$CONTENT 5.149 +EOT 5.150 +) 5.151 +} 5.152 + 5.153 +template() 5.154 +{ 5.155 + html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT 5.156 +$html 5.157 +EOT 5.158 +) 5.159 +}