]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 24 Mar 2016 01:42:04 +0000 (01:42 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 24 Mar 2016 01:42:04 +0000 (01:42 +0000)
lib/css/db.css
lib/js/nb.js
lib/php/benchmark.php
lib/php/db.php
lib/php/db/index.php
lib/php/db/table.php
lib/php/functions.php
lib/php/nb.php
lib/php/out.php
lib/php/page.php

index 2f22a7eec1c600de66cdbca29a2b5aeac8781df4..5113a0910192560d9f4f2c7cd2fead3da49e69c1 100644 (file)
   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;
   }
 }
 
index e9a6b53f1ebc2fcc33178c8589bfef42baee0f74..cbf10a2df746d8798ece38704d13af0b0bb08786 100644 (file)
@@ -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');
     });
   }
 
index 5dec29a2d1f988998d3cd7dea670f71d65a26f70..8c4789c0f89adc316a4194a536cabd61e3e0284e 100755 (executable)
@@ -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'); }
index a0147334cb2c387a4a044071391bdf23937bee74..2433f515eaad8055680a8d73cb2d5c271f79d877 100644 (file)
@@ -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;
 
         }
index fa8c0204f8b3cbd50f1a9b05f7dfb70783b3f927..ee8c0c0729c9056fdf88f846f98e42b1e98b371e 100755 (executable)
@@ -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]);
index 16c57f78a4b0aae582b27ab418195551b096c565..194932f4778cafbac5e527f0e0dd79d9bf21cf8f 100644 (file)
@@ -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;
index 97655bc159a6cf611e7d1b1cdc9e636ebe7f2263..22083257a958390ed5c5892013d9ee24c4c85f86 100644 (file)
@@ -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')
     ? '<pre'
         .' class="err'. ( ($preff and $preff!='err') ? " $preff" : '' ).'"'
       .'>'.$msg.'</pre>'.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 = '<pre class="debug">'
       .(isset($_SERVER['HTTP_HOST']) ? htmlentities($msg) : $msg)
 # NB 16.12.15       .(isset($_SERVER['HTTP_HOST']) ? htmlspecialchars($msg) : $msg)
index d301f1d6039957b975cabaf4c519cb2df2e8bdb5..421ea0726667d22bb888c2dec5e82b9259a8a2c4 100644 (file)
@@ -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;
   }
 
   /*
index 743ce46f161818babebe15edda938672421ab204..b47a28335b99b1bd49f827ac5e02d0084e163f06 100644 (file)
@@ -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("<div class=\"rows\">".NB_EOL,"</div>".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("<center>".NB_EOL,"</center>".NB_EOL),
-        'tag_enclose' => 'div',
-        'tag' => 'label',
+        'enclose' => array("<center class=\"rows\">".NB_EOL,"</center>".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.'</'.$o['tag_key'].'>'.$v;
+    }
+
+    # Tag = o[tag] or key
+    if (isset($o['tag'])) {
       $class = ' class="'.$k.'"';
       $k = $o['tag']; 
     } else {
       $class = '';
     }
+
     echo ''
       .(NB_EOL ? '    ' : '')
       ."<$k$class>$v</$k>"
index 311b26a906a5b58011ca1e338489f22b59e01512..4489961f427106de467b3069ebabf6f1a0c9cea5 100644 (file)
@@ -423,5 +423,16 @@ class Page extends nb {
     $Page->title = join(' / ',$title);
   }
 
-} return;
+  public static function html_embed($iframe,$mime='text/html') {
+    return '<iframe class="embed" src="'.$iframe.'"></iframe>'.NB_EOL;
+    return ''
+      .'<object class="embed" data="'.$iframe.'" type="'.$mime.'">'.NB_EOL
+      .'<embed src="'.$iframe.'" type="'.$mime.'" />'.NB_EOL
+      .'</object>'.NB_EOL
+    ;
+    #echo '<embed src="'.$iframe.'" />'.NB_EOL;
+    #echo '<object class="data border" data="'.$iframe.'" />'.NB_EOL;
+  }
+
+} return; # < Class
 ?>