Advertisement
wawiwa

MultiLineInputter.js

Feb 8th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String.prototype.trim = function() {
  2.         return this.replace(/^\s+|\s+$/g,"");
  3. }
  4. Splunk.namespace("Module");
  5. Splunk.Module.MultiLineInputter = $.klass(Splunk.Module , {
  6.  
  7.     initialize: function($super, container) {
  8.         $super(container);
  9.        
  10.         this.logger = Splunk.Logger.getLogger("MultiLineInputter.js");
  11.         this.messenger = Splunk.Messenger.System.getInstance();
  12.        
  13.         this._handleParams();
  14.  
  15.         try {
  16.             this.innerDiv = $(".DynamicElementContainer", this.container).get(0);
  17.             this.outerDiv = $(".DynamicInputContainer", this.container);
  18.    
  19.             this.mods = $("div").filter( function() {
  20.                 return this.id.match(/MultiLineInputter+/);
  21.             });
  22.            
  23.             this._initForm();
  24.         } catch(e) {
  25.             alert(e);
  26.             return;
  27.         }
  28.     },
  29.    
  30.     /**
  31.      * consider moving to getModifiedContext()
  32.      */
  33.  
  34.     prepareContext: function() {
  35.        
  36.         var test = this.outerDiv.find('.DynamicInputRow');
  37.  
  38.         var texts = [];
  39.         var arry = [];
  40.         this.mods.each(function() {
  41.             console.info($(this).attr("id"));
  42.             console.info($(this).toSource());
  43.  
  44.             if(!texts) {
  45.                 texts = $(this).find('.DynamicInputRow');
  46.                 console.info("texts: " + texts.toSource());
  47.             }
  48.             else {
  49.                 texts.concat($(this).find('.DynamicInputRow'));
  50.                 console.info("texts: " + texts.toSource());
  51.             }
  52.  
  53.         });
  54.  
  55.        
  56.        
  57.         /**
  58.          * @return {Context}
  59.          */
  60.         var context = this.getContext();
  61.         var search  = context.get("search")
  62.         var texts = this.outerDiv.find('.DynamicInputRow');
  63.         var funnelstage = 1;
  64.         var prevstage = [];
  65.         var searches = [];
  66.         var quote = "\"";
  67.        
  68.    
  69.         // where expression syntax: AND OR NOT XOR < > <= >= != = == LIKE
  70.         for (var index =0; index < texts.length; index++) {
  71.             var value = texts[index];
  72.             // module has a value
  73.             if ($(value).find('.DynamicText').val()) {
  74.                 //first of possibly several filters
  75.                 if (funnelstage == 1) {
  76.                     //console.info("text?");
  77.                     //console.info("setOrder is Last, funnelstage is 1");
  78.                     searches.push(this.filterName + '==' + quote + $(value).find('.DynamicText').val().trim() + quote);
  79.                     //prevstage = $(value).find('.DynamicText').val().trim();
  80.                     funnelstage+=1;
  81.                 } else {
  82.                    
  83.                     //console.info("text?");
  84.                     //console.info("OR " + $(value).find('.DynamicText').val().trim());
  85.                     searches.push(' OR ' + this.filterName + '==' + quote + $(value).find('.DynamicText').val().trim() + quote);
  86.                     //prevstage = prevstage + ' AND ' + $(value).find('.DynamicText').val().trim();
  87.  
  88.                     funnelstage +=1;
  89.                 }
  90.             }
  91.         }
  92.  
  93.    
  94.         if (searches.length) {
  95.             console.info(searches.length);
  96.             var replacementSearch = search.getBaseSearch();
  97.             if(replacementSearch.search("where") >= 0) {
  98.                 searches.unshift(' OR ');
  99.             } else {
  100.                 searches.unshift(' | where ');
  101.             }
  102.            
  103.             replacementSearch = replacementSearch + " " + searches.join(" ");
  104.             search.abandonJob();
  105.             console.info(replacementSearch);
  106.             search.setBaseSearch(replacementSearch);
  107.             context.set("search", search);
  108.         }
  109.  
  110.         return context;    
  111.     },
  112.  
  113.     /**
  114.      * overrides
  115.      */
  116.  
  117.     getModifiedContext: function() {
  118.         //alert("getModifiedContext"+this.toSource());
  119.         return this.prepareContext();
  120.     },
  121.  
  122.     onContextChange: function() {
  123.         //alert("onContextChange"+this.toSource());
  124.         //this.pushContextToChildren(this.prepareContext());
  125.         this.applyContext(this.prepareContext());
  126.         //var root = this.getRootAncestor();
  127.         //root.getContext().setContext(this.prepareContext());
  128.     },
  129.  
  130.     updateButtons: function() {
  131.         texts = this.outerDiv.find('.DynamicInputRow');
  132.         texts.find('.DynamicDelete').show();
  133.         texts.find('.DynamicBelow').show();
  134.         texts.removeClass('last');
  135.         texts.last().addClass('last');
  136.        
  137.         if ( texts.length == 1) {
  138.                 texts.find('.DynamicDelete').hide();
  139.         } else if ( texts.length == this.limit) {
  140.                 texts.find('.DynamicBelow').hide();
  141.         }
  142.     },
  143.  
  144.    
  145.     /**
  146.      * event handlers
  147.      */
  148.  
  149.     onKeyDown: function(event) {
  150.         if (event.keyCode == 13) {
  151.             this.pushSearch();
  152.         }
  153.     },
  154.     pushSearch: function() {
  155.         //this.prepareContext();
  156.         //this.passContextToParent(this.prepareContext());
  157.         var root = this.getRootAncestor();
  158.         root.pushContextToChildren();
  159.     },
  160.    
  161.     onClickInsertBelow: function(event) {
  162.         $DynamicInputRow = $(event.target).closest('.DynamicInputRow');
  163.         $DynamicInputRow.clone(true, true).insertAfter($DynamicInputRow).find('input').attr("value", '');
  164.         this.updateButtons();
  165.     },
  166.     onClickRemove: function(event) {
  167.         $(event.target).closest('.DynamicInputRow').remove();
  168.         this.updateButtons();
  169.     },
  170.  
  171.     /*
  172.      * private functions
  173.      */
  174.  
  175.     _isNum: function(string) {
  176.         /**
  177.          * @private
  178.          */
  179.         var regex = /^\d+$/;
  180.         return regex.test(string);
  181.     },
  182.     _isValidCSV: function(string) {
  183.         /**
  184.          * @private
  185.          */
  186.         var regex = /^[a-zA-Z0-9\_\-\"\'\,]+$/;
  187.         if (!regex.test(string)) {
  188.             return false;
  189.         }
  190.         if (!string.split(",")) {
  191.             return false;
  192.         }
  193.     },
  194.     _handleParams: function() {
  195.         /**
  196.          * @private
  197.          */
  198.         try {
  199.             if (!this._isNum(this.getParam('limit'))) {
  200.                 this.logger.info(this.moduleType, "Parameter 'limit' is non-numeric; numeric value is required");
  201.                 this.messenger.send('warn', 'splunk.search', _(this.moduleType +": 'limit' must be numeric value."));
  202.             }
  203.             this.limit = this.getParam('limit');
  204.             if (!this._isNum(this.getParam('displayAtInit'))) {
  205.                 this.logger.info(this.moduleType, "Parameter 'displayAtInit' is non-numeric; numeric value is required");
  206.                 this.messenger.send('warn', 'splunk.search', _(this.moduleType +": 'displayAtInit' must be numeric value."));
  207.             }
  208.             this.displayAtInit = this.getParam('displayAtInit');    
  209.             this.filterName = this.getParam('filterName');
  210.             //this.setOrder = this.getParam('setOrder');
  211.             //this.setName = this.getParam('setName');
  212.            
  213.         } catch(e) {
  214.             alert(e);
  215.             return;
  216.         }
  217.     },
  218.    
  219.     _initForm: function() {
  220.         this.outerDiv.find(".DynamicBelow").click(this.onClickInsertBelow.bind(this));
  221.         this.outerDiv.find(".DynamicDelete").click(this.onClickRemove.bind(this));
  222.         this.outerDiv.find(".DynamicText").keypress(this.onKeyDown.bind(this));
  223.         this.outerDiv.find("#SubmitFunnel").click(this.pushSearch.bind(this));
  224.         /**
  225.          * @private
  226.          */
  227.         for (x = 1; x < this.displayAtInit; x++) {
  228.             $(".DynamicBelow").last().click();
  229.         }
  230.     },
  231.    
  232.     _isLimit: function(current) {
  233.         /**
  234.          * @private
  235.          * @param current {Integer} the current iterator
  236.          * @return undefined
  237.          */
  238.         if (current > this.limit) {
  239.             return true;
  240.         }
  241.         return false;
  242.     }
  243. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement