diff options
author | dizzy <diosmosis@users.noreply.github.com> | 2021-10-11 06:36:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-11 06:36:30 +0300 |
commit | cb717535f16686a1e7202a6377dc9273bab35ab1 (patch) | |
tree | e8283e642de185118d0ee6674bcc40b2f3df0fc4 /plugins/CoreHome/angularjs | |
parent | 94c4db44b96e9ca9bc01a4c610183d9ce1537695 (diff) |
[Vue] migrate piwik service and test (#18106)
Diffstat (limited to 'plugins/CoreHome/angularjs')
-rw-r--r-- | plugins/CoreHome/angularjs/common/services/piwik.js | 86 | ||||
-rw-r--r-- | plugins/CoreHome/angularjs/common/services/piwik.spec.js | 176 |
2 files changed, 0 insertions, 262 deletions
diff --git a/plugins/CoreHome/angularjs/common/services/piwik.js b/plugins/CoreHome/angularjs/common/services/piwik.js deleted file mode 100644 index 92a82bac2e..0000000000 --- a/plugins/CoreHome/angularjs/common/services/piwik.js +++ /dev/null @@ -1,86 +0,0 @@ -/*! - * Matomo - free/libre analytics platform - * - * @link https://matomo.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ -(function () { - angular.module('piwikApp.service').service('piwik', piwikService); - - piwikService.$inject = ['piwikPeriods', 'piwikUrl']; - - function piwikService(piwikPeriods, piwikUrl) { - var originalTitle; - piwik.helper = piwikHelper; - piwik.broadcast = broadcast; - piwik.updatePeriodParamsFromUrl = updatePeriodParamsFromUrl; - piwik.updateDateInTitle = updateDateInTitle; - piwik.hasUserCapability = hasUserCapability; - return piwik; - - function hasUserCapability(capability) { - return angular.isArray(piwik.userCapabilities) && piwik.userCapabilities.indexOf(capability) !== -1; - } - - function updatePeriodParamsFromUrl() { - var date = piwikUrl.getSearchParam('date'); - var period = piwikUrl.getSearchParam('period'); - if (!isValidPeriod(period, date)) { - // invalid data in URL - return; - } - - if (piwik.period === period && piwik.currentDateString === date) { - // this period / date is already loaded - return; - } - - piwik.period = period; - - var dateRange = piwikPeriods.parse(period, date).getDateRange(); - piwik.startDateString = piwikPeriods.format(dateRange[0]); - piwik.endDateString = piwikPeriods.format(dateRange[1]); - - updateDateInTitle(date, period); - - // do not set anything to previousN/lastN, as it's more useful to plugins - // to have the dates than previousN/lastN. - if (piwik.period === 'range') { - date = piwik.startDateString + ',' + piwik.endDateString; - } - - piwik.currentDateString = date; - } - - function isValidPeriod(periodStr, dateStr) { - try { - piwikPeriods.parse(periodStr, dateStr); - return true; - } catch (e) { - return false; - } - } - - function updateDateInTitle( date, period ) { - if (!$('.top_controls #periodString').length) { - return; - } - - // Cache server-rendered page title - originalTitle = originalTitle || document.title; - - if (0 === originalTitle.indexOf(piwik.siteName)) { - var dateString = ' - ' + piwikPeriods.parse(period, date).getPrettyString() + ' '; - document.title = piwik.siteName + dateString + originalTitle.substr(piwik.siteName.length); - } - } - } - - angular.module('piwikApp.service').run(initPiwikService); - - initPiwikService.$inject = ['piwik', '$rootScope']; - - function initPiwikService(piwik, $rootScope) { - $rootScope.$on('$locationChangeSuccess', piwik.updatePeriodParamsFromUrl); - } -})(); diff --git a/plugins/CoreHome/angularjs/common/services/piwik.spec.js b/plugins/CoreHome/angularjs/common/services/piwik.spec.js deleted file mode 100644 index 14d5d7bbad..0000000000 --- a/plugins/CoreHome/angularjs/common/services/piwik.spec.js +++ /dev/null @@ -1,176 +0,0 @@ -/*! - * Matomo - free/libre analytics platform - * - * @link https://matomo.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ -(function () { - describe('piwikService', function() { - var piwikService; - - beforeEach(module('piwikApp.service')); - beforeEach(inject(function($injector) { - piwikService = $injector.get('piwik'); - })); - - describe('#piwikService', function() { - - it('should be the same as piwik global var', function() { - piwik.should.equal(piwikService); - }); - - it('should mixin broadcast', function() { - expect(piwikService.broadcast).to.be.an('object'); - }); - - it('should mixin piwikHelper', function() { - expect(piwikService.helper).to.be.an('object'); - }); - }); - - describe('#piwik_url', function() { - - it('should contain the piwik url', function() { - expect(piwikService.piwik_url).to.eql('http://localhost/'); - }); - }); - - describe('#updatePeriodParamsFromUrl()', function() { - DATE_PERIODS_TO_TEST = [ - { - date: '2012-01-02', - period: 'day', - expected: { - currentDateString: '2012-01-02', - period: 'day', - startDateString: '2012-01-02', - endDateString: '2012-01-02' - } - }, - { - date: '2012-01-02', - period: 'week', - expected: { - currentDateString: '2012-01-02', - period: 'week', - startDateString: '2012-01-02', - endDateString: '2012-01-08' - } - }, - { - date: '2012-01-02', - period: 'month', - expected: { - currentDateString: '2012-01-02', - period: 'month', - startDateString: '2012-01-01', - endDateString: '2012-01-31' - } - }, - { - date: '2012-01-02', - period: 'year', - expected: { - currentDateString: '2012-01-02', - period: 'year', - startDateString: '2012-01-01', - endDateString: '2012-12-31' - } - }, - { - date: '2012-01-02,2012-02-03', - period: 'range', - expected: { - currentDateString: '2012-01-02,2012-02-03', - period: 'range', - startDateString: '2012-01-02', - endDateString: '2012-02-03' - } - }, - // invalid - { - date: '2012-01-02', - period: 'range', - expected: { - currentDateString: undefined, - period: undefined, - startDateString: undefined, - endDateString: undefined - } - }, - { - date: 'sldfjkdslkfj', - period: 'month', - expected: { - currentDateString: undefined, - period: undefined, - startDateString: undefined, - endDateString: undefined - } - }, - { - date: '2012-01-02', - period: 'sflkjdslkfj', - expected: { - currentDateString: undefined, - period: undefined, - startDateString: undefined, - endDateString: undefined - } - } - ]; - - DATE_PERIODS_TO_TEST.forEach(function (test) { - var date = test.date, - period = test.period, - expected = test.expected; - - it('should parse the period in the URL correctly when date=' + date + ' and period=' + period, function () { - delete piwikService.currentDateString; - delete piwikService.period; - delete piwikService.startDateString; - delete piwikService.endDateString; - - history.pushState(null, null, '?date=' + date + '&period=' + period); - - piwikService.updatePeriodParamsFromUrl(); - - expect(piwikService.currentDateString).to.equal(expected.currentDateString); - expect(piwikService.period).to.equal(expected.period); - expect(piwikService.startDateString).to.equal(expected.startDateString); - expect(piwikService.endDateString).to.equal(expected.endDateString); - }); - - it('should parse the period in the URL hash correctly when date=' + date + ' and period=' + period, function () { - delete piwikService.currentDateString; - delete piwikService.period; - delete piwikService.startDateString; - delete piwikService.endDateString; - - history.pushState(null, null, '?someparam=somevalue#?date=' + date + '&period=' + period); - - piwikService.updatePeriodParamsFromUrl(); - - expect(piwikService.currentDateString).to.equal(expected.currentDateString); - expect(piwikService.period).to.equal(expected.period); - expect(piwikService.startDateString).to.equal(expected.startDateString); - expect(piwikService.endDateString).to.equal(expected.endDateString); - }); - }); - - it('should not change object values if the current date/period is the same as the URL date/period', function () { - piwik.period = 'range'; - piwik.currentDateString = '2012-01-01,2012-01-02'; - piwik.startDateString = 'shouldnotchange'; - piwik.endDateString = 'shouldnotchangeeither'; - - history.pushState(null, null, '?someparam=somevalue#?date=' + piwik.currentDateString + '&period=' + piwik.period); - - piwikService.updatePeriodParamsFromUrl(); - - expect(piwikService.startDateString).to.equal('shouldnotchange'); - expect(piwikService.endDateString).to.equal('shouldnotchangeeither'); - }); - }); - }); -})();
\ No newline at end of file |