/*+**********************************************************************************
* Advanced Funnel Charts by SalesPlatform
* Copyright (C) 2011-2016 SalesPlatform Ltd
* All Rights Reserved.
* This extension is licensed to be used within one instance of Vtiger CRM.
* Source code or binaries may not be redistributed unless expressly permitted by SalesPlatform Ltd.
* If you have any questions or comments, please email: extensions@salesplatform.ru
************************************************************************************/
Vtiger_Edit_Js("SPFunnelCharts_Edit_Js",{},{
container : false,
funnelReportsListSelectElement : false,
reportsPlotDependencies : false,
init : function() {
var statusToProceed = this.proceedRegisterEvents();
if(!statusToProceed){
return;
}
this.setContainer($('#spWidgetReport'));
this.reportsPlotDependencies = JSON.parse($("#reportsPlotDependencies").val());
console.log(this.reportsPlotDependencies);
},
/**
* Function to get the container which holds all the reports elements
* @return jQuery object
*/
getContainer : function() {
return this.container;
},
/**
* Function to set the reports container
* @params : element - which represents the reports container
* @return : current instance
*/
setContainer : function(element) {
this.container = element;
return this;
},
getForm : function() {
return $('form', this.getContainer());
},
/**
* Return select reports ui element object
*/
getFunnelReportsListSelectElement : function() {
if(this.funnelReportsListSelectElement === false) {
this.funnelReportsListSelectElement = $('#reportsListSelectElement');
}
return this.funnelReportsListSelectElement;
},
/**
* Makes sorting select of funnel reports
*/
makeReportsListSortable : function() {
var selectElement = this.getFunnelReportsListSelectElement();
var select2Element = app.getSelect2ElementFromSelect(selectElement);
/* The sorting is only available when Select2 is attached to a hidden input field */
var select2ChoiceElement = select2Element.find('ul.select2-choices');
select2ChoiceElement.sortable({
containment: select2ChoiceElement
});
},
/**
* Function which will arrange the selected element choices in order
*/
arrangeSelectChoicesInOrder : function() {
var thisInstance = this;
var container = this.getContainer();
var selectElement = this.getFunnelReportsListSelectElement();
var select2Element = app.getSelect2ElementFromSelect(selectElement);
var choicesContainer = select2Element.find('ul.select2-choices');
var choicesList = choicesContainer.find('li.select2-search-choice');
var selectedOptions = selectElement.find('option:selected');
var selectedOrder = JSON.parse($('input[name="topReportsIdsList"]', container).val());
for(var index=selectedOrder.length ; index > 0 ; index--) {
var selectedValue = selectedOrder[index-1];
var option = selectedOptions.filter('[value="'+selectedValue+'"]');
choicesList.each(function(choiceListIndex,element){
var liElement = $(element);
if(liElement.find('div').html() == option.html()){
/* Dynamic add control option on which main field report can be ploted */
var plotByControlElement = thisInstance.createPlotByControlElement(selectedValue);
liElement.append(plotByControlElement);
choicesContainer.prepend(liElement);
$('[name="reports_plot_by['+selectedValue +']"]').select2();
return false;
}
});
}
},
/**
* Creates picklist for funnel step
*
* @returns {jQuery}
*/
createPlotByControlElement : function(reportId) {
/* Get current report info about dependency */
var plotDependency = this.reportsPlotDependencies[reportId];
var selectedDependency = plotDependency.selectedPlotType;
var selectableDependencies = plotDependency.selectablePlotTypes;
/* Create picklist values with selected */
var optionsHtml = '';
for(var dependencyIndex = 0; dependencyIndex < selectableDependencies.length; dependencyIndex++) {
var dependency = selectableDependencies[dependencyIndex];
var selectedOption = selectedDependency === dependency[0] ? ' selected' : '';
optionsHtml += '';
}
return $(
'