Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.trim = function() {
- return this.replace(/^\s+|\s+$/g,"");
- }
- Splunk.namespace("Module");
- Splunk.Module.MultiLineInputter = $.klass(Splunk.Module , {
- initialize: function($super, container) {
- $super(container);
- this.logger = Splunk.Logger.getLogger("MultiLineInputter.js");
- this.messenger = Splunk.Messenger.System.getInstance();
- this._handleParams();
- try {
- this.innerDiv = $(".DynamicElementContainer", this.container).get(0);
- this.outerDiv = $(".DynamicInputContainer", this.container);
- this.mods = $("div").filter( function() {
- return this.id.match(/MultiLineInputter+/);
- });
- this._initForm();
- } catch(e) {
- alert(e);
- return;
- }
- },
- /**
- * consider moving to getModifiedContext()
- */
- prepareContext: function() {
- var test = this.outerDiv.find('.DynamicInputRow');
- var texts = [];
- var arry = [];
- this.mods.each(function() {
- console.info($(this).attr("id"));
- console.info($(this).toSource());
- if(!texts) {
- texts = $(this).find('.DynamicInputRow');
- console.info("texts: " + texts.toSource());
- }
- else {
- texts.concat($(this).find('.DynamicInputRow'));
- console.info("texts: " + texts.toSource());
- }
- });
- /**
- * @return {Context}
- */
- var context = this.getContext();
- var search = context.get("search")
- var texts = this.outerDiv.find('.DynamicInputRow');
- var funnelstage = 1;
- var prevstage = [];
- var searches = [];
- var quote = "\"";
- // where expression syntax: AND OR NOT XOR < > <= >= != = == LIKE
- for (var index =0; index < texts.length; index++) {
- var value = texts[index];
- // module has a value
- if ($(value).find('.DynamicText').val()) {
- //first of possibly several filters
- if (funnelstage == 1) {
- //console.info("text?");
- //console.info("setOrder is Last, funnelstage is 1");
- searches.push(this.filterName + '==' + quote + $(value).find('.DynamicText').val().trim() + quote);
- //prevstage = $(value).find('.DynamicText').val().trim();
- funnelstage+=1;
- } else {
- //console.info("text?");
- //console.info("OR " + $(value).find('.DynamicText').val().trim());
- searches.push(' OR ' + this.filterName + '==' + quote + $(value).find('.DynamicText').val().trim() + quote);
- //prevstage = prevstage + ' AND ' + $(value).find('.DynamicText').val().trim();
- funnelstage +=1;
- }
- }
- }
- if (searches.length) {
- console.info(searches.length);
- var replacementSearch = search.getBaseSearch();
- if(replacementSearch.search("where") >= 0) {
- searches.unshift(' OR ');
- } else {
- searches.unshift(' | where ');
- }
- replacementSearch = replacementSearch + " " + searches.join(" ");
- search.abandonJob();
- console.info(replacementSearch);
- search.setBaseSearch(replacementSearch);
- context.set("search", search);
- }
- return context;
- },
- /**
- * overrides
- */
- getModifiedContext: function() {
- //alert("getModifiedContext"+this.toSource());
- return this.prepareContext();
- },
- onContextChange: function() {
- //alert("onContextChange"+this.toSource());
- //this.pushContextToChildren(this.prepareContext());
- this.applyContext(this.prepareContext());
- //var root = this.getRootAncestor();
- //root.getContext().setContext(this.prepareContext());
- },
- updateButtons: function() {
- texts = this.outerDiv.find('.DynamicInputRow');
- texts.find('.DynamicDelete').show();
- texts.find('.DynamicBelow').show();
- texts.removeClass('last');
- texts.last().addClass('last');
- if ( texts.length == 1) {
- texts.find('.DynamicDelete').hide();
- } else if ( texts.length == this.limit) {
- texts.find('.DynamicBelow').hide();
- }
- },
- /**
- * event handlers
- */
- onKeyDown: function(event) {
- if (event.keyCode == 13) {
- this.pushSearch();
- }
- },
- pushSearch: function() {
- //this.prepareContext();
- //this.passContextToParent(this.prepareContext());
- var root = this.getRootAncestor();
- root.pushContextToChildren();
- },
- onClickInsertBelow: function(event) {
- $DynamicInputRow = $(event.target).closest('.DynamicInputRow');
- $DynamicInputRow.clone(true, true).insertAfter($DynamicInputRow).find('input').attr("value", '');
- this.updateButtons();
- },
- onClickRemove: function(event) {
- $(event.target).closest('.DynamicInputRow').remove();
- this.updateButtons();
- },
- /*
- * private functions
- */
- _isNum: function(string) {
- /**
- * @private
- */
- var regex = /^\d+$/;
- return regex.test(string);
- },
- _isValidCSV: function(string) {
- /**
- * @private
- */
- var regex = /^[a-zA-Z0-9\_\-\"\'\,]+$/;
- if (!regex.test(string)) {
- return false;
- }
- if (!string.split(",")) {
- return false;
- }
- },
- _handleParams: function() {
- /**
- * @private
- */
- try {
- if (!this._isNum(this.getParam('limit'))) {
- this.logger.info(this.moduleType, "Parameter 'limit' is non-numeric; numeric value is required");
- this.messenger.send('warn', 'splunk.search', _(this.moduleType +": 'limit' must be numeric value."));
- }
- this.limit = this.getParam('limit');
- if (!this._isNum(this.getParam('displayAtInit'))) {
- this.logger.info(this.moduleType, "Parameter 'displayAtInit' is non-numeric; numeric value is required");
- this.messenger.send('warn', 'splunk.search', _(this.moduleType +": 'displayAtInit' must be numeric value."));
- }
- this.displayAtInit = this.getParam('displayAtInit');
- this.filterName = this.getParam('filterName');
- //this.setOrder = this.getParam('setOrder');
- //this.setName = this.getParam('setName');
- } catch(e) {
- alert(e);
- return;
- }
- },
- _initForm: function() {
- this.outerDiv.find(".DynamicBelow").click(this.onClickInsertBelow.bind(this));
- this.outerDiv.find(".DynamicDelete").click(this.onClickRemove.bind(this));
- this.outerDiv.find(".DynamicText").keypress(this.onKeyDown.bind(this));
- this.outerDiv.find("#SubmitFunnel").click(this.pushSearch.bind(this));
- /**
- * @private
- */
- for (x = 1; x < this.displayAtInit; x++) {
- $(".DynamicBelow").last().click();
- }
- },
- _isLimit: function(current) {
- /**
- * @private
- * @param current {Integer} the current iterator
- * @return undefined
- */
- if (current > this.limit) {
- return true;
- }
- return false;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement