new per-port mac list popup menu item
authorxornet <founder.sl@gmail.com>
Fri, 23 May 2014 16:41:31 +0000 (20:41 +0400)
committerAlexey Andriyanov <alan@al-an.info>
Tue, 27 May 2014 11:13:52 +0000 (15:13 +0400)
Add AJAX handler 'get-port-portmac' for requested port

changed:
formatPortMacHints: now can query device with getportmaclist operation for specified port
getPortInfoAJAX: new key 'get-port-portmac' in $funcmap and $opmap
switchportInfoJS: new key 'portmac' in $available_ops
navigation.php: new ajaxhandler 'get-port-portmac'
queryDevice: new function argument $args. Calls translateDeviceCommands with given args
portinfo.js:
  new menuitem 'Show port learned MACs' in menu_item_candidates
  menuItemClicked: new request data param 'port_name'
  applyMacData: Change popup-box dynamic block if it exists instead of just adding it

Conflicts:
ChangeLog

ChangeLog
wwwroot/inc/ajax-interface.php
wwwroot/inc/interface.php
wwwroot/inc/navigation.php
wwwroot/inc/remote.php
wwwroot/js/portinfo.js

index 84c7503..d9e4fc8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
        update: new {$client_x.x.x.x} auto tag containing client's IP
        update: explicitly require PHP version 5.3.0 or newer
        update: link status feature is available on Juniper
