Last active
December 20, 2015 10:31
-
-
Save marpontes/6116292 to your computer and use it in GitHub Desktop.
This is basically an object created to make it easy to handle the multi-valued MDX-formatted inputs being passed down to another artifacts, from a Pentaho CDE Dashboard to an Analyzer Report, for example.
If the parameter is mdx-like, is multi-valued and the first is "All", then it fits!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is basically an object created to make it easy to | |
* Handle the multi-valued MDX-formatted inputs being passed down to | |
* another artifacts, from a CDE Dashboard to an Analyzer Report, for example | |
* | |
* by @marpontes | |
* | |
* Ex | |
* <div id="SupplierHolder"> | |
* <select multiselect> | |
* <option value="[Supplier].[All]">[Supplier].[All]</option> | |
* <option value="[Supplier].[SUP A]">[Supplier].[SUP A]</option> | |
* <option value="[Supplier].[SUP B]">[Supplier].[SUP B]</option> | |
* <option value="[Supplier].[SUP C]">[Supplier].[SUP C]</option> | |
* </select> | |
* </div> | |
* | |
* <script> | |
* var cd = new DrillParamControl({ | |
* selectDivId: "SupplierHolder", | |
* targetParamName : "SupplierParameter" | |
* }); | |
* </script> | |
* | |
* | |
* ###### If [All],[A] and [C] selected: | |
* cd.getFormattedParam() -> | |
"SupplierParameter=SUP A&SupplierParameter=SUP B&SupplierParameter=SUP C" | |
* | |
* ###### With only [All] selected: | |
* cd.getFormattedParam() -> | |
"SupplierParameter=SUP A&SupplierParameter=SUP B&SupplierParameter=SUP C" | |
* | |
* ###### If [A] and [C] selected : | |
* cd.getFormattedParam() -> | |
* "SupplierParameter=SUP A&SupplierParameter=SUP C" | |
* | |
* | |
* On CDE | |
* 1. Create the Layout holder Item [e.g. SupplierHolder]; | |
* 2. Create your SelectMulti component; | |
* 3. Innitialize the object on a Script component | |
* e.g.: var cd = new DrillParamControl({ selectDivId: "SupplierHolder", | |
targetParamName: "SupplierParameter" }); | |
* 4. Call cd.updateLists(); on postExecution on the selectMultiComponent. | |
* 5. When calling urls, simply add cd.getFormattedParam() and voilà. | |
* | |
*/ | |
var DrillParamControl = function( options ){ | |
if (typeof options != "object") { | |
throw TypeError; | |
} | |
if (!options.selectDivId || !options.targetParamName ) { | |
throw TypeError; | |
} | |
var _div = options.selectDivId; | |
var _paramName = options.targetParamName; | |
var _labelElement = options.labelElement; | |
var _selector = "#" + _div + " select"; | |
var _selectorLabel = "#" + _labelElement; | |
var _selected = $(_selector).val(); | |
var _selectedLabels = $(_selector+" option:selected").map(function() {return " "+$(this).html();}).get(); | |
/* | |
* Vars with all the available options. Values and Labels | |
*/ | |
var _optArr = $(_selector).children().map(function() {return $(this).val();}).get(); | |
var _allLabels = $(_selector+" option").map(function() {return " "+$(this).html();}).get(); | |
/* | |
* Removes [x].[Tudo] and keeps it for comparison | |
* as well as the first Label "Tudo" | |
*/ | |
var _first = _optArr.splice(0,1).toString(); | |
var _firstLabel = _allLabels.splice(0,1).toString(); | |
/* | |
* This method has to be called everytime that the selection changes | |
* and/or a component/link is about to be called/clicked. | |
* It's responsible for updating the internal variables that stores: | |
* - The list of all values available | |
* - The list of all labels available | |
* - The the first value (usually [...].[All]) of options | |
* - The the first value (usually "All") of Labels | |
* - The list of all selected values | |
* - The list of all selected labels | |
*/ | |
var _updateLists = function(){ | |
_optArr = $(_selector).children().map(function() {return $(this).val();}).get(); | |
_allLabels = $(_selector+" option").map(function() {return " "+$(this).html();}).get(); | |
_first = _optArr.splice(0,1).toString(); | |
_firstLabel = _allLabels.splice(0,1).toString(); | |
_updateSelected(); | |
}; | |
/* | |
* Checks the component for selected values | |
* and updates the label that tells user for selected values | |
*/ | |
var _updateSelected = function(){ | |
_selected = $(_selector).val(); | |
_selectedLabels = $(_selector+" option:selected").map(function() {return " "+$(this).html();}).get(); | |
}; | |
/* | |
* Outputs to the div, the list of selected labels | |
*/ | |
var _printSelectionLabel = function(){ | |
console.log("Print Selection called"); | |
console.log(_getSelectedLabels()); | |
$(_selectorLabel).html(_getSelectedLabels().toString()); | |
}; | |
/* | |
* Checks the selected values array for [x].[Tudo], for example | |
*/ | |
var _existsAllInSelected = function(){ | |
_updateSelected(); | |
return _selected===null ? false : ( _selected.indexOf(_first)>-1 ); | |
}; | |
/* | |
* Chooses which array to use. If allmember-element is selected | |
* then it returns the array with all possible values. | |
*/ | |
var _getFinalArray = function(){ | |
return _existsAllInSelected() ? _optArr : _selected; | |
}; | |
var _getSelectedLabels = function(){ | |
return _existsAllInSelected() ? _firstLabel : _selectedLabels; | |
}; | |
/* | |
* Returns only the current member names from array of mdx members. | |
*/ | |
var _stripMdxFormat = function( arrMDX ){ | |
var arrOut = arrMDX.slice(0); // just a clone | |
$.each(arrOut,function(key, val){ | |
arrOut[key] = val.substring( val.lastIndexOf("[")+1 , val.lastIndexOf("]") ); | |
}); | |
return arrOut; | |
}; | |
/* | |
* Public Methods | |
*/ | |
return{ | |
/* | |
* Returns formatted param. | |
*/ | |
getFormattedParam : function(){ | |
var returnArray = _getFinalArray(); | |
if(returnArray === null) | |
return null; | |
returnArray = _stripMdxFormat(returnArray); | |
console.log(_paramName +"="+ returnArray.toString().replace(/\,/g,'&'+_paramName+'=')); | |
return _paramName +"="+ returnArray.toString().replace(/\,/g,'&'+_paramName+'='); | |
}, | |
/* | |
* Returns the selected params to show | |
* to the end user | |
*/ | |
getFormattedLabels : function(){ | |
return _getSelectedLabels(); | |
}, | |
/* | |
* Updates the param name | |
*/ | |
setTargetParamName : function(newName){ | |
_paramName = newName; | |
}, | |
getFirstElement : function(){ | |
return _first; | |
}, | |
getAllElementsArray : function (){ | |
return _optArr; | |
}, | |
getSelectedElementsArray : function(){ | |
return _selected; | |
}, | |
updateLists : function(paramPrintSelection){ | |
paramPrintSelection = paramPrintSelection || false; | |
_updateLists(); | |
if(paramPrintSelection) | |
_printSelectionLabel(); | |
}, | |
printSelectionLabel : function(){ | |
_printSelectionLabel(); | |
} | |
} | |
};//Object DrillParamControl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment