Port datapopup plugin to new Ajax-Plugin, jQuery Interface and SQLite
authorAndreas Boehler <andreas@aboehler.at>
Fri, 12 Sep 2014 07:02:44 +0200
changeset 1 347546fd9315
parent 0 11a7dcc4af7a
child 2 7e2f87288501
Port datapopup plugin to new Ajax-Plugin, jQuery Interface and SQLite
interface
action.php
ajax.php
plugin.info.txt
script.js
--- a/action.php	Fri Sep 12 07:01:55 2014 +0200
+++ b/action.php	Fri Sep 12 07:02:44 2014 +0200
@@ -24,11 +24,54 @@
     function register(&$controller) {
         global $JSINFO;
         $JSINFO['plugin']['datapopup']['registered_types'] = $this->getConf('registered_types');
+        
+        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
     }
 
-    function getFields($link, $name) {
-        return $this->dthlp->_resolveSummary($link, $name);
+    protected function _getFields($link, $name) {
+        if(!$this->dthlp)
+          return "The datapopup plugin reported an error. Please report.";
+        
+        $sqlite = $this->dthlp->_getDB();
+        if(!$sqlite) return "The datapopup had an error retrieving the SQLite plugin. Please report.";
+        $query = "SELECT data.value FROM data JOIN pages ON data.pid=pages.pid WHERE pages.page='".$link."' AND data.key='".strtolower($name)."';";
+        $res = $sqlite->query($query);
+        $rows = $sqlite->res2arr($res);
+        return $rows[0]['value'];
     }
+    
+    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
+        if($event->data != 'plugin_datapopup') return;    
+        global $auth;
+        global $INPUT;
+
+        $link = $INPUT->str('link');
+        
+        $fields = explode(',',$this->getConf('fields'));
+        $fields = array_map('trim',$fields);
+        $fields = array_filter($fields);
+
+        echo '<ul>';
+        foreach($fields as $name){
+            $val = $this->_getFields($link, $name);
+            if($val != "")
+            {
+                echo '<li class="dataov_name">';
+                echo '<div class="li">';
+                echo hsc($name);
+                echo '</div></li>';
+                echo '<li class="dataov_'.hsc($name).'">';
+                echo '<div class="li">';
+                echo hsc($val);
+                echo '</div></li>';
+            }
+        }
+        echo '</ul>';                
+    
+        
+        $event->preventDefault();
+        $event->stopPropagation();
+    }        
 
 }
 
--- a/ajax.php	Fri Sep 12 07:01:55 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-<?php
-global $auth;
-
-require_once 'action.php';
-$plugin = new action_plugin_datapopup;
-$fields = explode(',',$plugin->getConf('fields'));
-$fields = array_map('trim',$fields);
-$fields = array_filter($fields);
-
-echo '<ul>';
-foreach($fields as $name){
-    $val = $plugin->getFields($_POST['link'], $name);
-    if($val != "")
-    {
-        echo '<li class="dataov_name">';
-        echo '<div class="li">';
-        echo hsc($name);
-        echo '</div></li>';
-        echo '<li class="dataov_'.hsc($name).'">';
-        echo '<div class="li">';
-        echo hsc($val);
-        echo '</div></li>';
-    }
-}
-echo '</ul>';
-
--- a/plugin.info.txt	Fri Sep 12 07:01:55 2014 +0200
+++ b/plugin.info.txt	Fri Sep 12 07:02:44 2014 +0200
@@ -1,7 +1,7 @@
 base   datapopup
 author Andreas Boehler
 email  dev@aboehler.at
-date   2013-11-27
+date   2014-05-12
 name   datapopup plugin
 desc   Dynamically displays data entries in a popup
 url    http://www.aboehler.at
--- a/script.js	Fri Sep 12 07:01:55 2014 +0200
+++ b/script.js	Fri Sep 12 07:02:44 2014 +0200
@@ -1,8 +1,4 @@
-addInitEvent(function() {
-    var pages = getElementsByClass('dokuwiki', document, 'div');
-    if (pages.length === 0) {
-        return;
-    }
+jQuery(function () {
     regex = new Array();
     var typeArr = JSINFO.plugin.datapopup.registered_types.split(",");
     for(var i=0; i < typeArr.length; ++i) {
@@ -10,39 +6,63 @@
     }
 
     var id = 0;
-
-    function show_overlay() {
-        var overlays = getElementsByClass('datapopup_overlay', document, 'div');
-        for (var i = 0; i < overlays.length ; ++i) {
-            overlays[i].style.display = 'none';
+    
+    function show_overlay($link) {
+        if(!$link.datapopup_popup){
+            $link.datapopup_popup = dw_page.insituPopup($link, $link.datapopup_id);
+            $link.datapopup_popup.addClass('datapopup_overlay');
+            $link.datapopup_popup.load(
+                DOKU_BASE + 'lib/exe/ajax.php',
+                {
+                    call: 'plugin_datapopup',
+                    name: $link.datapopup_name,
+                    link: $link.datapopup_link,
+                }
+            );
         }
-        if (this.datapopup__overlay) {
-            this.datapopup__overlay.style.display='';
-            return;
-        }
-        this.datapopup__overlay = insitu_popup(this, 'datapopup__overlay_' + (id++));
-        this.datapopup__overlay.className += ' datapopup_overlay';
-        var ajax = new doku_ajax('plugin_datapopup', {name: this.datapopup__name, link: this.datapopup__link});
-        ajax.elementObj = this.datapopup__overlay;
-        ajax.runAJAX();
-    }
+        $link.datapopup_popup.show();
+    }    
 
-    function event_handler (delay) {
-        return function (e) {
-            delay.start.call(delay, this, e);
-        };
-    }
+    /**
+     * Find all links to user pages
+     *
+     * Adds events and info to the links.
+     *
+     * @type {number}
+     */
+    var links = 0;
+    jQuery('div.dokuwiki a').each(function () {
+        var $link = jQuery(this);
+        var href = $link.attr('href');
+        if (!href) return;
+        
+        for(var i = 0 ; i < regex.length ; ++i) {   
+            var match = href.replace(/\//g, ':').match(regex[i]);
+            if (!match) continue;
 
-    var links = pages[0].getElementsByTagName('a');
-    for (var link = 0 ; link < links.length ; ++link) {
-        for(var i = 0 ; i < regex.length ; ++i) {
-            var match = links[link].href.replace(/\//g, ':').match(regex[i]);
-            if(!match) continue;
-            links[link].datapopup__name = match[1];
-            links[link].datapopup__link = match[0];
-            var timer = new Delay(show_overlay);
-            addEvent(links[link], 'mouseover', event_handler(timer, 300));
-            addEvent(links[link], 'mouseout', bind(function (timer) { timer.delTimer(); }, timer));
-        };
-    }
+            $link.datapopup_name = match[1];
+            $link.datapopup_link = match[0];
+            $link.datapopup_id   = 'datapopup_'+(links++);
+
+            $link.mouseover(function () {
+                $link.datapopup_timer = window.setTimeout(
+                    function () {
+                        console.log($link.datapopup_name);
+                        show_overlay($link);
+                        $link.datapopup_timer = null;
+                    },
+                    300
+                );
+            });
+
+            $link.mouseout(function () {
+                if ($link.datapopup_timer) window.clearTimeout($link.datapopup_timer);
+                $link.datapopup_timer = null;
+            });
+        }
+
+
+    });
 });
+
+