gerd
11/7/2016 - 6:20 PM

createtemplategroups_udf.js

/**
 * This js will handle all the Create template Groups UDF functionalities
 * author : turlra01
 */

lexisnexis.component.CreateTemplateGroupsUDF = lexisnexis.component.JSTreeSingleNodes
.extend({
  data : {
    getGroupsUDFsURL : '/getGroupsUDFValues',
    GET_PROVIDER_COUNT_UDFs: "getGroupsCountForUDFs",
    availableDiv : '#availableUDFList',
    selectedDiv : '#selectedUDFList',
    searchUDFInputBox : '#udfSearch',
    udfMoveRightBtn : '#udf-move-right',
    udfMoveLeftBtn : '#udf-move-left',
    udfSelectAllAvailableCheckBox : '#udfSelectAllAvailable',
    udfSelectAllSelectedCheckBox : '#udfSelectAllSelected',
    selectedListPlaceHolder :"#udf-Selected-list-placeholder",
    availableListPlaceholder : "#udf-Avail-list-placeholder",
    udfClassification : "#udfClassification",
    noMatchFound : "#udf-No-Match-Result",
    outOfStockIds : [],
    maxRecordCount : LN.THRESHOLD_LIMIT,
    selectedItemsCountMsgDivID : "#selectedItemsCountMsgDiv",
    selectedMaxOutMessage : "You have exceeded the " +  LN.THRESHOLD_LIMIT + " maximum number of user defined fields that " +
    "you may add to the Selected List. Please uncheck <UNCHECK_COUNT> names(s) from the Available List",  
    searchKey : '',
    timerObj : ''
  },
  ready : function() {
    var cs = this;  
    cs.setupJSTree();
    cs.onChangeOfClassification();
    cs.setupUDFJSSearch();
    cs.onChangeOfSelectAllBox();
    cs.registerMoveRightButton();
    cs.registerMoveLeftButton();
    LN.getPage().registerEvent(EVENT_TYPES.APPLY_BUTTON_CLICK, cs, cs.onDataSetChangeProviderUDF);
  },
  /*
   *Method creates Available and selected Js trees 
   */
  setupJSTree : function(){
    var cs = this;
    var params = JSON.stringify({
      'selDataSet' : $('#ln_topmenu_dataset').attr("dataSetkey"),
      'selectedClassification' : $(cs.data.udfClassification).val(),
      'searchKey' : $(cs.data.searchUDFInputBox).val(),
      'availableDataRequred' : true
    });

    LN.ajaxPostJSON($(cs.data.availableDiv).parent(), LN.getContextPath() + cs.data.getGroupsUDFsURL, params, cs.setupJSTreeCallBack, cs); 
  },
  /**
   * Method would be called by setupJSTree function
   */
  setupJSTreeCallBack : function(cs, data){
    cs.updateOutOfStockNodes(cs, data.rightPanel);
    cs.setupAvailableJsTree(cs, data, cs.data.availableDiv);
    cs.setupSelectedJsTree(cs, data, cs.data.selectedDiv);
  },
  /**
   * Method would get called when user changes the 
   * classification drop down
   */
  onChangeOfClassification : function(){
    var cs = this;
    $(cs.data.udfClassification).on('change', function() {
      var params = JSON.stringify({
        'selDataSet' : $('#ln_topmenu_dataset').attr("dataSetkey"),
        'selectedClassification' : $(cs.data.udfClassification).val(),
        'searchKey' : $(cs.data.searchUDFInputBox).val(),
        'availableDataRequred' : false
      });
      $('#udfSelectAllAvailable').prop('checked', false);
      LN.ajaxPostJSON($(cs.data.availableDiv).parent(), LN.getContextPath() + cs.data.getGroupsUDFsURL, params, cs.reloadAvailableJsTree, cs);
    });
  },
  /**
   * Reload left panel when you change classification drop box
   */
  reloadAvailableJsTree : function(cs, data){
    var availableJsTree = $(cs.data.availableDiv);
    availableJsTree.jstree("destroy");
    cs.setupAvailableJsTree(cs, data, $(cs.data.availableDiv));
  },
  setupUDFJSSearch : function(){
    var cs = this;
    cs.setupJSSearch($(cs.data.searchUDFInputBox));
    // Search
    $(cs.data.searchUDFInputBox).keyup(function(e) {
      e.preventDefault();
      clearTimeout(cs.data.timerObj);
      cs.data.timerObj = setTimeout(function() {
        if(cs.data.searchKey != $(cs.data.searchUDFInputBox).val() 
                && $(cs.data.searchUDFInputBox).val().length >= 3 || ($(cs.data.searchUDFInputBox).val().length == 2 && cs.data.searchKey.length == 3)
                || $(cs.data.searchUDFInputBox).val().length == 0){
          var params = JSON.stringify({
            'selDataSet': $('#ln_topmenu_dataset').attr("dataSetkey"),
            'selectedClassification' : $(cs.data.udfClassification).val(),
            'searchKey' : $(cs.data.searchUDFInputBox).val().length >= 3 ? $(cs.data.searchUDFInputBox).val() : "",
            'availableDataRequred' : false
          });
          cs.data.searchKey = $.parseJSON(params).searchKey;
          LN.ajaxPostJSON($(cs.data.availableDiv).parent(), LN.getContextPath() + cs.data.getGroupsUDFsURL, params, cs.searchJsTreeCallBack, cs);
        }
      },1000);
    });
  },
  searchJsTreeCallBack : function(cs, data){
    var availableJsTree = $(cs.data.availableDiv);
    availableJsTree.jstree("destroy");
    cs.setupAvailableJsTree(cs, data, $(cs.data.availableDiv));
  },
  //Method to move nodes from left to right
  registerMoveRightButton : function(){
    var cs = this;
    $(cs.data.udfMoveRightBtn).click(function(e) {
      //LN.showAjaxIndicator($(cs.data.availableDiv).parent());
      cs.moveNodesToRight(cs.data.availableDiv, cs.data.selectedDiv, $(cs.data.udfClassification).val(), cs);
    });
  },
  //Method to move node right to left
  registerMoveLeftButton : function(){
    var cs = this;
    $(cs.data.udfMoveLeftBtn).click(function(e){
      //LN.showAjaxIndicator($(cs.data.availableDiv).parent());
      cs.moveNodesToLeft(cs.data.availableDiv, cs.data.selectedDiv, $(cs.data.udfClassification).val(), cs);
    });
  },
  /*
   * Method will get called when user changes dataset
   */
  onDataSetChangeProviderUDF : function(cs){
    var params = JSON.stringify({
      'selDataSet' : $('#ln_topmenu_dataset').attr("dataSetkey"),
      'selectedClassification' : $(cs.data.udfClassification).val(),
      'searchKey' : $(cs.data.searchUDFInputBox).val(),
      'availableDataRequred' : true
    });

    LN.ajaxPostJSON($(cs.data.availableDiv).parent(), LN.getContextPath() + cs.data.getGroupsUDFsURL, params, cs.dataSetChangeCallBack, cs); 
  },
  
  /**
   * Method would get fired when selectAll check box are changed 
   */
  onChangeOfSelectAllBox : function(){
    var cs = this;
    //Left panel
    $(cs.data.udfSelectAllAvailableCheckBox).on('change', function() {
      if($(cs.data.udfSelectAllAvailableCheckBox).is(':checked')){
        $(cs.data.availableDiv).jstree("select_all");   
      }
      else{
        $(cs.data.availableDiv).jstree("deselect_all");
      }
    });
    //Right panel
    $(cs.data.udfSelectAllSelectedCheckBox).on('change', function() {
      if($(cs.data.udfSelectAllSelectedCheckBox).is(':checked')){
        $(cs.data.selectedDiv).jstree("select_all");
      } 
      else{
        $(cs.data.selectedDiv).jstree("deselect_all");
      }
    });
  }
});