+       update: new port menu item: "Show port learned MACs"
 0.20.8
        bugfix: syntax error in functions.php when using PHP prior to 5.3.0 (#1195)
        bugfix: properly format comment text for locations and racks
index 17449ed..143f781 100644 (file)
@@ -58,7 +58,16 @@ function formatPortLinkHints ($object_id)
 function formatPortMacHints ($object_id)
 {
        $result = array();
-       $macList = queryDevice ($object_id, 'getmaclist');
+       if ($_REQUEST['ac'] == 'get-port-portmac')
+       {
+               $port_name = $_REQUEST['port_name'];
+               $ports = reduceSubarraysToColumn (getObjectPortsAndLinks($_REQUEST['object_id']), 'name');
+               $macList = in_array($port_name, $ports) ?
+                               queryDevice ($object_id, 'getportmaclist', array ($port_name)) :
+                               array();
+       }
+       else
+               $macList = queryDevice ($object_id, 'getmaclist');
        foreach ($macList as $portname => $list)
        {
                $list = $macList[$portname];
@@ -188,12 +197,14 @@ function getPortInfoAJAX()
        (
                'get-port-link' => 'formatPortLinkHints',
                'get-port-mac'  => 'formatPortMacHints',
+               'get-port-portmac' => 'formatPortMacHints',
                'get-port-conf' => 'formatPortConfigHints',
        );
        $opmap = array
        (
                'get-port-link' => 'get_link_status',
                'get-port-mac'  => 'get_mac_list',
+               'get-port-portmac' => 'get_port_mac_list',
                'get-port-conf' => 'get_port_conf',
        );
        genericAssertion ('object_id', 'uint');
index ce507ce..1dbb2b6 100644 (file)
@@ -8749,6 +8749,7 @@ function switchportInfoJS($object_id)
                'link' => array ('op' => 'get_link_status', 'gw' => 'getportstatus'),
                'conf' => array ('op' => 'get_port_conf', 'gw' => 'get8021q'),
                'mac' =>  array ('op' => 'get_mac_list', 'gw' => 'getmaclist'),
+               'portmac' => array ('op' => 'get_port_mac_list', 'gw' => 'getportmaclist'),
        );
        $breed = detectDeviceBreed ($object_id);
        $allowed_ops = array();
index d6992e7..8a4e9b6 100644 (file)
@@ -812,6 +812,7 @@ $ajaxhandler['get-location-select'] = 'getLocationSelectAJAX';
 $ajaxhandler['verifyCode'] = 'verifyCodeAJAX';
 $ajaxhandler['get-port-link'] = 'getPortInfoAJAX';
 $ajaxhandler['get-port-mac'] = 'getPortInfoAJAX';
+$ajaxhandler['get-port-portmac'] = 'getPortInfoAJAX';
 $ajaxhandler['get-port-conf'] = 'getPortInfoAJAX';
 $ajaxhandler['upd-ip-name'] = 'updateIPNameAJAX';
 $ajaxhandler['upd-ip-comment'] = 'updateIPCommentAJAX';
index eef7d04..7642826 100644 (file)
@@ -217,9 +217,19 @@ function assertBreedFunction ($breed, $command)
        return $breedfunc["${breed}-${command}-main"];
 }
 
-function queryDevice ($object_id, $command)
+function queryDevice ($object_id, $command, $args = array())
 {
-       $query = translateDeviceCommands ($object_id, array (array ('opcode' => $command)));
+       $request = array ('opcode' => $command);
+       if (is_array ($args) && count ($args))
+       {
+               $i = 1;
+               foreach ($args as $arg)
+               {
+                       $request["arg$i"] = $arg;
+                       $i++;
+               }
+       }
+       $query = translateDeviceCommands ($object_id, array ($request));
        if ($command == 'xlatepushq')
                return $query;
        $breed = assertDeviceBreed ($object_id);
index b2244c1..ac4c594 100644 (file)
@@ -9,7 +9,8 @@ bk_event = null;
        var menu_item_candidates = {
                link: {'Show links status': {onclick: menuItemClicked, className: 'itemname-link'}},
                conf: {'Show ports configuration': {onclick: menuItemClicked, className: 'itemname-conf'}},
-               mac: {'Show learned MACs': {onclick: menuItemClicked, className: 'itemname-mac'}}
+               mac: {'Show device learned MACs': {onclick: menuItemClicked, className: 'itemname-mac'}},
+               portmac: {'Show port learned MACs': {onclick: menuItemClicked, className: 'itemname-portmac'}}
        };
        var portinfo_enabled = false;
        for (var i in enabled_elements) {
@@ -63,6 +64,8 @@ function setItemIcon(menuItem, iconName) {
                iconURL = 'index.php?module=chrome&uri=pix/ajax-loader.gif';
        else if (iconName == 'ok')
                iconURL = 'index.php?module=chrome&uri=pix/checkbox_yes.png';
+       else
+               iconURL = '';
        $(menuItem).children('.' + $.contextMenu.innerDivClassName).css("background-image", "url(" + iconURL + ")");
 }
 
@@ -76,6 +79,8 @@ function menuItemClicked(menuItem, menu) {
        if (! matches)
                return;
        var type = matches[1];
+       var per_port_cmd = type == 'portmac';
+       var portnameElem = $(this).siblings('.interactive-portname')[0];
 
        var bSuccessIcon = false;
        $.ajax({
@@ -103,23 +108,30 @@ function menuItemClicked(menuItem, menu) {
                data: {
                        'module': 'ajax',
                        'ac': 'get-port-' + type,
-                       'object_id': getQueryString('object_id')
+                       'object_id': getQueryString('object_id'),
+                       'port_name': getPortName(portnameElem)
                },
                dataType: 'json',
                success: function(data, textStatus, XMLHttpRequest) {
                        if (! data)
                                return;
                        bSuccessIcon = true;
-                       setItemIcon(menuItem, 'ok');
+
+                       if (per_port_cmd)
+                               setItemIcon(menuItem, '');
+                       else
+                               setItemIcon(menuItem, 'ok');
+
                        if (type == 'link')
                                applyLinkData(data);
-                       else if (type == 'mac')
+                       else if (type == 'mac' || type == 'portmac')
                                applyMacData(data);
                        else if (type == 'conf')
                                applyConfData(data);
                }
        });
-       disableMenuItem(menuItem);
+       if (!per_port_cmd)
+               disableMenuItem(menuItem);
 }
 
 function rememberCursorPosition(event) {
@@ -156,11 +168,20 @@ function applyMacData(data) {
                if (item != null) {
                        var prepended;
                        if (item['inline'])
-                               appended = $('<div />').addClass('mac-count').html(item['inline']).appendTo($(this).parent());
+                       {
+                               var obj = $(this).parent().find('.mac-count');
+                               if (!obj.length)
+                                       appended = $('<div />').addClass('mac-count').html(item['inline']).appendTo($(this).parent());
+                               else
+                                       appended = obj.html(item['inline']);
+                       }
                        if (item['popup'] && appended) {
-                               if (! seen_portnames[portname])
-                                       seen_portnames[portname] = $('<div />').addClass('popup-box mac-list').html(item['popup']).appendTo('body');
-                               appended.thumbPopup(seen_portnames[portname]);
+                               var obj = $("[id='maclist-popup-"+portname+"']");
+                               if (! obj.size())
+                                       obj = $('<div />').addClass('popup-box mac-list').attr('id', 'maclist-popup-'+portname).html(item['popup']).appendTo('body');
+                               else
+                                       obj.html(item['popup']);
+                               appended.thumbPopup(obj);
                        }
                }
        });