]> git.nbdom.net Git - nb.git/commitdiff
js
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 4 Aug 2015 23:41:41 +0000 (00:41 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 4 Aug 2015 23:41:41 +0000 (00:41 +0100)
etc/vim/source/functions.vim
lib/css/db.css
lib/js/nb.js [new file with mode: 0644]
lib/js/tools.js [deleted file]
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php
lib/php/page.php

index 6614e3ed09b80d1bffa32eca69c3f08ad716c7db..d190251bac5e3c798e606a75df45b0118232fc85 100644 (file)
@@ -91,7 +91,7 @@ func! FileInit(...)
                let g:File['c'] = '//'
 
        elseif (g:File['type'] == 'php')
-               let g:File['c'] = '//'
+               let g:File['c'] = '#'
 
        elseif (g:File['type'] == 'sql')
                let g:File['c'] = '--'
index a2312007b128e052f45abe322d8644d247fa13c2..9eafc6497a75737ba13ea95c0a7e74291b4890f6 100644 (file)
@@ -13,14 +13,14 @@ form label
   margin: 0.3em 0.3em 0.3em 0;
 }
 
+  /*
 form label {
   display: inline-block;
-  width: 8em;
+  width: 7em;
   text-align: right;
 }
+  */
 
-/*
-*/
 /*
   List
 */
