[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/layouts/vlayout/modules/Reports/resources/ -> Edit2.js (source)

   1  /*+***********************************************************************************
   2   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   3   * ("License"); You may not use this file except in compliance with the License
   4   * The Original Code is:  vtiger CRM Open Source
   5   * The Initial Developer of the Original Code is vtiger.
   6   * Portions created by vtiger are Copyright (C) vtiger.
   7   * All Rights Reserved.
   8   *************************************************************************************/
   9  Reports_Edit_Js("Reports_Edit2_Js",{},{
  10  
  11      step2Container : false,
  12  
  13      //This will contain the reports multi select element
  14      reportsColumnsList : false,
  15  
  16      //This will contain the selected fields element
  17      selectedFields : false,
  18  
  19      init : function() {
  20          this.initialize();
  21      },
  22      /**
  23       * Function to get the container which holds all the report elements
  24       * @return jQuery object
  25       */
  26      getContainer : function() {
  27          return this.step2Container;
  28      },
  29  
  30      /**
  31       * Function to set the report step2 container
  32       * @params : element - which represents the report step2 container
  33       * @return : current instance
  34       */
  35      setContainer : function(element) {
  36          this.step2Container = element;
  37          return this;
  38      },
  39  
  40      /**
  41       * Function to get the multi select element
  42       * @return : jQuery object of reports multi select element
  43       */
  44      getReportsColumnsList : function() {
  45          if(this.reportsColumnsList == false) {
  46              this.reportsColumnsList = jQuery('#reportsColumnsList');
  47          }
  48          return this.reportsColumnsList;
  49      },
  50  
  51      /**
  52       * Function to get the selected fields
  53       * @return : jQuery object of selected fields
  54       */
  55      getSelectedFields : function() {
  56          if(this.selectedFields == false) {
  57              this.selectedFields = jQuery('#seleted_fields');
  58          }
  59          return this.selectedFields;
  60      },
  61  
  62      /**
  63       * Function  to intialize the reports step2
  64       */
  65      initialize : function(container) {
  66          if(typeof container == 'undefined') {
  67              container = jQuery('#report_step2');
  68          }
  69  
  70          if(container.is('#report_step2')) {
  71              this.setContainer(container);
  72          }else{
  73              this.setContainer(jQuery('#report_step2'));
  74          }
  75      },
  76      /*
  77       * Function to validate special cases in the form
  78       * returns result
  79       */
  80      isFormValidate : function(){
  81          var thisInstance = this;
  82          var selectElement = this.getReportsColumnsList();
  83          var select2Element = app.getSelect2ElementFromSelect(selectElement);
  84          var result = Vtiger_MultiSelect_Validator_Js.invokeValidation(selectElement);
  85          if(result != true){
  86              select2Element.validationEngine('showPrompt', result , 'error','bottomLeft',true);
  87              var form = thisInstance.getContainer();
  88              app.formAlignmentAfterValidation(form);
  89              return false;
  90          } else {
  91              select2Element.validationEngine('hide');
  92              return true;
  93          }
  94      },
  95      /*
  96       * Fucntion to perform all the requires calculation before submit
  97       */
  98      calculateValues : function(){
  99          var container = this.getContainer();
 100          //Handled select fields values
 101          var selectedFields = this.getSelectedColumns();
 102          this.getSelectedFields().val(JSON.stringify(selectedFields));
 103  
 104          //handled selected sort fields
 105          var selectedSortOrderFields = new Array();
 106          var selectedSortFieldsRows = jQuery('.sortFieldRow',container);
 107          jQuery.each(selectedSortFieldsRows,function(index,element){
 108              var currentElement = jQuery(element);
 109              var field = currentElement.find('.selectedSortFields').val();
 110              var order = currentElement.find('.sortOrder').filter(':checked').val();
 111              //TODO: need to handle sort type for Reports
 112              var type = currentElement.find('.sortType').val();
 113              selectedSortOrderFields.push([field,order,type]);
 114          });
 115          jQuery('#selected_sort_fields').val(JSON.stringify(selectedSortOrderFields));
 116  
 117          //handled Selected Calculation fields
 118  
 119          var selectedCalculationFields = {};
 120          var calculationFieldsTable = jQuery('.CalculationFields',container);
 121          var calculationFieldRows = calculationFieldsTable.find('.calculationFieldRow');
 122          var indexValue = 0;
 123          jQuery.each(calculationFieldRows,function(index,element){
 124              var calculationTypes = jQuery(element).find('.calculationType:checked');
 125              jQuery.each(calculationTypes,function(index,element){
 126                  selectedCalculationFields[indexValue] = jQuery(element).val();
 127                  indexValue++;
 128              });
 129          });
 130          jQuery('#calculation_fields').val(JSON.stringify(selectedCalculationFields));
 131      },
 132      submit : function(){
 133          var aDeferred = jQuery.Deferred();
 134          this.calculateValues();
 135          var form = this.getContainer();
 136          var formData = form.serializeFormData();
 137          var progressIndicatorElement = jQuery.progressIndicator({
 138              'position' : 'html',
 139              'blockInfo' : {
 140                  'enabled' : true
 141              }
 142          });
 143          AppConnector.request(formData).then(
 144              function(data) {
 145                  form.hide();
 146                  progressIndicatorElement.progressIndicator({
 147                      'mode' : 'hide'
 148                  })
 149                  aDeferred.resolve(data);
 150              },
 151              function(error,err){
 152  
 153              }
 154          );
 155          return aDeferred.promise();
 156      },
 157  
 158      /**
 159       * Function which will register the select2 elements for columns selection
 160       */
 161      registerSelect2ElementForReportColumns : function() {
 162          var selectElement = this.getReportsColumnsList();
 163          app.changeSelectElementView(selectElement, 'select2', {maximumSelectionSize: 25,dropdownCss : {'z-index' : 0}});
 164      },
 165  
 166      /**
 167       * Function which will get the selected columns with order preserved
 168       * @return : array of selected values in order
 169       */
 170      getSelectedColumns : function() {
 171          var columnListSelectElement = this.getReportsColumnsList();
 172          var select2Element = app.getSelect2ElementFromSelect(columnListSelectElement);
 173  
 174          var selectedValuesByOrder = new Array();
 175          var selectedOptions = columnListSelectElement.find('option:selected');
 176  
 177          var orderedSelect2Options = select2Element.find('li.select2-search-choice').find('div');
 178          orderedSelect2Options.each(function(index,element){
 179              var chosenOption = jQuery(element);
 180              var choiceElement = chosenOption.closest('.select2-search-choice');
 181              var choiceValue = choiceElement.data('select2Data').id;
 182              selectedOptions.each(function(optionIndex, domOption){
 183                  var option = jQuery(domOption);
 184                  if(option.val() == choiceValue) {
 185                      selectedValuesByOrder.push(option.val());
 186                      return false;
 187                  }
 188              });
 189          });
 190          return selectedValuesByOrder;
 191      },
 192  
 193      /**
 194       * Function which will arrange the select2 element choices in order
 195       */
 196      arrangeSelectChoicesInOrder : function() {
 197          var selectElement = this.getReportsColumnsList();
 198          var chosenElement = app.getSelect2ElementFromSelect(selectElement);
 199          var choicesContainer = chosenElement.find('ul.select2-choices');
 200          var choicesList = choicesContainer.find('li.select2-search-choice');
 201  
 202          //var coulmnListSelectElement = Vtiger_CustomView_Js.getColumnSelectElement();
 203          var selectedOptions = selectElement.find('option:selected');
 204          var selectedOrder = JSON.parse(this.getSelectedFields().val());
 205          var selectedOrderKeys = [];
 206          for(var key in selectedOrder) {
 207              if(selectedOrder.hasOwnProperty(key)){
 208                  selectedOrderKeys.push(key);
 209              }
 210          }
 211          for(var index=selectedOrderKeys.length ; index > 0 ; index--) {
 212              var selectedValue = selectedOrder[selectedOrderKeys[index-1]];
 213              var option = selectedOptions.filter('[value="'+selectedValue+'"]');
 214              choicesList.each(function(choiceListIndex,element){
 215                  var liElement = jQuery(element);
 216                  if(liElement.find('div').html() == option.html()){
 217                      choicesContainer.prepend(liElement);
 218                      return false;
 219                  }
 220              });
 221          }
 222      },
 223  
 224      /**
 225       * Function to regiser the event to make the columns list sortable
 226       */
 227      makeColumnListSortable : function() {
 228          var thisInstance = this;
 229          var selectElement = thisInstance.getReportsColumnsList();
 230          var select2Element = app.getSelect2ElementFromSelect(selectElement);
 231          //TODO : peform the selection operation in context this might break if you have multi select element in advance filter
 232          //The sorting is only available when Select2 is attached to a hidden input field.
 233          var chozenChoiceElement = select2Element.find('ul.select2-choices');
 234          chozenChoiceElement.sortable({
 235                  containment: 'parent',
 236                  start: function() {thisInstance.getSelectedFields().select2("onSortStart");},
 237                  update: function() {thisInstance.getSelectedFields().select2("onSortEnd");}
 238              });
 239      },
 240  
 241      /**
 242       * Function is used to limit the calculation for line item fields and inventory module fields.
 243       * only one of these fields can be used at a time
 244       */
 245      registerLineItemCalculationLimit : function() {
 246          var thisInstance = this;
 247          var primaryModule = jQuery('input[name="primary_module"]').val();
 248          var inventoryModules = ['Invoice', 'Quotes', 'PurchaseOrder', 'SalesOrder'];
 249          // To limit the calculation fields if secondary module contains inventoryModule
 250          var secodaryModules = jQuery('input[name="secondary_modules"]').val();
 251          var secondaryIsInventory = false;
 252          inventoryModules.forEach(function(entry){
 253             if(secodaryModules.indexOf(entry) != -1){
 254                 secondaryIsInventory = true;
 255             } 
 256          });
 257          if(jQuery.inArray(primaryModule, inventoryModules) !== -1 || secondaryIsInventory) {
 258              jQuery('.CalculationFields').on('change', 'input[type="checkbox"]', function(e) {
 259                  var element = jQuery(e.currentTarget);
 260                  var value = element.val();
 261                  var reg = new RegExp(/cb:vtiger_inventoryproductrel*/);
 262                  var attr = element.is(':checked');
 263                  var moduleCalculationFields = jQuery('.CalculationFields input[type="checkbox"]').not('[value^="cb:vtiger_inventoryproductrel"]');
 264                  var lineItemCalculationFields = jQuery('.CalculationFields').find('[value^="cb:vtiger_inventoryproductrel"]');
 265                  if(reg.test(value)) {    // line item field selected
 266                      if(attr) {    // disable all the other checkboxes
 267                          moduleCalculationFields.attr('checked',false).attr('disabled',true);
 268                      } else {
 269                          var otherLineItemFieldsCheckedLength = lineItemCalculationFields.filter(':checked').length;
 270                          if(otherLineItemFieldsCheckedLength == 0) moduleCalculationFields.attr('disabled',false);
 271                          else moduleCalculationFields.attr('checked',false).attr('disabled',true);
 272                      }
 273                  } else {        // some other field is selected
 274                      if(attr) {
 275                          lineItemCalculationFields.attr('checked',false).attr('disabled',true)
 276                      } else {
 277                          var moduleCalculationFieldLength = moduleCalculationFields.filter(':checked').length
 278                          if(moduleCalculationFieldLength == 0) lineItemCalculationFields.attr('disabled', false);
 279                          else lineItemCalculationFields.attr('disabled', true).attr('checked',false);
 280                      }
 281                  }
 282                  thisInstance.displayLineItemFieldLimitationMessage();
 283              });
 284          }
 285      },
 286      displayLineItemFieldLimitationMessage : function() {
 287          var message = app.vtranslate('JS_CALCULATION_LINE_ITEM_FIELDS_SELECTION_LIMITATION');
 288          if(jQuery('#calculationLimitationMessage').length == 0) {
 289              jQuery('.CalculationFields').parent().append('<div id="calculationLimitationMessage" class="pull-right alert alert-info">'+message+'</div>');
 290          } else {
 291              jQuery('#calculationLimitationMessage').html(message);
 292          }
 293      },
 294  
 295      registerLineItemCalculationLimitOnLoad : function() {
 296          var moduleCalculationFields = jQuery('.CalculationFields input[type="checkbox"]').not('[value^="cb:vtiger_inventoryproductrel"]');
 297          var lineItemFields = jQuery('.CalculationFields').find('[value^="cb:vtiger_inventoryproductrel"]');
 298          if(moduleCalculationFields.filter(':checked').length != 0) {
 299              lineItemFields.attr('checked', false).attr('disabled', true);
 300              this.displayLineItemFieldLimitationMessage();
 301          } else if(lineItemFields.filter(':checked').length != 0) {
 302              moduleCalculationFields.attr('checked', false).attr('disabled', true);
 303              this.displayLineItemFieldLimitationMessage();
 304          }
 305      },
 306  
 307      registerEvents : function(){
 308          var container = this.getContainer();
 309          //If the container is reloading, containers cache should be reset
 310          this.reportsColumnsList = false;
 311          this.selectedFields = false;
 312          this.registerSelect2ElementForReportColumns();
 313          this.arrangeSelectChoicesInOrder();
 314          this.makeColumnListSortable();
 315          this.registerLineItemCalculationLimit();
 316          this.registerLineItemCalculationLimitOnLoad();
 317          app.changeSelectElementView(container);
 318          container.validationEngine({
 319              // to prevent the page reload after the validation has completed
 320              'onValidationComplete' : function(form,valid){
 321                  return valid;
 322              }
 323          });
 324      }
 325  });
 326  
 327  


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1