diff --git a/plugins/Dashboard/javascripts/dashboardObject.js b/plugins/Dashboard/javascripts/dashboardObject.js index 038dd1a8a1618587a496263713fde43f78d34cfa..e6990aa72a1b71a942929fbe66c0bfe65e07eb79 100644 --- a/plugins/Dashboard/javascripts/dashboardObject.js +++ b/plugins/Dashboard/javascripts/dashboardObject.js @@ -57,7 +57,7 @@ if (options.layout) { generateLayout(options.layout); - buildMenu(); + handleMenuClick(); } else { methods.loadDashboard.apply(this, [dashboardId]); } @@ -85,7 +85,6 @@ * @param {int} dashboardIdToLoad */ loadDashboard: function (dashboardIdToLoad) { - $(dashboardElement).empty(); dashboardName = ''; dashboardLayout = null; @@ -94,7 +93,7 @@ broadcast.updateHashOnly = true; broadcast.propagateAjax('?idDashboard=' + dashboardIdToLoad); fetchLayout(generateLayout); - buildMenu(); + handleMenuClick(); return this; }, @@ -427,47 +426,43 @@ } /** - * Builds the menu for choosing between available dashboards + * Handle clicks for menu items for choosing between available dashboards */ - function buildMenu() { - - var success = function (dashboards) { - var dashboardMenuList = $('#Dashboard').find('> ul'); - dashboardMenuList.empty(); - if (dashboards.length > 1) { - dashboardMenuList.show(); - for (var i = 0; i < dashboards.length; i++) { - dashboardMenuList.append('<li id="Dashboard_embeddedIndex_' + dashboards[i].iddashboard + '" class="dashboardMenuItem"><a dashboardId="' + dashboards[i].iddashboard + '">' + piwikHelper.htmlEntities(dashboards[i].name) + '</a></li>'); - if (dashboards[i].iddashboard == dashboardId) { - dashboardName = dashboards[i].name; - } - } - $('#Dashboard_embeddedIndex_' + dashboardId).addClass('sfHover'); - } else { - dashboardMenuList.hide(); + function handleMenuClick() { + var $dashboardLinks = $('#Dashboard ul a').filter(function () { + return $(this).closest('li').attr('id').indexOf('Dashboard_embeddedIndex') === 0; + }); + + // setup each link (remove default onclick behavior, get idDashboard value and style currently selected + // link) + $dashboardLinks.each(function () { + $(this).removeAttr('onclick'); + + var linkIdDashboard = broadcast.getParamValue('idDashboard', $(this).attr('href')); + $(this).attr('data-idDashboard', linkIdDashboard); + + if (dashboardId == linkIdDashboard) { + $(this).closest('li').addClass('sfHover'); } + }); - $('.dashboardMenuItem').on('click', function () { - if (typeof piwikMenu != 'undefined') { - piwikMenu.activateMenu('Dashboard', 'embeddedIndex'); - } - $('.dashboardMenuItem').removeClass('sfHover'); - if ($(dashboardElement).length) { - $(dashboardElement).dashboard('loadDashboard', $('a', this).attr('dashboardId')); - } else { - broadcast.propagateAjax('module=Dashboard&action=embeddedIndex&idDashboard=' + $('a', this).attr('dashboardId')); - } - $(this).addClass('sfHover'); - }); - }; + $dashboardLinks.click(function (e) { + e.preventDefault(); + e.stopPropagation(); - var ajaxRequest = new ajaxHelper(); - ajaxRequest.addParams({ - module: 'Dashboard', - action: 'getAllDashboards' - }, 'get'); - ajaxRequest.setCallback(success); - ajaxRequest.send(false); + var idDashboard = $(this).attr('data-idDashboard'); + + if (typeof piwikMenu != 'undefined') { + piwikMenu.activateMenu('Dashboard', 'embeddedIndex'); + } + $('#Dashboard ul li').removeClass('sfHover'); + if ($(dashboardElement).length) { + $(dashboardElement).dashboard('loadDashboard', idDashboard); + } else { + broadcast.propagateAjax('module=Dashboard&action=embeddedIndex&idDashboard=' + idDashboard); + } + $(this).closest('li').addClass('sfHover'); + }); } /** @@ -515,7 +510,7 @@ function () { if (dashboardChanged) { dashboardChanged = false; - buildMenu(); + handleMenuClick(); } } ); diff --git a/plugins/SegmentEditor/Controller.php b/plugins/SegmentEditor/Controller.php index a3fd20fe8b1263bf46c7a4117aac48656bfd89d3..d8a8e8edcd69573d8362630ff75afc08a62855f5 100644 --- a/plugins/SegmentEditor/Controller.php +++ b/plugins/SegmentEditor/Controller.php @@ -10,86 +10,14 @@ */ namespace Piwik\Plugins\SegmentEditor; -use Piwik\Common; -use Piwik\Piwik; -use Piwik\Plugins\API\API as APIMetadata; -use Piwik\View; - /** * @package SegmentEditor */ class Controller extends \Piwik\Plugin\Controller { - public function getSelector() { - $view = new View('@SegmentEditor/getSelector'); - $idSite = Common::getRequestVar('idSite'); - $this->setGeneralVariablesView($view); - $segments = APIMetadata::getInstance()->getSegmentsMetadata($idSite); - - $segmentsByCategory = $customVariablesSegments = array(); - foreach ($segments as $segment) { - if ($segment['category'] == Piwik::translate('General_Visit') - && ($segment['type'] == 'metric' && $segment['segment'] != 'visitIp') - ) { - $metricsLabel = Piwik::translate('General_Metrics'); - $metricsLabel[0] = strtolower($metricsLabel[0]); - $segment['category'] .= ' (' . $metricsLabel . ')'; - } - $segmentsByCategory[$segment['category']][] = $segment; - } - uksort($segmentsByCategory, array($this, 'sortSegmentCategories')); - - $view->segmentsByCategory = $segmentsByCategory; - - $savedSegments = API::getInstance()->getAll($idSite); - foreach ($savedSegments as &$savedSegment) { - $savedSegment['name'] = Common::sanitizeInputValue($savedSegment['name']); - } - $view->savedSegmentsJson = Common::json_encode($savedSegments); - $view->authorizedToCreateSegments = !Piwik::isUserIsAnonymous(); - - $view->segmentTranslations = Common::json_encode($this->getTranslations()); - $out = $view->render(); - return $out; - } - - public function sortSegmentCategories($a, $b) - { - // Custom Variables last - if ($a == Piwik::translate('CustomVariables_CustomVariables')) { - return 1; - } - return 0; - } - - private function getTranslations() - { - $translationKeys = array( - 'General_OperationEquals', - 'General_OperationNotEquals', - 'General_OperationAtMost', - 'General_OperationAtLeast', - 'General_OperationLessThan', - 'General_OperationGreaterThan', - 'General_OperationContains', - 'General_OperationDoesNotContain', - 'General_OperationIs', - 'General_OperationIsNot', - 'General_OperationContains', - 'General_OperationDoesNotContain', - 'SegmentEditor_DefaultAllVisits', - 'General_DefaultAppended', - 'SegmentEditor_AddNewSegment', - 'General_Edit', - 'General_Search', - 'General_SearchNoResults', - ); - $translations = array(); - foreach ($translationKeys as $key) { - $translations[$key] = Piwik::translate($key); - } - return $translations; + $selector = new SegmentSelectorControl(); + return $selector->render(); } } diff --git a/plugins/SegmentEditor/SegmentSelectorControl.php b/plugins/SegmentEditor/SegmentSelectorControl.php new file mode 100644 index 0000000000000000000000000000000000000000..647acddf5f09cc8b3aead959578699b400636048 --- /dev/null +++ b/plugins/SegmentEditor/SegmentSelectorControl.php @@ -0,0 +1,100 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik_Plugins + * @package SegmentEditor + */ +namespace Piwik\Plugins\SegmentEditor; + +use Piwik\Common; +use Piwik\Piwik; +use Piwik\Plugins\API\API as APIMetadata; +use Piwik\View; +use Piwik\Access; + +/** + * Generates the HTML for the segment selector control (which includes the segment editor). + */ +class SegmentSelectorControl extends View +{ + const TEMPLATE = "@SegmentEditor/_segmentSelector"; + + /** + * Constructor. + */ + public function __construct() + { + parent::__construct(self::TEMPLATE); + + $this->isSuperUser = Access::getInstance()->hasSuperUserAccess(); + $this->idSite = Common::getRequestVar('idSite', false, 'int'); + + $segments = APIMetadata::getInstance()->getSegmentsMetadata($this->idSite); + + $segmentsByCategory = $customVariablesSegments = array(); + foreach ($segments as $segment) { + if ($segment['category'] == Piwik::translate('General_Visit') + && ($segment['type'] == 'metric' && $segment['segment'] != 'visitIp') + ) { + $metricsLabel = Piwik::translate('General_Metrics'); + $metricsLabel[0] = strtolower($metricsLabel[0]); + $segment['category'] .= ' (' . $metricsLabel . ')'; + } + $segmentsByCategory[$segment['category']][] = $segment; + } + uksort($segmentsByCategory, array($this, 'sortSegmentCategories')); + + $this->segmentsByCategory = $segmentsByCategory; + + $savedSegments = API::getInstance()->getAll($this->idSite); + foreach ($savedSegments as &$savedSegment) { + $savedSegment['name'] = Common::sanitizeInputValue($savedSegment['name']); + } + $this->savedSegmentsJson = Common::json_encode($savedSegments); + $this->authorizedToCreateSegments = !Piwik::isUserIsAnonymous(); + + $this->segmentTranslations = Common::json_encode($this->getTranslations()); + } + + public function sortSegmentCategories($a, $b) + { + // Custom Variables last + if ($a == Piwik::translate('CustomVariables_CustomVariables')) { + return 1; + } + return 0; + } + + private function getTranslations() + { + $translationKeys = array( + 'General_OperationEquals', + 'General_OperationNotEquals', + 'General_OperationAtMost', + 'General_OperationAtLeast', + 'General_OperationLessThan', + 'General_OperationGreaterThan', + 'General_OperationContains', + 'General_OperationDoesNotContain', + 'General_OperationIs', + 'General_OperationIsNot', + 'General_OperationContains', + 'General_OperationDoesNotContain', + 'SegmentEditor_DefaultAllVisits', + 'General_DefaultAppended', + 'SegmentEditor_AddNewSegment', + 'General_Edit', + 'General_Search', + 'General_SearchNoResults', + ); + $translations = array(); + foreach ($translationKeys as $key) { + $translations[$key] = Piwik::translate($key); + } + return $translations; + } +} \ No newline at end of file diff --git a/plugins/SegmentEditor/templates/getSelector.twig b/plugins/SegmentEditor/templates/_segmentSelector.twig similarity index 100% rename from plugins/SegmentEditor/templates/getSelector.twig rename to plugins/SegmentEditor/templates/_segmentSelector.twig