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/CoreHome/angularjs/widget/widget.directive.js | |
parent | 8ccc9dc05da021325cdbf141a548637fa52f16b2 (diff) |
generate pages instead of implementing them in each controller
Diffstat (limited to 'plugins/CoreHome/angularjs/widget/widget.directive.js')
-rw-r--r-- | plugins/CoreHome/angularjs/widget/widget.directive.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/plugins/CoreHome/angularjs/widget/widget.directive.js b/plugins/CoreHome/angularjs/widget/widget.directive.js new file mode 100644 index 0000000000..86574870f2 --- /dev/null +++ b/plugins/CoreHome/angularjs/widget/widget.directive.js @@ -0,0 +1,93 @@ +/*! + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +/** + * Renders any kind of widget. If you have a widget and you want to have it rendered, use this directive. It will + * display a name on top and the actual widget below. It can handle any kind of widget, no matter whether it is a + * regular widget or a container. + * + * @param {Object} piwikWidget A widget object as returned by the WidgetMetadata API. + * @param {Object} piwikWidget.middlewareParameters If present, we will request a URL using the given parameters and + * only if this URL returns a JSON `true` the widget will be shown. + * Otherwise the widget won't be shown. + * @param {String} containerId If you do not have a widget object but a containerId we will find the correct widget + * object based on the given containerId. Be aware that we might not find the widget if + * it is for example not available for the current user or period/date. + * @param {Boolean} widgetized true if the widget is widgetized (eg in Dashboard or exported). In this case we will add + * a URL parameter widget=1 to all widgets. Eg sparklines will be then displayed one after + * another (vertically aligned) instead of two next to each other. + * + * Example: + * <div piwik-widget="widget"></div> + * <div piwik-widget containerid="widgetGoalsOverview"></div> // in this case we will find the correct widget automatically + * <div piwik-widget="widget" widetized="true"></div> // disables rating feature, no initial headline + */ +(function () { + angular.module('piwikApp').directive('piwikWidget', piwikWidget); + + piwikWidget.$inject = ['piwik', 'piwikApi']; + + function piwikWidget(piwik, piwikApi){ + + function findContainerWidget(containerId, scope) { + widgetsHelper.getAvailableWidgets(function (categorizedWidgets) { + + angular.forEach(categorizedWidgets, function (widgets) { + angular.forEach(widgets, function (widget) { + + if (widget && widget.isContainer && widget.parameters.containerId === containerId) { + widget = angular.copy(widget); + if (scope.widgetized) { + widget.isFirstInPage = '1'; + widget.parameters.widget = '1'; + angular.forEach(widget.widgets, function (widget) { + widget.parameters.widget = '1'; + }); + } + scope.widget = widget; + applyMiddleware(scope); + } + }); + }); + + }); + } + + function applyMiddleware(scope) + { + if (!scope.widget.middlewareParameters) { + scope.$eval('view.showWidget = true'); + } else { + var params = angular.copy(scope.widget.middlewareParameters); + piwikApi.fetch(params).then(function (response) { + var enabled = response ? 'true' : 'false'; + scope.$eval('view.showWidget = ' + enabled); + }); + } + } + + return { + restrict: 'A', + scope: { + widget: '=?piwikWidget', + widgetized: '=?', + containerid: '=' + }, + templateUrl: 'plugins/CoreHome/angularjs/widget/widget.directive.html?cb=' + piwik.cacheBuster, + compile: function (element, attrs) { + + return function (scope, element, attrs, ngModel) { + if (scope.widget) { + applyMiddleware(scope); + } else if (attrs.containerid) { + findContainerWidget(attrs.containerid, scope); + } + } + } + }; + } +})();
\ No newline at end of file |