slitaz-dev-tools view tazwikiss/rootfs/var/www/wiki/plugins/wkp_Calc.sh @ rev 251

tazwikiss/wkp_Calc.sh: fix double quote escape
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Dec 19 12:30:53 2015 +0100 (2015-12-19)
parents 15df7d51af7c
children
line source
1 plugin="Calc"
2 description_fr="Feuille de calcul au format CSV"
3 description="CSV format spreadsheet"
4 help_fr="AideCalc"
5 help="HelpCalc"
7 case "$1" in
8 showhead) cat <<EOT
9 <!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
10 <script type="text/javascript">
11 <!--
12 function csv(id,rows,cols) {
13 var data = "";
14 for (var i=1; i<=rows; i++) {
15 for (var j=1; j<=cols; j++) {
16 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
17 data += document.getElementById(id+letter+i).title+';';
18 }
19 data += "\\n";
20 }
21 alert(data);
22 }
24 function cnt(from,to) {
25 return (to.charCodeAt(0) - from.charCodeAt(0) + 1) *
26 (parseInt(to.substring(1)) - parseInt(from.substring(1)) + 1)
27 }
29 function zone(id,from,to,init,func) {
30 var result=init
31 for (var l=from.charCodeAt(0);;l++) {
32 for (var n=parseInt(from.substring(1));
33 n <= parseInt(to.substring(1));n++) {
34 var e=document.getElementById(id+String.fromCharCode(l)+n)
35 result=func(result,parseFloat(e.value))
36 }
37 if (l == to.charCodeAt(0)) break
38 }
39 return result;
40 }
42 var DATA={};
43 function buildCalc(id, rows, cols) {
44 DATA[id] = {};
45 var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
46 "floor", "log", "pow", "random", "round", "sin",
47 "tan", "sqrt", "PI", "E" ];
48 for (var i=0; v = maths[i]; i++)
49 eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
50 DATA[id].rand = DATA[id].RAND = Math.random;
51 DATA[id].ln = DATA[id].LN = Math.log;
52 DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
53 DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
54 DATA[id].fact = DATA[id].FACT =
55 function(n){var x=1;while(n>1)x*=n--;return x;};
56 DATA[id].fib = DATA[id].FIB =
57 function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
58 DATA[id].sum = DATA[id].SUM =
59 function(a,b){return zone(id,a,b,0,function(a,b){return a+b});};
60 DATA[id].min = DATA[id].MIN =
61 function(a,b){return zone(id,a,b,Number.MAX_VALUE,Math.min);};
62 DATA[id].max = DATA[id].MAX =
63 function(a,b){return zone(id,a,b,Number.MIN_VALUE,Math.max);};
64 DATA[id].cnt = DATA[id].CNT = cnt
65 for (var i=0; i<=rows; i++) {
66 var row = document.getElementById(id).insertRow(-1);
67 for (var j=0; j<=cols && j<=26; j++) {
68 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
69 var cell = row.insertCell(-1);
70 if (i&&j) {
71 cell.className = "cellcalc";
72 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
73 }
74 else {
75 cell.className = "bordercalc";
76 cell.title = "Show CSV";
77 cell.onclick = function(){csv(id,rows,cols);};
78 cell.innerHTML = (i||j) ? i||letter : "&radic;";
79 }
80 }
81 }
82 }
84 function getWidth(s)
85 {
86 var e = document.getElementById("widthcalc");
87 e.innerHTML = s;
88 return (e.offsetWidth < e.minWidth || s.charAt(0) == "=") ?
89 e.minWidth : e.offsetWidth;
90 }
92 function setCell(e, v)
93 {
94 e.style.width = getWidth(v)+"px";
95 e.style.textAlign =
96 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
97 e.title = v;
98 }
99 //-->
100 </script>
101 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
102 EOT
103 exit 0 ;;
104 showtail) cat <<EOT
105 <script type="text/javascript">
106 <!--
107 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
108 INPUTS.forEach(function(elm) {
109 elm.onfocus = function(e) {
110 e.target.value = e.target.title || "";
111 };
112 elm.onblur = function(e) {
113 setCell(e.target, e.target.value);
114 computeAll();
115 };
116 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
117 var getter = function() {
118 var value = elm.title || "";
119 if (value.charAt(0) == "=")
120 with (DATA[calcid]) return eval(value.substring(1));
121 else return (value == "" || isNaN(value)) ? value : parseFloat(value);
122 };
123 Object.defineProperty(DATA[calcid], cellid, {get:getter});
124 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
125 });
126 (window.computeAll = function() {
127 INPUTS.forEach(function(elm) {
128 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
129 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
130 })();
131 //-->
132 </script>
133 EOT
134 exit 0 ;;
135 esac
137 formatEnd()
138 {
139 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
140 function showcalc()
141 {
142 if (lines > 1 && rows > 1) {
143 id="C" (100+cnt++)
144 print "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet " id "</a></noscript>"
145 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
146 print "<script type=\"text/javascript\">"
147 print "<!--"
148 print "buildCalc(\"" id "\"," lines "," rows ");"
149 for (i = 1; i <= lines; i++) {
150 gsub("&lt;","<",line[i])
151 for (j = 1; j < split(line[i],tmp,";"); j++) {
152 if (tmp[j] == "") continue
153 gsub("\"","\\\"",tmp[j])
154 s = "setCell(document.getElementById(\"" id
155 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
156 print s c i "\"), \"" tmp[j] "\")";
157 }
158 }
159 print "//-->"
160 print "</script>"
161 }
162 else for (i = 1; i <= lines; i++) print line[i]
163 rows = lines = gotcalc = 0
164 }
165 {
166 if (/;<br \/>$/) {
167 gotcalc = 1
168 if (!headdone) {
169 headdone = 1
170 showtail = 1
171 system("/bin/sh " prg " showhead")
172 }
173 line[++lines] = $0
174 gsub("&lt;","<",$0)
175 i = split($0,tmp,";")-1
176 if (lines == 1) rows = i
177 if (i != rows) rows = -1
178 }
179 else {
180 if (gotcalc) showcalc()
181 print
182 }
183 }
184 END {
185 if (gotcalc) showcalc()
186 if (showtail) system("/bin/sh " prg " showtail")
187 }
188 ' <<EOT
189 $CONTENT
190 EOT
191 )
192 }
194 template()
195 {
196 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
197 $html
198 EOT
199 )
200 }