From 43ec42c6a1a8d6fc4902578805dd72f711c8f6d6 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 24 Mar 2016 01:42:04 +0000 Subject: [PATCH] Bed --- lib/css/db.css | 3 +++ lib/js/nb.js | 49 +++++++++++++++-------------------------- lib/php/benchmark.php | 14 +++++++++--- lib/php/db.php | 4 ++-- lib/php/db/index.php | 2 ++ lib/php/db/table.php | 19 ++++++++++++---- lib/php/functions.php | 4 ++-- lib/php/nb.php | 47 ++++++++++++++++++++++++++++++++++----- lib/php/out.php | 51 ++++++++++++++++++++++++++++--------------- lib/php/page.php | 13 ++++++++++- 10 files changed, 140 insertions(+), 66 deletions(-) diff --git a/lib/css/db.css b/lib/css/db.css index 2f22a7ee..5113a091 100644 --- a/lib/css/db.css +++ b/lib/css/db.css @@ -27,12 +27,15 @@ margin-bottom: 0.5em; padding: 0.2em; } +.Zdb div.rows .row li { +} @media screen and (min-width: 400px) { .db div.rows .row { width: 24%; display: inline-block; overflow: auto; + min-width: 190px; } } diff --git a/lib/js/nb.js b/lib/js/nb.js index e9a6b53f..cbf10a2d 100644 --- a/lib/js/nb.js +++ b/lib/js/nb.js @@ -48,42 +48,27 @@ function NB() { } this.maxhw = function(s) { - var height = $('body').height(); + var height = $(window).height(); var width = $(window).width(); - /* - var prev = $(s).prev(); - for (var i=0;i<50 && prev;i++) { - prev = $(prev).prev(); - if (prev) { - try { - height -= that.wh(prev)[1]; - } catch (e) { - prev = undefined; - break; - } - } - } - */ -//console.log('total: '+width+'/'+height); - - //if (prev) height -= that.wh(prev)[1]; - //if (prev) height -= $(prev).height(); $(s).each(function(){ - w = width;// - that.wh(this)[0]; - h = height;// - that.wh(this)[1]; - //if ($(this).height() > h) $(this).height(h); - //if ($(this).width() > w) $(this).width(w); -//$(this).css('border','solid 1px red'); + w = width; + h = height; + + // fixed margins + h -= 20; w -= 20; + + // remove what is above + var pos = $(this).position(); if (pos) h -= parseInt(pos.top); + + // remove what is after + $(this).next(). + add($(this).parent().next()) + .each(function(){ h -= $(this).height(); }); + + // apply $(this).height(h); $(this).width(w); - /* - if (1 || $(this).height() > h || $(this).width() > w) $(this).css({ - //'display': 'block', - 'overflow': 'scroll', - 'border': 'solid 2px blue', - }); - */ -//console.log(w+'/'+h); + $(this).css('overflow','auto'); }); } diff --git a/lib/php/benchmark.php b/lib/php/benchmark.php index 5dec29a2..8c4789c0 100755 --- a/lib/php/benchmark.php +++ b/lib/php/benchmark.php @@ -5,6 +5,7 @@ if (0) die( .' strlen: '.(strlen('text/html')-strlen('ml')) ); require(dirname(__FILE__).'/nb.php'); +$_GET['var'] = 'sldkfhlsakhlshglzkdhzfgldznfdznbldzknb'; /* $a = array('a' => 'A'); @@ -18,9 +19,16 @@ echo $first; exit; */ -#function __f1(&$ar) { $v=$ar; return; foreach ($ar as $v) { 1; } } -function __f1(&$ar) { return; foreach ($ar as $v) { 1; } } -function __f2($ar) { return; foreach ($ar as $v) { 1; } } +#function _preg_match() { preg_match('/zaza/',$_GET['var']); } nb::benchmark('_preg_match'); +#function _strpos() { strpos('zaza',$_GET['var']) !== false; } nb::benchmark('_strpos'); +#function _static() { static $var = null; if ($var===null) $var='zaza'; } nb::benchmark('_static'); +#function _global() { if ($_GLOBAL['var'] === null) $var='zaza'; } nb::benchmark('_global'); +function _empty() { empty($var); } nb::benchmark('_empty'); +function _isset() { isset($var); } nb::benchmark('_isset'); +nb::benchmark(); exit; + +#function __f1(&$ar) { return; foreach ($ar as $v) { 1; } } +#function __f2($ar) { return; foreach ($ar as $v) { 1; } } $array = array_fill(0,100000,"a"); #if (function_exists('__f1')) { function _f1() { __f1($array); } nb::benchmark('_f1'); } #if (function_exists('__f2')) { function _f2() { __f2($array); } nb::benchmark('_f2'); } diff --git a/lib/php/db.php b/lib/php/db.php index a0147334..2433f515 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -499,7 +499,7 @@ EOF; return true; } - public function action($table) { + public function action($table=null) { #if ($this->p('format') == 'table') $this->pset('format',''); $this->pdef('format',($this->php_cli() ? 'csv' : '')); $actions = explode(',',$this->p('action')); @@ -564,7 +564,7 @@ EOF; ; $return = true; - } else { + } elseif($table) { if ($r=$table->action($action)) $return = $r; } diff --git a/lib/php/db/index.php b/lib/php/db/index.php index fa8c0204..ee8c0c07 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -9,6 +9,8 @@ require_once(dirname(__FILE__).'/../config.php'); require_once(dirname(__FILE__).'/../db.php'); +if($format = out::client_type()) Db::pdef('format',$format); + # Param - Extract dbname from table if (preg_match('/^(\w+)\.(.*?)$/',Db::p('table'),$m)) { Db::pset('db',$m[1]); diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 16c57f78..194932f4 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -636,7 +636,6 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. if (!isset($opt['is_html'])) $opt['is_html'] = preg_match('/^(table|div)$/',$format) ? ( $this->p('header')!=="0" - #and $this->p('download')!=="1" ) : false ; @@ -644,7 +643,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. # Use the module out when format unknow $out_conf = null; if ($this->p('out') or !preg_match('/^('.join('|', - array( 'table','div','csv','xml','json','yaml','sql' ) + array( 'table','sql','div','_csv','_xml','_json','_yaml' ) ).')$/',$format)) { @@ -652,7 +651,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $this->params += array_values(out::$types); if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('',''); - debug('Using out module!'); + debug('Using out module!',1); } @@ -838,7 +837,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. Yaml -----------------------------------------------------------------*/ public function rows_begin_yaml() { return "---\n"; } - public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); } + #public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); } + public function rows_rec_yaml(&$row) { out::row(array("function"=>"out_yaml"),$row); } public function rows_end_yaml() { return ''; } /*----------------------------------------------------------------- @@ -1153,6 +1153,17 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog. $this->rows(array( 'format' => $this->p('format'))); return true; + + } elseif ($action == 'edit') { + return $this->html_edit(); + + } elseif ($this->p('table')) { + # NB 17.03.16 } else { + $this->pdef('limit','20'); + + // Rows + return $this->rows(); + } return false; diff --git a/lib/php/functions.php b/lib/php/functions.php index 97655bc1..22083257 100644 --- a/lib/php/functions.php +++ b/lib/php/functions.php @@ -119,7 +119,7 @@ function err($msg='__err__',$preff='err',$backtrace_deep=0) { } #nb::msg( !nb::php_cli() - nb::msg( preg_match('/ml/i',nb::get_header('Content-type')) + nb::msg( nb::client_header('Accept','ml') ? ''.$msg.''.NB_EOL @@ -132,7 +132,7 @@ function debug($msg,$level=0) { if ($level and $level>DEBUG) return; $msg = is_scalar($msg) ? $msg : print_r($msg,true); - if (preg_match('/ml/i',nb::get_header('Content-type'))) { + if (nb::client_header('Accept','ml')) { $msg = '
'
       .(isset($_SERVER['HTTP_HOST']) ? htmlentities($msg) : $msg)
 # NB 16.12.15       .(isset($_SERVER['HTTP_HOST']) ? htmlspecialchars($msg) : $msg)
diff --git a/lib/php/nb.php b/lib/php/nb.php
index d301f1d6..421ea072 100644
--- a/lib/php/nb.php
+++ b/lib/php/nb.php
@@ -149,14 +149,51 @@ class nb {
   }
 
   /*
-   * Function: get_header
+   * Function: server_header
+   */
+  public static function server_header($name=null,$match=null) {
+    static $header = null;
+    if ( $header === null ) {
+      $header = array();
+      foreach (headers_list() as $h) {
+        if ( preg_match("/^([^:]+)\s*:\s+(.*?)\s*$/",$h,$m) ) $header[$m[1]] = $m[2];
+      }
+    }
+
+    # Array
+    if ($name === null) return $header;
+
+    # Search
+    if ($match) {
+      foreach ($header as $k=>$v) { if (strpos($v,$match) !== false) return $v; }
+      return false;
+    }
+
+    # Key
+    return isset($header[$name]) ? $header[$name] : null;
+  }
+
+  /*
+   * Function: client_header
    * Return a client header
    */
-  public static function get_header($name,$value=null) {
-    foreach (headers_list() as $a) {
-      if ( preg_match("/^$name:\s+(.*)/i",$a,$m) ) return $m[1];
+  public static function client_header($name=null,$match=null) {
+    static $header = null;
+    if ( $header === null ) {
+      $header = function_exists('getallheaders') ? getallheaders() : array();
     }
-    return null;
+
+    # Array
+    if ($name === null) return $header;
+
+    # Search
+    if ($match) {
+      foreach ($header as $k=>$v) { if (strpos($v,$match) !== false) return $v; }
+      return false;
+    }
+
+    # Key
+    return isset($header[$name]) ? $header[$name] : null;
   }
 
   /*
diff --git a/lib/php/out.php b/lib/php/out.php
index 743ce46f..b47a2833 100644
--- a/lib/php/out.php
+++ b/lib/php/out.php
@@ -8,25 +8,24 @@ require_once('nb.php');
 
 class Out extends Nb {
 
-  public static $types = null; public static function types() {
+  public static $types; public static function types() {
     self::$types = array(
       'sql' => array(
       ),
-      'divs' => array(
+      'div' => array(
         'is_html' => true,
         'enclose' => array("
".NB_EOL,"
".NB_EOL), 'tag_enclose' => 'div class="row"', - #'tag_head' => 'span', - #'tag_head_enclose' => 'span class="row"', 'tag' => 'div', + 'tag_key' => 'label', 'function' => 'out_tag', 'head' => 'out_tag_head', ), 'center' => array( 'is_html' => true, - 'enclose' => array("
".NB_EOL,"
".NB_EOL), - 'tag_enclose' => 'div', - 'tag' => 'label', + 'enclose' => array("
".NB_EOL,"
".NB_EOL), + 'tag_enclose' => 'div class="row"', + 'tag' => 'tag', 'function' => 'out_tag', 'head' => 'out_tag_head', ), @@ -53,6 +52,7 @@ class Out extends Nb { 'yaml' => array( 'enclose' => array("---\n",""), 'function' => 'out_yaml', + #'function' => 'yaml_emit', ), 'json' => array( 'enclose' => array('['.NB_EOL,NB_EOL.']'), @@ -69,7 +69,7 @@ class Out extends Nb { public static function begin($o,$head,$data=array()) { #if (self::p('header')==="0") return; - if (!empty($o['enclose'])) echo $o['enclose'][0]; + if (isset($o['enclose'])) echo $o['enclose'][0]; if (!isset($o['head'])) return; #var_dump($head); return; @@ -90,7 +90,7 @@ class Out extends Nb { if (isset($o['function'])) { if (preg_match('/^out_/',$o['function'])) { - echo $o['function']($row,$o); + $o['function']($row,$o); } else { echo $o['function']($row); } @@ -101,12 +101,12 @@ class Out extends Nb { } public static function concat($o) { - if (!empty($o['rec'])) echo $o['rec']; - if (!empty($o['eol'])) echo $o['eol']; + if (isset($o['rec'])) echo $o['rec']; + if (isset($o['eol'])) echo $o['eol']; } public static function end($o) { - if (!empty($o['enclose'])) echo $o['enclose'][1]; + if (isset($o['enclose'])) echo $o['enclose'][1]; } public static function rows($type,&$data,$head=array()) { @@ -158,13 +158,22 @@ class Out extends Nb { if ($count) self::end($conf); } - public static function type($name,$key='') { + public static function type($name=null,$key='') { if (empty(self::$types[$name])) return null; $type = self::$types[$name]; if ($key) return( (array_key_exists($key,$type)) ? $type[$key] : null ); return $type; } + public static function client_type() { + foreach (array_keys(self::$types) as $t) { + if (preg_match("@^\w+/$t@",self::client_header('Accept'))) return $t; + #if (preg_match("@^\w+/$t@",self::client_header('Accept'),$m)) bye( $m ); + #if (self::client_header('Accept',$t)) return $t; + } + return; + } + } Out::types() ; # < Class /****************************************************************************/ @@ -176,7 +185,8 @@ function out_yaml(&$row,$o) { #echo out::yaml_encode($row); return; $yaml = '- '.out::yaml_encode($row); #$yaml = (out::is_hash($row) ? ' ' : '- ') . out::yaml_encode($row); - echo preg_replace("/^(?!-)/m"," ",$yaml); + $yaml = preg_replace("/^(?!-)/m"," ",$yaml); + echo $yaml; } function out_csv(&$row,$o) { @@ -206,6 +216,7 @@ function out_csv_head(&$row,$o) { #function out_tag_head(&$row,$o) { return ''; } function out_tag_head(&$data,$o) { #return var_dump($row,true); return ''; + #if (isset($o['tag_key'])) return ''; # No header, field will be in rows - NB 24.03.16 if (isset($o['tag_head'])) $o['tag'] = $o['tag_head']; $row = out::ar_first($data); if (!out::is_hash($row)) $row = $data; @@ -225,14 +236,20 @@ function out_tag(&$row,&$o) { if (isset($o['tag_enclose'])) echo (NB_EOL ? ' ' : '').'<'.$o['tag_enclose'].'>'.NB_EOL; foreach ($row as $k => $v) { - #if (!is_scalar($v)) continue; -# NB 07.03.16 if (!is_scalar($v)) return var_export($row,true); - if (!empty($o['tag'])) { + + # Inside tag, ex: label + if (isset($o['tag_key'])) { + $v = '<'.$o['tag_key'].'>'.$k.''.$v; + } + + # Tag = o[tag] or key + if (isset($o['tag'])) { $class = ' class="'.$k.'"'; $k = $o['tag']; } else { $class = ''; } + echo '' .(NB_EOL ? ' ' : '') ."<$k$class>$v" diff --git a/lib/php/page.php b/lib/php/page.php index 311b26a9..4489961f 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -423,5 +423,16 @@ class Page extends nb { $Page->title = join(' / ',$title); } -} return; + public static function html_embed($iframe,$mime='text/html') { + return ''.NB_EOL; + return '' + .''.NB_EOL + .''.NB_EOL + .''.NB_EOL + ; + #echo ''.NB_EOL; + #echo ''.NB_EOL; + } + +} return; # < Class ?> -- 2.47.3