wok-current view wikiss/stuff/plugins/wkp_Calc.php @ rev 15797
Up: clex (4.6.patch6)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Sun Jan 19 14:41:49 2014 +0100 (2014-01-19) |
parents | |
children | 019e831466f7 |
line source
1 <?php # coding: utf-8
3 class Calc
4 {
5 public $description = "Feuille de calcul au format CSV";
7 var $lines = 0, $rows = 0, $cnt = 0, $gotcalc = 0, $line = array();
8 var $newCONTENT;
9 function showcalc()
10 {
11 if ($this->lines > 1 && $this->rows > 1) {
12 $id = "C".(100+$this->cnt++);
13 $this->newCONTENT .= "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet ".$id."</a></noscript>\n";
14 $this->newCONTENT .= "<table id=\"".$id."\" class=\"tablecalc\"></table>\n";
15 $this->newCONTENT .= "<script type=\"text/javascript\">\n";
16 $this->newCONTENT .= "<!--\n";
17 $this->newCONTENT .= "buildCalc(\"".$id."\",".$this->lines.",".$this->rows.");\n";
18 for ($i = 1; $i <= $this->lines; $i++) {
19 $this->line[$i] = preg_replace("/</","<",$this->line[$i]);
20 for ($tmp = explode(";",$this->line[$i]), $j = 0; $j < count($tmp) -1; $j++) {
21 if ($tmp[$j] == "") continue;
22 $tmp[$j] = preg_replace("/\"/","\\\\\"",$tmp[$j]);
23 $s = "setCell(document.getElementById(\"".$id;
24 $this->newCONTENT .= $s.chr(ord('A')+$j).$i."\"), \"".$tmp[$j]."\")\n";
25 }
26 }
27 $this->newCONTENT .= "//-->\n";
28 $this->newCONTENT .= "</script>\n";
29 }
30 else for ($i = 1; $i <= $this->lines; $i++)
31 $this->newCONTENT .= $this->line[$i]."\n";
32 $this->rows = $this->lines = $this->gotcalc = 0;
33 }
35 function formatEnd()
36 {
37 global $CONTENT;
38 $headdone = $gotcalc = $showtail = $this->lines = 0;
39 $this->newCONTENT = "";
40 $CONTENT = preg_replace("/<br \/>/","<br />\n",$CONTENT);
41 foreach (explode("\n", $CONTENT) as $current) {
42 if ($current == "") continue;
43 if (preg_match("/;<br \/>$/", $current)) {
44 $gotcalc = 1;
45 if (!$headdone) {
46 $headdone = 1;
47 $showtail = 1;
48 $this->newCONTENT .= <<<EOT
49 <!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
50 <script type="text/javascript">
51 <!--
52 function csv(id,rows,cols) {
53 var data = "";
54 for (var i=1; i<=rows; i++) {
55 for (var j=1; j<=cols; j++) {
56 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
57 data += document.getElementById(id+letter+i).title+';';
58 }
59 data += "\\n";
60 }
61 alert(data);
62 }
64 var DATA={};
65 function buildCalc(id, rows, cols) {
66 DATA[id] = {};
67 var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
68 "floor", "log", "max", "min", "pow", "random", "round", "sin",
69 "tan", "sqrt", "PI", "E" ];
70 for (var i=0; v = maths[i]; i++)
71 eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
72 DATA[id].rand = DATA[id].RAND = Math.random;
73 DATA[id].ln = DATA[id].LN = Math.log;
74 DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
75 DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
76 DATA[id].fact = DATA[id].FACT =
77 function(n){var x=1;while(n>1)x*=n--;return x;};
78 DATA[id].fib = DATA[id].FIB =
79 function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
80 for (var i=0; i<=rows; i++) {
81 var row = document.getElementById(id).insertRow(-1);
82 for (var j=0; j<=cols && j<=26; j++) {
83 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
84 var cell = row.insertCell(-1);
85 if (i&&j) {
86 cell.className = "cellcalc";
87 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
88 }
89 else {
90 cell.className = "bordercalc";
91 cell.title = "Show CSV";
92 cell.onclick = function(){csv(id,rows,cols);};
93 cell.innerHTML = (i||j) ? i||letter : "√";
94 }
95 }
96 }
97 }
99 function getWidth(s)
100 {
101 var e = document.getElementById("widthcalc");
102 e.innerHTML = s+" :";
103 return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
104 }
106 function setCell(e, v)
107 {
108 e.style.width = getWidth(v)+"px";
109 e.style.textAlign =
110 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
111 e.title = v;
112 }
113 //-->
114 </script>
115 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
116 EOT;
117 }
118 $this->line[++$this->lines] = $current;
119 $current = preg_replace("/</","<",$current);
120 $i = count(explode(";", $current))-1;
121 if ($this->lines == 1) $this->rows = $i;
122 if ($i != $this->rows) $this->rows = -1;
123 }
124 else {
125 if ($gotcalc) $this->showcalc();
126 $this->newCONTENT .= $current."\n";
127 }
128 }
129 if ($gotcalc) $this->showcalc();
130 if ($showtail) {
131 $this->newCONTENT .= <<<EOT
132 <script type="text/javascript">
133 <!--
134 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
135 INPUTS.forEach(function(elm) {
136 elm.onfocus = function(e) {
137 e.target.value = e.target.title || "";
138 };
139 elm.onblur = function(e) {
140 setCell(e.target, e.target.value);
141 computeAll();
142 };
143 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
144 var getter = function() {
145 var value = elm.title || "";
146 if (value.charAt(0) == "=")
147 with (DATA[calcid]) return eval(value.substring(1));
148 else return isNaN(parseFloat(value)) ? value : parseFloat(value);
149 };
150 Object.defineProperty(DATA[calcid], cellid, {get:getter});
151 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
152 });
153 (window.computeAll = function() {
154 INPUTS.forEach(function(elm) {
155 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
156 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
157 })();
158 //-->
159 </script>
160 EOT;
161 }
162 $CONTENT = $this->newCONTENT;
163 }
165 function template()
166 {
167 global $html;
168 $html = preg_replace("/<\/head>/",
169 "\t<style type=\"text/css\"> @import \"plugins/wkp_Calc.css\"; </style>\n</head>",
170 $html);
171 }
172 }
173 ?>