diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-07-03 03:54:27 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-10-06 18:25:13 +0300 |
commit | 9ba8f216fd7856ce5fef06bf82ecb8f8a2e7e630 (patch) | |
tree | 6ce07d18a85d00b39ab720abe042361c0775aead /plugins/Dashboard/angularjs | |
parent | 8ccc9dc05da021325cdbf141a548637fa52f16b2 (diff) |
generate pages instead of implementing them in each controller
Diffstat (limited to 'plugins/Dashboard/angularjs')
-rw-r--r-- | plugins/Dashboard/angularjs/common/services/dashboards-model.js | 68 | ||||
-rw-r--r-- | plugins/Dashboard/angularjs/dashboard/dashboard.directive.js | 103 |
2 files changed, 171 insertions, 0 deletions
diff --git a/plugins/Dashboard/angularjs/common/services/dashboards-model.js b/plugins/Dashboard/angularjs/common/services/dashboards-model.js new file mode 100644 index 0000000000..e0d861c2ba --- /dev/null +++ b/plugins/Dashboard/angularjs/common/services/dashboards-model.js @@ -0,0 +1,68 @@ +/*! + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +(function () { + angular.module('piwikApp.service').factory('dashboardsModel', dashboardsModel); + + dashboardsModel.$inject = ['piwikApi']; + + function dashboardsModel (piwikApi) { + + var dashboardsPromise = null; + + var model = { + dashboards: [], + getAllDashboards: getAllDashboards, + reloadAllDashboards: reloadAllDashboards, + getDashboard: getDashboard, + getDashboardLayout: getDashboardLayout + }; + + return model; + + function getDashboard(dashboardId) + { + return getAllDashboards().then(function (dashboards) { + var dashboard = null; + angular.forEach(dashboards, function (board) { + if (parseInt(board.id, 10) === parseInt(dashboardId, 10)) { + dashboard = board; + } + }); + return dashboard; + }); + } + + function getDashboardLayout(dashboardId) + { + return piwikApi.fetch({module: 'Dashboard', action: 'getDashboardLayout', idDashboard: dashboardId}); + } + + function reloadAllDashboards() + { + if (dashboardsPromise) { + dashboardsPromise = null; + } + + return getAllDashboards(); + } + + function getAllDashboards() + { + if (!dashboardsPromise) { + dashboardsPromise = piwikApi.fetch({method: 'Dashboard.getDashboards'}).then(function (response) { + if (response) { + model.dashboards = response; + } + + return response; + }); + } + + return dashboardsPromise; + } + } +})();
\ No newline at end of file diff --git a/plugins/Dashboard/angularjs/dashboard/dashboard.directive.js b/plugins/Dashboard/angularjs/dashboard/dashboard.directive.js new file mode 100644 index 0000000000..578737940c --- /dev/null +++ b/plugins/Dashboard/angularjs/dashboard/dashboard.directive.js @@ -0,0 +1,103 @@ +/*! + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +/** + * <div piwik-dashboard dashboardId="5" layout="10"></div> + */ +(function () { + angular.module('piwikApp').directive('piwikDashboard', piwikDashboard); + + piwikDashboard.$inject = ['dashboardsModel', '$rootScope', '$q']; + + function piwikDashboard(dashboardsModel, $rootScope, $q) { + + function renderDashboard(dashboardId, dashboard, layout) + { + $('.dashboardSettings').show(); + initTopControls(); + + // Embed dashboard + if (!$('#topBars').length) { + $('.dashboardSettings').after($('#Dashboard')); + $('#Dashboard_embeddedIndex_' + dashboardId).addClass('sfHover'); + } + + widgetsHelper.getAvailableWidgets(); + + $('#dashboardWidgetsArea') + .on('dashboardempty', showEmptyDashboardNotification) + .dashboard({ + idDashboard: dashboardId, + layout: layout, + name: dashboard ? dashboard.name : '' + }); + + $('#columnPreview').find('>div').each(function () { + var width = []; + $('div', this).each(function () { + width.push(this.className.replace(/width-/, '')); + }); + $(this).attr('layout', width.join('-')); + }); + + $('#columnPreview').find('>div').on('click', function () { + $('#columnPreview').find('>div').removeClass('choosen'); + $(this).addClass('choosen'); + }); + } + + function fetchDashboard(dashboardId) { + $('#dashboardWidgetsArea').innerHTML =''; + + var getDashboard = dashboardsModel.getDashboard(dashboardId); + var getLayout = dashboardsModel.getDashboardLayout(dashboardId); + + $q.all([getDashboard, getLayout]).then(function (response) { + var dashboard = response[0]; + var layout = response[1]; + + $(function() { + renderDashboard(dashboardId, dashboard, layout); + }); + }); + } + + function clearDashboard () { + $('.top_controls .dashboard-manager').hide(); + $('#dashboardWidgetsArea').dashboard('destroy'); + } + + return { + restrict: 'A', + scope: { + dashboardid: '=', + layout: '=' + }, + link: function (scope, element, attrs) { + + scope.$parent.fetchDashboard = function (dashboardId) { + scope.dashboardId = dashboardId; + fetchDashboard(dashboardId) + }; + + fetchDashboard(scope.dashboardid); + + function onLocationChange(event, url1, url2) + { + if (url1 !== url2) { + clearDashboard(); + } + } + + // should be rather handled in route or so. + var unbind = $rootScope.$on('$locationChangeSuccess', onLocationChange); + scope.$on('$destroy', onLocationChange); + scope.$on('$destroy', unbind); + } + }; + } +})();
\ No newline at end of file |