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

tazwikiss: fix a list side effect
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Nov 17 16:31:03 2013 +0000 (2013-11-17)
parents 73d6a8a85986
children d80619cb46db
line source
1 plugin="Calc"
2 description_fr="Tableur 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 var DATA={};
25 function buildCalc(id, rows, cols) {
26 DATA[id] = {};
27 for (var i=0; i<=rows; i++) {
28 var row = document.getElementById(id).insertRow(-1);
29 for (var j=0; j<=cols && j<=26; j++) {
30 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
31 var cell = row.insertCell(-1);
32 if (i&&j) {
33 cell.className = "cellcalc";
34 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
35 }
36 else {
37 cell.className = "bordercalc";
38 cell.title = "Show CSV";
39 cell.onclick = function(){csv(id,rows,cols);};
40 cell.innerHTML = (i||j) ? i||letter : "&radic;";
41 }
42 }
43 }
44 }
46 function getWidth(s)
47 {
48 var e = document.getElementById("widthcalc");
49 e.innerHTML = s+" :";
50 return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
51 }
53 function setCell(e, v)
54 {
55 e.style.width = getWidth(v)+"px";
56 e.style.textAlign =
57 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
58 e.title = v;
59 }
60 //-->
61 </script>
62 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
63 EOT
64 exit 0 ;;
65 showtail) cat <<EOT
66 <script type="text/javascript">
67 <!--
68 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
69 INPUTS.forEach(function(elm) {
70 elm.onfocus = function(e) {
71 e.target.value = e.target.title || "";
72 };
73 elm.onblur = function(e) {
74 setCell(e.target, e.target.value);
75 computeAll();
76 };
77 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
78 var getter = function() {
79 var value = elm.title || "";
80 if (value.charAt(0) == "=")
81 with (DATA[calcid]) return eval(value.substring(1));
82 else return isNaN(parseFloat(value)) ? value : parseFloat(value);
83 };
84 Object.defineProperty(DATA[calcid], cellid, {get:getter});
85 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
86 });
87 (window.computeAll = function() {
88 INPUTS.forEach(function(elm) {
89 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
90 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
91 })();
92 //-->
93 </script>
94 EOT
95 exit 0 ;;
96 esac
98 formatEnd()
99 {
100 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
101 function showcalc()
102 {
103 if (lines > 1 && rows > 1) {
104 id="C" (100+cnt++)
105 print "<noscript><u>Enable javascript to see the spreadsheet " id "</u></noscript>"
106 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
107 print "<script type=\"text/javascript\">"
108 print "<!--"
109 print "buildCalc(\"" id "\"," lines "," rows ");"
110 for (i = 1; i <= lines; i++) {
111 gsub("&lt;","<",line[i])
112 for (j = 1; j < split(line[i],tmp,";"); j++) {
113 if (tmp[j] == "") continue
114 gsub("\"","\\\\\"",tmp[j])
115 s = "setCell(document.getElementById(\"" id
116 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
117 print s c i "\"), \"" tmp[j] "\")";
118 }
119 }
120 print "//-->"
121 print "</script>"
122 }
123 else for (i = 1; i <= lines; i++) print line[i]
124 rows = lines = gotcalc = 0
125 }
126 {
127 if (/;<br \/>$/) {
128 gotcalc = 1
129 if (!headdone) {
130 headdone = 1
131 showtail = 1
132 system("/bin/sh " prg " showhead")
133 #print "system(" prg " showhead)"
134 }
135 line[++lines] = $0
136 gsub("&lt;","<",$0)
137 i = split($0,tmp,";")-1
138 if (lines == 1) rows = i
139 if (i != rows) rows = -1
140 }
141 else {
142 if (gotcalc) showcalc()
143 print
144 }
145 }
146 END {
147 if (gotcalc) showcalc()
148 if (showtail) system("/bin/sh " prg " showtail")
149 }
150 ' <<EOT
151 $CONTENT
152 EOT
153 )
154 }
156 template()
157 {
158 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
159 $html
160 EOT
161 )
162 }