| |
| |
| |
| |
| |
| |
|
|
| (function ($, piwik) { |
|
|
| var annotationsApi = { |
|
|
| |
| getAnnotationManager: function (idSite, date, period, lastN, callback) { |
| var ajaxParams = |
| { |
| module: 'Annotations', |
| action: 'getAnnotationManager', |
| idSite: idSite, |
| date: date, |
| period: period, |
| filter_limit: '-1' |
| }; |
| if (lastN) { |
| ajaxParams.lastN = lastN; |
| } |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams(ajaxParams, 'get'); |
| ajaxRequest.setCallback(callback); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| |
| addAnnotation: function (idSite, managerDate, managerPeriod, date, note, callback) { |
| var ajaxParams = |
| { |
| module: 'Annotations', |
| action: 'addAnnotation', |
| idSite: idSite, |
| date: date, |
| managerDate: managerDate, |
| managerPeriod: managerPeriod, |
| note: note |
| }; |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams(ajaxParams, 'get'); |
| |
| ajaxRequest.removeDefaultParameter('period'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setCallback(callback); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| |
| saveAnnotation: function (idSite, idNote, date, noteData, callback) { |
| var ajaxParams = |
| { |
| module: 'Annotations', |
| action: 'saveAnnotation', |
| idSite: idSite, |
| idNote: idNote, |
| date: date |
| }; |
|
|
| for (var key in noteData) { |
| ajaxParams[key] = noteData[key]; |
| } |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams(ajaxParams, 'get'); |
| |
| ajaxRequest.removeDefaultParameter('period'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setCallback(callback); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| |
| deleteAnnotation: function (idSite, idNote, managerDate, managerPeriod, callback) { |
| var ajaxParams = |
| { |
| module: 'Annotations', |
| action: 'deleteAnnotation', |
| idSite: idSite, |
| idNote: idNote, |
| date: managerDate, |
| period: managerPeriod |
| }; |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams(ajaxParams, 'get'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setCallback(callback); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| |
| getEvolutionIcons: function (idSite, date, period, lastN, callback) { |
| var ajaxParams = |
| { |
| module: 'Annotations', |
| action: 'getEvolutionIcons', |
| idSite: idSite, |
| date: date, |
| period: period, |
| filter_limit: '-1' |
| }; |
| if (lastN) { |
| ajaxParams.lastN = lastN; |
| } |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams(ajaxParams, 'get'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.setCallback(callback); |
| ajaxRequest.send(); |
| } |
| }; |
|
|
| var today = new Date(); |
|
|
| |
| |
| |
| |
| |
| var getDatePickerOptions = function (annotation) { |
| var annotationDateStr = annotation.attr('data-date'), |
| parts = annotationDateStr.split('-'), |
| annotationDate = new Date(parts[0], parts[1] - 1, parts[2]); |
|
|
| var result = piwik.getBaseDatePickerOptions(annotationDate); |
| result.showButtonPanel = true; |
| result.currentText = _pk_translate('Intl_Today'); |
|
|
| |
| var piwikMinDate = result.minDate; |
| result.beforeShowDay = function (date) { |
| var valid = true; |
|
|
| |
| if (date > today |
| || date < piwikMinDate) { |
| valid = false; |
| } |
|
|
| return [valid, '']; |
| }; |
|
|
| |
| result.onSelect = function (dateText) { |
| $('.annotation-period-edit>a', annotation).text(dateText); |
| $('.datepicker', annotation).hide(); |
| }; |
|
|
| return result; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| var toggleAnnotationMode = function (inAnnotationElement) { |
| var annotation = $(inAnnotationElement).closest('.annotation'); |
| annotation.toggleClass('edit') |
| $('.annotation-period,.annotation-period-edit,.delete-annotation,' + |
| '.annotation-edit-mode,.annotation-view-mode', annotation).toggle(); |
|
|
| return $(inAnnotationElement).find('.annotation-value'); |
| }; |
|
|
| |
| |
| |
| |
| |
| var createDatePicker = function (annotation) { |
| $('.datepicker', annotation).datepicker(getDatePickerOptions(annotation)).hide(); |
| }; |
|
|
| |
| |
| |
| |
| |
| var createDatePickers = function (manager) { |
| $('.annotation-period-edit', manager).each(function () { |
| createDatePicker($(this).parent().parent()); |
| }); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| var replaceAnnotationManager = function (manager, html) { |
| var newManager = $(html); |
| manager.html(newManager.html()) |
| .attr('data-date', newManager.attr('data-date')) |
| .attr('data-period', newManager.attr('data-period')); |
| createDatePickers(manager); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| var isAnnotationStarred = function (annotation) { |
| return !!(+$('.annotation-star', annotation).attr('data-starred') == 1); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| var replaceAnnotationHtml = function (annotation, html) { |
| var newHtml = $(html); |
| annotation.html(newHtml.html()).attr('data-date', newHtml.attr('data-date')); |
| createDatePicker(annotation); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| var bindAnnotationManagerEvents = function (manager, idSite, onAnnotationCountChange) { |
| if (!onAnnotationCountChange) { |
| onAnnotationCountChange = function () {}; |
| } |
|
|
| |
| manager.on('click', '.add-annotation', function (e) { |
| e.preventDefault(); |
|
|
| var $newRow = $('.new-annotation-row', manager); |
| $newRow.show(); |
| $(this).hide(); |
|
|
| return false; |
| }); |
|
|
| |
| manager.on('click', '.new-annotation-cancel', function () { |
| var newAnnotationRow = $(this).parent().parent(); |
| newAnnotationRow.hide(); |
|
|
| $('.add-annotation', newAnnotationRow.closest('.annotation-manager')).show(); |
| }); |
|
|
| |
| manager.on('click', '.new-annotation-save', function () { |
| var addRow = $(this).parent().parent(), |
| addNoteInput = addRow.find('.new-annotation-edit'), |
| noteDate = addRow.find('.annotation-period-edit>a').text(); |
|
|
| |
| if (!addNoteInput.val()) { |
| return; |
| } |
|
|
| |
| addNoteInput.attr('disabled', 'disabled'); |
| $(this).attr('disabled', 'disabled'); |
|
|
| |
| annotationsApi.addAnnotation( |
| idSite, |
| manager.attr('data-date'), |
| manager.attr('data-period'), |
| noteDate, |
| addNoteInput.val(), |
| function (response) { |
| replaceAnnotationManager(manager, response); |
|
|
| |
| onAnnotationCountChange(noteDate, 1, 0); |
| } |
| ); |
| }); |
|
|
| |
| manager.on('keypress', '.new-annotation-edit', function (e) { |
| if (e.which == 13) { |
| $(this).parent().find('.new-annotation-save').click(); |
| } |
| }); |
|
|
| |
| manager.on('click', '.annotation-enter-edit-mode', function (e) { |
| e.preventDefault(); |
|
|
| var annotationContent = toggleAnnotationMode(this); |
| annotationContent.find('.annotation-edit').focus(); |
|
|
| return false; |
| }); |
|
|
| |
| manager.on('click', '.annotation-cancel', function () { |
| toggleAnnotationMode(this); |
| }); |
|
|
| |
| manager.on('click', '.annotation-edit-mode .annotation-save', function () { |
| var annotation = $(this).parent().parent().parent(), |
| input = $('.annotation-edit', annotation), |
| dateEditText = $('.annotation-period-edit>a', annotation).text(); |
|
|
| |
| if (input[0].defaultValue == input.val() |
| && dateEditText == annotation.attr('data-date')) { |
| toggleAnnotationMode(this); |
| return; |
| } |
|
|
| |
| input.attr('disabled', 'disabled'); |
| $(this).attr('disabled', 'disabled'); |
|
|
| |
| annotationsApi.saveAnnotation( |
| idSite, |
| annotation.attr('data-id'), |
| dateEditText, |
| { |
| note: input.val() |
| }, |
| function (response) { |
| response = $(response); |
|
|
| var newDate = response.attr('data-date'), |
| isStarred = isAnnotationStarred(response), |
| originalDate = annotation.attr('data-date'); |
|
|
| replaceAnnotationHtml(annotation, response); |
|
|
| |
| if (originalDate != newDate) { |
| |
| onAnnotationCountChange(originalDate, -1, isStarred ? -1 : 0); |
|
|
| |
| onAnnotationCountChange(newDate, 1, isStarred ? 1 : 0); |
| } |
| } |
| ); |
| }); |
|
|
| |
| manager.on('keypress', '.annotation-value input', function (e) { |
| if (e.which == 13) { |
| $(this).parent().find('.annotation-save').click(); |
| } |
| }); |
|
|
| |
| manager.on('click', '.delete-annotation', function (e) { |
| e.preventDefault(); |
|
|
| var annotation = $(this).parent().parent(); |
| $(this).attr('disabled', 'disabled'); |
|
|
| |
| annotationsApi.deleteAnnotation( |
| idSite, |
| annotation.attr('data-id'), |
| manager.attr('data-date'), |
| manager.attr('data-period'), |
| function (response) { |
| replaceAnnotationManager(manager, response); |
|
|
| |
| var isStarred = isAnnotationStarred(annotation); |
| onAnnotationCountChange(annotation.attr('data-date'), -1, isStarred ? -1 : 0); |
| } |
| ); |
|
|
| return false; |
| }); |
|
|
| |
| manager.on('click', '.annotation-star-changeable', function (e) { |
| var annotation = $(this).parent().parent(), |
| newStarredVal = $(this).attr('data-starred') == 0 ? 1 : 0 |
| ; |
|
|
| |
| annotationsApi.saveAnnotation( |
| idSite, |
| annotation.attr('data-id'), |
| annotation.attr('data-date'), |
| { |
| starred: newStarredVal |
| }, |
| function (response) { |
| replaceAnnotationHtml(annotation, response); |
|
|
| |
| |
| onAnnotationCountChange(annotation.attr('data-date'), 0, newStarredVal == 0 ? -1 : 1); |
| } |
| ); |
| }); |
|
|
| |
| manager.on('click', '.annotation-period-edit>a', function (e) { |
| e.preventDefault(); |
| $('.datepicker', $(this).parent()).toggle(); |
| return false; |
| }); |
|
|
| |
| $('body').on('mouseup', function (e) { |
| var container = $('.annotation-period-edit>.datepicker:visible').parent(); |
|
|
| if (!container.has(e.target).length) { |
| container.find('.datepicker').hide(); |
| } |
| }); |
| }; |
|
|
| |
| var loadingAnnotationManager = false; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| var showAnnotationViewer = function (domElem, idSite, date, period, lastN, callback) { |
| var addToAnnotationCount = function (date, amt, starAmt) { |
| if (date.indexOf(',') != -1) { |
| date = date.split(',')[0]; |
| } |
|
|
| $('.evolution-annotations>span[data-date]', domElem).each(function () { |
| if ($(this).attr('data-date') == date) { |
| |
| var starredCount = +$(this).attr('data-starred'), |
| annotationCount = +$(this).attr('data-count'); |
|
|
| |
| var newStarCount = starredCount + starAmt; |
| var newAnno = 'icon-annotation'; |
| if (newStarCount > 0) { |
| newAnno += ' starred'; |
| } |
| $(this).attr('data-starred', newStarCount).find('span').attr('class', newAnno); |
|
|
| |
| var newCount = annotationCount + amt; |
| $(this).attr('data-count', newCount).css('opacity', newCount > 0 ? 1 : 0); |
|
|
| return false; |
| } |
| }); |
| }; |
|
|
| var manager = $('.annotation-manager', domElem); |
| if (manager.length) { |
| |
| |
| if (manager.attr('data-date') == date |
| && manager.attr('data-period') == period) { |
| |
| if (manager.is(':hidden')) { |
| manager.slideDown('slow', function () { if (callback) callback(manager) }); |
| } |
| else { |
| manager.slideUp('slow', function () { if (callback) callback(manager) }); |
| } |
| } |
| else { |
| |
| $('.annotations', manager).html(''); |
| $('.loadingPiwik', manager).show(); |
|
|
| |
| annotationsApi.getAnnotationManager(idSite, date, period, lastN, function (response) { |
|
|
| replaceAnnotationManager(manager, response); |
|
|
| createDatePickers(manager); |
|
|
| |
| if (manager.is(':hidden')) { |
| manager.slideDown('slow', function () { if (callback) callback(manager) }); |
| } |
| else { |
| if (callback) { |
| callback(manager); |
| } |
| } |
| }); |
| } |
| } |
| else { |
| |
| if (loadingAnnotationManager) { |
| return; |
| } |
|
|
| loadingAnnotationManager = true; |
|
|
| $('.loadingPiwikBelow', domElem).insertAfter($('.evolution-annotations', domElem)); |
|
|
| var loading = $('.loadingPiwikBelow', domElem).css({display: 'block'}); |
|
|
| |
| |
| annotationsApi.getAnnotationManager(idSite, date, period, lastN, function (response) { |
| var manager = $(response).hide(); |
|
|
| |
| if (!manager.hasClass('annotation-manager')) { |
| return; |
| } |
|
|
| |
| createDatePickers(manager); |
|
|
| bindAnnotationManagerEvents(manager, idSite, addToAnnotationCount); |
|
|
| loading.css('visibility', 'hidden'); |
|
|
| placeAnnotationManager(manager, domElem); |
|
|
| manager.slideDown('slow', function () { |
| loading.hide().css('visibility', 'visible'); |
| loadingAnnotationManager = false; |
|
|
| if (callback) callback(manager) |
| }); |
| }); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| var placeEvolutionIcons = function (annotations, graphElem) { |
| var canvases = $('.piwik-graph .jqplot-xaxis canvas', graphElem), |
| noteSize = 16; |
|
|
| |
| if (!canvases || canvases.length == 0) { |
| $('span[data-date]', annotations).hide(); |
| return true; |
| } |
|
|
| |
| $('span[data-date]', annotations).each(function (i) { |
| var canvas = $(canvases[i]), |
| canvasCenterX = canvas.position().left + (canvas.width() / 2); |
| $(this).css({ |
| left: canvasCenterX - noteSize / 2, |
| |
| opacity: +$(this).attr('data-count') > 0 ? 1 : 0 |
| }); |
| }); |
| }; |
|
|
| var getPlotLinesLegendFooter = function (graphElem) { |
| var legendFooter = $('.jqplot-legend-footer', graphElem); |
|
|
| if (legendFooter.length) { |
| return legendFooter; |
| } |
|
|
| return $(); |
| }; |
|
|
| var placeEvolutionAnnotations = function (annotations, graphElem) { |
| var legendFooter = getPlotLinesLegendFooter(graphElem); |
|
|
| if (legendFooter.length) { |
| annotations.insertBefore(legendFooter); |
| return; |
| } |
|
|
| annotations.insertBefore($('.dataTableFooterNavigation', graphElem)); |
| }; |
|
|
| var placeAnnotationManager = function (manager, graphElem) { |
| var legendFooter = getPlotLinesLegendFooter(graphElem); |
|
|
| if (legendFooter.length) { |
| manager.insertBefore(legendFooter); |
| return; |
| } |
|
|
| manager.insertAfter($('.evolution-annotations', graphElem)); |
| }; |
|
|
| |
| piwik.annotations = { |
| showAnnotationViewer: showAnnotationViewer, |
| placeEvolutionIcons: placeEvolutionIcons, |
| placeEvolutionAnnotations: placeEvolutionAnnotations, |
| api: annotationsApi |
| }; |
|
|
| }(jQuery, piwik)); |
|
|