Commit | Line | Data |
---|---|---|
f263929f CW |
1 | (function(angular, $, _) { |
2 | ||
3 | angular.module('crmDashboard').component('crmDashlet', { | |
4 | bindings: { | |
5 | dashlet: '<', | |
6 | remove: '&', | |
7 | fullscreen: '&', | |
8 | isFullscreen: '<' | |
9 | }, | |
10 | templateUrl: '~/crmDashboard/Dashlet.html', | |
11 | controller: function ($scope, $element, $timeout, $interval) { | |
12 | var ts = $scope.ts = CRM.ts(), | |
13 | ctrl = this, | |
14 | lastLoaded, | |
15 | checker; | |
16 | ||
17 | function getCache() { | |
18 | return CRM.cache.get('dashboard', {})[ctrl.dashlet.id] || {}; | |
19 | } | |
20 | ||
21 | function setCache(content) { | |
22 | var data = CRM.cache.get('dashboard', {}), | |
23 | cached = data[ctrl.dashlet.id] || {}; | |
24 | data[ctrl.dashlet.id] = { | |
25 | content: content || cached.content || null, | |
26 | collapsed: ctrl.collapsed, | |
27 | lastLoaded: content ? $.now() : (cached.lastLoaded || null) | |
28 | }; | |
29 | CRM.cache.set('dashboard', data); | |
30 | lastLoaded = data[ctrl.dashlet.id].lastLoaded; | |
31 | } | |
32 | ||
33 | function isFresh() { | |
34 | return lastLoaded && (ctrl.dashlet.cache_minutes * 60000 + lastLoaded) > $.now(); | |
35 | } | |
36 | ||
37 | function setChecker() { | |
38 | if (angular.isUndefined(checker)) { | |
39 | checker = $interval(function() { | |
40 | if (!ctrl.collapsed && !isFresh() && (!document.hasFocus || document.hasFocus())) { | |
41 | stopChecker(); | |
42 | reload(ctrl.dashlet.url); | |
43 | } | |
44 | }, 1000); | |
45 | } | |
46 | } | |
47 | ||
48 | function stopChecker() { | |
49 | if (angular.isDefined(checker)) { | |
50 | $interval.cancel(checker); | |
51 | checker = undefined; | |
52 | } | |
53 | } | |
54 | ||
55 | this.toggleCollapse = function() { | |
56 | ctrl.collapsed = !ctrl.collapsed; | |
57 | setCache(); | |
58 | }; | |
59 | ||
60 | this.forceRefresh = function() { | |
61 | if (ctrl.dashlet.url) { | |
62 | reload(ctrl.dashlet.url); | |
63 | } else if (ctrl.dashlet.directive) { | |
64 | var directive = ctrl.dashlet.directive; | |
65 | ctrl.dashlet.directive = null; | |
66 | $timeout(function() { | |
67 | ctrl.dashlet.directive = directive; | |
68 | }, 10); | |
69 | } | |
70 | }; | |
71 | ||
72 | function reload(path) { | |
73 | var extern = path.slice(0, 1) === '/' || path.slice(0, 4) === 'http', | |
74 | url = extern ? path : CRM.url(path); | |
75 | CRM.loadPage(url, {target: $('.crm-dashlet-content', $element)}); | |
76 | } | |
77 | ||
78 | this.$onInit = function() { | |
79 | if (this.isFullscreen && this.dashlet.fullscreen_url) { | |
80 | reload(this.dashlet.fullscreen_url); | |
81 | return; | |
82 | } | |
83 | ||
84 | var cache = getCache(); | |
85 | lastLoaded = cache.lastLoaded; | |
86 | ctrl.collapsed = !this.fullscreen && !!cache.collapsed; | |
87 | ||
88 | if (ctrl.dashlet.url) { | |
89 | var fresh = cache.content && isFresh(); | |
90 | if (fresh) { | |
91 | $('.crm-dashlet-content', $element).html(cache.content).trigger('crmLoad'); | |
92 | setChecker(); | |
93 | } | |
94 | ||
95 | $element.on('crmLoad', function(event, data) { | |
96 | if ($(event.target).is('.crm-dashlet-content')) { | |
97 | setCache(data.content); | |
98 | setChecker(); | |
99 | } | |
100 | }); | |
101 | ||
102 | if (!fresh) { | |
103 | reload(ctrl.dashlet.url); | |
104 | } | |
105 | } | |
106 | ||
107 | }; | |
108 | ||
109 | this.$onDestroy = function() { | |
110 | stopChecker(); | |
111 | }; | |
112 | } | |
113 | }); | |
114 | ||
115 | })(angular, CRM.$, CRM._); |