@@ -50,7 +50,8 @@ div.rows .row {
   margin-bottom: 0.5em;
   padding: 0.2em;
   display: inline-block;
-  width: 22em;
+  width: 21em;
+  width: 16em;
 }
 
 div.rows .nav {
@@ -116,6 +117,8 @@ form.db.edit {
 form.db.edit label {
   width: 20%;
   margin-bottom: 0.3em;
+  display: inline-block;
+  text-align: right;
 }
 
 form.db.edit span,
diff --git a/lib/js/nb.js b/lib/js/nb.js
new file mode 100644 (file)
index 0000000..cb8c8ef
--- /dev/null
@@ -0,0 +1,112 @@
+/*!
+ * NB JavaScript Library v0.0.0
+ *
+ * Date: 28.03.15
+ * See: http://www.w3schools.com/jquery/jquery_ref_selectors.asp
+ */
+function NB() {
+
+  this.form_submit_clean = function(f) {
+    var i = 0;
+    var msg = '';
+    var url = '';
+    msg += f.action + '\n';
+
+    for(i=0;i<f.length;i++) {
+      var p = f[i];
+      if (!p.name) continue;
+      if (p.value=='') continue;
+      if (p.value=='undefined') continue;
+      msg += p.name + '=' + p.value + '\n';
+  // NB 02.09.13               url += (url ? '&' : '?') + escape(p.name) + '=' + escape(p.value);
+      url += (url ? '&' : '?') + encodeURI(p.name) + '=' + encodeURI(p.value);
+    }
+
+    url = f.action + url;
+
+    //document.location = url;
+
+  /*
+  if (izi_opt['test_version']) {
+    html_msg(''
+      + '<a href="#" onclick="window.location=\''+(url)+'\'; return false;">'
+      + (url)
+      + '</a>'
+    );
+    return false;
+  }
+  */
+
+    //if (url == window.location) return false;
+    window.location = url;
+    return false;
+  }
+
+  this.form_clear = function(form) {
+    var e;
+    for(i=0;i<form.length;i++) {
+      e = form.elements[i];
+      if (e.style.display == 'none') continue;
+      if (e.type != 'text') continue;
+      e.value = ''
+    }
+  };
+
+  this.debug = function(test) {
+    console.log(test);
+  };
+
+  this.get_http = function(url) {
+
+    var xhr_content='';
+    // try when from other domain, ex google cache
+    try {
+      var xhr;
+      if(document.all && !window.opera) { //Internet Explorer
+        xhr = new ActiveXObject("Microsoft.XMLHTTP") ;
+        //xhr = new ActiveXObject("Msxml2.XMLHTTP") ;
+      } else { //Mozilla
+        xhr = new XMLHttpRequest();
+      }
+      xhr.onreadystatechange = function() {};
+      xhr.open("GET", url, false);
+      xhr.send(null);
+      if ((xhr.readyState == 4) && (xhr.status == 200)) xhr_content = xhr.responseText;
+    }
+
+    catch (e) { console.log (e) }
+    return xhr_content;
+
+  };
+
+  this.str2a = function(elems,compact=true,class_exp_file='dir|path') {
+    $(elems).each(function(){
+
+      html = $(this).html();
+      if (html == '') return;
+
+      // Search /home
+      url = html
+      if (html.match(/^~/)) {
+        ua = navigator.userAgent
+        if (ua.match(/Macintosh/)) { url = url.replace(/^~/,'/Users/') }
+        else                       { url = url.replace(/^~/,'/home/')  }
+        //console.log(url);
+      }
+      
+      if (class_exp_file) {
+        var re = new RegExp(class_exp_file);
+        if (this.className.match(re) && !url.match(/^\w+:/)) url = 'file://'+encodeURI(url);
+      }
+
+      if (compact) html = html.replace(/^((?:\w+:\/+)?[^\/]+\/)(.*?)(\/?[^\/]+)$/,'$1..$3');
+
+      $(this).html('<a href="'+url+'" class="small" target="_blank">'+html+'</a>');
+
+    });
+
+  };
+
+};
+
+//NB.prototype.your_method = function() { }
diff --git a/lib/js/tools.js b/lib/js/tools.js
deleted file mode 100644 (file)
index f92a247..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-/*!
- * NB JavaScript Library v0.0.0
- *
- * Date: 28.03.15
- */
-function NB() {
-
-  this.form_submit_clean = function(f) {
-    var i = 0;
-    var msg = '';
-    var url = '';
-    msg += f.action + '\n';
-
-    for(i=0;i<f.length;i++) {
-      var p = f[i];
-      if (!p.name) continue;
-      if (p.value=='') continue;
-      if (p.value=='undefined') continue;
-      msg += p.name + '=' + p.value + '\n';
-  // NB 02.09.13               url += (url ? '&' : '?') + escape(p.name) + '=' + escape(p.value);
-      url += (url ? '&' : '?') + encodeURI(p.name) + '=' + encodeURI(p.value);
-    }
-
-    url = f.action + url;
-
-    //document.location = url;
-
-  /*
-  if (izi_opt['test_version']) {
-    html_msg(''
-      + '<a href="#" onclick="window.location=\''+(url)+'\'; return false;">'
-      + (url)
-      + '</a>'
-    );
-    return false;
-  }
-  */
-
-    //if (url == window.location) return false;
-    window.location = url;
-    return false;
-  }
-
-  this.form_clear = function(form) {
-    var e;
-    for(i=0;i<form.length;i++) {
-      e = form.elements[i];
-      if (e.style.display == 'none') continue;
-      if (e.type != 'text') continue;
-      e.value = ''
-    }
-  };
-
-  this.debug = function(test) {
-    console.log(test);
-  };
-
-  this.get_http = function(url) {
-
-    var xhr_content='';
-    // try when from other domain, ex google cache
-    try {
-      var xhr;
-      if(document.all && !window.opera) { //Internet Explorer
-        xhr = new ActiveXObject("Microsoft.XMLHTTP") ;
-        //xhr = new ActiveXObject("Msxml2.XMLHTTP") ;
-      } else { //Mozilla
-        xhr = new XMLHttpRequest();
-      }
-      xhr.onreadystatechange = function() {};
-      xhr.open("GET", url, false);
-      xhr.send(null);
-      if ((xhr.readyState == 4) && (xhr.status == 200)) xhr_content = xhr.responseText;
-    }
-
-    catch (e) { console.log (e) }
-    return xhr_content;
-
-  };
-
-};
-
-//NB.prototype.your_method = function() { }
index c3dd045d92c323ab6cfd3e5b5356f2b1c3be95b6..1533ceefda1b1f785b4a746919e4f17d918c19ab 100644 (file)
@@ -10,12 +10,14 @@ require_once(dirname(__FILE__).'/functions.php');
 require_once(dirname(__FILE__).'/db/table.php');
 require_once(dirname(__FILE__).'/db/field.php');
 if (!defined('DB_HTML_FORM_BUTTONS')) define('DB_HTML_FORM_BUTTONS',''
-  .'<input type="button" class="button" value="Add" onclick="window.location = \'?action=edit&amp;table=\'+this.parentElement.elements[\'table\'].value"/>'
   #.'<input type="button" class="button" value="Add" onclick="this.setAttribute(\'type\',\'submit\');this.setAttribute(\'name\',\'edit\')?1:1"/>'
   #.'<input type="button" class="button" value="Add" onclick="this.setAttribute(\'name\',\'edit\');this.parentElement.submit()?0:0"/>'
   #.'<input type="button" class="button" onclick="return nb.form_clear(this.parentElement)" value="X"/>'
-  .'<a class="button" href="#" onclick="nb.form_clear(this.parentElement)?0:0">X</a>'
-  .'<input type="submit" class="button" value="GO"/>'
+// NB 04.08.15   .'<a class="button form_clear" href="#" onclick="nb.form_clear(this.parentElement)?0:0">X</a>'
+  .'<span style="display:inline-block;">'
+  .'<input type="submit" class="button submit" value="GO"/>'
+  .'<input type="button" class="button table_add" value="Add" onclick="window.location = \'?action=edit&amp;table=\'+this.parentElement.elements[\'table\'].value"/>'
+  .'</span>'
 );
 #if (!defined('DB_HTML_BUTTON_SUBMIT')) define('DB_HTML_BUTTON_SUBMIT','<input type="submit" class="button" value="GO"/>');
 #if (!defined('DB_HTML_BUTTON_ADD'))    define('DB_HTML_BUTTON_ADD','<a class="button" href="?" onclick="nb.form_clear(this.parentElement) ? 0: 0">Add</a>');
@@ -263,30 +265,29 @@ EOF;
 
   function html_menu($table=null) {
 
-    $html = ' onchange="this.parentElement.submit()"';
-    $r = '<form class="db menu" method="get" action="" onsubmit="return nb.form_submit_clean(this)">'.PHP_EOL;
+    $r = '<form class="db menu" method="get" action="">'.PHP_EOL;
 
     // Tables
-    $r .= '<label>Tables</label>'.html_select_array($this->tables(),array(
-      'html'       => 'class="tables" name="table"'.preg_replace('/"/','"nb.form_clear(this.parentElement);',$html,1),
+    $r .= '<label for="table">Tables</label>'.html_select_array($this->tables(),array(
+      'html'       => 'class="tables" name="table" id="table"',
       'selected'   => $this->p('table'),
       'prettyText' => true,
     ));
 
     // Format
-    $r .= '<label>Format</label>'.html_select_array(array(
+    $r .= '<label for="format">Format</label>'.html_select_array(array(
       'table','div','csv','xml','json','yaml'
     ),array(
-      'html'       => 'class="format" name="format"'.$html,
+      'html'       => 'class="format" name="format" id="format"',
       'selected'   => $this->p('format'),
       'prettyText' => true,
     ));
 
     // Limit
-    $r .= '<label>Limit</label>'.html_select_array(array(
+    $r .= '<label for="limit">Limit</label>'.html_select_array(array(
       '10','20','50','100','500','1000'
     ),array(
-      'html'       => 'class="limit" name="limit"'.$html,
+      'html'       => 'class="limit" name="limit" id="limit"',
       'selected'   => $this->p('table'),
       'prettyText' => true,
     ));
@@ -302,11 +303,11 @@ EOF;
         $v = $this->p($k);
 
         $r .= ''
-          #.'<span class="i">'
+          .'<span class="label '.$k.'">'
           . '<label>'.prettyText($k)
           .'</label>'
           . '<input type="text" id="'.$k.'" name="'.$k.'" value="'.$v.'" />'
-          #.'</span>'
+          .'</span>'
         ;
 
       }
@@ -314,6 +315,11 @@ EOF;
       #$r .= '<span class=""></span>';
     }
 
+    $r .= ''
+      .'<input type="hidden" name="db" value="'.$this->p('db').'"/>'
+      .'<input type="hidden" name="debug" value="'.$this->p('debug').'"/>'
+    ;
+
     // Bye
     $r .= '</form>'.PHP_EOL;
     return $r;
index 59a3ce43f35125df838babc82c944cac03bc1ec6..f7ea589d95526f7f0534347887b6d4928a22278a 100644 (file)
@@ -34,13 +34,12 @@ class field {
 
   function html_edit($value) {
 
-    $value = htmlspecialchars($value);
-    return '<label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
+    return '<span class="label '.$this->name.'"><label for="'.$this->name.'">'.htmlspecialchars(prettyText($this->name)).'</label>'
       .($this->extra
-        ? "<span class=\"field.extra\">$value</span>"
+        ? '<span class="extra" id="'.$this->name.'">'.$this->out($value).'</span>'
         : '<input name="'.$this->name.'" id="'.$this->name.'" value="'.$value.'" type="text"/>'
       )
-    .PHP_EOL;
+    .'</span>'.PHP_EOL;
 
   }
 
@@ -142,9 +141,10 @@ class field {
 
   function out($value,$html_escape=false) {
 
+    #if ($value === null) return '';
     static $sqlite = null; if ($sqlite === null) $sqlite = $this->db()->type == 'sqlite';
 
-    # Anoying !
+    # Anoying, sqlite does not format properly !
     if ($sqlite and preg_match('/^float\((?:\d+,)?(\d+)\)/',$this->type,$m)) {
       $value = sprintf('%.'.$m[1].'f',$value);
     }
index 1bfae6869193b960219765f092ad2282db171f4d..16a152ceb22a376e688004cb91c65c04dafe02b7 100644 (file)
@@ -23,8 +23,8 @@ class table {
   public $name;
   public $db;
   public $sql;
-  public $fields = array();
-  public $fields_keys = array();
+  public $fields = null;
+  public $fields_keys = null;
   public $extras = array();
   public $params = array(
     'table',
@@ -92,7 +92,7 @@ class table {
    */
   function fields($name=null) {
 
-    if (!$this->fields) {
+    if ($this->fields === null) {
 
       if ($this->db->type == 'sqlite') {
         $sql = "PRAGMA table_info('$this->name')";
@@ -121,6 +121,15 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
 
       foreach ($rows as $row) {
 
+        $row = array_change_key_case($row,CASE_LOWER);
+
+        // Mysql field name
+        if (isset($row['field'])) {
+          $row['name'] = $row['field'];
+          unset($row['field']);
+        }
+
+#bye($row);
         $field = array(
           'name'          => $row['name'],
           'extra'         => null,
@@ -141,7 +150,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
         # Autoincrement
 
         # mysql autoincrement
-        if (isset($row['Extra']) and $row['Extra'] == 'auto_increment') {
+        if (isset($row['extra']) and $row['extra'] == 'auto_increment') {
           $field['autoincrement'] = true;
 
         # sqlite autoincrement
@@ -173,17 +182,19 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
           $field['default'] = $row[$f];
         }
 
-        foreach (array('pk','Key') as $f) {
+        foreach (array('pk','key') as $f) {
           if (!isset($row[$f])) continue;
           $field['key'] = preg_match('/^1|yes|t/i',$row[$f]) ? 1 : 0;
         }
 
-        $this->fields[$row['name']] = new field($field);
+        $this->fields[$field['name']] = new field($field);
 
       }
 
+      if (empty($this->fields)) bye($this->name." does not exists!");
     }
 
+#bye($this->fields);
     if ($name !== null ) {
       if (!isset($this->fields[$name])) return null;
       return $this->fields[$name];
@@ -206,6 +217,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       ;
     }
 
+    if (($v = $this->db->p('db'))) $url[] = 'db='.$this->db->p('db');
     return $url ? '?table='.$this->db->p('table').$sep.join($sep,$url) : '';
   }
 
@@ -266,7 +278,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       .'<div class="db buttons">'
       .'<input type="submit"/>'
       .'<input type="hidden" name="table" value="'.$this->db->p('table').'"/>'
-      .'<input type="hidden" name="action" value="'.($add ? 'insert' : 'update').'"/>'
+      .'<input type="hidden" name="db" value="'.$this->db->p('db').'"/>'
+      .'<input type="hidden" name="debug" value="'.$this->db->p('debug').'"/>'
       .'<input type="reset" />'
       .( empty($_SERVER['HTTP_REFERER']) ? '' : '<input type="button" onclick="document.location=document.referrer" value="Cancel" />')
       .'</div>'.PHP_EOL
@@ -372,7 +385,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   }
 
   function form_criterias($opt=array()) {
-  // Legacy from sem - NB 25.07.15
+  // Legacy from sem - NB 25.07.15 !!!!!!!!!!!!!!!!!!!
     
     echo '<form class="criteria" method="get" action="">'.PHP_EOL;
     echo '<div class="small help">Use: '.join(' | ',$this->db->help_criterias).'</div>'.PHP_EOL;
@@ -389,8 +402,10 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       $v = empty($_REQUEST[$k]) ? '' : $_REQUEST[$k];
 
       $criteria[] = ''
-        . '<label>'.prettyText($k).':</label>'
+        .'<span class="label '.$k.'">'
+        . '<label for="'.$k.'">'.prettyText($k).':</label>'
         . '<input type="text" id="'.$k.'" name="'.$k.'" value="'.$v.'" />'
+        .'</span>'
       ;
 
     }
@@ -589,7 +604,7 @@ 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) ? true : false;
 
     $escape = preg_match('/^(table|row|xml)$/',$format) ? true : false;
-    if (preg_match('/^(1)?$/',$this->db->p('header'))) echo $this->{"rows_begin_$format"}($opt);
+    if (preg_match('/^(1)?$/',$this->db->p('header'))) echo $this->{"rows_begin_$format"}($this->fields());
 
     $count = 0;
     while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
@@ -597,8 +612,11 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
       $count_fields = 0;
 
       foreach ($this->fields() as $f => $field) {
+      #foreach ($row as $f => $value) {
 
+        #$field = $this->fields($f);
         #echo '<pre>'; var_dump($field); echo '</pre>';
+#bye(array($field,$row));
         $row[$f] = $field->out($row[$f]);
 // NB 02.08.15         if ($escape and empty($this->extra)) $row[$f] = htmlspecialchars($row[$f]);
 
@@ -703,8 +721,8 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
   /*-----------------------------------------------------------------
     Csv
   -----------------------------------------------------------------*/
-  function rows_begin_csv() {
-    return join("\t",array_keys($this->fields()))."\n";
+  function rows_begin_csv($fields) {
+    return join("\t",array_keys($fields))."\n";
   }
 
   function rows_rec_csv($row) {
@@ -803,7 +821,7 @@ FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.
     foreach ($row as $k => $v) {
       $html .= '<li>'
         .( $k == '0' ? '' : '<label>'.prettyText($k).'</label>')
-        .'<span>'.$v.'</span>'
+        .'<span class="'.$k.'">'.$v.'</span>'
         .'</li>';
     }
 
index dea7980df0991b46183eec9eff82c9160ad9718e..664608d01edbe903252c5f1ba48b1d295c0eb693 100644 (file)
@@ -19,7 +19,7 @@ $Page = new Page(array(
 class Page {
 
   public $call = array();
-  public $output = '';
+# NB 04.08.15 wtf ??? public $output;
 
   public $charset = 'utf-8';
   public $content_type = 'text/html';
@@ -32,8 +32,10 @@ class Page {
   public $js = array();
   public $js_code = '';
 
-  public $body_class;
-  public $body_id;
+  public $body_class = '';
+  public $body_id = '';
+
+  public $h1 = '';
 
   /*
     Create
@@ -47,13 +49,7 @@ class Page {
     }
     
     // Defaults
-    if ( ! isset($opt['title']) )
-      #$this->title = $GLOBALS['argv'][0]
-      $this->title = $this->filename2title();
-      #bye($this->title);
-      #$this->title = preg_replace('@^.*?([^/\.]+)[^/]*@','\1',$_SERVER['SCRIPT_NAME']);
-      #die( $this->title);
-    ;
+    if ( empty($this->title) ) $this->title = $this->filename2title();
 
     //// Default
     #print_r($this);
@@ -87,6 +83,7 @@ class Page {
 
   function filename2title() {
     $str = '';
+    #$this->title = $GLOBALS['argv'][0]
     if (isset($_SERVER['REQUEST_URI'])) $str = preg_replace('/\?.*$/','',preg_replace(',^.*?([^/]+)(\/?.*)?$,','\1',$_SERVER['REQUEST_URI']));
     if ($str == '' or $str == '/') $str = preg_replace('@^.*?([^/\.]+)(/index)?(\..*?)$@','\1',$_SERVER['SCRIPT_NAME']);
     $str = preg_replace('@[\'_-]+$@',' ',$str);
@@ -150,7 +147,8 @@ class Page {
     return htmlspecialchars($v,$replace_flags,$charset);
   }
 
-  function tag($content='',$tag,$attrs='') {
+# NB 04.08.15   function tag($content='',$tag,$attrs='') {
+  function tag($tag,$content='',$attrs='') {
     
     if (!is_array($content)) $content = array($content);
     $tags = array();
@@ -207,7 +205,9 @@ class Page {
         .(empty($this->body_class) ? '' : ' class="'.$this->body_class.'"')
         .(empty($this->body_id)    ? '' : ' id="'.$this->body_id.'"')
       .'>' . PHP_EOL;
-      if ($this->title) echo $this->tag($this->title,'h1').PHP_EOL;
+
+      if     ($this->h1)    echo $this->tag('h1',$this->h1).PHP_EOL;
+      elseif ($this->title) echo $this->tag('h1',$this->title).PHP_EOL;
     }
 
   }
@@ -273,7 +273,7 @@ class Page {
       $head .= '<head>'.PHP_EOL;
       #$head .= '</head>'.PHP_EOL; return $head;
 
-      if ($this->title) $head .= $this->tag($this->title,'title') . PHP_EOL;
+      if ($this->title) $head .= $this->tag('title',$this->title) . PHP_EOL;
       if ($this->charset) $head .= '<meta http-equiv="Content-Type" content="text/html; charset='.$this->charset.'" />'.PHP_EOL;
       if (preg_match('/tml$/',$this->content_type)) {
 
@@ -314,5 +314,9 @@ class Page {
     die("Db->is(): unknow argument '$is'. Accepted values are xhtml, html, xml");
   }
 
+  function path() {
+    return preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']);
+  }
+
 } return;
 ?>