wok rev 15573

wikiss: add Calc plugin
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Nov 28 19:25:13 2013 +0000 (2013-11-28)
parents 63b1f79f8bd9
children b96c11f50ce8
files wikiss/stuff/plugins/wkp_Calc.css wikiss/stuff/plugins/wkp_Calc.php
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/wikiss/stuff/plugins/wkp_Calc.css	Thu Nov 28 19:25:13 2013 +0000
     1.3 @@ -0,0 +1,31 @@
     1.4 +.bordercalc {
     1.5 +    background-color: #ccc;
     1.6 +    font-weight: bold;
     1.7 +    text-align: center;
     1.8 +    border: 1px solid #999;
     1.9 +    padding: 0;
    1.10 +}
    1.11 +
    1.12 +.inputcalc {
    1.13 +    border: none;
    1.14 +    width: 80px;
    1.15 +    font-size: 14px;
    1.16 +    padding: 2px;
    1.17 +}
    1.18 +
    1.19 +.inputcalc:hover {
    1.20 +    background-color: #eee;
    1.21 +}
    1.22 +
    1.23 +.inputcalc:focus {
    1.24 +    background-color: #ccf;
    1.25 +}
    1.26 +
    1.27 +.tablecalc {
    1.28 +    border-collapse: collapse;  
    1.29 +}
    1.30 +
    1.31 +.cellcalc {
    1.32 +    border: 1px solid #999;
    1.33 +    padding: 0;
    1.34 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/wikiss/stuff/plugins/wkp_Calc.php	Thu Nov 28 19:25:13 2013 +0000
     2.3 @@ -0,0 +1,173 @@
     2.4 +<?php # coding: utf-8
     2.5 +
     2.6 +class Calc
     2.7 +{
     2.8 +   public $description = "Feuille de calcul au format CSV";
     2.9 +   
    2.10 +   var $lines = 0, $rows = 0, $cnt = 0, $gotcalc = 0, $line = array();
    2.11 +   var $newCONTENT;
    2.12 +   function showcalc()
    2.13 +   {
    2.14 +      if ($this->lines > 1 && $this->rows > 1) {
    2.15 +         $id = "C".(100+$this->cnt++); 
    2.16 +         $this->newCONTENT .= "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet ".$id."</a></noscript>\n";
    2.17 +         $this->newCONTENT .= "<table id=\"".$id."\" class=\"tablecalc\"></table>\n";
    2.18 +         $this->newCONTENT .= "<script type=\"text/javascript\">\n";
    2.19 +         $this->newCONTENT .= "<!--\n";
    2.20 +         $this->newCONTENT .= "buildCalc(\"".$id."\",".$this->lines.",".$this->rows.");\n";
    2.21 +         for ($i = 1; $i <= $this->lines; $i++) {
    2.22 +            $this->line[$i] = preg_replace("/&lt;/","<",$this->line[$i]);
    2.23 +            for ($tmp = explode(";",$this->line[$i]), $j = 0; $j < count($tmp) -1; $j++) {
    2.24 +               if ($tmp[$j] == "") continue;
    2.25 +               $tmp[$j] = preg_replace("/\"/","\\\\\"",$tmp[$j]);
    2.26 +               $s = "setCell(document.getElementById(\"".$id;
    2.27 +               $this->newCONTENT .= $s.chr(ord('A')+$j).$i."\"), \"".$tmp[$j]."\")\n";
    2.28 +            }
    2.29 +         }
    2.30 +         $this->newCONTENT .= "//-->\n";
    2.31 +         $this->newCONTENT .= "</script>\n";
    2.32 +      }
    2.33 +      else for ($i = 1; $i <= $this->lines; $i++)
    2.34 +         $this->newCONTENT .= $this->line[$i]."\n";
    2.35 +      $this->rows = $this->lines = $this->gotcalc = 0;
    2.36 +   }
    2.37 +
    2.38 +   function formatEnd()
    2.39 +   {
    2.40 +      global $CONTENT;
    2.41 +      $headdone = $gotcalc = $showtail = $this->lines = 0;
    2.42 +      $this->newCONTENT = "";
    2.43 +      $CONTENT = preg_replace("/<br \/>/","<br />\n",$CONTENT);
    2.44 +      foreach (explode("\n", $CONTENT) as $current) {
    2.45 +      	 if ($current == "") continue;
    2.46 +         if (preg_match("/;<br \/>$/", $current)) {
    2.47 +            $gotcalc = 1;
    2.48 +            if (!$headdone) {
    2.49 +               $headdone = 1;
    2.50 +               $showtail = 1;
    2.51 +               $this->newCONTENT .= <<<EOT
    2.52 +<!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
    2.53 +<script type="text/javascript">
    2.54 +<!--
    2.55 +function csv(id,rows,cols) {
    2.56 +    var data = "";
    2.57 +    for (var i=1; i<=rows; i++) {
    2.58 +	for (var j=1; j<=cols; j++) {
    2.59 +            var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
    2.60 +	    data += document.getElementById(id+letter+i).title+';';
    2.61 +	}
    2.62 +	data += "\\n";
    2.63 +    }
    2.64 +    alert(data);
    2.65 +}
    2.66 +
    2.67 +var DATA={};
    2.68 +function buildCalc(id, rows, cols) {
    2.69 +    DATA[id] = {};
    2.70 +    var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
    2.71 +		  "floor", "log", "max", "min", "pow", "random", "round", "sin",
    2.72 +		  "tan", "sqrt", "PI", "E" ];
    2.73 +    for (var i=0; v = maths[i]; i++)
    2.74 +	eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
    2.75 +    DATA[id].rand = DATA[id].RAND = Math.random;
    2.76 +    DATA[id].ln   = DATA[id].LN   = Math.log;
    2.77 +    DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
    2.78 +    DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
    2.79 +    DATA[id].fact = DATA[id].FACT = 
    2.80 +	function(n){var x=1;while(n>1)x*=n--;return x;};
    2.81 +    DATA[id].fib  = DATA[id].FIB  = 
    2.82 +	function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
    2.83 +    for (var i=0; i<=rows; i++) {
    2.84 +        var row = document.getElementById(id).insertRow(-1);
    2.85 +        for (var j=0; j<=cols && j<=26; j++) {
    2.86 +            var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
    2.87 +	    var cell = row.insertCell(-1);
    2.88 +	    if (i&&j) {
    2.89 +		cell.className = "cellcalc";
    2.90 +		cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
    2.91 +	    }
    2.92 +	    else {
    2.93 +		cell.className = "bordercalc";
    2.94 +		cell.title = "Show CSV";
    2.95 +		cell.onclick = function(){csv(id,rows,cols);};
    2.96 +		cell.innerHTML = (i||j) ? i||letter : "&radic;";
    2.97 +	    }
    2.98 +        }
    2.99 +    }
   2.100 +}
   2.101 +
   2.102 +function getWidth(s)
   2.103 +{
   2.104 +	var e = document.getElementById("widthcalc");
   2.105 +	e.innerHTML = s+" :";
   2.106 +	return (e.offsetWidth < 80 || s.charAt(0) == "=") ? 80 : e.offsetWidth;
   2.107 +}
   2.108 +
   2.109 +function setCell(e, v)
   2.110 +{
   2.111 +    e.style.width = getWidth(v)+"px";
   2.112 +    e.style.textAlign = 
   2.113 +	(isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
   2.114 +    e.title = v;
   2.115 +}
   2.116 +//-->
   2.117 +</script>
   2.118 +<span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
   2.119 +EOT;
   2.120 +            }
   2.121 +            $this->line[++$this->lines] = $current;
   2.122 +            $current = preg_replace("/&lt;/","<",$current);
   2.123 +            $i = count(explode(";", $current))-1;
   2.124 +            if ($this->lines == 1) $this->rows = $i;
   2.125 +            if ($i != $this->rows) $this->rows = -1;
   2.126 +         }
   2.127 +         else {
   2.128 +            if ($gotcalc) $this->showcalc();
   2.129 +            $this->newCONTENT .= $current."\n";
   2.130 +         }
   2.131 +      }
   2.132 +      if ($gotcalc) $this->showcalc();
   2.133 +      if ($showtail) {
   2.134 +         $this->newCONTENT .= <<<EOT
   2.135 +<script type="text/javascript">
   2.136 +<!--
   2.137 +var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
   2.138 +INPUTS.forEach(function(elm) {
   2.139 +    elm.onfocus = function(e) {
   2.140 +        e.target.value = e.target.title || "";
   2.141 +    };
   2.142 +    elm.onblur = function(e) {
   2.143 +	setCell(e.target, e.target.value);
   2.144 +        computeAll();
   2.145 +    };
   2.146 +    var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
   2.147 +    var getter = function() {
   2.148 +        var value = elm.title || "";
   2.149 +        if (value.charAt(0) == "=")
   2.150 +		with (DATA[calcid]) return eval(value.substring(1));
   2.151 +        else return isNaN(parseFloat(value)) ? value : parseFloat(value);
   2.152 +    };
   2.153 +    Object.defineProperty(DATA[calcid], cellid, {get:getter});
   2.154 +    Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
   2.155 +});
   2.156 +(window.computeAll = function() {
   2.157 +    INPUTS.forEach(function(elm) {
   2.158 +	var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
   2.159 +	try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
   2.160 +})();
   2.161 +//-->
   2.162 +</script>
   2.163 +EOT;
   2.164 +      }
   2.165 +      $CONTENT = $this->newCONTENT;
   2.166 +   }
   2.167 +
   2.168 +   function template()
   2.169 +   {
   2.170 +      global $html;
   2.171 +      $html = preg_replace("/<\/head>/",
   2.172 +      "\t<style type=\"text/css\"> @import \"plugins/wkp_Calc.css\"; </style>\n</head>",
   2.173 +      $html);
   2.174 +   }
   2.175 +}
   2.176 +?>