1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
/**
* Loads any custom widget or URL based on the given parameters.
*
* The currently active idSite, period, date and segment (if needed) is automatically appended to the parameters. If
* this widget is removed from the DOM and requests are in progress, these requests will be aborted. A loading message
* or an error message on failure is shown as well. It's kinda similar to ng-include but there it is not possible to
* listen to HTTP errors etc.
*
* Example:
* <div piwik-widget-loader="{module: '', action: '', ...}"></div>
*/
(function () {
angular.module('piwikApp').directive('piwikWidgetLoader', piwikWidgetLoader);
piwikWidgetLoader.$inject = ['piwik', 'piwikUrl', '$http', '$compile', '$q', '$location'];
function piwikWidgetLoader(piwik, piwikUrl, $http, $compile, $q, $location){
return {
restrict: 'A',
transclude: true,
scope: {
piwikWidgetLoader: '='
},
templateUrl: 'plugins/CoreHome/angularjs/widget-loader/widgetloader.directive.html?cb=' + piwik.cacheBuster,
compile: function (element, attrs) {
return function (scope, element, attrs, ngModel) {
var changeCounter = 0,
currentScope,
currentElement,
httpCanceler,
contentNode = element.find('.theWidgetContent');
var cleanupLastWidgetContent = function() {
if (currentElement) {
currentElement.remove();
currentElement = null;
}
if (currentScope) {
currentScope.$destroy();
currentScope = null;
}
};
var abortHttpRequestIfNeeded = function () {
if (httpCanceler) {
httpCanceler.resolve();
httpCanceler = null;
}
}
function getFullWidgetUrl(parameters) {
var url = $.param(parameters);
var $urlParams = $location.search();
$urlParams = angular.copy($urlParams);
delete $urlParams['category'];
delete $urlParams['subcategory'];
angular.forEach($urlParams, function (value, key) {
if (!(key in parameters)) {
url += '&' + key + '=' + piwikUrl.getSearchParam(key);
}
});
url += '&random=' + parseInt(Math.random() * 10000);
return '?' + url;
}
function loadWidgetUrl(parameters, thisChangeId)
{
scope.loading = true;
var url = getFullWidgetUrl(parameters);
abortHttpRequestIfNeeded();
cleanupLastWidgetContent();
httpCanceler = $q.defer();
$http.get(url, {timeout: httpCanceler.promise}).success(function(response) {
if (thisChangeId !== changeCounter || !response) {
// another widget was requested meanwhile, ignore this response
return;
}
httpCanceler = null;
var newScope = scope.$new();
currentScope = newScope;
scope.loading = false;
scope.loadingFailed = false;
currentElement = contentNode.html(response).children();
$compile(currentElement)(newScope);
}).error(function () {
if (thisChangeId !== changeCounter) {
// another widget was requested meanwhile, ignore this response
return;
}
httpCanceler = null;
cleanupLastWidgetContent();
scope.loading = false;
scope.loadingFailed = true;
});
}
scope.$watch('piwikWidgetLoader', function (parameters, oldUrl) {
if (parameters) {
loadWidgetUrl(parameters, ++changeCounter);
}
});
element.on('$destroy', function() {
abortHttpRequestIfNeeded();
});
};
}
};
}
})();
|