Dotclear

Changeset 2164


Ignore:
Timestamp:
04/06/10 17:08:54 (13 years ago)
Author:
Tomtom33
Message:

commentNotifications 0.3 : Remove all PHP management for notifications => Completely JS/AJAX

Location:
plugins/commentNotifications
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • plugins/commentNotifications/_admin.js

    r2151 r2164  
    1111# -- END LICENSE BLOCK ------------------------------------ 
    1212*/ 
    13 $(document).ready(function() { 
    14      var css = { 
    15           background: 'transparent url(index.php?pf=commentNotifications/img/bubble.png) no-repeat top 20px', 
    16           height: '24px', 
    17           width: '24px', 
    18           overflow: 'hidden', 
    19           position: 'absolute', 
    20           'text-align': 'center', 
    21           'font-weight': 'bold', 
    22           display: 'none', 
    23           top: $('li a[href="comments.php"]').position().top - 20, 
    24           left: $('li a[href="comments.php"]').position().left + $('li a[href="comments.php"]').width() - 25 
    25      }; 
    26      $('li a[href="comments.php"]').parent().append('<li class="new" rel="comments"></li>'); 
    27      $('li[rel="comments"]').css(css); 
     13var notificator = { 
     14     css: { 
     15          'menu': { 
     16               'background': 'transparent url(index.php?pf=commentNotifications/img/bubble.png) no-repeat top 20px', 
     17               'height': '24px', 
     18               'width': '24px', 
     19               'overflow': 'hidden', 
     20               'position': 'absolute', 
     21               'text-align': 'center', 
     22               'font-weight': 'bold', 
     23               'display': 'none' 
     24          } 
     25     }, 
     26     cookie: 'dc_nb_comments', 
     27     msg: {}, 
    2828      
    29      var name = 'dc_nb_comments='; 
    30      var ca = document.cookie.split(';'); 
    31      var nb = '9999999999'; 
    32      for(var i=0;i < ca.length;i++) { 
    33           var c = ca[i]; 
    34           while (c.charAt(0)==' ') c = c.substring(1,c.length); 
    35           if (c.indexOf(name) == 0) nb = c.substring(name.length,c.length); 
    36      } 
    37      if (nb < nb_comments) { 
    38           $('li[rel="comments"]').html(nb_comments-nb); 
     29     init: function () { 
     30          /* Set cookie */ 
     31          if ($.cookie(notificator.cookie) == null || notificator.reload_nb_comments == 'true') { 
     32               $.cookie(notificator.cookie,notificator.nb_comments); 
     33          } 
     34          /* Set menu */ 
     35          var p = { 
     36               'top': $('li a[href="comments.php"]').position().top - 20, 
     37               'left': $('li a[href="comments.php"]').position().left + $('li a[href="comments.php"]').width() - 25 
     38          }; 
     39          notificator.css.menu = $.extend(notificator.css.menu,p); 
     40          $('li a[href="comments.php"]').parent().append('<li class="new" rel="comments"></li>'); 
     41          $('li[rel="comments"]').css(notificator.css.menu); 
     42           
     43          notificator.getNbComments(); 
     44     }, 
     45      
     46     getNbComments: function () { 
     47          $.get('services.php',{f:'getNbComments'},function(data) { 
     48               var rsp = $(data).children('rsp')[0]; 
     49                
     50               if (rsp.attributes[0].value == 'ok') { 
     51                    var delta = $(rsp).find('comments').text() - $.cookie(notificator.cookie); 
     52                     
     53                    if (delta > 0) { 
     54                         notificator.applyChanges(delta); 
     55                    } 
     56               }; 
     57               setTimeout('notificator.getNbComments()',10000); 
     58          }); 
     59     }, 
     60      
     61     applyChanges: function (num) { 
     62          /* Changes for the dasboard */ 
     63          var legend = $('span a[href="comments.php"]'); 
     64          var img = $('a[href="comments.php"] img'); 
     65          var html = ""; 
     66          html += (notificator.nb_comments + num) + " "; 
     67          html += notificator.nb_comments > 1 ? notificator.msg.comments : notificator.msg.comment; 
     68          html += " (" + num + " "; 
     69          html += (num > 1 ? notificator.msg.recents : notificator.msg.recent) + ")"; 
     70          legend.html(html); 
     71          img.attr("src","index.php?pf=commentNotifications/img/comments-b.png"); 
     72           
     73          /* Changes for the menu */ 
     74          $('li[rel="comments"]').html(num); 
    3975          $('li[rel="comments"]').fadeIn("slow"); 
    4076     } 
    41       
    42      getNbComments(); 
     77}; 
     78 
     79 
     80$(document).ready(function() { 
     81     notificator.init(); 
    4382}); 
    44  
    45 function getNbComments() 
    46 { 
    47      $.get('services.php',{f:'getNbComments'},function(data) { 
    48           var rsp = $(data).children('rsp')[0]; 
    49            
    50           if (rsp.attributes[0].value == 'ok') { 
    51                var nb = $(rsp).find('comments').text(); 
    52                if (nb > nb_comments) { 
    53                     $('li[rel="comments"]').html(nb-nb_comments); 
    54                     $('li[rel="comments"]').fadeIn("slow"); 
    55                } 
    56           }; 
    57           setTimeout('getNbComments()',10000); 
    58      }); 
    59 } 
  • plugins/commentNotifications/_define.php

    r2151 r2164  
    1515$this->registerModule( 
    1616     'commentNotifications', 
    17      'Displays small bubble notification for new comments on a right of comments link in administration interface', 
     17     'Displays notifications on menu and dashboard when new comments arrive', 
    1818     'Tomtom (http://blog.zenstyle.fr/)', 
    19      '0.2', 
     19     '0.3', 
    2020     'usage' 
    2121); 
  • plugins/commentNotifications/_prepend.php

    r2151 r2164  
    1313if (!defined('DC_RC_PATH')) { return; } 
    1414 
    15 # Init cookie 
    16 if (defined('DC_CONTEXT_ADMIN')) { 
    17      $params = array(); 
    18      $cookie_name = 'dc_nb_comments'; 
    19      $nb_comments = $core->blog->getComments($params,true)->f(0); 
    20            
    21      if (!isset($_COOKIE[$cookie_name])) { 
    22           setcookie($cookie_name,$core->blog->getComments($params,true)->f(0)); 
    23      } 
    24 } 
    25  
    2615$__autoload['commentNotificationsRestMethods'] = dirname(__FILE__).'/_services.php'; 
    2716 
    2817$core->addBehavior('adminPageHTMLHead',array('commentNotificationsBehaviors','adminPageHTMLHead')); 
    29 $core->addBehavior('adminCommentsHeaders',array('commentNotificationsBehaviors','adminCommentsHeaders')); 
    3018 
    3119$core->rest->addFunction('getNbComments',array('commentNotificationsRestMethods','getNbComments')); 
     
    3826           
    3927          $params = array(); 
    40           $cookie_name = 'dc_nb_comments'; 
    4128          $nb_comments = $core->blog->getComments($params,true)->f(0); 
    42            
    43           if (!isset($_COOKIE[$cookie_name])) { 
    44                setcookie($cookie_name,$core->blog->getComments($params,true)->f(0)); 
    45           } 
     29          $reload_nb_comments = preg_match('/comments.php(.*)?/',$_SERVER['REQUEST_URI']); 
    4630           
    4731          echo 
    48           '<script type="text/javascript">'. 
    49           'var nb_comments = '.$nb_comments.';'. 
    50           '</script>'. 
    51           '<script type="text/javascript" src="index.php?pf='. 
    52           basename(dirname(__FILE__)).'/_admin.js"></script>'; 
    53      } 
    54       
    55      public static function adminCommentsHeaders() 
    56      { 
    57           global $core; 
    58            
    59           $params = array(); 
    60           $cookie_name = 'dc_nb_comments'; 
    61            
    62           if (isset($_COOKIE[$cookie_name])) { 
    63                setcookie($cookie_name,$core->blog->getComments($params,true)->f(0)); 
    64           } 
     32          dcPage::jsLoad('index.php?pf='.basename(dirname(__FILE__)).'/_admin.js'). 
     33          '<script type="text/javascript">'."\n". 
     34          "//<![CDATA[\n". 
     35          dcPage::jsVar('notificator.nb_comments',$nb_comments). 
     36          dcPage::jsVar('notificator.reload_nb_comments',($reload_nb_comments ? 'true' : 'false')). 
     37          dcPage::jsVar('notificator.msg.comment',__('comment')). 
     38          dcPage::jsVar('notificator.msg.comments',__('comments')). 
     39          dcPage::jsVar('notificator.msg.recent',__('new')). 
     40          dcPage::jsVar('notificator.msg.recents',__('news')). 
     41          "\n//]]>\n". 
     42          "</script>\n"; 
    6543     } 
    6644} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map